|
1 | 1 | import { vitest, expect, describe, it, beforeEach } from "vitest"; |
2 | | -import { Pointer } from "@ethdebug/format"; |
| 2 | +import { Pointer, schemas } from "@ethdebug/format"; |
3 | 3 | import { Machine } from "#machine"; |
4 | 4 | import { Data } from "#data"; |
5 | 5 | import { dereference } from "./index.js"; |
@@ -336,6 +336,83 @@ describe("dereference", () => { |
336 | 336 | expect(regions[1]).toMatchObject({ slot: Data.fromNumber(2) }); |
337 | 337 | }); |
338 | 338 |
|
| 339 | + describe("define member order", () => { |
| 340 | + // the region of a scope whose `in` is at storage slot "result" |
| 341 | + const regionOf = async ( |
| 342 | + define: Pointer.Collection.Scope["define"], |
| 343 | + outer?: Pointer.Collection.Scope["define"], |
| 344 | + ) => { |
| 345 | + const scope: Pointer = { |
| 346 | + define, |
| 347 | + in: { location: "storage", slot: "result" }, |
| 348 | + }; |
| 349 | + const pointer: Pointer = outer ? { define: outer, in: scope } : scope; |
| 350 | + const { regions } = await (await dereference(pointer)).view(state); |
| 351 | + return regions[0]; |
| 352 | + }; |
| 353 | + |
| 354 | + it("evaluates members after the members they use", async () => { |
| 355 | + expect( |
| 356 | + await regionOf({ result: { "~sum": ["a", 1] }, a: 5 }), |
| 357 | + ).toMatchObject({ slot: Data.fromNumber(6) }); |
| 358 | + }); |
| 359 | + |
| 360 | + it("evaluates a longer chain in any order", async () => { |
| 361 | + expect( |
| 362 | + await regionOf({ |
| 363 | + result: { "~sum": ["c", 1] }, |
| 364 | + c: { "~product": ["b", { "~sum": ["a", 1] }] }, |
| 365 | + b: { "~sum": ["a", 2] }, |
| 366 | + a: 1, |
| 367 | + }), |
| 368 | + ).toMatchObject({ slot: Data.fromNumber(7) }); |
| 369 | + }); |
| 370 | + |
| 371 | + it("binds a member's own name to the enclosing scope", async () => { |
| 372 | + expect( |
| 373 | + await regionOf({ result: { "~sum": ["result", 1] } }, { result: 4 }), |
| 374 | + ).toMatchObject({ slot: Data.fromNumber(5) }); |
| 375 | + }); |
| 376 | + |
| 377 | + it("hides an outer variable from the other members", async () => { |
| 378 | + expect( |
| 379 | + await regionOf({ result: { "~sum": ["a", 1] }, a: 5 }, { a: 100 }), |
| 380 | + ).toMatchObject({ slot: Data.fromNumber(6) }); |
| 381 | + }); |
| 382 | + |
| 383 | + it("resolves the scope schema's out-of-order example", async () => { |
| 384 | + const { examples } = schemas[ |
| 385 | + "schema:ethdebug/format/pointer/collection/scope" |
| 386 | + ] as { examples: Pointer[] }; |
| 387 | + const example = examples.find((pointer) => |
| 388 | + JSON.stringify(pointer).includes('"end"'), |
| 389 | + )!; |
| 390 | + const { regions } = await (await dereference(example)).view(state); |
| 391 | + expect(regions[0]).toMatchObject({ |
| 392 | + offset: Data.fromNumber(96), |
| 393 | + length: Data.fromNumber(32), |
| 394 | + }); |
| 395 | + }); |
| 396 | + |
| 397 | + it("throws on a cycle between members", async () => { |
| 398 | + await expect(regionOf({ result: "a", a: "b", b: "a" })).rejects.toThrow( |
| 399 | + "Circular reference detected", |
| 400 | + ); |
| 401 | + }); |
| 402 | + |
| 403 | + it("throws on a cycle even when outer variables match", async () => { |
| 404 | + await expect( |
| 405 | + regionOf({ result: "a", a: "b", b: "a" }, { a: 1, b: 2 }), |
| 406 | + ).rejects.toThrow("Circular reference detected"); |
| 407 | + }); |
| 408 | + |
| 409 | + it("still throws on an unknown variable", async () => { |
| 410 | + await expect(regionOf({ result: "a", b: 1 })).rejects.toThrow( |
| 411 | + "Unknown variable with identifier a", |
| 412 | + ); |
| 413 | + }); |
| 414 | + }); |
| 415 | + |
339 | 416 | it("scopes variables defined inside referenced templates", async () => { |
340 | 417 | const pointer: Pointer = { |
341 | 418 | define: { slot: "0x00" }, |
|
0 commit comments