Re: libseed-list Call a Vala Function from Javascript



Hi Andrew,

JavascriptCore offer facilities to expose native functions within the js
context. Seed makes it more convenient.
So you ont be able to expose your vala procedure as-is but its C
counterpart.

First you need a hold on the js context object. Webkit gtk gives you a
chance to modify it when the global js object representing the "window"
is created. Just attach a handler to the "window-object-cleared" WebView
signal. See

http://webkitgtk.org/reference/webkitgtk-webkitwebview.html#WebKitWebView-window-object-cleared

The signature of the handler is 
void on_window_object_cleared (WebKitWebView  *web_view,
                               WebKitWebFrame *frame,
                               gpointer        context,
                               gpointer        window_object,
                               gpointer        user_data);

>From there, you can call JavascriptCore's

JSObjectRef JSObjectMakeFunctionWithCallback(
    JSContextRef ctx,
    JSStringRef name,
    JSObjectCallAsFunctionCallback callAsFunction);

where callAsFunction is a pointer to your routine.

Then expose the returned object, which represent the javascript function
associated with your callback, in the js context using the following:

JS_EXPORT void JSObjectSetProperty(
    JSContextRef ctx,
    JSObjectRef object,
    JSStringRef propertyName,
    JSValueRef value,
    JSPropertyAttributes attributes,
    JSValueRef *exception);  

object should point to the "window" object, propertyName will be the
name of your function in the js context, value the object we got
beforehand.


So to sum that up you should have something like this:

static void my_native_print(char *it)
{
	printf("%s\n", it);
}

JSValueRef
my_native_print_wrapper (JSContextRef ctx, JSObjectRef function, 
			    JSObjectRef thisObject,
	                    size_t argumentCount,
                            const JSValueRef arguments[],
                            JSValueRef* exception)
{
	// convert first argument to utf8 char*
	char argument1[256];
	JSStringGetUTF8CString (arguments[0], argument1, 256);
 	
	my_native_print(argument1);
}

void on_window_object_cleared (WebKitWebView  *web_view,
                               WebKitWebFrame *frame,
                               gpointer        context,
                               gpointer        window_object,
                               gpointer        user_data)
{

	JSObjectRef js_func =
        JSObjectMakeFunctionWithCallback(context, NULL,
 					 my_native_print_wrapper);

       JSObjectSetProperty(context, window_object, "native_print",
			   js_func, NULL, NULL);       
}

"window.native_print" should now be available in the js context and call
your native function.

You might need to tweak a little, I havent tried to compile it.

Le vendredi 20 mai 2011 à 08:27 +0100, Andrew a écrit :
> Hi Everyone,
> 
> So I have a Vala function:
> 
> public void say_my_name(string name) {
>     stdout.printf("Hi, my name is %s", name);
> }
> 
> And I want to be able to call this in Javascript like so:
> 
> say_my_name("John Doe");
> 
> This Javascript however, is on a local HTML file, that I access through
> a Webkit.WebView
> 
> Could someone point to an existing example (can be in C), or just talk
> through the basic steps needed to acomplish this? If you need any more
> information, please don't hesitate to contact me.
> 
> Thanks
> 




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]