@fluffylabs/anan-as 1.2.0-41e43f6 → 1.2.0-5196d29

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 (77) hide show
  1. package/dist/bin/index.js +3 -2
  2. package/dist/bin/src/fuzz.js +1 -1
  3. package/dist/bin/src/test-json.js +2 -6
  4. package/dist/bin/src/trace-replay.js +8 -4
  5. package/dist/build/compiler-inline.js +1 -1
  6. package/dist/build/compiler.d.ts +2 -13
  7. package/dist/build/compiler.js +1 -25
  8. package/dist/build/compiler.wasm +0 -0
  9. package/dist/build/debug-inline.js +1 -1
  10. package/dist/build/debug-raw-inline.js +1 -1
  11. package/dist/build/debug-raw.d.ts +27 -25
  12. package/dist/build/debug-raw.js +58 -57
  13. package/dist/build/debug-raw.wasm +0 -0
  14. package/dist/build/debug.d.ts +27 -25
  15. package/dist/build/debug.js +59 -58
  16. package/dist/build/debug.wasm +0 -0
  17. package/dist/build/js/assembly/api-debugger.d.ts +3 -3
  18. package/dist/build/js/assembly/api-debugger.js +6 -6
  19. package/dist/build/js/assembly/api-internal.d.ts +1 -1
  20. package/dist/build/js/assembly/api-internal.js +2 -3
  21. package/dist/build/js/assembly/api-types.d.ts +0 -1
  22. package/dist/build/js/assembly/api-types.js +0 -1
  23. package/dist/build/js/assembly/api-utils.d.ts +12 -5
  24. package/dist/build/js/assembly/api-utils.js +28 -13
  25. package/dist/build/js/assembly/arguments.d.ts +4 -4
  26. package/dist/build/js/assembly/arguments.js +10 -10
  27. package/dist/build/js/assembly/gas.d.ts +1 -1
  28. package/dist/build/js/assembly/gas.js +3 -2
  29. package/dist/build/js/assembly/instructions/bit.js +22 -22
  30. package/dist/build/js/assembly/instructions/branch.js +56 -56
  31. package/dist/build/js/assembly/instructions/jump.js +10 -10
  32. package/dist/build/js/assembly/instructions/load.js +41 -41
  33. package/dist/build/js/assembly/instructions/logic.js +20 -20
  34. package/dist/build/js/assembly/instructions/math.js +105 -105
  35. package/dist/build/js/assembly/instructions/misc.js +10 -10
  36. package/dist/build/js/assembly/instructions/mov.js +16 -16
  37. package/dist/build/js/assembly/instructions/outcome.d.ts +7 -7
  38. package/dist/build/js/assembly/instructions/outcome.js +63 -36
  39. package/dist/build/js/assembly/instructions/rot.js +18 -18
  40. package/dist/build/js/assembly/instructions/set.js +18 -18
  41. package/dist/build/js/assembly/instructions/shift.js +59 -59
  42. package/dist/build/js/assembly/instructions/store.js +29 -29
  43. package/dist/build/js/assembly/instructions/utils.d.ts +6 -4
  44. package/dist/build/js/assembly/instructions/utils.js +32 -16
  45. package/dist/build/js/assembly/instructions.d.ts +2 -3
  46. package/dist/build/js/assembly/instructions.js +4 -4
  47. package/dist/build/js/assembly/interpreter.d.ts +0 -1
  48. package/dist/build/js/assembly/interpreter.js +15 -22
  49. package/dist/build/js/assembly/math.d.ts +6 -8
  50. package/dist/build/js/assembly/math.js +21 -13
  51. package/dist/build/js/assembly/memory.js +9 -7
  52. package/dist/build/js/assembly/portable.js +1 -0
  53. package/dist/build/js/assembly/program-build.js +4 -4
  54. package/dist/build/js/assembly/program.d.ts +15 -8
  55. package/dist/build/js/assembly/program.js +90 -40
  56. package/dist/build/js/assembly/spi.d.ts +1 -1
  57. package/dist/build/js/assembly/spi.js +2 -2
  58. package/dist/build/js/portable-bundle.js +666 -579
  59. package/dist/build/release-inline.js +1 -1
  60. package/dist/build/release-mini-inline.js +1 -1
  61. package/dist/build/release-mini.d.ts +27 -25
  62. package/dist/build/release-mini.js +59 -58
  63. package/dist/build/release-mini.wasm +0 -0
  64. package/dist/build/release-stub-inline.js +1 -1
  65. package/dist/build/release-stub.d.ts +27 -25
  66. package/dist/build/release-stub.js +59 -58
  67. package/dist/build/release-stub.wasm +0 -0
  68. package/dist/build/release.d.ts +27 -25
  69. package/dist/build/release.js +59 -58
  70. package/dist/build/release.wasm +0 -0
  71. package/dist/build/test-inline.js +1 -1
  72. package/dist/build/test.wasm +0 -0
  73. package/dist/test/test-gas-cost.js +2 -3
  74. package/dist/test/test-w3f-common.js +1 -2
  75. package/package.json +7 -7
  76. package/dist/build/js/assembly/gas-costs.d.ts +0 -6
  77. package/dist/build/js/assembly/gas-costs.js +0 -39
@@ -29,46 +29,49 @@ async function instantiate(module, imports = {}) {
29
29
  },
30
30
  wrapAsProgram(bytecode) {
31
31
  // assembly/program-build/wrapAsProgram(~lib/typedarray/Uint8Array) => ~lib/typedarray/Uint8Array
32
- bytecode = __lowerTypedArray(Uint8Array, 12, 0, bytecode) || __notnull();
32
+ bytecode = __lowerTypedArray(Uint8Array, 15, 0, bytecode) || __notnull();
33
33
  return __liftTypedArray(Uint8Array, exports.wrapAsProgram(bytecode) >>> 0);
34
34
  },
