<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#c7">Comment # 7</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:anewtobi&#64;gmail.com" title="anewtobi&#64;gmail.com">anewtobi&#64;gmail.com</a>
</span></b>
        <pre>(In reply to <a href="show_bug.cgi?id=136989#c6">comment #6</a>)
<span class="quote">&gt; Thanks for the patch. I think the long term solution would be to have
&gt; GObject bindings for JSC API, but for now I guess we need something current
&gt; WebKit bindings could use. However, I'm not sure adding _as_string is the
&gt; best approach.  Why as_string and not also as_number, as_bool, etc.? I
&gt; prefer if we add something like as_gvalue() for example, that would work for
&gt; bindings, but would also be more convenient to use for C API users and would
&gt; cover all possible values of the result that can be represented as a GValue.</span >

I agree with the long term solution.

Why as_string and not as_number and as_bool?

Because numbers and bools can (and do) automatically get &quot;stringified&quot; (I don't mean from the JS-code side, I mean by JSValueToStringCopy), so numbers and booleans are also &quot;covered&quot; with this solution with little extra &quot;costs&quot; and nothing extra to maintain. Furthermore the assumption can be made that people will send complex data as JSON strings with JSON.stringify and than decode the JSON with something like JSON.loads (in python).

The as_gvariant / as_gvalue idea, also occured to me, but seeing that strings can be very easily converted to ints/floats/booleans in python and also seeing that JSON.stringify (js) and JSON.loads (python) work flawlessly through postMessage()/as_string, I saw little need for as_number, as_bool or as_gvariant as it would complicate the code a little bit (I mean in the webkitjavascriptresult implementation, but also when you're using the API in python). I'm not against as_gvalue, but I'd like to have as_string, as it makes the python code really simple.

Also consider this example in the documenation from:
<a href="https://lazka.github.io/pgi-docs/index.html#WebKit2-4.0/classes/WebView.html#WebKit2.WebView.run_javascript_finish">https://lazka.github.io/pgi-docs/index.html#WebKit2-4.0/classes/WebView.html#WebKit2.WebView.run_javascript_finish</a>

static void
web_view_javascript_finished (GObject      *object,
                              GAsyncResult *result,
                              gpointer      user_data)
{
    WebKitJavascriptResult *js_result;
    JSValueRef              value;
    JSGlobalContextRef      context;
    GError                 *error = NULL;

    js_result = webkit_web_view_run_javascript_finish (WEBKIT_WEB_VIEW (object), result, &amp;amp;error);
    if (!js_result) {
        g_warning (&quot;Error running javascript: %s&quot;, error-&gt;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);
        str_value = (gchar *)g_malloc (str_length);
        JSStringGetUTF8CString (js_str_value, str_value, str_length);
        JSStringRelease (js_str_value);
        g_print (&quot;Script result: %s\n&quot;, str_value);
        g_free (str_value);
    } else {
        g_warning (&quot;Error running javascript: unexpected return value&quot;);
    }
    webkit_javascript_result_unref (js_result);
}

That's a lot of boilerplate code simply to get a string. I imagine that getting a string is a common end result you want from javascript_finish and from messageHandlers. In C you can at least do it, but look at how much code that is!
Now you can you use as_string in this C example and be done. So, this is an additional reason for having as_string beyond just enabling python scripts to use the API at all.</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>