From 5a90255cb947f14df345ab1d8b46bba5161b2ebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Thu, 21 Mar 2024 10:30:13 +0100 Subject: [PATCH] Add global `structuredClone()` --- package-lock.json | 8 ++++---- package.json | 2 +- src/globals.js | 6 ++++++ src/thread.js | 6 +++--- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3614c89..488daed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,7 @@ "bare-os": "^2.0.0", "bare-path": "^2.0.0", "bare-repl": "^1.0.0", - "bare-structured-clone": "^1.0.1", + "bare-structured-clone": "^1.0.2", "bare-timers": "^2.0.0", "bare-tty": "^3.0.0", "bare-url": "^1.0.0", @@ -560,9 +560,9 @@ } }, "node_modules/bare-structured-clone": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/bare-structured-clone/-/bare-structured-clone-1.0.1.tgz", - "integrity": "sha512-12J7XJXbgwMHQyAxOHqEU0orVKcasew0V5Puaz7nSciJpFDu900FEu6eogaYZ2VJPFfzvOtE9idMu44i2V9PAQ==", + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/bare-structured-clone/-/bare-structured-clone-1.0.2.tgz", + "integrity": "sha512-j5sxrncxE3IFXyytErlHAi7DMMMp6rA0e/UJjjkLLcs36eunPkrdzzfDU7pZLnKXQy+eYwaGU8C8uVH/WRMvIg==", "dependencies": { "bits-to-bytes": "^1.3.0", "compact-encoding": "^2.15.0", diff --git a/package.json b/package.json index 55dd031..c8e1ace 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "bare-os": "^2.0.0", "bare-path": "^2.0.0", "bare-repl": "^1.0.0", - "bare-structured-clone": "^1.0.1", + "bare-structured-clone": "^1.0.2", "bare-timers": "^2.0.0", "bare-tty": "^3.0.0", "bare-url": "^1.0.0", diff --git a/src/globals.js b/src/globals.js index 53fa7ad..8b223bb 100644 --- a/src/globals.js +++ b/src/globals.js @@ -21,6 +21,12 @@ global.queueMicrotask = function queueMicrotask (fn) { .catch(err => setImmediate(() => { throw err })) } +/** + * https://developer.mozilla.org/en-US/docs/Web/API/structuredClone + */ + +global.structuredClone = require('bare-structured-clone') + /** * Thanks to Node.js, it is customary for the buffer API to be available * globally and many of the core modules in Bare also assume this. diff --git a/src/thread.js b/src/thread.js index 6818fa4..f58e1b4 100644 --- a/src/thread.js +++ b/src/thread.js @@ -37,12 +37,12 @@ module.exports = exports = class Thread { const serialized = structuredClone.serialize(data) const state = { start: 0, end: 0, buffer: null } + structuredClone.preencode(state, serialized) - state.buffer = Buffer.allocUnsafe(state.end) - structuredClone.encode(state, serialized) + state.buffer = data = Buffer.allocUnsafe(state.end) - data = state.buffer + structuredClone.encode(state, serialized) } this._handle = bare.setupThread(filename, source, data, stackSize)