@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
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Effect, Exit, Scope } from "effect";
|
|
2
2
|
import type { Diagnostic } from "../codemode.js";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
export declare class
|
|
3
|
+
import { Obj, PromiseObj } from "./objects.js";
|
|
4
|
+
import type { Interpreter } from "./interpreter.js";
|
|
5
|
+
export declare class Pending<R> {
|
|
6
6
|
private readonly scope;
|
|
7
7
|
private readonly proto;
|
|
8
8
|
private readonly active;
|
|
@@ -10,19 +10,19 @@ export declare class PromiseRuntime<R> {
|
|
|
10
10
|
private readonly observed;
|
|
11
11
|
private readonly failures;
|
|
12
12
|
private nextID;
|
|
13
|
-
constructor(scope: Scope.Scope, proto:
|
|
13
|
+
constructor(scope: Scope.Scope, proto: Obj);
|
|
14
14
|
createWithSelf(body: (self: {
|
|
15
|
-
promise?:
|
|
16
|
-
}) => Effect.Effect<unknown, unknown, R>): Effect.Effect<
|
|
17
|
-
create(effect: Effect.Effect<unknown, unknown, R>): Effect.Effect<
|
|
18
|
-
markObserved(promise:
|
|
19
|
-
await(promise:
|
|
15
|
+
promise?: PromiseObj;
|
|
16
|
+
}) => Effect.Effect<unknown, unknown, R>): Effect.Effect<PromiseObj, never, R>;
|
|
17
|
+
create(effect: Effect.Effect<unknown, unknown, R>): Effect.Effect<PromiseObj, never, R>;
|
|
18
|
+
markObserved(promise: PromiseObj): void;
|
|
19
|
+
await(promise: PromiseObj): Effect.Effect<Exit.Exit<unknown, unknown>>;
|
|
20
20
|
fork(effect: Effect.Effect<unknown, unknown, R>): Effect.Effect<void, never, R>;
|
|
21
21
|
diagnostics(): Array<Diagnostic>;
|
|
22
22
|
interrupt(): Effect.Effect<Array<Diagnostic>>;
|
|
23
23
|
}
|
|
24
|
-
export declare const resolvePromiseValue: <R>(
|
|
25
|
-
promise?:
|
|
24
|
+
export declare const resolvePromiseValue: <R>(ctx: Interpreter<R>, value: unknown, own?: {
|
|
25
|
+
promise?: PromiseObj;
|
|
26
26
|
}) => Effect.Effect<unknown, unknown, R>;
|
|
27
|
-
export declare const resolvePromise: <R>(
|
|
28
|
-
export declare const promiseGlobal: <R>(
|
|
27
|
+
export declare const resolvePromise: <R>(ctx: Interpreter<R>, value: unknown) => Effect.Effect<PromiseObj, never, R>;
|
|
28
|
+
export declare const promiseGlobal: <R>(ctx: Interpreter<R>) => import("./objects.js").Native<R>;
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { Cause, Deferred, Effect, Exit, Fiber, Scope } from "effect";
|
|
2
2
|
import { MAX_PENDING_PROMISES } from "./limits.js";
|
|
3
|
-
import { CallSite,
|
|
4
|
-
import { Callable, define, get, hidden,
|
|
3
|
+
import { CallSite, Throw, rangeError, typeError } from "./model.js";
|
|
4
|
+
import { Callable, define, get, hidden, Arr, Fn, Obj, PromiseObj, record } from "./objects.js";
|
|
5
5
|
import { constructor, fn, methods, native, receiver, requiresNew } from "./native.js";
|
|
6
6
|
import { createAggregateErrorValue, locate, materialize, normalizeError } from "./errors.js";
|
|
7
7
|
import { typeofValue } from "./references.js";
|
|
8
|
-
import { applyCollectionCallback, isSupportedCallback } from "./
|
|
8
|
+
import { applyCollectionCallback, isSupportedCallback } from "./callback.js";
|
|
9
9
|
// A `resolve`/`reject` handed to an executor or thenable: calling it settles the capability.
|
|
10
|
-
const capability = (
|
|
10
|
+
const capability = (ctx, name, settle) => fn(ctx.builtins, name, 1, (_, args) => {
|
|
11
11
|
settle(args[0]);
|
|
12
12
|
return undefined;
|
|
13
13
|
});
|
|
14
14
|
// Observation only controls rejection reporting; program completion interrupts all promise work.
|
|
15
|
-
export class
|
|
15
|
+
export class Pending {
|
|
16
16
|
scope;
|
|
17
17
|
proto;
|
|
18
18
|
active = new Set();
|
|
@@ -41,7 +41,7 @@ export class PromiseRuntime {
|
|
|
41
41
|
const id = this.nextID++;
|
|
42
42
|
const body = Effect.catchDefect(effect, (defect) => Effect.die(locate(defect, site.node)));
|
|
43
43
|
return Effect.map(Effect.forkIn(body, this.scope, { startImmediately: true }), (fiber) => {
|
|
44
|
-
const promise = new
|
|
44
|
+
const promise = new PromiseObj(this.proto, fiber);
|
|
45
45
|
this.active.add(promise);
|
|
46
46
|
this.ids.set(promise, id);
|
|
47
47
|
fiber.addObserver((exit) => {
|
|
@@ -88,13 +88,13 @@ export class PromiseRuntime {
|
|
|
88
88
|
});
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
|
-
export const resolvePromiseValue = (
|
|
91
|
+
export const resolvePromiseValue = (ctx, value, own) => {
|
|
92
92
|
if (own?.promise !== undefined && value === own.promise) {
|
|
93
93
|
return Effect.die(typeError("Chaining cycle detected: a promise cannot resolve with itself."));
|
|
94
94
|
}
|
|
95
|
-
if (value instanceof
|
|
96
|
-
return
|
|
97
|
-
if (!(value instanceof
|
|
95
|
+
if (value instanceof PromiseObj)
|
|
96
|
+
return ctx.await(value);
|
|
97
|
+
if (!(value instanceof Obj))
|
|
98
98
|
return Effect.succeed(value);
|
|
99
99
|
const then = get(value, "then");
|
|
100
100
|
if (typeofValue(then) !== "function")
|
|
@@ -103,32 +103,32 @@ export const resolvePromiseValue = (runner, value, own) => {
|
|
|
103
103
|
// Promise resolution invokes a thenable's method in a later job.
|
|
104
104
|
yield* Effect.yieldNow;
|
|
105
105
|
const deferred = Deferred.makeUnsafe();
|
|
106
|
-
const resolve = capability(
|
|
107
|
-
const reject = capability(
|
|
108
|
-
const executed = yield* Effect.exit(
|
|
106
|
+
const resolve = capability(ctx, "resolve", (result) => Deferred.doneUnsafe(deferred, Exit.succeed(result)));
|
|
107
|
+
const reject = capability(ctx, "reject", (reason) => Deferred.doneUnsafe(deferred, Exit.fail(new Throw(reason))));
|
|
108
|
+
const executed = yield* Effect.exit(ctx.call(then, value, [resolve, reject]));
|
|
109
109
|
if (!Exit.isSuccess(executed)) {
|
|
110
110
|
if (Cause.hasInterruptsOnly(executed.cause))
|
|
111
111
|
return yield* Effect.failCause(executed.cause);
|
|
112
112
|
Deferred.doneUnsafe(deferred, Exit.fail(Cause.squash(executed.cause)));
|
|
113
113
|
}
|
|
114
|
-
return yield* resolvePromiseValue(
|
|
114
|
+
return yield* resolvePromiseValue(ctx, yield* Deferred.await(deferred), own);
|
|
115
115
|
});
|
|
116
116
|
};
|
|
117
|
-
export const resolvePromise = (
|
|
118
|
-
if (value instanceof
|
|
117
|
+
export const resolvePromise = (ctx, value) => {
|
|
118
|
+
if (value instanceof PromiseObj)
|
|
119
119
|
return Effect.succeed(value);
|
|
120
|
-
return
|
|
120
|
+
return ctx.pending.createWithSelf((self) => resolvePromiseValue(ctx, value, self));
|
|
121
121
|
};
|
|
122
122
|
const promiseStatics = ["all", "allSettled", "race", "any", "resolve", "reject"];
|
|
123
|
-
const invokePromiseMethod = (
|
|
123
|
+
const invokePromiseMethod = (ctx, name, args) => {
|
|
124
124
|
if (name === "resolve") {
|
|
125
|
-
return resolvePromise(
|
|
125
|
+
return resolvePromise(ctx, args[0]);
|
|
126
126
|
}
|
|
127
127
|
if (name === "reject") {
|
|
128
|
-
return
|
|
128
|
+
return ctx.pending.create(Effect.fail(new Throw(args[0])));
|
|
129
129
|
}
|
|
130
|
-
return
|
|
131
|
-
const cursor = yield*
|
|
130
|
+
return ctx.pending.create(Effect.gen(function* () {
|
|
131
|
+
const cursor = yield* ctx.iterate(args[0]);
|
|
132
132
|
if (cursor === undefined)
|
|
133
133
|
throw typeError(`Promise.${name} expects an array or other synchronous iterable.`);
|
|
134
134
|
const items = [];
|
|
@@ -136,68 +136,68 @@ const invokePromiseMethod = (runner, promises, name, args) => {
|
|
|
136
136
|
const step = yield* cursor.next;
|
|
137
137
|
if (step.done)
|
|
138
138
|
break;
|
|
139
|
-
const item = yield* resolvePromise(
|
|
140
|
-
|
|
139
|
+
const item = yield* resolvePromise(ctx, step.value);
|
|
140
|
+
ctx.pending.markObserved(item);
|
|
141
141
|
items.push(item);
|
|
142
142
|
}
|
|
143
143
|
if (name === "all") {
|
|
144
|
-
return new
|
|
144
|
+
return new Arr(ctx.builtins.Array, yield* settleAfterTurn(Effect.all(items.map((item) => Effect.flatten(ctx.pending.await(item))), { concurrency: "unbounded" })));
|
|
145
145
|
}
|
|
146
146
|
if (name === "allSettled") {
|
|
147
147
|
const outcomes = [];
|
|
148
148
|
for (const item of items) {
|
|
149
|
-
const exit = yield*
|
|
149
|
+
const exit = yield* ctx.pending.await(item);
|
|
150
150
|
if (Exit.isSuccess(exit)) {
|
|
151
|
-
outcomes.push(record(
|
|
151
|
+
outcomes.push(record(ctx.builtins.Object, { status: "fulfilled", value: exit.value }));
|
|
152
152
|
continue;
|
|
153
153
|
}
|
|
154
154
|
if (Cause.hasInterruptsOnly(exit.cause))
|
|
155
155
|
return yield* Effect.failCause(exit.cause);
|
|
156
|
-
outcomes.push(record(
|
|
156
|
+
outcomes.push(record(ctx.builtins.Object, {
|
|
157
157
|
status: "rejected",
|
|
158
|
-
reason: materialize(
|
|
158
|
+
reason: materialize(ctx, Cause.squash(exit.cause)),
|
|
159
159
|
}));
|
|
160
160
|
}
|
|
161
161
|
yield* Effect.yieldNow;
|
|
162
|
-
return new
|
|
162
|
+
return new Arr(ctx.builtins.Array, outcomes);
|
|
163
163
|
}
|
|
164
164
|
if (name === "race") {
|
|
165
165
|
if (items.length === 0) {
|
|
166
166
|
throw typeError("Promise.race([]) would never settle; provide at least one promise or value.");
|
|
167
167
|
}
|
|
168
|
-
return yield* settleAfterTurn(Effect.flatten(Effect.raceAll(items.map((item) =>
|
|
168
|
+
return yield* settleAfterTurn(Effect.flatten(Effect.raceAll(items.map((item) => ctx.pending.await(item)))));
|
|
169
169
|
}
|
|
170
|
-
const flipped = items.map((item) => Effect.flatMap(
|
|
170
|
+
const flipped = items.map((item) => Effect.flatMap(ctx.pending.await(item), (exit) => {
|
|
171
171
|
if (Exit.isSuccess(exit))
|
|
172
172
|
return Effect.fail(new PromiseAnyFulfilled(exit.value));
|
|
173
173
|
if (Cause.hasInterruptsOnly(exit.cause))
|
|
174
174
|
return Effect.failCause(exit.cause);
|
|
175
|
-
return Effect.succeed(materialize(
|
|
175
|
+
return Effect.succeed(materialize(ctx, Cause.squash(exit.cause)));
|
|
176
176
|
}));
|
|
177
|
-
return yield* settleAfterTurn(Effect.all(flipped, { concurrency: "unbounded" }).pipe(Effect.flatMap((reasons) => Effect.fail(new
|
|
177
|
+
return yield* settleAfterTurn(Effect.all(flipped, { concurrency: "unbounded" }).pipe(Effect.flatMap((reasons) => Effect.fail(new Throw(createAggregateErrorValue(ctx, reasons, "All promises were rejected")))), Effect.catch((error) => error instanceof PromiseAnyFulfilled ? Effect.succeed(error.value) : Effect.fail(error))));
|
|
178
178
|
}));
|
|
179
179
|
};
|
|
180
|
-
const instanceMethod = (
|
|
180
|
+
const instanceMethod = (ctx, name, thisValue, args) => {
|
|
181
181
|
const method = `Promise.prototype.${name}`;
|
|
182
|
-
const promise = receiver(
|
|
183
|
-
|
|
182
|
+
const promise = receiver(PromiseObj, thisValue, method);
|
|
183
|
+
ctx.pending.markObserved(promise);
|
|
184
184
|
if (name === "finally") {
|
|
185
|
-
return chainFinally(
|
|
185
|
+
return chainFinally(ctx, promise, reactionHandler(args[0], method), method);
|
|
186
186
|
}
|
|
187
187
|
const onFulfilled = name === "then" ? reactionHandler(args[0], method) : undefined;
|
|
188
188
|
const onRejected = reactionHandler(name === "then" ? args[1] : args[0], method);
|
|
189
|
-
return chainReaction(
|
|
189
|
+
return chainReaction(ctx, promise, onFulfilled, onRejected, method);
|
|
190
190
|
};
|
|
191
|
-
const constructPromise = (
|
|
192
|
-
if (!(executor instanceof
|
|
191
|
+
const constructPromise = (ctx, executor) => {
|
|
192
|
+
if (!(executor instanceof Fn)) {
|
|
193
193
|
throw typeError("new Promise(...) expects an executor function (e.g. new Promise((resolve, reject) => { ... })).");
|
|
194
194
|
}
|
|
195
195
|
return Effect.gen(function* () {
|
|
196
196
|
const deferred = Deferred.makeUnsafe();
|
|
197
|
-
const promise = yield*
|
|
198
|
-
const resolve = capability(
|
|
199
|
-
const reject = capability(
|
|
200
|
-
const executed = yield* Effect.exit(
|
|
197
|
+
const promise = yield* ctx.pending.createWithSelf((self) => Effect.flatMap(Deferred.await(deferred), (value) => resolvePromiseValue(ctx, value, self)));
|
|
198
|
+
const resolve = capability(ctx, "resolve", (value) => Deferred.doneUnsafe(deferred, Exit.succeed(value)));
|
|
199
|
+
const reject = capability(ctx, "reject", (value) => Deferred.doneUnsafe(deferred, Exit.fail(new Throw(value))));
|
|
200
|
+
const executed = yield* Effect.exit(ctx.call(executor, undefined, [resolve, reject]));
|
|
201
201
|
if (!Exit.isSuccess(executed)) {
|
|
202
202
|
if (Cause.hasInterruptsOnly(executed.cause))
|
|
203
203
|
return yield* Effect.failCause(executed.cause);
|
|
@@ -223,58 +223,58 @@ const reactionHandler = (value, method) => {
|
|
|
223
223
|
return undefined;
|
|
224
224
|
};
|
|
225
225
|
// Teardown bypasses handlers; settled reactions yield once so handlers never run inline.
|
|
226
|
-
const reactionExit = (
|
|
227
|
-
const exit = yield*
|
|
226
|
+
const reactionExit = (ctx, source) => Effect.gen(function* () {
|
|
227
|
+
const exit = yield* ctx.pending.await(source);
|
|
228
228
|
if (!Exit.isSuccess(exit) && Cause.hasInterruptsOnly(exit.cause))
|
|
229
229
|
return yield* Effect.failCause(exit.cause);
|
|
230
230
|
yield* Effect.yieldNow;
|
|
231
231
|
return exit;
|
|
232
232
|
});
|
|
233
|
-
const chainReaction = (
|
|
234
|
-
return
|
|
235
|
-
const exit = yield* reactionExit(
|
|
233
|
+
const chainReaction = (ctx, source, onFulfilled, onRejected, method) => {
|
|
234
|
+
return ctx.pending.createWithSelf((self) => Effect.gen(function* () {
|
|
235
|
+
const exit = yield* reactionExit(ctx, source);
|
|
236
236
|
const handler = Exit.isSuccess(exit) ? onFulfilled : onRejected;
|
|
237
237
|
if (handler === undefined)
|
|
238
238
|
return yield* exit;
|
|
239
|
-
const input = Exit.isSuccess(exit) ? exit.value : materialize(
|
|
240
|
-
const result = yield* applyCollectionCallback(
|
|
241
|
-
return yield* resolvePromiseValue(
|
|
239
|
+
const input = Exit.isSuccess(exit) ? exit.value : materialize(ctx, Cause.squash(exit.cause));
|
|
240
|
+
const result = yield* applyCollectionCallback(ctx, handler, method)([input]);
|
|
241
|
+
return yield* resolvePromiseValue(ctx, result, self);
|
|
242
242
|
}));
|
|
243
243
|
};
|
|
244
|
-
const chainFinally = (
|
|
245
|
-
const exit = yield* reactionExit(
|
|
244
|
+
const chainFinally = (ctx, source, cleanup, method) => ctx.pending.create(Effect.gen(function* () {
|
|
245
|
+
const exit = yield* reactionExit(ctx, source);
|
|
246
246
|
if (cleanup !== undefined) {
|
|
247
|
-
const result = yield* applyCollectionCallback(
|
|
248
|
-
const intermediate = yield*
|
|
249
|
-
yield*
|
|
247
|
+
const result = yield* applyCollectionCallback(ctx, cleanup, method)([]);
|
|
248
|
+
const intermediate = yield* ctx.pending.create(Effect.gen(function* () {
|
|
249
|
+
yield* ctx.await(yield* resolvePromise(ctx, result));
|
|
250
250
|
return yield* exit;
|
|
251
251
|
}));
|
|
252
|
-
return yield*
|
|
252
|
+
return yield* ctx.await(intermediate);
|
|
253
253
|
}
|
|
254
254
|
return yield* exit;
|
|
255
255
|
}));
|
|
256
|
-
export const promiseGlobal = (
|
|
257
|
-
const
|
|
258
|
-
const proto =
|
|
259
|
-
const promise = constructor(
|
|
256
|
+
export const promiseGlobal = (ctx) => {
|
|
257
|
+
const builtins = ctx.builtins;
|
|
258
|
+
const proto = builtins.Promise;
|
|
259
|
+
const promise = constructor(builtins, proto, {
|
|
260
260
|
name: "Promise",
|
|
261
261
|
length: 1,
|
|
262
262
|
call: requiresNew("Promise"),
|
|
263
|
-
construct: (args) => constructPromise(
|
|
263
|
+
construct: (args) => constructPromise(ctx, args[0]),
|
|
264
264
|
});
|
|
265
265
|
// Combinators are not callbacks: `[p].map(Promise.resolve)` must ask for an arrow function.
|
|
266
266
|
for (const name of promiseStatics) {
|
|
267
|
-
define(promise, name, native(
|
|
267
|
+
define(promise, name, native(builtins, {
|
|
268
268
|
name,
|
|
269
269
|
length: 1,
|
|
270
|
-
call: (_, args) => invokePromiseMethod(
|
|
270
|
+
call: (_, args) => invokePromiseMethod(ctx, name, args),
|
|
271
271
|
callback: false,
|
|
272
272
|
}), hidden);
|
|
273
273
|
}
|
|
274
|
-
methods(
|
|
275
|
-
["then", 2, (thisValue, args) => instanceMethod(
|
|
276
|
-
["catch", 1, (thisValue, args) => instanceMethod(
|
|
277
|
-
["finally", 1, (thisValue, args) => instanceMethod(
|
|
274
|
+
methods(builtins, proto, [
|
|
275
|
+
["then", 2, (thisValue, args) => instanceMethod(ctx, "then", thisValue, args)],
|
|
276
|
+
["catch", 1, (thisValue, args) => instanceMethod(ctx, "catch", thisValue, args)],
|
|
277
|
+
["finally", 1, (thisValue, args) => instanceMethod(ctx, "finally", thisValue, args)],
|
|
278
278
|
]);
|
|
279
279
|
return promise;
|
|
280
280
|
};
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { ToolReference } from "../tool-runtime.js";
|
|
2
2
|
import { invalidData } from "./model.js";
|
|
3
|
-
import { Callable, getOwn, isWrapper, ownKeys,
|
|
3
|
+
import { Callable, getOwn, isWrapper, ownKeys, Arr, Bytes, DateObj, GeneratorObj, Handle, MapObj, Obj, PromiseObj, RegExpObj, SetObj, URLObj, URLSearchParamsObj, } from "./objects.js";
|
|
4
4
|
/** Values that cannot cross the data boundary. */
|
|
5
5
|
export const isRuntimeReference = (value) => value instanceof Callable ||
|
|
6
|
-
value instanceof
|
|
7
|
-
value instanceof
|
|
6
|
+
value instanceof GeneratorObj ||
|
|
7
|
+
value instanceof Handle ||
|
|
8
8
|
value instanceof ToolReference ||
|
|
9
|
-
value instanceof
|
|
9
|
+
value instanceof PromiseObj ||
|
|
10
10
|
isWrapper(value);
|
|
11
11
|
function* childValues(value) {
|
|
12
|
-
if (!(value instanceof
|
|
12
|
+
if (!(value instanceof Obj))
|
|
13
13
|
return;
|
|
14
14
|
for (const key of ownKeys(value))
|
|
15
15
|
yield getOwn(value, key);
|
|
@@ -46,27 +46,29 @@ export const rejectCircularInsertion = (container, value, label, seen = new Set(
|
|
|
46
46
|
export const describeValue = (value) => {
|
|
47
47
|
if (value === null || value === undefined)
|
|
48
48
|
return String(value);
|
|
49
|
-
if (value instanceof
|
|
49
|
+
if (value instanceof Arr)
|
|
50
50
|
return "an array";
|
|
51
|
-
if (value instanceof
|
|
51
|
+
if (value instanceof PromiseObj)
|
|
52
52
|
return "an un-awaited Promise";
|
|
53
53
|
if (value instanceof ToolReference)
|
|
54
54
|
return "a tool reference";
|
|
55
|
-
if (value instanceof
|
|
55
|
+
if (value instanceof DateObj)
|
|
56
56
|
return "a Date";
|
|
57
|
-
if (value instanceof
|
|
57
|
+
if (value instanceof RegExpObj)
|
|
58
58
|
return "a RegExp";
|
|
59
|
-
if (value instanceof
|
|
59
|
+
if (value instanceof MapObj)
|
|
60
60
|
return "a Map";
|
|
61
|
-
if (value instanceof
|
|
61
|
+
if (value instanceof SetObj)
|
|
62
62
|
return "a Set";
|
|
63
|
-
if (value instanceof
|
|
63
|
+
if (value instanceof URLObj)
|
|
64
64
|
return "a URL";
|
|
65
|
-
if (value instanceof
|
|
65
|
+
if (value instanceof URLSearchParamsObj)
|
|
66
66
|
return "a URLSearchParams";
|
|
67
|
-
if (value instanceof
|
|
67
|
+
if (value instanceof Bytes)
|
|
68
|
+
return "a Uint8Array";
|
|
69
|
+
if (value instanceof GeneratorObj)
|
|
68
70
|
return "a generator";
|
|
69
|
-
if (value instanceof
|
|
71
|
+
if (value instanceof Handle)
|
|
70
72
|
return `a ${value.instance.constructor.name}`;
|
|
71
73
|
if (isRuntimeReference(value))
|
|
72
74
|
return "a function";
|
package/dist/openapi/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { HttpClient } from "effect/unstable/http";
|
|
3
3
|
import type { Tool, JsonSchema } from "../tool.js";
|
|
4
|
-
/** A parsed OpenAPI 3.x document. YAML must be parsed by the
|
|
4
|
+
/** A parsed OpenAPI 3.x document. YAML must be parsed by the ctx. */
|
|
5
5
|
export type Document = Record<string, unknown>;
|
|
6
6
|
/** The operation identity handed to auth resolution and errors. */
|
|
7
7
|
export type Operation = {
|
package/dist/stdlib/array.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
-
import {
|
|
3
|
-
export declare const sortArray: <R>(
|
|
4
|
-
export declare const arrayGlobal: <R>(
|
|
2
|
+
import type { Interpreter } from "../interpreter/interpreter.js";
|
|
3
|
+
export declare const sortArray: <R>(ctx: Interpreter<R>, target: Array<unknown>, comparator: unknown, name: string) => Effect.Effect<Array<unknown>, unknown, R>;
|
|
4
|
+
export declare const arrayGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native<R>;
|
package/dist/stdlib/array.js
CHANGED
|
@@ -2,13 +2,13 @@ import { Effect } from "effect";
|
|
|
2
2
|
import { constructor, methods, prototypeFrom, receiver } from "../interpreter/native.js";
|
|
3
3
|
import { checkArrayLength, checkStringLength, MAX_ARRAY_LENGTH } from "../interpreter/limits.js";
|
|
4
4
|
import { invalidData, rangeError, typeError } from "../interpreter/model.js";
|
|
5
|
-
import { get,
|
|
5
|
+
import { get, Arr, GeneratorObj, Obj } from "../interpreter/objects.js";
|
|
6
6
|
import { describeValue, rejectCircularInsertion } from "../interpreter/references.js";
|
|
7
|
-
import { applyCollectionCallback, preserveConsumerError } from "../interpreter/
|
|
7
|
+
import { applyCollectionCallback, preserveConsumerError } from "../interpreter/callback.js";
|
|
8
8
|
import { compareText } from "../tool-runtime.js";
|
|
9
9
|
import { coerceToNumber, coerceToString } from "./value.js";
|
|
10
10
|
const arrayLikeSource = (source) => {
|
|
11
|
-
if (source instanceof
|
|
11
|
+
if (source instanceof Obj && typeof get(source, "length") === "number") {
|
|
12
12
|
const length = get(source, "length");
|
|
13
13
|
const normalized = Number.isNaN(length) || length <= 0 ? 0 : Math.trunc(length);
|
|
14
14
|
checkArrayLength(normalized);
|
|
@@ -16,14 +16,14 @@ const arrayLikeSource = (source) => {
|
|
|
16
16
|
}
|
|
17
17
|
throw invalidData(`Array.from expects an array, string, Map, Set, or array-like value, received ${describeValue(source)}.`);
|
|
18
18
|
};
|
|
19
|
-
const arrayFrom = (
|
|
19
|
+
const arrayFrom = (ctx, args) => {
|
|
20
20
|
const source = args[0];
|
|
21
|
-
const proto =
|
|
22
|
-
const apply = args.length < 2 || args[1] === undefined ? undefined : applyCollectionCallback(
|
|
21
|
+
const proto = ctx.builtins.Array;
|
|
22
|
+
const apply = args.length < 2 || args[1] === undefined ? undefined : applyCollectionCallback(ctx, args[1], "Array.from");
|
|
23
23
|
return Effect.gen(function* () {
|
|
24
|
-
const cursor = yield*
|
|
24
|
+
const cursor = yield* ctx.iterate(source);
|
|
25
25
|
if (cursor === undefined) {
|
|
26
|
-
if (source instanceof
|
|
26
|
+
if (source instanceof GeneratorObj) {
|
|
27
27
|
throw typeError("Array.from expects a synchronous iterable or array-like value.");
|
|
28
28
|
}
|
|
29
29
|
const arrayLike = arrayLikeSource(source);
|
|
@@ -32,24 +32,24 @@ const arrayFrom = (runner, args) => {
|
|
|
32
32
|
const item = get(arrayLike.source, index);
|
|
33
33
|
values.push(apply === undefined ? item : yield* apply([item, index]));
|
|
34
34
|
}
|
|
35
|
-
return new
|
|
35
|
+
return new Arr(proto, values);
|
|
36
36
|
}
|
|
37
37
|
const values = [];
|
|
38
38
|
let index = 0;
|
|
39
39
|
while (true) {
|
|
40
40
|
const step = yield* cursor.next;
|
|
41
41
|
if (step.done)
|
|
42
|
-
return new
|
|
42
|
+
return new Arr(proto, values);
|
|
43
43
|
values.push(apply === undefined ? step.value : yield* preserveConsumerError(cursor, apply([step.value, index])));
|
|
44
44
|
index += 1;
|
|
45
45
|
}
|
|
46
46
|
});
|
|
47
47
|
};
|
|
48
|
-
export const sortArray = (
|
|
48
|
+
export const sortArray = (ctx, target, comparator, name) => {
|
|
49
49
|
if (comparator === undefined) {
|
|
50
50
|
return Effect.sync(() => [...target].sort((a, b) => compareText(coerceToString(a), coerceToString(b))));
|
|
51
51
|
}
|
|
52
|
-
const apply = applyCollectionCallback(
|
|
52
|
+
const apply = applyCollectionCallback(ctx, comparator, name);
|
|
53
53
|
const mergeSort = (items) => {
|
|
54
54
|
if (items.length <= 1)
|
|
55
55
|
return Effect.succeed(items);
|
|
@@ -76,33 +76,33 @@ export const sortArray = (runner, target, comparator, name) => {
|
|
|
76
76
|
return Effect.map(mergeSort(defined), (items) => [...items, ...Array(undefinedCount).fill(undefined)]);
|
|
77
77
|
};
|
|
78
78
|
// Array constructs identically with or without new, like JS.
|
|
79
|
-
export const arrayGlobal = (
|
|
80
|
-
const
|
|
81
|
-
const proto =
|
|
82
|
-
const wrap = (items) => new
|
|
79
|
+
export const arrayGlobal = (ctx) => {
|
|
80
|
+
const builtins = ctx.builtins;
|
|
81
|
+
const proto = builtins.Array;
|
|
82
|
+
const wrap = (items) => new Arr(proto, items);
|
|
83
83
|
const construct = (args, into) => {
|
|
84
84
|
if (args.length !== 1)
|
|
85
|
-
return new
|
|
85
|
+
return new Arr(into, [...args]);
|
|
86
86
|
const first = args[0];
|
|
87
87
|
if (typeof first !== "number")
|
|
88
|
-
return new
|
|
88
|
+
return new Arr(into, [first]);
|
|
89
89
|
if (!Number.isInteger(first) || first < 0 || first > MAX_ARRAY_LENGTH)
|
|
90
90
|
throw rangeError("Invalid array length.");
|
|
91
91
|
// Sparse like JS: Array(3) has holes, and combinator loops already skip them.
|
|
92
|
-
return new
|
|
92
|
+
return new Arr(into, new Array(first));
|
|
93
93
|
};
|
|
94
|
-
const array = constructor(
|
|
94
|
+
const array = constructor(builtins, proto, {
|
|
95
95
|
name: "Array",
|
|
96
96
|
length: 1,
|
|
97
97
|
call: (_, args) => Effect.sync(() => construct(args, proto)),
|
|
98
98
|
construct: (args, newTarget) => Effect.sync(() => construct(args, prototypeFrom(newTarget, proto))),
|
|
99
99
|
});
|
|
100
|
-
methods(
|
|
101
|
-
["isArray", 1, (_, args) => args[0] instanceof
|
|
100
|
+
methods(builtins, array, [
|
|
101
|
+
["isArray", 1, (_, args) => args[0] instanceof Arr],
|
|
102
102
|
["of", 0, (_, args) => wrap([...args])],
|
|
103
|
-
["from", 1, (_, args) => arrayFrom(
|
|
103
|
+
["from", 1, (_, args) => arrayFrom(ctx, args)],
|
|
104
104
|
]);
|
|
105
|
-
const self = (thisValue, name) => receiver(
|
|
105
|
+
const self = (thisValue, name) => receiver(Arr, thisValue, `Array.prototype.${name}`);
|
|
106
106
|
const optNumber = (name, value, label) => {
|
|
107
107
|
if (value === undefined)
|
|
108
108
|
return undefined;
|
|
@@ -117,10 +117,10 @@ export const arrayGlobal = (runner) => {
|
|
|
117
117
|
length,
|
|
118
118
|
(thisValue, args) => {
|
|
119
119
|
const target = self(thisValue, name);
|
|
120
|
-
return body(target.items, target, applyCollectionCallback(
|
|
120
|
+
return body(target.items, target, applyCollectionCallback(ctx, args[0], `Array.${name}`), args);
|
|
121
121
|
},
|
|
122
122
|
];
|
|
123
|
-
methods(
|
|
123
|
+
methods(builtins, proto, [
|
|
124
124
|
[
|
|
125
125
|
"join",
|
|
126
126
|
1,
|
|
@@ -179,7 +179,7 @@ export const arrayGlobal = (runner) => {
|
|
|
179
179
|
"concat",
|
|
180
180
|
1,
|
|
181
181
|
(thisValue, args) => {
|
|
182
|
-
const joined = self(thisValue, "concat").items.concat(...args.map((item) => (item instanceof
|
|
182
|
+
const joined = self(thisValue, "concat").items.concat(...args.map((item) => (item instanceof Arr ? item.items : item)));
|
|
183
183
|
checkArrayLength(joined.length);
|
|
184
184
|
return wrap(joined);
|
|
185
185
|
},
|
|
@@ -188,7 +188,7 @@ export const arrayGlobal = (runner) => {
|
|
|
188
188
|
"flat",
|
|
189
189
|
0,
|
|
190
190
|
(thisValue, args) => {
|
|
191
|
-
const flatten = (items, depth) => items.flatMap((item) => (item instanceof
|
|
191
|
+
const flatten = (items, depth) => items.flatMap((item) => (item instanceof Arr && depth > 0 ? flatten(item.items, depth - 1) : [item]));
|
|
192
192
|
const flattened = flatten(self(thisValue, "flat").items, optNumber("flat", args[0], "depth") ?? 1);
|
|
193
193
|
checkArrayLength(flattened.length);
|
|
194
194
|
return wrap(flattened);
|
|
@@ -212,7 +212,7 @@ export const arrayGlobal = (runner) => {
|
|
|
212
212
|
const length = items.length;
|
|
213
213
|
const holeCount = Array.from({ length }, (_, index) => Object.hasOwn(items, index)).filter((o) => !o).length;
|
|
214
214
|
const itemCount = length - holeCount;
|
|
215
|
-
return Effect.map(sortArray(
|
|
215
|
+
return Effect.map(sortArray(ctx, items, args[0], "Array.sort"), (sorted) => {
|
|
216
216
|
sorted.slice(0, itemCount).forEach((item, index) => {
|
|
217
217
|
items[index] = item;
|
|
218
218
|
});
|
|
@@ -226,7 +226,7 @@ export const arrayGlobal = (runner) => {
|
|
|
226
226
|
[
|
|
227
227
|
"toSorted",
|
|
228
228
|
1,
|
|
229
|
-
(thisValue, args) => Effect.map(sortArray(
|
|
229
|
+
(thisValue, args) => Effect.map(sortArray(ctx, self(thisValue, "toSorted").items, args[0], "Array.toSorted"), wrap),
|
|
230
230
|
],
|
|
231
231
|
["toReversed", 0, (thisValue) => wrap([...self(thisValue, "toReversed").items].reverse())],
|
|
232
232
|
[
|
|
@@ -342,7 +342,7 @@ export const arrayGlobal = (runner) => {
|
|
|
342
342
|
if (!(index in target))
|
|
343
343
|
continue;
|
|
344
344
|
const mapped = yield* apply([target[index], index, receiver]);
|
|
345
|
-
if (mapped instanceof
|
|
345
|
+
if (mapped instanceof Arr)
|
|
346
346
|
values.push(...mapped.items);
|
|
347
347
|
else
|
|
348
348
|
values.push(mapped);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Obj } from "../interpreter/objects.js";
|
|
2
|
+
import type { Interpreter } from "../interpreter/interpreter.js";
|
|
3
|
+
export declare const uint8ArrayGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native<R>;
|
|
4
|
+
export declare const textEncoderGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native<R>;
|
|
5
|
+
/** A `TextDecoder` holding the host decoder its label and options configured. */
|
|
6
|
+
export declare class TextDecoderObj extends Obj {
|
|
7
|
+
readonly decoder: TextDecoder;
|
|
8
|
+
constructor(proto: Obj, decoder: TextDecoder);
|
|
9
|
+
}
|
|
10
|
+
export declare const textDecoderGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native<R>;
|