GeezerVison is showing github.com — BridgeJS: Take ownership of source Uint8Array id in `swift_js_init_me… · matthewa26/JavaScriptKit@118db02
File treeExpand file treeCollapse file tree Open diff view settings Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests
Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -297,6 +297,9 @@ public struct BridgeJSLink { | 297 | 297 | printer.write( | 298 | 298 | "const source = \(JSGlueVariableScope.reservedSwift).\(JSGlueVariableScope.reservedMemory).getObject(sourceId);" | 299 | 299 | ) | | 300 | + printer.write( | | 301 | + "\(JSGlueVariableScope.reservedSwift).\(JSGlueVariableScope.reservedMemory).release(sourceId);" | | 302 | + ) | 300 | 303 | printer.write( | 301 | 304 | "const bytes = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, bytesPtr);" | 302 | 305 | ) | |
Collapse file Copy file name to clipboardExpand all lines: Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift +3-30Lines changed: 3 additions & 30 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -238,13 +238,12 @@ struct IntrinsicJSFragment: Sendable { | 238 | 238 | static let stringLowerParameter = IntrinsicJSFragment( | 239 | 239 | parameters: ["value"], | 240 | 240 | printCode: { arguments, context in | 241 | | - let (scope, printer, cleanupCode) = (context.scope, context.printer, context.cleanupCode) | | 241 | + let (scope, printer) = (context.scope, context.printer) | 242 | 242 | let argument = arguments[0] | 243 | 243 | let bytesLabel = scope.variable("\(argument)Bytes") | 244 | 244 | let bytesIdLabel = scope.variable("\(argument)Id") | 245 | 245 | printer.write("const \(bytesLabel) = \(JSGlueVariableScope.reservedTextEncoder).encode(\(argument));") | 246 | 246 | printer.write("const \(bytesIdLabel) = \(JSGlueVariableScope.reservedSwift).memory.retain(\(bytesLabel));") | 247 | | - cleanupCode.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(bytesIdLabel));") | 248 | 247 | return [bytesIdLabel, "\(bytesLabel).length"] | 249 | 248 | } | 250 | 249 | ) | @@ -840,12 +839,6 @@ struct IntrinsicJSFragment: Sendable { | 840 | 839 | printer.write("\(idVar) = \(JSGlueVariableScope.reservedSwift).memory.retain(\(bytesVar));") | 841 | 840 | } | 842 | 841 | printer.write("}") | 843 | | - cleanupCode.write("if (\(idVar) != undefined) {") | 844 | | - cleanupCode.indent { | 845 | | - cleanupCode.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(idVar));") | 846 | | - } | 847 | | - cleanupCode.write("}") | 848 | | - | 849 | 842 | return ["+\(isSomeVar)", "\(isSomeVar) ? \(idVar) : 0", "\(isSomeVar) ? \(bytesVar).length : 0"] | 850 | 843 | case .jsValue: | 851 | 844 | let lowered = try jsValueLower.printCode([value], context) | @@ -1920,11 +1913,6 @@ struct IntrinsicJSFragment: Sendable { | 1920 | 1913 | } | 1921 | 1914 | printer.write("}") | 1922 | 1915 | scope.emitPushI32Parameter("\(isSomeVar) ? 1 : 0", printer: printer) | 1923 | | - cleanup.write("if(\(idVar)) {") | 1924 | | - cleanup.indent { | 1925 | | - cleanup.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(idVar));") | 1926 | | - } | 1927 | | - cleanup.write("}") | 1928 | 1916 | case .int, .uint: | 1929 | 1917 | scope.emitPushI32Parameter("\(isSomeVar) ? (\(value) | 0) : 0", printer: printer) | 1930 | 1918 | scope.emitPushI32Parameter("\(isSomeVar) ? 1 : 0", printer: printer) | @@ -1964,13 +1952,6 @@ struct IntrinsicJSFragment: Sendable { | 1964 | 1952 | } | 1965 | 1953 | printer.write("}") | 1966 | 1954 | scope.emitPushI32Parameter("\(isSomeVar) ? 1 : 0", printer: printer) | 1967 | | - cleanup.write("if(\(idVar)) {") | 1968 | | - cleanup.indent { | 1969 | | - cleanup.write( | 1970 | | - "\(JSGlueVariableScope.reservedSwift).memory.release(\(idVar));" | 1971 | | - ) | 1972 | | - } | 1973 | | - cleanup.write("}") | 1974 | 1955 | case .float: | 1975 | 1956 | scope.emitPushF32Parameter("\(isSomeVar) ? Math.fround(\(value)) : 0.0", printer: printer) | 1976 | 1957 | scope.emitPushI32Parameter("\(isSomeVar) ? 1 : 0", printer: printer) | @@ -2554,15 +2535,14 @@ struct IntrinsicJSFragment: Sendable { | 2554 | 2535 | return IntrinsicJSFragment( | 2555 | 2536 | parameters: ["value"], | 2556 | 2537 | printCode: { arguments, context in | 2557 | | - let (scope, printer, cleanup) = (context.scope, context.printer, context.cleanupCode) | | 2538 | + let (scope, printer) = (context.scope, context.printer) | 2558 | 2539 | let value = arguments[0] | 2559 | 2540 | let bytesVar = scope.variable("bytes") | 2560 | 2541 | let idVar = scope.variable("id") | 2561 | 2542 | printer.write("const \(bytesVar) = \(JSGlueVariableScope.reservedTextEncoder).encode(\(value));") | 2562 | 2543 | printer.write("const \(idVar) = \(JSGlueVariableScope.reservedSwift).memory.retain(\(bytesVar));") | 2563 | 2544 | scope.emitPushI32Parameter("\(bytesVar).length", printer: printer) | 2564 | 2545 | scope.emitPushI32Parameter(idVar, printer: printer) | 2565 | | - cleanup.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(idVar));") | 2566 | 2546 | return [] | 2567 | 2547 | } | 2568 | 2548 | ) | @@ -2632,7 +2612,7 @@ struct IntrinsicJSFragment: Sendable { | 2632 | 2612 | return IntrinsicJSFragment( | 2633 | 2613 | parameters: ["value"], | 2634 | 2614 | printCode: { arguments, context in | 2635 | | - let (scope, printer, cleanup) = (context.scope, context.printer, context.cleanupCode) | | 2615 | + let (scope, printer) = (context.scope, context.printer) | 2636 | 2616 | let value = arguments[0] | 2637 | 2617 | let bytesVar = scope.variable("bytes") | 2638 | 2618 | let idVar = scope.variable("id") | @@ -2644,7 +2624,6 @@ struct IntrinsicJSFragment: Sendable { | 2644 | 2624 | ) | 2645 | 2625 | scope.emitPushI32Parameter("\(bytesVar).length", printer: printer) | 2646 | 2626 | scope.emitPushI32Parameter(idVar, printer: printer) | 2647 | | - cleanup.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(idVar));") | 2648 | 2627 | return [] | 2649 | 2628 | } | 2650 | 2629 | ) | @@ -3088,9 +3067,6 @@ struct IntrinsicJSFragment: Sendable { | 3088 | 3067 | } | 3089 | 3068 | printer.write("}") | 3090 | 3069 | scope.emitPushI32Parameter("\(isSomeVar) ? 1 : 0", printer: printer) | 3091 | | - cleanup.write( | 3092 | | - "if(\(idVar) !== undefined) { \(JSGlueVariableScope.reservedSwift).memory.release(\(idVar)); }" | 3093 | | - ) | 3094 | 3070 | return [idVar] | 3095 | 3071 | case .float: | 3096 | 3072 | printer.write("if (\(isSomeVar)) {") | @@ -3179,9 +3155,6 @@ struct IntrinsicJSFragment: Sendable { | 3179 | 3155 | } | 3180 | 3156 | printer.write("}") | 3181 | 3157 | scope.emitPushI32Parameter("\(isSomeVar) ? 1 : 0", printer: printer) | 3182 | | - cleanup.write( | 3183 | | - "if(\(idVar) !== undefined) { \(JSGlueVariableScope.reservedSwift).memory.release(\(idVar)); }" | 3184 | | - ) | 3185 | 3158 | return [idVar] | 3186 | 3159 | } else if case .jsObject = wrappedType { | 3187 | 3160 | let idVar = scope.variable("id") | |
Collapse file Copy file name to clipboardExpand all lines: Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ArrayTypes.js +1-11Lines changed: 1 addition & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -71,6 +71,7 @@ export async function createInstantiator(options, swift) { | 71 | 71 | } | 72 | 72 | bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { | 73 | 73 | const source = swift.memory.getObject(sourceId); | | 74 | + swift.memory.release(sourceId); | 74 | 75 | const bytes = new Uint8Array(memory.buffer, bytesPtr); | 75 | 76 | bytes.set(source); | 76 | 77 | } | @@ -317,9 +318,6 @@ export async function createInstantiator(options, swift) { | 317 | 318 | const id = swift.memory.retain(bytes); | 318 | 319 | i32Stack.push(bytes.length); | 319 | 320 | i32Stack.push(id); | 320 | | - arrayCleanups.push(() => { | 321 | | - swift.memory.release(id); | 322 | | - }); | 323 | 321 | } | 324 | 322 | i32Stack.push(ret.length); | 325 | 323 | } catch (error) { | @@ -411,9 +409,6 @@ export async function createInstantiator(options, swift) { | 411 | 409 | const id = swift.memory.retain(bytes); | 412 | 410 | i32Stack.push(bytes.length); | 413 | 411 | i32Stack.push(id); | 414 | | - arrayCleanups.push(() => { | 415 | | - swift.memory.release(id); | 416 | | - }); | 417 | 412 | } | 418 | 413 | i32Stack.push(values.length); | 419 | 414 | instance.exports.bjs_processStringArray(); | @@ -539,7 +534,6 @@ export async function createInstantiator(options, swift) { | 539 | 534 | instance.exports.bjs_findFirstPoint(matchingId, matchingBytes.length); | 540 | 535 | const structValue = structHelpers.Point.lift(); | 541 | 536 | for (const cleanup of arrayCleanups) { cleanup(); } | 542 | | - swift.memory.release(matchingId); | 543 | 537 | return structValue; | 544 | 538 | }, | 545 | 539 | processUnsafeRawPointerArray: function bjs_processUnsafeRawPointerArray(values) { | @@ -632,7 +626,6 @@ export async function createInstantiator(options, swift) { | 632 | 626 | const id = swift.memory.retain(bytes); | 633 | 627 | i32Stack.push(bytes.length); | 634 | 628 | i32Stack.push(id); | 635 | | - arrayCleanups.push(() => { swift.memory.release(id); }); | 636 | 629 | } else { | 637 | 630 | i32Stack.push(0); | 638 | 631 | i32Stack.push(0); | @@ -817,9 +810,6 @@ export async function createInstantiator(options, swift) { | 817 | 810 | const id = swift.memory.retain(bytes); | 818 | 811 | i32Stack.push(bytes.length); | 819 | 812 | i32Stack.push(id); | 820 | | - arrayCleanups1.push(() => { | 821 | | - swift.memory.release(id); | 822 | | - }); | 823 | 813 | } | 824 | 814 | i32Stack.push(elem.length); | 825 | 815 | arrayCleanups.push(() => { | |
Collapse file Copy file name to clipboardExpand all lines: Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Async.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -43,6 +43,7 @@ export async function createInstantiator(options, swift) { | 43 | 43 | } | 44 | 44 | bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { | 45 | 45 | const source = swift.memory.getObject(sourceId); | | 46 | + swift.memory.release(sourceId); | 46 | 47 | const bytes = new Uint8Array(memory.buffer, bytesPtr); | 47 | 48 | bytes.set(source); | 48 | 49 | } | @@ -225,7 +226,6 @@ export async function createInstantiator(options, swift) { | 225 | 226 | const ret = instance.exports.bjs_asyncRoundTripString(vId, vBytes.length); | 226 | 227 | const ret1 = swift.memory.getObject(ret); | 227 | 228 | swift.memory.release(ret); | 228 | | - swift.memory.release(vId); | 229 | 229 | return ret1; | 230 | 230 | }, | 231 | 231 | asyncRoundTripBool: function bjs_asyncRoundTripBool(v) { | |
Collapse file Copy file name to clipboardExpand all lines: Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DefaultParameters.js +2-26Lines changed: 2 additions & 26 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,10 +44,7 @@ export async function createInstantiator(options, swift) { | 44 | 44 | i32Stack.push(id); | 45 | 45 | i32Stack.push((value.value | 0)); | 46 | 46 | i32Stack.push(value.enabled ? 1 : 0); | 47 | | - const cleanup = () => { | 48 | | - swift.memory.release(id); | 49 | | - }; | 50 | | - return { cleanup }; | | 47 | + return { cleanup: undefined }; | 51 | 48 | }, | 52 | 49 | lift: () => { | 53 | 50 | const bool = i32Stack.pop() !== 0; | @@ -96,6 +93,7 @@ export async function createInstantiator(options, swift) { | 96 | 93 | } | 97 | 94 | bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) { | 98 | 95 | const source = swift.memory.getObject(sourceId); | | 96 | + swift.memory.release(sourceId); | 99 | 97 | const bytes = new Uint8Array(memory.buffer, bytesPtr); | 100 | 98 | bytes.set(source); | 101 | 99 | } | @@ -325,7 +323,6 @@ export async function createInstantiator(options, swift) { | 325 | 323 | const nameBytes = textEncoder.encode(name); | 326 | 324 | const nameId = swift.memory.retain(nameBytes); | 327 | 325 | const ret = instance.exports.bjs_DefaultGreeter_init(nameId, nameBytes.length); | 328 | | - swift.memory.release(nameId); | 329 | 326 | return DefaultGreeter.__construct(ret); | 330 | 327 | } | 331 | 328 | get name() { | @@ -338,7 +335,6 @@ export async function createInstantiator(options, swift) { | 338 | 335 | const valueBytes = textEncoder.encode(value); | 339 | 336 | const valueId = swift.memory.retain(valueBytes); | 340 | 337 | instance.exports.bjs_DefaultGreeter_name_set(this.pointer, valueId, valueBytes.length); | 341 | | - swift.memory.release(valueId); | 342 | 338 | } | 343 | 339 | } | 344 | 340 | class EmptyGreeter extends SwiftHeapObject { | @@ -366,10 +362,6 @@ export async function createInstantiator(options, swift) { | 366 | 362 | tagId = swift.memory.retain(tagBytes); | 367 | 363 | } | 368 | 364 | const ret = instance.exports.bjs_ConstructorDefaults_init(nameId, nameBytes.length, count, enabled, status, +isSome, isSome ? tagId : 0, isSome ? tagBytes.length : 0); | 369 | | - swift.memory.release(nameId); | 370 | | - if (tagId != undefined) { | 371 | | - swift.memory.release(tagId); | 372 | | - } | 373 | 365 | return ConstructorDefaults.__construct(ret); | 374 | 366 | } | 375 | 367 | get name() { | @@ -382,7 +374,6 @@ export async function createInstantiator(options, swift) { | 382 | 374 | const valueBytes = textEncoder.encode(value); | 383 | 375 | const valueId = swift.memory.retain(valueBytes); | 384 | 376 | instance.exports.bjs_ConstructorDefaults_name_set(this.pointer, valueId, valueBytes.length); | 385 | | - swift.memory.release(valueId); | 386 | 377 | } | 387 | 378 | get count() { | 388 | 379 | const ret = instance.exports.bjs_ConstructorDefaults_count_get(this.pointer); | @@ -419,9 +410,6 @@ export async function createInstantiator(options, swift) { | 419 | 410 | valueId = swift.memory.retain(valueBytes); | 420 | 411 | } | 421 | 412 | instance.exports.bjs_ConstructorDefaults_tag_set(this.pointer, +isSome, isSome ? valueId : 0, isSome ? valueBytes.length : 0); | 422 | | - if (valueId != undefined) { | 423 | | - swift.memory.release(valueId); | 424 | | - } | 425 | 413 | } | 426 | 414 | } | 427 | 415 | const ConfigHelpers = __bjs_createConfigHelpers()(); | @@ -440,7 +428,6 @@ export async function createInstantiator(options, swift) { | 440 | 428 | instance.exports.bjs_testStringDefault(messageId, messageBytes.length); | 441 | 429 | const ret = tmpRetString; | 442 | 430 | tmpRetString = undefined; | 443 | | - swift.memory.release(messageId); | 444 | 431 | return ret; | 445 | 432 | }, | 446 | 433 | testNegativeIntDefault: function bjs_testNegativeIntDefault(value = -42) { | @@ -469,9 +456,6 @@ export async function createInstantiator(options, swift) { | 469 | 456 | instance.exports.bjs_testOptionalDefault(+isSome, isSome ? nameId : 0, isSome ? nameBytes.length : 0); | 470 | 457 | const optResult = tmpRetString; | 471 | 458 | tmpRetString = undefined; | 472 | | - if (nameId != undefined) { | 473 | | - swift.memory.release(nameId); | 474 | | - } | 475 | 459 | return optResult; | 476 | 460 | }, | 477 | 461 | testOptionalStringDefault: function bjs_testOptionalStringDefault(greeting = "Hi") { | @@ -484,9 +468,6 @@ export async function createInstantiator(options, swift) { | 484 | 468 | instance.exports.bjs_testOptionalStringDefault(+isSome, isSome ? greetingId : 0, isSome ? greetingBytes.length : 0); | 485 | 469 | const optResult = tmpRetString; | 486 | 470 | tmpRetString = undefined; | 487 | | - if (greetingId != undefined) { | 488 | | - swift.memory.release(greetingId); | 489 | | - } | 490 | 471 | return optResult; | 491 | 472 | }, | 492 | 473 | testMultipleDefaults: function bjs_testMultipleDefaults(title = "Default Title", count = 10, enabled = false) { | @@ -495,7 +476,6 @@ export async function createInstantiator(options, swift) { | 495 | 476 | instance.exports.bjs_testMultipleDefaults(titleId, titleBytes.length, count, enabled); | 496 | 477 | const ret = tmpRetString; | 497 | 478 | tmpRetString = undefined; | 498 | | - swift.memory.release(titleId); | 499 | 479 | return ret; | 500 | 480 | }, | 501 | 481 | testEnumDefault: function bjs_testEnumDefault(status = StatusValues.Active) { | @@ -572,9 +552,6 @@ export async function createInstantiator(options, swift) { | 572 | 552 | const id = swift.memory.retain(bytes); | 573 | 553 | i32Stack.push(bytes.length); | 574 | 554 | i32Stack.push(id); | 575 | | - arrayCleanups.push(() => { | 576 | | - swift.memory.release(id); | 577 | | - }); | 578 | 555 | } | 579 | 556 | i32Stack.push(names.length); | 580 | 557 | instance.exports.bjs_testStringArrayDefault(); | @@ -650,7 +627,6 @@ export async function createInstantiator(options, swift) { | 650 | 627 | instance.exports.bjs_testMixedWithArrayDefault(nameId, nameBytes.length, enabled); | 651 | 628 | const ret = tmpRetString; | 652 | 629 | tmpRetString = undefined; | 653 | | - swift.memory.release(nameId); | 654 | 630 | for (const cleanup of arrayCleanups) { cleanup(); } | 655 | 631 | return ret; | 656 | 632 | }, | |
|