swiftwasm / JavaScriptKit

Swift framework to interact with JavaScript through WebAssembly.
https://swiftpackageindex.com/swiftwasm/JavaScriptKit/main/documentation/javascriptkit
MIT License
664 stars 44 forks source link

Fix object decode #241

Closed omochi closed 5 months ago

omochi commented 5 months ago

The current JSObject.construct(from:) failed with JSValues where its case is function, symbol, or bigInt.

For example, JSFunction.construct succeeds with case function and JSObject.construct doesn't, but it should succeed because JSFunction is a JSObject.

symbol and bigint are primitive values in JavaScript side, and because of:

Symbol("s") instanceof Object // false

It's unclear if JSObject.construct(symbol) should succeed or nor, but I think it should because JSSymbol is a JSObject.

Note for function case:

(() => {}) instanceof Object // true

従来の実装では、JSValue の case が function, symbol, bigInt であるような JSValue を、 JSObject.construct(from:) に渡した場合にデコードに失敗していました。

例えば JSFunction.construct に case function を渡す場合は成功しますが、 値がfunction であるとわからない状況においても、 JSFunction is JSObject である以上、 JSObject としてのデコードが成功する必要があります。

symbol と bigInt については JavaScript側においてはプリミティブであり、

Symbol("s") instanceof Object // false

なので、 JSObject としてデコードできるのが正しいのかよくわからないですが、 少なくとも JSSymbol is JSObject となっている以上は、 そうなるべきだと思います。

ちなみに function については

(() => {}) instanceof Object // true

です。