diff --git a/README.md b/README.md index 52a054b..ce3c145 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,23 @@ # 🍍 anan-as -Assembly Script implementation of the JAM PVM (32bit). +AssemblyScript implementation of the JAM PVM (64-bit). [Demo](https://todr.me/anan-as) -#### Todo +## Todo - [x] Memory - [x] [JAM tests](https://github.com/w3f/jamtestvectors/pull/3) compatibility - [x] 64-bit & new instructions ([GrayPaper v0.5.0](https://graypaper.fluffylabs.dev)) - [x] GP 0.5.4 compatibility (ZBB extensions) -### Why? +## Why? -- [Pineaples](https://en.wikipedia.org/wiki/Ananas) are cool. +- [Pineapples](https://en.wikipedia.org/wiki/Ananas) are cool. - [JAM](https://graypaper.com/) is promising. - [PVM](https://github.com/paritytech/polkavm) is neat. - -### Useful where? +## Useful where? - Potentially as an alternative implementation for [`typeberry`](https://github.com/fluffylabs). - To test out the [PVM debugger](https://pvm.fluffylabs.dev). @@ -54,9 +53,11 @@ ananAs.__collect(); ``` -### Raw Bindings +## Raw Bindings -Raw bindings give you direct access to WebAssembly exports without the JavaScript wrapper layer. This is useful for instantiating multiple instances or when you need more control: +Raw bindings give you direct access to WebAssembly exports +without the JavaScript wrapper layer. +This is useful for instantiating multiple instances or when you need more control: ```javascript // Raw bindings @@ -74,9 +75,10 @@ const ananAs = await instantiate(module); ``` -### Version Tags +## Version Tags -When installing the package, you can choose between stable releases and bleeding-edge builds: +When installing the package, you can choose between stable releases +and bleeding-edge builds: ```bash # Latest stable release @@ -89,24 +91,25 @@ npm install @fluffylabs/anan-as@next ## Building To download the dependencies: -``` -$ npm ci + +```cmd +npm ci ``` To build the WASM modules (in `./build/{release,debug}.wasm`): -``` -$ npm run build +```cmd +npm run build ``` To run the example in the browser at [http://localhost:3000](http://localhost:3000). -``` -$ npm run web +```cmd +npm run web ``` To run JSON test vectors. -``` -$ npm start ./path/to/tests/*.json +```cmd +npm start ./path/to/tests/*.json ``` diff --git a/assembly/api-debugger.ts b/assembly/api-debugger.ts index d5c0045..1ea1f70 100644 --- a/assembly/api-debugger.ts +++ b/assembly/api-debugger.ts @@ -167,6 +167,20 @@ export function getPageDump(index: u32): Uint8Array { return page; } +export function getMemory(address: u32, length: u32): Uint8Array { + if (interpreter === null) { + return new Uint8Array(0); + } + const int = interpreter; + const result = new Uint8Array(length); + const faultRes = new MaybePageFault(); + int.memory.bytesRead(faultRes, address, result, 0); + if (faultRes.isFault) { + return new Uint8Array(0); + } + return result; +} + export function setMemory(address: u32, data: Uint8Array): void { if (interpreter === null) { return; diff --git a/assembly/api-internal.ts b/assembly/api-internal.ts index 4291cd1..3106d0b 100644 --- a/assembly/api-internal.ts +++ b/assembly/api-internal.ts @@ -75,8 +75,10 @@ export function runVm(input: VmInput, logs: boolean = false, useSbrkGas: boolean int.useSbrkGas = useSbrkGas; int.nextPc = input.pc; int.gas.set(input.gas); - - return executeProgram(int, logs); + const result = executeProgram(int, logs); + // release used pages back + int.memory.free(); + return result; } export function getOutputChunks(memory: Memory): InitialChunk[] { @@ -147,6 +149,7 @@ function executeProgram(int: Interpreter, logs: boolean = false): VmOutput { if (logs) console.log(`REGISTERS = ${int.registers.join(", ")} (final)`); if (logs) console.log(`REGISTERS = ${int.registers.map((x: u64) => `0x${x.toString(16)}`).join(", ")} (final)`); if (logs) console.log(`Finished with status: ${int.status}`); + if (logs) console.log(`Exit code: ${int.exitCode}`); break; } @@ -182,8 +185,5 @@ function executeProgram(int: Interpreter, logs: boolean = false): VmOutput { output.memory = getOutputChunks(int.memory); output.exitCode = int.exitCode; - // release used pages back - int.memory.free(); - return output; }