@basthon/kernel-javascript 0.62.21 → 0.62.22

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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import { expose } from "comlink";
2
+ import { JavaScriptKernelWorker } from "./worker";
3
+ expose(JavaScriptKernelWorker);
package/lib/index.d.ts CHANGED
@@ -1 +1 @@
1
- export * from './kernel';
1
+ export * from "./kernel";
package/lib/index.js CHANGED
@@ -1 +1 @@
1
- export * from './kernel';
1
+ export * from "./kernel";
package/lib/kernel.d.ts CHANGED
@@ -1,18 +1,11 @@
1
- import { KernelBase } from '@basthon/kernel-base';
2
- export declare class KernelJavaScript extends KernelBase {
3
- private _context;
1
+ import { KernelMainBase } from "@basthon/kernel-base/worker";
2
+ import { JavaScriptKernelWorker } from "./worker";
3
+ export declare class JavaScriptKernel extends KernelMainBase<JavaScriptKernelWorker> {
4
4
  constructor(options: any);
5
+ protected newWorker(): Worker;
5
6
  language(): string;
6
7
  languageName(): string;
7
8
  moduleExts(): string[];
8
- launch(): Promise<void>;
9
- eval(code: string): any;
10
- evalAsync(code: string, outCallback: (_: string) => void, errCallback: (_: string) => void, data?: any): Promise<any>;
11
9
  ps1(): string;
12
10
  ps2(): string;
13
- restart(): void;
14
- more(source: string): boolean;
15
- complete(code: string): [string[], number] | [];
16
- putFile(filename: string, content: ArrayBuffer): Promise<void>;
17
- putModule(filename: string, content: ArrayBuffer): Promise<void>;
18
11
  }
package/lib/kernel.js CHANGED
@@ -1,86 +1,28 @@
1
- import { KernelBase } from '@basthon/kernel-base';
2
- export class KernelJavaScript extends KernelBase {
1
+ import { KernelMainBase } from "@basthon/kernel-base/worker";
2
+ export class JavaScriptKernel extends KernelMainBase {
3
3
  constructor(options) {
4
4
  super(options);
5
- this._context = {
6
- Basthon: undefined,
7
- Jupyter: undefined,
8
- window: undefined,
9
- };
10
- this._execution_count = 0;
11
5
  }
12
- language() { return "javascript"; }
13
- ;
14
- languageName() { return "Javascript"; }
15
- ;
16
- moduleExts() { return ['js']; }
17
- ;
18
- async launch() { }
19
- ;
20
- eval(code) {
21
- try {
22
- // using context to hide several global variables
23
- return window.eval(`with(Basthon._context) { ${code} }`);
24
- }
25
- catch (e) {
26
- if (e instanceof SyntaxError)
27
- return (new Function(`with(this) { ${code} }`)).call(this._context);
28
- throw e;
29
- }
6
+ newWorker() {
7
+ return new Worker(new URL("./comlink-worker.js", import.meta.url), {
8
+ type: "module",
9
+ //@ts-ignore
10
+ sandboxed: true, // removing this line will cause security issues
11
+ });
30
12
  }
31
- ;
32
- async evalAsync(code, outCallback, errCallback, data = null) {
33
- // force interactivity in all modes
34
- data.interactive = true;
35
- // backup
36
- const console_log = console.log;
37
- const console_error = console.error;
38
- console.log = outCallback;
39
- console.error = errCallback;
40
- this._execution_count++;
41
- // evaluation
42
- let result = undefined;
43
- try {
44
- result = this.eval(code);
45
- }
46
- catch (e) {
47
- console.error(`Uncaught ${e.name || ""}: ${e === null || e === void 0 ? void 0 : e.message}`);
48
- }
49
- // restoration
50
- console.log = console_log;
51
- console.error = console_error;
52
- // return result
53
- if (typeof result !== 'undefined')
54
- result = { 'text/plain': JSON.stringify(result) };
55
- return [result, this._execution_count];
13
+ language() {
14
+ return "javascript";
56
15
  }
57
- ;
58
- ps1() { return " js> "; }
59
- ;
60
- ps2() { return "...> "; }
61
- ;
62
- restart() { this._execution_count = 0; }
63
- ;
64
- more(source) { return false; }
65
- ;
66
- complete(code) { return []; }
67
- ;
68
- async putFile(filename, content) {
69
- console.error(`Fichier ${filename} not added since putFile has no mean in the JS context.`);
16
+ languageName() {
17
+ return "Javascript";
70
18
  }
71
- ;
72
- async putModule(filename, content) {
73
- content = new Uint8Array(content);
74
- const ext = filename.split('.').pop();
75
- switch (ext) {
76
- case 'js':
77
- let decoder = new TextDecoder("utf-8");
78
- const _content = decoder.decode(content);
79
- this.eval(_content);
80
- break;
81
- default:
82
- throw { message: "Only '.js' files supported." };
83
- }
19
+ moduleExts() {
20
+ return ["js"];
21
+ }
22
+ ps1() {
23
+ return " js> ";
24
+ }
25
+ ps2() {
26
+ return "...> ";
84
27
  }
85
- ;
86
28
  }
@@ -0,0 +1,17 @@
1
+ import { KernelWorkerBase } from "@basthon/kernel-base/worker";
2
+ declare global {
3
+ interface DedicatedWorkerGlobalScope {
4
+ jsoo_runtime?: any;
5
+ window?: any;
6
+ Canvas: typeof OffscreenCanvas;
7
+ }
8
+ }
9
+ export declare class JavaScriptKernelWorker extends KernelWorkerBase {
10
+ constructor(options?: any);
11
+ protected _init(): Promise<void>;
12
+ protected _eval(data: any, code: string): Promise<any>;
13
+ putFile(filename: string, content: ArrayBuffer): void;
14
+ putModule(filename: string, content: ArrayBuffer): Promise<void>;
15
+ complete(code: string): Promise<[string[], number] | []>;
16
+ more(code: string): Promise<boolean>;
17
+ }
package/lib/worker.js ADDED
@@ -0,0 +1,58 @@
1
+ import objectInspect from "object-inspect";
2
+ import { KernelWorkerBase } from "@basthon/kernel-base/worker";
3
+ export class JavaScriptKernelWorker extends KernelWorkerBase {
4
+ constructor(options) {
5
+ // do not forget to call the parent constructor
6
+ super(options);
7
+ // the window object is not available in a webworker...
8
+ self.window = self;
9
+ self.Canvas = globalThis.OffscreenCanvas;
10
+ }
11
+ /*
12
+ * Initialize the kernel.
13
+ */
14
+ async _init() {
15
+ console.info = (...args) => console.log(...args);
16
+ console.warn = (...args) => console.error(...args);
17
+ globalThis.addEventListener("error", (e) => console.error(e.toString()));
18
+ }
19
+ async _eval(data, code) {
20
+ console.log = (...args) => {
21
+ this.sendStdoutStream(data, args.join(" ") + "\n");
22
+ };
23
+ console.error = (...args) => {
24
+ this.sendStderrStream(data, args.join(" ") + "\n");
25
+ };
26
+ const result = await globalThis.eval(code); // if a promise is returned, wait for it
27
+ return typeof result === "undefined"
28
+ ? undefined
29
+ : { "text/plain": objectInspect(result) };
30
+ }
31
+ putFile(filename, content) {
32
+ console.error(`Fichier ${filename} not added since putFile has no mean in the JS context.`);
33
+ }
34
+ async putModule(filename, content) {
35
+ content = new Uint8Array(content);
36
+ const ext = filename.split(".").pop();
37
+ switch (ext) {
38
+ case "js":
39
+ let decoder = new TextDecoder("utf-8");
40
+ const _content = decoder.decode(content);
41
+ await globalThis.eval(_content);
42
+ break;
43
+ default:
44
+ throw { message: "Only '.js' files supported." };
45
+ }
46
+ }
47
+ async complete(code) {
48
+ // basic completion based on global object
49
+ const vars = Object.getOwnPropertyNames(self);
50
+ const words = code.match(/(\w+)$/) ?? [];
51
+ const word = words[0] ?? "";
52
+ const matches = vars.filter((v) => v.startsWith(word));
53
+ return [matches, code.length - word.length];
54
+ }
55
+ async more(code) {
56
+ return false;
57
+ }
58
+ }
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@basthon/kernel-javascript",
3
- "version": "0.62.21",
3
+ "version": "0.62.22",
4
4
  "description": "Basthon - JavaScript Kernel",
5
5
  "homepage": "https://basthon.fr",
6
6
  "bugs": {
7
- "url": "https://framagit.org/basthon/basthon-kernel/issues"
7
+ "url": "https://forge.apps.education.fr/basthon/basthon-kernel/issues"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://framagit.org/basthon/basthon-kernel.git"
11
+ "url": "https://forge.apps.education.fr/basthon/basthon-kernel.git"
12
12
  },
13
13
  "license": "GPL-3.0-or-later",
14
14
  "author": "Romain Casati",
@@ -21,19 +21,20 @@
21
21
  "lib"
22
22
  ],
23
23
  "scripts": {
24
- "build": "gulp all",
24
+ "build": "tsc",
25
25
  "clean": "rm -rf lib/"
26
26
  },
27
27
  "dependencies": {
28
- "@basthon/kernel-base": "0.62.21"
28
+ "@basthon/kernel-base": "0.62.22",
29
+ "comlink": "^4.4.1",
30
+ "object-inspect": "^1.13.1"
29
31
  },
30
32
  "devDependencies": {
31
- "gulp": "^4.0.2",
32
- "gulp-typescript": "^6.0.0-alpha.1",
33
- "typescript": "~4.4.4"
33
+ "@types/object-inspect": "^1.13.0",
34
+ "typescript": "^5.4.5"
34
35
  },
35
36
  "publishConfig": {
36
37
  "access": "public"
37
38
  },
38
- "gitHead": "59343558e9ae17fbb08eda1e81a36b182e572d05"
39
+ "gitHead": "1dd6225a7caf14cbee203687cac611495f5b18d4"
39
40
  }