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
16 changes: 16 additions & 0 deletions Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ public struct BridgeJSLink {
"let \(JSGlueVariableScope.reservedF32Stack) = [];",
"let \(JSGlueVariableScope.reservedF64Stack) = [];",
"let \(JSGlueVariableScope.reservedPointerStack) = [];",
"let \(JSGlueVariableScope.reservedTaStack) = [];",
"const \(JSGlueVariableScope.reservedEnumHelpers) = {};",
"const \(JSGlueVariableScope.reservedStructHelpers) = {};",
"",
Expand Down Expand Up @@ -489,6 +490,21 @@ public struct BridgeJSLink {
printer.write("return \(JSGlueVariableScope.reservedI64Stack).pop();")
}
printer.write("}")
// Typed array constructors indexed by kind (must match _BridgedNumericArrayKind)
printer.write(
"const taCtors = [Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array];"
)
printer.write("bjs[\"swift_js_push_typed_array\"] = function(kind, ptr, count) {")
printer.indent {
printer.write("const Ctor = taCtors[kind];")
printer.write("const byteLen = count * Ctor.BYTES_PER_ELEMENT;")
// slice() copies the bytes into a new ArrayBuffer that is properly aligned
printer.write(
"const copy = \(JSGlueVariableScope.reservedMemory).buffer.slice(ptr, ptr + byteLen);"
)
printer.write("\(JSGlueVariableScope.reservedTaStack).push(Array.from(new Ctor(copy)));")
}
printer.write("}")
if !allStructs.isEmpty {
for structDef in allStructs {
printer.write("bjs[\"swift_js_struct_lower_\(structDef.abiName)\"] = function(objectId) {")
Expand Down
29 changes: 21 additions & 8 deletions Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ final class JSGlueVariableScope {
static let reservedStructHelpers = "structHelpers"
static let reservedSwiftClosureRegistry = "swiftClosureRegistry"
static let reservedMakeSwiftClosure = "makeClosure"
static let reservedTaStack = "taStack"

private let intrinsicRegistry: JSIntrinsicRegistry

Expand Down Expand Up @@ -63,6 +64,7 @@ final class JSGlueVariableScope {
reservedStructHelpers,
reservedSwiftClosureRegistry,
reservedMakeSwiftClosure,
reservedTaStack,
]

init(intrinsicRegistry: JSIntrinsicRegistry) {
Expand Down Expand Up @@ -1896,20 +1898,31 @@ struct IntrinsicJSFragment: Sendable {
let (scope, printer) = (context.scope, context.printer)
let resultVar = scope.variable("arrayResult")
let lenVar = scope.variable("arrayLen")
let iVar = scope.variable("i")

printer.write("const \(lenVar) = \(scope.popI32());")
printer.write("const \(resultVar) = [];")
printer.write("for (let \(iVar) = 0; \(iVar) < \(lenVar); \(iVar)++) {")
printer.write("let \(resultVar);")
printer.write("if (\(lenVar) === -1) {")
printer.indent {
// Bulk path: Swift pushed a typed array onto the typed-array stack
printer.write("\(resultVar) = \(JSGlueVariableScope.reservedTaStack).pop();")
}
printer.write("} else {")
try printer.indent {
let elementFragment = try stackLiftFragment(elementType: elementType)
let elementResults = try elementFragment.printCode([], context)
if let elementExpr = elementResults.first {
printer.write("\(resultVar).push(\(elementExpr));")
// Element-by-element path (original behavior)
let iVar = scope.variable("i")
printer.write("\(resultVar) = [];")
printer.write("for (let \(iVar) = 0; \(iVar) < \(lenVar); \(iVar)++) {")
try printer.indent {
let elementFragment = try stackLiftFragment(elementType: elementType)
let elementResults = try elementFragment.printCode([], context)
if let elementExpr = elementResults.first {
printer.write("\(resultVar).push(\(elementExpr));")
}
}
printer.write("}")
printer.write("\(resultVar).reverse();")
}
printer.write("}")
printer.write("\(resultVar).reverse();")
return [resultVar]
}
)
Expand Down
Loading
Loading