diff --git a/src/client/util/util-webkit.vala b/src/client/util/util-webkit.vala index cfce6f37..79bdd3ba 100644 --- a/src/client/util/util-webkit.vala +++ b/src/client/util/util-webkit.vala @@ -32,18 +32,10 @@ namespace WebKitUtil { * This will raise a {@link Geary.JS.Error.TYPE} error if the * result is not a JavaScript `Number`. */ - public double to_number(WebKit.JavascriptResult result) + public inline double to_number(WebKit.JavascriptResult result) throws Geary.JS.Error { - JS.GlobalContext context = result.get_global_context(); - JS.Value value = result.get_value(); - if (!value.is_number(context)) { - throw new Geary.JS.Error.TYPE("Result is not a JS Number object"); - } - - JS.Value? err = null; - double number = value.to_number(context, out err); - Geary.JS.check_exception(context, err); - return number; + return Geary.JS.to_number(result.get_global_context(), + result.get_value()); } /** @@ -52,19 +44,10 @@ namespace WebKitUtil { * This will raise a {@link Geary.JS.Error.TYPE} error if the * result is not a JavaScript `String`. */ - public string? to_string(WebKit.JavascriptResult result) + public inline string to_string(WebKit.JavascriptResult result) throws Geary.JS.Error { - JS.GlobalContext context = result.get_global_context(); - JS.Value js_str_value = result.get_value(); - if (!js_str_value.is_string(context)) { - throw new Geary.JS.Error.TYPE("Result is not a JS String object"); - } - - JS.Value? err = null; - JS.String js_str = js_str_value.to_string_copy(context, out err); - Geary.JS.check_exception(context, err); - - return Geary.JS.to_string_released(js_str); + return Geary.JS.to_string(result.get_global_context(), + result.get_value()); } /** @@ -74,7 +57,7 @@ namespace WebKitUtil { * result to a string, effectively by calling the JavaScript * `toString()` method on it, and returning that value. */ - public string? as_string(WebKit.JavascriptResult result) + public string as_string(WebKit.JavascriptResult result) throws Geary.JS.Error { JS.GlobalContext context = result.get_global_context(); JS.Value js_str_value = result.get_value(); diff --git a/src/engine/util/util-js.vala b/src/engine/util/util-js.vala index ddcf8d4f..a25e3308 100644 --- a/src/engine/util/util-js.vala +++ b/src/engine/util/util-js.vala @@ -36,6 +36,45 @@ namespace Geary.JS { return (js == null || js.get_type(context) == global::JS.Type.NULL); } + /** + * Returns a JSC Value as a number. + * + * This will raise a {@link Geary.JS.Error.TYPE} error if the + * value is not a JavaScript `Number`. + */ + public double to_number(global::JS.Context context, + global::JS.Value value) + throws Geary.JS.Error { + if (!value.is_number(context)) { + throw new Geary.JS.Error.TYPE("Value is not a JS Number object"); + } + + global::JS.Value? err = null; + double number = value.to_number(context, out err); + Geary.JS.check_exception(context, err); + return number; + } + + /** + * Returns a JSC Value as a string. + * + * This will raise a {@link Geary.JS.Error.TYPE} error if the + * value is not a JavaScript `String`. + */ + public string to_string(global::JS.Context context, + global::JS.Value value) + throws Geary.JS.Error { + if (!value.is_string(context)) { + throw new Geary.JS.Error.TYPE("Value is not a JS String object"); + } + + global::JS.Value? err = null; + global::JS.String js_str = value.to_string_copy(context, out err); + Geary.JS.check_exception(context, err); + + return Geary.JS.to_string_released(js_str); + } + /** * Returns a JSC {@link JS.String} as a Vala {@link string}. */