Skip to content

Commit

Permalink
Merge branch 'main' into infinite-scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian50 committed Nov 13, 2024
2 parents b648d49 + a2313c6 commit b3d031e
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 209 deletions.
18 changes: 14 additions & 4 deletions src/packages/web-worker/command-handlers/init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InitialState } from "@/types/pvm";
import { initPvm } from "../pvm";
import { getState, isInternalPvm, regsAsUint8 } from "../utils";
import { chunksAsUint8, getState, isInternalPvm, pageMapAsUint8, regsAsUint8 } from "../utils";
import { logger } from "@/utils/loggerService";
import { CommandStatus, PvmApiInterface } from "../types";

Expand All @@ -26,9 +26,19 @@ const init = async ({ pvm, program, initialState }: InitParams) => {
} else {
logger.info("PVM init external", pvm);
const gas = initialState.gas || 10000;
pvm.resetGeneric(program, regsAsUint8(initialState.regs), BigInt(gas));
pvm.setNextProgramCounter(initialState.pc ?? 0);
pvm.setGasLeft(BigInt(gas));
if (pvm.resetGenericWithMemory) {
pvm.resetGenericWithMemory(
program,
regsAsUint8(initialState.regs),
pageMapAsUint8(initialState.pageMap),
chunksAsUint8(initialState.memory),
BigInt(gas),
);
} else if (pvm.resetGeneric) {
pvm.resetGeneric(program, regsAsUint8(initialState.regs), BigInt(gas));
}
pvm.setNextProgramCounter && pvm.setNextProgramCounter(initialState.pc ?? 0);
pvm.setGasLeft && pvm.setGasLeft(BigInt(gas));
pvm.nextStep();
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/packages/web-worker/command-handlers/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const load = async (args: LoadParams): Promise<PvmApiInterface | null> => {

logger.info("Load WASM from file", file);
const bytes = await file.arrayBuffer();
return await loadArrayBufferAsWasm(bytes);
return await loadArrayBufferAsWasm(bytes, args.params.lang);
} else if (args.type === PvmTypes.WASM_URL) {
const url = args.params.url ?? "";
const isValidUrl = Boolean(new URL(url));
Expand Down
2 changes: 1 addition & 1 deletion src/packages/web-worker/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CurrentInstruction, ExpectedState, InitialState } from "@/types/pvm";
import { SupportedLangs } from "./utils";
import { WasmPvmShellInterface } from "./wasmPvmShell";
import { WasmPvmShellInterface } from "./wasmBindgenShell";
import { Pvm as InternalPvm } from "@/types/pvm";

type CommonWorkerResponseParams = { status: CommandStatus; error?: unknown; messageId: string };
Expand Down
61 changes: 53 additions & 8 deletions src/packages/web-worker/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ExpectedState, Pvm as InternalPvm, RegistersArray } from "@/types/pvm.ts";
import { ExpectedState, Pvm as InternalPvm, MemoryChunkItem, PageMapItem, RegistersArray } from "@/types/pvm.ts";
import { Pvm as InternalPvmInstance } from "@typeberry/pvm-debugger-adapter";
import { createWasmPvmShell } from "@/packages/web-worker/wasmPvmShell.ts";
import { createWasmPvmShell } from "@/packages/web-worker/wasmBindgenShell.ts";
import "./goWasmExec.js";
import "./goWasmExec.d.ts";
import { createGoWasmPvmShell } from "@/packages/web-worker/wasmGoPvmShell.ts";
import { createGoWasmPvmShell } from "@/packages/web-worker/wasmGoShell.ts";
import { logger } from "@/utils/loggerService.tsx";
import { PvmApiInterface } from "./types.ts";

Expand Down Expand Up @@ -40,15 +40,24 @@ export function regsAsUint8(regs?: RegistersArray): Uint8Array {

let i = 0;
for (const reg of regs) {
arr[i] = reg & 0xff;
arr[i + 1] = (reg >> 8) & 0xff;
arr[i + 2] = (reg >> 16) & 0xff;
arr[i + 3] = (reg >> 24) & 0xff;
i += 4;
const bytes = u32_le_bytes(reg);
for (let a = 0; a < bytes.length; a += 1) {
arr[i] = bytes[a];
i += 1;
}
}
return arr;
}

