Skip to content

Commit 7a2de9f

Browse files
committed
experimental: possible option 1) for better API for UUID usage through exports and namespaces
1 parent c53390e commit 7a2de9f

File tree

5 files changed

+67
-102
lines changed

5 files changed

+67
-102
lines changed

Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/Namespaces.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
@JS("__Swift.Foundation.UUID") func create() -> String { UUID().uuidString }
2-
@JS("__Swift.Foundation.UUID") func validate(uuid: String) -> Bool { true }
3-
41
@JS func plainFunction() -> String { "plain" }
52

63
@JS("__Swift.Foundation") class Greeter {
@@ -26,3 +23,11 @@
2623
return String(value)
2724
}
2825
}
26+
27+
@JS("__Swift.Foundation")
28+
class UUID {
29+
@JS("__Swift.Foundation")
30+
@JS func uuidString() -> String {
31+
Foundation.UUID().uuidString
32+
}
33+
}

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Namespaces.Export.d.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@ declare global {
2222
greet(): string;
2323
changeName(name: string): void;
2424
}
25-
}
26-
}
27-
namespace __Swift {
28-
namespace Foundation {
29-
namespace UUID {
30-
function create(): string;
31-
function validate(uuid: string): boolean;
25+
class UUID {
26+
uuidString(): string;
3227
}
3328
}
3429
}
@@ -48,15 +43,18 @@ export interface Greeter extends SwiftHeapObject {
4843
export interface Converter extends SwiftHeapObject {
4944
toString(value: number): string;
5045
}
46+
export interface UUID extends SwiftHeapObject {
47+
uuidString(): string;
48+
}
5149
export type Exports = {
5250
Greeter: {
5351
new(name: string): Greeter;
5452
}
5553
Converter: {
5654
new(): Converter;
5755
}
58-
create(): string;
59-
validate(uuid: string): boolean;
56+
UUID: {
57+
}
6058
plainFunction(): string;
6159
}
6260
export type Imports = {

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Namespaces.Export.js

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,18 @@ export async function createInstantiator(options, swift) {
108108
return ret;
109109
}
110110
}
111-
const exports = {
112-
Greeter,
113-
Converter,
114-
create: function bjs_create() {
115-
instance.exports.bjs_create();
111+
class UUID extends SwiftHeapObject {
112+
uuidString() {
113+
instance.exports.bjs_UUID_uuidString(this.pointer);
116114
const ret = tmpRetString;
117115
tmpRetString = undefined;
118116
return ret;
119-
},
120-
validate: function bjs_validate(uuid) {
121-
const uuidBytes = textEncoder.encode(uuid);
122-
const uuidId = swift.memory.retain(uuidBytes);
123-
const ret = instance.exports.bjs_validate(uuidId, uuidBytes.length) !== 0;
124-
swift.memory.release(uuidId);
125-
return ret;
126-
},
117+
}
118+
}
119+
const exports = {
120+
Greeter,
121+
Converter,
122+
UUID,
127123
plainFunction: function bjs_plainFunction() {
128124
instance.exports.bjs_plainFunction();
129125
const ret = tmpRetString;
@@ -144,13 +140,9 @@ export async function createInstantiator(options, swift) {
144140
if (typeof globalThis.__Swift.Foundation === 'undefined') {
145141
globalThis.__Swift.Foundation = {};
146142
}
147-
if (typeof globalThis.__Swift.Foundation.UUID === 'undefined') {
148-
globalThis.__Swift.Foundation.UUID = {};
149-
}
150143
globalThis.__Swift.Foundation.Greeter = exports.Greeter;
151144
globalThis.Utils.Converters.Converter = exports.Converter;
152-
globalThis.__Swift.Foundation.UUID.create = exports.create;
153-
globalThis.__Swift.Foundation.UUID.validate = exports.validate;
145+
globalThis.__Swift.Foundation.UUID = exports.UUID;
154146

155147
return exports;
156148
},

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Namespaces.json

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -121,59 +121,38 @@
121121
"Utils",
122122
"Converters"
123123
]
124-
}
125-
],
126-
"functions" : [
127-
{
128-
"abiName" : "bjs_create",
129-
"effects" : {
130-
"isAsync" : false,
131-
"isThrows" : false
132-
},
133-
"name" : "create",
134-
"namespace" : [
135-
"__Swift",
136-
"Foundation",
137-
"UUID"
138-
],
139-
"parameters" : [
140-
141-
],
142-
"returnType" : {
143-
"string" : {
144-
145-
}
146-
}
147124
},
148125
{
149-
"abiName" : "bjs_validate",
150-
"effects" : {
151-
"isAsync" : false,
152-
"isThrows" : false
153-
},
154-
"name" : "validate",
155-
"namespace" : [
156-
"__Swift",
157-
"Foundation",
158-
"UUID"
159-
],
160-
"parameters" : [
126+
"methods" : [
161127
{
162-
"label" : "uuid",
163-
"name" : "uuid",
164-
"type" : {
128+
"abiName" : "bjs_UUID_uuidString",
129+
"effects" : {
130+
"isAsync" : false,
131+
"isThrows" : false
132+
},
133+
"name" : "uuidString",
134+
"namespace" : [
135+
"__Swift",
136+
"Foundation"
137+
],
138+
"parameters" : [
139+
140+
],
141+
"returnType" : {
165142
"string" : {
166143

167144
}
168145
}
169146
}
170147
],
171-
"returnType" : {
172-
"bool" : {
173-
174-
}
175-
}
176-
},
148+
"name" : "UUID",
149+
"namespace" : [
150+
"__Swift",
151+
"Foundation"
152+
]
153+
}
154+
],
155+
"functions" : [
177156
{
178157
"abiName" : "bjs_plainFunction",
179158
"effects" : {

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Namespaces.swift

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,6 @@
66

77
@_spi(BridgeJS) import JavaScriptKit
88

9-
@_expose(wasm, "bjs_create")
10-
@_cdecl("bjs_create")
11-
public func _bjs_create() -> Void {
12-
#if arch(wasm32)
13-
var ret = create()
14-
return ret.withUTF8 { ptr in
15-
_swift_js_return_string(ptr.baseAddress, Int32(ptr.count))
16-
}
17-
#else
18-
fatalError("Only available on WebAssembly")
19-
#endif
20-
}
21-
22-
@_expose(wasm, "bjs_validate")
23-
@_cdecl("bjs_validate")
24-
public func _bjs_validate(uuidBytes: Int32, uuidLen: Int32) -> Int32 {
25-
#if arch(wasm32)
26-
let uuid = String(unsafeUninitializedCapacity: Int(uuidLen)) { b in
27-
_swift_js_init_memory(uuidBytes, b.baseAddress.unsafelyUnwrapped)
28-
return Int(uuidLen)
29-
}
30-
let ret = validate(uuid: uuid)
31-
return Int32(ret ? 1 : 0)
32-
#else
33-
fatalError("Only available on WebAssembly")
34-
#endif
35-
}
36-
379
@_expose(wasm, "bjs_plainFunction")
3810
@_cdecl("bjs_plainFunction")
3911
public func _bjs_plainFunction() -> Void {
@@ -123,4 +95,23 @@ public func _bjs_Converter_toString(_self: UnsafeMutableRawPointer, value: Int32
12395
@_cdecl("bjs_Converter_deinit")
12496
public func _bjs_Converter_deinit(pointer: UnsafeMutableRawPointer) {
12597
Unmanaged<Converter>.fromOpaque(pointer).release()
98+
}
99+
100+
@_expose(wasm, "bjs_UUID_uuidString")
101+
@_cdecl("bjs_UUID_uuidString")
102+
public func _bjs_UUID_uuidString(_self: UnsafeMutableRawPointer) -> Void {
103+
#if arch(wasm32)
104+
var ret = Unmanaged<UUID>.fromOpaque(_self).takeUnretainedValue().uuidString()
105+
return ret.withUTF8 { ptr in
106+
_swift_js_return_string(ptr.baseAddress, Int32(ptr.count))
107+
}
108+
#else
109+
fatalError("Only available on WebAssembly")
110+
#endif
111+
}
112+
113+
@_expose(wasm, "bjs_UUID_deinit")
114+
@_cdecl("bjs_UUID_deinit")
115+
public func _bjs_UUID_deinit(pointer: UnsafeMutableRawPointer) {
116+
Unmanaged<UUID>.fromOpaque(pointer).release()
126117
}

0 commit comments

Comments
 (0)