[webkit-dev] JavaScriptCore: Accessing things created in C from script

Thomas Gutteridge thomas.gutteridge at gmail.com
Thu Dec 11 03:33:20 PST 2008


Great, it works like a dream!

Thank you Cameron, Oliver. I haven't a lot of experience with
JavaScript, otherwise that may have been more obvious :)


2008/12/11 Oliver Hunt <oliver at apple.com>:
> On Dec 11, 2008, at 2:27 AM, Thomas Gutteridge wrote:
>
>> Hello
>>
>> Can you help me? I'm using WebKit's JavaScriptCore from within an
>> application to allow some logic to be implemented in script and
>> therefore modified without recompilation. Although, I can create
>> JavaScript functions and objects using the C API, I can't see how to
>> give them names that can be used to reference them from script.
>>
>> For example, I'm trying to use JSObjectMakeFunctionWithCallback to
>> create a JavaScript function with a native implementation. Crucially,
>> I'd like to then invoke this function from a script. However, the
>> "name" parameter is apparently only "used when converting the function
>> to string". The name I give is undefined if I use it in a script.
>>
>> Is what I'm trying to do possible?
>>
>
> The "name" property that you're specifying is what will be used if you want
> to do myFunction.toString().  Accessing a function by name requires the
> function (or other object) being on an object in scope.
>
> The easiest way to achieve this is by putting it on the global object.  This
> will let you call it in the same way people use 'alert', 'document', etc on
> js in a web page.
>
> Something akin to the following should work (assuming jsFunction is your JSC
> function wrapper, and context is your JSC context):
> JSValueRef exception = 0;
> JSObjectRef global = JSContextGetGlobalObject(context);
> JSObjectSetProperty(context, global,
> JSStringCreateWithUTF8CString("myCFunction"), jsFunction,
> kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, &exception);
> if (exception)
>    printf("Argh! an exception!!!!");
>
> Now your function will be available in JS as "myCFunction" on the global
> object.
>
> --Oliver
>
>
>


More information about the webkit-dev mailing list