forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSFunction.swift
More file actions
133 lines (115 loc) · 4.59 KB
/
JSFunction.swift
File metadata and controls
133 lines (115 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import _CJavaScriptKit
public class JSFunctionRef: JSObjectRef {
@discardableResult
public func callAsFunction(this: JSObjectRef? = nil, arguments: [JSValueConvertible]) -> JSValue {
let result = arguments.withRawJSValues { rawValues in
rawValues.withUnsafeBufferPointer { bufferPointer -> RawJSValue in
let argv = bufferPointer.baseAddress
let argc = bufferPointer.count
var result = RawJSValue()
if let thisId = this?.id {
_call_function_with_this(thisId,
self.id, argv, Int32(argc),
&result.kind, &result.payload1, &result.payload2, &result.payload3)
} else {
_call_function(
self.id, argv, Int32(argc),
&result.kind, &result.payload1, &result.payload2, &result.payload3
)
}
return result
}
}
return result.jsValue()
}
@discardableResult
public func callAsFunction(this: JSObjectRef? = nil, _ arguments: JSValueConvertible...) -> JSValue {
self(this: this, arguments: arguments)
}
public func new(_ arguments: JSValueConvertible...) -> JSObjectRef {
new(arguments: arguments)
}
// Guaranteed to return an object because either:
// a) the constructor explicitly returns an object, or
// b) the constructor returns nothing, which causes JS to return the `this` value, or
// c) the constructor returns undefined, null or a non-object, in which case JS also returns `this`.
public func new(arguments: [JSValueConvertible]) -> JSObjectRef {
arguments.withRawJSValues { rawValues in
rawValues.withUnsafeBufferPointer { bufferPointer in
let argv = bufferPointer.baseAddress
let argc = bufferPointer.count
var resultObj = JavaScriptObjectRef()
_call_new(
self.id, argv, Int32(argc),
&resultObj
)
return JSObjectRef(id: resultObj)
}
}
}
@available(*, unavailable, message: "Please use JSClosure instead")
public static func from(_: @escaping ([JSValue]) -> JSValue) -> JSFunctionRef {
fatalError("unavailable")
}
override public func jsValue() -> JSValue {
.function(self)
}
}
public class JSClosure: JSFunctionRef {
static var sharedFunctions: [JavaScriptHostFuncRef: ([JSValue]) -> JSValue] = [:]
private var hostFuncRef: JavaScriptHostFuncRef = 0
private var isReleased = false
public init(_ body: @escaping ([JSValue]) -> JSValue) {
super.init(id: 0)
let objectId = ObjectIdentifier(self)
let funcRef = JavaScriptHostFuncRef(bitPattern: Int32(objectId.hashValue))
Self.sharedFunctions[funcRef] = body
var objectRef: JavaScriptObjectRef = 0
_create_function(funcRef, &objectRef)
hostFuncRef = funcRef
id = objectRef
}
convenience public init(_ body: @escaping ([JSValue]) -> ()) {
self.init { (arguments: [JSValue]) -> JSValue in
body(arguments)
return .undefined
}
}
public func release() {
Self.sharedFunctions[hostFuncRef] = nil
isReleased = true
}
deinit {
guard isReleased else {
fatalError("""
release() must be called on closures manually before deallocating.
This is caused by the lack of support for the `FinalizationRegistry` API in Safari.
""")
}
}
}
@_cdecl("swjs_prepare_host_function_call")
public func _prepare_host_function_call(_ argc: Int32) -> UnsafeMutableRawPointer {
let argumentSize = MemoryLayout<RawJSValue>.size * Int(argc)
return malloc(Int(argumentSize))!
}
@_cdecl("swjs_cleanup_host_function_call")
public func _cleanup_host_function_call(_ pointer: UnsafeMutableRawPointer) {
free(pointer)
}
@_cdecl("swjs_call_host_function")
public func _call_host_function(
_ hostFuncRef: JavaScriptHostFuncRef,
_ argv: UnsafePointer<RawJSValue>, _ argc: Int32,
_ callbackFuncRef: JavaScriptObjectRef
) {
guard let hostFunc = JSClosure.sharedFunctions[hostFuncRef] else {
fatalError("The function was already released")
}
let arguments = UnsafeBufferPointer(start: argv, count: Int(argc)).map {
$0.jsValue()
}
let result = hostFunc(arguments)
let callbackFuncRef = JSFunctionRef(id: callbackFuncRef)
_ = callbackFuncRef(result)
}