numbl 0.4.13 → 0.4.15

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/README.md CHANGED
@@ -50,10 +50,6 @@ Full documentation is available at **[numbl.org/docs](https://numbl.org/docs)**,
50
50
 
51
51
  The [Numbl extension for VS Code](https://marketplace.visualstudio.com/items?itemName=jmagland.numbl) provides inline error diagnostics and a built-in figure viewer.
52
52
 
53
- ## Embedding
54
-
55
- Numbl scripts can be embedded in HTML and Markdown pages. See the [numbl-embed-example](https://magland.github.io/numbl-embed-example/) for usage and a [live demo](https://magland.github.io/numbl-embed-example/example1).
56
-
57
53
  ## Authors
58
54
 
59
55
  Jeremy Magland and Dan Fortunato, Center for Computational Mathematics, Flatiron Institute.
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * numbl/browser — managed in-browser sessions.
3
3
  *
4
- * Usage:
4
+ * Run-a-script usage:
5
5
  * import { createNumblSession } from "numbl/browser";
6
6
  * const session = await createNumblSession({
7
7
  * files: [{ path: "main.m", content: "mip load --install chebfun;\n..." }],
@@ -10,12 +10,21 @@
10
10
  * });
11
11
  * session.dispatchHtmlEvent(compId, "eventName", payload);
12
12
  *
13
+ * REPL usage (persistent workspace across calls):
14
+ * const session = await createNumblSession({ onOutput: console.log });
15
+ * await session.execute("x = linspace(0, 2*pi, 100);");
16
+ * const { plotInstructions } = await session.execute("plot(x, sin(x))");
17
+ * // render plotInstructions with figuresReducer/FigureView from numbl/graphics
18
+ *
13
19
  * The session runs in a worker numbl manages (inlined at build time — no
14
20
  * bundler worker support needed), with mip bootstrapped from its GitHub
15
21
  * release and installed packages persisted in IndexedDB across page loads.
16
22
  *
17
- * Not available in sessions: delaunay/convhull (qhull WASM is not loaded)
18
- * and non-uihtml figures (plot instructions other than uihtml are ignored).
23
+ * Figures are not rendered by the session itself: plot instructions are
24
+ * returned/surfaced for the host to render (numbl/graphics provides a React
25
+ * renderer). Not available in sessions: delaunay/convhull (qhull WASM is not
26
+ * loaded).
19
27
  */
20
28
  export { createNumblSession, type NumblSession, type NumblSessionOptions, } from "./session.js";
21
- export type { BootFile, UihtmlComponent } from "./protocol.js";
29
+ export type { BootFile, ExecuteResult, UihtmlComponent } from "./protocol.js";
30
+ export type { PlotInstruction } from "../graphics/types.js";
@@ -1,4 +1,5 @@
1
1
  /** Messages between the NumblSession host and its worker. */
2
+ import type { PlotInstruction } from "../graphics/types.js";
2
3
  export interface BootFile {
3
4
  path: string;
4
5
  content: string | Uint8Array;
@@ -6,13 +7,22 @@ export interface BootFile {
6
7
  export interface BootMessage {
7
8
  type: "boot";
8
9
  files: BootFile[];
9
- mainFile: string;
10
+ /** Script to run at boot. When omitted, the session boots idle and code is
11
+ * run incrementally via `execute`. */
12
+ mainFile?: string;
10
13
  mip: boolean;
11
14
  persistSystem: boolean;
12
15
  systemInactivityMs: number;
13
16
  optimization: "0" | "1";
14
17
  maxIterations: number;
15
18
  displayResults: boolean;
19
+ /**
20
+ * Shared cancellation flag (Int32[0] != 0 ⇒ cancel the running code). The
21
+ * worker keeps this reference and passes it to every executeCode call, so
22
+ * the host can interrupt a runaway run cooperatively. Only present when the
23
+ * page is cross-origin isolated (SharedArrayBuffer available).
24
+ */
25
+ cancelSAB?: SharedArrayBuffer;
16
26
  }
17
27
  export type ToWorker = BootMessage | {
18
28
  type: "writeFile";
@@ -22,6 +32,10 @@ export type ToWorker = BootMessage | {
22
32
  type: "readFile";
23
33
  id: number;
24
34
  path: string;
35
+ } | {
36
+ type: "execute";
37
+ id: number;
38
+ code: string;
25
39
  } | {
26
40
  type: "dispatch";
27
41
  id: number;
@@ -31,8 +45,26 @@ export type ToWorker = BootMessage | {
31
45
  };
32
46
  export interface UihtmlComponent {
33
47
  compId: string;
48
+ /** The component's HTML markup (render with buildUihtmlSrcDoc). */
49
+ html: string;
50
+ /** The component's Data, JSON-encoded. */
34
51
  dataJson: string;
35
52
  }
53
+ export interface ExecuteResult {
54
+ ok: boolean;
55
+ /** Concatenated console output (also streamed live via onOutput). */
56
+ output: string;
57
+ /** Every plot instruction the execution produced, uihtml included. */
58
+ plotInstructions: PlotInstruction[];
59
+ /** Formatted error message when `ok` is false. */
60
+ error?: string;
61
+ /**
62
+ * True when the run ended because it was interrupted via
63
+ * `NumblSession.interrupt()` (rather than finishing or erroring). The
64
+ * workspace is left at its pre-run state, so variables survive the abort.
65
+ */
66
+ aborted?: boolean;
67
+ }
36
68
  export type FromWorker = {
37
69
  type: "progress";
38
70
  message: string;
@@ -42,6 +74,7 @@ export type FromWorker = {
42
74
  } | {
43
75
  type: "uihtml";
44
76
  compId: string;
77
+ html: string;
45
78
  dataJson: string;
46
79
  } | {
47
80
  type: "ready";
@@ -55,6 +88,10 @@ export type FromWorker = {
55
88
  compId: string;
56
89
  name: string;
57
90
  dataJson: string;
91
+ } | {
92
+ type: "executeResult";
93
+ id: number;
94
+ result: ExecuteResult;
58
95
  } | {
59
96
  type: "dispatchResult";
60
97
  id: number;
@@ -1,9 +1,10 @@
1
- import type { BootFile, UihtmlComponent } from "./protocol.js";
1
+ import type { BootFile, ExecuteResult, UihtmlComponent } from "./protocol.js";
2
2
  export interface NumblSessionOptions {
3
- /** Project files. Relative paths land under /project/. */
4
- files: BootFile[];
5
- /** The script to run, as one of the paths in `files`. */
6
- mainFile: string;
3
+ /** Project files. Relative paths land under /project/. Default none. */
4
+ files?: BootFile[];
5
+ /** The script to run at boot, as one of the paths in `files`. When omitted
6
+ * the session boots idle; run code incrementally with `execute`. */
7
+ mainFile?: string;
7
8
  /**
8
9
  * Bootstrap the mip package manager (fetched from its GitHub release)
9
10
  * so the script can `mip load --install <pkg>`. Default true.
@@ -26,8 +27,8 @@ export interface NumblSessionOptions {
26
27
  onOutput?: (text: string) => void;
27
28
  /** Boot progress (package downloads, engine start). */
28
29
  onProgress?: (message: string) => void;
29
- /** A uihtml component was created/updated (its Data JSON-encoded). */
30
- onUihtml?: (compId: string, dataJson: string) => void;
30
+ /** A uihtml component was created/updated (Data JSON-encoded + markup). */
31
+ onUihtml?: (compId: string, dataJson: string, html: string) => void;
31
32
  /** Script -> host events (MATLAB `sendEventToHTMLSource`). */
32
33
  onHtmlSourceEvent?: (compId: string, name: string, dataJson: string) => void;
33
34
  }
@@ -36,6 +37,14 @@ export interface NumblSession {
36
37
  readonly uihtmlComponents: readonly UihtmlComponent[];
37
38
  /** True if the run left a live uihtml session (events can be dispatched). */
38
39
  readonly hasUihtmlSession: boolean;
40
+ /**
41
+ * Execute code against the session's persistent workspace (REPL semantics:
42
+ * variables persist across calls, expression results are auto-displayed).
43
+ * Resolves with the run's output and plot instructions; a numbl error
44
+ * resolves with `ok: false` and a formatted `error` (the promise rejects
45
+ * only on session-level failures). Calls run sequentially in the worker.
46
+ */
47
+ execute(code: string): Promise<ExecuteResult>;
39
48
  /** Write a file into the session VFS (visible to later dispatched events). */
40
49
  writeFile(path: string, content: string | Uint8Array): void;
41
50
  /**
@@ -48,13 +57,28 @@ export interface NumblSession {
48
57
  * the callback returns; rejects if it errors (the session stays usable).
49
58
  */
50
59
  dispatchHtmlEvent(compId: string, name: string, data: unknown): Promise<void>;
60
+ /**
61
+ * Cooperatively interrupt the currently running `execute` (or boot script):
62
+ * sets a shared cancel flag the worker polls at loop iterations and function
63
+ * calls, so it throws and the pending `execute` resolves with
64
+ * `aborted: true`. The worker and the persistent workspace survive — only
65
+ * the running command is abandoned. No-op when `canInterrupt` is false.
66
+ */
67
+ interrupt(): void;
68
+ /**
69
+ * Whether `interrupt()` can actually stop a run. True only when the page is
70
+ * cross-origin isolated (SharedArrayBuffer available). When false, a runaway
71
+ * run can only be stopped by disposing the session.
72
+ */
73
+ readonly canInterrupt: boolean;
51
74
  /** Terminate the worker. The session is unusable afterwards. */
52
75
  dispose(): void;
53
76
  }
54
77
  /**
55
78
  * Boot a numbl session in a dedicated worker: restore the persisted /system
56
- * directory, bootstrap mip, run `mainFile`, and keep the uihtml session live.
57
- * Resolves once the main script has finished (including any
58
- * `mip load --install` downloads it performs).
79
+ * directory, bootstrap mip, run `mainFile` when given, and keep the uihtml
80
+ * session live. Resolves once boot (and the main script, if any) has
81
+ * finished, including any `mip load --install` downloads it performs.
82
+ * Without a `mainFile` the session boots idle — run code with `execute`.
59
83
  */
60
84
  export declare function createNumblSession(options: NumblSessionOptions): Promise<NumblSession>;