@basthon/kernel-base 0.64.0 → 0.64.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.64.0";
1
+ export declare const VERSION = "0.64.2";
package/lib/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "0.64.0";
1
+ export const VERSION = "0.64.2";
@@ -5,6 +5,7 @@ export declare class KernelMainBase<Type extends KernelWorkerBase> extends Kerne
5
5
  private _worker?;
6
6
  private _remote?;
7
7
  private _legacy;
8
+ private _sab?;
8
9
  constructor(options: any);
9
10
  /**
10
11
  * Is this a legacy worker? i.e. not runing in a worker.
@@ -28,6 +28,8 @@ export class KernelMainBase extends KernelBase {
28
28
  super(options);
29
29
  this._legacy = false;
30
30
  this._legacy = options?.legacy === true;
31
+ if (!this.isLegacy() && globalThis.crossOriginIsolated)
32
+ this._sab = new SharedArrayBuffer(1024);
31
33
  }
32
34
  /**
33
35
  * Is this a legacy worker? i.e. not runing in a worker.
@@ -98,6 +100,10 @@ export class KernelMainBase extends KernelBase {
98
100
  const proxy = wrap(proxyWorker);
99
101
  // setup the kernel worker
100
102
  await this.setupWorker();
103
+ if (this._sab != null) {
104
+ console.log("Using SharedArrayBuffer for synchronous worker communication.");
105
+ await this._remote?.setSAB(this._sab);
106
+ }
101
107
  // bind ports
102
108
  const port = await proxy[createEndpoint]();
103
109
  await this.remote?.setProxyPort(transfer(port, [port]));
@@ -139,12 +145,14 @@ export class KernelMainBase extends KernelBase {
139
145
  this.dispatchEvent("eval.display", msg.data);
140
146
  break;
141
147
  case "input":
142
- const { prompt, password, data, sab } = msg.content;
148
+ const { prompt, password, data } = msg.content;
143
149
  (async () => {
144
150
  const res = (await this.inputAsync(prompt, password, data));
151
+ if (this._sab == null)
152
+ return;
145
153
  const encodedRes = new TextEncoder().encode(res);
146
- new Uint8Array(sab).set(encodedRes.subarray(0, sab.byteLength));
147
- Atomics.notify(new Int32Array(sab), 0, 1);
154
+ new Uint8Array(this._sab).set(encodedRes.subarray(0, this._sab.byteLength));
155
+ Atomics.notify(new Int32Array(this._sab), 0, 1);
148
156
  })();
149
157
  break;
150
158
  default: // ignoring (probably a comlink message)
@@ -5,6 +5,8 @@ declare global {
5
5
  _remoteProxy: Remote<ProxyFetch>;
6
6
  basthon: KernelWorkerBase;
7
7
  _prompt: (p?: string, _d?: string) => string | null;
8
+ window?: DedicatedWorkerGlobalScope;
9
+ Canvas: typeof OffscreenCanvas | ((w: number, h: number) => HTMLCanvasElement);
8
10
  }
9
11
  }
10
12
  type ExecuteResult = {
@@ -19,11 +21,16 @@ declare class KernelWorkerBase {
19
21
  private _ready;
20
22
  protected __eval_data__: any;
21
23
  protected postMessage?: InstanceType<typeof MessagePort>["postMessage"];
24
+ private _sab?;
22
25
  constructor(options?: any);
23
26
  /**
24
27
  * Is this a legacy worker? i.e. not runing in a worker.
25
28
  */
26
29
  isLegacy(): boolean;
30
+ /**
31
+ * Set SharedArrayBuffer from main thread.
32
+ */
33
+ setSAB(sab: SharedArrayBuffer): void;
27
34
  protected _init(): Promise<void>;
28
35
  /**
29
36
  * Initialize the kernel. It is called by initWorker in KernelBase.
@@ -113,7 +120,7 @@ declare class KernelWorkerBase {
113
120
  private decodeSharedArrayBuffer;
114
121
  /**
115
122
  * Sync input that does not use the ugly prompt but send
116
- * an eval.input event tho the main thread and wait for the response.
123
+ * an eval.input event to the main thread and wait for the response.
117
124
  */
