@opencode/codemode 2.1.4 → 2.2.1

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.
Files changed (55) hide show
  1. package/README.md +33 -4
  2. package/dist/codemode.d.ts +9 -7
  3. package/dist/codemode.js +1 -1
  4. package/dist/data.d.ts +5 -4
  5. package/dist/data.js +7 -4
  6. package/dist/extension.d.ts +17 -4
  7. package/dist/extension.js +8 -0
  8. package/dist/interpreter/callback.d.ts +10 -10
  9. package/dist/interpreter/callback.js +43 -31
  10. package/dist/interpreter/errors.d.ts +1 -1
  11. package/dist/interpreter/errors.js +12 -15
  12. package/dist/interpreter/execute.js +59 -29
  13. package/dist/interpreter/extensions.d.ts +2 -2
  14. package/dist/interpreter/extensions.js +46 -18
  15. package/dist/interpreter/generators.js +162 -19
  16. package/dist/interpreter/globals.js +12 -9
  17. package/dist/interpreter/interpreter.d.ts +20 -13
  18. package/dist/interpreter/interpreter.js +1329 -1147
  19. package/dist/interpreter/intrinsics.js +1 -2
  20. package/dist/interpreter/model.d.ts +4 -7
  21. package/dist/interpreter/model.js +1 -11
  22. package/dist/interpreter/native.d.ts +7 -8
  23. package/dist/interpreter/native.js +4 -10
  24. package/dist/interpreter/objects.d.ts +47 -21
  25. package/dist/interpreter/objects.js +33 -12
  26. package/dist/interpreter/promises.d.ts +15 -53
  27. package/dist/interpreter/promises.js +226 -401
  28. package/dist/interpreter/runner.d.ts +188 -0
  29. package/dist/interpreter/runner.js +492 -0
  30. package/dist/stdlib/array.d.ts +3 -3
  31. package/dist/stdlib/array.js +106 -102
  32. package/dist/stdlib/bytes.d.ts +3 -3
  33. package/dist/stdlib/bytes.js +46 -41
  34. package/dist/stdlib/collections.d.ts +5 -5
  35. package/dist/stdlib/collections.js +160 -165
  36. package/dist/stdlib/date.d.ts +1 -1
  37. package/dist/stdlib/date.js +28 -20
  38. package/dist/stdlib/headers.d.ts +1 -1
  39. package/dist/stdlib/headers.js +14 -19
  40. package/dist/stdlib/iterator.d.ts +1 -1
  41. package/dist/stdlib/iterator.js +140 -122
  42. package/dist/stdlib/json.js +9 -12
  43. package/dist/stdlib/math.js +10 -10
  44. package/dist/stdlib/number.d.ts +2 -2
  45. package/dist/stdlib/object.d.ts +1 -1
  46. package/dist/stdlib/object.js +21 -22
  47. package/dist/stdlib/regexp.d.ts +1 -1
  48. package/dist/stdlib/regexp.js +2 -3
  49. package/dist/stdlib/string.d.ts +1 -1
  50. package/dist/stdlib/string.js +18 -20
  51. package/dist/stdlib/url.d.ts +5 -5
  52. package/dist/stdlib/url.js +32 -33
  53. package/dist/stdlib/value.d.ts +2 -2
  54. package/dist/stdlib/web.d.ts +2 -2
  55. package/package.json +2 -1
@@ -1,4 +1,3 @@
1
- import { Effect } from "effect";
2
1
  import { constructor, fn, methods } from "../interpreter/native.js";
3
2
  import { checkArrayLength, checkStringLength } from "../interpreter/limits.js";
4
3
  import { invalidData, IteratorSymbol, rangeError, typeError } from "../interpreter/model.js";
@@ -19,7 +18,7 @@ const replaceAllNeedsGlobal = (pattern) => {
19
18
  throw typeError(`String.replaceAll requires a regular expression with the global (g) flag: write /${pattern.source}/${pattern.flags}g, or use String.replace to replace only the first match.`);
20
19
  }
21
20
  };