35
- resetJAM(program, pc, initialGas, args, hasMetadata) {
36
- // assembly/api-debugger/resetJAM(~lib/array/Array<u8>, u32, u64, ~lib/array/Array<u8>, bool?) => void
37
- program = __retain(__lowerArray(__setU8, 7, 0, program) || __notnull());
35
+ resetJAM(program, pc, initialGas, args, hasMetadata, useBlockGas) {
36
+ // assembly/api-debugger/resetJAM(~lib/array/Array<u8>, u32, u64, ~lib/array/Array<u8>, bool?, bool?) => void
37
+ program = __retain(__lowerArray(__setU8, 32, 0, program) || __notnull());
38
38
  initialGas = initialGas || 0n;
39
- args = __lowerArray(__setU8, 7, 0, args) || __notnull();
39
+ args = __lowerArray(__setU8, 32, 0, args) || __notnull();
40
40
  hasMetadata = hasMetadata ? 1 : 0;
41
+ useBlockGas = useBlockGas ? 1 : 0;
41
42
  try {
42
43
  exports.__setArgumentsLength(arguments.length);
43
- exports.resetJAM(program, pc, initialGas, args, hasMetadata);
44
+ exports.resetJAM(program, pc, initialGas, args, hasMetadata, useBlockGas);
44
45
  } finally {
45
46
  __release(program);
46
47
  }
47
48
  },
48
- resetGeneric(program, flatRegisters, initialGas, hasMetadata) {
49
- // assembly/api-debugger/resetGeneric(~lib/array/Array<u8>, ~lib/array/Array<u8>, u64, bool?) => void
50
- program = __retain(__lowerArray(__setU8, 7, 0, program) || __notnull());
51
- flatRegisters = __lowerArray(__setU8, 7, 0, flatRegisters) || __notnull();
49
+ resetGeneric(program, flatRegisters, initialGas, hasMetadata, useBlockGas) {
50
+ // assembly/api-debugger/resetGeneric(~lib/array/Array<u8>, ~lib/array/Array<u8>, u64, bool?, bool?) => void
51
+ program = __retain(__lowerArray(__setU8, 32, 0, program) || __notnull());
52
+ flatRegisters = __lowerArray(__setU8, 32, 0, flatRegisters) || __notnull();
52
53
  initialGas = initialGas || 0n;
53
54
  hasMetadata = hasMetadata ? 1 : 0;
55
+ useBlockGas = useBlockGas ? 1 : 0;
54
56
  try {
55
57
  exports.__setArgumentsLength(arguments.length);
56
- exports.resetGeneric(program, flatRegisters, initialGas, hasMetadata);
58
+ exports.resetGeneric(program, flatRegisters, initialGas, hasMetadata, useBlockGas);
57
59
  } finally {
58
60
  __release(program);
59
61
  }
60
62
  },
61
- resetGenericWithMemory(program, flatRegisters, pageMap, chunks, initialGas, hasMetadata) {
62
- // assembly/api-debugger/resetGenericWithMemory(~lib/array/Array<u8>, ~lib/array/Array<u8>, ~lib/typedarray/Uint8Array, ~lib/typedarray/Uint8Array, u64, bool?) => void
63
- program = __retain(__lowerArray(__setU8, 7, 0, program) || __notnull());
64
- flatRegisters = __retain(__lowerArray(__setU8, 7, 0, flatRegisters) || __notnull());
65
- pageMap = __retain(__lowerTypedArray(Uint8Array, 12, 0, pageMap) || __notnull());
66
- chunks = __lowerTypedArray(Uint8Array, 12, 0, chunks) || __notnull();
63
+ resetGenericWithMemory(program, flatRegisters, pageMap, chunks, initialGas, hasMetadata, useBlockGas) {
64
+ // assembly/api-debugger/resetGenericWithMemory(~lib/array/Array<u8>, ~lib/array/Array<u8>, ~lib/typedarray/Uint8Array, ~lib/typedarray/Uint8Array, u64, bool?, bool?) => void
65
+ program = __retain(__lowerArray(__setU8, 32, 0, program) || __notnull());
66
+ flatRegisters = __retain(__lowerArray(__setU8, 32, 0, flatRegisters) || __notnull());
67
+ pageMap = __retain(__lowerTypedArray(Uint8Array, 15, 0, pageMap) || __notnull());
68
+ chunks = __lowerTypedArray(Uint8Array, 15, 0, chunks) || __notnull();
67
69
  initialGas = initialGas || 0n;
68
70
  hasMetadata = hasMetadata ? 1 : 0;
71
+ useBlockGas = useBlockGas ? 1 : 0;
69
72
  try {
70
73
  exports.__setArgumentsLength(arguments.length);
71
- exports.resetGenericWithMemory(program, flatRegisters, pageMap, chunks, initialGas, hasMetadata);
74
+ exports.resetGenericWithMemory(program, flatRegisters, pageMap, chunks, initialGas, hasMetadata, useBlockGas);
72
75
  } finally {
73
76
  __release(program);
74
77
  __release(flatRegisters);
@@ -102,7 +105,7 @@ async function instantiate(module, imports = {}) {
102
105
  },
103
106
  setRegisters(flatRegisters) {
104
107
  // assembly/api-debugger/setRegisters(~lib/array/Array<u8>) => void
105
- flatRegisters = __lowerArray(__setU8, 7, 0, flatRegisters) || __notnull();
108
+ flatRegisters = __lowerArray(__setU8, 32, 0, flatRegisters) || __notnull();
106
109
  exports.setRegisters(flatRegisters);
107
110
  },
108
111
  getPageDump(index) {
@@ -119,7 +122,7 @@ async function instantiate(module, imports = {}) {
119
122
  },
120
123
  setMemory(address, data) {
121
124
  // assembly/api-debugger/setMemory(u32, ~lib/typedarray/Uint8Array) => bool
122
- data = __lowerTypedArray(Uint8Array, 12, 0, data) || __notnull();
125
+ data = __lowerTypedArray(Uint8Array, 15, 0, data) || __notnull();
123
126
  return exports.setMemory(address, data) != 0;
124
127
  },
125
128
  InputKind: (values => (
@@ -134,25 +137,26 @@ async function instantiate(module, imports = {}) {
134
137
  values[values.No = exports["HasMetadata.No"].valueOf()] = "No",
135
138
  values
136
139
  ))({}),
137
- getGasCosts(input, kind, withMetadata) {
138
- // assembly/api-utils/getGasCosts(~lib/array/Array<u8>, i32, i32) => ~lib/array/Array<assembly/gas-costs/BlockGasCost>
139
- input = __lowerArray(__setU8, 7, 0, input) || __notnull();
140
- return __liftArray(pointer => __liftRecord54(__getU32(pointer)), 2, exports.getGasCosts(input, kind, withMetadata) >>> 0);
140
+ getBlockGasCosts(input, kind, withMetadata) {
141
+ // assembly/api-utils/getBlockGasCosts(~lib/array/Array<u8>, i32, i32) => ~lib/array/Array<assembly/api-utils/BlockGasCost>
142
+ input = __lowerArray(__setU8, 32, 0, input) || __notnull();
143
+ return __liftArray(pointer => __liftRecord58(__getU32(pointer)), 2, exports.getBlockGasCosts(input, kind, withMetadata) >>> 0);
141
144
  },
142
145
  disassemble(input, kind, withMetadata) {
143
146
  // assembly/api-utils/disassemble(~lib/array/Array<u8>, i32, i32) => ~lib/string/String
144
- input = __lowerArray(__setU8, 7, 0, input) || __notnull();
147
+ input = __lowerArray(__setU8, 32, 0, input) || __notnull();
145
148
  return __liftString(exports.disassemble(input, kind, withMetadata) >>> 0);
146
149
  },
147
- prepareProgram(kind, hasMetadata, program, initialRegisters, initialPageMap, initialMemory, args, preallocateMemoryPages) {
148
- // assembly/api-utils/prepareProgram(i32, i32, ~lib/array/Array<u8>, ~lib/array/Array<u64>, ~lib/array/Array<assembly/api-types/InitialPage>, ~lib/array/Array<assembly/api-types/InitialChunk>, ~lib/array/Array<u8>, u32) => assembly/spi/StandardProgram
149
- program = __retain(__lowerArray(__setU8, 7, 0, program) || __notnull());
150
- initialRegisters = __retain(__lowerArray(__setU64, 56, 3, initialRegisters) || __notnull());
151
- initialPageMap = __retain(__lowerArray((pointer, value) => { __setU32(pointer, __lowerRecord50(value) || __notnull()); }, 51, 2, initialPageMap) || __notnull());
152
- initialMemory = __retain(__lowerArray((pointer, value) => { __setU32(pointer, __lowerRecord52(value) || __notnull()); }, 53, 2, initialMemory) || __notnull());
153
- args = __lowerArray(__setU8, 7, 0, args) || __notnull();
150
+ prepareProgram(kind, hasMetadata, program, initialRegisters, initialPageMap, initialMemory, args, preallocateMemoryPages, useBlockGas) {
151
+ // assembly/api-utils/prepareProgram(i32, i32, ~lib/array/Array<u8>, ~lib/array/Array<u64>, ~lib/array/Array<assembly/api-types/InitialPage>, ~lib/array/Array<assembly/api-types/InitialChunk>, ~lib/array/Array<u8>, u32, bool) => assembly/spi/StandardProgram
152
+ program = __retain(__lowerArray(__setU8, 32, 0, program) || __notnull());
153
+ initialRegisters = __retain(__lowerArray(__setU64, 60, 3, initialRegisters) || __notnull());
154
+ initialPageMap = __retain(__lowerArray((pointer, value) => { __setU32(pointer, __lowerRecord54(value) || __notnull()); }, 55, 2, initialPageMap) || __notnull());
155
+ initialMemory = __retain(__lowerArray((pointer, value) => { __setU32(pointer, __lowerRecord56(value) || __notnull()); }, 57, 2, initialMemory) || __notnull());
156
+ args = __lowerArray(__setU8, 32, 0, args) || __notnull();
157
+ useBlockGas = useBlockGas ? 1 : 0;
154
158
  try {
155
- return __liftInternref(exports.prepareProgram(kind, hasMetadata, program, initialRegisters, initialPageMap, initialMemory, args, preallocateMemoryPages) >>> 0);
159
+ return __liftInternref(exports.prepareProgram(kind, hasMetadata, program, initialRegisters, initialPageMap, initialMemory, args, preallocateMemoryPages, useBlockGas) >>> 0);
156
160
  } finally {
157
161
  __release(program);
158
162
  __release(initialRegisters);
@@ -160,30 +164,27 @@ async function instantiate(module, imports = {}) {
160
164
  __release(initialMemory);
161
165
  }
162
166
  },
163
- runProgram(program, initialGas, programCounter, logs, useSbrkGas, dumpMemory) {
164
- // assembly/api-utils/runProgram(assembly/spi/StandardProgram, i64?, u32?, bool?, bool?, bool?) => assembly/api-types/VmOutput
167
+ runProgram(program, initialGas, programCounter, logs, dumpMemory) {
168
+ // assembly/api-utils/runProgram(assembly/spi/StandardProgram, i64?, u32?, bool?, bool?) => assembly/api-types/VmOutput
165
169
  program = __lowerInternref(program) || __notnull();
166
170
  initialGas = initialGas || 0n;
167
171
  logs = logs ? 1 : 0;
168
- useSbrkGas = useSbrkGas ? 1 : 0;
169
172
  dumpMemory = dumpMemory ? 1 : 0;
170
173
  exports.__setArgumentsLength(arguments.length);
171
- return __liftRecord59(exports.runProgram(program, initialGas, programCounter, logs, useSbrkGas, dumpMemory) >>> 0);
174
+ return __liftRecord62(exports.runProgram(program, initialGas, programCounter, logs, dumpMemory) >>> 0);
172
175
  },
173
- pvmStart(program, useSbrkGas) {
174
- // assembly/api-utils/pvmStart(assembly/spi/StandardProgram, bool?) => u32
176
+ pvmStart(program) {
177
+ // assembly/api-utils/pvmStart(assembly/spi/StandardProgram) => u32
175
178
  program = __lowerInternref(program) || __notnull();
176
- useSbrkGas = useSbrkGas ? 1 : 0;
177
- exports.__setArgumentsLength(arguments.length);
178
- return exports.pvmStart(program, useSbrkGas) >>> 0;
179
+ return exports.pvmStart(program) >>> 0;
179
180
  },
180
181
  pvmDestroy(pvmId) {
181
182
  // assembly/api-utils/pvmDestroy(u32) => assembly/api-types/VmOutput | null
182
- return __liftRecord59(exports.pvmDestroy(pvmId) >>> 0);
183
+ return __liftRecord62(exports.pvmDestroy(pvmId) >>> 0);
183
184
  },
184
185
  pvmSetRegisters(pvmId, registers) {
185
186
  // assembly/api-utils/pvmSetRegisters(u32, ~lib/array/Array<u64>) => void
186
- registers = __lowerArray(__setU64, 56, 3, registers) || __notnull();
187
+ registers = __lowerArray(__setU64, 60, 3, registers) || __notnull();
187
188
  exports.pvmSetRegisters(pvmId, registers);
188
189
  },
189
190
  pvmReadMemory(pvmId, address, length) {
@@ -196,7 +197,7 @@ async function instantiate(module, imports = {}) {
196
197
  },
197
198
  pvmWriteMemory(pvmId, address, data) {
198
199
  // assembly/api-utils/pvmWriteMemory(u32, u32, ~lib/typedarray/Uint8Array) => bool
199
- data = __lowerTypedArray(Uint8Array, 12, 0, data) || __notnull();
200
+ data = __lowerTypedArray(Uint8Array, 15, 0, data) || __notnull();
200
201
  return exports.pvmWriteMemory(pvmId, address, data) != 0;
201
202
  },
202
203
  pvmResume(pvmId, gas, pc, logs) {
@@ -204,11 +205,11 @@ async function instantiate(module, imports = {}) {
204
205
  gas = gas || 0n;
205
206
  logs = logs ? 1 : 0;
206
207
  exports.__setArgumentsLength(arguments.length);
207
- return __liftRecord65(exports.pvmResume(pvmId, gas, pc, logs) >>> 0);
208
+ return __liftRecord68(exports.pvmResume(pvmId, gas, pc, logs) >>> 0);
208
209
  },
209
210
  }, exports);
210
- function __liftRecord54(pointer) {
211
- // assembly/gas-costs/BlockGasCost
211
+ function __liftRecord58(pointer) {
212
+ // assembly/api-utils/BlockGasCost
212
213
  // Hint: Opt-out from lifting as a record by providing an empty constructor
213
214
  if (!pointer) return null;
214
215
  return {
@@ -216,28 +217,28 @@ async function instantiate(module, imports = {}) {
216
217
  gas: __getU64(pointer + 8),
217
218
  };
218
219
  }
219
- function __lowerRecord50(value) {
220
+ function __lowerRecord54(value) {
220
221
  // assembly/api-types/InitialPage
221
222
  // Hint: Opt-out from lowering as a record by providing an empty constructor
222
223
  if (value == null) return 0;
223
- const pointer = exports.__pin(exports.__new(12, 50));
224
+ const pointer = exports.__pin(exports.__new(12, 54));
224
225
  __setU32(pointer + 0, value.address);
225
226
  __setU32(pointer + 4, value.length);
226
227
  __setU32(pointer + 8, value.access);
227
228
  exports.__unpin(pointer);
228
229
  return pointer;
229
230
  }
230
- function __lowerRecord52(value) {
231
+ function __lowerRecord56(value) {
231
232
  // assembly/api-types/InitialChunk
232
233
  // Hint: Opt-out from lowering as a record by providing an empty constructor
233
234
  if (value == null) return 0;
234
- const pointer = exports.__pin(exports.__new(8, 52));
235
+ const pointer = exports.__pin(exports.__new(8, 56));
235
236
  __setU32(pointer + 0, value.address);
236
- __setU32(pointer + 4, __lowerArray(__setU8, 7, 0, value.data) || __notnull());
237
+ __setU32(pointer + 4, __lowerArray(__setU8, 32, 0, value.data) || __notnull());
237
238
  exports.__unpin(pointer);
238
239
  return pointer;
239
240
  }
240
- function __liftRecord52(pointer) {
241
+ function __liftRecord56(pointer) {
241
242
  // assembly/api-types/InitialChunk
242
243
  // Hint: Opt-out from lifting as a record by providing an empty constructor
243
244
  if (!pointer) return null;
@@ -246,7 +247,7 @@ async function instantiate(module, imports = {}) {
246
247
  data: __liftArray(__getU8, 0, __getU32(pointer + 4)),
247
248
  };
248
249
  }
249
- function __liftRecord59(pointer) {
250
+ function __liftRecord62(pointer) {
250
251
  // assembly/api-types/VmOutput
251
252
  // Hint: Opt-out from lifting as a record by providing an empty constructor
252
253
  if (!pointer) return null;
@@ -257,10 +258,10 @@ async function instantiate(module, imports = {}) {
257
258
  gas: __getU64(pointer + 16),
258
259
  result: __liftArray(__getU8, 0, __getU32(pointer + 24)),
259
260
  registers: __liftArray(pointer => BigInt.asUintN(64, __getU64(pointer)), 3, __getU32(pointer + 28)),
260
- memory: __liftArray(pointer => __liftRecord52(__getU32(pointer)), 2, __getU32(pointer + 32)),
261
+ memory: __liftArray(pointer => __liftRecord56(__getU32(pointer)), 2, __getU32(pointer + 32)),
261
262
  };
262
263
  }
263
- function __liftRecord65(pointer) {
264
+ function __liftRecord68(pointer) {
264
265
  // assembly/api-types/VmPause
265
266
  // Hint: Opt-out from lifting as a record by providing an empty constructor
266
267
  if (!pointer) return null;
@@ -444,7 +445,7 @@ export const {
444
445
  setMemory,
445
446
  InputKind,
446
447
  HasMetadata,
447
- getGasCosts,
448
+ getBlockGasCosts,
448
449
  disassemble,
449
450
  prepareProgram,
450
451
  runProgram,
Binary file
@@ -1,7 +1,7 @@
1
1
  import { Gas } from "./gas";
2
- export declare function resetJAM(program: u8[], pc: u32, initialGas: Gas, args: u8[], hasMetadata?: boolean): void;
3
- export declare function resetGeneric(program: u8[], flatRegisters: u8[], initialGas: Gas, hasMetadata?: boolean): void;
4
- export declare function resetGenericWithMemory(program: u8[], flatRegisters: u8[], pageMap: Uint8Array, chunks: Uint8Array, initialGas: Gas, hasMetadata?: boolean): void;
2
+ export declare function resetJAM(program: u8[], pc: u32, initialGas: Gas, args: u8[], hasMetadata?: boolean, useBlockGas?: boolean): void;
3
+ export declare function resetGeneric(program: u8[], flatRegisters: u8[], initialGas: Gas, hasMetadata?: boolean, useBlockGas?: boolean): void;
4
+ export declare function resetGenericWithMemory(program: u8[], flatRegisters: u8[], pageMap: Uint8Array, chunks: Uint8Array, initialGas: Gas, hasMetadata?: boolean, useBlockGas?: boolean): void;
5
5
  export declare function nextStep(): boolean;
6
6
  export declare function nSteps(steps: u32): boolean;
7
7
  export declare function getProgramCounter(): u32;
@@ -8,9 +8,9 @@ import { deblob, extractCodeAndMetadata, liftBytes } from "./program";
8
8
  import { NO_OF_REGISTERS, newRegisters, REG_SIZE_BYTES } from "./registers";
9
9
  import { decodeSpi } from "./spi";
10
10
  let interpreter = null;
11
- export function resetJAM(program, pc, initialGas, args, hasMetadata = false) {
11
+ export function resetJAM(program, pc, initialGas, args, hasMetadata = false, useBlockGas = false) {
12
12
  const code = hasMetadata ? extractCodeAndMetadata(liftBytes(program)).code : liftBytes(program);
13
- const p = decodeSpi(code, liftBytes(args), 128);
13
+ const p = decodeSpi(code, liftBytes(args), 128, useBlockGas);
14
14
  const int = new Interpreter(p.program, p.registers, p.memory);
15
15
  int.nextPc = pc;
16
16
  int.gas.set(initialGas);
@@ -19,9 +19,9 @@ export function resetJAM(program, pc, initialGas, args, hasMetadata = false) {
19
19
  }
20
20
  interpreter = int;
21
21
  }
22
- export function resetGeneric(program, flatRegisters, initialGas, hasMetadata = false) {
22
+ export function resetGeneric(program, flatRegisters, initialGas, hasMetadata = false, useBlockGas = false) {
23
23
  const code = hasMetadata ? extractCodeAndMetadata(liftBytes(program)).code : liftBytes(program);
24
- const p = deblob(code);
24
+ const p = deblob(code, useBlockGas);
25
25
  const registers = newRegisters();
26
26
  fillRegisters(registers, flatRegisters);
27
27
  const int = new Interpreter(p, registers);
@@ -31,9 +31,9 @@ export function resetGeneric(program, flatRegisters, initialGas, hasMetadata = f
31
31
  }
32
32
  interpreter = int;
33
33
  }
34
- export function resetGenericWithMemory(program, flatRegisters, pageMap, chunks, initialGas, hasMetadata = false) {
34
+ export function resetGenericWithMemory(program, flatRegisters, pageMap, chunks, initialGas, hasMetadata = false, useBlockGas = false) {
35
35
  const code = hasMetadata ? extractCodeAndMetadata(liftBytes(program)).code : liftBytes(program);
36
- const p = deblob(code);
36
+ const p = deblob(code, useBlockGas);
37
37
  const registers = newRegisters();
38
38
  fillRegisters(registers, flatRegisters);
39
39
  const builder = new MemoryBuilder();
@@ -5,7 +5,7 @@ import { Program } from "./program";
5
5
  export declare function getAssembly(p: Program): string;
6
6
  export declare function buildMemory(builder: MemoryBuilder, pages: InitialPage[], chunks: InitialChunk[]): Memory;
7
7
  /** Initialize new VM for execution. */
8
- export declare function vmInit(input: VmInput, useSbrkGas?: boolean): Interpreter;
8
+ export declare function vmInit(input: VmInput): Interpreter;
9
9
  /** Initialize & run & destroy a VM in a single go. */
10
10
  export declare function vmRunOnce(input: VmInput, options: VmRunOptions): VmOutput;
11
11
  export declare function vmExecute(int: Interpreter, logs?: boolean): void;
@@ -60,16 +60,15 @@ export function buildMemory(builder, pages, chunks) {
60
60
  return builder.build(sbrkIndex);
61
61
  }
62
62
  /** Initialize new VM for execution. */
63
- export function vmInit(input, useSbrkGas = false) {
63
+ export function vmInit(input) {
64
64
  const int = new Interpreter(input.program, input.registers, input.memory);
65
- int.useSbrkGas = useSbrkGas;
66
65
  int.nextPc = input.pc;
67
66
  int.gas.set(input.gas);
68
67
  return int;
69
68
  }
70
69
  /** Initialize & run & destroy a VM in a single go. */
71
70
  export function vmRunOnce(input, options) {
72
- const int = vmInit(input, options.useSbrkGas);
71
+ const int = vmInit(input);
73
72
  vmExecute(int, options.logs);
74
73
  return vmDestroy(int, options.dumpMemory);
75
74
  }
@@ -15,7 +15,6 @@ export declare class InitialChunk {
15
15
  data: u8[];
16
16
  }
17
17
  export declare class VmRunOptions {
18
- useSbrkGas: boolean;
19
18
  logs: boolean;
20
19
  dumpMemory: boolean;
21
20
  }
@@ -16,7 +16,6 @@ export class InitialChunk {
16
16
  }
17
17
  export class VmRunOptions {
18
18
  constructor() {
19
- this.useSbrkGas = false;
20
19
  this.logs = false;
21
20
  this.dumpMemory = false;
22
21
  }
@@ -1,6 +1,6 @@
1
1
  import { InitialChunk, InitialPage, VmOutput, VmPause } from "./api-types";
2
2
  import { Gas } from "./gas";
3
- import { BlockGasCost } from "./gas-costs";
3
+ import { ProgramCounter } from "./program";
4
4
  import { StandardProgram } from "./spi";
5
5
  export declare enum InputKind {
6
6
  Generic = 0,
@@ -10,7 +10,11 @@ export declare enum HasMetadata {
10
10
  Yes = 0,
11
11
  No = 1
12
12
  }
13
- export declare function getGasCosts(input: u8[], kind: InputKind, withMetadata: HasMetadata): BlockGasCost[];
13
+ declare class BlockGasCost {
14
+ pc: ProgramCounter;
15
+ gas: Gas;
16
+ }
17
+ export declare function getBlockGasCosts(input: u8[], kind: InputKind, withMetadata: HasMetadata): BlockGasCost[];
14
18
  export declare function disassemble(input: u8[], kind: InputKind, withMetadata: HasMetadata): string;
15
19
  export declare function prepareProgram(kind: InputKind, hasMetadata: HasMetadata, program: u8[],
16
20
  /** NOTE: ignored in case of SPI. */
@@ -22,15 +26,17 @@ initialMemory: InitialChunk[],
22
26
  /** NOTE: ONLY needed for SPI. */
23
27
  args: u8[],
24
28
  /** Preallocate a bunch of memory pages for faster execution. */
25
- preallocateMemoryPages: u32): StandardProgram;
29
+ preallocateMemoryPages: u32,
30
+ /** Compute gas per-block instead of per-instruction. */
31
+ useBlockGas: boolean): StandardProgram;
26
32
  /** Execute PVM program and stop. */
27
- export declare function runProgram(program: StandardProgram, initialGas?: i64, programCounter?: u32, logs?: boolean, useSbrkGas?: boolean, dumpMemory?: boolean): VmOutput;
33
+ export declare function runProgram(program: StandardProgram, initialGas?: i64, programCounter?: u32, logs?: boolean, dumpMemory?: boolean): VmOutput;
28
34
  /**
29
35
  * Allocate new PVM instance to execute given program.
30
36
  *
31
37
  * NOTE: the PVM MUST be de-allocated using `pvmDestroy`.
32
38
  */
33
- export declare function pvmStart(program: StandardProgram, useSbrkGas?: boolean): u32;
39
+ export declare function pvmStart(program: StandardProgram): u32;
34
40
  /** Deallocate PVM resources. */
35
41
  export declare function pvmDestroy(pvmId: u32): VmOutput | null;
36
42
  /** Set register values of a paused PVM. */
@@ -70,3 +76,4 @@ export declare function pvmGetPagePointer(pvmId: u32, page: u32): usize;
70
76
  export declare function pvmWriteMemory(pvmId: u32, address: u32, data: Uint8Array): boolean;
71
77
  /** Resume execution of paused VM. */
72
78
  export declare function pvmResume(pvmId: u32, gas: Gas, pc: u32, logs?: boolean): VmPause | null;
79
+ export {};
@@ -1,8 +1,6 @@
1
1
  import { buildMemory, getAssembly, vmDestroy, vmExecute, vmInit, vmRunOnce } from "./api-internal";
2
2
  import { VmInput, VmPause, VmRunOptions } from "./api-types";
3
- import { computeGasCosts } from "./gas-costs";
4
3
  import { MaybePageFault, MemoryBuilder } from "./memory";
5
- import { portable } from "./portable";
6
4
  import { deblob, extractCodeAndMetadata, liftBytes } from "./program";
7
5
  import { NO_OF_REGISTERS, newRegisters } from "./registers";
8
6
  import { decodeSpi, StandardProgram } from "./spi";
@@ -16,13 +14,29 @@ export var HasMetadata;
16
14
  HasMetadata[HasMetadata["Yes"] = 0] = "Yes";
17
15
  HasMetadata[HasMetadata["No"] = 1] = "No";
18
16
  })(HasMetadata || (HasMetadata = {}));
19
- export function getGasCosts(input, kind, withMetadata) {
20
- const program = prepareProgram(kind, withMetadata, input, [], [], [], [], 0);
21
- // @ts-ignore: AS returns T[], JS returns iterator - asArray handles both
22
- return portable.asArray(computeGasCosts(program.program).values());
17
+ class BlockGasCost {
18
+ constructor() {
19
+ this.pc = 0;
20
+ this.gas = 0;
21
+ }
22
+ }
23
+ export function getBlockGasCosts(input, kind, withMetadata) {
24
+ const program = prepareProgram(kind, withMetadata, input, [], [], [], [], 0, true);
25
+ const blockCosts = [];
26
+ const costs = program.program.gasCosts.codeAndGas;
27
+ for (let n = 0; n < costs.length; n += 1) {
28
+ const gas = costs[n] >> 8;
29
+ if (gas !== 0) {
30
+ const x = new BlockGasCost();
31
+ x.pc = n;
32
+ x.gas = costs[n];
33
+ blockCosts.push(x);
34
+ }
35
+ }
36
+ return blockCosts;
23
37
  }
24
38
  export function disassemble(input, kind, withMetadata) {
25
- const program = prepareProgram(kind, withMetadata, input, [], [], [], [], 0);
39
+ const program = prepareProgram(kind, withMetadata, input, [], [], [], [], 0, false);
26
40
  let output = "";
27
41
  if (withMetadata === HasMetadata.Yes) {
28
42
  output = "Metadata: \n";
@@ -43,7 +57,9 @@ initialMemory,
43
57
  /** NOTE: ONLY needed for SPI. */
44
58
  args,
45
59
  /** Preallocate a bunch of memory pages for faster execution. */
46
- preallocateMemoryPages) {
60
+ preallocateMemoryPages,
61
+ /** Compute gas per-block instead of per-instruction. */
62
+ useBlockGas) {
47
63
  let code = liftBytes(program);
48
64
  let metadata = new Uint8Array(0);
49
65
  if (hasMetadata === HasMetadata.Yes) {
@@ -54,7 +70,7 @@ preallocateMemoryPages) {
54
70
  metadata = data.metadata;
55
71
  }
56
72
  if (kind === InputKind.Generic) {
57
- const program = deblob(code);
73
+ const program = deblob(code, useBlockGas);
58
74
  const builder = new MemoryBuilder(preallocateMemoryPages);
59
75
  const memory = buildMemory(builder, initialPageMap, initialMemory);
60
76
  const registers = newRegisters();
@@ -74,13 +90,12 @@ preallocateMemoryPages) {
74
90
  throw new Error(`Unknown kind: ${kind}`);
75
91
  }
76
92
  /** Execute PVM program and stop. */
77
- export function runProgram(program, initialGas = 0, programCounter = 0, logs = false, useSbrkGas = false, dumpMemory = false) {
93
+ export function runProgram(program, initialGas = 0, programCounter = 0, logs = false, dumpMemory = false) {
78
94
  const vmInput = new VmInput(program.program, program.memory, program.registers);
79
95
  vmInput.gas = i64(initialGas);
80
96
  vmInput.pc = programCounter;
81
97
  const vmOptions = new VmRunOptions();
82
98
  vmOptions.logs = logs;
83
- vmOptions.useSbrkGas = useSbrkGas;
84
99
  vmOptions.dumpMemory = dumpMemory;
85
100
  return vmRunOnce(vmInput, vmOptions);
86
101
  }
@@ -93,10 +108,10 @@ const pvms = new Map();
93
108
  *
94
109
  * NOTE: the PVM MUST be de-allocated using `pvmDestroy`.
95
110
  */
96
- export function pvmStart(program, useSbrkGas = false) {
111
+ export function pvmStart(program) {
97
112
  const vmInput = new VmInput(program.program, program.memory, program.registers);
98
113
  nextPvmId += 1;
99
- pvms.set(nextPvmId, vmInit(vmInput, useSbrkGas));
114
+ pvms.set(nextPvmId, vmInit(vmInput));
100
115
  return nextPvmId;
101
116
  }
102
117
  /** Deallocate PVM resources. */
@@ -14,9 +14,9 @@ export declare enum Arguments {
14
14
  ThreeReg = 12
15
15
  }
16
16
  /** How many numbers in `Args` is relevant for given `Arguments`. */
17
- export declare const RELEVANT_ARGS: number[];
17
+ export declare const RELEVANT_ARGS: StaticArray<i32>;
18
18
  /** How many bytes is required by given `Arguments`. */
19
- export declare const REQUIRED_BYTES: number[];
19
+ export declare const REQUIRED_BYTES: StaticArray<i32>;
20
20
  export declare class Args {
21
21
  fill(a: u32, b?: u32, c?: u32, d?: u32): Args;
22
22
  /**
@@ -37,8 +37,8 @@ export declare class Args {
37
37
  c: u32;
38
38
  d: u32;
39
39
  }
40
- type ArgsDecoder = (args: Args, code: u8[], offset: u32, end: u32) => Args;
41
- export declare const DECODERS: ArgsDecoder[];
40
+ type ArgsDecoder = (args: Args, code: StaticArray<u8>, offset: u32, end: u32) => Args;
41
+ export declare const DECODERS: StaticArray<ArgsDecoder>;
42
42
  export declare function lowNibble(byte: u8): u8;
43
43
  export declare function higNibble(byte: u8): u8;
44
44
  export {};
@@ -1,4 +1,4 @@
1
- import { minI32 } from "./math";
1
+ import { IntMath } from "./math";
2
2
  import { portable } from "./portable";
3
3
  export var Arguments;
4
4
  (function (Arguments) {
@@ -17,9 +17,9 @@ export var Arguments;
17
17
  Arguments[Arguments["ThreeReg"] = 12] = "ThreeReg";
18
18
  })(Arguments || (Arguments = {}));
19
19
  /** How many numbers in `Args` is relevant for given `Arguments`. */
20
- export const RELEVANT_ARGS = [0, 1, 2, 1, 2, 3, 3, 3, 2, 3, 3, 4, 3];
20
+ export const RELEVANT_ARGS = StaticArray.fromArray([0, 1, 2, 1, 2, 3, 3, 3, 2, 3, 3, 4, 3]);
21
21
  /** How many bytes is required by given `Arguments`. */
22
- export const REQUIRED_BYTES = [0, 0, 1, 0, 1, 9, 1, 1, 1, 1, 1, 2, 2];
22
+ export const REQUIRED_BYTES = StaticArray.fromArray([0, 0, 1, 0, 1, 9, 1, 1, 1, 1, 1, 2, 2]);
23
23
  // @unmanaged
24
24
  export class Args {
25
25
  constructor() {
@@ -50,13 +50,13 @@ export class Args {
50
50
  }
51
51
  }
52
52
  function twoImm(args, code, offset, end) {
53
- const low = lowNibble(portable.arrayAt(code, offset));
54
- const split = minI32(4, low) + 1;
53
+ const low = lowNibble(portable.staticArrayAt(code, offset));
54
+ const split = IntMath.minI32(4, low) + 1;
55
55
  const first = decodeI32(code, offset + 1, offset + split);
56
56
  const second = decodeI32(code, offset + split, end);
57
57
  return args.fill(first, second, 0, 0);
58
58
  }
59
- export const DECODERS = [
59
+ export const DECODERS = StaticArray.fromArray([
60
60
  // DECODERS[Arguments.Zero] =
61
61
  (args, _d, _o, _l) => {
62
62
  return args.fill(0, 0, 0, 0);
@@ -86,7 +86,7 @@ export const DECODERS = [
86
86
  (args, data, o, lim) => {
87
87
  const h = higNibble(data[o]);
88
88
  const l = lowNibble(data[o]);
89
- const split = minI32(4, h) + 1;
89
+ const split = IntMath.minI32(4, h) + 1;
90
90
  const immA = decodeI32(data, o + 1, o + split);
91
91
  const immB = decodeI32(data, o + split, lim);
92
92
  return args.fill(l, immA, immB, 0);
@@ -95,7 +95,7 @@ export const DECODERS = [
95
95
  (args, data, o, lim) => {
96
96
  const h = higNibble(data[o]);
97
97
  const l = lowNibble(data[o]);
98
- const split = minI32(4, h) + 1;
98
+ const split = IntMath.minI32(4, h) + 1;
99
99
  const immA = decodeI32(data, o + 1, o + split);
100
100
  const offs = decodeI32(data, o + split, lim);
101
101
  return args.fill(l, immA, offs, 0);
@@ -130,7 +130,7 @@ export const DECODERS = [
130
130
  const b = lowNibble(data[o + 1]);
131
131
  return args.fill(hig, low, b, 0);
132
132
  },
133
- ];
133
+ ]);
134
134
  // @inline
135
135
  export function lowNibble(byte) {
136
136
  return byte & 0xf;
@@ -149,7 +149,7 @@ function decodeI32(input, start, end) {
149
149
  for (let i = 0; i < len; i++) {
150
150
  num |= u32(input[start + i]) << (i * 8);
151
151
  }
152
- const msb = portable.arrayAt(input, start + len - 1) & 0x80;
152
+ const msb = portable.staticArrayAt(input, start + len - 1) & 0x80;
153
153
  if (len < 4 && msb > 0) {
154
154
  num |= 4294967295 << (len * 8);
155
155
  }
@@ -7,5 +7,5 @@ export declare class GasCounter {
7
7
  constructor(gas: Gas);
8
8
  set(g: Gas): void;
9
9
  get(): Gas;
10
- sub(g: Gas): boolean;
10
+ sub(g: u32): boolean;
11
11
  }
@@ -19,11 +19,12 @@ export class GasCounter {
19
19
  return this.gas;
20
20
  }
21
21
  sub(g) {
22
- if (g > this.gas) {
22
+ const cost = u64(g);
23
+ if (cost > this.gas) {
23
24
  this.gas = u64(0);
24
25
  return true;
25
26
  }
26
- this.gas = this.gas - g;
27
+ this.gas = this.gas - cost;
27
28
  return false;
28
29
  }
29
30
  }