@@ -15,13 +15,15 @@ public protocol JSClosureProtocol: JSValueCompatible {
1515public class JSOneshotClosure : JSObject , JSClosureProtocol {
1616 private var hostFuncRef : JavaScriptHostFuncRef = 0
1717
18- public init ( _ body: @escaping ( [ JSValue ] ) -> JSValue ) {
18+ public init ( _ body: @escaping ( [ JSValue ] ) -> JSValue , file : String = #fileID , line : UInt32 = #line ) {
1919 // 1. Fill `id` as zero at first to access `self` to get `ObjectIdentifier`.
2020 super. init ( id: 0 )
2121
2222 // 2. Create a new JavaScript function which calls the given Swift function.
2323 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+ }
2527
2628 // 3. Retain the given body in static storage by `funcRef`.
2729 JSClosure . sharedClosures [ hostFuncRef] = ( self , {
@@ -72,13 +74,15 @@ public class JSClosure: JSObject, JSClosureProtocol {
7274 } )
7375 }
7476
75- public init ( _ body: @escaping ( [ JSValue ] ) -> JSValue ) {
77+ public init ( _ body: @escaping ( [ JSValue ] ) -> JSValue , file : String = #fileID , line : UInt32 = #line ) {
7678 // 1. Fill `id` as zero at first to access `self` to get `ObjectIdentifier`.
7779 super. init ( id: 0 )
7880
7981 // 2. Create a new JavaScript function which calls the given Swift function.
8082 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+ }
8286
8387 // 3. Retain the given body in static storage by `funcRef`.
8488 Self . sharedClosures [ hostFuncRef] = ( self , body)
@@ -128,19 +132,21 @@ public class JSClosure: JSObject, JSClosureProtocol {
128132// │ │ │
129133// └─────────────────────┴──────────────────────────┘
130134
135+ /// Returns true if the host function has been already released, otherwise false.
131136@_cdecl ( " _call_host_function_impl " )
132137func _call_host_function_impl(
133138 _ hostFuncRef: JavaScriptHostFuncRef ,
134139 _ argv: UnsafePointer < RawJSValue > , _ argc: Int32 ,
135140 _ callbackFuncRef: JavaScriptObjectRef
136- ) {
141+ ) -> Bool {
137142 guard let ( _, hostFunc) = JSClosure . sharedClosures [ hostFuncRef] else {
138- fatalError ( " The function was already released " )
143+ return true
139144 }
140145 let arguments = UnsafeBufferPointer ( start: argv, count: Int ( argc) ) . map ( \. jsValue)
141146 let result = hostFunc ( arguments)
142147 let callbackFuncRef = JSFunction ( id: callbackFuncRef)
143148 _ = callbackFuncRef ( result)
149+ return false
144150}
145151
146152
0 commit comments