export function u32_le_bytes(val: number) {
const out = new Uint8Array(4);
out[0] = val & 0xff;
out[1] = (val >> 8) & 0xff;
out[2] = (val >> 16) & 0xff;
out[3] = (val >> 24) & 0xff;
return out;
}

export function uint8asRegs(arr: Uint8Array): RegistersArray {
const regs: RegistersArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const u32Regs = new Uint32Array(arr.buffer);
Expand Down Expand Up @@ -100,3 +109,39 @@ export function getMemorySize(pvm: PvmApiInterface | null) {

return page.length;
}

export function chunksAsUint8(memory?: MemoryChunkItem[]): Uint8Array {
if (!memory) {
return new Uint8Array();
}

const result = [];
for (const chunk of memory) {
const address = u32_le_bytes(chunk.address);
const length = u32_le_bytes(chunk.contents.length);
// chunk: (u32, u32, Vec<u8>)
result.push(...address);
result.push(...length);
result.push(...chunk.contents);
}

return new Uint8Array(result);
}
export function pageMapAsUint8(pageMap?: PageMapItem[]): Uint8Array {
if (!pageMap) {
return new Uint8Array();
}

const result = [];
for (const page of pageMap) {
const address = u32_le_bytes(page.address);
const length = u32_le_bytes(page.length);
const isWriteable = page["is-writable"] ? 1 : 0;
// page: (u32, u32, bool)
result.push(...address);
result.push(...length);
result.push(isWriteable);
}

return new Uint8Array(result);
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ export function resetGeneric(program, registers, gas) {
wasm.reset(ptr0, len0, ptr1, len1, gas);
}

/**
* @param {Uint8Array} program
* @param {Uint8Array} registers
* @param {Uint8Array} page_map
* @param {Uint8Array} chunks
* @param {bigint} gas
*/
export function resetGenericWithMemory(program, registers, page_map, chunks, gas) {
// TODO [ToDr] Temporary support for older versions.
if (!wasm.resetGenericWithMemory) {
return resetGeneric(program, registers, gas);
}

const ptr0 = passArray8ToWasm0(program, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm0(registers, wasm.__wbindgen_malloc);
const len1 = WASM_VECTOR_LEN;
const ptr2 = passArray8ToWasm0(page_map, wasm.__wbindgen_malloc);
const len2 = WASM_VECTOR_LEN;
const ptr3 = passArray8ToWasm0(chunks, wasm.__wbindgen_malloc);
const len3 = WASM_VECTOR_LEN;
wasm.resetGenericWithMemory(ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, gas);
}

/**
* @returns {boolean}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { Status } from "@/types/pvm.ts";
import * as wasm from "./wasmInit";
import * as wasm from "./wasmBindgenInit";

export interface WasmPvmShellInterface {
__wbg_set_wasm(val: unknown): void;
setNextProgramCounter(pc: number): void;
setGasLeft(gas: bigint): void;
setNextProgramCounter?(pc: number): void;
setGasLeft?(gas: bigint): void;
resetGeneric(program: Uint8Array, registers: Uint8Array, gas: bigint): void;
resetGenericWithMemory?(
program: Uint8Array,
registers: Uint8Array,
pageMap: Uint8Array,
chunks: Uint8Array,
gas: bigint,
): void;
nextStep(): boolean;
getProgramCounter(): number;
getStatus(): Status;
Expand All @@ -18,6 +25,7 @@ export function createWasmPvmShell(): WasmPvmShellInterface {
const {
__wbg_set_wasm,
resetGeneric,
resetGenericWithMemory,
nextStep,
getProgramCounter,
setNextProgramCounter,
Expand All @@ -30,6 +38,7 @@ export function createWasmPvmShell(): WasmPvmShellInterface {
return {
__wbg_set_wasm,
resetGeneric,
resetGenericWithMemory,
nextStep,
getProgramCounter,
setNextProgramCounter,
Expand Down
178 changes: 178 additions & 0 deletions src/packages/web-worker/wasmGoInit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
/* eslint-disable */

// TODO [ToDr] This file is most likely auto-generated, figure out where from.

let wasm;
let cachedUint8ArrayMemory0 = null;
let cachedDataViewMemory0 = null;
let WASM_VECTOR_LEN = 0;

export function __wbg_set_wasm(val) {
wasm = val;
cachedUint8ArrayMemory0 = null;
cachedDataViewMemory0 = null;
}

export function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8ArrayMemory0;
}

export function passArray8ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 1, 1) >>> 0;
getUint8ArrayMemory0().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}

