Skip to content

Commit ba66ff1

Browse files
authored
Merge pull request #693 from PassiveLogic/kr/fix-declare-global-class-stubs
BridgeJS: Include properties and release() in declare global class stubs
2 parents 3d25cd9 + 757f2a0 commit ba66ff1

File tree

8 files changed

+58
-0
lines changed

8 files changed

+58
-0
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,6 +2948,16 @@ extension BridgeJSLink {
29482948
"\(method.name)\(renderTSSignatureCallback(method.parameters, method.returnType, method.effects));"
29492949
printer.write(methodSignature)
29502950
}
2951+
2952+
let sortedProperties = klass.properties.filter { !$0.isStatic }.sorted {
2953+
$0.name < $1.name
2954+
}
2955+
for property in sortedProperties {
2956+
let readonly = property.isReadonly ? "readonly " : ""
2957+
printer.write("\(readonly)\(property.name): \(property.type.tsType);")
2958+
}
2959+
2960+
printer.write("release(): void;")
29512961
}
29522962
printer.write("}")
29532963
}

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumNamespace.Global.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ declare global {
3434
class Converter {
3535
constructor();
3636
format(value: number): string;
37+
release(): void;
3738
}
3839
}
3940
namespace Networking {
4041
namespace API {
4142
class HTTPServer {
4243
constructor();
4344
call(method: Networking.API.MethodTag): void;
45+
release(): void;
4446
}
4547
const MethodValues: {
4648
readonly Get: 0;
@@ -55,6 +57,7 @@ declare global {
5557
class TestServer {
5658
constructor();
5759
call(method: Networking.APIV2.Internal.SupportedMethodTag): void;
60+
release(): void;
5861
}
5962
const SupportedMethodValues: {
6063
readonly Get: 0;
@@ -77,6 +80,8 @@ declare global {
7780
class Converter {
7881
constructor();
7982
toString(value: number): string;
83+
precision: number;
84+
release(): void;
8085
}
8186
}
8287
}

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MixedModules.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ declare global {
1111
class GlobalClass {
1212
constructor();
1313
greet(): string;
14+
release(): void;
1415
}
1516
function globalFunction(): string;
1617
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ declare global {
1212
constructor();
1313
addItem(item: Greeter): void;
1414
getItems(): Greeter[];
15+
release(): void;
1516
}
1617
}
1718
namespace MyModule {
@@ -24,6 +25,7 @@ declare global {
2425
class Converter {
2526
constructor();
2627
toString(value: number): string;
28+
release(): void;
2729
}
2830
}
2931
}
@@ -32,9 +34,11 @@ declare global {
3234
class Greeter {
3335
constructor(name: string);
3436
greet(): string;
37+
release(): void;
3538
}
3639
class UUID {
3740
uuidString(): string;
41+
release(): void;
3842
}
3943
}
4044
}

Tests/BridgeJSGlobalTests/Generated/BridgeJS.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,27 @@ public func _bjs_GlobalUtils_PublicConverter_toString(_ _self: UnsafeMutableRawP
296296
#endif
297297
}
298298

299+
@_expose(wasm, "bjs_GlobalUtils_PublicConverter_precision_get")
300+
@_cdecl("bjs_GlobalUtils_PublicConverter_precision_get")
301+
public func _bjs_GlobalUtils_PublicConverter_precision_get(_ _self: UnsafeMutableRawPointer) -> Int32 {
302+
#if arch(wasm32)
303+
let ret = GlobalUtils.PublicConverter.bridgeJSLiftParameter(_self).precision
304+
return ret.bridgeJSLowerReturn()
305+
#else
306+
fatalError("Only available on WebAssembly")
307+
#endif
308+
}
309+
310+
@_expose(wasm, "bjs_GlobalUtils_PublicConverter_precision_set")
311+
@_cdecl("bjs_GlobalUtils_PublicConverter_precision_set")
312+
public func _bjs_GlobalUtils_PublicConverter_precision_set(_ _self: UnsafeMutableRawPointer, _ value: Int32) -> Void {
313+
#if arch(wasm32)
314+
GlobalUtils.PublicConverter.bridgeJSLiftParameter(_self).precision = Int.bridgeJSLiftParameter(value)
315+
#else
316+
fatalError("Only available on WebAssembly")
317+
#endif
318+
}
319+
299320
@_expose(wasm, "bjs_GlobalUtils_PublicConverter_deinit")
300321
@_cdecl("bjs_GlobalUtils_PublicConverter_deinit")
301322
public func _bjs_GlobalUtils_PublicConverter_deinit(_ pointer: UnsafeMutableRawPointer) -> Void {

Tests/BridgeJSGlobalTests/Generated/JavaScript/BridgeJS.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,19 @@
156156
"GlobalUtils"
157157
],
158158
"properties" : [
159+
{
160+
"isReadonly" : false,
161+
"isStatic" : false,
162+
"name" : "precision",
163+
"namespace" : [
164+
"GlobalUtils"
165+
],
166+
"type" : {
167+
"int" : {
159168

169+
}
170+
}
171+
}
160172
],
161173
"swiftCallName" : "GlobalUtils.PublicConverter"
162174
}

Tests/BridgeJSGlobalTests/GlobalAPITests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ enum Internal {
6363

6464
@JS enum GlobalUtils {
6565
@JS class PublicConverter {
66+
@JS var precision: Int = 2
67+
6668
@JS init() {}
6769

6870
@JS func toString(value: Int) -> String {

Tests/prelude.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,9 @@ function BridgeJSGlobalTests_runJsWorksGlobal() {
10101010

10111011
const globalConverter = new globalThis.GlobalUtils.PublicConverter();
10121012
assert.equal(globalConverter.toString(99), "99");
1013+
assert.equal(globalConverter.precision, 2);
1014+
globalConverter.precision = 5;
1015+
assert.equal(globalConverter.precision, 5);
10131016
globalConverter.release();
10141017

10151018
const globalHttpServer = new globalThis.GlobalNetworking.API.TestHTTPServer();

0 commit comments

Comments
 (0)