JSObjectMakeFunction question

View: New views
7 Messages — Rating Filter:   Alert me  

JSObjectMakeFunction question

by wootton :: Rate this Message:

| View Threaded | Show Only this Message

Hi all,

I'm attempting to use the function JSObjectMakeFunction described here:

http://gemma.apple.com/documentation/Carbon/Reference/WebKit_JavaScriptCore_Ref/JSObjectRef/index.html

I would like to call a JavaScript function from C.  I assume the
following is required:

1. Create a function using JSObjectMakeFunction.
2. Add it as a property of the Window JSObjectRef.
3. Call it using JSObjectCallAsFunction.

However JSObjectMakeFunction has the following signature:

JSObjectRef JSObjectMakeFunction(
    JSContextRef ctx,
    JSStringRef name,
    unsigned parameterCount,
    const JSStringRef parameterNames[],
    JSStringRef body,
    JSStringRef sourceURL,
    int startingLineNumber,
    JSValueRef *exception);

My question is about the parameters to this function:

JSStringRef body - Since I wish to call a JavaScript function from C,
why would I specify the body of the function.  Surely this would be
implemented by the JavaScript developer?  I don't understand this
parameter in the context of calling a user defined JavaScript function
from within C.

Can anyone help?

Cheers,
Jack
_______________________________________________
webkit-dev mailing list
webkit-dev@...
http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev

Re: JSObjectMakeFunction question

by Kalle Vahlman :: Rate this Message:

| View Threaded | Show Only this Message

2009/6/1 Jack Wootton <jackwootton@...>:
> Hi all,

Hi!

> I'm attempting to use the function JSObjectMakeFunction described here:
>
> http://gemma.apple.com/documentation/Carbon/Reference/WebKit_JavaScriptCore_Ref/JSObjectRef/index.html
>
> I would like to call a JavaScript function from C.  I assume the
> following is required:
>
> 1. Create a function using JSObjectMakeFunction.
> 2. Add it as a property of the Window JSObjectRef.
> 3. Call it using JSObjectCallAsFunction.

JSObjectMakeFunction creates a JavaScript function *from* C.

To just call a JS function (not create one), all you need to do is get
the function object (most likely JSObjectGetProperty() on the global
object) and then call it with JSObjectCallAsFunction().

--
Kalle Vahlman, zuh@...
Powered by http://movial.com
Interesting stuff at http://sandbox.movial.com
See also http://syslog.movial.fi
_______________________________________________
webkit-dev mailing list
webkit-dev@...
http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev

Re: JSObjectMakeFunction question

by wootton :: Rate this Message:

| View Threaded | Show Only this Message

It seems some casting is required for this though?


JSValueRef JSObjectCallAsFunction(
    JSContextRef ctx,
    JSObjectRef object,
    JSObjectRef thisObject,
    size_t argumentCount,
    const JSValueRef arguments[],
    JSValueRef *exception);


The above function accepts a JSObjectRef object.


However

JSValueRef JSObjectGetProperty(
    JSContextRef ctx,
    JSObjectRef object,
    JSStringRef propertyName,
    JSValueRef *exception);

returns JSValueRef

Looking at the typedefs in APICast.h

typedef const struct OpaqueJSValue* JSValueRef;
typedef struct OpaqueJSValue* JSObjectRef;

If I didn't cast, then wouldn't I be attempting to pass a const value
into a function that accepts a non-const parameter?  It is however
reporting a compilation error because the types don't' match,


On Mon, Jun 1, 2009 at 2:53 PM, Kalle Vahlman <kalle.vahlman@...> wrote:

> 2009/6/1 Jack Wootton <jackwootton@...>:
>> Hi all,
>
> Hi!
>
>> I'm attempting to use the function JSObjectMakeFunction described here:
>>
>> http://gemma.apple.com/documentation/Carbon/Reference/WebKit_JavaScriptCore_Ref/JSObjectRef/index.html
>>
>> I would like to call a JavaScript function from C.  I assume the
>> following is required:
>>
>> 1. Create a function using JSObjectMakeFunction.
>> 2. Add it as a property of the Window JSObjectRef.
>> 3. Call it using JSObjectCallAsFunction.
>
> JSObjectMakeFunction creates a JavaScript function *from* C.
>
> To just call a JS function (not create one), all you need to do is get
> the function object (most likely JSObjectGetProperty() on the global
> object) and then call it with JSObjectCallAsFunction().
>
> --
> Kalle Vahlman, zuh@...
> Powered by http://movial.com
> Interesting stuff at http://sandbox.movial.com
> See also http://syslog.movial.fi
>



