@fluffylabs/anan-as 1.2.0 → 1.3.0-39d3435

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 (110) hide show
  1. package/dist/bin/index.js +138 -5
  2. package/dist/bin/src/fuzz.js +2 -2
  3. package/dist/bin/src/test-json.js +2 -6
  4. package/dist/bin/src/trace-parse.js +1 -0
  5. package/dist/bin/src/trace-replay.js +14 -9
  6. package/dist/bin/src/tracer.js +16 -13
  7. package/dist/bin/src/utils.js +2 -2
  8. package/dist/build/compiler-inline.js +1 -1
  9. package/dist/build/compiler.d.ts +17 -13
  10. package/dist/build/compiler.js +13 -21
  11. package/dist/build/compiler.wasm +0 -0
  12. package/dist/build/debug-inline.js +1 -1
  13. package/dist/build/debug-raw-inline.js +1 -1
  14. package/dist/build/debug-raw.d.ts +50 -112
  15. package/dist/build/debug-raw.js +78 -139
  16. package/dist/build/debug-raw.wasm +0 -0
  17. package/dist/build/debug.d.ts +50 -112
  18. package/dist/build/debug.js +81 -147
  19. package/dist/build/debug.wasm +0 -0
  20. package/dist/build/js/assembly/api-debugger.d.ts +55 -0
  21. package/dist/build/js/assembly/api-debugger.js +245 -0
  22. package/dist/build/js/assembly/api-internal.d.ts +13 -0
  23. package/dist/build/js/assembly/api-internal.js +191 -0
  24. package/dist/build/js/assembly/api-types.d.ts +45 -0
  25. package/dist/build/js/assembly/api-types.js +52 -0
  26. package/dist/build/js/assembly/api-utils.d.ts +79 -0
  27. package/dist/build/js/assembly/api-utils.js +221 -0
  28. package/dist/build/js/assembly/arguments.d.ts +44 -0
  29. package/dist/build/js/assembly/arguments.js +164 -0
  30. package/dist/build/js/assembly/codec.d.ts +24 -0
  31. package/dist/build/js/assembly/codec.js +139 -0
  32. package/dist/build/js/assembly/gas.d.ts +11 -0
  33. package/dist/build/js/assembly/gas.js +33 -0
  34. package/dist/build/js/assembly/index-shared.d.ts +4 -0
  35. package/dist/build/js/assembly/index-shared.js +4 -0
  36. package/dist/build/js/assembly/instructions/bit.d.ts +11 -0
  37. package/dist/build/js/assembly/instructions/bit.js +53 -0
  38. package/dist/build/js/assembly/instructions/branch.d.ts +17 -0
  39. package/dist/build/js/assembly/instructions/branch.js +120 -0
  40. package/dist/build/js/assembly/instructions/jump.d.ts +5 -0
  41. package/dist/build/js/assembly/instructions/jump.js +21 -0
  42. package/dist/build/js/assembly/instructions/load.d.ts +17 -0
  43. package/dist/build/js/assembly/instructions/load.js +134 -0
  44. package/dist/build/js/assembly/instructions/logic.d.ts +10 -0
  45. package/dist/build/js/assembly/instructions/logic.js +47 -0
  46. package/dist/build/js/assembly/instructions/math.d.ts +28 -0
  47. package/dist/build/js/assembly/instructions/math.js +225 -0
  48. package/dist/build/js/assembly/instructions/misc.d.ts +6 -0
  49. package/dist/build/js/assembly/instructions/misc.js +22 -0
  50. package/dist/build/js/assembly/instructions/mov.d.ts +6 -0
  51. package/dist/build/js/assembly/instructions/mov.js +35 -0
  52. package/dist/build/js/assembly/instructions/outcome.d.ts +30 -0
  53. package/dist/build/js/assembly/instructions/outcome.js +88 -0
  54. package/dist/build/js/assembly/instructions/rot.d.ts +15 -0
  55. package/dist/build/js/assembly/instructions/rot.js +66 -0
  56. package/dist/build/js/assembly/instructions/set.d.ts +7 -0
  57. package/dist/build/js/assembly/instructions/set.js +36 -0
  58. package/dist/build/js/assembly/instructions/shift.d.ts +19 -0
  59. package/dist/build/js/assembly/instructions/shift.js +121 -0
  60. package/dist/build/js/assembly/instructions/store.d.ts +17 -0
  61. package/dist/build/js/assembly/instructions/store.js +101 -0
  62. package/dist/build/js/assembly/instructions/utils.d.ts +25 -0
  63. package/dist/build/js/assembly/instructions/utils.js +91 -0
  64. package/dist/build/js/assembly/instructions-exe.d.ts +2 -0
  65. package/dist/build/js/assembly/instructions-exe.js +245 -0
  66. package/dist/build/js/assembly/instructions.d.ts +10 -0
  67. package/dist/build/js/assembly/instructions.js +252 -0
  68. package/dist/build/js/assembly/interpreter.d.ts +28 -0
  69. package/dist/build/js/assembly/interpreter.js +221 -0
  70. package/dist/build/js/assembly/math.d.ts +6 -0
  71. package/dist/build/js/assembly/math.js +22 -0
  72. package/dist/build/js/assembly/memory-page.d.ts +36 -0
  73. package/dist/build/js/assembly/memory-page.js +74 -0
  74. package/dist/build/js/assembly/memory.d.ts +83 -0
  75. package/dist/build/js/assembly/memory.js +482 -0
  76. package/dist/build/js/assembly/portable.d.ts +24 -0
  77. package/dist/build/js/assembly/portable.js +363 -0
  78. package/dist/build/js/assembly/program-build.d.ts +2 -0
  79. package/dist/build/js/assembly/program-build.js +104 -0
  80. package/dist/build/js/assembly/program.d.ts +85 -0
  81. package/dist/build/js/assembly/program.js +340 -0
  82. package/dist/build/js/assembly/registers.d.ts +6 -0
  83. package/dist/build/js/assembly/registers.js +9 -0
  84. package/dist/build/js/assembly/spi.d.ts +92 -0
  85. package/dist/build/js/assembly/spi.js +152 -0
  86. package/dist/build/js/portable/bootstrap.d.ts +1 -0
  87. package/dist/build/js/portable/bootstrap.js +6 -0
  88. package/dist/build/js/portable/index.d.ts +4 -0
  89. package/dist/build/js/portable/index.js +6 -0
  90. package/dist/build/js/portable-bundle.js +4497 -0
  91. package/dist/build/release-inline.js +1 -1
  92. package/dist/build/release-mini-inline.js +1 -1
  93. package/dist/build/release-mini.d.ts +50 -112
  94. package/dist/build/release-mini.js +81 -147
  95. package/dist/build/release-mini.wasm +0 -0
  96. package/dist/build/release-stub-inline.js +1 -1
  97. package/dist/build/release-stub.d.ts +50 -112
  98. package/dist/build/release-stub.js +81 -147
  99. package/dist/build/release-stub.wasm +0 -0
  100. package/dist/build/release.d.ts +50 -112
  101. package/dist/build/release.js +81 -147
  102. package/dist/build/release.wasm +0 -0
  103. package/dist/build/test-inline.js +1 -1
  104. package/dist/build/test.wasm +0 -0
  105. package/dist/test/test-gas-cost.js +2 -3
  106. package/dist/test/test-trace-format.js +166 -0
  107. package/dist/test/test-w3f-common.js +125 -0
  108. package/dist/test/test-w3f-portable.js +5 -0
  109. package/dist/test/test-w3f.js +3 -120
  110. package/package.json +22 -11
