I am learning webExtensions and I had written a sample web extension for webkit where I used javascriptcore DOM/C API.. I had referred Binding native objects with JavascriptCore C Api and https://github.com/vrruiz/WebKit-JavaScriptCore-Extensions And I was able to return all data types including native objects.
With recent webkit releases, new JavascriptCore Glib API have been introduced and I am trying to migrate my test code to new JSC glib API. I referred Test code in webkit Tools directory and was able to return native number, string, boolean values to javascript (Using a HTML test app) But I was not able to return any native objects. For example when I tried to return Gfile object , I got below error
TypeError: unsupported type GObject
I tried changing the return type from G_TYPE_OBJECT to JSC_TYPE_VALUE But I was not sure how to convert GFile object into JSCvalue. If i don't convert then in app I get NULL object.
HTML & JavaScript code
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Extending JavaScript with WebKit</h1>
<div id="test-extension"></div>
<script type="text/javascript">
var display = function () {
var div = document.getElementById("test-extension");
var testExt = getTestExtObject('.');
var extString = "Test Extension Object " + testExt + "<br>";
div.innerHTML = extString;
}
display();
}
</script>
</body>
</html>
C++ code
extern "C" void webkit_web_extension_initialize_with_user_data(WebKitWebExtension* extension, GVariant* userData)
{
g_signal_connect(webkit_script_world_get_default(), "window-object-cleared", G_CALLBACK(windowObjectCleared), nullptr);
}
static GFile* getTestExtObject(const char* path)
{
GFile* ret = g_file_new_for_path(path);
return ret;
}
static void windowObjectCleared(WebKitScriptWorld* world, WebKitWebPage* page, WebKitFrame* frame, gpointer)
{
GRefPtr<JSCContext> jsContext = adoptGRef(webkit_frame_get_js_context_for_script_world(frame, world));
g_assert_true(JSC_IS_CONTEXT(jsContext.get()));
GRefPtr<JSCValue> function = adoptGRef(jsc_value_new_function(jsContext.get(), "getTestExtObject", G_CALLBACK(getTestExtObject), NULL, NULL, G_TYPE_OBJECT, 1, G_TYPE_STRING));
jsc_context_set_value(jsContext.get(), "getTestExtObject", function.get());
}