Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
```
14 changes: 14 additions & 0 deletions assembly/api-debugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>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;
}
Comment thread
tomusdrw marked this conversation as resolved.

export function setMemory(address: u32, data: Uint8Array): void {
if (interpreter === null) {
return;
Expand Down
10 changes: 5 additions & 5 deletions assembly/api-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();

Comment thread
tomusdrw marked this conversation as resolved.
return output;
}