[webkit-gtk] webkit_web_view_run_javascript_finish can't get results on webkit2, crashes

Egor Egorov me at egorfine.com
Fri Sep 20 18:36:05 PDT 2013


Thank you very much, it worked!

On 20 Sep 2013, at 03:07, Carlos Garcia Campos <cgarcia at igalia.com> wrote:

> El jue, 19-09-2013 a las 18:51 +0300, Egor Egorov escribió:
>> Hello. 
>> 
>> This simple reduced test case crashes at JSStringGetMaximumUTF8CStringSize(). Actually I tried a few options to get the string value back from webkit2 instance and googled a lot to no avail. 
>> 
>> It segfaults both on gtk webkit 2.0.4 and 2.1.92, both built by gcc 4.8.1 on Ubuntu 13.04 with all packages updated. 
>> 
>> Any chance to fix it? 
> 
> The problem is that you are not including the JSC headers required to
> use the JSC API, simply add:
> 
> #include <JavaScriptCore/JSStringRef.h>
> #include <JavaScriptCore/JSValueRef.h>
> 
> And it works. I wonder why that builds, giving only compile warnings,
> though.
> 
>> #include <gtk/gtk.h>
>> #include <webkit2/webkit2.h>
>> 
>> 
>> void gotInnerHtml(GObject *object, GAsyncResult *result, gpointer data) {
>> 	WebKitJavascriptResult *js_result;
>> 	JSValueRef              value;
>> 	JSGlobalContextRef      context;
>> 	GError                 *error = NULL;
>> 
>> 	GtkWidget *webView = (GtkWidget *) data;
>> 
>> 	js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(webView), result, &error);
>> 	if (!js_result) {
>> 		g_warning ("Error running javascript: %s", error->message);
>> 		g_error_free (error);
>> 		return;
>> 	}
>> 
>> 	context = webkit_javascript_result_get_global_context(js_result);
>> 	value = webkit_javascript_result_get_value(js_result);
>> 
>> 	if (JSValueIsString (context, value)) {
>> 		JSStringRef js_str_value;
>> 		gchar      *str_value;
>> 		gsize       str_length;
>> 
>> 		js_str_value = JSValueToStringCopy (context, value, NULL);
>> 
>> 		str_length = JSStringGetMaximumUTF8CStringSize(js_str_value); // crashes here
>> 		str_value = (gchar *)g_malloc (str_length);
>> 
>> 		JSStringGetUTF8CString (js_str_value, str_value, str_length);
>> 		JSStringRelease (js_str_value);
>> 
>> 		g_print ("Script result: %s\n", str_value);
>> 		g_free (str_value);
>> 	} else {
>> 		g_warning ("Error running javascript: unexpected return value");
>> 	}
>> 
>> 	webkit_javascript_result_unref (js_result);
>> }
>> 
>> void getInnerHtml(GtkWidget *webView) {
>> 	webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(webView), "'a little string';", NULL, gotInnerHtml, webView);
>> }
>> 
>> gboolean doGetHtml(gpointer data) {
>> 	getInnerHtml((GtkWidget *)data);
>> 	return FALSE;
>> }
>> 
>> int main(int argc, char* argv[]) {
>> 	printf("Version %d.%d.%d\n", webkit_get_major_version(), webkit_get_minor_version(), webkit_get_micro_version());
>> 	gtk_init(&argc, &argv);
>> 
>> 	GtkWidget *mainWebWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
>> 	gtk_window_set_default_size(GTK_WINDOW(mainWebWindow), 800, 600);
>> 
>> 	GtkWidget *webView = webkit_web_view_new();
>> 
>> 	gtk_container_add(GTK_CONTAINER(mainWebWindow), webView);
>> 
>> 	WebKitURIRequest *request = webkit_uri_request_new("http://www.google.com/");
>> 	webkit_web_view_load_request(WEBKIT_WEB_VIEW(webView), request);
>> 
>> 	GtkWidget *window1 = gtk_widget_get_toplevel(webView);
>> 	gtk_widget_show_all(window1);
>> 
>> 	g_timeout_add(2000, doGetHtml, webView);
>> 
>> 	gtk_main();
>> 
>> 	return 0;
>> }
>> 
>> 
>> 
> 
> -- 
> Carlos Garcia Campos
> http://pgp.rediris.es:11371/pks/lookup?op=get&search=0xF3D322D0EC4582C3

-- 
Egor Egorov
http://egorfine.com/

Warning: this email may contain humor, sarcasm, parody, and perhaps even irony. Read at your own risk.




More information about the webkit-gtk mailing list