[webkit-dev] JavaScriptCore question
Oliver Hunt
oliver at apple.com
Fri Dec 16 09:13:25 PST 2011
On Dec 16, 2011, at 9:05 AM, Rolando Abarca wrote:
> Hi all,
>
> I'm using JavaScriptCore for scripting purposes (isolated from webkit), and I'm encountering the following problem:
>
> I would like to store a reference to an anonymous function, and later to be able to call it as a callback, something like this:
>
> ---javascript
> node.schedule("some name", function () { ... });
> ---
>
> What I'm doing right now is in the definition of node.schedule, I store the context and the function (as a JSValueRef), then when I need to call code in there, I'm doing this:
>
> JSObjectRef func = JSValueToObject(storedContext, storedCallback, NULL);
> JSObjectCallAsFunction(cb->context, func, storedObject, 0, NULL, NULL)
>
> It doesn't matter how I store the context/function, it always crashes when trying to either convert the value to object or call the object as function. At first I thought that the function was being garbage collected, but even after protecting it, I still got the crash.
>
> My guess is that the context is not the right one, so another question is: what context should I be passing here?
This looks like you're storing the JSContextRef you receive when your callback is called. That's incorrect as a given JSContextRef is only guaranteed to be valid within the callback. All your functions should be from the same initial JSGlobalContextRef you created when initialising your environment, and you should just be using that GlobalContext rather than any per instance context.
You should also be protecting the function with JSValueProtect.
Hopefully this helps.
>
> What would be the right way to do this?
> Thanks,
> --
> Rolando Abarca M.
> http://rolando.cl
> Phones: +1 (415) 655-1041
> +56 (2) 581-4591
>
> _______________________________________________
> webkit-dev mailing list
> webkit-dev at lists.webkit.org
> http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20111216/ad3e995f/attachment.html>
More information about the webkit-dev
mailing list