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
2 changes: 1 addition & 1 deletion src/components/HostCalls/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const HostCalls = () => {
await dispatch(setAllWorkersStorage()).unwrap();
try {
if (isOnEcalli) {
await dispatch(handleHostCall()).unwrap();
await dispatch(handleHostCall({})).unwrap();
}

dispatch(setHasHostCallOpen(false));
Expand Down
108 changes: 58 additions & 50 deletions src/store/workers/workersSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,62 +264,70 @@ export const refreshPageAllWorkers = createAsyncThunk(
},
);

export const handleHostCall = createAsyncThunk("workers/handleHostCall", async (_, { getState, dispatch }) => {
const state = getState() as RootState;

if (state.debugger.storage === null) {
return dispatch(setHasHostCallOpen(true));
} else {
const previousInstruction = nextInstruction(
state.workers[0].previousState.pc ?? 0,
new Uint8Array(state.debugger.program),
);
export const handleHostCall = createAsyncThunk(
"workers/handleHostCall",
async ({ workerId }: { workerId?: string }, { getState, dispatch }) => {
const state = getState() as RootState;

const instructionEnriched = state.debugger.programPreviewResult.find(
(instruction) => instruction.instructionCode === previousInstruction?.instructionCode,
);
if (state.debugger.storage === null) {
return dispatch(setHasHostCallOpen(true));
} else {
const previousInstruction = nextInstruction(
state.workers[0].previousState.pc ?? 0,
new Uint8Array(state.debugger.program),
);

if (
!instructionEnriched ||
isInstructionError(instructionEnriched)
// !isOneImmediateArgs(instructionEnriched.args)
) {
throw new Error("Invalid host call instruction");
}
const instructionEnriched = state.debugger.programPreviewResult.find(
(instruction) => instruction.instructionCode === previousInstruction?.instructionCode,
);

await Promise.all(
state.workers.map(async (worker) => {
const resp = await asyncWorkerPostMessage(worker.id, worker.worker, {
command: Commands.HOST_CALL,
payload: { hostCallIdentifier: worker.exitArg as HostCallIdentifiers },
});
if (
resp.payload.hostCallIdentifier === HostCallIdentifiers.WRITE &&
resp.payload.storage &&
// Remove if we decide to make storage initialization optional
state.debugger.storage
) {
const newStorage = mergePVMAndDebuggerEcalliStorage(resp.payload.storage, state.debugger.storage);
dispatch(setStorage(newStorage));
}
if (
!instructionEnriched ||
isInstructionError(instructionEnriched)
// !isOneImmediateArgs(instructionEnriched.args)
) {
throw new Error("Invalid host call instruction");
}

if ((getState() as RootState).debugger.isRunMode) {
dispatch(continueAllWorkers());
}
await Promise.all(
state.workers
.filter(({ id }) => {
// Allow to call it for a single worker
return workerId ? workerId === id : true;
})
.map(async (worker) => {
const resp = await asyncWorkerPostMessage(worker.id, worker.worker, {
command: Commands.HOST_CALL,
payload: { hostCallIdentifier: worker.exitArg as HostCallIdentifiers },
});
if (
resp.payload.hostCallIdentifier === HostCallIdentifiers.WRITE &&
resp.payload.storage &&
// Remove if we decide to make storage initialization optional
state.debugger.storage
) {
const newStorage = mergePVMAndDebuggerEcalliStorage(resp.payload.storage, state.debugger.storage);
dispatch(setStorage(newStorage));
}

if ((getState() as RootState).debugger.isRunMode) {
dispatch(continueAllWorkers());
}

if (hasCommandStatusError(resp)) {
throw new Error(resp.error.message);
}
}),
);

if (hasCommandStatusError(resp)) {
throw new Error(resp.error.message);
}
}),
);
if (selectShouldContinueRunning(state)) {
dispatch(continueAllWorkers());
}

if (selectShouldContinueRunning(state)) {
dispatch(continueAllWorkers());
return;
}

return;
}
});
},
);

export const continueAllWorkers = createAsyncThunk("workers/continueAllWorkers", async (_, { getState, dispatch }) => {
const stepAllWorkersAgain = async () => {
Expand Down Expand Up @@ -416,7 +424,7 @@ export const stepAllWorkers = createAsyncThunk(
dispatch(setIsRunMode(false));
}

await dispatch(handleHostCall()).unwrap();
await dispatch(handleHostCall({ workerId: worker.id })).unwrap();
}

return {
Expand Down
Loading