Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ public struct BridgeJSLink {
printer.write(
"const source = \(JSGlueVariableScope.reservedSwift).\(JSGlueVariableScope.reservedMemory).getObject(sourceId);"
)
printer.write(
"\(JSGlueVariableScope.reservedSwift).\(JSGlueVariableScope.reservedMemory).release(sourceId);"
)
printer.write(
"const bytes = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, bytesPtr);"
)
Expand Down
33 changes: 3 additions & 30 deletions Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,12 @@ struct IntrinsicJSFragment: Sendable {
static let stringLowerParameter = IntrinsicJSFragment(
parameters: ["value"],
printCode: { arguments, context in
let (scope, printer, cleanupCode) = (context.scope, context.printer, context.cleanupCode)
let (scope, printer) = (context.scope, context.printer)
let argument = arguments[0]
let bytesLabel = scope.variable("\(argument)Bytes")
let bytesIdLabel = scope.variable("\(argument)Id")
printer.write("const \(bytesLabel) = \(JSGlueVariableScope.reservedTextEncoder).encode(\(argument));")
printer.write("const \(bytesIdLabel) = \(JSGlueVariableScope.reservedSwift).memory.retain(\(bytesLabel));")
cleanupCode.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(bytesIdLabel));")
return [bytesIdLabel, "\(bytesLabel).length"]
}
)
Expand Down Expand Up @@ -840,12 +839,6 @@ struct IntrinsicJSFragment: Sendable {
printer.write("\(idVar) = \(JSGlueVariableScope.reservedSwift).memory.retain(\(bytesVar));")
}
printer.write("}")
cleanupCode.write("if (\(idVar) != undefined) {")
cleanupCode.indent {
cleanupCode.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(idVar));")
}
cleanupCode.write("}")

