@isolated-vm/experimental 0.0.12 → 0.0.13

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 (48) hide show
  1. package/backend/backend.cts +2 -2
  2. package/backend/backend.d.cts +10 -17
  3. package/backend/require.cts +1 -1
  4. package/dist/backend/backend.cjs +2 -2
  5. package/dist/backend/backend.cjs.map +1 -1
  6. package/dist/backend/require.cjs +1 -1
  7. package/dist/backend/require.cjs.map +1 -1
  8. package/dist/frontend/agent.d.ts +3 -3
  9. package/dist/frontend/agent.d.ts.map +1 -1
  10. package/dist/frontend/agent.js +5 -12
  11. package/dist/frontend/agent.js.map +1 -1
  12. package/dist/frontend/module.d.ts +2 -5
  13. package/dist/frontend/module.d.ts.map +1 -1
  14. package/dist/frontend/module.js +20 -33
  15. package/dist/frontend/module.js.map +1 -1
  16. package/dist/frontend/realm.d.ts +1 -3
  17. package/dist/frontend/realm.d.ts.map +1 -1
  18. package/dist/frontend/realm.js +0 -1
  19. package/dist/frontend/realm.js.map +1 -1
  20. package/dist/frontend/reference.d.ts +1 -1
  21. package/dist/frontend/reference.d.ts.map +1 -1
  22. package/dist/test/10.transfer/20.transfer_list.d.ts +2 -0
  23. package/dist/test/10.transfer/20.transfer_list.d.ts.map +1 -0
  24. package/dist/test/10.transfer/20.transfer_list.js +199 -0
  25. package/dist/test/10.transfer/20.transfer_list.js.map +1 -0
  26. package/dist/test/20.intrinsics/15.transfer_return.d.ts +2 -0
  27. package/dist/test/20.intrinsics/15.transfer_return.d.ts.map +1 -0
  28. package/dist/test/20.intrinsics/15.transfer_return.js +189 -0
  29. package/dist/test/20.intrinsics/15.transfer_return.js.map +1 -0
  30. package/dist/test/20.intrinsics/20.module.js +8 -8
  31. package/dist/test/20.intrinsics/20.module.js.map +1 -1
  32. package/dist/test/50.features/50.timers.js +4 -4
  33. package/dist/test/50.features/50.timers.js.map +1 -1
  34. package/dist/tsconfig.tsbuildinfo +1 -1
  35. package/dist/utility/object.d.ts +2 -0
  36. package/dist/utility/object.d.ts.map +1 -0
  37. package/dist/utility/object.js +14 -0
  38. package/dist/utility/object.js.map +1 -0
  39. package/frontend/agent.ts +10 -13
  40. package/frontend/module.ts +25 -31
  41. package/frontend/realm.ts +0 -4
  42. package/frontend/reference.ts +1 -1
  43. package/package.json +9 -10
  44. package/test/10.transfer/20.transfer_list.ts +102 -0
  45. package/test/20.intrinsics/15.transfer_return.ts +109 -0
  46. package/test/20.intrinsics/20.module.ts +8 -8
  47. package/test/50.features/50.timers.ts +4 -4
  48. package/utility/object.ts +22 -0
