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
13 changes: 7 additions & 6 deletions Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ struct StackCodegen {
switch type {
case .string:
return "String.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32())"
case .int:
case .int, .uint:
return "Int.bridgeJSLiftParameter(_swift_js_pop_param_int32())"
case .bool:
return "Bool.bridgeJSLiftParameter(_swift_js_pop_param_int32())"
Expand Down Expand Up @@ -873,7 +873,7 @@ struct StackCodegen {
case .string:
return
"Optional<String>.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32(), _swift_js_pop_param_int32())"
case .int:
case .int, .uint:
return "Optional<Int>.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32())"
case .bool:
return "Optional<Bool>.bridgeJSLiftParameter(_swift_js_pop_param_int32(), _swift_js_pop_param_int32())"
Expand Down Expand Up @@ -945,7 +945,7 @@ struct StackCodegen {
"var __bjs_\(raw: varPrefix) = \(raw: accessor)",
"__bjs_\(raw: varPrefix).withUTF8 { ptr in _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) }",
]
case .int:
case .int, .uint:
return ["_swift_js_push_int(Int32(\(raw: accessor)))"]
case .bool:
return ["_swift_js_push_int(\(raw: accessor) ? 1 : 0)"]
Expand Down Expand Up @@ -1052,7 +1052,7 @@ struct StackCodegen {
"var __bjs_str_\(raw: varPrefix) = \(raw: unwrappedVar)",
"__bjs_str_\(raw: varPrefix).withUTF8 { ptr in _swift_js_push_string(ptr.baseAddress, Int32(ptr.count)) }",
]
case .int:
case .int, .uint:
return ["_swift_js_push_int(Int32(\(raw: unwrappedVar)))"]
case .bool:
return ["_swift_js_push_int(\(raw: unwrappedVar) ? 1 : 0)"]
Expand Down Expand Up @@ -1643,6 +1643,7 @@ extension BridgeType {
switch self {
case .bool: return "Bool"
case .int: return "Int"
case .uint: return "UInt"
case .float: return "Float"
case .double: return "Double"
case .string: return "String"
Expand Down Expand Up @@ -1687,7 +1688,7 @@ extension BridgeType {
func liftParameterInfo() throws -> LiftingIntrinsicInfo {
switch self {
case .bool: return .bool
case .int: return .int
case .int, .uint: return .int
case .float: return .float
case .double: return .double
case .string: return .string
Expand Down Expand Up @@ -1739,7 +1740,7 @@ extension BridgeType {
func loweringReturnInfo() throws -> LoweringIntrinsicInfo {
switch self {
case .bool: return .bool
case .int: return .int
case .int, .uint: return .int
case .float: return .float
case .double: return .double
case .string: return .string
Expand Down
4 changes: 2 additions & 2 deletions Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ extension BridgeType {
func loweringParameterInfo(context: BridgeContext = .importTS) throws -> LoweringParameterInfo {
switch self {
case .bool: return .bool
case .int: return .int
case .int, .uint: return .int
case .float: return .float
case .double: return .double
case .string: return .string
Expand Down Expand Up @@ -960,7 +960,7 @@ extension BridgeType {
) throws -> LiftingReturnInfo {
switch self {
case .bool: return .bool
case .int: return .int
case .int, .uint: return .int
case .float: return .float
case .double: return .double
case .string: return .string
Expand Down
2 changes: 1 addition & 1 deletion Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3401,7 +3401,7 @@ extension BridgeType {
return "void"
case .string:
return "string"
case .int:
case .int, .uint:
return "number"
case .float:
return "number"
Expand Down
39 changes: 25 additions & 14 deletions Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ struct IntrinsicJSFragment: Sendable {
}
)

/// Convert signed Int32 to unsigned for UInt values
static let uintLiftReturn = IntrinsicJSFragment(
parameters: ["value"],
printCode: { arguments, scope, printer, cleanupCode in
return ["\(arguments[0]) >>> 0"]
}
)
static let uintLiftParameter = uintLiftReturn

static let stringLowerParameter = IntrinsicJSFragment(
parameters: ["value"],
printCode: { arguments, scope, printer, cleanupCode in
Expand Down Expand Up @@ -519,7 +528,7 @@ struct IntrinsicJSFragment: Sendable {
case .bool:
printer.write("const \(resultVar) = \(JSGlueVariableScope.reservedStorageToReturnOptionalBool);")
printer.write("\(JSGlueVariableScope.reservedStorageToReturnOptionalBool) = undefined;")
case .int:
case .int, .uint:
printer.write("const \(resultVar) = \(JSGlueVariableScope.reservedStorageToReturnOptionalInt);")
printer.write("\(JSGlueVariableScope.reservedStorageToReturnOptionalInt) = undefined;")
case .float:
Expand Down Expand Up @@ -651,7 +660,7 @@ struct IntrinsicJSFragment: Sendable {
printer.write(
"bjs[\"swift_js_return_optional_bool\"](\(isSomeVar) ? 1 : 0, \(isSomeVar) ? (\(value) ? 1 : 0) : 0);"
)
case .int:
case .int, .uint:
printer.write(
"bjs[\"swift_js_return_optional_int\"](\(isSomeVar) ? 1 : 0, \(isSomeVar) ? (\(value) | 0) : 0);"
)
Expand Down Expand Up @@ -937,7 +946,7 @@ struct IntrinsicJSFragment: Sendable {
)
printer.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(value));")
printer.write("\(targetVar) = String(\(objectLabel));")
case .int:
case .int, .uint:
printer.write("\(targetVar) = \(value) | 0;")
case .bool:
printer.write("\(targetVar) = \(value) !== 0;")
Expand Down Expand Up @@ -1121,7 +1130,7 @@ struct IntrinsicJSFragment: Sendable {
case .string:
printer.write("\(JSGlueVariableScope.reservedStorageToReturnString) = \(result);")
printer.write("return;")
case .int:
case .int, .uint:
printer.write("\(JSGlueVariableScope.reservedStorageToReturnOptionalInt) = \(result);")
printer.write("return;")
case .bool:
Expand Down Expand Up @@ -1370,7 +1379,7 @@ struct IntrinsicJSFragment: Sendable {
/// Returns a fragment that lowers a JS value to Wasm core values for parameters
static func lowerParameter(type: BridgeType) throws -> IntrinsicJSFragment {
switch type {
case .int, .float, .double, .bool, .unsafePointer: return .identity
case .int, .uint, .float, .double, .bool, .unsafePointer: return .identity
case .string: return .stringLowerParameter
case .jsObject: return .jsObjectLowerParameter
case .swiftHeapObject:
Expand Down Expand Up @@ -1414,6 +1423,7 @@ struct IntrinsicJSFragment: Sendable {
static func liftReturn(type: BridgeType) throws -> IntrinsicJSFragment {
switch type {
case .int, .float, .double: return .identity
case .uint: return .uintLiftReturn
case .bool: return .boolLiftReturn
case .string: return .stringLiftReturn
case .jsObject: return .jsObjectLiftReturn
Expand Down Expand Up @@ -1460,6 +1470,7 @@ struct IntrinsicJSFragment: Sendable {
static func liftParameter(type: BridgeType, context: BridgeContext = .importTS) throws -> IntrinsicJSFragment {
switch type {
case .int, .float, .double: return .identity
case .uint: return .uintLiftParameter
case .bool: return .boolLiftParameter
case .string: return .stringLiftParameter
case .jsObject: return .jsObjectLiftParameter
Expand Down Expand Up @@ -1560,7 +1571,7 @@ struct IntrinsicJSFragment: Sendable {
/// Returns a fragment that lowers a JS value to Wasm core values for return values
static func lowerReturn(type: BridgeType, context: BridgeContext = .importTS) throws -> IntrinsicJSFragment {
switch type {
case .int, .float, .double: return .identity
case .int, .uint, .float, .double: return .identity
case .bool: return .boolLowerReturn
case .string: return .stringLowerReturn
case .jsObject: return .jsObjectLowerReturn
Expand Down Expand Up @@ -1909,7 +1920,7 @@ struct IntrinsicJSFragment: Sendable {
return []
}
)
case .int:
case .int, .uint:
return IntrinsicJSFragment(
parameters: ["value"],
printCode: { arguments, scope, printer, cleanup in
Expand Down Expand Up @@ -1967,7 +1978,7 @@ struct IntrinsicJSFragment: Sendable {
cleanup.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(idVar));")
}
cleanup.write("}")
case .int:
case .int, .uint:
printer.write(
"\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? (\(value) | 0) : 0);"
)
Expand Down Expand Up @@ -2024,7 +2035,7 @@ struct IntrinsicJSFragment: Sendable {
return [bVar]
}
)
case .int:
case .int, .uint:
return IntrinsicJSFragment(
parameters: [],
printCode: { arguments, scope, printer, cleanup in
Expand Down Expand Up @@ -2214,7 +2225,7 @@ struct IntrinsicJSFragment: Sendable {
return [bVar]
}
)
case .int:
case .int, .uint:
return IntrinsicJSFragment(
parameters: [],
printCode: { arguments, scope, printer, cleanup in
Expand Down Expand Up @@ -2363,7 +2374,7 @@ struct IntrinsicJSFragment: Sendable {
return []
}
)
case .int:
case .int, .uint:
return IntrinsicJSFragment(
parameters: ["value"],
printCode: { arguments, scope, printer, cleanup in
Expand Down Expand Up @@ -2779,7 +2790,7 @@ struct IntrinsicJSFragment: Sendable {
return []
}
)
case .int:
case .int, .uint:
return IntrinsicJSFragment(
parameters: ["value"],
printCode: { arguments, scope, printer, cleanup in
Expand Down Expand Up @@ -3009,7 +3020,7 @@ struct IntrinsicJSFragment: Sendable {
} else {
// Handle optional primitive types using helper
switch wrappedType {
case .int:
case .int, .uint:
pushOptionalPrimitive(
value: value,
isSomeVar: isSomeVar,
Expand Down Expand Up @@ -3260,7 +3271,7 @@ struct IntrinsicJSFragment: Sendable {
return [bVar]
}
)
case .int:
case .int, .uint:
return IntrinsicJSFragment(
parameters: [],
printCode: { arguments, scope, printer, cleanup in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public struct UnsafePointerType: Codable, Equatable, Hashable, Sendable {
}

public enum BridgeType: Codable, Equatable, Hashable, Sendable {
case int, float, double, string, bool, jsObject(String?), swiftHeapObject(String), void
case int, uint, float, double, string, bool, jsObject(String?), swiftHeapObject(String), void
case unsafePointer(UnsafePointerType)
indirect case optional(BridgeType)
indirect case array(BridgeType)
Expand Down Expand Up @@ -860,6 +860,8 @@ extension BridgeType {
switch swiftType {
case "Int":
self = .int
case "UInt":
self = .uint
case "Float":
self = .float
case "Double":
Expand Down Expand Up @@ -887,7 +889,7 @@ extension BridgeType {
switch self {
case .void: return nil
case .bool: return .i32
case .int: return .i32
case .int, .uint: return .i32
case .float: return .f32
case .double: return .f64
case .string: return nil
Expand Down Expand Up @@ -933,6 +935,7 @@ extension BridgeType {
public var mangleTypeName: String {
switch self {
case .int: return "Si"
case .uint: return "Su"
case .float: return "Sf"
case .double: return "Sd"
case .string: return "SS"
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@JS func check(a: Int, b: Float, c: Double, d: Bool) {}
@JS func check(a: Int, b: UInt, c: Float, d: Double, e: Bool) {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@JS func checkInt() -> Int { fatalError() }
@JS func checkUInt() -> UInt { fatalError() }
@JS func checkFloat() -> Float { fatalError() }
@JS func checkDouble() -> Double { fatalError() }
@JS func checkBool() -> Bool { fatalError() }
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// `swift package bridge-js`.

export type Exports = {
check(a: number, b: number, c: number, d: boolean): void;
check(a: number, b: number, c: number, d: number, e: boolean): void;
}
export type Imports = {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ export async function createInstantiator(options, swift) {
createExports: (instance) => {
const js = swift.memory.heap;
const exports = {
check: function bjs_check(a, b, c, d) {
instance.exports.bjs_check(a, b, c, d);
check: function bjs_check(a, b, c, d, e) {
instance.exports.bjs_check(a, b, c, d, e);
},
};
_exports = exports;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

export type Exports = {
checkInt(): number;
checkUInt(): number;
checkFloat(): number;
checkDouble(): number;
checkBool(): boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ export async function createInstantiator(options, swift) {
const ret = instance.exports.bjs_checkInt();
return ret;
},
checkUInt: function bjs_checkUInt() {
const ret = instance.exports.bjs_checkUInt();
return ret >>> 0;
},
checkFloat: function bjs_checkFloat() {
const ret = instance.exports.bjs_checkFloat();
return ret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"label" : "b",
"name" : "b",
"type" : {
"float" : {
"uint" : {

}
}
Expand All @@ -38,14 +38,23 @@
"label" : "c",
"name" : "c",
"type" : {
"double" : {
"float" : {

}
}
},
{
"label" : "d",
"name" : "d",
"type" : {
"double" : {

}
}
},
{
"label" : "e",
"name" : "e",
"type" : {
"bool" : {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@_expose(wasm, "bjs_check")
@_cdecl("bjs_check")
public func _bjs_check(_ a: Int32, _ b: Float32, _ c: Float64, _ d: Int32) -> Void {
public func _bjs_check(_ a: Int32, _ b: Int32, _ c: Float32, _ d: Float64, _ e: Int32) -> Void {
#if arch(wasm32)
check(a: Int.bridgeJSLiftParameter(a), b: Float.bridgeJSLiftParameter(b), c: Double.bridgeJSLiftParameter(c), d: Bool.bridgeJSLiftParameter(d))
check(a: Int.bridgeJSLiftParameter(a), b: UInt.bridgeJSLiftParameter(b), c: Float.bridgeJSLiftParameter(c), d: Double.bridgeJSLiftParameter(d), e: Bool.bridgeJSLiftParameter(e))
#else
fatalError("Only available on WebAssembly")
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@
}
}
},
{
"abiName" : "bjs_checkUInt",
"effects" : {
"isAsync" : false,
"isStatic" : false,
"isThrows" : false
},
"name" : "checkUInt",
"parameters" : [

],
"returnType" : {
"uint" : {

}
}
},
{
"abiName" : "bjs_checkFloat",
"effects" : {
Expand Down
Loading