Skip to content

Commit 2e3fac7

Browse files
Merge pull request #25 from ruby/pr-8f8a37b33be4c86bada5168d60bc6d442b1ea14b
Inspect JS value by `String(v)` instead of `v.toString`
2 parents 04d7511 + 7d97098 commit 2e3fac7

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

packages/npm-packages/ruby-wasm-wasi/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ export class RubyVM {
102102
return new RbValue(abiValue, this, this.privateObject());
103103
},
104104
jsValueToString: (value) => {
105-
return value.toString();
105+
// According to the [spec](https://tc39.es/ecma262/multipage/text-processing.html#sec-string-constructor-string-value)
106+
// `String(value)` always returns a string.
107+
return String(value);
106108
},
107109
exportJsValueToHost: (value) => {
108110
// See `JsValueExporter` for the reason why we need to do this

packages/npm-packages/ruby-wasm-wasi/test/js_from_rb.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ describe("Manipulation of JS from Ruby", () => {
133133
test.each([
134134
{ expr: `JS.global`, expected: "[object Object]" },
135135
{ expr: `1.to_js`, expected: "1" },
136+
{ expr: `JS.eval("return null")`, expected: "null" },
137+
{ expr: `JS.eval("return undefined")`, expected: "undefined" },
138+
{ expr: `JS.eval("return Symbol('sym')")`, expected: "Symbol(sym)" },
136139
{ expr: `JS.eval("return {}")`, expected: "[object Object]" },
137140
{
138141
expr: `JS.eval("class X {}; return new X()")`,

0 commit comments

Comments
 (0)