return ["+\(isSomeVar)", "\(isSomeVar) ? \(idVar) : 0", "\(isSomeVar) ? \(bytesVar).length : 0"]
case .jsValue:
let lowered = try jsValueLower.printCode([value], context)
Expand Down Expand Up @@ -1920,11 +1913,6 @@ struct IntrinsicJSFragment: Sendable {
}
printer.write("}")
scope.emitPushI32Parameter("\(isSomeVar) ? 1 : 0", printer: printer)
cleanup.write("if(\(idVar)) {")
cleanup.indent {
cleanup.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(idVar));")
}
cleanup.write("}")
case .int, .uint:
scope.emitPushI32Parameter("\(isSomeVar) ? (\(value) | 0) : 0", printer: printer)
scope.emitPushI32Parameter("\(isSomeVar) ? 1 : 0", printer: printer)
Expand Down Expand Up @@ -1964,13 +1952,6 @@ struct IntrinsicJSFragment: Sendable {
}
printer.write("}")
scope.emitPushI32Parameter("\(isSomeVar) ? 1 : 0", printer: printer)
cleanup.write("if(\(idVar)) {")
cleanup.indent {
cleanup.write(
"\(JSGlueVariableScope.reservedSwift).memory.release(\(idVar));"
)
}
cleanup.write("}")
case .float:
scope.emitPushF32Parameter("\(isSomeVar) ? Math.fround(\(value)) : 0.0", printer: printer)
scope.emitPushI32Parameter("\(isSomeVar) ? 1 : 0", printer: printer)
Expand Down Expand Up @@ -2554,15 +2535,14 @@ struct IntrinsicJSFragment: Sendable {
return IntrinsicJSFragment(
parameters: ["value"],
printCode: { arguments, context in
let (scope, printer, cleanup) = (context.scope, context.printer, context.cleanupCode)
let (scope, printer) = (context.scope, context.printer)
let value = arguments[0]
let bytesVar = scope.variable("bytes")
let idVar = scope.variable("id")
printer.write("const \(bytesVar) = \(JSGlueVariableScope.reservedTextEncoder).encode(\(value));")
printer.write("const \(idVar) = \(JSGlueVariableScope.reservedSwift).memory.retain(\(bytesVar));")
scope.emitPushI32Parameter("\(bytesVar).length", printer: printer)
scope.emitPushI32Parameter(idVar, printer: printer)
cleanup.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(idVar));")
return []
}
)
Expand Down Expand Up @@ -2632,7 +2612,7 @@ struct IntrinsicJSFragment: Sendable {
return IntrinsicJSFragment(
parameters: ["value"],
printCode: { arguments, context in
let (scope, printer, cleanup) = (context.scope, context.printer, context.cleanupCode)
let (scope, printer) = (context.scope, context.printer)
let value = arguments[0]
let bytesVar = scope.variable("bytes")
let idVar = scope.variable("id")
Expand All @@ -2644,7 +2624,6 @@ struct IntrinsicJSFragment: Sendable {
)
scope.emitPushI32Parameter("\(bytesVar).length", printer: printer)
scope.emitPushI32Parameter(idVar, printer: printer)
cleanup.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(idVar));")
return []
}
)
Expand Down Expand Up @@ -3088,9 +3067,6 @@ struct IntrinsicJSFragment: Sendable {
}
printer.write("}")
scope.emitPushI32Parameter("\(isSomeVar) ? 1 : 0", printer: printer)
cleanup.write(
"if(\(idVar) !== undefined) { \(JSGlueVariableScope.reservedSwift).memory.release(\(idVar)); }"
)
return [idVar]
case .float:
printer.write("if (\(isSomeVar)) {")
Expand Down Expand Up @@ -3179,9 +3155,6 @@ struct IntrinsicJSFragment: Sendable {
}
printer.write("}")
scope.emitPushI32Parameter("\(isSomeVar) ? 1 : 0", printer: printer)
cleanup.write(
"if(\(idVar) !== undefined) { \(JSGlueVariableScope.reservedSwift).memory.release(\(idVar)); }"
)
return [idVar]
} else if case .jsObject = wrappedType {
let idVar = scope.variable("id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export async function createInstantiator(options, swift) {
}
bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) {
const source = swift.memory.getObject(sourceId);
swift.memory.release(sourceId);
const bytes = new Uint8Array(memory.buffer, bytesPtr);
bytes.set(source);
}
Expand Down Expand Up @@ -317,9 +318,6 @@ export async function createInstantiator(options, swift) {
const id = swift.memory.retain(bytes);
i32Stack.push(bytes.length);
i32Stack.push(id);
arrayCleanups.push(() => {
swift.memory.release(id);
});
}
i32Stack.push(ret.length);
} catch (error) {
Expand Down Expand Up @@ -411,9 +409,6 @@ export async function createInstantiator(options, swift) {
const id = swift.memory.retain(bytes);
i32Stack.push(bytes.length);
i32Stack.push(id);
arrayCleanups.push(() => {
swift.memory.release(id);
});
}
i32Stack.push(values.length);
instance.exports.bjs_processStringArray();
Expand Down Expand Up @@ -539,7 +534,6 @@ export async function createInstantiator(options, swift) {
instance.exports.bjs_findFirstPoint(matchingId, matchingBytes.length);
const structValue = structHelpers.Point.lift();
for (const cleanup of arrayCleanups) { cleanup(); }
swift.memory.release(matchingId);
return structValue;
},
processUnsafeRawPointerArray: function bjs_processUnsafeRawPointerArray(values) {
Expand Down Expand Up @@ -632,7 +626,6 @@ export async function createInstantiator(options, swift) {
const id = swift.memory.retain(bytes);
i32Stack.push(bytes.length);
i32Stack.push(id);
arrayCleanups.push(() => { swift.memory.release(id); });
} else {
i32Stack.push(0);
i32Stack.push(0);
Expand Down Expand Up @@ -817,9 +810,6 @@ export async function createInstantiator(options, swift) {
const id = swift.memory.retain(bytes);
i32Stack.push(bytes.length);
i32Stack.push(id);
arrayCleanups1.push(() => {
swift.memory.release(id);
});
}
i32Stack.push(elem.length);
arrayCleanups.push(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export async function createInstantiator(options, swift) {
}
bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) {
const source = swift.memory.getObject(sourceId);
swift.memory.release(sourceId);
const bytes = new Uint8Array(memory.buffer, bytesPtr);
bytes.set(source);
}
Expand Down Expand Up @@ -225,7 +226,6 @@ export async function createInstantiator(options, swift) {
const ret = instance.exports.bjs_asyncRoundTripString(vId, vBytes.length);
const ret1 = swift.memory.getObject(ret);
swift.memory.release(ret);
swift.memory.release(vId);
return ret1;
},
asyncRoundTripBool: function bjs_asyncRoundTripBool(v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ export async function createInstantiator(options, swift) {
i32Stack.push(id);
i32Stack.push((value.value | 0));
i32Stack.push(value.enabled ? 1 : 0);
const cleanup = () => {
swift.memory.release(id);
};
return { cleanup };
return { cleanup: undefined };
},
lift: () => {
const bool = i32Stack.pop() !== 0;
Expand Down Expand Up @@ -96,6 +93,7 @@ export async function createInstantiator(options, swift) {
}
bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) {
const source = swift.memory.getObject(sourceId);
swift.memory.release(sourceId);
const bytes = new Uint8Array(memory.buffer, bytesPtr);
bytes.set(source);
}
Expand Down Expand Up @@ -325,7 +323,6 @@ export async function createInstantiator(options, swift) {
const nameBytes = textEncoder.encode(name);
const nameId = swift.memory.retain(nameBytes);
const ret = instance.exports.bjs_DefaultGreeter_init(nameId, nameBytes.length);
swift.memory.release(nameId);
return DefaultGreeter.__construct(ret);
}
get name() {
Expand All @@ -338,7 +335,6 @@ export async function createInstantiator(options, swift) {
const valueBytes = textEncoder.encode(value);
const valueId = swift.memory.retain(valueBytes);
instance.exports.bjs_DefaultGreeter_name_set(this.pointer, valueId, valueBytes.length);
swift.memory.release(valueId);
}
}
class EmptyGreeter extends SwiftHeapObject {
Expand Down Expand Up @@ -366,10 +362,6 @@ export async function createInstantiator(options, swift) {
tagId = swift.memory.retain(tagBytes);
}
const ret = instance.exports.bjs_ConstructorDefaults_init(nameId, nameBytes.length, count, enabled, status, +isSome, isSome ? tagId : 0, isSome ? tagBytes.length : 0);
swift.memory.release(nameId);
if (tagId != undefined) {
swift.memory.release(tagId);
}
return ConstructorDefaults.__construct(ret);
}
get name() {
Expand All @@ -382,7 +374,6 @@ export async function createInstantiator(options, swift) {
const valueBytes = textEncoder.encode(value);
const valueId = swift.memory.retain(valueBytes);
instance.exports.bjs_ConstructorDefaults_name_set(this.pointer, valueId, valueBytes.length);
swift.memory.release(valueId);
}
get count() {
const ret = instance.exports.bjs_ConstructorDefaults_count_get(this.pointer);
Expand Down Expand Up @@ -419,9 +410,6 @@ export async function createInstantiator(options, swift) {
valueId = swift.memory.retain(valueBytes);
}
instance.exports.bjs_ConstructorDefaults_tag_set(this.pointer, +isSome, isSome ? valueId : 0, isSome ? valueBytes.length : 0);
if (valueId != undefined) {
swift.memory.release(valueId);
}
}
}
const ConfigHelpers = __bjs_createConfigHelpers()();
Expand All @@ -440,7 +428,6 @@ export async function createInstantiator(options, swift) {
instance.exports.bjs_testStringDefault(messageId, messageBytes.length);
const ret = tmpRetString;
tmpRetString = undefined;
swift.memory.release(messageId);
return ret;
},
testNegativeIntDefault: function bjs_testNegativeIntDefault(value = -42) {
Expand Down Expand Up @@ -469,9 +456,6 @@ export async function createInstantiator(options, swift) {
instance.exports.bjs_testOptionalDefault(+isSome, isSome ? nameId : 0, isSome ? nameBytes.length : 0);
const optResult = tmpRetString;
tmpRetString = undefined;
if (nameId != undefined) {
swift.memory.release(nameId);
}
return optResult;
},
testOptionalStringDefault: function bjs_testOptionalStringDefault(greeting = "Hi") {
Expand All @@ -484,9 +468,6 @@ export async function createInstantiator(options, swift) {
instance.exports.bjs_testOptionalStringDefault(+isSome, isSome ? greetingId : 0, isSome ? greetingBytes.length : 0);
const optResult = tmpRetString;
tmpRetString = undefined;
if (greetingId != undefined) {
swift.memory.release(greetingId);
}
return optResult;
},
testMultipleDefaults: function bjs_testMultipleDefaults(title = "Default Title", count = 10, enabled = false) {
Expand All @@ -495,7 +476,6 @@ export async function createInstantiator(options, swift) {
instance.exports.bjs_testMultipleDefaults(titleId, titleBytes.length, count, enabled);
const ret = tmpRetString;
tmpRetString = undefined;
swift.memory.release(titleId);
return ret;
},
testEnumDefault: function bjs_testEnumDefault(status = StatusValues.Active) {
Expand Down Expand Up @@ -572,9 +552,6 @@ export async function createInstantiator(options, swift) {
const id = swift.memory.retain(bytes);
i32Stack.push(bytes.length);
i32Stack.push(id);
arrayCleanups.push(() => {
swift.memory.release(id);
});
}
i32Stack.push(names.length);
instance.exports.bjs_testStringArrayDefault();
Expand Down Expand Up @@ -650,7 +627,6 @@ export async function createInstantiator(options, swift) {
instance.exports.bjs_testMixedWithArrayDefault(nameId, nameBytes.length, enabled);
const ret = tmpRetString;
tmpRetString = undefined;
swift.memory.release(nameId);
for (const cleanup of arrayCleanups) { cleanup(); }
return ret;
},
Expand Down
Loading