Skip to content

Commit

Permalink
TextEncoder/TextDecoderv8.serialize/v8.deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobJingleheimer committed Sep 19, 2022
1 parent ff8f2aa commit 14fe47c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
9 changes: 5 additions & 4 deletions lib/internal/modules/esm/public_loader_proxy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { Worker } = require('internal/worker');
const { deserialize, serialize } = require('v8');


/**
Expand All @@ -13,7 +14,7 @@ const commsChannel = new SharedArrayBuffer(2048);
const lock = new Int32Array(commsChannel, 0, 4);
/**
* The request & response segment of the shared memory. TextEncoder/Decoder (needed to convert
* requests & responses into a format supported by the coms channel) reads and writes with
* requests & responses into a format supported by the comms channel) reads and writes with
* Uint8Array.
*/
const requestResponseData = new Uint8Array(commsChannel, 4, 2044);
Expand All @@ -27,12 +28,12 @@ Atomics.wait(lock, 0, 1); // ! Block this module until the worker is ready.

function makeRequest(type, data) {
requestResponseData.fill(0); // Erase any previous request/response (it's already been handled)
const request = JSON.stringify({ data, type });
new TextEncoder().encodeInto(request, requestResponseData);
Atomics.store(lock, 0, 1); // Send request to worker
const request = serialize({ data, type });
requestResponseData.set(request);
Atomics.notify(lock, 0); // Notify worker of new request
Atomics.wait(lock, 0, 1); // Sleep until worker responds
return new TextDecoder().decode(requestResponseData);
return deserialize(requestResponseData);
}

module.exports = {
Expand Down
10 changes: 4 additions & 6 deletions lib/internal/modules/esm/worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { workerData } = require('internal/worker_threads');
const { ESMLoader } = require('internal/modules/esm/loader');
const { workerData } = require('internal/worker_threads');
const { deserialize, serialize } = require('v8');

const { commsChannel } = workerData;
const lock = new Int32Array(commsChannel, 0, 4); // Required by Atomics
Expand All @@ -23,13 +24,10 @@ const types = {
while (true) { // event loop
Atomics.wait(lock, 0, 0); // This pauses the while loop
// Worker is now active and main is sleeping
const { data, type } = JSON.parse(
(new TextDecoder().decode(requestResponseData))
.replaceAll('\x00', '') // strip empty space (not a great solution)
);
const { data, type } = deserialize(requestResponseData);
const response = await types[type](data);
requestResponseData.fill(0);
new TextEncoder().encodeInto(response, requestResponseData);
Atomics.store(lock, 0, 0); // Send response to main
requestResponseData.set(serialize(response));
Atomics.notify(lock, 0); // Notify main of new response
};

0 comments on commit 14fe47c

Please sign in to comment.