kernel-base/worker
The base of the kernels running in a worker (@basthon/kernel-base/worker): KernelMainBase on the main thread side, KernelWorkerBase in the worker, connected by expose (see "Writing a new kernel" in the contributors' documentation).
Classes
DOMNodeBus
Objects (e.g. HTML elements) exchanged by id between the worker's code and the main thread in legacy mode, where they share the same JS context but messages are serialized.
Extends
Map<number,any>
Constructors
Constructor
new DOMNodeBus(entries?): DOMNodeBus;Parameters
| Parameter | Type |
|---|---|
entries? | readonly (readonly [number, any])[] | null |
Returns
Inherited from
Map<number, any>.constructor
Constructor
new DOMNodeBus(iterable?): DOMNodeBus;Parameters
| Parameter | Type |
|---|---|
iterable? | Iterable<readonly [number, any], any, any> | null |
Returns
Inherited from
Map<number, any>.constructor
Methods
pop()
pop(id): any;Take an object out of the bus.
Parameters
| Parameter | Type | Description |
|---|---|---|
id | number | The object's id (see push). |
Returns
any
The object.
push()
push(obj): number;Put an object in the bus.
Parameters
| Parameter | Type | Description |
|---|---|---|
obj | any | The object. |
Returns
number
Its id, to get it back with pop.
KernelMainBase
The main thread side of the kernels that run in a worker (all of them): it starts the kernel's worker (an opaque origin worker, see SandboxedWorker, with a proxy worker to fetch Basthon's files), talks to it through comlink and a message port, and turns its messages into events. It also answers the worker's synchronous requests (inputs, kernel iframes' messages) through a SharedArrayBuffer or the ephemeral API, and opens the camera and the audio contexts it asks for.
Each language extends it (kernel.ts of its package) and implements at least language, languageName, moduleExts and newWorker.
Extends
Type Parameters
| Type Parameter | Description |
|---|---|
Type extends KernelWorkerBase | The class of the kernel's worker. |
Constructors
Constructor
new KernelMainBase<Type>(options): KernelMainBase<Type>;Parameters
| Parameter | Type | Description |
|---|---|---|
options | KernelOptions | The kernel's options. |
Returns
KernelMainBase<Type>
Overrides
Properties
| Property | Modifier | Type | Default value | Description | Inherited from |
|---|---|---|---|---|---|
_execution_count | protected | number | 0 | - | KernelBase._execution_count |
_files | protected | Map<string, ArrayBuffer> | ... | - | KernelBase._files |
_modules | protected | Map<string, ArrayBuffer> | ... | - | KernelBase._modules |
_options | protected | KernelOptions | undefined | - | KernelBase._options |
_pendingInput? | protected | PromiseDelegate<InputAnswer> | undefined | - | KernelBase._pendingInput |
_runningData | protected | any | {} | - | KernelBase._runningData |
beforeEval | public | (data) => Promise<void> | ... | Called by execute before each evaluation, that waits for it. Frontends set it to do what has to be done first (e.g. a backup of the code). | KernelBase.beforeEval |
confirmCamera | public | (data) => Promise<boolean> | ... | Ask the user to let the running code use the camera, at each request (on top of the browser's permission, granted once for all the code run on the page). Frontends set their own dialog, window.confirm is the default. | KernelBase.confirmCamera |
Accessors
execution_count
Get Signature
get execution_count(): number;Number of evaluations since the kernel started (reset at each restart), as Jupyter's execution count.
Returns
number
Inherited from
isReady
Get Signature
get isReady(): boolean;Is the kernel started and ready to run code?
Returns
boolean
Inherited from
remote
Get Signature
get protected remote(): Remote<Type> | undefined;The worker, through comlink (undefined when stopped).
Returns
Remote<Type> | undefined
Methods
_init()
protected _init(): Promise<void>;The kernel's own initialization, run once by init (to be overridden).
Returns
Promise<void>
Inherited from
_start()
_start(): Promise<void>;Start the kernel: set up the worker, the proxy worker, the synchronous communication (shared buffer, or the ephemeral API's keys) and initialize the worker.
Returns
Promise<void>
Overrides
_stop()
_stop(): Promise<void>;Stop the kernel: terminate the worker, stop the cameras and the audio contexts it opened.
Returns
Promise<void>
Overrides
addEventListener()
Call Signature
addEventListener<K>(eventName, callback): void;Listen to an event of this kernel (see the events of KernelBase).
Type Parameters
| Type Parameter |
|---|
K extends keyof KernelEvents |
Parameters
| Parameter | Type | Description |
|---|---|---|
eventName | K | The event's name (e.g. "eval.output"). |
callback | (data) => void | Called with the event's data. |
Returns
void
Inherited from
Call Signature
addEventListener(eventName, callback): void;Listen to an event of this kernel (see the events of KernelBase).
Parameters
| Parameter | Type | Description |
|---|---|---|
eventName | string | The event's name (e.g. "eval.output"). |
callback | (_) => void | Called with the event's data. |
Returns
void
Inherited from
assetsURL()
assetsURL(absolute?): string;URL of the assets directory (see KernelOptions.rootPath).
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
absolute? | boolean | false | Return an absolute URL. |
Returns
string
Inherited from
banner()
banner(): string;The message displayed by consoles at startup.
Returns
string
Inherited from
basthonRoot()
basthonRoot(absolute?): string;URL of the kernel's files: <assets>/<version>/<language>.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
absolute? | boolean | false | Return an absolute URL. |
Returns
string
Inherited from
cancelPendingInput()
cancelPendingInput(): void;Cancel a pending input on the kernel side (e.g. the kernel gave up waiting for the answer): the frontend is notified by an eval.input-cancel event (with the eval.input data) to close its input field.
Returns
void
Inherited from
clone()
clone(obj): any;Copy a JSON-serializable object.
Parameters
| Parameter | Type | Description |
|---|---|---|
obj | any | The object to copy. |
Returns
any
Its copy.
Inherited from
complete()
complete(code): Promise<[string[], number] | []>;Completions of the code at its end, by the worker (see KernelBase.complete).
Parameters
| Parameter | Type | Description |
|---|---|---|
code | string | The code before the cursor. |
Returns
Promise<[string[], number] | []>
[completions, start], or [] if there is none.
Overrides
dispatchEvent()
protected dispatchEvent(eventName, data): void;Dispatch an event of this kernel (see the events of KernelBase).
Parameters
| Parameter | Type | Description |
|---|---|---|
eventName | string | The event's name (e.g. "eval.output"). |
data | any | The event's data. |
Returns
void
Inherited from
download()
download(data, filename): void;Download a file (the browser saves it, or asks where to).
Parameters
| Parameter | Type | Description |
|---|---|---|
data | string | Uint8Array<ArrayBuffer> | The file's content: bytes or a data URL. |
filename | string | The name of the saved file. |
Returns
void
Inherited from
endPendingInput()
endPendingInput(): void;End a pending input: there is nothing more to read (EOFError in Python), e.g. the program's stdin is closed.
Returns
void
Inherited from
evalAsync()
evalAsync(
code,
outCallback,
errCallback,
data?
): Promise<any>;Run code in the worker (see KernelBase.evalAsync).
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
code | string | undefined | The code to run. |
outCallback | (_) => void | undefined | Unused: outputs come as worker's messages. |
errCallback | (_) => void | undefined | Unused: outputs come as worker's messages. |
data? | any | null | The evaluation's data. |
Returns
Promise<any>
[result, executionCount].
Throws
A KernelError (already displayed) when the code fails.
Overrides
execute()
execute(data): Promise<void>;Run code in this kernel and dispatch the evaluation's events: eval.output while it runs, then eval.finished or eval.error.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | EvalRequest | The request's data, with the code to run (its other fields are copied to the evaluation's events). |
Returns
Promise<void>
Resolved at the end of the evaluation, even if it failed (see eval.error).
Inherited from
getFile()
getFile(path): Promise<Uint8Array<ArrayBufferLike>>;Read a file of the kernel's (virtual) filesystem.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | The file's path (relative to the working directory or absolute). |
Returns
Promise<Uint8Array<ArrayBufferLike>>
The file's content.
Inherited from
getUserModuleFile()
getUserModuleFile(filename): Promise<Uint8Array<ArrayBufferLike>>;Read a module installed by putModule.
Parameters
| Parameter | Type | Description |
|---|---|---|
filename | string | The module's file name. |
Returns
Promise<Uint8Array<ArrayBufferLike>>
The module's content.
Inherited from
importLegacyWorker()
protected importLegacyWorker(): Promise<void>;Import the worker's code in the main thread (legacy kernel). Only the Python kernel has a legacy mode, the other ones throw an error.
Returns
Promise<void>
init()
init(): Promise<void>;Initialize the kernel (once) and start it. Wait for ready before running code.
Returns
Promise<void>
Throws
The initialization's error (the kernel can't be used).
Inherited from
inputAsync()
inputAsync(
prompt,
password?,
data?
): Promise<InputAnswer>;Ask the frontend for an input: dispatch an eval.input event and wait for its answer (given to the event's resolve).
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
prompt | string | null | undefined | undefined | The text displayed before the input. |
password? | boolean | false | Hide the typed text. |
data? | any | undefined | The evaluation's data (sent with the event). |
Returns
Promise<InputAnswer>
The answer (see InputAnswer).
Inherited from
interruptPendingInput()
interruptPendingInput(): void;Interrupt a pending input: the running program stops (KeyboardInterrupt in Python) instead of getting an answer.
Returns
void
Inherited from
KernelBase.interruptPendingInput
isLegacy()
isLegacy(): boolean;Does the kernel run in the main thread (legacy kernel)?
Returns
boolean
language()
language(): string;The kernel's language, as used in URLs and file paths (lower case, e.g. "python3", "ocaml").
Returns
string
Inherited from
languageName()
languageName(): string;The kernel's language as displayed to the user (e.g. "Python 3").
Returns
string
Inherited from
moduleExts()
moduleExts(): string[];Extensions of the files installed as modules by putRessource (e.g. ["py"]), without the dot.
Returns
string[]
Inherited from
more()
more(code): Promise<boolean>;Does the code need more lines (see KernelBase.more), by the worker?
Parameters
| Parameter | Type | Description |
|---|---|---|
code | string | The lines typed so far. |
Returns
Promise<boolean>
true if the code is incomplete.
Overrides
newWorker()
protected newWorker(): Worker;Create the kernel's worker, with the sandboxed: true option (an opaque origin worker). To be overridden.
Returns
Worker
The worker.
pendingInput()
pendingInput(): boolean;Is the running code waiting for an input?
Returns
boolean
Inherited from
postIFrameMessage()
postIFrameMessage(key, message): void;Relay a kernel iframe's message to the worker waiting for it (see waitIFrameMessage in the worker), e.g. the user's answer to a tkinter dialog: the worker can't read the iframe's port while it waits. Iframes may be untrusted: only keys made for them are accepted, and big messages are ignored.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The message's key. |
message | string | The message. |
Returns
void
Overrides
processWorkerMessage()
protected processWorkerMessage(msg): Promise<void>;Process a message of the worker: outputs, displays, inputs, downloads, validation logs, camera and audio requests...
Parameters
| Parameter | Type | Description |
|---|---|---|
msg | { content: any; data: any; type: string; } | The message: its type, its content and the evaluation's data. |
msg.content | any | - |
msg.data | any | - |
msg.type | string | - |
Returns
Promise<void>
ps1()
ps1(): string;The console's prompt for a new instruction.
Returns
string
Inherited from
ps2()
ps2(): string;The console's prompt for a continuation line (see more).
Returns
string
Inherited from
putFile()
putFile(filename, content): Promise<void>;Put a file in the worker's filesystem (see KernelBase.putFile).
Parameters
| Parameter | Type | Description |
|---|---|---|
filename | string | The file's name. |
content | ArrayBuffer | The file's content. |
Returns
Promise<void>
Overrides
putModule()
putModule(filename, content): Promise<void>;Install a module in the worker (see KernelBase.putModule).
Parameters
| Parameter | Type | Description |
|---|---|---|
filename | string | The module's file name. |
content | ArrayBuffer | The module's content. |
Returns
Promise<void>
Overrides
putRessource()
putRessource(filename, content): Promise<void>;Put a file as a module (putModule) if its extension is one of moduleExts, as an auxiliary file (putFile) otherwise.
Parameters
| Parameter | Type | Description |
|---|---|---|
filename | string | The file's name. |
content | ArrayBuffer | The file's content. |
Returns
Promise<void>
Inherited from
ready()
ready(): Promise<void>;Wait for the kernel to be ready (started).
Returns
Promise<void>
Throws
The error of the kernel's initialization or start.
Inherited from
removeEventListener()
Call Signature
removeEventListener<K>(eventName, callback): void;Stop listening to an event of this kernel.
Type Parameters
| Type Parameter |
|---|
K extends keyof KernelEvents |
Parameters
| Parameter | Type | Description |
|---|---|---|
eventName | K | The event's name. |
callback | (data) => void | The callback given to addEventListener. |
Returns
void
Inherited from
KernelBase.removeEventListener
Call Signature
removeEventListener(eventName, callback): void;Stop listening to an event of this kernel.
Parameters
| Parameter | Type | Description |
|---|---|---|
eventName | string | The event's name. |
callback | (_) => void | The callback given to addEventListener. |
Returns
void
Inherited from
KernelBase.removeEventListener
resolvePendingInput()
resolvePendingInput(): void;Answer a pending input with an empty text.
Returns
void
Inherited from
KernelBase.resolvePendingInput
restart()
restart(): Promise<void>;Restart the kernel (stop then start): the user's variables are lost, the files and modules are kept.
Returns
Promise<void>
Inherited from
safeKernel()
safeKernel(): boolean;Does the kernel run in an isolated worker (not legacy)?
Returns
boolean
Overrides
setRunningData()
setRunningData(data): Promise<void>;Set the running data (see KernelBase.setRunningData), also in the worker.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | any | The data to add. |
Returns
Promise<void>
Overrides
setupWorker()
protected setupWorker(): Promise<void>;Create the worker (or import its code in legacy mode) and set up the comlink and message channels with it.
Returns
Promise<void>
start()
start(): Promise<void>;Start the kernel (after init or stop): reset the execution count and put the files and modules again.
Returns
Promise<void>
Inherited from
stop()
stop(): void;Stop the kernel: it is no longer ready once the call returns (the end of its stop is awaited by the next start).
Returns
void
Inherited from
syncCommSupport()
syncCommSupport(): boolean;Can the worker wait synchronously for the main thread (the page is cross-origin isolated, so a SharedArrayBuffer is shared)? Otherwise, the ephemeral API is used.
Returns
boolean
userModules()
userModules(): Promise<string[]>;The modules installed by putModule.
Returns
Promise<string[]>
Their file names.
Inherited from
version()
version(): string;The kernel's version (Basthon's version, e.g. "0.87.0").
Returns
string
Inherited from
workerOptions()
protected workerOptions(): any;The options given to the worker's constructor (the kernel's options by default).
Returns
any
KernelWorkerBase
The worker side of a kernel: runs the user's code with the language's runtime and talks to the main thread (KernelMainBase) through comlink (calls such as eval) and a message port (outputs, displays, inputs, downloads...). Each language extends it (worker.ts of its package) and implements at least language, _eval, putFile, putModule and more.
In legacy mode, it runs in the main thread (same API).
Constructors
Constructor
new KernelWorkerBase(options?): KernelWorkerBase;Parameters
| Parameter | Type | Description |
|---|---|---|
options? | any | The kernel's options (see KernelMainBase.workerOptions): legacy runs in the main thread, the other ones are the language's. |
Returns
Properties
| Property | Modifier | Type |
|---|---|---|
__eval_data__ | protected | any |
_legacy | protected readonly | boolean |
_options | protected | any |
postMessage? | protected | { (message, transfer): void; (message, options?): void; } |
Methods
_eval()
protected _eval(data, code): Promise<
| {
[key: string]: string;
}
| undefined>;Run code with the language's runtime. To be overridden.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | any | The evaluation's data. |
code | string | The code to run. |
Returns
Promise< | { [key: string]: string; } | undefined>
The result as a MIME bundle, or undefined if the code has no value.
Throws
The code's error (a KernelError already displayed if the kernel printed it).
_init()
protected _init(): Promise<void>;The kernel's own initialization (loading the runtime...), run once by init. To be overridden.
Returns
Promise<void>
addEvalPromise()
protected addEvalPromise(promise): void;Make the current evaluation wait for a promise before ending (e.g. a display that needs an asynchronous conversion).
Parameters
| Parameter | Type | Description |
|---|---|---|
promise | Promise<void> | The promise to wait for. |
Returns
void
basthonRoot()
basthonRoot(): string;URL of the kernel's files: <assets>/<version>/<language>.
Returns
string
blobToDataURL()
protected blobToDataURL(blob): Promise<string>;Convert a blob to a data URL.
Parameters
| Parameter | Type | Description |
|---|---|---|
blob | Blob | The blob. |
Returns
Promise<string>
The data URL.
clearOutput()
clearOutput(wait): void;Clear the current output (an eval.clear-output event).
Parameters
| Parameter | Type | Description |
|---|---|---|
wait | boolean | Clear only when a new output arrives (no flickering). |
Returns
void
clone()
protected clone(data): any;Copy a JSON-serializable object.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | any | The object. |
Returns
any
Its copy.
complete()
complete(code): Promise<[string[], number] | []>;Completions of the code at its end.
Parameters
| Parameter | Type | Description |
|---|---|---|
code | string | The code before the cursor. |
Returns
Promise<[string[], number] | []>
[completions, start], or [] if there is none.
createCanvas()
createCanvas(width?, height?): OffscreenCanvas | HTMLCanvasElement;Create a canvas (an OffscreenCanvas in a worker, an HTMLCanvasElement in legacy mode), live as soon as created so that event listeners can be set before it is displayed (see canvas/worker/live-canvas.ts).
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
width? | number | 0 | The canvas' width. |
height? | number | 0 | The canvas' height. |
Returns
OffscreenCanvas | HTMLCanvasElement
The canvas.
display()
display(element): void;Display something in the frontend (an eval.display event): a canvas (as an image), a MIME bundle ({"text/html": ...}), or an HTML element in legacy mode.
Parameters
| Parameter | Type | Description |
|---|---|---|
element | any | What to display. |
Returns
void
displayBlob()
protected displayBlob(blob, data): Promise<void>;Display a PNG image given as a blob (other types are ignored).
Parameters
| Parameter | Type | Description |
|---|---|---|
blob | Blob | The image. |
data | any | The evaluation's data (the current one if null). |
Returns
Promise<void>
displayCanvas()
protected displayCanvas(canvas, data): Promise<void>;Display a canvas as a still image (its current pixels).
Parameters
| Parameter | Type | Description |
|---|---|---|
canvas | OffscreenCanvas | The canvas. |
data | any | The evaluation's data (the current one if null). |
Returns
Promise<void>
displayKernelIFrame()
displayKernelIFrame(iframeHTML, fallback?): MessagePort;Display a kernel iframe: send its page and one end of a new message channel to the frontend, which renders the page in a sandboxed iframe and transfers the port to it.
The channel is a spare one, whose other end the main thread received while the worker was idle: Firefox (128 and 140 at least, not 158) holds the messages of a port transferred while the worker is busy (e.g. a figure updated by plt.pause in a loop) until the worker's task ends. A new channel is used when there is no spare one left.
Parameters
| Parameter | Type | Description |
|---|---|---|
iframeHTML | string | The iframe's page. |
fallback? | Record<string, string> | A MIME bundle displayed when the iframe can't be (e.g. a notebook opened from a file). |
Returns
MessagePort
The kernel's end of the channel, to talk to the iframe.
displayLiveCanvas()
protected displayLiveCanvas(canvas): Promise<void>;Display a canvas in a canvas kernel iframe, where it is updated and receives the mouse and keyboard events (a PNG image of it is the fallback).
Parameters
| Parameter | Type | Description |
|---|---|---|
canvas | OffscreenCanvas | HTMLCanvasElement | The canvas (see createCanvas). |
Returns
Promise<void>
download()
download(content, filename): void;Download a file (the main thread only accepts bytes or a data URL).
Parameters
| Parameter | Type | Description |
|---|---|---|
content | string | Uint8Array<ArrayBufferLike> | The file's content. |
filename | string | The name of the saved file. |
Returns
void
downloadSVGAsPNG()
downloadSVGAsPNG(
svg,
filename,
scale?
): void;Download an SVG image as a PNG one, converted by the main thread (workers can't decode SVG images).
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
svg | string | undefined | The SVG image. |
filename | string | undefined | The name of the saved file. |
scale? | number | 1 | The scale of the PNG image (1 for the SVG's size). |
Returns
void
eval()
eval(data, code): Promise<ExecuteResult>;Run code (called by the main thread through comlink): _eval, then wait for the promises added during the evaluation.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | any | The evaluation's data (given to KernelBase.execute). |
code | string | The code to run. |
Returns
Promise<ExecuteResult>
The result or the error (also printed on stderr).
getRunningData()
getRunningData(): any;The running data (see setRunningData).
Returns
any
iframeMessageKey()
iframeMessageKey(): string;A new key for a message of a kernel iframe while the program waits for it (see waitIFrameMessage).
Returns
string
init()
init(): Promise<void>;Initialize the kernel (called by the main thread once the worker is set up).
Returns
Promise<void>
Throws
The initialization's error.
input()
input(prompt, password?): string;Ask the user for an input and wait for the answer (synchronously): through the shared buffer, the browser's prompt in legacy mode, or the ephemeral API.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
prompt | string | null | undefined | undefined | The text displayed before the input. |
password? | boolean | false | Hide the typed text. |
Returns
string
The answer.
Throws
InputInterruptedError if the input is interrupted, InputEOFError at the end of the input.
isLegacy()
isLegacy(): boolean;Does the worker run in the main thread (legacy kernel)?
Returns
boolean
language()
language(): string;The kernel's language, as used in URLs and file paths (lower case, e.g. "python3"). To be overridden.
Returns
string
legacyStart()
legacyStart(): Promise<void>;Start again a legacy kernel (its restart is lazy: the runtime is kept). To be overridden by the kernels with a legacy mode.
Returns
Promise<void>
legacyStop()
legacyStop(): Promise<void>;Stop a legacy kernel (see legacyStart).
Returns
Promise<void>
logValidation()
logValidation(data): void;Log a self-assessment validation (a validation.log event), with the current date if data has none.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | any | The validation's log. |
Returns
void
more()
more(code): Promise<boolean>;Does the code need more lines to be complete (consoles then ask for a continuation line)? To be overridden.
Parameters
| Parameter | Type | Description |
|---|---|---|
code | string | The lines typed so far. |
Returns
Promise<boolean>
true if the code is incomplete.
openAudio()
openAudio(options?): MessagePort;Ask the main thread to open an audio context, where the audio graph built in the worker is mirrored (see installWebAudio in audio/worker.ts).
Parameters
| Parameter | Type | Description |
|---|---|---|
options? | AudioContextOptions | The audio context's options. |
Returns
MessagePort
The port of the mirroring.
openCamera()
openCamera(constraints?): MessagePort;Ask the main thread to open the camera (after the user's confirmation) (see CameraStream.open in camera/worker.ts).
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
constraints? | CameraConstraints | true | The video constraints of getUserMedia. |
Returns
MessagePort
The port where its frames are sent.
popDOMNode()
popDOMNode(id): any;Take an HTML element displayed in legacy mode (see display): the main thread gets it from its id.
Parameters
| Parameter | Type | Description |
|---|---|---|
id | number | The element's id in the bus. |
Returns
any
The element.
processMainThreadMessage()
protected processMainThreadMessage(msg): Promise<void>;Process a message of the main thread.
Parameters
| Parameter | Type | Description |
|---|---|---|
msg | { content: any; data: any; type: string; } | The message (type and content). |
msg.content | any | - |
msg.data | any | - |
msg.type | string | - |
Returns
Promise<void>
putFile()
putFile(filename, content): void;Put a file in the kernel's (virtual) filesystem, in its working directory. To be overridden.
Parameters
| Parameter | Type | Description |
|---|---|---|
filename | string | The file's name. |
content | ArrayBuffer | The file's content. |
Returns
void
putModule()
putModule(filename, content): void;Install a module for the user's code (and load its dependencies). To be overridden.
Parameters
| Parameter | Type | Description |
|---|---|---|
filename | string | The module's file name. |
content | ArrayBuffer | The module's content. |
Returns
void
ready()
ready(): Promise<void>;Wait for the kernel to be initialized.
Returns
Promise<void>
sendStderrStream()
protected sendStderrStream(data, text): void;Print text on stderr (an eval.output event).
Parameters
| Parameter | Type | Description |
|---|---|---|
data | any | The evaluation's data. |
text | string | The text. |
Returns
void
sendStdoutStream()
protected sendStdoutStream(data, text): void;Print text on stdout (an eval.output event).
Parameters
| Parameter | Type | Description |
|---|---|---|
data | any | The evaluation's data. |
text | string | The text. |
Returns
void
setCommPort()
setCommPort(port): void;Set the port of the messages exchanged with the main thread (outputs, displays, inputs...).
Parameters
| Parameter | Type | Description |
|---|---|---|
port | MessagePort | The worker's end of the channel. |
Returns
void
setEphemeralAPI()
setEphemeralAPI(url): Promise<void>;Set the URL of the ephemeral API.
Parameters
| Parameter | Type | Description |
|---|---|---|
url | string | The URL. |
Returns
Promise<void>
setFallbackKeyAndPass()
setFallbackKeyAndPass(key, pass): Promise<void>;Set the base key and the encryption key of the ephemeral API's answers (when SharedArrayBuffer is not available). Each input gets its own key (base key and a counter) so that a late answer can't reach a later input.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The base key (random, new at each start). |
pass | Uint8Array | The AES-GCM key (256 bits) the answers are encrypted with. |
Returns
Promise<void>
setProxyPort()
setProxyPort(port): void;Set the port of the proxy worker that fetches Basthon's files for this opaque origin worker (see ProxyFetch).
Parameters
| Parameter | Type | Description |
|---|---|---|
port | MessagePort | A comlink endpoint of the proxy. |
Returns
void
setRunningData()
setRunningData(data): Promise<void>;Set the running data (see KernelBase.setRunningData): replaced, not merged (the main thread merges them).
Parameters
| Parameter | Type | Description |
|---|---|---|
data | any | The data. |
Returns
Promise<void>
setSAB()
setSAB(sab): void;Set the buffer shared with the main thread for the synchronous communication (inputs, sleep): its first 4 bytes are a flag, the rest the answer.
Parameters
| Parameter | Type | Description |
|---|---|---|
sab | SharedArrayBuffer | The shared buffer. |
Returns
void
sleep()
sleep(duration): void;Pause the program (an Atomics.wait with a shared buffer, an active wait otherwise).
Parameters
| Parameter | Type | Description |
|---|---|---|
duration | number | The duration in seconds. |
Returns
void
syncCommSupport()
syncCommSupport(): boolean;Can the worker wait synchronously for the main thread (a SharedArrayBuffer was given, see setSAB)? Otherwise, inputs go through the ephemeral API.
Returns
boolean
toplevelInput()
toplevelInput(
prompt,
password?,
data?
): Promise<string>;Ask the user for an input and wait for the answer asynchronously (for the code run at the top level, which can be awaited: no synchronous communication needed).
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
prompt | string | null | undefined | undefined | The text displayed before the input. |
password? | boolean | false | Hide the typed text. |
data? | any | undefined | The evaluation's data. |
Returns
Promise<string>
The answer.
Throws
InputInterruptedError if the input is interrupted, InputEOFError at the end of the input.
version()
version(): string;The kernel's version (the same as the main thread's version()).
Returns
string
waitIFrameMessage()
waitIFrameMessage(key): string | null;Wait (synchronously) for the message that a kernel iframe posts with this key through the frontend (see KernelBase.postIFrameMessage): the iframe can't reach the worker while the program waits, e.g. for the answer of a tkinter dialog.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The message's key (see iframeMessageKey). |
Returns
string | null
The message, or null when nothing came for a while (the caller waits again).
Throws
InputInterruptedError if the wait is interrupted.
Type Aliases
ExecuteResult
type ExecuteResult = {
data: any;
error?: {
name: string;
traceback?: string;
value: string;
};
result?: any;
status: "ok" | "error";
};Properties
data
data: any;error?
error?: {
name: string;
traceback?: string;
value: string;
};name
name: string;traceback?
traceback?: string;value
value: string;result?
result?: any;status
status: "ok" | "error";Functions
expose()
function expose(kwc): void;Expose the kernel worker class to the main thread (see WorkerHost).
Parameters
| Parameter | Type | Description |
|---|---|---|
kwc | any | The kernel worker class. |
Returns
void
loadScript()
function loadScript(url): Promise<void>;Load and run a (classic) script (see WorkerHost).
Parameters
| Parameter | Type | Description |
|---|---|---|
url | string | The script's URL. |
Returns
Promise<void>
References
DownloadError
Re-exports DownloadError
KernelError
Re-exports KernelError