From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Ergus Newsgroups: gmane.emacs.devel Subject: Re: Using JavaScriptCore patch Date: Tue, 17 Sep 2019 21:30:00 +0200 Message-ID: <20190917193000.gndkgn7k42eiided@Ergus> References: <20190806162925.pnv6yg56brh67dfx@Ergus> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="72394"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: NeoMutt/20180716 To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Sep 17 21:31:24 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1iAJC6-000IfQ-2e for ged-emacs-devel@m.gmane.org; Tue, 17 Sep 2019 21:31:22 +0200 Original-Received: from localhost ([::1]:51620 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iAJC4-00058a-Cz for ged-emacs-devel@m.gmane.org; Tue, 17 Sep 2019 15:31:20 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:46884) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iAJB4-00050W-5Q for emacs-devel@gnu.org; Tue, 17 Sep 2019 15:30:20 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iAJB1-0008Bp-E0 for emacs-devel@gnu.org; Tue, 17 Sep 2019 15:30:17 -0400 Original-Received: from sonic316-11.consmr.mail.bf2.yahoo.com ([74.6.130.121]:38251) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iAJB0-0008BF-RT for emacs-devel@gnu.org; Tue, 17 Sep 2019 15:30:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aol.com; s=a2048; t=1568748613; bh=PipL7N8Zh0hoG5P70mBIf7st6PIeu52PMfX6wcscmYM=; h=Date:From:To:Subject:References:In-Reply-To:From:Subject; b=tMQX/qdT+Bx57IVV/R3Cbytg1eGCkfcU5pEkyet5EJIqHdSEjze58BWltpAknV4gZZzqKewnJWHLIaRBKOqZgrTcqje7f5BmsJkNw7Wj7BWzqfE0iqT9PrXqugMUm6ODi+y/6mfvcmqcyIPqlXVJ5M4ucRjtkTQEb4HAWFWoSJjpeqcZe4A3zWRaGidfApd7ebo8ksOwlNbdCBy5lQgL6pO2NwPhiHE1L/Oq9ike8fO52hWUs/OlQH1WxgKD0mrt+JVes7w9F+7PGMFyqvkj4nvN8i9u8mI+lIzfOPaxVkXuhOKz7sArXZ76g0gWLHBJLnek6LYOyciXiIrMHlNduQ== X-YMail-OSG: UqtWSGQVM1lPpWMxpOEVsso4hdvW1qRlLu6HvaxnxphPTcl3G9eoAYSBiM3B85t v2cKaw3gR.UAaK21i0AnkbnbxONjvJTVnZGVhudkVm3fORyU9P5tZOt6zb0asyW7jTQ.YU3uvfwY ZA1rRqZC1B0Cq54uxH6HLJczmMSFW.NwHJvsebJStiSeR0wmXxSW209yGMfGJ6irReq1kYpF3KyX OnS0v7OsYa9dq2X_hHwbuw8YAQK85MM5ohz_eJOtl4fVWZwYFENFXq7gl0HCafUNiVm_Zd0QoFdg wD2khix4FkmrVKs_A1KbPUqcr1bsDDuQIklKeNW.Ung1PZCeUYinFOtY6km5rCGBKPQCeMt7u3Pl KYciY7SfCNi3KoXWH_sXIiuR5oPb1t6hLggoXuvak25Nmj5.nTM3dFJ29LRmJQLqJ4eT40rQEQWm EPtAc5UH.Grag55Ix8LiyNPQNAo6Lc_.311TPLCAtCvVNQ2R56Kzln6eARif7cWkYcPdU.4oFosv B3XAlEF3Y1DvauyhO9_5d_8tybEMNXqQHcO.rfe7gLPGzVZWG.2DZA.q8iioelL2bjSxmJbqJLlQ Hm31FaOj_5h4OSA6c5PPktx3pBIqv7nk4WeLo_6Owo.u5NtyukNI9TuwuJjCUaT.4rIarizNcoUW vURZisJqkJtWapw6QSAoXWZ7BnpUZ1dMG8rAnn3lID4JyHD4bdWuZd_1JpSjGtjzDcGa0_Z_ozMU 82VQlX6mntBFm5c1vVfJd.dYc6nBjlNjT7wwt_4lADk5E79_3.Mk1YPftt2fxP0VJfF2vOp.PAlD .BEY9JJewt53qawcfUHqqKSnajeRgLfZmIIhmknqzi Original-Received: from sonic.gate.mail.ne1.yahoo.com by sonic316.consmr.mail.bf2.yahoo.com with HTTP; Tue, 17 Sep 2019 19:30:13 +0000 Original-Received: by smtp418.mail.ir2.yahoo.com (Oath Hermes SMTP Server) with ESMTPA ID 73649e28012ce577fd4331c528dc1206; Tue, 17 Sep 2019 19:30:08 +0000 (UTC) Content-Disposition: inline In-Reply-To: <20190806162925.pnv6yg56brh67dfx@Ergus> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 74.6.130.121 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:240112 Archived-At: Hi: I will apply the patch in this thread to master if nobody cares at the end. Best, Ergus On Tue, Aug 06, 2019 at 06:29:27PM +0200, Ergus wrote: >Hi: > >I just see that there was a TODO in src/xwidget.c to delete a warning >about deprecations in two functions and use JavaScriptCore API. > >I don't know if anyone is working on that already, so I just propose the >next patch (attachment). > >If there is any specialist that want to correct/improve/comment this; it >is very welcome. > >At least it is working for me. > >Best, >Ergus > >diff --git a/src/xwidget.c b/src/xwidget.c >index 121510ebac..ecb3793629 100644 >--- a/src/xwidget.c >+++ b/src/xwidget.c >@@ -31,14 +31,6 @@ along with GNU Emacs. If not, see . */ > #include > #include > >-/* Suppress GCC deprecation warnings starting in WebKitGTK+ 2.21.1 for >- webkit_javascript_result_get_global_context and >- webkit_javascript_result_get_value (Bug#33679). >- FIXME: Use the JavaScriptCore GLib API instead, and remove this hack. */ >-#if WEBKIT_CHECK_VERSION (2, 21, 1) && GNUC_PREREQ (4, 2, 0) >-# pragma GCC diagnostic ignored "-Wdeprecated-declarations" >-#endif >- > static struct xwidget * > allocate_xwidget (void) > { >@@ -284,95 +276,70 @@ webkit_view_load_changed_cb (WebKitWebView *webkitwebview, > > /* Recursively convert a JavaScript value to a Lisp value. */ > static Lisp_Object >-webkit_js_to_lisp (JSContextRef context, JSValueRef value) >+webkit_js_to_lisp (JSCValue *value) > { >- switch (JSValueGetType (context, value)) >+ if (jsc_value_is_string (value)) > { >- case kJSTypeString: >- { >- JSStringRef js_str_value; >- gchar *str_value; >- gsize str_length; >- >- js_str_value = JSValueToStringCopy (context, value, NULL); >- str_length = JSStringGetMaximumUTF8CStringSize (js_str_value); >- str_value = (gchar *)g_malloc (str_length); >- JSStringGetUTF8CString (js_str_value, str_value, str_length); >- JSStringRelease (js_str_value); >- return build_string (str_value); >- } >- case kJSTypeBoolean: >- return (JSValueToBoolean (context, value)) ? Qt : Qnil; >- case kJSTypeNumber: >- return make_fixnum (JSValueToNumber (context, value, NULL)); >- case kJSTypeObject: >- { >- if (JSValueIsArray (context, value)) >- { >- JSStringRef pname = JSStringCreateWithUTF8CString("length"); >- JSValueRef len = JSObjectGetProperty (context, (JSObjectRef) value, >- pname, NULL); >- double dlen = JSValueToNumber (context, len, NULL); >- JSStringRelease(pname); >- >- Lisp_Object obj; >- if (! (0 <= dlen && dlen < PTRDIFF_MAX + 1.0)) >- memory_full (SIZE_MAX); >- ptrdiff_t n = dlen; >- struct Lisp_Vector *p = allocate_vector (n); >- >- for (ptrdiff_t i = 0; i < n; ++i) >- { >- p->contents[i] = >- webkit_js_to_lisp (context, >- JSObjectGetPropertyAtIndex (context, >- (JSObjectRef) value, >- i, NULL)); >- } >- XSETVECTOR (obj, p); >- return obj; >- } >- else >- { >- JSPropertyNameArrayRef properties = >- JSObjectCopyPropertyNames (context, (JSObjectRef) value); >- >- size_t n = JSPropertyNameArrayGetCount (properties); >- Lisp_Object obj; >- >- /* TODO: can we use a regular list here? */ >- if (PTRDIFF_MAX < n) >- memory_full (n); >- struct Lisp_Vector *p = allocate_vector (n); >- >- for (ptrdiff_t i = 0; i < n; ++i) >- { >- JSStringRef name = JSPropertyNameArrayGetNameAtIndex (properties, i); >- JSValueRef property = JSObjectGetProperty (context, >- (JSObjectRef) value, >- name, NULL); >- gchar *str_name; >- gsize str_length; >- str_length = JSStringGetMaximumUTF8CStringSize (name); >- str_name = (gchar *)g_malloc (str_length); >- JSStringGetUTF8CString (name, str_name, str_length); >- JSStringRelease (name); >- >- p->contents[i] = >- Fcons (build_string (str_name), >- webkit_js_to_lisp (context, property)); >- } >- >- JSPropertyNameArrayRelease (properties); >- XSETVECTOR (obj, p); >- return obj; >- } >- } >- case kJSTypeUndefined: >- case kJSTypeNull: >- default: >- return Qnil; >+ gchar *str_value = jsc_value_to_string (value); >+ Lisp_Object ret = build_string (str_value); >+ g_free (str_value); >+ >+ return ret; >+ } >+ else if (jsc_value_is_boolean (value)) >+ { >+ return (jsc_value_to_boolean (value)) ? Qt : Qnil; >+ } >+ else if (jsc_value_is_number (value)) >+ { >+ return make_fixnum (jsc_value_to_int32 (value)); >+ } >+ else if (jsc_value_is_array (value)) >+ { >+ JSCValue *len = jsc_value_object_get_property (value, "length"); >+ const gint32 dlen = jsc_value_to_int32 (len); >+ >+ Lisp_Object obj; >+ if (! (0 <= dlen && dlen < PTRDIFF_MAX + 1.0)) >+ memory_full (SIZE_MAX); >+ >+ ptrdiff_t n = dlen; >+ struct Lisp_Vector *p = allocate_vector (n); >+ >+ for (ptrdiff_t i = 0; i < n; ++i) >+ { >+ p->contents[i] = >+ webkit_js_to_lisp (jsc_value_object_get_property_at_index (value, i)); >+ } >+ XSETVECTOR (obj, p); >+ return obj; >+ } >+ else if (jsc_value_is_object (value)) >+ { >+ char **properties_names = jsc_value_object_enumerate_properties (value); >+ guint n = g_strv_length (properties_names); >+ >+ Lisp_Object obj; >+ if (PTRDIFF_MAX < n) >+ memory_full (n); >+ struct Lisp_Vector *p = allocate_vector (n); >+ >+ for (ptrdiff_t i = 0; i < n; ++i) >+ { >+ const char *name = properties_names[i]; >+ JSCValue *property = jsc_value_object_get_property (value, name); >+ >+ p->contents[i] = >+ Fcons (build_string (name), webkit_js_to_lisp (property)); >+ } >+ >+ g_strfreev (properties_names); >+ >+ XSETVECTOR (obj, p); >+ return obj; > } >+ >+ return Qnil; > } > > static void >@@ -380,41 +347,39 @@ webkit_javascript_finished_cb (GObject *webview, > GAsyncResult *result, > gpointer arg) > { >- WebKitJavascriptResult *js_result; >- JSValueRef value; >- JSGlobalContextRef context; >- GError *error = NULL; >- struct xwidget *xw = g_object_get_data (G_OBJECT (webview), >- XG_XWIDGET); >- ptrdiff_t script_idx = (intptr_t) arg; >- Lisp_Object script_callback = AREF (xw->script_callbacks, script_idx); >- ASET (xw->script_callbacks, script_idx, Qnil); >- if (!NILP (script_callback)) >- xfree (xmint_pointer (XCAR (script_callback))); >- >- 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; >- } >+ GError *error = NULL; >+ struct xwidget *xw = g_object_get_data (G_OBJECT (webview), XG_XWIDGET); > >- if (!NILP (script_callback) && !NILP (XCDR (script_callback))) >- { >- context = webkit_javascript_result_get_global_context (js_result); >- value = webkit_javascript_result_get_value (js_result); >- Lisp_Object lisp_value = webkit_js_to_lisp (context, value); >- >- /* Register an xwidget event here, which then runs the callback. >- This ensures that the callback runs in sync with the Emacs >- event loop. */ >- store_xwidget_js_callback_event (xw, XCDR (script_callback), lisp_value); >- } >+ ptrdiff_t script_idx = (intptr_t) arg; >+ Lisp_Object script_callback = AREF (xw->script_callbacks, script_idx); >+ ASET (xw->script_callbacks, script_idx, Qnil); >+ if (!NILP (script_callback)) >+ xfree (xmint_pointer (XCAR (script_callback))); >+ >+ WebKitJavascriptResult *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; >+ } >+ >+ if (!NILP (script_callback) && !NILP (XCDR (script_callback))) >+ { >+ JSCValue *value = webkit_javascript_result_get_js_value (js_result); >+ >+ Lisp_Object lisp_value = webkit_js_to_lisp (value); >+ >+ /* Register an xwidget event here, which then runs the callback. >+ This ensures that the callback runs in sync with the Emacs >+ event loop. */ >+ store_xwidget_js_callback_event (xw, XCDR (script_callback), lisp_value); >+ } > >- webkit_javascript_result_unref (js_result); >+ webkit_javascript_result_unref (js_result); > } > >