File tree 5 files changed +43
-0
lines changed
swift/Sources/JavaScriptKit
JavaScriptKitExec/Sources/JavaScriptKitExec
5 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -16,13 +16,26 @@ public class JSObjectRef: Equatable {
16
16
}
17
17
}
18
18
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
+
19
31
public enum JSValue : Equatable {
20
32
case boolean( Bool )
21
33
case string( String )
22
34
case number( Int32 )
23
35
case object( JSObjectRef )
24
36
case null
25
37
case undefined
38
+ case function( JSFunctionRef )
26
39
}
27
40
28
41
protocol JSValueConvertible {
@@ -66,6 +79,8 @@ extension RawJSValue: JSValueConvertible {
66
79
return . null
67
80
case JavaScriptValueKind_Undefined:
68
81
return . undefined
82
+ case JavaScriptValueKind_Function:
83
+ return . function( JSFunctionRef ( id: payload1) )
69
84
default :
70
85
fatalError ( " unreachable " )
71
86
}
@@ -105,6 +120,10 @@ extension JSValue {
105
120
kind = JavaScriptValueKind_Undefined
106
121
payload1 = 0
107
122
payload2 = 0
123
+ case let . function( functionRef) :
124
+ kind = JavaScriptValueKind_Function
125
+ payload1 = functionRef. id
126
+ payload2 = 0
108
127
}
109
128
let rawValue = RawJSValue ( kind: kind, payload1: payload1, payload2: payload2)
110
129
return body ( rawValue)
Original file line number Diff line number Diff line change @@ -107,6 +107,9 @@ export class SwiftRuntime {
107
107
case JavaScriptValueKind . Undefined : {
108
108
return undefined
109
109
}
110
+ case JavaScriptValueKind . Function : {
111
+ return this . _heapValues [ payload1 ]
112
+ }
110
113
default :
111
114
throw new Error ( `Type kind "${ kind } " is not supported` )
112
115
}
@@ -156,6 +159,13 @@ export class SwiftRuntime {
156
159
payload2 : 0 ,
157
160
}
158
161
}
162
+ case "function" : {
163
+ return {
164
+ kind : JavaScriptValueKind . Function ,
165
+ payload1 : allocValue ( value ) ,
166
+ payload2 : 0 ,
167
+ }
168
+ }
159
169
default :
160
170
throw new Error ( `Type "${ typeof value } " is not supported yet` )
161
171
}
Original file line number Diff line number Diff line change @@ -21,6 +21,14 @@ func expectObject(_ value: JSValue) throws -> JSObjectRef {
21
21
}
22
22
}
23
23
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
+
24
32
func expectBoolean( _ value: JSValue ) throws -> Bool {
25
33
switch value {
26
34
case . boolean( let bool) : return bool
Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ Object_Conversion: do {
35
35
// "prop_4": [
36
36
// 3, 4, "str_elm_1", 5,
37
37
// ],
38
+ // "prop_5": function () {},
38
39
// }
39
40
// ```
40
41
//
@@ -58,6 +59,10 @@ Object_Conversion: do {
58
59
let actualElement = getJSValue ( this: prop_4Array, index: Int32 ( index) )
59
60
try expectEqual ( actualElement, expectedElement)
60
61
}
62
+
63
+ let prop_5 = getJSValue ( this: globalObject1Ref, name: " prop_5 " )
64
+ _ = try expectFunction ( prop_5)
65
+
61
66
} catch {
62
67
print ( error)
63
68
}
Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ global.globalObject1 = {
27
27
"prop_4" : [
28
28
3 , 4 , "str_elm_1" , 5 ,
29
29
] ,
30
+ "prop_5" : function ( ) { } ,
30
31
}
31
32
32
33
const startWasiTask = async ( ) => {
You can’t perform that action at this time.
0 commit comments