Skip to content
Draft
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/BridgeJSCore/ImportTS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,9 @@ extension BridgeType {
throw BridgeJSCoreError("Namespace enums cannot be used as parameters")
case .nullable(let wrappedType, _):
let wrappedInfo = try wrappedType.loweringParameterInfo(context: context)
if wrappedInfo.loweredParameters.isEmpty {
return LoweringParameterInfo(loweredParameters: [])
}
var params = [("isSome", WasmCoreType.i32)]
params.append(contentsOf: wrappedInfo.loweredParameters)
return LoweringParameterInfo(loweredParameters: params, useBorrowing: wrappedInfo.useBorrowing)
Expand Down
23 changes: 6 additions & 17 deletions Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -668,16 +668,13 @@ struct IntrinsicJSFragment: Sendable {
)
}

let innerFragment =
if wrappedType.optionalParameterUsesStackABI {
try stackLiftFragment(elementType: wrappedType)
} else {
try liftParameter(type: wrappedType, context: bridgeContext)
}
if wrappedType.optionalParameterUsesStackABI {
return try optionalElementRaiseFragment(wrappedType: wrappedType, kind: kind)
}
return compositeOptionalLiftParameter(
wrappedType: wrappedType,
kind: kind,
innerFragment: innerFragment
innerFragment: try liftParameter(type: wrappedType, context: bridgeContext)
)
}

