[webkit-help] Calling JS Function From C

demallien demallien at me.com
Thu Jan 27 07:59:11 PST 2011


The first thing you need to do is get a reference to the Javascript function that you want to call.  This is the order of things you have to do:

Get the Javascript context in which your function has been defined.  I haven't played around with WinLauncher, but in GtkLauncher it is obtained by using:
frame = webkit_web_view_get_main_frame (web_view);
context = webkit_web_frame_get_global_context(frame);

I would imagine WinLauncher is similar...

Once you have the context, assuming that your Javascript function is defined globally, you will need to get the context's global object:

JSObjectRef globalObject = JSContextGetGlobalObject(context);

Once you have the global object, you will need a JSStringRef containing the name of the global function that you wish to call.  So, if you have declared the Javascript function as follows:

function my_function()
//do something
you should create a string "my_function" as follows:
    JSStringRef     myFunctionName = JSStringCreateWithUTF8CString("my_function");

Now you can get the property of the global object with the name "my_function":
JSObjectRef functionObject = (JSObjectRef)JSObjectGetProperty(context, globalObject, myFunctionName, NULL);

functionObject now contains a reference to your Javascript function, and can be called as follows (I'm going to make an example where my_function takes two numbers as arguments):

JSValueRef  arguments[2];
JSValueRef result;
int num_arguments = 2;
arguments[0] = JSValueMakeNumber(context, 1.0);
arguments[1] = JSValueMakeNumber(context, 3.14);
result = JSObjectCallAsFunction(context, functionObject, globalObject, num_arguments, arguments, NULL);

I hope that helps.

Allison Newman

On 27 Jan, 2011,at 04:00 PM, webkit-help-request at lists.webkit.org wrote:

Send webkit-help mailing list submissions to
webkit-help at lists.webkit.org

To subscribe or unsubscribe via the World Wide Web, visit
or, via email, send a message with subject or body 'help' to
webkit-help-request at lists.webkit.org

You can reach the person managing the list at
webkit-help-owner at lists.webkit.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of webkit-help digest..."

Today's Topics:

1. Calling JS Function From C (Nimish Nayak)


Message: 1
Date: Thu, 27 Jan 2011 14:55:55 +0530
From: Nimish Nayak <nimishnayk at gmail.com>
To: webkit-help at lists.webkit.org
Subject: [webkit-help] Calling JS Function From C
<AANLkTimXy5j10Xbu24C37HPcGhPPQ45P_nP7pbh=hzMi at mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi all

In WinLauncher the WebFrameLoadDelegate Interface has already been
implemented.In the function didClearWindowObject I called my own function
where I Make a function with a call back set it as the property and call a C
function from JS But how can i call a JS function from C.
I was reading online i saw JSObjectGetProperty and JSObjectCallAsFunction
but I am not able to understand the parameters well of both these functions
is ther a way i can get this done.?

Mr. Nimish Nayak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-help/attachments/20110127/d239a0b6/attachment-0001.html>


webkit-help mailing list
webkit-help at lists.webkit.org

End of webkit-help Digest, Vol 19, Issue 18
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-help/attachments/20110127/d3d6e29c/attachment.html>

More information about the webkit-help mailing list