@@ -0,0 +1,221 @@
1
+ import { Args } from "./arguments";
2
+ import { gasCounter } from "./gas";
3
+ import { INSTRUCTIONS, MISSING_INSTRUCTION } from "./instructions";
4
+ import { Outcome, OutcomeData, Result } from "./instructions/outcome";
5
+ import { RUN } from "./instructions-exe";
6
+ import { MemoryBuilder } from "./memory";
7
+ import { RESERVED_MEMORY } from "./memory-page";
8
+ import { portable } from "./portable";
9
+ import { decodeArguments } from "./program";
10
+ export var Status;
11
+ (function (Status) {
12
+ Status[Status["OK"] = -1] = "OK";
13
+ Status[Status["HALT"] = 0] = "HALT";
14
+ Status[Status["PANIC"] = 1] = "PANIC";
15
+ Status[Status["FAULT"] = 2] = "FAULT";
16
+ Status[Status["HOST"] = 3] = "HOST";
17
+ Status[Status["OOG"] = 4] = "OOG";
18
+ })(Status || (Status = {}));
19
+ var DjumpStatus;
20
+ (function (DjumpStatus) {
21
+ DjumpStatus[DjumpStatus["OK"] = 0] = "OK";
22
+ DjumpStatus[DjumpStatus["HALT"] = 1] = "HALT";
23
+ DjumpStatus[DjumpStatus["PANIC"] = 2] = "PANIC";
24
+ })(DjumpStatus || (DjumpStatus = {}));
25
+ // @unmanaged
26
+ class DjumpResult {
27
+ constructor() {
28
+ this.status = DjumpStatus.OK;
29
+ this.newPc = 0;
30
+ }
31
+ }
32
+ // @unmanaged
33
+ class BranchResult {
34
+ constructor() {
35
+ this.isOkay = false;
36
+ this.newPc = 0;
37
+ }
38
+ }
39
+ export class Interpreter {
40
+ constructor(program, registers, memory = new MemoryBuilder().build(0)) {
41
+ this.djumpRes = new DjumpResult();
42
+ this.argsRes = new Args();
43
+ this.outcomeRes = new OutcomeData();
44
+ this.branchRes = new BranchResult();
45
+ this.program = program;
46
+ this.registers = registers;
47
+ this.memory = memory;
48
+ this.gas = gasCounter(i64(0));
49
+ this.pc = 0;
50
+ this.status = Status.OK;
51
+ this.exitCode = 0;
52
+ this.nextPc = 0;
53
+ }
54
+ nextSteps(nSteps = 1) {
55
+ // resuming after host call
56
+ if (this.status === Status.HOST) {
57
+ // let's assume all is good and move on :)
58
+ this.status = Status.OK;
59
+ // apply the nextPc, but don't stop, rather continue
60
+ // executing right away.
61
+ this.pc = this.nextPc;
62
+ this.nextPc = -1;
63
+ }
64
+ if (this.status !== Status.OK) {
65
+ return false;
66
+ }
67
+ // TODO [ToDr] Some weird pre-init step for the debugger?
68
+ if (this.nextPc !== -1) {
69
+ this.pc = this.nextPc;
70
+ this.nextPc = -1;
71
+ return true;
72
+ }
73
+ const code = this.program.code;
74
+ const mask = this.program.mask;
75
+ const gasCosts = this.program.gasCosts.codeAndGas;
76
+ const basicBlocks = this.program.basicBlocks;
77
+ const jumpTable = this.program.jumpTable;
78
+ const argsRes = this.argsRes;
79
+ const outcomeRes = this.outcomeRes;
80
+ for (let i = 0; i < nSteps; i++) {
81
+ // reset some stuff at start
82
+ this.exitCode = 0;
83
+ outcomeRes.result = Result.PANIC;
84
+ outcomeRes.outcome = Outcome.Ok;
85
+ const pc = this.pc;
86
+ // check if we are at the right location
87
+ if (!mask.isInstruction(pc)) {
88
+ // TODO [ToDr] Potential edge case here?
89
+ if (this.gas.sub(MISSING_INSTRUCTION.gas)) {
90
+ this.status = Status.OOG;
91
+ }
92
+ else {
93
+ this.status = Status.PANIC;
94
+ }
95
+ return false;
96
+ }
97
+ // check gas via pre-computed cost table (per-instruction or per-block)
98
+ const codeAndGas = portable.staticArrayAt(gasCosts, pc);
99
+ const instruction = codeAndGas & 0xff;
100
+ const gasCost = codeAndGas >> 8;
101
+ const iData = instruction < INSTRUCTIONS.length ? unchecked(INSTRUCTIONS[instruction]) : MISSING_INSTRUCTION;
102
+ if (gasCost > 0 && this.gas.sub(gasCost)) {
103
+ this.status = Status.OOG;
104
+ return false;
105
+ }
106
+ if (iData === MISSING_INSTRUCTION) {
107
+ this.status = Status.PANIC;
108
+ return false;
109
+ }
110
+ // get args and invoke instruction
111
+ const skipBytes = mask.skipBytesToNextInstruction(pc);
112
+ const args = decodeArguments(argsRes, iData.kind, code, pc + 1, skipBytes);
113
+ const exe = unchecked(RUN[instruction]);
114
+ const outcome = exe(outcomeRes, args, this.registers, this.memory);
115
+ // Fast path: Ok is the most common outcome (~70%+ of instructions)
116
+ if (outcome.outcome === Outcome.Ok) {
117
+ this.pc += 1 + skipBytes;
118
+ continue;
119
+ }
120
+ switch (outcome.outcome) {
121
+ case Outcome.StaticJump: {
122
+ const branchResult = branch(this.branchRes, basicBlocks, pc, outcome.staticJump);
123
+ if (!branchResult.isOkay) {
124
+ this.status = Status.PANIC;
125
+ return false;
126
+ }
127
+ this.pc = branchResult.newPc;
128
+ continue;
129
+ }
130
+ case Outcome.DynamicJump: {
131
+ const res = dJump(this.djumpRes, jumpTable, outcome.dJump);
132
+ if (res.status === DjumpStatus.HALT) {
133
+ this.status = Status.HALT;
134
+ return false;
135
+ }
136
+ if (res.status === DjumpStatus.PANIC) {
137
+ this.status = Status.PANIC;
138
+ return false;
139
+ }
140
+ const branchResult = branch(this.branchRes, basicBlocks, res.newPc, 0);
141
+ if (!branchResult.isOkay) {
142
+ this.status = Status.PANIC;
143
+ return false;
144
+ }
145
+ this.pc = branchResult.newPc;
146
+ continue;
147
+ }
148
+ case Outcome.Result: {
149
+ if (outcome.result === Result.HOST) {
150
+ this.status = Status.HOST;
151
+ this.exitCode = outcome.exitCode;
152
+ // set the next PC after the host call is called.
153
+ this.nextPc = this.pc + 1 + skipBytes;
154
+ return false;
155
+ }
156
+ if (outcome.result === Result.FAULT) {
157
+ // access to reserved memory should end with a panic.
158
+ if (outcome.exitCode < RESERVED_MEMORY) {
159
+ this.status = Status.PANIC;
160
+ }
161
+ else {
162
+ this.status = Status.FAULT;
163
+ this.exitCode = outcome.exitCode;
164
+ }
165
+ return false;
166
+ }
167
+ if (outcome.result === Result.FAULT_ACCESS) {
168
+ this.status = Status.PANIC;
169
+ // this.exitCode = outcome.exitCode;
170
+ return false;
171
+ }
172
+ if (outcome.result === Result.PANIC) {
173
+ this.status = Status.PANIC;
174
+ this.exitCode = outcome.exitCode;
175
+ return false;
176
+ }
177
+ throw new Error("Unknown result");
178
+ }
179
+ }
180
+ }
181
+ return true;
182
+ }
183
+ }
184
+ function branch(r, basicBlocks, pc, offset) {
185
+ const newPc = pc + offset;
186
+ if (basicBlocks.isStart(newPc)) {
187
+ r.isOkay = true;
188
+ r.newPc = newPc;
189
+ }
190
+ else {
191
+ r.isOkay = false;
192
+ r.newPc = 0;
193
+ }
194
+ return r;
195
+ }
196
+ const EXIT = 4294901760;
197
+ const JUMP_ALIGMENT_FACTOR = 2;
198
+ function dJump(r, jumpTable, address) {
199
+ if (address === EXIT) {
200
+ r.status = DjumpStatus.HALT;
201
+ return r;
202
+ }
203
+ if (address === 0 || address % JUMP_ALIGMENT_FACTOR !== 0) {
204
+ r.status = DjumpStatus.PANIC;
205
+ return r;
206
+ }
207
+ const index = u32(address / JUMP_ALIGMENT_FACTOR) - 1;
208
+ if (index >= jumpTable.jumps.length) {
209
+ r.status = DjumpStatus.PANIC;
210
+ return r;
211
+ }
212
+ const newPc = portable.staticArrayAt(jumpTable.jumps, index);
213
+ if (newPc >= MAX_U32) {
214
+ r.status = DjumpStatus.PANIC;
215
+ return r;
216
+ }
217
+ r.status = DjumpStatus.OK;
218
+ r.newPc = u32(newPc);
219
+ return r;
220
+ }
221
+ const MAX_U32 = u64(4294967296);
@@ -0,0 +1,6 @@
1
+ export declare class IntMath {
2
+ /** Integer minimum of two i32 values. */
3
+ static minI32(a: i32, b: i32): i32;
4
+ /** Unsigned integer minimum of two u32 values. */
5
+ static minU32(a: u32, b: u32): u32;
6
+ }
@@ -0,0 +1,22 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ export class IntMath {
8
+ /** Integer minimum of two i32 values. */
9
+ static minI32(a, b) {
10
+ return a < b ? a : b;
11
+ }
12
+ /** Unsigned integer minimum of two u32 values. */
13
+ static minU32(a, b) {
14
+ return a < b ? a : b;
15
+ }
16
+ }
17
+ __decorate([
18
+ inline
19
+ ], IntMath, "minI32", null);
20
+ __decorate([
21
+ inline
22
+ ], IntMath, "minU32", null);
@@ -0,0 +1,36 @@
1
+ export type PageIndex = u32;
2
+ export type ArenaId = u32;
3
+ /** `Z_P`: https://graypaper.fluffylabs.dev/#/ab2cdbd/44d20044d200?v=0.7.2 */
4
+ export declare const PAGE_SIZE: u32;
5
+ export declare const PAGE_SIZE_SHIFT: u32;
6
+ /** `Z_Z`: https://graypaper.fluffylabs.dev/#/ab2cdbd/2daf002daf00?v=0.7.2 */
7
+ export declare const SEGMENT_SIZE: u32;
8
+ export declare const SEGMENT_SIZE_SHIFT: u32;
9
+ /** https://graypaper.fluffylabs.dev/#/ab2cdbd/254401254a01?v=0.7.2 */
10
+ export declare const RESERVED_MEMORY: u32;
11
+ export declare const RESERVED_PAGES: u32;
12
+ export declare enum Access {
13
+ None = 0,
14
+ Read = 1,
15
+ Write = 2
16
+ }
17
+ export declare class Page {
18
+ readonly access: Access;
19
+ readonly raw: RawPage;
20
+ constructor(access: Access, raw: RawPage);
21
+ can(access: Access): boolean;
22
+ }
23
+ export declare class RawPage {
24
+ readonly id: ArenaId;
25
+ page: Uint8Array;
26
+ constructor(id: ArenaId, page: Uint8Array);
27
+ get data(): Uint8Array;
28
+ }
29
+ export declare class Arena {
30
+ private free;
31
+ private readonly arenaBytes;
32
+ private extraPageIndex;
33
+ constructor(pageCount: u32);
34
+ acquire(): RawPage;
35
+ release(page: RawPage): void;
36
+ }
@@ -0,0 +1,74 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { portable } from "./portable";
8
+ /** `Z_P`: https://graypaper.fluffylabs.dev/#/ab2cdbd/44d20044d200?v=0.7.2 */
9
+ export const PAGE_SIZE = 2 ** 12; // 4_096
10
+ export const PAGE_SIZE_SHIFT = 12;
11
+ /** `Z_Z`: https://graypaper.fluffylabs.dev/#/ab2cdbd/2daf002daf00?v=0.7.2 */
12
+ export const SEGMENT_SIZE = 2 ** 16; //65_536
13
+ export const SEGMENT_SIZE_SHIFT = 16;
14
+ /** https://graypaper.fluffylabs.dev/#/ab2cdbd/254401254a01?v=0.7.2 */
15
+ export const RESERVED_MEMORY = 2 ** 16;
16
+ export const RESERVED_PAGES = RESERVED_MEMORY / PAGE_SIZE; // 16
17
+ export var Access;
18
+ (function (Access) {
19
+ Access[Access["None"] = 0] = "None";
20
+ Access[Access["Read"] = 1] = "Read";
21
+ Access[Access["Write"] = 2] = "Write";
22
+ })(Access || (Access = {}));
23
+ export class Page {
24
+ constructor(access, raw) {
25
+ this.access = access;
26
+ this.raw = raw;
27
+ }
28
+ can(access) {
29
+ return this.access === Access.Write || this.access === access;
30
+ }
31
+ }
32
+ __decorate([
33
+ inline
34
+ ], Page.prototype, "can", null);
35
+ export class RawPage {
36
+ constructor(id, page) {
37
+ this.id = id;
38
+ this.page = page;
39
+ }
40
+ get data() {
41
+ return this.page;
42
+ }
43
+ }
44
+ __decorate([
45
+ inline
46
+ ], RawPage.prototype, "data", null);
47
+ export class Arena {
48
+ constructor(pageCount) {
49
+ this.arenaBytes = PAGE_SIZE * pageCount;
50
+ this.free = [];
51
+ this.extraPageIndex = pageCount;
52
+ const data = new ArrayBuffer(this.arenaBytes);
53
+ for (let i = 0; i < pageCount; i++) {
54
+ this.free.unshift(new RawPage(i, portable.uint8ArrayView(data, i * PAGE_SIZE, PAGE_SIZE)));
55
+ }
56
+ }
57
+ acquire() {
58
+ if (this.free.length > 0) {
59
+ return this.free.pop();
60
+ }
61
+ // no pages!
62
+ const allocatedMemory = this.extraPageIndex * PAGE_SIZE;
63
+ // print warning only once
64
+ if (allocatedMemory === this.arenaBytes) {
65
+ console.log("Warning: Run out of pages! Allocating.");
66
+ }
67
+ const data = new Uint8Array(PAGE_SIZE);
68
+ this.extraPageIndex += 1;
69
+ return new RawPage(this.extraPageIndex, data);
70
+ }
71
+ release(page) {
72
+ this.free.push(page);
73
+ }
74
+ }
@@ -0,0 +1,83 @@
1
+ import { Access, Arena, Page, PageIndex } from "./memory-page";
2
+ export declare class MaybePageFault {
3
+ /** Accessing memory triggered a page fault. */
4
+ isFault: boolean;
5
+ /** The page fault was caused by invalid memory access (i.e. writing to read-only memory). */
6
+ isAccess: boolean;
7
+ /** Start memory index of a page that triggered the fault. */
8
+ fault: u32;
9
+ }
10
+ export declare class MemoryBuilder {
11
+ private readonly pages;
12
+ private readonly arena;
13
+ constructor(preAllocatePages?: u32);
14
+ /** Allocates memory pages with given `access`, for given `address` and initialize with `zeroes` */
15
+ setEmpty(access: Access, address: u32, len: u32): MemoryBuilder;
16
+ /** Allocates memory pages with given `access`, for given `address` and writes there `data` */
17
+ setData(access: Access, address: u32, data: Uint8Array): MemoryBuilder;
18
+ /** Returns memory page for given address (creates if not exists) */
19
+ getOrCreatePageForAddress(access: Access, address: u32): Page;
20
+ build(sbrkAddress?: u32, maxHeapPointer?: u32): Memory;
21
+ }
22
+ export declare class Memory {
23
+ private readonly arena;
24
+ readonly pages: Map<PageIndex, Page>;
25
+ private sbrkAddress;
26
+ private lastAllocatedPage;
27
+ private pageResult;
28
+ private chunksResult;
29
+ private maxHeapPointer;
30
+ private cache;
31
+ constructor(arena: Arena, pages?: Map<PageIndex, Page>, sbrkAddress?: u32, maxHeapPointer?: u32);
32
+ pageDump(index: PageIndex): Uint8Array | null;
33
+ /**
34
+ * Returns the WASM linear memory pointer (byte offset) for the backing buffer of the page at `pageIndex`.
35
+ *
36
+ * Returns `0` if the page does not exist or is not readable (page/access fault).
37
+ *
38
+ * This enables efficient memory reading on the JS side without extra WASM allocations:
39
+ * ```ts
40
+ * let pagesRead = 0;
41
+ * for (let address = start; address < end; address += PAGE_SIZE) {
42
+ * const page = address >> PAGE_SIZE_SHIFT;
43
+ * const ptr = getPagePointer(page);
44
+ * if (ptr === 0) {
45
+ * throw new Error(`Page fault at ${page << PAGE_SIZE_SHIFT}`);
46
+ * }
47
+ * destination.set(
48
+ * new Uint8Array(wasm.instance.exports.memory.buffer, ptr, Math.min(end - address, PAGE_SIZE)),
49
+ * pagesRead << PAGE_SIZE_SHIFT,
50
+ * );
51
+ * pagesRead += 1;
52
+ * }
53
+ * ```
54
+ */
55
+ getPagePointer(pageIndex: u32): usize;
56
+ free(): void;
57
+ sbrk(faultRes: MaybePageFault, amount: u32): u64;
58
+ getU8(faultRes: MaybePageFault, address: u32): u64;
59
+ getU16(faultRes: MaybePageFault, address: u32): u64;
60
+ getU32(faultRes: MaybePageFault, address: u32): u64;
61
+ getU64(faultRes: MaybePageFault, address: u32): u64;
62
+ getI8(faultRes: MaybePageFault, address: u32): u64;
63
+ getI16(faultRes: MaybePageFault, address: u32): u64;
64
+ getI32(faultRes: MaybePageFault, address: u32): u64;
65
+ setU8(faultRes: MaybePageFault, address: u32, value: u8): void;
66
+ setU16(faultRes: MaybePageFault, address: u32, value: u16): void;
67
+ setU32(faultRes: MaybePageFault, address: u32, value: u32): void;
68
+ setU64(faultRes: MaybePageFault, address: u32, value: u64): void;
69
+ /**
70
+ * DO NOT USE.
71
+ *
72
+ * @deprecated exposed temporarily for debugger/typeberry API.
73
+ */
74
+ getMemory(fault: MaybePageFault, address: u32, length: u32): Uint8Array | null;
75
+ bytesRead(faultRes: MaybePageFault, address: u32, destination: Uint8Array, destinationOffset: u32): void;
76
+ /** Write bytes from given `source` (with `sourceOffset`) at given `address`. */
77
+ bytesWrite(faultRes: MaybePageFault, address: u32, source: Uint8Array, sourceOffset: u32): void;
78
+ private getPage;
79
+ private getChunks;
80
+ /** Write some bytes to at most 2 pages. */
81
+ private setBytes;
82
+ private getBytesReversed;
83
+ }