Skip to content

Commit 738d569

Browse files
committedMar 4, 2020
Support function interpolation
1 parent 44991c2 commit 738d569

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed
 

Diff for: ‎src/swift/Sources/JavaScriptKit/JSValue.swift

+19
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,26 @@ public class JSObjectRef: Equatable {
1616
}
1717
}
1818

19+
public class JSFunctionRef: Equatable {
20+
let id: UInt32
21+
22+
fileprivate init(id: UInt32) {
23+
self.id = id
24+
}
25+
26+
public static func == (lhs: JSFunctionRef, rhs: JSFunctionRef) -> Bool {
27+
return lhs.id == rhs.id
28+
}
29+
}
30+
1931
public enum JSValue: Equatable {
2032
case boolean(Bool)
2133
case string(String)
2234
case number(Int32)
2335
case object(JSObjectRef)
2436
case null
2537
case undefined
38+
case function(JSFunctionRef)
2639
}
2740

2841
protocol JSValueConvertible {
@@ -66,6 +79,8 @@ extension RawJSValue: JSValueConvertible {
6679
return .null
6780
case JavaScriptValueKind_Undefined:
6881
return .undefined
82+
case JavaScriptValueKind_Function:
83+
return .function(JSFunctionRef(id: payload1))
6984
default:
7085
fatalError("unreachable")
7186
}
@@ -105,6 +120,10 @@ extension JSValue {
105120
kind = JavaScriptValueKind_Undefined
106121
payload1 = 0
107122
payload2 = 0
123+
case let .function(functionRef):
124+
kind = JavaScriptValueKind_Function
125+
payload1 = functionRef.id
126+
payload2 = 0
108127
}
109128
let rawValue = RawJSValue(kind: kind, payload1: payload1, payload2: payload2)
110129
return body(rawValue)

Diff for: ‎src/web/src/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ export class SwiftRuntime {
107107
case JavaScriptValueKind.Undefined: {
108108
return undefined
109109
}
110+
case JavaScriptValueKind.Function: {
111+
return this._heapValues[payload1]
112+
}
110113
default:
111114
throw new Error(`Type kind "${kind}" is not supported`)
112115
}
@@ -156,6 +159,13 @@ export class SwiftRuntime {
156159
payload2: 0,
157160
}
158161
}
162+
case "function": {
163+
return {
164+
kind: JavaScriptValueKind.Function,
165+
payload1: allocValue(value),
166+
payload2: 0,
167+
}
168+
}
159169
default:
160170
throw new Error(`Type "${typeof value}" is not supported yet`)
161171
}

Diff for: ‎test/JavaScriptKitExec/Sources/JavaScriptKitExec/UnitTestUtils.swift

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ func expectObject(_ value: JSValue) throws -> JSObjectRef {
2121
}
2222
}
2323

24+
func expectFunction(_ value: JSValue) throws -> JSFunctionRef {
25+
switch value {
26+
case .function(let ref): return ref
27+
default:
28+
throw MessageError("Type of \(value) should be \"function\"")
29+
}
30+
}
31+
2432
func expectBoolean(_ value: JSValue) throws -> Bool {
2533
switch value {
2634
case .boolean(let bool): return bool

Diff for: ‎test/JavaScriptKitExec/Sources/JavaScriptKitExec/main.swift

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Object_Conversion: do {
3535
// "prop_4": [
3636
// 3, 4, "str_elm_1", 5,
3737
// ],
38+
// "prop_5": function () {},
3839
// }
3940
// ```
4041
//
@@ -58,6 +59,10 @@ Object_Conversion: do {
5859
let actualElement = getJSValue(this: prop_4Array, index: Int32(index))
5960
try expectEqual(actualElement, expectedElement)
6061
}
62+
63+
let prop_5 = getJSValue(this: globalObject1Ref, name: "prop_5")
64+
_ = try expectFunction(prop_5)
65+
6166
} catch {
6267
print(error)
6368
}

Diff for: ‎test/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ global.globalObject1 = {
2727
"prop_4": [
2828
3, 4, "str_elm_1", 5,
2929
],
30+
"prop_5": function () {},
3031
}
3132

3233
const startWasiTask = async () => {

0 commit comments

Comments
 (0)
Please sign in to comment.