Rework util code for extracting string/double values from JS.Values.

* src/client/util/util-webkit.vala (to_number, to_string): Move code for
  extracting actual vales to functions in engine/util/util-js.vala,
  rework to use those functions.
This commit is contained in:
Michael James Gratton 2017-01-23 23:53:09 +11:00
parent b47899ac69
commit 69da046ff3
2 changed files with 46 additions and 24 deletions

View file

@ -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();

View file

@ -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}.
*/