8000 Implement the JS::Object#to_b method. by ledsun · Pull Request #247 · ruby/ruby.wasm · GitHub
[go: up one dir, main page]

Skip to content

Implement the JS::Object#to_b method. #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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 @@ -151,6 +151,12 @@ rb_js_abi_host_js_abi_value_t rb_js_abi_host_rb_object_to_js_rb_value(uint32_t r
int32_t ret = __wasm_import_rb_js_abi_host_rb_object_to_js_rb_value((int32_t) (raw_rb_abi_value));
return (rb_js_abi_host_js_abi_value_t){ ret };
}
__attribute__((import_module("rb-js-abi-host"), import_name("js-value-to-bool: func(value: handle<js-abi-value>) -> bool")))
int32_t __wasm_import_rb_js_abi_host_js_value_to_bool(int32_t);
bool rb_js_abi_host_js_value_to_bool(rb_js_abi_host_js_abi_value_t value) {
int32_t ret = __wasm_import_rb_js_abi_host_js_value_to_bool((value).idx);
return ret;
}
__attribute__((import_module("rb-js-abi-host"), import_name("js-value-to-string: func(value: handle<js-abi-value>) -> string")))
void __wasm_import_rb_js_abi_host_js_value_to_string(int32_t, int32_t);
void rb_js_abi_host_js_value_to_string(rb_js_abi_host_js_abi_value_t value, rb_js_abi_host_string_t *ret0) {
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 @@ -57,6 +57,7 @@ extern "C"
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);
rb_js_abi_host_js_abi_value_t rb_js_abi_host_rb_object_to_js_rb_value(uint32_t raw_rb_abi_value);
bool rb_js_abi_host_js_value_to_bool(rb_js_abi_host_js_abi_value_t value);
void rb_js_abi_host_js_value_to_string(rb_js_abi_host_js_abi_value_t value, rb_js_abi_host_string_t *ret0);
void rb_js_abi_host_js_value_to_integer(rb_js_abi_host_js_abi_value_t value, rb_js_abi_host_raw_integer_t *ret0);
void rb_js_abi_host_export_js_value_to_host(rb_js_abi_host_js_abi_value_t value);
Expand Down
2 changes: 2 additions & 0 deletions ext/js/bindgen/rb-js-abi-host.wit
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ bool-to-js-bool: func(value: bool) -> js-abi-value
proc-to-js-function: func(value: u32) -> js-abi-value
rb-object-to-js-rb-value: func(raw-rb-abi-value: u32) -> js-abi-value

js-value-to-bool: func(value: js-abi-value) -> bool

js-value-to-string: func(value: js-abi-value) -> string

variant raw-integer {
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 @@ -324,6 +324,17 @@ static VALUE _rb_js_obj_typeof(VALUE obj) {
return rb_str_new(ret0.ptr, ret0.len);
}

/*
* call-seq:
* to_b -> boolean
*
*/
static VALUE _rb_js_obj_to_b(VALUE obj) {
struct jsvalue *p = check_jsvalue(obj);
bool ret0 = rb_js_abi_host_js_value_to_bool(p->abi);
return RBOOL(ret0);
}

/*
* call-seq:
* to_s -> string
Expand Down Expand Up @@ -560,6 +571,7 @@ void Init_js() {
0);
rb_define_private_method(rb_cJS_singleton, "__import_from_js",
_rb_js_import_from_js, 0);
rb_define_method(rb_cJS_Object, "to_b", _rb_js_obj_to_b, 0);
rb_define_method(rb_cJS_Object, "to_s", _rb_js_obj_to_s, 0);
rb_define_alias(rb_cJS_Object, "inspect", "to_s");
rb_define_method(rb_cJS_Object, "to_i", _rb_js_obj_to_i, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface RbJsAbiHost {
boolToJsBool(value: boolean): JsAbiValue;
procToJsFunction(value: number): JsAbiValue;
rbObjectToJsRbValue(rawRbAbiValue: number): JsAbiValue;
jsValueToBool(value: JsAbiValue): boolean;
jsValueToString(value: JsAbiValue): string;
jsValueToInteger(value: JsAbiValue): RawInteger;
exportJsValueToHost(value: JsAbiValue): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export function addRbJsAbiHostToImports(imports, obj, get_export) {
const ret0 = obj.rbObjectToJsRbValue(arg0 >>> 0);
return resources0.insert(ret0);
};
imports["rb-js-abi-host"]["js-value-to-bool: func(value: handle<js-abi-value>) -> bool"] = function(arg0) {
const ret0 = obj.jsValueToBool(resources0.get(arg0));
return ret0 ? 1 : 0;
};
imports["rb-js-abi-host"]["js-value-to-string: func(value: handle<js-abi-value>) -> string"] = function(arg0, arg1) {
const memory = get_export("memory");
const realloc = get_export("cabi_realloc");
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 @@ -213,6 +213,9 @@ export class RubyVM {
rbObjectToJsRbValue: (rawRbAbiValue) => {
return this.rbValueOfPointer(rawRbAbiValue);
},
jsValueToBool: (value) => {
return Boolean(value);
},
jsValueToString: (value) => {
// According to the [spec](https://tc39.es/ecma262/multipage/text-processing.html#sec-string-constructor-string-value)
// `String(value)` always returns a string.
Expand Down
10 changes: 10 additions & 0 deletions packages/npm-packages/ruby-wasm-wasi/test/unit/test_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ def test_strictly_eql?
JS.eval("return undefined;")
end

def test_to_b
assert_true JS.eval("return true;").to_b
assert_false JS.eval("return false;").to_b
assert_false JS.eval("return 0;").to_b
assert_false JS.eval("return -0;").to_b
assert_false JS.eval("return NaN;").to_b
assert_false JS.eval("return null;").to_b
assert_false JS.eval("return undefined;").to_b
end

def test_to_s
assert_equal "str", JS.eval("return 'str';").to_s
assert_equal "24", JS.eval("return 24;").to_s
Expand Down
0