Skip to content

Commit

Permalink
Fix object decode (#241)
Browse files Browse the repository at this point in the history
* fix JSObject.construct(from:)

* add unittest

* more precise tests
  • Loading branch information
omochi authored Apr 7, 2024
1 parent 3e07fdc commit 384d668
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 15 deletions.
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
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

0 comments on commit 384d668

Please sign in to comment.