Skip to content

Commit 9349cad

Browse files
Support Object interpolation
1 parent 63e11fb commit 9349cad

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

src/swift/Sources/JavaScriptKit/JSValue.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class JSRef: Equatable {
1919
public enum JSValue: Equatable {
2020
case boolean(Bool)
2121
case string(String)
22+
case object(JSRef)
2223
}
2324

2425
protocol JSValueConvertible {
@@ -48,6 +49,8 @@ public func getJSValue(this: JSRef, name: String) -> JSValue {
4849
_load_string(payload1 as JavaScriptValueId, buffer)
4950
let string = String(decodingCString: UnsafePointer(buffer), as: UTF8.self)
5051
return .string(string)
52+
case JavaScriptValueKind_Object:
53+
return .object(JSRef(id: payload1))
5154
default:
5255
fatalError("unreachable")
5356
}
@@ -73,6 +76,10 @@ public func setJSValue(this: JSRef, name: String, value: JSValue) {
7376
)
7477
}
7578
return
79+
case let .object(ref):
80+
kind = JavaScriptValueKind_Object
81+
payload1 = ref.id
82+
payload2 = 0
7683
}
7784
_set_js_value(this.id, name, Int32(name.count), kind, payload1, payload2)
7885
}

src/web/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export class SwiftRuntime {
9696
return readString(payload1, payload2)
9797
}
9898
case JavaScriptValueKind.Object: {
99+
return this._heapValues[payload1]
99100
}
100101
default:
101102
throw new Error(`Type kind "${kind}" is not supported`)
@@ -117,6 +118,13 @@ export class SwiftRuntime {
117118
payload2: value.length,
118119
}
119120
}
121+
case "object": {
122+
return {
123+
kind: JavaScriptValueKind.Object,
124+
payload1: allocValue(value),
125+
payload2: 0,
126+
}
127+
}
120128
default:
121129
throw new Error(`Type "${typeof value}" is not supported yet`)
122130
}
Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
11
import JavaScriptKit
22

3-
func expectEqual<T: Equatable>(_ lhs: T, _ rhs: T) {
3+
struct MessageError: Error {
4+
let message: String
5+
init(_ message: String) {
6+
self.message = message
7+
}
8+
}
9+
10+
func expectEqual<T: Equatable>(_ lhs: T, _ rhs: T) throws {
411
if lhs != rhs {
5-
print("[ERROR] Expect to be equal \"\(lhs)\" and \"\(rhs)\"")
12+
throw MessageError("Expect to be equal \"\(lhs)\" and \"\(rhs)\"")
13+
}
14+
}
15+
16+
func expectObject(_ value: JSValue) throws -> JSRef {
17+
switch value {
18+
case .object(let ref): return ref
19+
default:
20+
throw MessageError("Type of \(value) should be \"object\"")
621
}
722
}
823

9-
let global = JSRef.global()
10-
do {
24+
func expectBoolean(_ value: JSValue) throws -> Bool {
25+
switch value {
26+
case .boolean(let bool): return bool
27+
default:
28+
throw MessageError("Type of \(value) should be \"boolean\"")
29+
}
30+
}
31+
32+
Literal_Conversion: do {
33+
let global = JSRef.global()
1134
let inputs: [JSValue] = [
1235
.boolean(true),
1336
.boolean(false),
@@ -17,6 +40,31 @@ do {
1740
let prop = "prop_\(index)"
1841
setJSValue(this: global, name: prop, value: input)
1942
let got = getJSValue(this: global, name: prop)
20-
expectEqual(input, got)
43+
try expectEqual(input, got)
2144
}
45+
} catch {
46+
print(error)
47+
}
48+
49+
Object_Conversion: do {
50+
// Notes: globalObject1 is defined in JavaScript environment
51+
//
52+
// ```js
53+
// global.globalObject1 = {
54+
// "prop_1": {
55+
// "nested_prop": 1,
56+
// },
57+
// "prop_2": 2,
58+
// "prop_3": true,
59+
// }
60+
// ```
61+
//
62+
63+
let globalObject1 = getJSValue(this: .global(), name: "globalObject1")
64+
let globalObject1Ref = try expectObject(globalObject1)
65+
_ = try expectObject(getJSValue(this: globalObject1Ref, name: "prop_1"))
66+
let prop_3 = getJSValue(this: globalObject1Ref, name: "prop_3")
67+
try expectEqual(prop_3, .boolean(true))
68+
} catch {
69+
print(error)
2270
}

test/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ let wasi = new WASI({
1818
}
1919
});
2020

21+
global.globalObject1 = {
22+
"prop_1": {
23+
"nested_prop": 1,
24+
},
25+
"prop_2": 2,
26+
"prop_3": true,
27+
}
28+
2129
const startWasiTask = async () => {
2230
// Fetch our Wasm File
2331
const wasmBinary = await readFile("./dist/JavaScriptKitExec.wasm");

0 commit comments

Comments
 (0)