/**
* @param {Uint8Array} program
* @param {Uint8Array} registers
* @param {bigint} gas
*/
export function reset(program, registers, gas) {
var malloc = null;

if ("__wbindgen_add_to_stack_pointer" in wasm) {
malloc = wasm.wasm.__wbindgen_malloc;
} else if ("malloc" in wasm) {
malloc = wasm.malloc;
}

const ptr0 = passArray8ToWasm0(program, malloc);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArray8ToWasm0(registers, malloc);
const len1 = WASM_VECTOR_LEN;
console.log("---- reset ----", wasm, ptr0, len0, ptr1, len1, gas);
wasm.reset(ptr0, len0, ptr1, len1, gas);
}

/**
* @returns {boolean}
*/
export function nextStep() {
const ret = wasm.nextStep();
return ret !== 0;
}

/**
* @returns {number}
*/
export function getProgramCounter() {
const ret = wasm.getProgramCounter();
return ret >>> 0;
}

/**
* @returns {Status}
*/
export function getStatus() {
const ret = wasm.getStatus();
return ret;
}

/**
* @returns {bigint}
*/
export function getGasLeft() {
const ret = wasm.getGasLeft();
return ret;
}

export function getDataViewMemory0() {
if (
cachedDataViewMemory0 === null ||
cachedDataViewMemory0.buffer.detached === true ||
(cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)
) {
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
}
return cachedDataViewMemory0;
}

export function getArrayU8FromWasm0(ptr, len) {
ptr = ptr >>> 0;
return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
}

/**
* @returns {Uint8Array}
*/
export function getRegisters() {
var retptr = null;

try {
if ("__wbindgen_add_to_stack_pointer" in wasm) {
retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
} else if ("malloc" in wasm) {
retptr = wasm.malloc(16);
}

wasm.getRegisters(retptr);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
var v1 = getArrayU8FromWasm0(r0, r1).slice();

if ("__wbindgen_free" in wasm) {
wasm.__wbindgen_free(r0, r1 * 1, 1);
} else if ("free" in wasm) {
wasm.free(retptr);
}

return v1;
} finally {
if ("__wbindgen_add_to_stack_pointer" in wasm) {
retptr = wasm.__wbindgen_add_to_stack_pointer(16);
}
}
}

/**
* @param {number} index
* @returns {Uint8Array}
*/
export function getPageDump(index) {
var retptr = null;

try {
if ("__wbindgen_add_to_stack_pointer" in wasm) {
retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
} else if ("malloc" in wasm) {
retptr = wasm.malloc(16);
}

wasm.getPageDump(retptr, index);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
var v1 = getArrayU8FromWasm0(r0, r1).slice();

if ("__wbindgen_free" in wasm) {
wasm.__wbindgen_free(r0, r1 * 1, 1);
} else if ("free" in wasm) {
wasm.free(retptr);
}

return v1;
} finally {
if ("__wbindgen_add_to_stack_pointer" in wasm) {
retptr = wasm.__wbindgen_add_to_stack_pointer(16);
}
}
}

/**
*/
export const Status = Object.freeze({
Ok: 0,
"0": "Ok",
Halt: 1,
"1": "Halt",
Panic: 2,
"2": "Panic",
OutOfGas: 3,
"3": "OutOfGas",
});
Loading

0 comments on commit b3d031e

Please sign in to comment.