Expand All @@ -686,22 +683,14 @@ struct IntrinsicJSFragment: Sendable {
kind: JSOptionalKind,
innerFragment: IntrinsicJSFragment
) -> IntrinsicJSFragment {
let isStackConvention = wrappedType.optionalParameterUsesStackABI
let absenceLiteral = kind.absenceLiteral

let outerParams: [String]
if isStackConvention {
outerParams = ["isSome"]
} else {
outerParams = ["isSome"] + innerFragment.parameters
}

return IntrinsicJSFragment(
parameters: outerParams,
parameters: ["isSome"] + innerFragment.parameters,
printCode: { arguments, context in
let (scope, printer) = (context.scope, context.printer)
let isSome = arguments[0]
let innerArgs = isStackConvention ? [] : Array(arguments.dropFirst())
let innerArgs = Array(arguments.dropFirst())

let bufferPrinter = CodeFragmentPrinter()
let innerResults = try innerFragment.printCode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,20 +614,20 @@ func _$Promise_resolve_Sq10AsyncThemeO(_ promise: JSObject, _ value: Optional<As

#if arch(wasm32)
@_extern(wasm, module: "bjs", name: "promise_resolve_TestModule_Sq10AsyncPointV")
fileprivate func promise_resolve_TestModule_Sq10AsyncPointV_extern(_ promise: Int32, _ value: Int32) -> Void
fileprivate func promise_resolve_TestModule_Sq10AsyncPointV_extern(_ promise: Int32) -> Void
#else
fileprivate func promise_resolve_TestModule_Sq10AsyncPointV_extern(_ promise: Int32, _ value: Int32) -> Void {
fileprivate func promise_resolve_TestModule_Sq10AsyncPointV_extern(_ promise: Int32) -> Void {
fatalError("Only available on WebAssembly")
}
#endif
@inline(never) fileprivate func promise_resolve_TestModule_Sq10AsyncPointV(_ promise: Int32, _ value: Int32) -> Void {
return promise_resolve_TestModule_Sq10AsyncPointV_extern(promise, value)
@inline(never) fileprivate func promise_resolve_TestModule_Sq10AsyncPointV(_ promise: Int32) -> Void {
return promise_resolve_TestModule_Sq10AsyncPointV_extern(promise)
}

func _$Promise_resolve_Sq10AsyncPointV(_ promise: JSObject, _ value: Optional<AsyncPoint>) throws(JSException) -> Void {
let valueIsSome = value.bridgeJSLowerParameter()
let _ = value.bridgeJSLowerParameter()
let promiseValue = promise.bridgeJSLowerParameter()
promise_resolve_TestModule_Sq10AsyncPointV(promiseValue, valueIsSome)
promise_resolve_TestModule_Sq10AsyncPointV(promiseValue)
if let error = _swift_js_take_exception() { throw error }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ func _$logStrings(_ items: [String]) throws(JSException) -> Void {

#if arch(wasm32)
@_extern(wasm, module: "TestModule", name: "bjs_optionalArrayThenArray")
fileprivate func bjs_optionalArrayThenArray_extern(_ a: Int32) -> Int32
fileprivate func bjs_optionalArrayThenArray_extern() -> Int32
#else
fileprivate func bjs_optionalArrayThenArray_extern(_ a: Int32) -> Int32 {
fileprivate func bjs_optionalArrayThenArray_extern() -> Int32 {
fatalError("Only available on WebAssembly")
}
#endif
@inline(never) fileprivate func bjs_optionalArrayThenArray(_ a: Int32) -> Int32 {
return bjs_optionalArrayThenArray_extern(a)
@inline(never) fileprivate func bjs_optionalArrayThenArray() -> Int32 {
return bjs_optionalArrayThenArray_extern()
}

func _$optionalArrayThenArray(_ a: Optional<[Int]>, _ b: [Int]) throws(JSException) -> Int {
let _ = b.bridgeJSLowerParameter()
let aIsSome = a.bridgeJSLowerParameter()
let ret = bjs_optionalArrayThenArray(aIsSome)
let _ = a.bridgeJSLowerParameter()
let ret = bjs_optionalArrayThenArray()
if let error = _swift_js_take_exception() {
throw error
}
Expand All @@ -63,21 +63,21 @@ func _$optionalArrayThenArray(_ a: Optional<[Int]>, _ b: [Int]) throws(JSExcepti

#if arch(wasm32)
@_extern(wasm, module: "TestModule", name: "bjs_borrowedStringAroundStackParams")
fileprivate func bjs_borrowedStringAroundStackParams_extern(_ sBytes: Int32, _ sLength: Int32, _ a: Int32) -> Int32
fileprivate func bjs_borrowedStringAroundStackParams_extern(_ sBytes: Int32, _ sLength: Int32) -> Int32
#else
fileprivate func bjs_borrowedStringAroundStackParams_extern(_ sBytes: Int32, _ sLength: Int32, _ a: Int32) -> Int32 {
fileprivate func bjs_borrowedStringAroundStackParams_extern(_ sBytes: Int32, _ sLength: Int32) -> Int32 {
fatalError("Only available on WebAssembly")
}
#endif
@inline(never) fileprivate func bjs_borrowedStringAroundStackParams(_ sBytes: Int32, _ sLength: Int32, _ a: Int32) -> Int32 {
return bjs_borrowedStringAroundStackParams_extern(sBytes, sLength, a)
@inline(never) fileprivate func bjs_borrowedStringAroundStackParams(_ sBytes: Int32, _ sLength: Int32) -> Int32 {
return bjs_borrowedStringAroundStackParams_extern(sBytes, sLength)
}

func _$borrowedStringAroundStackParams(_ s: String, _ a: Optional<[Int]>, _ b: [Int]) throws(JSException) -> Int {
let ret0 = s.bridgeJSWithLoweredParameter { (sBytes, sLength) in
let _ = b.bridgeJSLowerParameter()
let aIsSome = a.bridgeJSLowerParameter()
let ret = bjs_borrowedStringAroundStackParams(sBytes, sLength, aIsSome)
let _ = a.bridgeJSLowerParameter()
let ret = bjs_borrowedStringAroundStackParams(sBytes, sLength)
return ret
}
let ret = ret0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -992,14 +992,14 @@ public func _invoke_swift_closure_TestModule_10TestModuleSq5ThemeO_Sq5ThemeO(_ b

#if arch(wasm32)
@_extern(wasm, module: "bjs", name: "invoke_js_callback_TestModule_10TestModuleSq6AnimalV_Sq6AnimalV")
fileprivate func invoke_js_callback_TestModule_10TestModuleSq6AnimalV_Sq6AnimalV_extern(_ callback: Int32, _ param0: Int32) -> Void
fileprivate func invoke_js_callback_TestModule_10TestModuleSq6AnimalV_Sq6AnimalV_extern(_ callback: Int32) -> Void
#else
fileprivate func invoke_js_callback_TestModule_10TestModuleSq6AnimalV_Sq6AnimalV_extern(_ callback: Int32, _ param0: Int32) -> Void {
fileprivate func invoke_js_callback_TestModule_10TestModuleSq6AnimalV_Sq6AnimalV_extern(_ callback: Int32) -> Void {
fatalError("Only available on WebAssembly")
}
#endif
@inline(never) fileprivate func invoke_js_callback_TestModule_10TestModuleSq6AnimalV_Sq6AnimalV(_ callback: Int32, _ param0: Int32) -> Void {
return invoke_js_callback_TestModule_10TestModuleSq6AnimalV_Sq6AnimalV_extern(callback, param0)
@inline(never) fileprivate func invoke_js_callback_TestModule_10TestModuleSq6AnimalV_Sq6AnimalV(_ callback: Int32) -> Void {
return invoke_js_callback_TestModule_10TestModuleSq6AnimalV_Sq6AnimalV_extern(callback)
}

#if arch(wasm32)
Expand All @@ -1019,9 +1019,9 @@ private enum _BJS_Closure_10TestModuleSq6AnimalV_Sq6AnimalV {
let callback = JSObject.bridgeJSLiftParameter(callbackId)
return { [callback] param0 in
#if arch(wasm32)
let param0IsSome = param0.bridgeJSLowerParameter()
let _ = param0.bridgeJSLowerParameter()
let callbackValue = callback.bridgeJSLowerParameter()
invoke_js_callback_TestModule_10TestModuleSq6AnimalV_Sq6AnimalV(callbackValue, param0IsSome)
invoke_js_callback_TestModule_10TestModuleSq6AnimalV_Sq6AnimalV(callbackValue)
return Optional<Animal>.bridgeJSLiftReturn()
#else
fatalError("Only available on WebAssembly")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ func _$translate(_ point: Point, _ dx: Int, _ dy: Int) throws(JSException) -> Po

#if arch(wasm32)
@_extern(wasm, module: "TestModule", name: "bjs_roundTripOptional")
fileprivate func bjs_roundTripOptional_extern(_ point: Int32) -> Void
fileprivate func bjs_roundTripOptional_extern() -> Void
#else
fileprivate func bjs_roundTripOptional_extern(_ point: Int32) -> Void {
fileprivate func bjs_roundTripOptional_extern() -> Void {
fatalError("Only available on WebAssembly")
}
#endif
@inline(never) fileprivate func bjs_roundTripOptional(_ point: Int32) -> Void {
return bjs_roundTripOptional_extern(point)
@inline(never) fileprivate func bjs_roundTripOptional() -> Void {
return bjs_roundTripOptional_extern()
}

func _$roundTripOptional(_ point: Optional<Point>) throws(JSException) -> Optional<Point> {
let pointIsSome = point.bridgeJSLowerParameter()
bjs_roundTripOptional(pointIsSome)
let _ = point.bridgeJSLowerParameter()
bjs_roundTripOptional()
if let error = _swift_js_take_exception() {
throw error
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,16 +324,17 @@ export async function createInstantiator(options, swift) {
setException(error);
}
}
bjs["promise_resolve_TestModule_Sq10AsyncPointV"] = function(promise, value) {
bjs["promise_resolve_TestModule_Sq10AsyncPointV"] = function(promise) {
try {
let optResult;
if (value) {
const struct = structHelpers.AsyncPoint.lift();
optResult = struct;
const isSome = i32Stack.pop();
let optValue;
if (isSome === 0) {
optValue = null;
} else {
optResult = null;
const struct = structHelpers.AsyncPoint.lift();
optValue = struct;
}
swift.memory.getObject(promise)[__bjs_promiseSettlers].resolve(optResult);
swift.memory.getObject(promise)[__bjs_promiseSettlers].resolve(optValue);
} catch (error) {
setException(error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,13 @@ export async function createInstantiator(options, swift) {
setException(error);
}
}
TestModule["bjs_optionalArrayThenArray"] = function bjs_optionalArrayThenArray(a) {
TestModule["bjs_optionalArrayThenArray"] = function bjs_optionalArrayThenArray() {
try {
let optResult;
if (a) {
const isSome = i32Stack.pop();
let optValue;
if (isSome === 0) {
optValue = null;
} else {
const arrayLen = i32Stack.pop();
let arrayResult;
if (arrayLen === -1) {
Expand All @@ -263,9 +266,7 @@ export async function createInstantiator(options, swift) {
}
arrayResult.reverse();
}
optResult = arrayResult;
} else {
optResult = null;
optValue = arrayResult;
}
const arrayLen1 = i32Stack.pop();
let arrayResult1;
Expand All @@ -279,18 +280,21 @@ export async function createInstantiator(options, swift) {
}
arrayResult1.reverse();
}
let ret = imports.optionalArrayThenArray(optResult, arrayResult1);
let ret = imports.optionalArrayThenArray(optValue, arrayResult1);
return ret;
} catch (error) {
setException(error);
return 0
}
}
TestModule["bjs_borrowedStringAroundStackParams"] = function bjs_borrowedStringAroundStackParams(sBytes, sCount, a) {
TestModule["bjs_borrowedStringAroundStackParams"] = function bjs_borrowedStringAroundStackParams(sBytes, sCount) {
try {
const string = decodeString(sBytes, sCount);
let optResult;
if (a) {
const isSome = i32Stack.pop();
let optValue;
if (isSome === 0) {
optValue = null;
} else {
const arrayLen = i32Stack.pop();
let arrayResult;
if (arrayLen === -1) {
Expand All @@ -303,9 +307,7 @@ export async function createInstantiator(options, swift) {
}
arrayResult.reverse();
}
optResult = arrayResult;
} else {
optResult = null;
optValue = arrayResult;
}
const arrayLen1 = i32Stack.pop();
let arrayResult1;
Expand All @@ -319,7 +321,7 @@ export async function createInstantiator(options, swift) {
}
arrayResult1.reverse();
}
let ret = imports.borrowedStringAroundStackParams(string, optResult, arrayResult1);
let ret = imports.borrowedStringAroundStackParams(string, optValue, arrayResult1);
return ret;
} catch (error) {
setException(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -852,22 +852,23 @@ export async function createInstantiator(options, swift) {
};
return makeClosure(boxPtr, file, line, lower_closure_TestModule_10TestModuleSq5ThemeO_Sq5ThemeO);
}
bjs["invoke_js_callback_TestModule_10TestModuleSq6AnimalV_Sq6AnimalV"] = function(callbackId, param0) {
bjs["invoke_js_callback_TestModule_10TestModuleSq6AnimalV_Sq6AnimalV"] = function(callbackId) {
try {
const callback = swift.memory.getObject(callbackId);
let optResult;
if (param0) {
const struct = structHelpers.Animal.lift();
optResult = struct;
const isSome = i32Stack.pop();
let optValue;
if (isSome === 0) {
optValue = null;
} else {
optResult = null;
const struct = structHelpers.Animal.lift();
optValue = struct;
}
let ret = callback(optResult);
const isSome = ret != null;
if (isSome) {
let ret = callback(optValue);
const isSome1 = ret != null;
if (isSome1) {
structHelpers.Animal.lower(ret);
}
i32Stack.push(isSome ? 1 : 0);
i32Stack.push(isSome1 ? 1 : 0);
} catch (error) {
setException(error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,21 +232,22 @@ export async function createInstantiator(options, swift) {
setException(error);
}
}
TestModule["bjs_roundTripOptional"] = function bjs_roundTripOptional(point) {
TestModule["bjs_roundTripOptional"] = function bjs_roundTripOptional() {
try {
let optResult;
if (point) {
const struct = structHelpers.Point.lift();
optResult = struct;
const isSome = i32Stack.pop();
let optValue;
if (isSome === 0) {
optValue = null;
} else {
optResult = null;
const struct = structHelpers.Point.lift();
optValue = struct;
}
let ret = imports.roundTripOptional(optResult);
const isSome = ret != null;
if (isSome) {
let ret = imports.roundTripOptional(optValue);
const isSome1 = ret != null;
if (isSome1) {
structHelpers.Point.lower(ret);
}
i32Stack.push(isSome ? 1 : 0);
i32Stack.push(isSome1 ? 1 : 0);
} catch (error) {
setException(error);
}
Expand Down
Loading
Loading