22
- const replaceWithCallback = (ctx, value, name, args) => {
21
+ function* replaceWithCallback(ctx, value, name, args) {
23
22
  const builtins = ctx.builtins;
24
23
  const apply = applyCollectionCallback(ctx, args[1], `String.${name}`);
25
24
  const matches = [];
@@ -51,18 +50,16 @@ const replaceWithCallback = (ctx, value, name, args) => {
51
50
  else
52
51
  value.replaceAll(search, collect);
53
52
  }
54
- return Effect.gen(function* () {
55
- const output = [];
56
- let end = 0;
57
- for (const match of matches) {
58
- const replacement = yield* apply(match.args);
59
- output.push(value.slice(end, match.offset), coerceToString(replacement));
60
- end = match.offset + match.match.length;
61
- }
62
- output.push(value.slice(end));
63
- return output.join("");
64
- });
65
- };
53
+ const output = [];
54
+ let end = 0;
55
+ for (const match of matches) {
56
+ const replacement = yield* apply(match.args);
57
+ output.push(value.slice(end, match.offset), coerceToString(replacement));
58
+ end = match.offset + match.match.length;
59
+ }
60
+ output.push(value.slice(end));
61
+ return output.join("");
62
+ }
66
63
  export const stringGlobal = (ctx) => {
67
64
  const builtins = ctx.builtins;
68
65
  const string = constructor(builtins, builtins.String, {
@@ -81,7 +78,7 @@ export const stringGlobal = (ctx) => {
81
78
  [
82
79
  "raw",
83
80
  1,
84
- (_, args) => {
81
+ function* (_, args) {
85
82
  const template = args[0];
86
83
  const raw = template instanceof Obj ? get(template, "raw") : undefined;
87
84
  if (!(raw instanceof Obj))
@@ -90,11 +87,12 @@ export const stringGlobal = (ctx) => {
90
87
  checkArrayLength(count);
91
88
  // Each literal is followed by its substitution, except the last literal, or when substitutions run out.
92
89
  const parts = Array.from({ length: count }, (_, index) => index + 1 < count && index + 1 < args.length ? [get(raw, index), args[index + 1]] : [get(raw, index)]).flat();
93
- return Effect.map(Effect.forEach(parts, (part) => toPrimitiveString(ctx, part)), (strings) => {
94
- const output = strings.join("");
95
- checkStringLength(output.length);
96
- return output;
97
- });
90
+ const strings = [];
91
+ for (const part of parts)
92
+ strings.push(yield* toPrimitiveString(ctx, part));
93
+ const output = strings.join("");
94
+ checkStringLength(output.length);
95
+ return output;
98
96
  },
99
97
  ],
100
98
  ]);
@@ -1,13 +1,13 @@
1
- import { Effect } from "effect";
1
+ import type { Eval } from "../interpreter/runner.js";
2
2
  import { type Value } from "../interpreter/objects.js";
3
3
  import type { Interpreter } from "../interpreter/interpreter.js";
4
4
  type UriFunction = "encodeURI" | "encodeURIComponent" | "decodeURI" | "decodeURIComponent";
5
- export declare const uriGlobal: <R>(ctx: Interpreter<R>, name: UriFunction) => import("../interpreter/objects.js").Native<R>;
6
- export declare const urlGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native<R>;
5
+ export declare const uriGlobal: <R>(ctx: Interpreter<R>, name: UriFunction) => import("../interpreter/objects.js").Native;
6
+ export declare const urlGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
7
7
  /**
8
8
  * Reads a synchronous iterable of `[name, value]` pairs as strings; `undefined` when `init` is not iterable. As in
9
9
  * WebIDL, the whole sequence is converted before any pair's length is checked.
10
10
  */
11
- export declare const readPairs: <R>(ctx: Interpreter<R>, init: Value, label: string) => Effect.Effect<Array<[string, string]> | undefined, unknown, R>;
12
- export declare const urlSearchParamsGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native<R>;
11
+ export declare function readPairs<R>(ctx: Interpreter<R>, init: Value, label: string): Eval<Array<[string, string]> | undefined>;
12
+ export declare const urlSearchParamsGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
13
13
  export {};
@@ -1,4 +1,3 @@
1
- import { Effect } from "effect";
2
1
  import { constructor, fn, methods, prototypeFrom, receiver, requiresNew } from "../interpreter/native.js";
3
2
  import { IteratorSymbol, PendingThrow, typeError, uriError } from "../interpreter/model.js";
4
3
  import { define, defineAccessor, entries, get, hidden, Arr, hostIterator, Obj, URLObj, URLSearchParamsObj, coerceToString, isRuntimeReference, } from "../interpreter/objects.js";
@@ -52,7 +51,7 @@ export const urlGlobal = (ctx) => {
52
51
  name: "URL",
53
52
  length: 1,
54
53
  call: requiresNew("URL"),
55
- construct: (args, newTarget) => Effect.sync(() => construct(args, prototypeFrom(newTarget, proto))),
54
+ construct: (args, newTarget) => construct(args, prototypeFrom(newTarget, proto)),
56
55
  });
57
56
  const parse = (name) => [
58
57
  name,
@@ -95,29 +94,33 @@ export const urlGlobal = (ctx) => {
95
94
  ]);
96
95
  return url;
97
96
  };
98
- const readPair = (ctx, value, label) => Effect.gen(function* () {
97
+ function* readPair(ctx, value, label) {
99
98
  const cursor = yield* ctx.iterate(value);
100
99
  if (cursor === undefined)
101
100
  throw typeError(`${label} expects iterable [name, value] pairs.`);
102
101
  const items = [];
103
102
  while (true) {
104
- const step = yield* cursor.next;
103
+ const step = yield* cursor.next();
105
104
  if (step.done)
106
105
  return items;
107
- items.push(yield* preserveConsumerError(cursor.close, Effect.sync(() => coerceToString(step.value))));
106
+ items.push(yield* preserveConsumerError(cursor.close, pairString(step.value)));
108
107
  }
109
- });
108
+ }
109
+ // A step, so a failing conversion closes the pair's iterator.
110
+ function* pairString(value) {
111
+ return coerceToString(value);
112
+ }
110
113
  /**
111
114
  * Reads a synchronous iterable of `[name, value]` pairs as strings; `undefined` when `init` is not iterable. As in
112
115
  * WebIDL, the whole sequence is converted before any pair's length is checked.
113
116
  */
114
- export const readPairs = (ctx, init, label) => Effect.gen(function* () {
117
+ export function* readPairs(ctx, init, label) {
115
118
  const cursor = yield* ctx.iterate(init);
116
119
  if (cursor === undefined)
117
120
  return undefined;
118
121
  const pairs = [];
119
122
  while (true) {
120
- const step = yield* cursor.next;
123
+ const step = yield* cursor.next();
121
124
  if (step.done) {
122
125
  if (pairs.some((entry) => entry.length !== 2))
123
126
  throw typeError(`${label} expects iterable [name, value] pairs.`);
@@ -125,31 +128,29 @@ export const readPairs = (ctx, init, label) => Effect.gen(function* () {
125
128
  }
126
129
  pairs.push(yield* preserveConsumerError(cursor.close, readPair(ctx, step.value, label)));
127
130
  }
128
- });
129
- const constructURLSearchParams = (ctx, init, proto) => {
131
+ }
132
+ function* constructURLSearchParams(ctx, init, proto) {
130
133
  const wrap = (params) => new URLSearchParamsObj(proto, params);
131
134
  if (init === undefined)
132
- return Effect.succeed(wrap(new URLSearchParams()));
135
+ return wrap(new URLSearchParams());
133
136
  if (init instanceof URLSearchParamsObj)
134
- return Effect.succeed(wrap(new URLSearchParams(init.params)));
137
+ return wrap(new URLSearchParams(init.params));
135
138
  if (typeof init === "string")
136
- return Effect.succeed(wrap(new URLSearchParams(init)));
139
+ return wrap(new URLSearchParams(init));
137
140
  if (init === null || typeof init === "number" || typeof init === "boolean") {
138
- return Effect.succeed(wrap(new URLSearchParams(coerceToString(init))));
141
+ return wrap(new URLSearchParams(coerceToString(init)));
139
142
  }
140
- return Effect.gen(function* () {
141
- const pairs = yield* readPairs(ctx, init, "new URLSearchParams(...)");
142
- if (pairs !== undefined)
143
- return wrap(new URLSearchParams(pairs));
144
- if (isRuntimeReference(init)) {
145
- throw typeError("new URLSearchParams(...) expects a query string, data object, or synchronous iterable pairs.");
146
- }
147
- if (!(init instanceof Obj)) {
148
- throw typeError("new URLSearchParams(...) expects a query string, data object, iterable pairs, or URLSearchParams.");
149
- }
150
- return wrap(new URLSearchParams(Object.fromEntries(entries(init).map(([key, value]) => [key, coerceToString(value)]))));
151
- });
152
- };
143
+ const pairs = yield* readPairs(ctx, init, "new URLSearchParams(...)");
144
+ if (pairs !== undefined)
145
+ return wrap(new URLSearchParams(pairs));
146
+ if (isRuntimeReference(init)) {
147
+ throw typeError("new URLSearchParams(...) expects a query string, data object, or synchronous iterable pairs.");
148
+ }
149
+ if (!(init instanceof Obj)) {
150
+ throw typeError("new URLSearchParams(...) expects a query string, data object, iterable pairs, or URLSearchParams.");
151
+ }
152
+ return wrap(new URLSearchParams(Object.fromEntries(entries(init).map(([key, value]) => [key, coerceToString(value)]))));
153
+ }
153
154
  export const urlSearchParamsGlobal = (ctx) => {
154
155
  const builtins = ctx.builtins;
155
156
  const proto = builtins.URLSearchParams;
@@ -239,15 +240,13 @@ export const urlSearchParamsGlobal = (ctx) => {
239
240
  [
240
241
  "forEach",
241
242
  1,
242
- (thisValue, args) => {
243
+ function* (thisValue, args) {
243
244
  requireArgs("forEach", args, 1);
244
245
  const target = self(thisValue, "forEach");
245
246
  const apply = applyCollectionCallback(ctx, args[0], "URLSearchParams.forEach");
246
- return Effect.gen(function* () {
247
- for (const [key, value] of Array.from(target.params.entries()))
248
- yield* apply([value, key, target], args[1]);
249
- return undefined;
250
- });
247
+ for (const [key, value] of Array.from(target.params.entries()))
248
+ yield* apply([value, key, target], args[1]);
249
+ return undefined;
251
250
  },
252
251
  ],
253
252
  ]);
@@ -2,6 +2,6 @@ import { type Native, type Value } from "../interpreter/objects.js";
2
2
  import type { Interpreter } from "../interpreter/interpreter.js";
3
3
  export declare const compoundOperators: Set<string>;
4
4
  export type Coercion = "Number" | "String" | "Boolean" | "parseInt" | "parseFloat" | "isFinite" | "isNaN";
5
- export declare const coerce: <R>(ctx: Interpreter<R>, name: Coercion, args: Array<Value>) => import("effect/Effect").Effect<Value, unknown, R> | Value;
5
+ export declare const coerce: <R>(ctx: Interpreter<R>, name: Coercion, args: Array<Value>) => import("../interpreter/objects.js").NativeResult;
6
6
  /** A global coercion function such as `Number` or `parseInt`. */
7
- export declare const coercion: <R>(ctx: Interpreter<R>, name: Coercion, length?: number) => Native<R>;
7
+ export declare const coercion: <R>(ctx: Interpreter<R>, name: Coercion, length?: number) => Native;
@@ -1,5 +1,5 @@
1
1
  import { Obj } from "../interpreter/objects.js";
2
2
  import type { Interpreter } from "../interpreter/interpreter.js";
3
- export declare const base64Global: <R>(ctx: Interpreter<R>, name: "atob" | "btoa") => import("../interpreter/objects.js").Native<R>;
4
- export declare const structuredCloneGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native<R>;
3
+ export declare const base64Global: <R>(ctx: Interpreter<R>, name: "atob" | "btoa") => import("../interpreter/objects.js").Native;
4
+ export declare const structuredCloneGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
5
5
  export declare const cryptoGlobal: <R>(ctx: Interpreter<R>) => Obj;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@opencode/codemode",
4
- "version": "2.1.4",
4
+ "version": "2.2.1",
5
5
  "description": "Effect-native confined code execution over schema-described tools",
6
6
  "type": "module",
7
7
  "license": "MIT",
@@ -24,6 +24,7 @@
24
24
  },
25
25
  "scripts": {
26
26
  "build": "bun run script/build.ts",
27
+ "pack": "bun run script/pack.ts",
27
28
  "typecheck": "bun check --no-script",
28
29
  "typecheck:tsc": "tsc --noEmit",
29
30
  "test": "bun test"