<html>
    <head>
      <base href="https://bugs.webkit.org/" />
    </head>
    <body>
      <p>
        <div>
            <b><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - WebKit2 Gtk+ JavaScriptCore bindings doesn't work properly"
   href="https://bugs.webkit.org/show_bug.cgi?id=136989#c8">Comment # 8</a>
              on <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - WebKit2 Gtk+ JavaScriptCore bindings doesn't work properly"
   href="https://bugs.webkit.org/show_bug.cgi?id=136989">bug 136989</a>
              from <span class="vcard"><a class="email" href="mailto:cgarcia&#64;igalia.com" title="Carlos Garcia Campos &lt;cgarcia&#64;igalia.com&gt;"> <span class="fn">Carlos Garcia Campos</span></a>
</span></b>
        <pre>(In reply to <a href="show_bug.cgi?id=136989#c7">comment #7</a>)
<span class="quote">&gt; (In reply to <a href="show_bug.cgi?id=136989#c6">comment #6</a>)
&gt; &gt; Thanks for the patch. I think the long term solution would be to have
&gt; &gt; GObject bindings for JSC API, but for now I guess we need something current
&gt; &gt; WebKit bindings could use. However, I'm not sure adding _as_string is the
&gt; &gt; best approach.  Why as_string and not also as_number, as_bool, etc.? I
&gt; &gt; prefer if we add something like as_gvalue() for example, that would work for
&gt; &gt; bindings, but would also be more convenient to use for C API users and would
&gt; &gt; cover all possible values of the result that can be represented as a GValue.
&gt; 
&gt; I agree with the long term solution.
&gt; 
&gt; Why as_string and not as_number and as_bool?
&gt; 
&gt; Because numbers and bools can (and do) automatically get &quot;stringified&quot; (I
&gt; don't mean from the JS-code side, I mean by JSValueToStringCopy), so numbers
&gt; and booleans are also &quot;covered&quot; with this solution with little extra &quot;costs&quot;
&gt; and nothing extra to maintain. Furthermore the assumption can be made that
&gt; people will send complex data as JSON strings with JSON.stringify and than
&gt; decode the JSON with something like JSON.loads (in python).</span >

In that case maybe it would be better to use to_string() since we are converting the value to a string in some cases.

<span class="quote">&gt; The as_gvariant / as_gvalue idea, also occured to me, but seeing that
&gt; strings can be very easily converted to ints/floats/booleans in python and
&gt; also seeing that JSON.stringify (js) and JSON.loads (python) work flawlessly
&gt; through postMessage()/as_string, I saw little need for as_number, as_bool or
&gt; as_gvariant as it would complicate the code a little bit (I mean in the
&gt; webkitjavascriptresult implementation, but also when you're using the API in
&gt; python). I'm not against as_gvalue, but I'd like to have as_string, as it
&gt; makes the python code really simple.
&gt; 
&gt; Also consider this example in the documenation from:
&gt; <a href="https://lazka.github.io/pgi-docs/index.html#WebKit2-4.0/classes/WebView">https://lazka.github.io/pgi-docs/index.html#WebKit2-4.0/classes/WebView</a>.
&gt; html#WebKit2.WebView.run_javascript_finish
&gt; 
&gt; static void
&gt; web_view_javascript_finished (GObject      *object,
&gt;                               GAsyncResult *result,
&gt;                               gpointer      user_data)
&gt; {
&gt;     WebKitJavascriptResult *js_result;
&gt;     JSValueRef              value;
&gt;     JSGlobalContextRef      context;
&gt;     GError                 *error = NULL;
&gt; 
&gt;     js_result = webkit_web_view_run_javascript_finish (WEBKIT_WEB_VIEW
&gt; (object), result, &amp;amp;error);
&gt;     if (!js_result) {
&gt;         g_warning (&quot;Error running javascript: %s&quot;, error-&gt;message);
&gt;         g_error_free (error);
&gt;         return;
&gt;     }
&gt; 
&gt;     context = webkit_javascript_result_get_global_context (js_result);
&gt;     value = webkit_javascript_result_get_value (js_result);
&gt;     if (JSValueIsString (context, value)) {
&gt;         JSStringRef js_str_value;
&gt;         gchar      *str_value;
&gt;         gsize       str_length;
&gt; 
&gt;         js_str_value = JSValueToStringCopy (context, value, NULL);
&gt;         str_length = JSStringGetMaximumUTF8CStringSize (js_str_value);
&gt;         str_value = (gchar *)g_malloc (str_length);
&gt;         JSStringGetUTF8CString (js_str_value, str_value, str_length);
&gt;         JSStringRelease (js_str_value);
&gt;         g_print (&quot;Script result: %s\n&quot;, str_value);
&gt;         g_free (str_value);
&gt;     } else {
&gt;         g_warning (&quot;Error running javascript: unexpected return value&quot;);
&gt;     }
&gt;     webkit_javascript_result_unref (js_result);
&gt; }</span >

Note that this example only uses JSValueToStringCopy if JSValueIsString() return true, to make it clear that we are expecting a string and getting a string.

<span class="quote">&gt; That's a lot of boilerplate code simply to get a string. I imagine that
&gt; getting a string is a common end result you want from javascript_finish and
&gt; from messageHandlers. In C you can at least do it, but look at how much code
&gt; that is!</span >

I agree, and that's why I said it would work for bindings but also would make the API more convenient.

<span class="quote">&gt; Now you can you use as_string in this C example and be done. So, this is an
&gt; additional reason for having as_string beyond just enabling python scripts
&gt; to use the API at all.</span >

I agree, yes.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are the assignee for the bug.</li>
      </ul>
    </body>
</html>