-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
307 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { runInit } from "./init"; | ||
import { runLoad } from "./load"; | ||
import { runStep } from "./step"; | ||
|
||
export default { | ||
runInit, | ||
runLoad, | ||
runStep, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { InitialState } from "@/types/pvm"; | ||
import { initPvm } from "../pvm"; | ||
import { getState, isInternalPvm, regsAsUint8 } from "../utils"; | ||
import { CommandStatus, PvmApiInterface } from "../worker"; | ||
import { Pvm as InternalPvmInstance } from "@typeberry/pvm-debugger-adapter"; | ||
import { logger } from "@/utils/loggerService"; | ||
|
||
export type InitParams = { | ||
pvm: PvmApiInterface | null; | ||
program: Uint8Array; | ||
initialState: InitialState; | ||
}; | ||
export type InitResponse = { | ||
initialState: InitialState; | ||
status: CommandStatus; | ||
error?: unknown; | ||
}; | ||
|
||
const init = async ({ pvm, program, initialState }: InitParams) => { | ||
if (!pvm) { | ||
throw new Error("PVM is uninitialized."); | ||
} | ||
if (isInternalPvm(pvm)) { | ||
logger.info("PVM init internal", pvm); | ||
// TODO Fix type guards | ||
initPvm(pvm as InternalPvmInstance, program, initialState); | ||
} else { | ||
logger.info("PVM init external", pvm); | ||
const gas = initialState.gas || 10000; | ||
pvm.reset(program, regsAsUint8(initialState.regs), BigInt(gas)); | ||
pvm.setNextProgramCounter(initialState.pc ?? 0); | ||
pvm.setGasLeft(BigInt(gas)); | ||
} | ||
}; | ||
|
||
export const runInit = async ({ pvm, program, initialState }: InitParams): Promise<InitResponse> => { | ||
if (program.length === 0) { | ||
console.warn("Skipping init, no program yet."); | ||
return { | ||
status: CommandStatus.SUCCESS, | ||
initialState: {}, | ||
}; | ||
} | ||
|
||
try { | ||
await init({ pvm, program, initialState }); | ||
|
||
return { | ||
status: CommandStatus.SUCCESS, | ||
initialState: pvm ? getState(pvm) : {}, | ||
}; | ||
} catch (error) { | ||
return { | ||
status: CommandStatus.ERROR, | ||
error, | ||
initialState: pvm ? getState(pvm) : {}, | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { logger } from "@/utils/loggerService"; | ||
import { loadArrayBufferAsWasm, SupportedLangs } from "../utils"; | ||
import { CommandStatus, PvmApiInterface, PvmTypes } from "../worker"; | ||
import { Pvm as InternalPvmInstance } from "@typeberry/pvm-debugger-adapter"; | ||
|
||
export type LoadParams = { type: PvmTypes; params: { url?: string; file?: Blob; lang?: SupportedLangs } }; | ||
export type LoadResponse = { pvm: PvmApiInterface | null; status: CommandStatus; error?: unknown }; | ||
|
||
const load = async (args: LoadParams): Promise<PvmApiInterface | null> => { | ||
if (args.type === PvmTypes.BUILT_IN) { | ||
return new InternalPvmInstance(); | ||
} else if (args.type === PvmTypes.WASM_FILE) { | ||
const file = args.params.file; | ||
if (!file) { | ||
throw new Error("No PVM file"); | ||
} | ||
|
||
logger.info("Load WASM from file", file); | ||
const bytes = await file.arrayBuffer(); | ||
return await loadArrayBufferAsWasm(bytes); | ||
} else if (args.type === PvmTypes.WASM_URL) { | ||
const url = args.params.url ?? ""; | ||
const isValidUrl = Boolean(new URL(url)); | ||
|
||
if (!isValidUrl) { | ||
throw new Error("Invalid PVM URL"); | ||
} | ||
|
||
logger.info("Load WASM from URL", url); | ||
const response = await fetch(url); | ||
const bytes = await response.arrayBuffer(); | ||
|
||
return await loadArrayBufferAsWasm(bytes, args.params.lang); | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export const runLoad = async (args: LoadParams): Promise<LoadResponse> => { | ||
try { | ||
const pvm = await load(args); | ||
if (pvm) { | ||
return { pvm, status: CommandStatus.SUCCESS }; | ||
} | ||
} catch (error) { | ||
return { pvm: null, status: CommandStatus.ERROR, error }; | ||
} | ||
|
||
return { pvm: null, status: CommandStatus.ERROR, error: new Error("Unknown PVM type") }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { CurrentInstruction, ExpectedState, Status } from "@/types/pvm"; | ||
import { nextInstruction } from "../pvm"; | ||
import { isInternalPvm, getState } from "../utils"; | ||
import { CommandStatus, PvmApiInterface } from "../worker"; | ||
|
||
export type StepParams = { program: number[]; pvm: PvmApiInterface | null }; | ||
export type StepResponse = { | ||
status: CommandStatus; | ||
error?: unknown; | ||
result: CurrentInstruction | object; | ||
state: ExpectedState; | ||
isFinished: boolean; | ||
}; | ||
|
||
const step = ({ pvm, program }: StepParams) => { | ||
if (!pvm) { | ||
throw new Error("PVM is uninitialized."); | ||
} | ||
|
||
let isFinished: boolean; | ||
if (isInternalPvm(pvm)) { | ||
isFinished = pvm.nextStep() !== Status.OK; | ||
} else { | ||
isFinished = !pvm.nextStep(); | ||
} | ||
|
||
const state = getState(pvm); | ||
const result = nextInstruction(state.pc ?? 0, program) as unknown as CurrentInstruction; | ||
|
||
return { result, state, isFinished }; | ||
}; | ||
|
||
export const runStep = ({ pvm, program }: StepParams): StepResponse => { | ||
try { | ||
const data = step({ pvm, program }); | ||
return { status: CommandStatus.SUCCESS, ...data }; | ||
} catch (error) { | ||
return { status: CommandStatus.ERROR, error, isFinished: true, result: {}, state: {} }; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.