@basthon/kernel-base 0.63.0 → 0.64.1

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.63.0";
1
+ export declare const VERSION = "0.64.1";
package/lib/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "0.63.0";
1
+ export const VERSION = "0.64.1";
@@ -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)
@@ -19,11 +19,16 @@ declare class KernelWorkerBase {
19
19
  private _ready;
20
20
  protected __eval_data__: any;
21
21
  protected postMessage?: InstanceType<typeof MessagePort>["postMessage"];
22
+ private _sab?;
22
23
  constructor(options?: any);
23
24
  /**
24
25
  * Is this a legacy worker? i.e. not runing in a worker.
25
26
  */
26
27
  isLegacy(): boolean;
28
+ /**
29
+ * Set SharedArrayBuffer from main thread.
30
+ */
31
+ setSAB(sab: SharedArrayBuffer): void;
27
32
  protected _init(): Promise<void>;
28
33
  /**
29
34
  * Initialize the kernel. It is called by initWorker in KernelBase.
@@ -113,7 +118,7 @@ declare class KernelWorkerBase {
113
118
  private decodeSharedArrayBuffer;
114
119
  /**
115
120
  * 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.
121
+ * an eval.input event to the main thread and wait for the response.
117
122
  */
118
123
  input(prompt: string | null | undefined, password?: boolean): string;
119
124
  }
@@ -16,7 +16,7 @@ class KernelWorkerBase {
16
16
  // global prompt redirected to our input
17
17
  if (self._prompt == null)
18
18
  self._prompt = globalThis.prompt;
19
- globalThis.prompt = (prompt) => this.input(prompt);
19
+ globalThis.prompt = (p) => this.input(p);
20
20
  }
21
21
  /**
22
22
  * Is this a legacy worker? i.e. not runing in a worker.
@@ -24,6 +24,12 @@ class KernelWorkerBase {
24
24
  isLegacy() {
25
25
  return this._legacy;
26
26
  }
27
+ /**
28
+ * Set SharedArrayBuffer from main thread.
29
+ */
30
+ setSAB(sab) {
31
+ this._sab = sab;
32
+ }
27
33
  /*
28
34
  * Initialize the kernel. Should be overloaded.
29
35
  */
@@ -200,9 +206,10 @@ class KernelWorkerBase {
200
206
  */
201
207
  sleep(duration) {
202
208
  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);
209
+ if (this._sab != null) {
210
+ // ensure value before waiting
211
+ Atomics.store(new Int32Array(this._sab), 0, 0);
212
+ Atomics.wait(new Int32Array(this._sab), 0, 0, duration);
206
213
  return;
207
214
  }
208
215
  else {
@@ -237,18 +244,19 @@ class KernelWorkerBase {
237
244
  }
238
245
  /**
239
246
  * 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.
247
+ * an eval.input event to the main thread and wait for the response.
241
248
  */
242
249
  input(prompt, password = false) {
243
- if (globalThis.crossOriginIsolated && !this.isLegacy()) {
250
+ if (this._sab != null) {
244
251
  const data = this.clone(this.__eval_data__);
245
- const sab = new SharedArrayBuffer(1024);
246
252
  this.postMessage?.({
247
253
  type: "input",
248
- content: { data, prompt, password, sab },
254
+ content: { data, prompt, password },
249
255
  });
250
- Atomics.wait(new Int32Array(sab), 0, 0);
251
- return this.decodeSharedArrayBuffer(sab);
256
+ // ensure value before waiting
257
+ Atomics.store(new Int32Array(this._sab), 0, 0);
258
+ Atomics.wait(new Int32Array(this._sab), 0, 0);
259
+ return this.decodeSharedArrayBuffer(this._sab);
252
260
  }
253
261
  else {
254
262
  return self._prompt(prompt ?? "") ?? "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@basthon/kernel-base",
3
- "version": "0.63.0",
3
+ "version": "0.64.1",
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": "bd6fd80041c6cb09cfc5ecbbff3dc69b65974eb8"
47
+ "gitHead": "cb50a95c46ffe82c7510d28316cd102f2fb701b4"
48
48
  }