GeezerVison is showing github.com — Stop managing JSOneshotClosure by FinalizationRegistry · cuberoot/JavaScriptKit@7f9094e
File treeExpand file treeCollapse file tree Open diff view settings Plugins/PackageToJS/Templates JavaScriptKit/FundamentalObjects
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Plugins/PackageToJS/Templates/runtime.mjs +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -638,6 +638,12 @@ class SwiftRuntime { | 638 | 638 | (_a = this.closureDeallocator) === null || _a === void 0 ? void 0 : _a.track(func, host_func_id); | 639 | 639 | return func_ref; | 640 | 640 | }, | | 641 | + swjs_create_oneshot_function: (host_func_id, line, file) => { | | 642 | + const fileString = this.memory.getObject(file); | | 643 | + const func = (...args) => this.callHostFunction(host_func_id, line, fileString, args); | | 644 | + const func_ref = this.memory.retain(func); | | 645 | + return func_ref; | | 646 | + }, | 641 | 647 | swjs_create_typed_array: (constructor_ref, elementsPtr, length) => { | 642 | 648 | const ArrayType = this.memory.getObject(constructor_ref); | 643 | 649 | if (length == 0) { | |
Collapse file Copy file name to clipboardExpand all lines: Runtime/src/index.ts +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -557,6 +557,18 @@ export class SwiftRuntime { | 557 | 557 | return func_ref; | 558 | 558 | }, | 559 | 559 |
| | 560 | + swjs_create_oneshot_function: ( | | 561 | + host_func_id: number, | | 562 | + line: number, | | 563 | + file: ref | | 564 | + ) => { | | 565 | + const fileString = this.memory.getObject(file) as string; | | 566 | + const func = (...args: any[]) => | | 567 | + this.callHostFunction(host_func_id, line, fileString, args); | | 568 | + const func_ref = this.memory.retain(func); | | 569 | + return func_ref; | | 570 | + }, | | 571 | + | 560 | 572 | swjs_create_typed_array: <T extends Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | BigInt64Array | BigUint64Array | Float32Array | Float64Array | Uint8ClampedArray>( | 561 | 573 | constructor_ref: ref, | 562 | 574 | elementsPtr: pointer, | |
Collapse file Copy file name to clipboardExpand all lines: Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,7 +25,7 @@ public class JSOneshotClosure: JSObject, JSClosureProtocol { | 25 | 25 | // 2. Create a new JavaScript function which calls the given Swift function. | 26 | 26 | hostFuncRef = JavaScriptHostFuncRef(bitPattern: ObjectIdentifier(self)) | 27 | 27 | _id = withExtendedLifetime(JSString(file)) { file in | 28 | | - swjs_create_function(hostFuncRef, line, file.asInternalJSRef()) | | 28 | + swjs_create_oneshot_function(hostFuncRef, line, file.asInternalJSRef()) | 29 | 29 | } | 30 | 30 |
| 31 | 31 | // 3. Retain the given body in static storage by `funcRef`. | |
Collapse file Copy file name to clipboardExpand all lines: Sources/_CJavaScriptKit/include/_CJavaScriptKit.h +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -278,6 +278,16 @@ IMPORT_JS_FUNCTION(swjs_create_function, JavaScriptObjectRef, (const JavaScriptH | 278 | 278 | unsigned int line, | 279 | 279 | JavaScriptObjectRef file)) | 280 | 280 |
| | 281 | +/// Creates a oneshot JavaScript thunk function that calls Swift side closure. | | 282 | +/// | | 283 | +/// @param host_func_id The target Swift side function called by the created thunk function. | | 284 | +/// @param line The line where the function is created. Will be used for diagnostics | | 285 | +/// @param file The file name where the function is created. Will be used for diagnostics | | 286 | +/// @returns A reference to the newly-created JavaScript thunk function | | 287 | +IMPORT_JS_FUNCTION(swjs_create_oneshot_function, JavaScriptObjectRef, (const JavaScriptHostFuncRef host_func_id, | | 288 | + unsigned int line, | | 289 | + JavaScriptObjectRef file)) | | 290 | + | 281 | 291 | /// Instantiates a new `TypedArray` object with given elements | 282 | 292 | /// This is used to provide an efficient way to create `TypedArray`. | 283 | 293 | /// | |
|