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
45 changes: 15 additions & 30 deletions Benchmarks/Sources/Generated/BridgeJS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ extension SimpleStruct: _BridgedSwiftStruct {
}

init(unsafelyCopying jsObject: JSObject) {
let __bjs_cleanupId = _bjs_struct_lower_SimpleStruct(jsObject.bridgeJSLowerParameter())
defer {
_swift_js_struct_cleanup(__bjs_cleanupId)
}
_bjs_struct_lower_SimpleStruct(jsObject.bridgeJSLowerParameter())
self = Self.bridgeJSStackPop()
}

Expand All @@ -148,9 +145,9 @@ extension SimpleStruct: _BridgedSwiftStruct {

#if arch(wasm32)
@_extern(wasm, module: "bjs", name: "swift_js_struct_lower_SimpleStruct")
fileprivate func _bjs_struct_lower_SimpleStruct(_ objectId: Int32) -> Int32
fileprivate func _bjs_struct_lower_SimpleStruct(_ objectId: Int32) -> Void
#else
fileprivate func _bjs_struct_lower_SimpleStruct(_ objectId: Int32) -> Int32 {
fileprivate func _bjs_struct_lower_SimpleStruct(_ objectId: Int32) -> Void {
fatalError("Only available on WebAssembly")
}
#endif
Expand Down Expand Up @@ -179,10 +176,7 @@ extension Address: _BridgedSwiftStruct {
}

init(unsafelyCopying jsObject: JSObject) {
let __bjs_cleanupId = _bjs_struct_lower_Address(jsObject.bridgeJSLowerParameter())
defer {
_swift_js_struct_cleanup(__bjs_cleanupId)
}
_bjs_struct_lower_Address(jsObject.bridgeJSLowerParameter())
self = Self.bridgeJSStackPop()
}

Expand All @@ -195,9 +189,9 @@ extension Address: _BridgedSwiftStruct {

#if arch(wasm32)
@_extern(wasm, module: "bjs", name: "swift_js_struct_lower_Address")
fileprivate func _bjs_struct_lower_Address(_ objectId: Int32) -> Int32
fileprivate func _bjs_struct_lower_Address(_ objectId: Int32) -> Void
#else
fileprivate func _bjs_struct_lower_Address(_ objectId: Int32) -> Int32 {
fileprivate func _bjs_struct_lower_Address(_ objectId: Int32) -> Void {
fatalError("Only available on WebAssembly")
}
#endif
Expand Down Expand Up @@ -232,10 +226,7 @@ extension Person: _BridgedSwiftStruct {
}

init(unsafelyCopying jsObject: JSObject) {
let __bjs_cleanupId = _bjs_struct_lower_Person(jsObject.bridgeJSLowerParameter())
defer {
_swift_js_struct_cleanup(__bjs_cleanupId)
}
_bjs_struct_lower_Person(jsObject.bridgeJSLowerParameter())
self = Self.bridgeJSStackPop()
}

Expand All @@ -248,9 +239,9 @@ extension Person: _BridgedSwiftStruct {

#if arch(wasm32)
@_extern(wasm, module: "bjs", name: "swift_js_struct_lower_Person")
fileprivate func _bjs_struct_lower_Person(_ objectId: Int32) -> Int32
fileprivate func _bjs_struct_lower_Person(_ objectId: Int32) -> Void
#else
fileprivate func _bjs_struct_lower_Person(_ objectId: Int32) -> Int32 {
fileprivate func _bjs_struct_lower_Person(_ objectId: Int32) -> Void {
fatalError("Only available on WebAssembly")
}
#endif
Expand Down Expand Up @@ -285,10 +276,7 @@ extension ComplexStruct: _BridgedSwiftStruct {
}

init(unsafelyCopying jsObject: JSObject) {
let __bjs_cleanupId = _bjs_struct_lower_ComplexStruct(jsObject.bridgeJSLowerParameter())
defer {
_swift_js_struct_cleanup(__bjs_cleanupId)
}
_bjs_struct_lower_ComplexStruct(jsObject.bridgeJSLowerParameter())
self = Self.bridgeJSStackPop()
}

Expand All @@ -301,9 +289,9 @@ extension ComplexStruct: _BridgedSwiftStruct {

#if arch(wasm32)
@_extern(wasm, module: "bjs", name: "swift_js_struct_lower_ComplexStruct")
fileprivate func _bjs_struct_lower_ComplexStruct(_ objectId: Int32) -> Int32
fileprivate func _bjs_struct_lower_ComplexStruct(_ objectId: Int32) -> Void
#else
fileprivate func _bjs_struct_lower_ComplexStruct(_ objectId: Int32) -> Int32 {
fileprivate func _bjs_struct_lower_ComplexStruct(_ objectId: Int32) -> Void {
fatalError("Only available on WebAssembly")
}
#endif
Expand All @@ -330,10 +318,7 @@ extension Point: _BridgedSwiftStruct {
}

init(unsafelyCopying jsObject: JSObject) {
let __bjs_cleanupId = _bjs_struct_lower_Point(jsObject.bridgeJSLowerParameter())
defer {
_swift_js_struct_cleanup(__bjs_cleanupId)
}
_bjs_struct_lower_Point(jsObject.bridgeJSLowerParameter())
self = Self.bridgeJSStackPop()
}

Expand All @@ -346,9 +331,9 @@ extension Point: _BridgedSwiftStruct {

#if arch(wasm32)
@_extern(wasm, module: "bjs", name: "swift_js_struct_lower_Point")
fileprivate func _bjs_struct_lower_Point(_ objectId: Int32) -> Int32
fileprivate func _bjs_struct_lower_Point(_ objectId: Int32) -> Void
#else
fileprivate func _bjs_struct_lower_Point(_ objectId: Int32) -> Int32 {
fileprivate func _bjs_struct_lower_Point(_ objectId: Int32) -> Void {
fatalError("Only available on WebAssembly")
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ extension PlayBridgeJSOutput: _BridgedSwiftStruct {
}

init(unsafelyCopying jsObject: JSObject) {
let __bjs_cleanupId = _bjs_struct_lower_PlayBridgeJSOutput(jsObject.bridgeJSLowerParameter())
defer {
_swift_js_struct_cleanup(__bjs_cleanupId)
}
_bjs_struct_lower_PlayBridgeJSOutput(jsObject.bridgeJSLowerParameter())
self = Self.bridgeJSStackPop()
}

Expand All @@ -40,9 +37,9 @@ extension PlayBridgeJSOutput: _BridgedSwiftStruct {

#if arch(wasm32)
@_extern(wasm, module: "bjs", name: "swift_js_struct_lower_PlayBridgeJSOutput")
fileprivate func _bjs_struct_lower_PlayBridgeJSOutput(_ objectId: Int32) -> Int32
fileprivate func _bjs_struct_lower_PlayBridgeJSOutput(_ objectId: Int32) -> Void
#else
fileprivate func _bjs_struct_lower_PlayBridgeJSOutput(_ objectId: Int32) -> Int32 {
fileprivate func _bjs_struct_lower_PlayBridgeJSOutput(_ objectId: Int32) -> Void {
fatalError("Only available on WebAssembly")
}
#endif
Expand Down Expand Up @@ -77,10 +74,7 @@ extension PlayBridgeJSDiagnostic: _BridgedSwiftStruct {
}

init(unsafelyCopying jsObject: JSObject) {
let __bjs_cleanupId = _bjs_struct_lower_PlayBridgeJSDiagnostic(jsObject.bridgeJSLowerParameter())
defer {
_swift_js_struct_cleanup(__bjs_cleanupId)
}
_bjs_struct_lower_PlayBridgeJSDiagnostic(jsObject.bridgeJSLowerParameter())
self = Self.bridgeJSStackPop()
}

Expand All @@ -93,9 +87,9 @@ extension PlayBridgeJSDiagnostic: _BridgedSwiftStruct {

#if arch(wasm32)
@_extern(wasm, module: "bjs", name: "swift_js_struct_lower_PlayBridgeJSDiagnostic")
fileprivate func _bjs_struct_lower_PlayBridgeJSDiagnostic(_ objectId: Int32) -> Int32
fileprivate func _bjs_struct_lower_PlayBridgeJSDiagnostic(_ objectId: Int32) -> Void
#else
fileprivate func _bjs_struct_lower_PlayBridgeJSDiagnostic(_ objectId: Int32) -> Int32 {
fileprivate func _bjs_struct_lower_PlayBridgeJSDiagnostic(_ objectId: Int32) -> Void {
fatalError("Only available on WebAssembly")
}
#endif
Expand All @@ -122,10 +116,7 @@ extension PlayBridgeJSResult: _BridgedSwiftStruct {
}

init(unsafelyCopying jsObject: JSObject) {
let __bjs_cleanupId = _bjs_struct_lower_PlayBridgeJSResult(jsObject.bridgeJSLowerParameter())
defer {
_swift_js_struct_cleanup(__bjs_cleanupId)
}
_bjs_struct_lower_PlayBridgeJSResult(jsObject.bridgeJSLowerParameter())
self = Self.bridgeJSStackPop()
}

Expand All @@ -138,9 +129,9 @@ extension PlayBridgeJSResult: _BridgedSwiftStruct {

#if arch(wasm32)
@_extern(wasm, module: "bjs", name: "swift_js_struct_lower_PlayBridgeJSResult")
fileprivate func _bjs_struct_lower_PlayBridgeJSResult(_ objectId: Int32) -> Int32
fileprivate func _bjs_struct_lower_PlayBridgeJSResult(_ objectId: Int32) -> Void
#else
fileprivate func _bjs_struct_lower_PlayBridgeJSResult(_ objectId: Int32) -> Int32 {
fileprivate func _bjs_struct_lower_PlayBridgeJSResult(_ objectId: Int32) -> Void {
fatalError("Only available on WebAssembly")
}
#endif
Expand Down
7 changes: 2 additions & 5 deletions Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1243,10 +1243,7 @@ struct StructCodegen {
printer.write(
multilineString: """
\(accessControl)init(unsafelyCopying jsObject: JSObject) {
let __bjs_cleanupId = \(lowerFunctionName)(jsObject.bridgeJSLowerParameter())
defer {
_swift_js_struct_cleanup(__bjs_cleanupId)
}
\(lowerFunctionName)(jsObject.bridgeJSLowerParameter())
self = Self.bridgeJSStackPop()
}
"""
Expand Down Expand Up @@ -1274,7 +1271,7 @@ struct StructCodegen {
functionName: lowerFunctionName,
signature: SwiftSignatureBuilder.buildABIFunctionSignature(
abiParameters: [("objectId", .i32)],
returnType: .i32
returnType: nil
)
)
let liftExternDeclPrinter = CodeFragmentPrinter()
Expand Down
52 changes: 6 additions & 46 deletions Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ public struct BridgeJSLink {
"let \(JSGlueVariableScope.reservedF32Stack) = [];",
"let \(JSGlueVariableScope.reservedF64Stack) = [];",
"let \(JSGlueVariableScope.reservedPointerStack) = [];",
"let \(JSGlueVariableScope.reservedTmpStructCleanups) = [];",
"const \(JSGlueVariableScope.reservedEnumHelpers) = {};",
"const \(JSGlueVariableScope.reservedStructHelpers) = {};",
"",
Expand Down Expand Up @@ -403,38 +402,13 @@ public struct BridgeJSLink {
printer.write("return \(JSGlueVariableScope.reservedPointerStack).pop();")
}
printer.write("}")
printer.write("bjs[\"swift_js_struct_cleanup\"] = function(cleanupId) {")
printer.indent {
printer.write("if (cleanupId === 0) { return; }")
printer.write("const index = (cleanupId | 0) - 1;")
printer.write("const cleanup = \(JSGlueVariableScope.reservedTmpStructCleanups)[index];")
printer.write("\(JSGlueVariableScope.reservedTmpStructCleanups)[index] = null;")
printer.write("if (cleanup) { cleanup(); }")
printer.write(
"while (\(JSGlueVariableScope.reservedTmpStructCleanups).length > 0 && \(JSGlueVariableScope.reservedTmpStructCleanups)[\(JSGlueVariableScope.reservedTmpStructCleanups).length - 1] == null) {"
)
printer.indent {
printer.write("\(JSGlueVariableScope.reservedTmpStructCleanups).pop();")
}
printer.write("}")
}
printer.write("}")

if !allStructs.isEmpty {
for structDef in allStructs {
printer.write("bjs[\"swift_js_struct_lower_\(structDef.name)\"] = function(objectId) {")
printer.indent {
printer.write(
"const { cleanup: cleanup } = \(JSGlueVariableScope.reservedStructHelpers).\(structDef.name).lower(\(JSGlueVariableScope.reservedSwift).memory.getObject(objectId));"
"\(JSGlueVariableScope.reservedStructHelpers).\(structDef.name).lower(\(JSGlueVariableScope.reservedSwift).memory.getObject(objectId));"
)
printer.write("if (cleanup) {")
printer.indent {
printer.write(
"return \(JSGlueVariableScope.reservedTmpStructCleanups).push(cleanup);"
)
}
printer.write("}")
printer.write("return 0;")
}
printer.write("}")

Expand Down Expand Up @@ -956,14 +930,12 @@ public struct BridgeJSLink {
for structDef in allStructs {
let structPrinter = CodeFragmentPrinter()
let structScope = JSGlueVariableScope(intrinsicRegistry: intrinsicRegistry)
let structCleanup = CodeFragmentPrinter()
let fragment = IntrinsicJSFragment.structHelper(structDefinition: structDef, allStructs: allStructs)
_ = try fragment.printCode(
[structDef.name],
IntrinsicJSFragment.PrintCodeContext(
scope: structScope,
printer: structPrinter,
cleanupCode: structCleanup
printer: structPrinter
)
)
bodyPrinter.write(lines: structPrinter.lines)
Expand All @@ -975,14 +947,12 @@ public struct BridgeJSLink {
for enumDef in allAssocEnums {
let enumPrinter = CodeFragmentPrinter()
let enumScope = JSGlueVariableScope(intrinsicRegistry: intrinsicRegistry)
let enumCleanup = CodeFragmentPrinter()
let fragment = IntrinsicJSFragment.associatedValueEnumHelperFactory(enumDefinition: enumDef)
_ = try fragment.printCode(
[enumDef.valuesName],
IntrinsicJSFragment.PrintCodeContext(
scope: enumScope,
printer: enumPrinter,
cleanupCode: enumCleanup
printer: enumPrinter
)
)
bodyPrinter.write(lines: enumPrinter.lines)
Expand Down Expand Up @@ -1227,7 +1197,6 @@ public struct BridgeJSLink {

class ExportedThunkBuilder {
var body: CodeFragmentPrinter
var cleanupCode: CodeFragmentPrinter
var parameterForwardings: [String] = []
let effects: Effects
let scope: JSGlueVariableScope
Expand All @@ -1237,11 +1206,9 @@ public struct BridgeJSLink {
self.effects = effects
self.scope = JSGlueVariableScope(intrinsicRegistry: intrinsicRegistry)
self.body = CodeFragmentPrinter()
self.cleanupCode = CodeFragmentPrinter()
self.context = IntrinsicJSFragment.PrintCodeContext(
scope: scope,
printer: body,
cleanupCode: cleanupCode,
hasDirectAccessToSwiftClass: hasDirectAccessToSwiftClass
)
}
Expand Down Expand Up @@ -1312,10 +1279,9 @@ public struct BridgeJSLink {
]
}

/// Renders the thunk body (body code, cleanup, exception handling, and optional return) into a printer.
/// Renders the thunk body (body code, exception handling, and optional return) into a printer.
func renderFunctionBody(into printer: CodeFragmentPrinter, returnExpr: String?) {
printer.write(contentsOf: body)
printer.write(contentsOf: cleanupCode)
printer.write(lines: checkExceptionLines())
if let returnExpr = returnExpr {
printer.write("return \(returnExpr);")
Expand Down Expand Up @@ -1541,8 +1507,7 @@ public struct BridgeJSLink {
let printer = CodeFragmentPrinter()
let context = IntrinsicJSFragment.PrintCodeContext(
scope: scope,
printer: printer,
cleanupCode: CodeFragmentPrinter()
printer: printer
)
let enumValuesName = enumDefinition.valuesName

Expand Down Expand Up @@ -2127,7 +2092,6 @@ extension BridgeJSLink {
class ImportedThunkBuilder {
let body: CodeFragmentPrinter
let scope: JSGlueVariableScope
let cleanupCode: CodeFragmentPrinter
let context: BridgeContext
var parameterNames: [String] = []
var parameterForwardings: [String] = []
Expand All @@ -2136,12 +2100,10 @@ extension BridgeJSLink {
init(context: BridgeContext = .importTS, intrinsicRegistry: JSIntrinsicRegistry) {
self.body = CodeFragmentPrinter()
self.scope = JSGlueVariableScope(intrinsicRegistry: intrinsicRegistry)
self.cleanupCode = CodeFragmentPrinter()
self.context = context
self.printContext = IntrinsicJSFragment.PrintCodeContext(
scope: scope,
printer: body,
cleanupCode: cleanupCode
printer: body
)
}

Expand Down Expand Up @@ -2680,7 +2642,6 @@ extension BridgeJSLink {
getterPrinter.write("get \(property.name)() {")
getterPrinter.indent {
getterPrinter.write(contentsOf: getterThunkBuilder.body)
getterPrinter.write(contentsOf: getterThunkBuilder.cleanupCode)
getterPrinter.write(lines: getterThunkBuilder.checkExceptionLines())
if let returnExpr = getterReturnExpr {
getterPrinter.write("return \(returnExpr);")
Expand Down Expand Up @@ -2708,7 +2669,6 @@ extension BridgeJSLink {
setterPrinter.write("set \(property.name)(value) {")
setterPrinter.indent {
setterPrinter.write(contentsOf: setterThunkBuilder.body)
setterPrinter.write(contentsOf: setterThunkBuilder.cleanupCode)
setterPrinter.write(lines: setterThunkBuilder.checkExceptionLines())
}
setterPrinter.write("},")
Expand Down
Loading