Skip to content

Commit 95a2a49

Browse files
Merge pull request swiftwasm#644 from PassiveLogic/kr/prettier-ci-and-find-global-cleanup
Runtime: Enforce prettier formatting, simplify find-global.ts
2 parents 04ed1c2 + 504ae31 commit 95a2a49

File tree

9 files changed

+495
-253
lines changed

9 files changed

+495
-253
lines changed

.github/workflows/test.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ jobs:
119119
env:
120120
DEVELOPER_DIR: /Applications/${{ matrix.xcode }}.app/Contents/Developer/
121121

122+
prettier:
123+
runs-on: ubuntu-latest
124+
steps:
125+
- uses: actions/checkout@v6
126+
- uses: actions/setup-node@v6
127+
with:
128+
node-version: '20'
129+
- run: npm install
130+
- run: npx prettier --check Runtime/src
131+
122132
format:
123133
runs-on: ubuntu-latest
124134
container:

Plugins/PackageToJS/Templates/runtime.mjs

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/// Memory lifetime of closures in Swift are managed by Swift side
22
class SwiftClosureDeallocator {
3-
constructor(exports) {
3+
constructor(exports$1) {
44
if (typeof FinalizationRegistry === "undefined") {
55
throw new Error("The Swift part of JavaScriptKit was configured to require " +
66
"the availability of JavaScript WeakRefs. Please build " +
77
"with `-Xswiftc -DJAVASCRIPTKIT_WITHOUT_WEAKREFS` to " +
88
"disable features that use WeakRefs.");
99
}
1010
this.functionRegistry = new FinalizationRegistry((id) => {
11-
exports.swjs_free_host_function(id);
11+
exports$1.swjs_free_host_function(id);
1212
});
1313
}
1414
track(func, func_ref) {
@@ -126,12 +126,12 @@ class ITCInterface {
126126
}
127127
send(sendingObject, transferringObjects, sendingContext) {
128128
const object = this.memory.getObject(sendingObject);
129-
const transfer = transferringObjects.map(ref => this.memory.getObject(ref));
129+
const transfer = transferringObjects.map((ref) => this.memory.getObject(ref));
130130
return { object, sendingContext, transfer };
131131
}
132132
sendObjects(sendingObjects, transferringObjects, sendingContext) {
133-
const objects = sendingObjects.map(ref => this.memory.getObject(ref));
134-
const transfer = transferringObjects.map(ref => this.memory.getObject(ref));
133+
const objects = sendingObjects.map((ref) => this.memory.getObject(ref));
134+
const transfer = transferringObjects.map((ref) => this.memory.getObject(ref));
135135
return { object: objects, sendingContext, transfer };
136136
}
137137
release(objectRef) {
@@ -168,7 +168,9 @@ class MessageBroker {
168168
this.handlers.onResponse(message);
169169
return;
170170
}
171-
const transfer = message.data.response.ok ? message.data.response.value.transfer : [];
171+
const transfer = message.data.response.ok
172+
? message.data.response.value.transfer
173+
: [];
172174
if ("postMessageToWorkerThread" in this.threadChannel) {
173175
// The response is for another worker thread sent from the main thread
174176
this.threadChannel.postMessageToWorkerThread(message.data.sourceTid, message, transfer);
@@ -186,7 +188,7 @@ class MessageBroker {
186188
this.handlers.onRequest(message);
187189
}
188190
else if ("postMessageToWorkerThread" in this.threadChannel) {
189-
// Receive a request from a worker thread to other worker on main thread.
191+
// Receive a request from a worker thread to other worker on main thread.
190192
// Proxy the request to the target worker thread.
191193
this.threadChannel.postMessageToWorkerThread(message.data.targetTid, message, []);
192194
}
@@ -202,7 +204,9 @@ class MessageBroker {
202204
else if ("postMessageToWorkerThread" in this.threadChannel) {
203205
// Receive a response from a worker thread to other worker on main thread.
204206
// Proxy the response to the target worker thread.
205-
const transfer = message.data.response.ok ? message.data.response.value.transfer : [];
207+
const transfer = message.data.response.ok
208+
? message.data.response.value.transfer
209+
: [];
206210
this.threadChannel.postMessageToWorkerThread(message.data.sourceTid, message, transfer);
207211
}
208212
else if ("postMessageToMainThread" in this.threadChannel) {
@@ -213,7 +217,14 @@ class MessageBroker {
213217
}
214218
function serializeError(error) {
215219
if (error instanceof Error) {
216-
return { isError: true, value: { message: error.message, name: error.name, stack: error.stack } };
220+
return {
221+
isError: true,
222+
value: {
223+
message: error.message,
224+
name: error.name,
225+
stack: error.stack,
226+
},
227+
};
217228
}
218229
return { isError: false, value: error };
219230
}
@@ -224,19 +235,7 @@ function deserializeError(error) {
224235
return error.value;
225236
}
226237

227-
let globalVariable;
228-
if (typeof globalThis !== "undefined") {
229-
globalVariable = globalThis;
230-
}
231-
else if (typeof window !== "undefined") {
232-
globalVariable = window;
233-
}
234-
else if (typeof global !== "undefined") {
235-
globalVariable = global;
236-
}
237-
else if (typeof self !== "undefined") {
238-
globalVariable = self;
239-
}
238+
const globalVariable = globalThis;
240239

241240
class JSObjectSpace {
242241
constructor() {
@@ -312,7 +311,8 @@ class SwiftRuntime {
312311
// 1. It may not be available in the global scope if the context is not cross-origin isolated.
313312
// 2. The underlying buffer may be still backed by SAB even if the context is not cross-origin
314313
// isolated (e.g. localhost on Chrome on Android).
315-
if (Object.getPrototypeOf(wasmMemory.buffer).constructor.name === "SharedArrayBuffer") {
314+
if (Object.getPrototypeOf(wasmMemory.buffer).constructor.name ===
315+
"SharedArrayBuffer") {
316316
// When the wasm memory is backed by a SharedArrayBuffer, growing the memory
317317
// doesn't invalidate the data view by setting the byte length to 0. Instead,
318318
// the data view points to an old buffer after growing the memory. So we have
@@ -463,7 +463,10 @@ class SwiftRuntime {
463463
returnValue = { ok: true, value: result };
464464
}
465465
catch (error) {
466-
returnValue = { ok: false, error: serializeError(error) };
466+
returnValue = {
467+
ok: false,
468+
error: serializeError(error),
469+
};
467470
}
468471
const responseMessage = {
469472
type: "response",
@@ -479,7 +482,7 @@ class SwiftRuntime {
479482
catch (error) {
480483
responseMessage.data.response = {
481484
ok: false,
482-
error: serializeError(new TypeError(`Failed to serialize message: ${error}`))
485+
error: serializeError(new TypeError(`Failed to serialize message: ${error}`)),
483486
};
484487
newBroker.reply(responseMessage);
485488
}
@@ -494,7 +497,7 @@ class SwiftRuntime {
494497
const errorObject = this.memory.retain(error);
495498
this.exports.swjs_receive_error(errorObject, message.data.context);
496499
}
497-
}
500+
},
498501
});
499502
broker = newBroker;
500503
return newBroker;
@@ -532,21 +535,19 @@ class SwiftRuntime {
532535
this.getDataView().setUint32(bytes_ptr_result, bytes_ptr, true);
533536
return bytes.length;
534537
},
535-
swjs_decode_string: (
538+
swjs_decode_string:
536539
// NOTE: TextDecoder can't decode typed arrays backed by SharedArrayBuffer
537540
this.options.sharedMemory == true
538-
? ((bytes_ptr, length) => {
539-
const bytes = this.getUint8Array()
540-
.slice(bytes_ptr, bytes_ptr + length);
541+
? (bytes_ptr, length) => {
542+
const bytes = this.getUint8Array().slice(bytes_ptr, bytes_ptr + length);
541543
const string = this.textDecoder.decode(bytes);
542544
return this.memory.retain(string);
543-
})
544-
: ((bytes_ptr, length) => {
545-
const bytes = this.getUint8Array()
546-
.subarray(bytes_ptr, bytes_ptr + length);
545+
}
546+
: (bytes_ptr, length) => {
547+
const bytes = this.getUint8Array().subarray(bytes_ptr, bytes_ptr + length);
547548
const string = this.textDecoder.decode(bytes);
548549
return this.memory.retain(string);
549-
})),
550+
},
550551
swjs_load_string: (ref, buffer) => {
551552
const bytes = this.memory.getObject(ref);
552553
this.getUint8Array().set(bytes, buffer);
@@ -658,7 +659,9 @@ class SwiftRuntime {
658659
// Call `.slice()` to copy the memory
659660
return this.memory.retain(array.slice());
660661
},
661-
swjs_create_object: () => { return this.memory.retain({}); },
662+
swjs_create_object: () => {
663+
return this.memory.retain({});
664+
},
662665
swjs_load_typed_array: (ref, buffer) => {
663666
const memory = this.memory;
664667
const typedArray = memory.getObject(ref);
@@ -683,8 +686,8 @@ class SwiftRuntime {
683686
request: {
684687
method: "release",
685688
parameters: [ref],
686-
}
687-
}
689+
},
690+
},
688691
});
689692
},
690693
swjs_i64_to_bigint: (value, signed) => {
@@ -708,17 +711,23 @@ class SwiftRuntime {
708711
swjs_i64_to_bigint_slow: (lower, upper, signed) => {
709712
const value = BigInt.asUintN(32, BigInt(lower)) +
710713
(BigInt.asUintN(32, BigInt(upper)) << BigInt(32));
711-
return this.memory.retain(signed ? BigInt.asIntN(64, value) : BigInt.asUintN(64, value));
714+
return this.memory.retain(signed
715+
? BigInt.asIntN(64, value)
716+
: BigInt.asUintN(64, value));
712717
},
713718
swjs_unsafe_event_loop_yield: () => {
714719
throw new UnsafeEventLoopYield();
715720
},
716721
swjs_send_job_to_main_thread: (unowned_job) => {
717-
this.postMessageToMainThread({ type: "job", data: unowned_job });
722+
this.postMessageToMainThread({
723+
type: "job",
724+
data: unowned_job,
725+
});
718726
},
719727
swjs_listen_message_from_main_thread: () => {
720728
const threadChannel = this.options.threadChannel;
721-
if (!(threadChannel && "listenMessageFromMainThread" in threadChannel)) {
729+
if (!(threadChannel &&
730+
"listenMessageFromMainThread" in threadChannel)) {
722731
throw new Error("listenMessageFromMainThread is not set in options given to SwiftRuntime. Please set it to listen to wake events from the main thread.");
723732
}
724733
const broker = getMessageBroker(threadChannel);
@@ -746,7 +755,8 @@ class SwiftRuntime {
746755
},
747756
swjs_listen_message_from_worker_thread: (tid) => {
748757
const threadChannel = this.options.threadChannel;
749-
if (!(threadChannel && "listenMessageFromWorkerThread" in threadChannel)) {
758+
if (!(threadChannel &&
759+
"listenMessageFromWorkerThread" in threadChannel)) {
750760
throw new Error("listenMessageFromWorkerThread is not set in options given to SwiftRuntime. Please set it to listen to jobs from worker threads.");
751761
}
752762
const broker = getMessageBroker(threadChannel);
@@ -795,9 +805,13 @@ class SwiftRuntime {
795805
context: sending_context,
796806
request: {
797807
method: "send",
798-
parameters: [sending_object, transferringObjects, sending_context],
799-
}
800-
}
808+
parameters: [
809+
sending_object,
810+
transferringObjects,
811+
sending_context,
812+
],
813+
},
814+
},
801815
});
802816
},
803817
swjs_request_sending_objects: (sending_objects, sending_objects_count, transferring_objects, transferring_objects_count, object_source_tid, sending_context) => {
@@ -817,9 +831,13 @@ class SwiftRuntime {
817831
context: sending_context,
818832
request: {
819833
method: "sendObjects",
820-
parameters: [sendingObjects, transferringObjects, sending_context],
821-
}
822-
}
834+
parameters: [
835+
sendingObjects,
836+
transferringObjects,
837+
sending_context,
838+
],
839+
},
840+
},
823841
});
824842
},
825843
};

Runtime/src/closure-heap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class SwiftClosureDeallocator {
1010
"The Swift part of JavaScriptKit was configured to require " +
1111
"the availability of JavaScript WeakRefs. Please build " +
1212
"with `-Xswiftc -DJAVASCRIPTKIT_WITHOUT_WEAKREFS` to " +
13-
"disable features that use WeakRefs."
13+
"disable features that use WeakRefs.",
1414
);
1515
}
1616

Runtime/src/find-global.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1 @@
1-
interface GlobalVariable {}
2-
declare const global: GlobalVariable;
3-
4-
export let globalVariable: any;
5-
if (typeof globalThis !== "undefined") {
6-
globalVariable = globalThis;
7-
} else if (typeof window !== "undefined") {
8-
globalVariable = window;
9-
} else if (typeof global !== "undefined") {
10-
globalVariable = global;
11-
} else if (typeof self !== "undefined") {
12-
globalVariable = self;
13-
}
1+
export const globalVariable: any = globalThis;

0 commit comments

Comments
 (0)