@opencode/codemode 0.0.0-dev-884 → 0.0.0-dev-900
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 +3 -0
- package/dist/codemode.d.ts +2 -0
- package/dist/codemode.js +10 -4
- package/dist/vm/async.d.ts +78 -0
- package/dist/vm/async.js +335 -0
- package/dist/vm/code.d.ts +122 -0
- package/dist/vm/code.js +407 -0
- package/dist/vm/compiler/compile.d.ts +13 -0
- package/dist/vm/compiler/compile.js +1477 -0
- package/dist/vm/compiler/scope.d.ts +74 -0
- package/dist/vm/compiler/scope.js +515 -0
- package/dist/vm/data.d.ts +7 -0
- package/dist/vm/data.js +13 -0
- package/dist/vm/generators.d.ts +42 -0
- package/dist/vm/generators.js +413 -0
- package/dist/vm/host.d.ts +103 -0
- package/dist/vm/host.js +226 -0
- package/dist/vm/index.d.ts +3 -0
- package/dist/vm/index.js +3 -0
- package/dist/vm/internal-prelude.d.ts +45 -0
- package/dist/vm/internal-prelude.js +534 -0
- package/dist/vm/opcodes.d.ts +222 -0
- package/dist/vm/opcodes.js +235 -0
- package/dist/vm/prelude/bytecode.d.ts +18 -0
- package/dist/vm/prelude/bytecode.js +31 -0
- package/dist/vm/prelude/bytecode.json +1 -0
- package/dist/vm/prelude/helpers.d.ts +8 -0
- package/dist/vm/prelude/helpers.js +258 -0
- package/dist/vm/prelude/prelude.d.ts +21 -0
- package/dist/vm/prelude/prelude.js +53 -0
- package/dist/vm/prelude/source.d.ts +1 -0
- package/dist/vm/prelude/source.js +652 -0
- package/dist/vm/runtime/callback.d.ts +25 -0
- package/dist/vm/runtime/callback.js +51 -0
- package/dist/vm/runtime/driver.d.ts +41 -0
- package/dist/vm/runtime/driver.js +142 -0
- package/dist/vm/runtime/errors.d.ts +23 -0
- package/dist/vm/runtime/errors.js +169 -0
- package/dist/vm/runtime/execute.d.ts +22 -0
- package/dist/vm/runtime/execute.js +278 -0
- package/dist/vm/runtime/extensions.d.ts +16 -0
- package/dist/vm/runtime/extensions.js +270 -0
- package/dist/vm/runtime/generators.d.ts +6 -0
- package/dist/vm/runtime/generators.js +44 -0
- package/dist/vm/runtime/globals.d.ts +6 -0
- package/dist/vm/runtime/globals.js +137 -0
- package/dist/vm/runtime/interpreter.d.ts +50 -0
- package/dist/vm/runtime/interpreter.js +94 -0
- package/dist/vm/runtime/intrinsic-names.d.ts +56 -0
- package/dist/vm/runtime/intrinsic-names.js +166 -0
- package/dist/vm/runtime/intrinsics.d.ts +15 -0
- package/dist/vm/runtime/intrinsics.js +111 -0
- package/dist/vm/runtime/limits.d.ts +10 -0
- package/dist/vm/runtime/limits.js +19 -0
- package/dist/vm/runtime/model.d.ts +58 -0
- package/dist/vm/runtime/model.js +57 -0
- package/dist/vm/runtime/modules.d.ts +4 -0
- package/dist/vm/runtime/modules.js +33 -0
- package/dist/vm/runtime/native.d.ts +19 -0
- package/dist/vm/runtime/native.js +41 -0
- package/dist/vm/runtime/objects.d.ts +403 -0
- package/dist/vm/runtime/objects.js +784 -0
- package/dist/vm/runtime/operations.d.ts +49 -0
- package/dist/vm/runtime/operations.js +288 -0
- package/dist/vm/runtime/promises.d.ts +55 -0
- package/dist/vm/runtime/promises.js +232 -0
- package/dist/vm/runtime/references.d.ts +8 -0
- package/dist/vm/runtime/references.js +51 -0
- package/dist/vm/runtime/signals.d.ts +69 -0
- package/dist/vm/runtime/signals.js +68 -0
- package/dist/vm/runtime/source.d.ts +29 -0
- package/dist/vm/runtime/source.js +63 -0
- package/dist/vm/runtime/table.d.ts +43 -0
- package/dist/vm/runtime/table.js +132 -0
- package/dist/vm/scheduler.d.ts +178 -0
- package/dist/vm/scheduler.js +63 -0
- package/dist/vm/snapshot/codec.d.ts +88 -0
- package/dist/vm/snapshot/codec.js +752 -0
- package/dist/vm/snapshot/snapshot.d.ts +39 -0
- package/dist/vm/snapshot/snapshot.js +69 -0
- package/dist/vm/snapshot.d.ts +29 -0
- package/dist/vm/snapshot.js +304 -0
- package/dist/vm/stdlib/array.d.ts +16 -0
- package/dist/vm/stdlib/array.js +311 -0
- package/dist/vm/stdlib/bytes.d.ts +11 -0
- package/dist/vm/stdlib/bytes.js +222 -0
- package/dist/vm/stdlib/collections.d.ts +7 -0
- package/dist/vm/stdlib/collections.js +188 -0
- package/dist/vm/stdlib/console.d.ts +6 -0
- package/dist/vm/stdlib/console.js +93 -0
- package/dist/vm/stdlib/date.d.ts +2 -0
- package/dist/vm/stdlib/date.js +123 -0
- package/dist/vm/stdlib/headers.d.ts +2 -0
- package/dist/vm/stdlib/headers.js +92 -0
- package/dist/vm/stdlib/iterator.d.ts +2 -0
- package/dist/vm/stdlib/iterator.js +30 -0
- package/dist/vm/stdlib/json.d.ts +3 -0
- package/dist/vm/stdlib/json.js +42 -0
- package/dist/vm/stdlib/math.d.ts +3 -0
- package/dist/vm/stdlib/math.js +73 -0
- package/dist/vm/stdlib/number.d.ts +3 -0
- package/dist/vm/stdlib/number.js +72 -0
- package/dist/vm/stdlib/object.d.ts +6 -0
- package/dist/vm/stdlib/object.js +201 -0
- package/dist/vm/stdlib/regexp.d.ts +8 -0
- package/dist/vm/stdlib/regexp.js +119 -0
- package/dist/vm/stdlib/string.d.ts +2 -0
- package/dist/vm/stdlib/string.js +272 -0
- package/dist/vm/stdlib/url.d.ts +25 -0
- package/dist/vm/stdlib/url.js +255 -0
- package/dist/vm/stdlib/value.d.ts +6 -0
- package/dist/vm/stdlib/value.js +32 -0
- package/dist/vm/stdlib/web.d.ts +5 -0
- package/dist/vm/stdlib/web.js +97 -0
- package/dist/vm/vm.d.ts +224 -0
- package/dist/vm/vm.js +2921 -0
- package/package.json +5 -2
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Node } from "acorn";
|
|
2
|
+
import type { ErrorType } from "./intrinsics.js";
|
|
3
|
+
import type { DiagnosticKind } from "../../codemode.js";
|
|
4
|
+
import type { ErrorObj, Value } from "./objects.js";
|
|
5
|
+
/** Any parsed node; the compiler narrows on `type`, and diagnostics read `loc`. */
|
|
6
|
+
export type AstNode = Node;
|
|
7
|
+
/** A global binding: globals are immutable per run, and top-level declarations are the program's own locals. */
|
|
8
|
+
export type Binding = {
|
|
9
|
+
readonly value: Value;
|
|
10
|
+
};
|
|
11
|
+
export type GeneratorRequestKind = "next" | "return" | "throw";
|
|
12
|
+
export declare const AsyncIteratorSymbol: unique symbol;
|
|
13
|
+
export declare const IteratorSymbol: unique symbol;
|
|
14
|
+
export declare class Throw {
|
|
15
|
+
readonly value: Value;
|
|
16
|
+
constructor(value: Value);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A failure raised by the interpreter or a built-in. It travels as a defect and becomes one program Error object
|
|
20
|
+
* the first time a handler observes it, so every observer of the same failure sees the same value.
|
|
21
|
+
*/
|
|
22
|
+
export declare class PendingThrow {
|
|
23
|
+
/** The JS error class a program sees when it catches this failure. */
|
|
24
|
+
readonly type: ErrorType;
|
|
25
|
+
readonly message: string;
|
|
26
|
+
readonly kind: DiagnosticKind;
|
|
27
|
+
readonly suggestions?: ReadonlyArray<string> | undefined;
|
|
28
|
+
node?: AstNode;
|
|
29
|
+
value?: ErrorObj;
|
|
30
|
+
constructor(
|
|
31
|
+
/** The JS error class a program sees when it catches this failure. */
|
|
32
|
+
type: ErrorType, message: string, node?: AstNode, kind?: DiagnosticKind, suggestions?: ReadonlyArray<string> | undefined);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A host value a restored run cannot have back: a function an extension returned, or what a host function returned,
|
|
36
|
+
* which have no data form. Using it fails with `lostError`.
|
|
37
|
+
*/
|
|
38
|
+
export declare class Lost {
|
|
39
|
+
readonly description: string;
|
|
40
|
+
constructor(description: string);
|
|
41
|
+
}
|
|
42
|
+
/** What using a value lost to a suspend throws. */
|
|
43
|
+
export declare const lostError: (description: string, node?: AstNode) => PendingThrow;
|
|
44
|
+
export declare const typeError: (message: string, node?: AstNode) => PendingThrow;
|
|
45
|
+
export declare const invalidData: (message: string, node?: AstNode) => PendingThrow;
|
|
46
|
+
export declare const rangeError: (message: string, node?: AstNode) => PendingThrow;
|
|
47
|
+
export declare const referenceError: (message: string, node?: AstNode) => PendingThrow;
|
|
48
|
+
export declare const syntaxError: (message: string, node?: AstNode) => PendingThrow;
|
|
49
|
+
export declare const uriError: (message: string, node?: AstNode) => PendingThrow;
|
|
50
|
+
export declare const supportedSyntaxMessage = "This is a restricted JavaScript-like language. Supported: plain and async functions, data literals, destructuring, standard control flow, await and Promise, and built-ins such as Array, Object, Math, JSON, Date, RegExp, Map, Set, and URL. Unsupported: classes, getters/setters, BigInt, and custom Symbols. Use plain functions and data objects instead.";
|
|
51
|
+
export declare const sourceLocation: (node: AstNode) => {
|
|
52
|
+
readonly line: number;
|
|
53
|
+
readonly column: number;
|
|
54
|
+
};
|
|
55
|
+
export declare const formatLocation: (location?: {
|
|
56
|
+
readonly line: number;
|
|
57
|
+
readonly column: number;
|
|
58
|
+
}) => string;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export const AsyncIteratorSymbol = Symbol("codemode.async-iterator");
|
|
2
|
+
export const IteratorSymbol = Symbol("codemode.iterator");
|
|
3
|
+
export class Throw {
|
|
4
|
+
value;
|
|
5
|
+
constructor(value) {
|
|
6
|
+
this.value = value;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A failure raised by the interpreter or a built-in. It travels as a defect and becomes one program Error object
|
|
11
|
+
* the first time a handler observes it, so every observer of the same failure sees the same value.
|
|
12
|
+
*/
|
|
13
|
+
export class PendingThrow {
|
|
14
|
+
type;
|
|
15
|
+
message;
|
|
16
|
+
kind;
|
|
17
|
+
suggestions;
|
|
18
|
+
node;
|
|
19
|
+
value;
|
|
20
|
+
constructor(
|
|
21
|
+
/** The JS error class a program sees when it catches this failure. */
|
|
22
|
+
type, message, node, kind = "ExecutionFailure", suggestions) {
|
|
23
|
+
this.type = type;
|
|
24
|
+
this.message = message;
|
|
25
|
+
this.kind = kind;
|
|
26
|
+
this.suggestions = suggestions;
|
|
27
|
+
if (node)
|
|
28
|
+
this.node = node;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* A host value a restored run cannot have back: a function an extension returned, or what a host function returned,
|
|
33
|
+
* which have no data form. Using it fails with `lostError`.
|
|
34
|
+
*/
|
|
35
|
+
export class Lost {
|
|
36
|
+
description;
|
|
37
|
+
constructor(description) {
|
|
38
|
+
this.description = description;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** What using a value lost to a suspend throws. */
|
|
42
|
+
export const lostError = (description, node) => new PendingThrow("TypeError", `${description} came from the host and was lost when the run was suspended; get it again after resuming.`, node);
|
|
43
|
+
const failure = (type, kind) => (message, node) => new PendingThrow(type, message, node, kind);
|
|
44
|
+
export const typeError = failure("TypeError");
|
|
45
|
+
export const invalidData = failure("TypeError", "InvalidDataValue");
|
|
46
|
+
export const rangeError = failure("RangeError");
|
|
47
|
+
export const referenceError = failure("ReferenceError");
|
|
48
|
+
export const syntaxError = failure("SyntaxError");
|
|
49
|
+
export const uriError = failure("URIError");
|
|
50
|
+
// Orient the agent rather than enumerate JavaScript; interpreter-support.md is the full matrix.
|
|
51
|
+
export const supportedSyntaxMessage = "This is a restricted JavaScript-like language. Supported: plain and async functions, data literals, destructuring, standard control flow, await and Promise, and built-ins such as Array, Object, Math, JSON, Date, RegExp, Map, Set, and URL. Unsupported: classes, getters/setters, BigInt, and custom Symbols. Use plain functions and data objects instead.";
|
|
52
|
+
// Acorn lines are 1-based and its columns are 0-based. Diagnostics use 1-based columns of the submitted source.
|
|
53
|
+
export const sourceLocation = (node) => ({
|
|
54
|
+
line: node.loc?.start.line ?? 1,
|
|
55
|
+
column: (node.loc?.start.column ?? 0) + 1,
|
|
56
|
+
});
|
|
57
|
+
export const formatLocation = (location) => location ? ` (line ${location.line}, col ${location.column})` : "";
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { define, get, Obj } from "./objects.js";
|
|
2
|
+
/**
|
|
3
|
+
* What `import` and `require` load: a global, by the Node module name that provides it. `fs` loads the `fs` global
|
|
4
|
+
* and `crypto` the `crypto` global; the names below come from globals with different names. A module that would be
|
|
5
|
+
* empty isn't available.
|
|
6
|
+
*/
|
|
7
|
+
const aliases = {
|
|
8
|
+
"fs/promises": (global) => {
|
|
9
|
+
const fs = global("fs");
|
|
10
|
+
return fs instanceof Obj ? get(fs, "promises") : undefined;
|
|
11
|
+
},
|
|
12
|
+
url: (global) => pick(global, ["URL", "URLSearchParams"]),
|
|
13
|
+
util: (global) => pick(global, ["TextEncoder", "TextDecoder"]),
|
|
14
|
+
buffer: (global) => pick(global, ["Buffer", "atob", "btoa"]),
|
|
15
|
+
};
|
|
16
|
+
/** `node:fs` names the same module as `fs`. */
|
|
17
|
+
export const moduleName = (specifier) => (specifier.startsWith("node:") ? specifier.slice(5) : specifier);
|
|
18
|
+
export const resolveModule = (specifier, global) => {
|
|
19
|
+
const name = moduleName(specifier);
|
|
20
|
+
return global(name) ?? aliases[name]?.(global);
|
|
21
|
+
};
|
|
22
|
+
function pick(global, names) {
|
|
23
|
+
const found = names.flatMap((name) => {
|
|
24
|
+
const value = global(name);
|
|
25
|
+
return value === undefined ? [] : [[name, value]];
|
|
26
|
+
});
|
|
27
|
+
if (found.length === 0)
|
|
28
|
+
return undefined;
|
|
29
|
+
const module = new Obj(null);
|
|
30
|
+
for (const [name, value] of found)
|
|
31
|
+
define(module, name, value);
|
|
32
|
+
return module;
|
|
33
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Builtins } from "./intrinsics.js";
|
|
2
|
+
import { type Callable, Native, type NativeCall, type NativeOptions, Obj, type PositionSource, type Value } from "./objects.js";
|
|
3
|
+
/** A native function body: a value, a thrown `PendingThrow`, or what the VM carries out as the call. */
|
|
4
|
+
export type Impl = NativeCall;
|
|
5
|
+
export declare const native: (builtins: Builtins, options: NativeOptions) => Native;
|
|
6
|
+
export declare const fn: (builtins: Builtins, name: string, length: number, impl: Impl) => Native;
|
|
7
|
+
export type Method = readonly [name: string, length: number, impl: Impl];
|
|
8
|
+
export declare const methods: (builtins: Builtins, target: Obj, table: ReadonlyArray<Method>) => void;
|
|
9
|
+
/** `keys`, `values`, and `entries`: built-in iterators over the receiver `self` checks. */
|
|
10
|
+
export declare const iteratorMethods: (builtins: Builtins, self: (thisValue: Value, name: string) => PositionSource) => Array<Method>;
|
|
11
|
+
export declare const constants: (target: Obj, table: Record<string, Value>) => void;
|
|
12
|
+
/** A constructor wired to its prototype: `C.prototype === proto` and `proto.constructor === C`. */
|
|
13
|
+
export declare const constructor: (builtins: Builtins, proto: Obj, options: NativeOptions) => Native;
|
|
14
|
+
/** The `call` of a constructor that JS requires to be invoked with `new`. */
|
|
15
|
+
export declare const requiresNew: (name: string) => () => never;
|
|
16
|
+
/** The instance prototype for `new` via `newTarget.prototype`, falling back to the built-in's own. */
|
|
17
|
+
export declare const prototypeFrom: (newTarget: Callable, fallback: Obj) => Obj;
|
|
18
|
+
/** Narrows a method receiver to the built-in it belongs to, or throws the TypeError JS would. */
|
|
19
|
+
export declare const receiver: <T extends Obj>(cls: abstract new (...args: never) => T, thisValue: Value, method: string) => T;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { typeError } from "./model.js";
|
|
2
|
+
import { define, frozen, hidden, Native, Obj, positionIterator, } from "./objects.js";
|
|
3
|
+
import { describeValue } from "./references.js";
|
|
4
|
+
export const native = (builtins, options) => new Native(builtins.Function, options);
|
|
5
|
+
export const fn = (builtins, name, length, impl) => native(builtins, { name, length, call: impl });
|
|
6
|
+
export const methods = (builtins, target, table) => {
|
|
7
|
+
for (const [name, length, impl] of table)
|
|
8
|
+
define(target, name, fn(builtins, name, length, impl), hidden);
|
|
9
|
+
};
|
|
10
|
+
/** `keys`, `values`, and `entries`: built-in iterators over the receiver `self` checks. */
|
|
11
|
+
export const iteratorMethods = (builtins, self) => ["keys", "values", "entries"].map((kind) => [
|
|
12
|
+
kind,
|
|
13
|
+
0,
|
|
14
|
+
(thisValue) => positionIterator(builtins, self(thisValue, kind), kind),
|
|
15
|
+
]);
|
|
16
|
+
export const constants = (target, table) => {
|
|
17
|
+
for (const [name, value] of Object.entries(table))
|
|
18
|
+
define(target, name, value, frozen);
|
|
19
|
+
};
|
|
20
|
+
/** A constructor wired to its prototype: `C.prototype === proto` and `proto.constructor === C`. */
|
|
21
|
+
export const constructor = (builtins, proto, options) => {
|
|
22
|
+
const ctor = native(builtins, options);
|
|
23
|
+
define(ctor, "prototype", proto, frozen);
|
|
24
|
+
define(proto, "constructor", ctor, hidden);
|
|
25
|
+
return ctor;
|
|
26
|
+
};
|
|
27
|
+
/** The `call` of a constructor that JS requires to be invoked with `new`. */
|
|
28
|
+
export const requiresNew = (name) => () => {
|
|
29
|
+
throw typeError(`Constructor ${name} requires 'new'.`);
|
|
30
|
+
};
|
|
31
|
+
/** The instance prototype for `new` via `newTarget.prototype`, falling back to the built-in's own. */
|
|
32
|
+
export const prototypeFrom = (newTarget, fallback) => {
|
|
33
|
+
const proto = newTarget.props.get("prototype");
|
|
34
|
+
return proto !== undefined && "value" in proto && proto.value instanceof Obj ? proto.value : fallback;
|
|
35
|
+
};
|
|
36
|
+
/** Narrows a method receiver to the built-in it belongs to, or throws the TypeError JS would. */
|
|
37
|
+
export const receiver = (cls, thisValue, method) => {
|
|
38
|
+
if (thisValue instanceof cls)
|
|
39
|
+
return thisValue;
|
|
40
|
+
throw typeError(`${method} called on incompatible receiver ${describeValue(thisValue)}.`);
|
|
41
|
+
};
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import { ToolReference } from "../../tool-runtime.js";
|
|
2
|
+
import type { Builtins } from "./intrinsics.js";
|
|
3
|
+
import { type AstNode, type PendingThrow } from "./model.js";
|
|
4
|
+
import { type Convert, type GeneratorRequest, Invoke, type Push, type Resume } from "./signals.js";
|
|
5
|
+
import type { Code } from "../code.js";
|
|
6
|
+
import { type Cell } from "../opcodes.js";
|
|
7
|
+
import type { Reaction, Resolving } from "../scheduler.js";
|
|
8
|
+
import type { Frame } from "../vm.js";
|
|
9
|
+
import { OrderedTable, type TableCursor } from "./table.js";
|
|
10
|
+
/** Property attributes, as in a JS property descriptor. */
|
|
11
|
+
export type Attributes = {
|
|
12
|
+
readonly writable: boolean;
|
|
13
|
+
readonly enumerable: boolean;
|
|
14
|
+
readonly configurable: boolean;
|
|
15
|
+
};
|
|
16
|
+
export type Getter = (receiver: Value) => Value;
|
|
17
|
+
export type Setter = (receiver: Value, value: Value) => void;
|
|
18
|
+
/** One own property: a data slot or a native accessor pair. */
|
|
19
|
+
export type Slot = {
|
|
20
|
+
value: Value;
|
|
21
|
+
writable: boolean;
|
|
22
|
+
enumerable: boolean;
|
|
23
|
+
configurable: boolean;
|
|
24
|
+
} | {
|
|
25
|
+
get: Getter | undefined;
|
|
26
|
+
set: Setter | undefined;
|
|
27
|
+
enumerable: boolean;
|
|
28
|
+
configurable: boolean;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Runs `make`, which makes a runtime's intrinsics, and returns every object made meanwhile, in creation order, after
|
|
32
|
+
* `first` (the built-in prototypes, made before). An object's index in the list is its static intrinsic id.
|
|
33
|
+
*/
|
|
34
|
+
export declare const makingIntrinsics: <A>(first: ReadonlyArray<Obj>, make: () => A) => readonly [A, Array<Obj>];
|
|
35
|
+
/** Marks an intrinsic as changed by the program, so saving a run records it; any other object is left alone. */
|
|
36
|
+
export declare const changed: (target: Obj) => void;
|
|
37
|
+
/** Ordinary assignment: writable, enumerable, configurable. */
|
|
38
|
+
export declare const data: Attributes;
|
|
39
|
+
/** Built-in methods and `constructor`: writable and configurable but hidden from enumeration. */
|
|
40
|
+
export declare const hidden: Attributes;
|
|
41
|
+
/** Function `name` and `length`: read-only but deletable. */
|
|
42
|
+
export declare const readonly: Attributes;
|
|
43
|
+
/** Constants such as `Math.PI` and a constructor's `prototype`. */
|
|
44
|
+
export declare const frozen: Attributes;
|
|
45
|
+
/**
|
|
46
|
+
* An object owned by the program: own properties plus a prototype link. Subclasses answer, in one place, how a
|
|
47
|
+
* built-in kind of object prints, coerces, iterates, and crosses to the host.
|
|
48
|
+
*/
|
|
49
|
+
export declare class Obj {
|
|
50
|
+
proto: Obj | null;
|
|
51
|
+
readonly props: Map<string | symbol, Slot>;
|
|
52
|
+
/** [[Extensible]]: cleared by `Object.preventExtensions`, `seal`, and `freeze`. */
|
|
53
|
+
extensible: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* 0 for an object the program made. For an intrinsic, its id plus one while it is as the runtime made it, negated
|
|
56
|
+
* once it changes (see `changed` and `IntrinsicNames`).
|
|
57
|
+
*/
|
|
58
|
+
intrinsic: number;
|
|
59
|
+
constructor(proto: Obj | null);
|
|
60
|
+
/** The class name `Object.prototype.toString` reports: `[object Map]`. */
|
|
61
|
+
readonly tag: string;
|
|
62
|
+
/** How diagnostics refer to a value of this kind. */
|
|
63
|
+
get describe(): string;
|
|
64
|
+
/** ToString without consulting program-defined methods. */
|
|
65
|
+
toString(): string;
|
|
66
|
+
/** ToPrimitive without consulting program-defined methods: only a Date answers a number hint differently. */
|
|
67
|
+
toPrimitive(hint: "default" | "number" | "string"): string | number;
|
|
68
|
+
/** ToNumber without consulting program-defined methods. */
|
|
69
|
+
toNumber(): number;
|
|
70
|
+
/** How `console.log` shows the value; `item` formats a child with cycle and depth tracking. */
|
|
71
|
+
inspect(item: (value: Value) => string): string;
|
|
72
|
+
/** A copy the host can hold; `item` converts a child. A `__proto__` key never reaches host code. */
|
|
73
|
+
toHost(item: (value: Value) => unknown): unknown;
|
|
74
|
+
/** The built-in iteration `for...of` and spread use, when this kind of object has one. */
|
|
75
|
+
iterator(builtins: Builtins): Cursor | undefined;
|
|
76
|
+
}
|
|
77
|
+
export declare class Arr extends Obj {
|
|
78
|
+
readonly items: Array<Value>;
|
|
79
|
+
readonly tag = "Array";
|
|
80
|
+
/** The attributes every live element shares; `seal` and `freeze` narrow them since elements have no slots. */
|
|
81
|
+
elements: Attributes;
|
|
82
|
+
constructor(proto: Obj, items?: Array<Value>);
|
|
83
|
+
get describe(): string;
|
|
84
|
+
toString(): string;
|
|
85
|
+
inspect(item: (value: Value) => string): string;
|
|
86
|
+
toHost(item: (value: Value) => unknown): unknown[];
|
|
87
|
+
iterator(builtins: Builtins): Position;
|
|
88
|
+
}
|
|
89
|
+
/** An object with the [[ErrorData]] slot: what `Error.prototype.toString` and the host boundary recognize as an error. */
|
|
90
|
+
export declare class ErrorObj extends Obj {
|
|
91
|
+
readonly tag = "Error";
|
|
92
|
+
/**
|
|
93
|
+
* The interpreter failure this error materialized from, so rethrowing it keeps the diagnostic kind and location. It
|
|
94
|
+
* is plain data (type, message, kind, suggestions, and the node it is located at), shared with the failure so a
|
|
95
|
+
* location attached later is seen here too.
|
|
96
|
+
*/
|
|
97
|
+
failure?: PendingThrow;
|
|
98
|
+
/** Error.prototype.toString: "name: message", or just one when the other is empty. */
|
|
99
|
+
toString(): string;
|
|
100
|
+
inspect(): string;
|
|
101
|
+
}
|
|
102
|
+
/** Interpreter machinery a program can hold but never inspect, serialize, or hand to the host. */
|
|
103
|
+
export declare abstract class Opaque extends Obj {
|
|
104
|
+
inspect(): string;
|
|
105
|
+
}
|
|
106
|
+
export declare abstract class Callable extends Opaque {
|
|
107
|
+
readonly length: number;
|
|
108
|
+
readonly tag = "Function";
|
|
109
|
+
constructor(proto: Obj, name: string, length: number);
|
|
110
|
+
get describe(): string;
|
|
111
|
+
}
|
|
112
|
+
/** A program or prelude function: its bytecode, and the cells of the bindings it captured. */
|
|
113
|
+
export declare class Fn extends Callable {
|
|
114
|
+
readonly code: Code;
|
|
115
|
+
readonly cells: ReadonlyArray<Cell>;
|
|
116
|
+
constructor(proto: Obj, code: Code, cells: ReadonlyArray<Cell>, name?: string);
|
|
117
|
+
get generator(): boolean;
|
|
118
|
+
}
|
|
119
|
+
/** The strict `arguments` object: an ordinary object with indexed own properties and a hidden `length`. */
|
|
120
|
+
export declare class Arguments extends Obj {
|
|
121
|
+
readonly tag = "Arguments";
|
|
122
|
+
constructor(proto: Obj, args: Array<Value>);
|
|
123
|
+
iterator(builtins: Builtins): Position;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* A native function body: a value, or what the VM carries out as the call: work it hands over by name (`Push`), a
|
|
127
|
+
* suspended generator to continue (`Resume`), a call it hands on (`Invoke`), or conversions to run before it runs again
|
|
128
|
+
* (`Convert`). A native never runs program code itself.
|
|
129
|
+
*/
|
|
130
|
+
export type NativeResult = Value | Push | Resume | Invoke | Convert;
|
|
131
|
+
/** `site` is the call running the built-in, where a failure it reports later (a rejection) is located. */
|
|
132
|
+
export type NativeCall = (thisValue: Value, args: Array<Value>, site: AstNode | undefined) => NativeResult;
|
|
133
|
+
export type NativeConstruct = (args: Array<Value>, newTarget: Callable, site: AstNode | undefined) => NativeResult;
|
|
134
|
+
export type NativeOptions = {
|
|
135
|
+
/**
|
|
136
|
+
* Why this native may be made while the program runs, for the few kinds that still capture state in a host closure.
|
|
137
|
+
* Every other native is made with the intrinsics; see `nativeRule`.
|
|
138
|
+
*/
|
|
139
|
+
readonly runtime?: RuntimeNative;
|
|
140
|
+
readonly name: string;
|
|
141
|
+
readonly length?: number;
|
|
142
|
+
readonly call: NativeCall;
|
|
143
|
+
/** `new name(...)`; without it the function is not a constructor. */
|
|
144
|
+
readonly construct?: NativeConstruct;
|
|
145
|
+
/** Whether callback sites (array methods, replacers, promise reactions) admit this function. Defaults to true. */
|
|
146
|
+
readonly callback?: boolean;
|
|
147
|
+
};
|
|
148
|
+
/** The natives still made at run time, each holding state in a host closure that cannot be saved yet. */
|
|
149
|
+
export type RuntimeNative = "extension";
|
|
150
|
+
/**
|
|
151
|
+
* Natives are made only with the intrinsics, so each one has an intrinsic name and run state never holds a host
|
|
152
|
+
* closure. When `check` is on (tests turn it on), making a native for a runtime whose intrinsics are `sealed` (keyed by
|
|
153
|
+
* its `Function.prototype`) throws, unless it declares one of the `RuntimeNative` exceptions.
|
|
154
|
+
*/
|
|
155
|
+
export declare const nativeRule: {
|
|
156
|
+
check: boolean;
|
|
157
|
+
sealed: WeakSet<Obj>;
|
|
158
|
+
observe?: (native: Native) => void;
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* A built-in function: a `Native` made with the intrinsics, or a data kind such as `BoundFn` or `Resolver` whose state
|
|
162
|
+
* is plain fields and whose behaviour lives on its class.
|
|
163
|
+
*/
|
|
164
|
+
export declare abstract class Builtin extends Callable {
|
|
165
|
+
/** `new name(...)`; without it the function is not a constructor. */
|
|
166
|
+
abstract readonly construct: NativeConstruct | undefined;
|
|
167
|
+
/** Whether callback sites (array methods, replacers, promise reactions) admit this function. */
|
|
168
|
+
abstract readonly callback: boolean;
|
|
169
|
+
abstract invoke(thisValue: Value, args: Array<Value>, site: AstNode | undefined): NativeResult;
|
|
170
|
+
}
|
|
171
|
+
export declare class Native extends Builtin {
|
|
172
|
+
readonly call: NativeCall;
|
|
173
|
+
readonly construct: NativeConstruct | undefined;
|
|
174
|
+
readonly callback: boolean;
|
|
175
|
+
constructor(proto: Obj, options: NativeOptions);
|
|
176
|
+
invoke(thisValue: Value, args: Array<Value>, site: AstNode | undefined): NativeResult;
|
|
177
|
+
}
|
|
178
|
+
/** `Function.prototype.bind`'s result: calls `target` with the bound receiver and leading arguments. */
|
|
179
|
+
export declare class BoundFn extends Builtin {
|
|
180
|
+
readonly target: Callable;
|
|
181
|
+
readonly boundThis: Value;
|
|
182
|
+
readonly boundArgs: ReadonlyArray<Value>;
|
|
183
|
+
readonly construct: undefined;
|
|
184
|
+
readonly callback = true;
|
|
185
|
+
constructor(proto: Obj, target: Callable, boundThis: Value, boundArgs: ReadonlyArray<Value>);
|
|
186
|
+
invoke(_: Value, args: Array<Value>, site: AstNode | undefined): NativeResult;
|
|
187
|
+
}
|
|
188
|
+
/** A `resolve` or `reject` function handed to an executor or thenable: settles its shared `Resolving` once. */
|
|
189
|
+
export declare class Resolver extends Builtin {
|
|
190
|
+
readonly resolving: Resolving;
|
|
191
|
+
readonly kind: "resolve" | "reject";
|
|
192
|
+
readonly construct: undefined;
|
|
193
|
+
readonly callback = true;
|
|
194
|
+
constructor(proto: Obj, name: string, resolving: Resolving, kind: "resolve" | "reject");
|
|
195
|
+
invoke(_: Value, args: Array<Value>): NativeResult;
|
|
196
|
+
}
|
|
197
|
+
export declare class PromiseObj extends Opaque {
|
|
198
|
+
/** Creation order, for reporting unhandled rejections in the order their promises were made. */
|
|
199
|
+
readonly id: number;
|
|
200
|
+
readonly tag = "Promise";
|
|
201
|
+
state: "pending" | "fulfilled" | "rejected";
|
|
202
|
+
/** The value, or the failure as thrown; a handler receives it materialized. */
|
|
203
|
+
result: unknown;
|
|
204
|
+
reactions: Array<Reaction>;
|
|
205
|
+
/** [[PromiseIsHandled]]: a reaction was attached, so a rejection is not reported as unhandled. */
|
|
206
|
+
handled: boolean;
|
|
207
|
+
constructor(proto: Obj,
|
|
208
|
+
/** Creation order, for reporting unhandled rejections in the order their promises were made. */
|
|
209
|
+
id: number);
|
|
210
|
+
get describe(): string;
|
|
211
|
+
inspect(): string;
|
|
212
|
+
}
|
|
213
|
+
export type GeneratorState = "suspendedStart" | "suspendedYield" | "executing" | "awaiting-return" | "draining" | "completed";
|
|
214
|
+
export declare class GeneratorObj extends Opaque {
|
|
215
|
+
readonly asynchronous: boolean;
|
|
216
|
+
readonly tag = "Generator";
|
|
217
|
+
state: GeneratorState;
|
|
218
|
+
/** The suspended body's frame, kept while it waits for the next request, which puts it back on the job stack. */
|
|
219
|
+
frame: Frame | undefined;
|
|
220
|
+
/** An async generator's requests, oldest first, each with what settles its promise. */
|
|
221
|
+
readonly queue: Array<{
|
|
222
|
+
readonly request: GeneratorRequest;
|
|
223
|
+
readonly resolving: Resolving;
|
|
224
|
+
}>;
|
|
225
|
+
constructor(proto: Obj, asynchronous: boolean);
|
|
226
|
+
get describe(): string;
|
|
227
|
+
}
|
|
228
|
+
/** One pull from an iterator, as `for...of` sees it. */
|
|
229
|
+
export type Step = {
|
|
230
|
+
readonly done: boolean;
|
|
231
|
+
readonly value: Value;
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* An iterator the interpreter drives, as data: a built-in cursor over a collection (`Position`), or a program
|
|
235
|
+
* iterator driven by its own `next` method (`CustomIterator`).
|
|
236
|
+
*/
|
|
237
|
+
export type Cursor = Position | CustomIterator;
|
|
238
|
+
/** A program iterator record: the iterator, its `next` method, and whether its results are awaited. */
|
|
239
|
+
export type CustomIterator = {
|
|
240
|
+
readonly iterator: Obj;
|
|
241
|
+
readonly next: Value;
|
|
242
|
+
readonly asynchronous: boolean;
|
|
243
|
+
};
|
|
244
|
+
/** What a built-in iterator yields for each entry: `keys()`, `values()`, or `entries()`. */
|
|
245
|
+
export type IterationKind = "keys" | "values" | "entries";
|
|
246
|
+
/** What a built-in iterator walks: a collection, a string, or a fixed list of values. */
|
|
247
|
+
export type PositionSource = Arr | Bytes | MapObj | SetObj | HeadersObj | URLSearchParamsObj | string | ReadonlyArray<Value>;
|
|
248
|
+
/**
|
|
249
|
+
* A built-in iterator's place in its source, as data: what it walks, what it yields, and the next index. Iteration is
|
|
250
|
+
* live, as in JS: an array iterator reads the length on every step, and a Map or Set iterator is kept in place by its
|
|
251
|
+
* table. Once done it stays done. There is nothing to close.
|
|
252
|
+
*/
|
|
253
|
+
export declare class Position implements TableCursor {
|
|
254
|
+
readonly source: PositionSource;
|
|
255
|
+
readonly kind: IterationKind;
|
|
256
|
+
/** The prototype of the `[key, value]` arrays `entries()` yields. */
|
|
257
|
+
readonly arrays: Obj;
|
|
258
|
+
index: number;
|
|
259
|
+
done: boolean;
|
|
260
|
+
constructor(source: PositionSource, kind: IterationKind,
|
|
261
|
+
/** The prototype of the `[key, value]` arrays `entries()` yields. */
|
|
262
|
+
arrays: Obj);
|
|
263
|
+
step(): Step;
|
|
264
|
+
private yield;
|
|
265
|
+
private finish;
|
|
266
|
+
}
|
|
267
|
+
/** A built-in iterator: a live cursor over a host collection or an iterator helper, yielding program values. */
|
|
268
|
+
export declare class IteratorObj extends Opaque {
|
|
269
|
+
readonly cursor: Cursor;
|
|
270
|
+
readonly tag = "Iterator";
|
|
271
|
+
constructor(proto: Obj, cursor: Cursor);
|
|
272
|
+
get describe(): string;
|
|
273
|
+
}
|
|
274
|
+
/** A built-in iterator over a collection, e.g. `array.values()`. */
|
|
275
|
+
export declare const positionIterator: (builtins: Builtins, source: PositionSource, kind: IterationKind) => IteratorObj;
|
|
276
|
+
/** A built-in object around a host value: data-like, so it prints as itself and crosses to extensions as a copy. */
|
|
277
|
+
export declare abstract class Wrapper extends Obj {
|
|
278
|
+
inspect(item: (value: Value) => string): string;
|
|
279
|
+
}
|
|
280
|
+
export declare class DateObj extends Wrapper {
|
|
281
|
+
time: number;
|
|
282
|
+
readonly tag = "Date";
|
|
283
|
+
constructor(proto: Obj, time: number);
|
|
284
|
+
toString(): string;
|
|
285
|
+
toPrimitive(hint: "default" | "number" | "string"): string | number;
|
|
286
|
+
toHost(): Date;
|
|
287
|
+
}
|
|
288
|
+
/** A RegExp as data: its source, flags, and `lastIndex`; the host regex is only a compiled cache of the first two. */
|
|
289
|
+
export declare class RegExpObj extends Wrapper {
|
|
290
|
+
readonly tag = "RegExp";
|
|
291
|
+
readonly source: string;
|
|
292
|
+
readonly flags: string;
|
|
293
|
+
/** Shared by `exec`/`test` and the String methods, as the host regex's own `lastIndex` would be. */
|
|
294
|
+
lastIndex: number;
|
|
295
|
+
private compiled;
|
|
296
|
+
constructor(proto: Obj, pattern: string, flags: string);
|
|
297
|
+
/** The compiled regex, for reading what it is; anything that matches goes through `use`. */
|
|
298
|
+
get regex(): RegExp;
|
|
299
|
+
/** Runs host regex work starting from this object's `lastIndex`, and keeps the `lastIndex` the work leaves. */
|
|
300
|
+
use<T>(work: (regex: RegExp) => T): T;
|
|
301
|
+
toString(): string;
|
|
302
|
+
toHost(): RegExp;
|
|
303
|
+
}
|
|
304
|
+
export declare class MapObj extends Wrapper {
|
|
305
|
+
readonly tag = "Map";
|
|
306
|
+
readonly map: OrderedTable;
|
|
307
|
+
inspect(item: (value: Value) => string): string;
|
|
308
|
+
toHost(item: (value: Value) => unknown): Map<unknown, unknown>;
|
|
309
|
+
iterator(builtins: Builtins): Position;
|
|
310
|
+
}
|
|
311
|
+
export declare class SetObj extends Wrapper {
|
|
312
|
+
readonly tag = "Set";
|
|
313
|
+
readonly set: OrderedTable;
|
|
314
|
+
inspect(item: (value: Value) => string): string;
|
|
315
|
+
toHost(item: (value: Value) => unknown): Set<unknown>;
|
|
316
|
+
iterator(builtins: Builtins): Position;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Keys are held strongly for the run, which is short-lived; garbage collection is not observable in Code Mode, and a
|
|
320
|
+
* plain table keeps the state as data.
|
|
321
|
+
*/
|
|
322
|
+
export declare class WeakMapObj extends Wrapper {
|
|
323
|
+
readonly tag = "WeakMap";
|
|
324
|
+
readonly map: Map<Obj, Value>;
|
|
325
|
+
inspect(): string;
|
|
326
|
+
}
|
|
327
|
+
export declare class WeakSetObj extends Wrapper {
|
|
328
|
+
readonly tag = "WeakSet";
|
|
329
|
+
readonly set: Set<Obj>;
|
|
330
|
+
inspect(): string;
|
|
331
|
+
}
|
|
332
|
+
export declare class URLSearchParamsObj extends Wrapper {
|
|
333
|
+
readonly params: URLSearchParams;
|
|
334
|
+
readonly tag = "URLSearchParams";
|
|
335
|
+
constructor(proto: Obj, params: URLSearchParams);
|
|
336
|
+
toString(): string;
|
|
337
|
+
toHost(): URLSearchParams;
|
|
338
|
+
iterator(builtins: Builtins): Position;
|
|
339
|
+
}
|
|
340
|
+
export declare class HeadersObj extends Wrapper {
|
|
341
|
+
readonly headers: Headers;
|
|
342
|
+
readonly tag = "Headers";
|
|
343
|
+
constructor(proto: Obj, headers: Headers);
|
|
344
|
+
inspect(): string;
|
|
345
|
+
toHost(): Headers;
|
|
346
|
+
iterator(builtins: Builtins): Position;
|
|
347
|
+
}
|
|
348
|
+
export declare class URLObj extends Wrapper {
|
|
349
|
+
readonly url: URL;
|
|
350
|
+
readonly tag = "URL";
|
|
351
|
+
readonly searchParams: URLSearchParamsObj;
|
|
352
|
+
constructor(proto: Obj, searchParamsProto: Obj, url: URL);
|
|
353
|
+
toString(): string;
|
|
354
|
+
toHost(): URL;
|
|
355
|
+
}
|
|
356
|
+
/** A `Uint8Array`: the host array does the byte clamping and ignores out-of-range writes, as JS does. */
|
|
357
|
+
export declare class Bytes extends Wrapper {
|
|
358
|
+
readonly bytes: Uint8Array;
|
|
359
|
+
readonly tag = "Uint8Array";
|
|
360
|
+
constructor(proto: Obj, bytes: Uint8Array);
|
|
361
|
+
toString(): string;
|
|
362
|
+
inspect(): string;
|
|
363
|
+
toHost(): Uint8Array<ArrayBuffer>;
|
|
364
|
+
iterator(builtins: Builtins): Position;
|
|
365
|
+
}
|
|
366
|
+
/** Every value a program can hold. Host values never appear here; they are copied in at the boundaries. */
|
|
367
|
+
export type Value = string | number | boolean | null | undefined | symbol | Obj | ToolReference;
|
|
368
|
+
/** ToString without consulting program-defined methods. */
|
|
369
|
+
export declare const coerceToString: (value: Value) => string;
|
|
370
|
+
/** ToNumber without consulting program-defined methods; tool references are not numbers. */
|
|
371
|
+
export declare const coerceToNumber: (value: Value) => number;
|
|
372
|
+
/** ToIntegerOrInfinity: NaN is 0, fractions truncate. */
|
|
373
|
+
export declare const coerceToInteger: (value: Value) => number;
|
|
374
|
+
/** Values that cannot cross the data boundary: opaque machinery and host-backed wrappers. */
|
|
375
|
+
export declare const isRuntimeReference: (value: Value) => boolean;
|
|
376
|
+
export declare const parseArrayIndex: (key: string | number) => number | undefined;
|
|
377
|
+
/** The own property under `key`, including an array's live indexes and `length`. */
|
|
378
|
+
export declare const own: (target: Obj, key: PropertyKey) => Slot | undefined;
|
|
379
|
+
export declare const hasOwn: (target: Obj, key: PropertyKey) => boolean;
|
|
380
|
+
export declare const getOwn: (target: Obj, key: PropertyKey) => Value;
|
|
381
|
+
/** [[Get]]: walks the prototype chain; accessors see `receiver`, which is the primitive for wrapper prototypes. */
|
|
382
|
+
export declare const get: (target: Obj, key: PropertyKey, receiver?: Value) => Value;
|
|
383
|
+
export declare const has: (target: Obj, key: PropertyKey) => boolean;
|
|
384
|
+
export declare const hasPrototype: (value: Value, proto: Obj) => boolean;
|
|
385
|
+
/** [[Set]]: an inherited setter or read-only property decides before an own data property is created. */
|
|
386
|
+
export declare const set: (target: Obj, key: PropertyKey, value: Value) => boolean;
|
|
387
|
+
/** Creating a property on a non-extensible object is the one [[Set]] failure with its own message. */
|
|
388
|
+
export declare const rejectAddition: (target: Obj, key: string | symbol | number) => void;
|
|
389
|
+
/** [[DefineOwnProperty]] for a data property, ignoring the chain. */
|
|
390
|
+
export declare const define: (target: Obj, key: PropertyKey, value: Value, attrs?: Attributes) => void;
|
|
391
|
+
export declare const defineAccessor: (target: Obj, key: PropertyKey, get: Getter | undefined, set?: Setter) => void;
|
|
392
|
+
export declare const remove: (target: Obj, key: PropertyKey) => boolean;
|
|
393
|
+
export declare const ownKeys: (target: Obj) => Array<string | symbol>;
|
|
394
|
+
/** Own enumerable keys, including the iterator symbols; what spread and `Object.assign` copy. */
|
|
395
|
+
export declare const enumerableKeys: (target: Obj) => Array<string | symbol>;
|
|
396
|
+
/** Own enumerable string keys: `Object.keys`. */
|
|
397
|
+
export declare const keys: (target: Obj) => Array<string>;
|
|
398
|
+
/** Own enumerable string entries: `Object.entries` and serialization. */
|
|
399
|
+
export declare const entries: (target: Obj) => Array<[string, Value]>;
|
|
400
|
+
/** An iterator result `{ value, done }`, built directly since every iteration step makes one. */
|
|
401
|
+
export declare const iteratorResult: (proto: Obj, value: Value, done: boolean) => Obj;
|
|
402
|
+
export declare const record: (proto: Obj, fields: Record<string, Value>) => Obj;
|
|
403
|
+
export declare const assign: (target: Obj, source: Obj, skip?: ReadonlySet<PropertyKey>) => void;
|