Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ext/js/bindgen/rb-js-abi-host.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ rb_js_abi_host_js_abi_value_t rb_js_abi_host_int_to_js_number(int32_t value) {
int32_t ret = __wasm_import_rb_js_abi_host_int_to_js_number(value);
return (rb_js_abi_host_js_abi_value_t){ ret };
}
__attribute__((import_module("rb-js-abi-host"), import_name("float-to-js-number: func(value: float64) -> handle<js-abi-value>")))
int32_t __wasm_import_rb_js_abi_host_float_to_js_number(double);
rb_js_abi_host_js_abi_value_t rb_js_abi_host_float_to_js_number(double value) {
int32_t ret = __wasm_import_rb_js_abi_host_float_to_js_number(value);
return (rb_js_abi_host_js_abi_value_t){ ret };
}
__attribute__((import_module("rb-js-abi-host"), import_name("string-to-js-string: func(value: string) -> handle<js-abi-value>")))
int32_t __wasm_import_rb_js_abi_host_string_to_js_string(int32_t, int32_t);
rb_js_abi_host_js_abi_value_t rb_js_abi_host_string_to_js_string(rb_js_abi_host_string_t *value) {
Expand Down
1 change: 1 addition & 0 deletions ext/js/bindgen/rb-js-abi-host.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ extern "C"
bool rb_js_abi_host_instance_of(rb_js_abi_host_js_abi_value_t value, rb_js_abi_host_js_abi_value_t klass);
rb_js_abi_host_js_abi_value_t rb_js_abi_host_global_this(void);
rb_js_abi_host_js_abi_value_t rb_js_abi_host_int_to_js_number(int32_t value);
rb_js_abi_host_js_abi_value_t rb_js_abi_host_float_to_js_number(double value);
rb_js_abi_host_js_abi_value_t rb_js_abi_host_string_to_js_string(rb_js_abi_host_string_t *value);
rb_js_abi_host_js_abi_value_t rb_js_abi_host_bool_to_js_bool(bool value);
rb_js_abi_host_js_abi_value_t rb_js_abi_host_proc_to_js_function(uint32_t value);
Expand Down
1 change: 1 addition & 0 deletions ext/js/bindgen/rb-js-abi-host.wit
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ is-js: func(value: js-abi-value) -> bool
instance-of: func(value: js-abi-value, klass: js-abi-value) -> bool
global-this: func() -> js-abi-value
int-to-js-number: func(value: s32) -> js-abi-value
float-to-js-number: func(value: float64) -> js-abi-value
string-to-js-string: func(value: string) -> js-abi-value
bool-to-js-bool: func(value: bool) -> js-abi-value
proc-to-js-function: func(value: u32) -> js-abi-value
Expand Down
12 changes: 12 additions & 0 deletions ext/js/js-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

extern VALUE rb_mKernel;
extern VALUE rb_cInteger;
extern VALUE rb_cFloat;
extern VALUE rb_cString;
extern VALUE rb_cTrueClass;
extern VALUE rb_cFalseClass;
Expand Down Expand Up @@ -444,6 +445,16 @@ static VALUE _rb_js_integer_to_js(VALUE obj) {
}
}

/*
* call-seq:
* to_js -> JS::Object
*
* Returns +self+ as a JS::Object.
*/
static VALUE _rb_js_float_to_js(VALUE obj) {
return jsvalue_s_new(rb_js_abi_host_float_to_js_number(RFLOAT_VALUE(obj)));
}

/*
* call-seq:
* to_js -> JS::Object
Expand Down Expand Up @@ -556,6 +567,7 @@ void Init_js() {
rb_define_singleton_method(rb_cJS_Object, "wrap", _rb_js_obj_wrap, 1);

rb_define_method(rb_cInteger, "to_js", _rb_js_integer_to_js, 0);
rb_define_method(rb_cFloat, "to_js", _rb_js_float_to_js, 0);
rb_define_method(rb_cString, "to_js", _rb_js_string_to_js, 0);
rb_define_method(rb_cTrueClass, "to_js", _rb_js_true_to_js, 0);
rb_define_method(rb_cFalseClass, "to_js", _rb_js_false_to_js, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface RbJsAbiHost {
instanceOf(value: JsAbiValue, klass: JsAbiValue): boolean;
globalThis(): JsAbiValue;
intToJsNumber(value: number): JsAbiValue;
floatToJsNumber(value: number): JsAbiValue;
stringToJsString(value: string): JsAbiValue;
boolToJsBool(value: boolean): JsAbiValue;
procToJsFunction(value: number): JsAbiValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export function addRbJsAbiHostToImports(imports, obj, get_export) {
const ret0 = obj.intToJsNumber(arg0);
return resources0.insert(ret0);
};
imports["rb-js-abi-host"]["float-to-js-number: func(value: float64) -> handle<js-abi-value>"] = function(arg0) {
const ret0 = obj.floatToJsNumber(arg0);
return resources0.insert(ret0);
};
imports["rb-js-abi-host"]["string-to-js-string: func(value: string) -> handle<js-abi-value>"] = function(arg0, arg1) {
const memory = get_export("memory");
const ptr0 = arg0;
Expand Down
3 changes: 3 additions & 0 deletions packages/npm-packages/ruby-wasm-wasi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ export class RubyVM {
intToJsNumber: (value) => {
return value;
},
floatToJsNumber: (value) => {
return value;
},
stringToJsString: (value) => {
return value;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ describe("Manipulation of JS from Ruby", () => {
test.each([
{ expr: `JS.global`, expected: "[object Object]" },
{ expr: `1.to_js`, expected: "1" },
{ expr: `(0.5).to_js`, expected: "0.5" },
{ expr: `JS.eval("return null")`, expected: "null" },
{ expr: `JS.eval("return undefined")`, expected: "undefined" },
{ expr: `JS.eval("return Symbol('sym')")`, expected: "Symbol(sym)" },
Expand Down
1 change: 1 addition & 0 deletions packages/npm-packages/ruby-wasm-wasi/test/test_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
require_relative "./unit/test_object"
require_relative "./unit/test_error"
require_relative "./unit/test_async"
require_relative "./unit/test_float"
10 changes: 10 additions & 0 deletions packages/npm-packages/ruby-wasm-wasi/test/unit/test_float.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "test-unit"
require "js"

class JS::TestFloat < Test::Unit::TestCase
def test_to_js
assert_equal (1.0).to_js, JS.eval("return 1.0;")
assert_equal (0.5).to_js, JS.eval("return 0.5;")
assert_equal (0.3).to_js, JS.eval("return 0.3;")
end
end