@opencode/codemode 0.0.0-dev-19631 → 0.0.0-dev-19633
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/dist/codemode.js +1 -1
- package/dist/data.d.ts +3 -3
- package/dist/data.js +37 -31
- package/dist/interpreter/callback.d.ts +20 -0
- package/dist/interpreter/{runner.js → callback.js} +10 -10
- package/dist/interpreter/errors.d.ts +5 -5
- package/dist/interpreter/errors.js +21 -22
- package/dist/interpreter/execute.d.ts +2 -2
- package/dist/interpreter/execute.js +11 -11
- package/dist/interpreter/extensions.d.ts +2 -2
- package/dist/interpreter/extensions.js +42 -36
- package/dist/interpreter/generators.d.ts +2 -3
- package/dist/interpreter/generators.js +8 -8
- package/dist/interpreter/globals.d.ts +2 -12
- package/dist/interpreter/globals.js +41 -37
- package/dist/interpreter/interpreter.d.ts +31 -0
- package/dist/interpreter/{runtime.js → interpreter.js} +118 -117
- package/dist/interpreter/intrinsics.d.ts +5 -5
- package/dist/interpreter/intrinsics.js +16 -10
- package/dist/interpreter/model.d.ts +3 -3
- package/dist/interpreter/model.js +1 -1
- package/dist/interpreter/native.d.ts +9 -9
- package/dist/interpreter/native.js +9 -9
- package/dist/interpreter/objects.d.ts +51 -46
- package/dist/interpreter/objects.js +57 -40
- package/dist/interpreter/promises.d.ts +13 -13
- package/dist/interpreter/promises.js +70 -70
- package/dist/interpreter/references.js +17 -15
- package/dist/openapi/types.d.ts +1 -1
- package/dist/stdlib/array.d.ts +3 -3
- package/dist/stdlib/array.js +31 -31
- package/dist/stdlib/bytes.d.ts +10 -0
- package/dist/stdlib/bytes.js +228 -0
- package/dist/stdlib/collections.d.ts +4 -4
- package/dist/stdlib/collections.js +55 -55
- package/dist/stdlib/console.d.ts +3 -3
- package/dist/stdlib/console.js +24 -22
- package/dist/stdlib/date.d.ts +2 -2
- package/dist/stdlib/date.js +19 -19
- package/dist/stdlib/json.d.ts +3 -3
- package/dist/stdlib/json.js +24 -24
- package/dist/stdlib/math.d.ts +3 -3
- package/dist/stdlib/math.js +7 -7
- package/dist/stdlib/number.d.ts +3 -3
- package/dist/stdlib/number.js +11 -11
- package/dist/stdlib/object.d.ts +5 -5
- package/dist/stdlib/object.js +41 -39
- package/dist/stdlib/regexp.d.ts +6 -6
- package/dist/stdlib/regexp.js +24 -24
- package/dist/stdlib/string.d.ts +2 -2
- package/dist/stdlib/string.js +24 -24
- package/dist/stdlib/url.d.ts +6 -6
- package/dist/stdlib/url.js +39 -39
- package/dist/stdlib/value.d.ts +3 -3
- package/dist/stdlib/value.js +18 -14
- package/dist/stdlib/web.d.ts +4 -4
- package/dist/stdlib/web.js +19 -5
- package/dist/tool-runtime.d.ts +2 -2
- package/dist/tool-runtime.js +2 -2
- package/package.json +1 -1
- package/dist/interpreter/runner.d.ts +0 -27
- package/dist/interpreter/runtime.d.ts +0 -19
package/dist/stdlib/date.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { constructor, methods, prototypeFrom, receiver } from "../interpreter/native.js";
|
|
3
3
|
import { rangeError } from "../interpreter/model.js";
|
|
4
|
-
import {
|
|
5
|
-
import { toPrimitive, toPrimitiveNumber } from "../interpreter/
|
|
4
|
+
import { DateObj, Obj } from "../interpreter/objects.js";
|
|
5
|
+
import { toPrimitive, toPrimitiveNumber } from "../interpreter/callback.js";
|
|
6
6
|
import { coerceToNumber, coerceToString } from "./value.js";
|
|
7
|
-
const constructDate = (
|
|
7
|
+
const constructDate = (ctx, args, proto) => {
|
|
8
8
|
if (args.length === 0)
|
|
9
|
-
return Effect.succeed(new
|
|
9
|
+
return Effect.succeed(new DateObj(proto, Date.now()));
|
|
10
10
|
if (args.length === 1) {
|
|
11
11
|
const arg = args[0];
|
|
12
|
-
if (arg instanceof
|
|
13
|
-
return Effect.succeed(new
|
|
14
|
-
return Effect.map(toPrimitive(
|
|
15
|
-
? new
|
|
16
|
-
: new
|
|
12
|
+
if (arg instanceof DateObj)
|
|
13
|
+
return Effect.succeed(new DateObj(proto, arg.time));
|
|
14
|
+
return Effect.map(toPrimitive(ctx, arg, "default"), (value) => typeof value === "string"
|
|
15
|
+
? new DateObj(proto, Date.parse(value))
|
|
16
|
+
: new DateObj(proto, new Date(coerceToNumber(value)).getTime()));
|
|
17
17
|
}
|
|
18
18
|
const parts = args.map((arg) => coerceToNumber(arg));
|
|
19
|
-
return Effect.succeed(new
|
|
19
|
+
return Effect.succeed(new DateObj(proto, new Date(...parts).getTime()));
|
|
20
20
|
};
|
|
21
21
|
const getters = [
|
|
22
22
|
"getFullYear",
|
|
@@ -54,28 +54,28 @@ const setters = [
|
|
|
54
54
|
["setFullYear", 3],
|
|
55
55
|
["setUTCFullYear", 3],
|
|
56
56
|
];
|
|
57
|
-
export const dateGlobal = (
|
|
58
|
-
const
|
|
59
|
-
const proto =
|
|
60
|
-
const date = constructor(
|
|
57
|
+
export const dateGlobal = (ctx) => {
|
|
58
|
+
const builtins = ctx.builtins;
|
|
59
|
+
const proto = builtins.Date;
|
|
60
|
+
const date = constructor(builtins, proto, {
|
|
61
61
|
name: "Date",
|
|
62
62
|
length: 7,
|
|
63
63
|
// ISO instead of the host's locale string: date strings are deterministic and must not leak the host timezone.
|
|
64
64
|
call: () => Effect.sync(() => new Date().toISOString()),
|
|
65
|
-
construct: (args, newTarget) => constructDate(
|
|
65
|
+
construct: (args, newTarget) => constructDate(ctx, args, prototypeFrom(newTarget, proto)),
|
|
66
66
|
});
|
|
67
|
-
methods(
|
|
67
|
+
methods(builtins, date, [
|
|
68
68
|
["now", 0, () => Date.now()],
|
|
69
69
|
["parse", 1, (_, args) => Date.parse(coerceToString(args[0]))],
|
|
70
70
|
["UTC", 7, (_, args) => Date.UTC(...args.map((arg) => coerceToNumber(arg)))],
|
|
71
71
|
]);
|
|
72
|
-
const self = (thisValue, name) => receiver(
|
|
72
|
+
const self = (thisValue, name) => receiver(DateObj, thisValue, `Date.prototype.${name}`);
|
|
73
73
|
const iso = (value) => {
|
|
74
74
|
if (!Number.isFinite(value.time))
|
|
75
75
|
throw rangeError("Invalid time value.");
|
|
76
76
|
return new Date(value.time).toISOString();
|
|
77
77
|
};
|
|
78
|
-
methods(
|
|
78
|
+
methods(builtins, proto, [
|
|
79
79
|
["getTime", 0, (thisValue) => self(thisValue, "getTime").time],
|
|
80
80
|
["valueOf", 0, (thisValue) => self(thisValue, "valueOf").time],
|
|
81
81
|
["toISOString", 0, (thisValue) => iso(self(thisValue, "toISOString"))],
|
|
@@ -100,7 +100,7 @@ export const dateGlobal = (runner) => {
|
|
|
100
100
|
const target = self(thisValue, name);
|
|
101
101
|
// Native setters read the current time before argument coercion, whose callbacks may mutate the Date.
|
|
102
102
|
const hosted = new Date(target.time);
|
|
103
|
-
return Effect.map(Effect.forEach(args.slice(0, length), (arg) => toPrimitiveNumber(
|
|
103
|
+
return Effect.map(Effect.forEach(args.slice(0, length), (arg) => toPrimitiveNumber(ctx, arg), {
|
|
104
104
|
concurrency: 1,
|
|
105
105
|
}), (values) => {
|
|
106
106
|
target.time = hosted[name](...values);
|
package/dist/stdlib/json.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
export declare const jsonGlobal: <R>(
|
|
1
|
+
import type { Interpreter } from "../interpreter/interpreter.js";
|
|
2
|
+
import { Obj } from "../interpreter/objects.js";
|
|
3
|
+
export declare const jsonGlobal: <R>(ctx: Interpreter<R>) => Obj;
|
package/dist/stdlib/json.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { methods } from "../interpreter/native.js";
|
|
3
|
-
import { applyCollectionCallback } from "../interpreter/
|
|
3
|
+
import { applyCollectionCallback } from "../interpreter/callback.js";
|
|
4
4
|
import { checkStringLength } from "../interpreter/limits.js";
|
|
5
5
|
import { syntaxError, typeError } from "../interpreter/model.js";
|
|
6
6
|
import { typeofValue } from "../interpreter/references.js";
|
|
7
7
|
import { fromData, toData, toProgram } from "../data.js";
|
|
8
|
-
import { Callable, get, keys,
|
|
9
|
-
export const jsonGlobal = (
|
|
10
|
-
const json = new
|
|
11
|
-
methods(
|
|
12
|
-
["parse", 2, (_, args) => parse(
|
|
13
|
-
["stringify", 3, (_, args) => stringify(
|
|
8
|
+
import { Callable, get, keys, Arr, Obj, record, remove, set } from "../interpreter/objects.js";
|
|
9
|
+
export const jsonGlobal = (ctx) => {
|
|
10
|
+
const json = new Obj(ctx.builtins.Object);
|
|
11
|
+
methods(ctx.builtins, json, [
|
|
12
|
+
["parse", 2, (_, args) => parse(ctx, args)],
|
|
13
|
+
["stringify", 3, (_, args) => stringify(ctx, args)],
|
|
14
14
|
]);
|
|
15
15
|
return json;
|
|
16
16
|
};
|
|
17
|
-
const parse = (
|
|
17
|
+
const parse = (ctx, args) => {
|
|
18
18
|
const text = args[0];
|
|
19
19
|
if (typeof text !== "string")
|
|
20
20
|
throw typeError("JSON.parse expects a string.");
|
|
21
21
|
const parsed = (() => {
|
|
22
22
|
try {
|
|
23
|
-
return fromData(
|
|
23
|
+
return fromData(ctx.builtins, JSON.parse(text), "JSON.parse result");
|
|
24
24
|
}
|
|
25
25
|
catch (error) {
|
|
26
26
|
throw syntaxError(`JSON.parse received invalid JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
@@ -28,10 +28,10 @@ const parse = (runner, args) => {
|
|
|
28
28
|
})();
|
|
29
29
|
if (typeofValue(args[1]) !== "function")
|
|
30
30
|
return Effect.succeed(parsed);
|
|
31
|
-
const apply = applyCollectionCallback(
|
|
31
|
+
const apply = applyCollectionCallback(ctx, args[1], "JSON.parse");
|
|
32
32
|
const visit = (holder, key) => Effect.gen(function* () {
|
|
33
33
|
const value = get(holder, key);
|
|
34
|
-
if (value instanceof
|
|
34
|
+
if (value instanceof Obj) {
|
|
35
35
|
for (const name of keys(value)) {
|
|
36
36
|
const revived = yield* visit(value, name);
|
|
37
37
|
if (revived === undefined)
|
|
@@ -42,14 +42,14 @@ const parse = (runner, args) => {
|
|
|
42
42
|
}
|
|
43
43
|
return yield* apply([key, value]);
|
|
44
44
|
});
|
|
45
|
-
return visit(record(
|
|
45
|
+
return visit(record(ctx.builtins.Object, { "": parsed }), "");
|
|
46
46
|
};
|
|
47
|
-
const stringify = (
|
|
47
|
+
const stringify = (ctx, args) => {
|
|
48
48
|
const space = args[2];
|
|
49
49
|
const indent = typeof space === "number" || typeof space === "string" ? space : undefined;
|
|
50
50
|
const replacer = args[1];
|
|
51
51
|
if (typeofValue(replacer) !== "function") {
|
|
52
|
-
const properties = replacer instanceof
|
|
52
|
+
const properties = replacer instanceof Arr
|
|
53
53
|
? replacer.items
|
|
54
54
|
.filter((item) => typeof item === "string" || typeof item === "number")
|
|
55
55
|
.map(String)
|
|
@@ -61,24 +61,24 @@ const stringify = (runner, args) => {
|
|
|
61
61
|
return Effect.succeed(text);
|
|
62
62
|
}
|
|
63
63
|
// Validate up front; the replacer walk below reads the original value.
|
|
64
|
-
toProgram(
|
|
65
|
-
const apply = applyCollectionCallback(
|
|
64
|
+
toProgram(ctx.builtins, args[0], "JSON.stringify value");
|
|
65
|
+
const apply = applyCollectionCallback(ctx, replacer, "JSON.stringify");
|
|
66
66
|
const stack = new Set();
|
|
67
67
|
const visit = (holder, key) => Effect.gen(function* () {
|
|
68
|
-
const value = yield* apply([key, yield* toJSONValue(
|
|
68
|
+
const value = yield* apply([key, yield* toJSONValue(ctx, get(holder, key), key)]);
|
|
69
69
|
if (value === undefined || typeofValue(value) === "function")
|
|
70
70
|
return undefined;
|
|
71
|
-
toProgram(
|
|
71
|
+
toProgram(ctx.builtins, value, "JSON.stringify replacer result");
|
|
72
72
|
if (typeof value === "number")
|
|
73
73
|
return Number.isFinite(value) ? value : null;
|
|
74
74
|
if (value === null || typeof value === "string" || typeof value === "boolean")
|
|
75
75
|
return value;
|
|
76
|
-
if (!(value instanceof
|
|
76
|
+
if (!(value instanceof Obj))
|
|
77
77
|
return {};
|
|
78
78
|
if (stack.has(value))
|
|
79
79
|
throw typeError("Converting circular structure to JSON.");
|
|
80
80
|
stack.add(value);
|
|
81
|
-
if (value instanceof
|
|
81
|
+
if (value instanceof Arr) {
|
|
82
82
|
const result = [];
|
|
83
83
|
for (let index = 0; index < value.items.length; index += 1) {
|
|
84
84
|
result.push((yield* visit(value, String(index))) ?? null);
|
|
@@ -95,12 +95,12 @@ const stringify = (runner, args) => {
|
|
|
95
95
|
stack.delete(value);
|
|
96
96
|
return result;
|
|
97
97
|
});
|
|
98
|
-
return Effect.map(visit(record(
|
|
98
|
+
return Effect.map(visit(record(ctx.builtins.Object, { "": args[0] }), ""), (value) => JSON.stringify(value, null, indent));
|
|
99
99
|
};
|
|
100
100
|
// SerializeJSONProperty step 2: a callable `toJSON` decides the value, as Date and URL define.
|
|
101
|
-
const toJSONValue = (
|
|
102
|
-
if (!(value instanceof
|
|
101
|
+
const toJSONValue = (ctx, value, key) => {
|
|
102
|
+
if (!(value instanceof Obj))
|
|
103
103
|
return Effect.succeed(value);
|
|
104
104
|
const toJSON = get(value, "toJSON");
|
|
105
|
-
return toJSON instanceof Callable ?
|
|
105
|
+
return toJSON instanceof Callable ? ctx.call(toJSON, value, [key]) : Effect.succeed(value);
|
|
106
106
|
};
|
package/dist/stdlib/math.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Obj } from "../interpreter/objects.js";
|
|
2
|
+
import type { Interpreter } from "../interpreter/interpreter.js";
|
|
3
3
|
declare global {
|
|
4
4
|
interface Math {
|
|
5
5
|
sumPrecise(values: Iterable<number>): number;
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
|
-
export declare const mathGlobal: <R>(
|
|
8
|
+
export declare const mathGlobal: <R>(ctx: Interpreter<R>) => Obj;
|
package/dist/stdlib/math.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { constants, methods } from "../interpreter/native.js";
|
|
3
3
|
import { typeError } from "../interpreter/model.js";
|
|
4
|
-
import {
|
|
5
|
-
import { preserveConsumerError } from "../interpreter/
|
|
4
|
+
import { Obj } from "../interpreter/objects.js";
|
|
5
|
+
import { preserveConsumerError } from "../interpreter/callback.js";
|
|
6
6
|
// Validate only the arguments a method consumes; like JS, extras are ignored
|
|
7
7
|
// (so built-ins work as callbacks receiving (element, index, array)).
|
|
8
8
|
const number = (name, args, index) => {
|
|
@@ -28,9 +28,9 @@ const variadic = (name, op) => [
|
|
|
28
28
|
return arg;
|
|
29
29
|
})),
|
|
30
30
|
];
|
|
31
|
-
export const mathGlobal = (
|
|
32
|
-
const
|
|
33
|
-
const math = new
|
|
31
|
+
export const mathGlobal = (ctx) => {
|
|
32
|
+
const builtins = ctx.builtins;
|
|
33
|
+
const math = new Obj(builtins.Object);
|
|
34
34
|
constants(math, {
|
|
35
35
|
PI: Math.PI,
|
|
36
36
|
E: Math.E,
|
|
@@ -41,7 +41,7 @@ export const mathGlobal = (runner) => {
|
|
|
41
41
|
SQRT2: Math.SQRT2,
|
|
42
42
|
SQRT1_2: Math.SQRT1_2,
|
|
43
43
|
});
|
|
44
|
-
methods(
|
|
44
|
+
methods(builtins, math, [
|
|
45
45
|
["random", 0, () => Math.random()],
|
|
46
46
|
variadic("max", Math.max),
|
|
47
47
|
variadic("min", Math.min),
|
|
@@ -82,7 +82,7 @@ export const mathGlobal = (runner) => {
|
|
|
82
82
|
"sumPrecise",
|
|
83
83
|
1,
|
|
84
84
|
(_, args) => Effect.gen(function* () {
|
|
85
|
-
const cursor = yield*
|
|
85
|
+
const cursor = yield* ctx.iterate(args[0]);
|
|
86
86
|
if (cursor === undefined) {
|
|
87
87
|
throw typeError("Math.sumPrecise expects a synchronous iterable.");
|
|
88
88
|
}
|
package/dist/stdlib/number.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const numberGlobal: <R>(
|
|
3
|
-
export declare const booleanGlobal: <R>(
|
|
1
|
+
import type { Interpreter } from "../interpreter/interpreter.js";
|
|
2
|
+
export declare const numberGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native<R>;
|
|
3
|
+
export declare const booleanGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native<R>;
|
package/dist/stdlib/number.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { constructor, constants, methods } from "../interpreter/native.js";
|
|
2
2
|
import { rangeError, typeError } from "../interpreter/model.js";
|
|
3
3
|
import { coercion, coerceToString } from "./value.js";
|
|
4
|
-
export const numberGlobal = (
|
|
5
|
-
const
|
|
6
|
-
const number = constructor(
|
|
4
|
+
export const numberGlobal = (ctx) => {
|
|
5
|
+
const builtins = ctx.builtins;
|
|
6
|
+
const number = constructor(builtins, builtins.Number, {
|
|
7
7
|
name: "Number",
|
|
8
8
|
length: 1,
|
|
9
|
-
call: coercion(
|
|
9
|
+
call: coercion(ctx, "Number").call,
|
|
10
10
|
});
|
|
11
11
|
constants(number, {
|
|
12
12
|
MAX_SAFE_INTEGER: Number.MAX_SAFE_INTEGER,
|
|
@@ -18,7 +18,7 @@ export const numberGlobal = (runner) => {
|
|
|
18
18
|
POSITIVE_INFINITY: Number.POSITIVE_INFINITY,
|
|
19
19
|
NEGATIVE_INFINITY: Number.NEGATIVE_INFINITY,
|
|
20
20
|
});
|
|
21
|
-
methods(
|
|
21
|
+
methods(builtins, number, [
|
|
22
22
|
["isInteger", 1, (_, args) => Number.isInteger(args[0])],
|
|
23
23
|
["isFinite", 1, (_, args) => Number.isFinite(args[0])],
|
|
24
24
|
["isNaN", 1, (_, args) => Number.isNaN(args[0])],
|
|
@@ -48,7 +48,7 @@ export const numberGlobal = (runner) => {
|
|
|
48
48
|
throw typeError(`Number.${name} expects a number argument.`);
|
|
49
49
|
return arg;
|
|
50
50
|
};
|
|
51
|
-
methods(
|
|
51
|
+
methods(builtins, builtins.Number, [
|
|
52
52
|
["toFixed", 1, (thisValue, args) => self(thisValue, "toFixed").toFixed(optNum("toFixed", args[0]))],
|
|
53
53
|
[
|
|
54
54
|
"toExponential",
|
|
@@ -80,19 +80,19 @@ export const numberGlobal = (runner) => {
|
|
|
80
80
|
]);
|
|
81
81
|
return number;
|
|
82
82
|
};
|
|
83
|
-
export const booleanGlobal = (
|
|
84
|
-
const
|
|
85
|
-
const boolean = constructor(
|
|
83
|
+
export const booleanGlobal = (ctx) => {
|
|
84
|
+
const builtins = ctx.builtins;
|
|
85
|
+
const boolean = constructor(builtins, builtins.Boolean, {
|
|
86
86
|
name: "Boolean",
|
|
87
87
|
length: 1,
|
|
88
|
-
call: coercion(
|
|
88
|
+
call: coercion(ctx, "Boolean").call,
|
|
89
89
|
});
|
|
90
90
|
const self = (thisValue, name) => {
|
|
91
91
|
if (typeof thisValue === "boolean")
|
|
92
92
|
return thisValue;
|
|
93
93
|
throw typeError(`Boolean.prototype.${name} requires that 'this' be a Boolean.`);
|
|
94
94
|
};
|
|
95
|
-
methods(
|
|
95
|
+
methods(builtins, builtins.Boolean, [
|
|
96
96
|
["toString", 0, (thisValue) => String(self(thisValue, "toString"))],
|
|
97
97
|
["valueOf", 0, (thisValue) => self(thisValue, "valueOf")],
|
|
98
98
|
]);
|
package/dist/stdlib/object.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type AstNode } from "../interpreter/model.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
export declare const enumerableSource: <R>(
|
|
5
|
-
export declare const objectAssign: <R>(
|
|
2
|
+
import { Obj } from "../interpreter/objects.js";
|
|
3
|
+
import type { Interpreter } from "../interpreter/interpreter.js";
|
|
4
|
+
export declare const enumerableSource: <R>(ctx: Interpreter<R>, label: string, value: unknown, node?: AstNode) => Obj;
|
|
5
|
+
export declare const objectAssign: <R>(ctx: Interpreter<R>, args: Array<unknown>) => unknown;
|
|
6
6
|
export declare const classTag: (value: unknown) => string;
|
|
7
|
-
export declare const objectGlobal: <R>(
|
|
7
|
+
export declare const objectGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native<R>;
|
package/dist/stdlib/object.js
CHANGED
|
@@ -2,44 +2,44 @@ import { Effect } from "effect";
|
|
|
2
2
|
import { toProgram } from "../data.js";
|
|
3
3
|
import { constructor, methods, receiver } from "../interpreter/native.js";
|
|
4
4
|
import { AsyncIteratorSymbol, invalidData, IteratorSymbol, rangeError, typeError, } from "../interpreter/model.js";
|
|
5
|
-
import { Callable, define, entries, enumerableKeys, getOwn, hasOwn, hasPrototype, hidden, keys, own,
|
|
5
|
+
import { Callable, define, entries, enumerableKeys, getOwn, hasOwn, hasPrototype, hidden, keys, own, Arr, Bytes, DateObj, ErrorObj, Obj, PromiseObj, RegExpObj, set, } from "../interpreter/objects.js";
|
|
6
6
|
import { containsOpaqueReference, describeValue, rejectCircularInsertion } from "../interpreter/references.js";
|
|
7
|
-
import { preserveConsumerError } from "../interpreter/
|
|
7
|
+
import { preserveConsumerError } from "../interpreter/callback.js";
|
|
8
8
|
import { ToolReference } from "../tool-runtime.js";
|
|
9
9
|
import { groupBy } from "./collections.js";
|
|
10
10
|
import { coerceToString } from "./value.js";
|
|
11
11
|
// ToObject for enumeration.
|
|
12
|
-
export const enumerableSource = (
|
|
12
|
+
export const enumerableSource = (ctx, label, value, node) => {
|
|
13
13
|
if (value === null || value === undefined) {
|
|
14
14
|
throw typeError(`${label} cannot convert ${describeValue(value)} to an object.`, node);
|
|
15
15
|
}
|
|
16
|
-
if (value instanceof
|
|
16
|
+
if (value instanceof PromiseObj) {
|
|
17
17
|
throw invalidData(`${label} received an un-awaited Promise; await it before inspecting the result.`, node);
|
|
18
18
|
}
|
|
19
19
|
if (value instanceof ToolReference) {
|
|
20
20
|
throw invalidData(`${label} cannot read tool references: they are not plain data. Use Object.keys(tools) for names, or search({ query }) for signatures.`, node);
|
|
21
21
|
}
|
|
22
22
|
if (typeof value === "string")
|
|
23
|
-
return new
|
|
24
|
-
if (value instanceof
|
|
23
|
+
return new Arr(ctx.builtins.Array, [...value]);
|
|
24
|
+
if (value instanceof Obj)
|
|
25
25
|
return value;
|
|
26
|
-
return new
|
|
26
|
+
return new Obj(ctx.builtins.Object);
|
|
27
27
|
};
|
|
28
|
-
export const objectAssign = (
|
|
28
|
+
export const objectAssign = (ctx, args) => {
|
|
29
29
|
const target = args[0];
|
|
30
30
|
// JS would box a primitive target; wrappers and primitives cannot hold fields here.
|
|
31
|
-
if (!(target instanceof
|
|
31
|
+
if (!(target instanceof Obj)) {
|
|
32
32
|
throw typeError(`Object.assign expects a data object or array target, received ${describeValue(target)}.`);
|
|
33
33
|
}
|
|
34
34
|
const seen = new Set();
|
|
35
35
|
for (const source of args.slice(1)) {
|
|
36
36
|
if (source === null || source === undefined)
|
|
37
37
|
continue;
|
|
38
|
-
const from = enumerableSource(
|
|
38
|
+
const from = enumerableSource(ctx, "Object.assign(...)", source);
|
|
39
39
|
for (const key of enumerableKeys(from)) {
|
|
40
40
|
rejectCircularInsertion(target, getOwn(from, key), "Object.assign result", seen);
|
|
41
41
|
if (!set(target, key, getOwn(from, key))) {
|
|
42
|
-
if (target instanceof
|
|
42
|
+
if (target instanceof Arr && key === "length")
|
|
43
43
|
throw rangeError("Invalid array length");
|
|
44
44
|
throw typeError(`Cannot assign to read only property '${String(key)}'.`);
|
|
45
45
|
}
|
|
@@ -47,10 +47,10 @@ export const objectAssign = (runner, args) => {
|
|
|
47
47
|
}
|
|
48
48
|
return target;
|
|
49
49
|
};
|
|
50
|
-
const objectFromEntries = (
|
|
51
|
-
const out = new
|
|
50
|
+
const objectFromEntries = (ctx, source) => {
|
|
51
|
+
const out = new Obj(ctx.builtins.Object);
|
|
52
52
|
return Effect.gen(function* () {
|
|
53
|
-
const cursor = yield*
|
|
53
|
+
const cursor = yield* ctx.iterate(source);
|
|
54
54
|
if (cursor === undefined) {
|
|
55
55
|
throw typeError("Object.fromEntries expects a synchronous iterable of entries.");
|
|
56
56
|
}
|
|
@@ -59,7 +59,7 @@ const objectFromEntries = (runner, source) => {
|
|
|
59
59
|
if (step.done)
|
|
60
60
|
return out;
|
|
61
61
|
yield* preserveConsumerError(cursor, Effect.sync(() => {
|
|
62
|
-
if (!(step.value instanceof
|
|
62
|
+
if (!(step.value instanceof Obj) || containsOpaqueReference(step.value)) {
|
|
63
63
|
throw typeError("Object.fromEntries expects [key, value] entry objects.");
|
|
64
64
|
}
|
|
65
65
|
define(out, coerceToString(getOwn(step.value, 0)), getOwn(step.value, 1));
|
|
@@ -72,16 +72,18 @@ export const classTag = (value) => {
|
|
|
72
72
|
return "Null";
|
|
73
73
|
if (value === undefined)
|
|
74
74
|
return "Undefined";
|
|
75
|
-
if (value instanceof
|
|
75
|
+
if (value instanceof Arr)
|
|
76
76
|
return "Array";
|
|
77
77
|
if (value instanceof Callable)
|
|
78
78
|
return "Function";
|
|
79
|
-
if (value instanceof
|
|
79
|
+
if (value instanceof ErrorObj)
|
|
80
80
|
return "Error";
|
|
81
|
-
if (value instanceof
|
|
81
|
+
if (value instanceof DateObj)
|
|
82
82
|
return "Date";
|
|
83
|
-
if (value instanceof
|
|
83
|
+
if (value instanceof RegExpObj)
|
|
84
84
|
return "RegExp";
|
|
85
|
+
if (value instanceof Bytes)
|
|
86
|
+
return "Uint8Array";
|
|
85
87
|
if (typeof value === "string")
|
|
86
88
|
return "String";
|
|
87
89
|
if (typeof value === "number")
|
|
@@ -93,41 +95,41 @@ export const classTag = (value) => {
|
|
|
93
95
|
const propertyKey = (value) => value === AsyncIteratorSymbol || value === IteratorSymbol ? value : coerceToString(value);
|
|
94
96
|
// Object constructs identically with or without new, like JS. Only `keys` copies its result into the
|
|
95
97
|
// program; `values`, `entries`, `assign`, and `fromEntries` hand back the program's own values.
|
|
96
|
-
export const objectGlobal = (
|
|
97
|
-
const
|
|
98
|
+
export const objectGlobal = (ctx) => {
|
|
99
|
+
const builtins = ctx.builtins;
|
|
98
100
|
const construct = (args) => {
|
|
99
101
|
const first = args[0];
|
|
100
102
|
if (first === null || first === undefined)
|
|
101
|
-
return new
|
|
102
|
-
if (first instanceof
|
|
103
|
+
return new Obj(builtins.Object);
|
|
104
|
+
if (first instanceof Obj)
|
|
103
105
|
return first;
|
|
104
106
|
throw typeError(`Object(${typeof first}) wrapper objects are not supported; use the primitive value directly.`);
|
|
105
107
|
};
|
|
106
|
-
const object = constructor(
|
|
108
|
+
const object = constructor(builtins, builtins.Object, {
|
|
107
109
|
name: "Object",
|
|
108
110
|
length: 1,
|
|
109
111
|
call: (_, args) => Effect.sync(() => construct(args)),
|
|
110
112
|
construct: (args) => Effect.sync(() => construct(args)),
|
|
111
113
|
});
|
|
112
|
-
methods(
|
|
114
|
+
methods(builtins, object, [
|
|
113
115
|
[
|
|
114
116
|
"keys",
|
|
115
117
|
1,
|
|
116
|
-
(_, args) => toProgram(
|
|
117
|
-
? [...
|
|
118
|
-
: keys(enumerableSource(
|
|
118
|
+
(_, args) => toProgram(builtins, args[0] instanceof ToolReference
|
|
119
|
+
? [...ctx.tools.keys(args[0].path)]
|
|
120
|
+
: keys(enumerableSource(ctx, "Object.keys(...)", args[0])), "Object.keys result"),
|
|
119
121
|
],
|
|
120
122
|
[
|
|
121
123
|
"values",
|
|
122
124
|
1,
|
|
123
|
-
(_, args) => new
|
|
125
|
+
(_, args) => new Arr(builtins.Array, entries(enumerableSource(ctx, "Object.values(...)", args[0])).map((entry) => entry[1])),
|
|
124
126
|
],
|
|
125
127
|
[
|
|
126
128
|
"entries",
|
|
127
129
|
1,
|
|
128
|
-
(_, args) => new
|
|
130
|
+
(_, args) => new Arr(builtins.Array, entries(enumerableSource(ctx, "Object.entries(...)", args[0])).map((entry) => new Arr(builtins.Array, entry))),
|
|
129
131
|
],
|
|
130
|
-
["hasOwn", 2, (_, args) => hasOwn(enumerableSource(
|
|
132
|
+
["hasOwn", 2, (_, args) => hasOwn(enumerableSource(ctx, "Object.hasOwn(...)", args[0]), propertyKey(args[1]))],
|
|
131
133
|
[
|
|
132
134
|
"is",
|
|
133
135
|
2,
|
|
@@ -138,26 +140,26 @@ export const objectGlobal = (runner, toolKeys) => {
|
|
|
138
140
|
return Object.is(args[0], args[1]);
|
|
139
141
|
},
|
|
140
142
|
],
|
|
141
|
-
["assign", 2, (_, args) => objectAssign(
|
|
142
|
-
["fromEntries", 1, (_, args) => objectFromEntries(
|
|
143
|
+
["assign", 2, (_, args) => objectAssign(ctx, args)],
|
|
144
|
+
["fromEntries", 1, (_, args) => objectFromEntries(ctx, args[0])],
|
|
143
145
|
]);
|
|
144
|
-
define(object, "groupBy", groupBy(
|
|
145
|
-
methods(
|
|
146
|
+
define(object, "groupBy", groupBy(ctx, "Object"), hidden);
|
|
147
|
+
methods(builtins, builtins.Object, [
|
|
146
148
|
[
|
|
147
149
|
"hasOwnProperty",
|
|
148
150
|
1,
|
|
149
|
-
(thisValue, args) => hasOwn(receiver(
|
|
151
|
+
(thisValue, args) => hasOwn(receiver(Obj, thisValue, "Object.prototype.hasOwnProperty"), propertyKey(args[0])),
|
|
150
152
|
],
|
|
151
153
|
[
|
|
152
154
|
"isPrototypeOf",
|
|
153
155
|
1,
|
|
154
|
-
(thisValue, args) => hasPrototype(args[0], receiver(
|
|
156
|
+
(thisValue, args) => hasPrototype(args[0], receiver(Obj, thisValue, "Object.prototype.isPrototypeOf")),
|
|
155
157
|
],
|
|
156
158
|
[
|
|
157
159
|
"propertyIsEnumerable",
|
|
158
160
|
1,
|
|
159
|
-
(thisValue, args) => own(receiver(
|
|
160
|
-
|
|
161
|
+
(thisValue, args) => own(receiver(Obj, thisValue, "Object.prototype.propertyIsEnumerable"), propertyKey(args[0]))?.enumerable ===
|
|
162
|
+
true,
|
|
161
163
|
],
|
|
162
164
|
["toString", 0, (thisValue) => `[object ${classTag(thisValue)}]`],
|
|
163
165
|
["toLocaleString", 0, (thisValue) => `[object ${classTag(thisValue)}]`],
|
package/dist/stdlib/regexp.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
import type {
|
|
1
|
+
import type { Builtins } from "../interpreter/intrinsics.js";
|
|
2
|
+
import { Arr, Obj, RegExpObj } from "../interpreter/objects.js";
|
|
3
|
+
import type { Interpreter } from "../interpreter/interpreter.js";
|
|
4
4
|
export declare const toHostRegex: (arg: unknown, method: string, extraFlags?: string) => RegExp;
|
|
5
|
-
export declare const matchToValue: (
|
|
6
|
-
export declare const constructRegExp: (
|
|
7
|
-
export declare const regexpGlobal: <R>(
|
|
5
|
+
export declare const matchToValue: (builtins: Builtins, match: RegExpMatchArray) => Arr;
|
|
6
|
+
export declare const constructRegExp: (builtins: Builtins, args: Array<unknown>, proto?: Obj) => RegExpObj;
|
|
7
|
+
export declare const regexpGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native<R>;
|