Skip to content
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

Fix object decode #241

Merged
merged 3 commits into from
Apr 7, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ func expectNotNil<T>(_ value: T?, file: StaticString = #file, line: UInt = #line
throw MessageError("Expect a non-nil value", file: file, line: line, column: column)
}
}
func expectNil<T>(_ value: T?, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws {
switch value {
case .some:
throw MessageError("Expect an nil", file: file, line: line, column: column)
case .none: return
}
}

class Expectation {
private(set) var isFulfilled: Bool = false
Expand Down
42 changes: 42 additions & 0 deletions IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,48 @@ try test("New Object Construction") {
try expectEqual(dog1Bark(), .string("wan"))
}

try test("Object Decoding") {
/*
```js
global.objectDecodingTest = {
obj: {},
fn: () => {},
sym: Symbol("s"),
bi: BigInt(3)
};
```
*/
let js: JSValue = JSObject.global.objectDecodingTest

// I can't use regular name like `js.object` here
// cz its conflicting with case name and DML.
// so I use abbreviated names
let object: JSValue = js.obj
let function: JSValue = js.fn
let symbol: JSValue = js.sym
let bigInt: JSValue = js.bi

try expectNotNil(JSObject.construct(from: object))
try expectEqual(JSObject.construct(from: function).map { $0 is JSFunction }, .some(true))
try expectEqual(JSObject.construct(from: symbol).map { $0 is JSSymbol }, .some(true))
try expectEqual(JSObject.construct(from: bigInt).map { $0 is JSBigInt }, .some(true))

try expectNil(JSFunction.construct(from: object))
try expectNotNil(JSFunction.construct(from: function))
try expectNil(JSFunction.construct(from: symbol))
try expectNil(JSFunction.construct(from: bigInt))

try expectNil(JSSymbol.construct(from: object))
try expectNil(JSSymbol.construct(from: function))
try expectNotNil(JSSymbol.construct(from: symbol))
try expectNil(JSSymbol.construct(from: bigInt))

try expectNil(JSBigInt.construct(from: object))
try expectNil(JSBigInt.construct(from: function))
try expectNil(JSBigInt.construct(from: symbol))
try expectNotNil(JSBigInt.construct(from: bigInt))
}

try test("Call Function With This") {
// ```js
// global.Animal = function(name, age, isCat) {
Expand Down
7 changes: 7 additions & 0 deletions IntegrationTests/bin/primary-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ global.callThrowingClosure = (c) => {
}
};

global.objectDecodingTest = {
obj: {},
fn: () => {},
sym: Symbol("s"),
bi: BigInt(3)
};

const { startWasiTask, WASI } = require("../lib");

startWasiTask("./dist/PrimaryTests.wasm").catch((err) => {
Expand Down
2 changes: 1 addition & 1 deletion IntegrationTests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Sources/JavaScriptKit/FundamentalObjects/JSBigInt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ public final class JSBigInt: JSObject {
super.init(id: _i64_to_bigint_slow(UInt32(value & 0xffffffff), UInt32(value >> 32), false))
}

override public class func construct(from value: JSValue) -> Self? {
value.bigInt as? Self
}

override public var jsValue: JSValue {
.bigInt(self)
}
Expand Down
4 changes: 0 additions & 4 deletions Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ public class JSFunction: JSObject {
fatalError("unavailable")
}

override public class func construct(from value: JSValue) -> Self? {
return value.function as? Self
}

override public var jsValue: JSValue {
.function(self)
}
Expand Down
18 changes: 16 additions & 2 deletions Sources/JavaScriptKit/FundamentalObjects/JSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,22 @@ public class JSObject: Equatable {
return lhs.id == rhs.id
}

public class func construct(from value: JSValue) -> Self? {
return value.object as? Self
public static func construct(from value: JSValue) -> Self? {
switch value {
case .boolean,
.string,
.number,
.null,
.undefined: return nil
case .object(let object):
return object as? Self
case .function(let function):
return function as? Self
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as? Self のキャストがある事で、
このメソッドが JSObject.construct として呼ばれた時と、
JSFunction.construct として呼ばれた時だけ通ります。
JSSymbol.construct として呼ばれている場合は nil になります。
そのため、各種の型でのオーバライド実装が不要になって、
class func から static func に変更できます。

case .symbol(let symbol):
return symbol as? Self
case .bigInt(let bigInt):
return bigInt as? Self
}
}

public var jsValue: JSValue {
Expand Down
4 changes: 0 additions & 4 deletions Sources/JavaScriptKit/FundamentalObjects/JSSymbol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ public class JSSymbol: JSObject {
Symbol.keyFor!(symbol).string
}

override public class func construct(from value: JSValue) -> Self? {
return value.symbol as? Self
}

override public var jsValue: JSValue {
.symbol(self)
}
Expand Down
Loading