118
125
  input(prompt: string | null | undefined, password?: boolean): string;
119
126
  }
@@ -13,10 +13,24 @@ class KernelWorkerBase {
13
13
  mockFetch(this.isLegacy());
14
14
  // useful for ffi
15
15
  self.basthon = this;
16
+ // the window object is not available in a webworker...
17
+ if (self.window == null)
18
+ self.window = self;
19
+ // common way to create a canvas in worker and legacy mode
20
+ self.Canvas = !this.isLegacy()
21
+ ? globalThis.OffscreenCanvas
22
+ : function Canvas(width, height) {
23
+ const canvas = document.createElement("canvas");
24
+ if (width != null)
25
+ canvas.width = width;
26
+ if (height != null)
27
+ canvas.height = height;
28
+ return canvas;
29
+ };
16
30
  // global prompt redirected to our input
17
31
  if (self._prompt == null)
18
32
  self._prompt = globalThis.prompt;
19
- globalThis.prompt = (prompt) => this.input(prompt);
33
+ globalThis.prompt = (p) => this.input(p);
20
34
  }
21
35
  /**
22
36
  * Is this a legacy worker? i.e. not runing in a worker.
@@ -24,6 +38,12 @@ class KernelWorkerBase {
24
38
  isLegacy() {
25
39
  return this._legacy;
26
40
  }
41
+ /**
42
+ * Set SharedArrayBuffer from main thread.
43
+ */
44
+ setSAB(sab) {
45
+ this._sab = sab;
46
+ }
27
47
  /*
28
48
  * Initialize the kernel. Should be overloaded.
29
49
  */
@@ -200,9 +220,10 @@ class KernelWorkerBase {
200
220
  */
201
221
  sleep(duration) {
202
222
  duration = duration * 1000; // to milliseconds
203
- if (globalThis.crossOriginIsolated && !this.isLegacy()) {
204
- const sab = new Int32Array(new SharedArrayBuffer(4));
205
- Atomics.wait(sab, 0, 0, duration);
223
+ if (this._sab != null) {
224
+ // ensure value before waiting
225
+ Atomics.store(new Int32Array(this._sab), 0, 0);
226
+ Atomics.wait(new Int32Array(this._sab), 0, 0, duration);
206
227
  return;
207
228
  }
208
229
  else {
@@ -237,18 +258,19 @@ class KernelWorkerBase {
237
258
  }
238
259
  /**
239
260
  * Sync input that does not use the ugly prompt but send
240
- * an eval.input event tho the main thread and wait for the response.
261
+ * an eval.input event to the main thread and wait for the response.
241
262
  */
242
263
  input(prompt, password = false) {
243
- if (globalThis.crossOriginIsolated && !this.isLegacy()) {
264
+ if (this._sab != null) {
244
265
  const data = this.clone(this.__eval_data__);
245
- const sab = new SharedArrayBuffer(1024);
246
266
  this.postMessage?.({
247
267
  type: "input",
248
- content: { data, prompt, password, sab },
268
+ content: { data, prompt, password },
249
269
  });
250
- Atomics.wait(new Int32Array(sab), 0, 0);
251
- return this.decodeSharedArrayBuffer(sab);
270
+ // ensure value before waiting
271
+ Atomics.store(new Int32Array(this._sab), 0, 0);
272
+ Atomics.wait(new Int32Array(this._sab), 0, 0);
273
+ return this.decodeSharedArrayBuffer(this._sab);
252
274
  }
253
275
  else {
254
276
  return self._prompt(prompt ?? "") ?? "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@basthon/kernel-base",
3
- "version": "0.64.0",
3
+ "version": "0.64.2",
4
4
  "description": "Basthon - Base Kernel",
5
5
  "homepage": "https://basthon.fr",
6
6
  "bugs": {
@@ -44,5 +44,5 @@
44
44
  "publishConfig": {
45
45
  "access": "public"
46
46
  },
47
- "gitHead": "1e6bb930f8b51e2a307179afcd9426d2672af4e3"
47
+ "gitHead": "69cabf8bc55521ec363d74e780f9998604bef012"
48
48
  }