@@ -15,13 +15,15 @@ public protocol JSClosureProtocol: JSValueCompatible {
15
15
public class JSOneshotClosure : JSObject , JSClosureProtocol {
16
16
private var hostFuncRef : JavaScriptHostFuncRef = 0
17
17
18
- public init ( _ body: @escaping ( [ JSValue ] ) -> JSValue ) {
18
+ public init ( _ body: @escaping ( [ JSValue ] ) -> JSValue , file : String = #fileID , line : UInt32 = #line ) {
19
19
// 1. Fill `id` as zero at first to access `self` to get `ObjectIdentifier`.
20
20
super. init ( id: 0 )
21
21
22
22
// 2. Create a new JavaScript function which calls the given Swift function.
23
23
hostFuncRef = JavaScriptHostFuncRef ( bitPattern: Int32 ( ObjectIdentifier ( self ) . hashValue) )
24
- id = _create_function ( hostFuncRef)
24
+ id = withExtendedLifetime ( JSString ( file) ) { file in
25
+ _create_function ( hostFuncRef, line, file. asInternalJSRef ( ) )
26
+ }
25
27
26
28
// 3. Retain the given body in static storage by `funcRef`.
27
29
JSClosure . sharedClosures [ hostFuncRef] = ( self , {
@@ -72,13 +74,15 @@ public class JSClosure: JSObject, JSClosureProtocol {
72
74
} )
73
75
}
74
76
75
- public init ( _ body: @escaping ( [ JSValue ] ) -> JSValue ) {
77
+ public init ( _ body: @escaping ( [ JSValue ] ) -> JSValue , file : String = #fileID , line : UInt32 = #line ) {
76
78
// 1. Fill `id` as zero at first to access `self` to get `ObjectIdentifier`.
77
79
super. init ( id: 0 )
78
80
79
81
// 2. Create a new JavaScript function which calls the given Swift function.
80
82
hostFuncRef = JavaScriptHostFuncRef ( bitPattern: Int32 ( ObjectIdentifier ( self ) . hashValue) )
81
- id = _create_function ( hostFuncRef)
83
+ id = withExtendedLifetime ( JSString ( file) ) { file in
84
+ _create_function ( hostFuncRef, line, file. asInternalJSRef ( ) )
85
+ }
82
86
83
87
// 3. Retain the given body in static storage by `funcRef`.
84
88
Self . sharedClosures [ hostFuncRef] = ( self , body)
@@ -128,19 +132,21 @@ public class JSClosure: JSObject, JSClosureProtocol {
128
132
// │ │ │
129
133
// └─────────────────────┴──────────────────────────┘
130
134
135
+ /// Returns true if the host function has been already released, otherwise false.
131
136
@_cdecl ( " _call_host_function_impl " )
132
137
func _call_host_function_impl(
133
138
_ hostFuncRef: JavaScriptHostFuncRef ,
134
139
_ argv: UnsafePointer < RawJSValue > , _ argc: Int32 ,
135
140
_ callbackFuncRef: JavaScriptObjectRef
136
- ) {
141
+ ) -> Bool {
137
142
guard let ( _, hostFunc) = JSClosure . sharedClosures [ hostFuncRef] else {
138
- fatalError ( " The function was already released " )
143
+ return true
139
144
}
140
145
let arguments = UnsafeBufferPointer ( start: argv, count: Int ( argc) ) . map ( \. jsValue)
141
146
let result = hostFunc ( arguments)
142
147
let callbackFuncRef = JSFunction ( id: callbackFuncRef)
143
148
_ = callbackFuncRef ( result)
149
+ return false
144
150
}
145
151
146
152
0 commit comments