Re: [webkit-dev] [JavaScriptCore] When to JSValueProtect?

2013-12-09 Thread Oliver Hunt

On Dec 7, 2013, at 12:15 PM, Alexei Sholik alcosho...@gmail.com wrote:

 The garbage collector scans the C stack.
 
 Hm, let me make sure I understand this correctly.
 ...
 I'm asking because I've never previously dealt with a library that scans the 
 host application's stack. So it sounds pretty incredible to me.

Every JSC vm will do a full conservative scan of the in use portion of the 
stack for every thread in your application whenever it needs to GC.  Anything 
that looks like it could be a pointer to a gc value is treated as though it is 
_definitely_ a GC value and marked accordingly.

 
 Thanks!

 
 
 On Sat, Dec 7, 2013 at 9:37 PM, Geoffrey Garen gga...@apple.com wrote:
  At this point, the code is not inside the JS stack, so is it possible for 
  an object to be collected between the calls to JSObjectMake and 
  JSObjectCallAsFunction?
 
 The garbage collector scans the C stack.
 
 Geoff
 
 
 
 -- 
 Best regards
 Alexei Sholik
 ___
 webkit-dev mailing list
 webkit-dev@lists.webkit.org
 https://lists.webkit.org/mailman/listinfo/webkit-dev

___
webkit-dev mailing list
webkit-dev@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-dev


Re: [webkit-dev] [JavaScriptCore] When to JSValueProtect?

2013-12-08 Thread Alexei Sholik
Hi Geoffrey,

 Therefore, if I'm not immediately returning the created object, it might
 get cleaned up prematurely.



 Because the GC scans the stack, and the value you just created is on the
 stack, it won’t be garbage collected right away.


What about the case when the creation of an object originates from C++
land? For example, if I tap on a view and it needs to invoke a JS callback.
I'm creating an object to pass to JSEvaluateScript or as an argument to
JSObjectCallAsFunction.

At this point, the code is not inside the JS stack, so is it possible for
an object to be collected between the calls to JSObjectMake and
JSObjectCallAsFunction?

Thanks!


On Sat, Dec 7, 2013 at 8:55 PM, Geoffrey Garen gga...@apple.com wrote:

  My question in short: is it necessary to call JSValueProtect right after
 object/value creation in C++?

 No.

  I don't know the semantics of the GC, but I'm assuming that almost any
 call to a JSC function that takes a context may run a garbage collection
 cycle.

 Yes.

  Therefore, if I'm not immediately returning the created object, it might
 get cleaned up prematurely.

 Because the GC scans the stack, and the value you just created is on the
 stack, it won’t be garbage collected right away.

  Could you share some advice on the proper usage of JSValueProtect and on
 keeping objects from going away unexpectedly?

 If you store a JSValueRef or JSObjectRef into a C/C++ heap object, then
 you should JSValueProtect the JSValueRef/JSObjectRef, since your reference
 to it will outlast the pointer to it on the stack.

 Use JSValueProtect/JSValueUnprotect just like you would use any other
 reference-counting API — such as CFRetain/CFRelease, NSObject
 retain/release, or IUnknown AddRef/RemoveRef — with the added feature that
 you do not need to retain local variables.

 Geoff




-- 
Best regards
Alexei Sholik
___
webkit-dev mailing list
webkit-dev@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-dev


Re: [webkit-dev] [JavaScriptCore] When to JSValueProtect?

2013-12-08 Thread Geoffrey Garen
 At this point, the code is not inside the JS stack, so is it possible for an 
 object to be collected between the calls to JSObjectMake and 
 JSObjectCallAsFunction?

The garbage collector scans the C stack.

Geoff
___
webkit-dev mailing list
webkit-dev@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-dev


Re: [webkit-dev] [JavaScriptCore] When to JSValueProtect?

2013-12-08 Thread Geoffrey Garen
 My question in short: is it necessary to call JSValueProtect right after 
 object/value creation in C++?

No.

 I don't know the semantics of the GC, but I'm assuming that almost any call 
 to a JSC function that takes a context may run a garbage collection cycle.

Yes.

 Therefore, if I'm not immediately returning the created object, it might get 
 cleaned up prematurely.

Because the GC scans the stack, and the value you just created is on the stack, 
it won’t be garbage collected right away.

 Could you share some advice on the proper usage of JSValueProtect and on 
 keeping objects from going away unexpectedly?

If you store a JSValueRef or JSObjectRef into a C/C++ heap object, then you 
should JSValueProtect the JSValueRef/JSObjectRef, since your reference to it 
will outlast the pointer to it on the stack.

Use JSValueProtect/JSValueUnprotect just like you would use any other 
reference-counting API — such as CFRetain/CFRelease, NSObject retain/release, 
or IUnknown AddRef/RemoveRef — with the added feature that you do not need to 
retain local variables.

Geoff
___
webkit-dev mailing list
webkit-dev@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-dev


Re: [webkit-dev] [JavaScriptCore] When to JSValueProtect?

2013-12-08 Thread Geoffrey Garen
 Will JSC scan the current stack which arg1 and arg2 are on?

Yes.

 Will it scan the stack even further back?

Yes.

Geoff
___
webkit-dev mailing list
webkit-dev@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-dev


[webkit-dev] [JavaScriptCore] When to JSValueProtect?

2013-12-07 Thread Alexei Sholik
Hi,

My question in short: is it necessary to call JSValueProtect right after
object/value creation in C++? I don't know the semantics of the GC, but I'm
assuming that almost any call to a JSC function that takes a context may
run a garbage collection cycle. Therefore, if I'm not immediately returning
the created object, it might get cleaned up prematurely.

Could you share some advice on the proper usage of JSValueProtect and on
keeping objects from going away unexpectedly?

Some background info:

I'm experiencing intermittent bugs in my C++ to JS binding, I'm only
guessing that GC might be related to that. What I'm seeing is that objects
created in C++ are occasionally missing their properties.

In one example, there is a single code path from C++ to JS that takes a
native object and creates a corresponding JS object using appropriate
JSClassRef (which is prepopulated with some methods).

In the JS land, there is always the same method called on the object (the
one that has just been created in C++ land with the appropriate
JSClassRef). 9 out of 10 times it works fine, but every once in a while I
get an exception undefined is not a function, i.e. the method on the
object is missing.

The way I'm linking native and JS objects is by keeping an associative
array of their pointers, so that I can get back to the native object in the
C++ callback called from JS.

-- 
Best regards
Alexei Sholik
___
webkit-dev mailing list
webkit-dev@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-dev