[webkit-help] Calling Javascript Method from CPP

Sumedh vsbhogle at gmail.com
Tue Aug 30 00:00:21 PDT 2011


We are customizing webkit apple port. I have written a method to call
javascript function from cpp code. Following is the code, please tell me if
anything wrong.

void Invoke_Script(HWND hwnd,char *script_name, int argc, ...) {

    int i;
    HRESULT hr;
    va_list ap;
    char *script_arg;
    IWebView* lWebView = 0;
    IWebFrame* frame = 0;
    JSContextRef context;
    JSValueRef *arguments=NULL;
    JSStringRef functionName,argStr;
    JSObjectRef globalObject,functionObject;

    //Get WebView Interface Object
    lWebView = (IWebView *)GetWindowLong(hwnd, GWL_USERDATA);

    //Get the Web Frame
    hr = lWebView->mainFrame(&frame);
    if (FAILED(hr))
        goto exit;

    //Get the Context Ref
    context = frame->globalContext();

    //Get the Global Object
    globalObject = JSContextGetGlobalObject(context);

    //Get the Function Name in JSString
    functionName = JSStringCreateWithUTF8CString(script_name);

    //Get the Arguments

        arguments = (JSValueRef*)GlobalAlloc(GPTR,sizeof(JSValueRef)*argc);

    //for (i=argc-1;i>=0;i--) {
    for (i=0;i<argc;i++) {


        argStr = JSStringCreateWithUTF8CString(script_arg);
        arguments[i] = JSValueMakeString(context, argStr);



    //Get a reference to your Javascript function
    functionObject = (JSObjectRef)JSObjectGetProperty(context, globalObject,
functionName, NULL);

    //Call the JS Function
    JSValueRef result = JSObjectCallAsFunction(context, functionObject,
globalObject, 1, arguments, NULL);



    if (frame)

If this code is right, why my memory is spiking when i call this function.
JSValueRef is retained as far as apple documentation, but then when it is
garbage collected. The issue is, we are calling this method say n times in a
minute, memory goes on increasing and is not freed. (We are checking memory
footprint in task manager). First I thought that may be i was leaking memory
in my internal code. But then found out that if i don't call this function,
memory print does not climb. Have i implemented this function correctly?

Dhanyawaad (Thanks)

Sumedh S Bhogle
Mumbai | Mob. 09969085122
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-help/attachments/20110830/9cd96335/attachment.html>

More information about the webkit-help mailing list