--
Regards
Jack
_______________________________________________
webkit-dev mailing list
webkit-dev@...
http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev

Re: JSObjectMakeFunction question

by Darin Adler :: Rate this Message:

| View Threaded | Show Only this Message

On Jun 1, 2009, at 8:29 AM, Jack Wootton wrote:

> It seems some casting is required for this though?

Yes, conversion to JSObjectRef is needed, although you don’t have to  
do any casting.

You can call JSValueToObject to turn the value into an object, making  
the appropriate object wrapper for non-object types.

     -- Darin

_______________________________________________
webkit-dev mailing list
webkit-dev@...
http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev

Re: JSObjectMakeFunction question

by Maciej Stachowiak :: Rate this Message:

| View Threaded | Show Only this Message


On Jun 1, 2009, at 8:29 AM, Jack Wootton wrote:

> It seems some casting is required for this though?
>
>
> JSValueRef JSObjectCallAsFunction(

[snip]

> The above function accepts a JSObjectRef object.
>
>
> However
>
> JSValueRef JSObjectGetProperty(

[snip]

>
> returns JSValueRef

Setting aside the actual typedefs (which are our attempt to trick the  
C type system), JavaScript has objects and some primitive values that  
are not objects. So every object is a kind of value, but not all  
values are objects.

JSObjectGetProperty retrieves a property from an object, and it can't  
guarantee whether the result will be an object or a primitive, so it  
returns JSValueRef. If you can be certain the value is in fact an  
object (functions are a kind of object), you can just downcast. If you  
are not certain (for example the property can be defined or altered by  
script), you can use JSValueIsObject to check before casting.

Regards,
Maciej

_______________________________________________
webkit-dev mailing list
webkit-dev@...
http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev

Re: JSObjectMakeFunction question

by Maciej Stachowiak :: Rate this Message:

| View Threaded | Show Only this Message


On Jun 1, 2009, at 8:55 AM, Darin Adler wrote:

> On Jun 1, 2009, at 8:29 AM, Jack Wootton wrote:
>
>> It seems some casting is required for this though?
>
> Yes, conversion to JSObjectRef is needed, although you don’t have to  
> do any casting.
>
> You can call JSValueToObject to turn the value into an object,  
> making the appropriate object wrapper for non-object types.

That's a possible alternative to my suggestion (in other email)l. In  
this particular case, it would be fruitless to attempt to call the  
object wrapper for a primitive value as a function, so it's may be  
better idea to do the check up front.

Regards,
Maciej

_______________________________________________
webkit-dev mailing list
webkit-dev@...
http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev

Re: JSObjectMakeFunction question

by Vemulapally :: Rate this Message:

| View Threaded | Show Only this Message

Hi Darin,
   I am trying to call JS callback function as attached in the sample file. I am launchig my JavaScript Application using GtkLauncher. My JavaScript application contains the JavaScript Callback function and the code for periodic Ajax request and processing. From  C++ application I am calling the JavaScript Callback function on the event asynchronously but while calling the JS callback function webkit is gettig crashing especially when the webkit is busy with Ajaxrequest and processing. I am suspecting context switching is not happening. could you please suggest me is it synchronization problem or problem with JSC ( context switching is not handling ) ?

Could you please suggest me why it is crashing ?


Thanks,
Anil.V
cpp2js_notify.cpp
Darin Adler wrote:
On Jun 1, 2009, at 8:29 AM, Jack Wootton wrote:

> It seems some casting is required for this though?

Yes, conversion to JSObjectRef is needed, although you don’t have to  
do any casting.

You can call JSValueToObject to turn the value into an object, making  
the appropriate object wrapper for non-object types.

     -- Darin

_______________________________________________
webkit-dev mailing list
webkit-dev@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev