Skip to content

Commit c090528

Browse files
Merge pull request #376 from swiftwasm/pr-01a4359f450a43ed54d0c1c321c8628af91b3bf0
BridgeJS: Add more smoke tests for throwing functions
2 parents 63ede7a + 5bd426a commit c090528

File tree

5 files changed

+290
-6
lines changed

5 files changed

+290
-6
lines changed

Plugins/BridgeJS/Sources/BridgeJSTool/ExportSwift.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ class ExportSwift {
523523
case .i64: return "return 0"
524524
case .f32: return "return 0.0"
525525
case .f64: return "return 0.0"
526-
case .pointer: return "return UnsafeMutableRawPointer(bitPattern: -1)"
526+
case .pointer: return "return UnsafeMutableRawPointer(bitPattern: -1).unsafelyUnwrapped"
527527
case .none: return "return"
528528
}
529529
}

Tests/BridgeJSRuntimeTests/ExportAPITests.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,18 @@ struct TestError: Error {
3636
let message: String
3737
}
3838

39-
@JS func throwsSwiftError() throws(JSException) -> Void {
40-
throw JSException(JSError(message: "TestError").jsValue)
39+
@JS func throwsSwiftError(shouldThrow: Bool) throws(JSException) -> Void {
40+
if shouldThrow {
41+
throw JSException(JSError(message: "TestError").jsValue)
42+
}
4143
}
44+
@JS func throwsWithIntResult() throws(JSException) -> Int { return 1 }
45+
@JS func throwsWithStringResult() throws(JSException) -> String { return "Ok" }
46+
@JS func throwsWithBoolResult() throws(JSException) -> Bool { return true }
47+
@JS func throwsWithFloatResult() throws(JSException) -> Float { return 1.0 }
48+
@JS func throwsWithDoubleResult() throws(JSException) -> Double { return 1.0 }
49+
@JS func throwsWithSwiftHeapObjectResult() throws(JSException) -> Greeter { return Greeter(name: "Test") }
50+
@JS func throwsWithJSObjectResult() throws(JSException) -> JSObject { return JSObject() }
4251

4352
@JS class Greeter {
4453
var name: String

Tests/BridgeJSRuntimeTests/Generated/ExportSwift.swift

Lines changed: 151 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ public func _bjs_roundTripJSObject(v: Int32) -> Int32 {
7979

8080
@_expose(wasm, "bjs_throwsSwiftError")
8181
@_cdecl("bjs_throwsSwiftError")
82-
public func _bjs_throwsSwiftError() -> Void {
82+
public func _bjs_throwsSwiftError(shouldThrow: Int32) -> Void {
8383
do {
84-
try throwsSwiftError()
84+
try throwsSwiftError(shouldThrow: shouldThrow == 1)
8585
} catch let error {
8686
if let error = error.thrownValue.object {
8787
withExtendedLifetime(error) {
@@ -97,6 +97,155 @@ public func _bjs_throwsSwiftError() -> Void {
9797
}
9898
}
9999

100+
@_expose(wasm, "bjs_throwsWithIntResult")
101+
@_cdecl("bjs_throwsWithIntResult")
102+
public func _bjs_throwsWithIntResult() -> Int32 {
103+
do {
104+
let ret = try throwsWithIntResult()
105+
return Int32(ret)
106+
} catch let error {
107+
if let error = error.thrownValue.object {
108+
withExtendedLifetime(error) {
109+
_swift_js_throw(Int32(bitPattern: $0.id))
110+
}
111+
} else {
112+
let jsError = JSError(message: String(describing: error))
113+
withExtendedLifetime(jsError.jsObject) {
114+
_swift_js_throw(Int32(bitPattern: $0.id))
115+
}
116+
}
117+
return 0
118+
}
119+
}
120+
121+
@_expose(wasm, "bjs_throwsWithStringResult")
122+
@_cdecl("bjs_throwsWithStringResult")
123+
public func _bjs_throwsWithStringResult() -> Void {
124+
do {
125+
var ret = try throwsWithStringResult()
126+
return ret.withUTF8 { ptr in
127+
_return_string(ptr.baseAddress, Int32(ptr.count))
128+
}
129+
} catch let error {
130+
if let error = error.thrownValue.object {
131+
withExtendedLifetime(error) {
132+
_swift_js_throw(Int32(bitPattern: $0.id))
133+
}
134+
} else {
135+
let jsError = JSError(message: String(describing: error))
136+
withExtendedLifetime(jsError.jsObject) {
137+
_swift_js_throw(Int32(bitPattern: $0.id))
138+
}
139+
}
140+
return
141+
}
142+
}
143+
144+
@_expose(wasm, "bjs_throwsWithBoolResult")
145+
@_cdecl("bjs_throwsWithBoolResult")
146+
public func _bjs_throwsWithBoolResult() -> Int32 {
147+
do {
148+
let ret = try throwsWithBoolResult()
149+
return Int32(ret ? 1 : 0)
150+
} catch let error {
151+
if let error = error.thrownValue.object {
152+
withExtendedLifetime(error) {
153+
_swift_js_throw(Int32(bitPattern: $0.id))
154+
}
155+
} else {
156+
let jsError = JSError(message: String(describing: error))
157+
withExtendedLifetime(jsError.jsObject) {
158+
_swift_js_throw(Int32(bitPattern: $0.id))
159+
}
160+
}
161+
return 0
162+
}
163+
}
164+
165+
@_expose(wasm, "bjs_throwsWithFloatResult")
166+
@_cdecl("bjs_throwsWithFloatResult")
167+
public func _bjs_throwsWithFloatResult() -> Float32 {
168+
do {
169+
let ret = try throwsWithFloatResult()
170+
return Float32(ret)
171+
} catch let error {
172+
if let error = error.thrownValue.object {
173+
withExtendedLifetime(error) {
174+
_swift_js_throw(Int32(bitPattern: $0.id))
175+
}
176+
} else {
177+
let jsError = JSError(message: String(describing: error))
178+
withExtendedLifetime(jsError.jsObject) {
179+
_swift_js_throw(Int32(bitPattern: $0.id))
180+
}
181+
}
182+
return 0.0
183+
}
184+
}
185+
186+
@_expose(wasm, "bjs_throwsWithDoubleResult")
187+
@_cdecl("bjs_throwsWithDoubleResult")
188+
public func _bjs_throwsWithDoubleResult() -> Float64 {
189+
do {
190+
let ret = try throwsWithDoubleResult()
191+
return Float64(ret)
192+
} catch let error {
193+
if let error = error.thrownValue.object {
194+
withExtendedLifetime(error) {
195+
_swift_js_throw(Int32(bitPattern: $0.id))
196+
}
197+
} else {
198+
let jsError = JSError(message: String(describing: error))
199+
withExtendedLifetime(jsError.jsObject) {
200+
_swift_js_throw(Int32(bitPattern: $0.id))
201+
}
202+
}
203+
return 0.0
204+
}
205+
}
206+
207+
@_expose(wasm, "bjs_throwsWithSwiftHeapObjectResult")
208+
@_cdecl("bjs_throwsWithSwiftHeapObjectResult")
209+
public func _bjs_throwsWithSwiftHeapObjectResult() -> UnsafeMutableRawPointer {
210+
do {
211+
let ret = try throwsWithSwiftHeapObjectResult()
212+
return Unmanaged.passRetained(ret).toOpaque()
213+
} catch let error {
214+
if let error = error.thrownValue.object {
215+
withExtendedLifetime(error) {
216+
_swift_js_throw(Int32(bitPattern: $0.id))
217+
}
218+
} else {
219+
let jsError = JSError(message: String(describing: error))
220+
withExtendedLifetime(jsError.jsObject) {
221+
_swift_js_throw(Int32(bitPattern: $0.id))
222+
}
223+
}
224+
return UnsafeMutableRawPointer(bitPattern: -1).unsafelyUnwrapped
225+
}
226+
}
227+
228+
@_expose(wasm, "bjs_throwsWithJSObjectResult")
229+
@_cdecl("bjs_throwsWithJSObjectResult")
230+
public func _bjs_throwsWithJSObjectResult() -> Int32 {
231+
do {
232+
let ret = try throwsWithJSObjectResult()
233+
return _swift_js_retain(Int32(bitPattern: ret.id))
234+
} catch let error {
235+
if let error = error.thrownValue.object {
236+
withExtendedLifetime(error) {
237+
_swift_js_throw(Int32(bitPattern: $0.id))
238+
}
239+
} else {
240+
let jsError = JSError(message: String(describing: error))
241+
withExtendedLifetime(jsError.jsObject) {
242+
_swift_js_throw(Int32(bitPattern: $0.id))
243+
}
244+
}
245+
return 0
246+
}
247+
}
248+
100249
@_expose(wasm, "bjs_takeGreeter")
101250
@_cdecl("bjs_takeGreeter")
102251
public func _bjs_takeGreeter(g: UnsafeMutableRawPointer, nameBytes: Int32, nameLen: Int32) -> Void {

Tests/BridgeJSRuntimeTests/Generated/JavaScript/ExportSwift.json

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,134 @@
257257
},
258258
"name" : "throwsSwiftError",
259259
"parameters" : [
260+
{
261+
"label" : "shouldThrow",
262+
"name" : "shouldThrow",
263+
"type" : {
264+
"bool" : {
260265

266+
}
267+
}
268+
}
261269
],
262270
"returnType" : {
263271
"void" : {
264272

265273
}
266274
}
267275
},
276+
{
277+
"abiName" : "bjs_throwsWithIntResult",
278+
"effects" : {
279+
"isAsync" : false,
280+
"isThrows" : true
281+
},
282+
"name" : "throwsWithIntResult",
283+
"parameters" : [
284+
285+
],
286+
"returnType" : {
287+
"int" : {
288+
289+
}
290+
}
291+
},
292+
{
293+
"abiName" : "bjs_throwsWithStringResult",
294+
"effects" : {
295+
"isAsync" : false,
296+
"isThrows" : true
297+
},
298+
"name" : "throwsWithStringResult",
299+
"parameters" : [
300+
301+
],
302+
"returnType" : {
303+
"string" : {
304+
305+
}
306+
}
307+
},
308+
{
309+
"abiName" : "bjs_throwsWithBoolResult",
310+
"effects" : {
311+
"isAsync" : false,
312+
"isThrows" : true
313+
},
314+
"name" : "throwsWithBoolResult",
315+
"parameters" : [
316+
317+
],
318+
"returnType" : {
319+
"bool" : {
320+
321+
}
322+
}
323+
},
324+
{
325+
"abiName" : "bjs_throwsWithFloatResult",
326+
"effects" : {
327+
"isAsync" : false,
328+
"isThrows" : true
329+
},
330+
"name" : "throwsWithFloatResult",
331+
"parameters" : [
332+
333+
],
334+
"returnType" : {
335+
"float" : {
336+
337+
}
338+
}
339+
},
340+
{
341+
"abiName" : "bjs_throwsWithDoubleResult",
342+
"effects" : {
343+
"isAsync" : false,
344+
"isThrows" : true
345+
},
346+
"name" : "throwsWithDoubleResult",
347+
"parameters" : [
348+
349+
],
350+
"returnType" : {
351+
"double" : {
352+
353+
}
354+
}
355+
},
356+
{
357+
"abiName" : "bjs_throwsWithSwiftHeapObjectResult",
358+
"effects" : {
359+
"isAsync" : false,
360+
"isThrows" : true
361+
},
362+
"name" : "throwsWithSwiftHeapObjectResult",
363+
"parameters" : [
364+
365+
],
366+
"returnType" : {
367+
"swiftHeapObject" : {
368+
"_0" : "Greeter"
369+
}
370+
}
371+
},
372+
{
373+
"abiName" : "bjs_throwsWithJSObjectResult",
374+
"effects" : {
375+
"isAsync" : false,
376+
"isThrows" : true
377+
},
378+
"name" : "throwsWithJSObjectResult",
379+
"parameters" : [
380+
381+
],
382+
"returnType" : {
383+
"jsObject" : {
384+
385+
}
386+
}
387+
},
268388
{
269389
"abiName" : "bjs_takeGreeter",
270390
"effects" : {

Tests/prelude.mjs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,17 @@ function BridgeJSRuntimeTests_runJsWorks(instance, exports) {
107107
assert.equal(exports.roundTripJSObject(anyObject), anyObject);
108108

109109
try {
110-
exports.throwsSwiftError();
110+
exports.throwsSwiftError(true);
111111
assert.fail("Expected error");
112112
} catch (error) {
113113
assert.equal(error.message, "TestError", error);
114114
}
115+
116+
try {
117+
exports.throwsSwiftError(false);
118+
} catch (error) {
119+
assert.fail("Expected no error");
120+
}
115121
}
116122

117123
function setupTestGlobals(global) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy