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: Fri, 9 Aug 2019 00:55:53 +0200 Message-ID: <20190808225553.65qm2evpvj662iyz@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="172457"; 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 Fri Aug 09 00:56:15 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 1hvrKQ-000inp-4C for ged-emacs-devel@m.gmane.org; Fri, 09 Aug 2019 00:56:14 +0200 Original-Received: from localhost ([::1]:55474 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvrKO-0002pa-F9 for ged-emacs-devel@m.gmane.org; Thu, 08 Aug 2019 18:56:12 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:50001) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvrKJ-0002ou-Ex for emacs-devel@gnu.org; Thu, 08 Aug 2019 18:56:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hvrKI-0007Ws-4G for emacs-devel@gnu.org; Thu, 08 Aug 2019 18:56:07 -0400 Original-Received: from sonic305-21.consmr.mail.ir2.yahoo.com ([77.238.177.83]:36512) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hvrKG-0007Vp-US for emacs-devel@gnu.org; Thu, 08 Aug 2019 18:56:06 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aol.com; s=a2048; t=1565304962; bh=UfcmNJEEm7FOW6mbIOoaWwqiTm2SAWZIqfTC2kkb+uk=; h=Date:From:To:Subject:References:In-Reply-To:From:Subject; b=c/W8s1zo5aK1TuOI9s7j63QTFUc8B4S9S1Ptpuy19QkX3anwbNTc2/TwxMHO6DvtFnfFCIa7B8zYp+xKRpaZ0ukf+y6uZVunRZ65VByDuuHipKjM6j2vsDHgSugeVHuxvR91lwvzZQnT3vvM38Mibn9XfiKhRxDBNe95xTGabTpUYlJAPlsinIAprnb2uC+6KpucJVRYbhfvrKD0FHIgEGLIHFZyho10mQMoipAqrMMXhonSf96fQa3+h6jWH9QVl/XNDpdwcABUeJiscqIpOkEbTCBq2PhFLW5tY0ZrdJ9l74e5d+8RKABubOtATKUR+lVeLBMUeaXGaitty0y77g== X-YMail-OSG: S9d8r58VM1kdmOCKoAzmZYW_EdTVgXZwVOhWbhJUR9KQPHpkBxc80AqpT6xuVl6 O3L8S3pPMMQ8XwffGKS.dKM1NG4tgYMtFdURrqcKfxmgnvLXiHvhrb9zECBJGJ0LIneE.ING1wKZ QORV2cl_I2qF5oTRQYp.f4.5Ns3Fwc_3GeF8j4j_MQDqh5rx0lYoSQiPigQ0b59YqMQf_AVnM.K4 QI.QNHIySoSHonQeW6SztwDr0p6se1BC9n7VKaZA7FyGfFlCall1c4Ijf5tANi7YKEuuT3xvB6BO MYGqFtWmhHeoG6eiSdUKT3dnj29_K2nEMgxWv5Mgik2A2B_Awvv9WcqPGSE6blrpjegioYxlkH4A UsrS1cqE1_fL3O.BANo1s5d0EBYsFO1TnHuEBHeGL3UiN7T.AirtpukRW3516RrdRPmSbw3JHGFA G.IUfH.s9BMblbFw1P2TK7K2FVODfHWfuMEGVrntG.qQF089Htozohk_q26_yqS0hSMw2xvpBkFr EAb8vkqV8ZL4x6hZuinFRq8NFo65oRICfqPG1cMikV93HURx8xQyweC1vxE2kKHkeimyc7OqmI8F GjPoWnUYLRfWTvasZnLCxx78tEB223QXjdwpfnMxZSeyaeMxkwwzo80pc9cxGuTUTRAco6Rs_T5Q 6dpaR29hsKudqUv.UOhAig_Dwva1dVez_2m40NE98YFoOUW1GMkg4EgC.a10k.fv3Y8A5MMoX.Io YK9OvGKFr0sqdgv8EhdmqQFwYuqeyNer5bsiaVNkoBoziKJY2j75CFQKthcBwm.pXvvgAo4kK5Pc ev2oekNxUuOaCL0RubNV8glNsBBhH6f0f_quufsz3. Original-Received: from sonic.gate.mail.ne1.yahoo.com by sonic305.consmr.mail.ir2.yahoo.com with HTTP; Thu, 8 Aug 2019 22:56:02 +0000 Original-Received: by smtp428.mail.ir2.yahoo.com (Oath Hermes SMTP Server) with ESMTPA ID 2022ad376e412266267b8c159f573efb; Thu, 08 Aug 2019 22:55:57 +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: 77.238.177.83 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:239264 Archived-At: Hi: Nobody comments this please? Should anyone suggests any change or propose a better patch? 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); > } > >