@basthon/kernel-javascript 0.84.0 → 0.85.0

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/worker.d.ts CHANGED
@@ -4,8 +4,21 @@ export declare class JavaScriptKernelWorker extends KernelWorkerBase {
4
4
  language(): string;
5
5
  protected _init(): Promise<void>;
6
6
  protected _eval(data: any, code: string): Promise<any>;
7
+ /**
8
+ * Display a canvas created with basthon.createCanvas(width, height) (to
9
+ * be called by the user's code as basthon.displayLiveCanvas(canvas)).
10
+ * The canvas is live: its drawings are displayed and it receives mouse
11
+ * events (keyboard ones on canvas.ownerDocument), as a canvas of a page
12
+ * (see kernel-base's canvas/worker/live-canvas.ts).
13
+ */
14
+ displayLiveCanvas(canvas: OffscreenCanvas | HTMLCanvasElement): Promise<void>;
7
15
  putFile(filename: string, content: ArrayBuffer): void;
8
16
  putModule(filename: string, content: ArrayBuffer): Promise<void>;
9
17
  complete(code: string): Promise<[string[], number] | []>;
18
+ /**
19
+ * Is the source ready to be evaluated or want we more? It wants more
20
+ * when it does not parse because it ends too early (e.g. an unclosed
21
+ * block): as Python's codeop.
22
+ */
10
23
  more(code: string): Promise<boolean>;
11
24
  }
package/lib/worker.js CHANGED
@@ -1,5 +1,41 @@
1
+ import { parse } from "acorn";
1
2
  import objectInspect from "object-inspect";
2
3
  import { KernelWorkerBase } from "@basthon/kernel-base/worker";
4
+ const PARSE_OPTIONS = { ecmaVersion: "latest", sourceType: "script" };
5
+ /**
6
+ * Code where the top-level declarations (let, const and class) are global
7
+ * variables (var), as in a browser's console: they persist from an
8
+ * evaluation to the next (the code is evaluated by an indirect eval where
9
+ * they would be local to the evaluation). Unchanged if it does not parse
10
+ * (the evaluation reports the error).
11
+ */
12
+ const persistentDeclarations = (code) => {
13
+ let program;
14
+ try {
15
+ program = parse(code, PARSE_OPTIONS);
16
+ }
17
+ catch {
18
+ return code;
19
+ }
20
+ // [start, end, replacement] of the changed parts (in order)
21
+ const changes = [];
22
+ for (const node of program.body) {
23
+ if (node.type === "VariableDeclaration" && node.kind !== "var") {
24
+ const start = node.start;
25
+ changes.push([start, start + node.kind.length, "var"]);
26
+ }
27
+ else if (node.type === "ClassDeclaration" && node.id != null) {
28
+ // class A {} -> var A = class A {};
29
+ const start = node.start;
30
+ changes.push([start, start, `var ${node.id.name} = `]);
31
+ changes.push([node.end, node.end, ";"]);
32
+ }
33
+ }
34
+ let result = code;
35
+ for (const [start, end, replacement] of changes.reverse())
36
+ result = result.slice(0, start) + replacement + result.slice(end);
37
+ return result;
38
+ };
3
39
  export class JavaScriptKernelWorker extends KernelWorkerBase {
4
40
  constructor(options) {
5
41
  // do not forget to call the parent constructor
@@ -23,11 +59,24 @@ export class JavaScriptKernelWorker extends KernelWorkerBase {
23
59
  console.error = (...args) => {
24
60
  this.sendStderrStream(data, args.join(" ") + "\n");
25
61
  };
26
- const result = await globalThis.eval(code); // if a promise is returned, wait for it
62
+ // if a promise is returned, wait for it
63
+ const result = await globalThis.eval(persistentDeclarations(code));
27
64
  return typeof result === "undefined"
28
65
  ? undefined
29
66
  : { "text/plain": objectInspect(result) };
30
67
  }
68
+ /**
69
+ * Display a canvas created with basthon.createCanvas(width, height) (to
70
+ * be called by the user's code as basthon.displayLiveCanvas(canvas)).
71
+ * The canvas is live: its drawings are displayed and it receives mouse
72
+ * events (keyboard ones on canvas.ownerDocument), as a canvas of a page
73
+ * (see kernel-base's canvas/worker/live-canvas.ts).
74
+ */
75
+ displayLiveCanvas(canvas) {
76
+ const promise = super.displayLiveCanvas(canvas);
77
+ this.addEvalPromise(promise);
78
+ return promise;
79
+ }
31
80
  putFile(filename, content) {
32
81
  console.error(`File ${filename} not added since putFile has no mean in the JS context.`);
33
82
  }
@@ -51,7 +100,18 @@ export class JavaScriptKernelWorker extends KernelWorkerBase {
51
100
  const matches = vars.filter((v) => v.startsWith(word));
52
101
  return [matches, code.length - word.length];
53
102
  }
103
+ /**
104
+ * Is the source ready to be evaluated or want we more? It wants more
105
+ * when it does not parse because it ends too early (e.g. an unclosed
106
+ * block): as Python's codeop.
107
+ */
54
108
  async more(code) {
55
- return false;
109
+ try {
110
+ parse(code, PARSE_OPTIONS);
111
+ return false;
112
+ }
113
+ catch (error) {
114
+ return error instanceof SyntaxError && error.pos >= code.length;
115
+ }
56
116
  }
57
117
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@basthon/kernel-javascript",
3
- "version": "0.84.0",
3
+ "version": "0.85.0",
4
4
  "description": "Basthon - JavaScript Kernel",
5
5
  "homepage": "https://basthon.fr",
6
6
  "bugs": {
@@ -25,7 +25,8 @@
25
25
  "clean": "rimraf lib/"
26
26
  },
27
27
  "dependencies": {
28
- "@basthon/kernel-base": "0.84.0",
28
+ "@basthon/kernel-base": "0.85.0",
29
+ "acorn": "^8.14.0",
29
30
  "comlink": "^4.4.2",
30
31
  "object-inspect": "^1.13.4"
31
32
  },