@@ -0,0 +1,102 @@
1
+ import type { Reference } from "@isolated-vm/experimental";
2
+ import * as assert from "node:assert/strict";
3
+ import { test } from "node:test";
4
+ import { Agent, expect } from "@isolated-vm/experimental";
5
+ import { expectComplete, unsafeEvalAsStringInRealm } from "@isolated-vm/experimental/test/fixtures";
6
+
7
+ /** Returns a reference to a function which describes the buffers in its parameter */
8
+ async function makeDescribe(agent: Agent) {
9
+ type Describe = (message?: unknown) => readonly (number | boolean | null)[] | null;
10
+ const realm = expect(await agent.createRealm());
11
+ await unsafeEvalAsStringInRealm(agent, realm, () => {
12
+ // @ts-expect-error
13
+ globalThis.fn = (message: { buffer?: ArrayBuffer; view?: Uint8Array }) =>
14
+ message.buffer
15
+ ? [ message.view ? message.view.buffer === message.buffer : null, ...new Uint8Array(message.buffer) ]
16
+ : null;
17
+ });
18
+ const global = await realm.acquireGlobalObject();
19
+ return expect(await global.get("fn")) as Reference<Describe>;
20
+ }
21
+
22
+ await test("transferred buffer is moved", async () => {
23
+ await using agent = await Agent.create();
24
+ const fn = await makeDescribe(agent);
25
+ const buffer = Uint8Array.from([ 1, 2, 3 ]).buffer;
26
+ const result = expectComplete(await fn.invoke([ { buffer } ], { transfer: [ buffer ] }));
27
+ assert.equal(buffer.byteLength, 0);
28
+ assert.deepStrictEqual(result, [ null, 1, 2, 3 ]);
29
+ });
30
+
31
+ await test("transferred buffer reached through a view", async () => {
32
+ await using agent = await Agent.create();
33
+ const fn = await makeDescribe(agent);
34
+ const view = Uint8Array.from([ 1, 2, 3 ]);
35
+ const result = expectComplete(await fn.invoke([ { buffer: view.buffer, view } ], { transfer: [ view.buffer ] }));
36
+ assert.equal(view.buffer.byteLength, 0);
37
+ assert.deepStrictEqual(result, [ true, 1, 2, 3 ]);
38
+ });
39
+
40
+ await test("transferred buffer keeps its identity", async () => {
41
+ await using agent = await Agent.create();
42
+ const fn = await makeDescribe(agent);
43
+ const buffer = Uint8Array.from([ 1, 2, 3 ]).buffer;
44
+ const view = new Uint8Array(buffer);
45
+ const result = expectComplete(await fn.invoke([ { buffer, view } ], { transfer: [ buffer ] }));
46
+ assert.equal(buffer.byteLength, 0);
47
+ assert.deepStrictEqual(result, [ true, 1, 2, 3 ]);
48
+ });
49
+
50
+ await test("unlisted buffer is copied", async () => {
51
+ await using agent = await Agent.create();
52
+ const fn = await makeDescribe(agent);
53
+ const buffer = Uint8Array.from([ 1, 2, 3 ]).buffer;
54
+ const result = expectComplete(await fn.invoke([ { buffer } ]));
55
+ assert.equal(buffer.byteLength, 3);
56
+ assert.deepStrictEqual(result, [ null, 1, 2, 3 ]);
57
+ });
58
+
59
+ await test("unvisited buffer is detached", async () => {
60
+ await using agent = await Agent.create();
61
+ const fn = await makeDescribe(agent);
62
+ const buffer = new ArrayBuffer(3);
63
+ expectComplete(await fn.invoke([ "hello" ], { transfer: [ buffer ] }));
64
+ assert.equal(buffer.byteLength, 0);
65
+ });
66
+
67
+ await test("duplicate buffer throws", async () => {
68
+ await using agent = await Agent.create();
69
+ const fn = await makeDescribe(agent);
70
+ const buffer = new ArrayBuffer(3);
71
+ assert.throws(() => void fn.invoke([], { transfer: [ buffer, buffer ] }));
72
+ assert.equal(buffer.byteLength, 3);
73
+ });
74
+
75
+ await test("unknown transfer value throws", async () => {
76
+ await using agent = await Agent.create();
77
+ const fn = await makeDescribe(agent);
78
+ assert.throws(() => void fn.invoke([], { transfer: [ {} ] }));
79
+ });
80
+
81
+ await test("sparse transfer list throws", async () => {
82
+ await using agent = await Agent.create();
83
+ const fn = await makeDescribe(agent);
84
+ const buffer = new ArrayBuffer(3);
85
+ const list = new Array<ArrayBuffer>(2);
86
+ list[1] = buffer;
87
+ assert.throws(() => void fn.invoke([], { transfer: list }), /sparse/i);
88
+ assert.equal(buffer.byteLength, 3);
89
+ });
90
+
91
+ await test("throwing transfer list getter propagates", async () => {
92
+ await using agent = await Agent.create();
93
+ const fn = await makeDescribe(agent);
94
+ const list: ArrayBuffer[] = [];
95
+ Object.defineProperty(list, 0, {
96
+ enumerable: true,
97
+ get: () => {
98
+ throw new Error("boom");
99
+ },
100
+ });
101
+ assert.throws(() => void fn.invoke([], { transfer: list }), /boom/);
102
+ });
@@ -0,0 +1,109 @@
1
+ import type { Realm, Reference } from "@isolated-vm/experimental";
2
+ import * as assert from "node:assert/strict";
3
+ import { test } from "node:test";
4
+ import { Agent, expect } from "@isolated-vm/experimental";
5
+ import { expectComplete, expectThrow, unsafeEvalAsStringInRealm } from "@isolated-vm/experimental/test/fixtures";
6
+ import { makeDirectResolver, makeLinker, makeStaticLoader } from "@isolated-vm/experimental/utility/linker";
7
+
8
+ async function makeTransferFunction(agent: Agent, realm: Realm, body: string) {
9
+ const runtime = expect(await realm.instantiateRuntime());
10
+ const module = expectComplete(await agent.compileModule(`
11
+ import { transfer } from "isolated-vm://runtime";
12
+ globalThis.fn = () => { ${body} };
13
+ `));
14
+ await module.link(realm, makeLinker(makeDirectResolver(), makeStaticLoader({ "isolated-vm://runtime": runtime })));
15
+ expectComplete(await module.evaluate(realm));
16
+ const global = await realm.acquireGlobalObject();
17
+ return expect(await global.get("fn")) as Reference<(...args: unknown[]) => unknown>;
18
+ }
19
+
20
+ function byteLengthInRealm(agent: Agent, realm: Realm) {
21
+ return unsafeEvalAsStringInRealm(agent, realm, () =>
22
+ (globalThis as unknown as { buffer: ArrayBuffer }).buffer.byteLength);
23
+ }
24
+
25
+ await test("returned buffer is moved", async () => {
26
+ await using agent = await Agent.create();
27
+ const realm = expect(await agent.createRealm());
28
+ const fn = await makeTransferFunction(agent, realm,
29
+ `const buffer = Uint8Array.from([ 1, 2, 3 ]).buffer;
30
+ globalThis.buffer = buffer;
31
+ return transfer(buffer, [ buffer ]);`);
32
+ const result = expectComplete(await fn.invoke([])) as ArrayBuffer;
33
+ assert.deepStrictEqual(Array.from(new Uint8Array(result)), [ 1, 2, 3 ]);
34
+ assert.equal(await byteLengthInRealm(agent, realm), 0);
35
+ });
36
+
37
+ await test("returned buffer reached through a view", async () => {
38
+ await using agent = await Agent.create();
39
+ const realm = expect(await agent.createRealm());
40
+ const fn = await makeTransferFunction(agent, realm,
41
+ `const view = Uint8Array.from([ 1, 2, 3 ]);
42
+ globalThis.buffer = view.buffer;
43
+ return transfer(view, [ view.buffer ]);`);
44
+ const result = expectComplete(await fn.invoke([])) as Uint8Array;
45
+ assert.deepStrictEqual(Array.from(result), [ 1, 2, 3 ]);
46
+ assert.equal(await byteLengthInRealm(agent, realm), 0);
47
+ });
48
+
49
+ await test("returned buffer keeps its identity", async () => {
50
+ await using agent = await Agent.create();
51
+ const realm = expect(await agent.createRealm());
52
+ const fn = await makeTransferFunction(agent, realm,
53
+ `const buffer = Uint8Array.from([ 1, 2, 3 ]).buffer;
54
+ const view = new Uint8Array(buffer);
55
+ return transfer({ buffer, view }, [ buffer ]);`);
56
+ const result = expectComplete(await fn.invoke([])) as { buffer: ArrayBuffer; view: Uint8Array };
57
+ assert.equal(result.view.buffer, result.buffer);
58
+ assert.deepStrictEqual(Array.from(new Uint8Array(result.buffer)), [ 1, 2, 3 ]);
59
+ });
60
+
61
+ await test("unlisted buffer is copied", async () => {
62
+ await using agent = await Agent.create();
63
+ const realm = expect(await agent.createRealm());
64
+ const fn = await makeTransferFunction(agent, realm,
65
+ `const buffer = Uint8Array.from([ 1, 2, 3 ]).buffer;
66
+ globalThis.buffer = buffer;
67
+ return transfer(buffer);`);
68
+ const result = expectComplete(await fn.invoke([])) as ArrayBuffer;
69
+ assert.deepStrictEqual(Array.from(new Uint8Array(result)), [ 1, 2, 3 ]);
70
+ assert.equal(await byteLengthInRealm(agent, realm), 3);
71
+ });
72
+
73
+ await test("unvisited buffer is detached", async () => {
74
+ await using agent = await Agent.create();
75
+ const realm = expect(await agent.createRealm());
76
+ const fn = await makeTransferFunction(agent, realm,
77
+ `globalThis.buffer = new ArrayBuffer(3);
78
+ return transfer("hello", [ globalThis.buffer ]);`);
79
+ const result = expectComplete(await fn.invoke([]));
80
+ assert.equal(result, "hello");
81
+ assert.equal(await byteLengthInRealm(agent, realm), 0);
82
+ });
83
+
84
+ await test("getters throw", async () => {
85
+ await using agent = await Agent.create();
86
+ const realm = expect(await agent.createRealm());
87
+ const fn = await makeTransferFunction(agent, realm,
88
+ `const buffer = new ArrayBuffer(3);
89
+ const transferList = [ buffer ];
90
+ Object.defineProperty(transferList, 0, {
91
+ get() { return buffer }
92
+ });
93
+ return transfer("hello", transferList);`);
94
+ expectThrow(await fn.invoke([]));
95
+ });
96
+
97
+ await test("invalid transfer list throws", async () => {
98
+ await using agent = await Agent.create();
99
+ const realm = expect(await agent.createRealm());
100
+ const fn = await makeTransferFunction(agent, realm, "return transfer(1, 'nope')");
101
+ expectThrow(await fn.invoke([]));
102
+ });
103
+
104
+ await test("unknown transfer value throws", async () => {
105
+ await using agent = await Agent.create();
106
+ const realm = expect(await agent.createRealm());
107
+ const fn = await makeTransferFunction(agent, realm, "return transfer(1, [ {} ])");
108
+ expectThrow(await fn.invoke([]));
109
+ });
@@ -32,13 +32,13 @@ await test("synthetic module capability", async () => {
32
32
  let didInvoke = false;
33
33
  const realm = expect(await agent.createRealm());
34
34
  const capability = expect(await realm.createCapability(
35
- () => ({
35
+ {
36
36
  default: (value1: unknown, value2: unknown) => {
37
37
  didInvoke = true;
38
38
  assert.equal(value1, "hello");
39
39
  assert.equal(value2, "world");
40
40
  },
41
- }),
41
+ },
42
42
  { origin: capabilityName }));
43
43
  const left = expectComplete(await agent.compileModule(`
44
44
  import capability from ${JSON.stringify(capabilityName)};
@@ -57,14 +57,14 @@ await test("synthetic module with circular reference", async () => {
57
57
  const capabilityName = "isolated-vm:///capability";
58
58
  const realm = expect(await agent.createRealm());
59
59
  const capability = expect(await realm.createCapability(
60
- () => ({
60
+ {
61
61
  default: (value1: unknown) => {
62
62
  // @ts-expect-error
63
63
  assert.strictEqual(value1.date1, value1.date2);
64
64
  // @ts-expect-error
65
65
  assert.strictEqual(value1.object, value1);
66
66
  },
67
- }),
67
+ },
68
68
  { origin: capabilityName }));
69
69
  const left = expectComplete(await agent.compileModule(`
70
70
  import capability from ${JSON.stringify(capabilityName)};
@@ -89,9 +89,9 @@ await test("synthetic module with data templates", async () => {
89
89
  const capabilityName = "isolated-vm:///capability";
90
90
  const realm = expect(await agent.createRealm());
91
91
  const capability = expect(await realm.createCapability(
92
- () => ({
92
+ {
93
93
  string: "string",
94
- }),
94
+ },
95
95
  { origin: capabilityName }));
96
96
  const entry = expectComplete(await agent.compileModule(`
97
97
  import { string } from ${JSON.stringify(capabilityName)};
@@ -113,11 +113,11 @@ await test("synthetic module with data templates", async () => {
113
113
  // const capabilityName = "isolated-vm:///capability";
114
114
  // const realm = await agent.createRealm();
115
115
  // const capability = await realm.createCapability(
116
- // () => ({
116
+ // {
117
117
  // default: () => {
118
118
  // throw new Error("capability error");
119
119
  // },
120
- // }),
120
+ // },
121
121
  // { origin: capabilityName });
122
122
  // const left = expectComplete(await agent.compileModule(`
123
123
  // import capability from ${JSON.stringify(capabilityName)};
@@ -11,18 +11,18 @@ await test("setTimeout capability", async () => {
11
11
  const resolvers = Promise.withResolvers();
12
12
  const capabilities = makeStaticLoader({
13
13
  "isolated-vm:capability/timers": expect(await realm.createCapability(
14
- () => ({
14
+ {
15
15
  default: (/*timeout*/) => {
16
16
  void async function() {
17
17
  await runTimers.run(realm);
18
18
  }();
19
19
  },
20
- }),
20
+ },
21
21
  { origin: "isolated-vm:capability/timers" })),
22
22
  "notify-test": expect(await realm.createCapability(
23
- () => ({
23
+ {
24
24
  default: (message: unknown) => { resolvers.resolve(message); },
25
- }),
25
+ },
26
26
  { origin: "notify-test" })),
27
27
  });
28
28
 
@@ -0,0 +1,22 @@
1
+ /** @internal */
2
+ export type Constructor<Type> = (abstract new (...args: any[]) => Type);
3
+
4
+ type AddThis<Type, Fn> =
5
+ Fn extends (...args: infer Args) => infer Return
6
+ ? (this: Type, ...args: Args) => Return
7
+ : never;
8
+
9
+ /** @internal */
10
+ export function extend<Type, Proto extends {
11
+ [Key in keyof Type]?: AddThis<Type, Type[Key]>;
12
+ }>(constructor: Constructor<Type>, prototype: Proto): void {
13
+ const descriptors = Object.getOwnPropertyDescriptors(prototype);
14
+ for (const key of Reflect.ownKeys(descriptors)) {
15
+ const info = descriptors[key as keyof Proto];
16
+ if (info.value && typeof info.value === "function") {
17
+ Object.defineProperty(constructor.prototype, key, { ...info, enumerable: false });
18
+ } else {
19
+ throw new Error(`Cannot extend ${constructor.name} with non-function property ${String(key)}`);
20
+ }
21
+ }
22
+ }