-
Notifications
You must be signed in to change notification settings - Fork 3
host calls wip #455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
host calls wip #455
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bd31e1d
Handle host calls.
tomusdrw 6d0739a
Initial version of host calls handling.
tomusdrw 885e941
More wip stuff
tomusdrw 640b4f9
Update gol example.
tomusdrw c5bcab2
Increase default gas.
tomusdrw 450a112
Refresh faster
tomusdrw f0f63f9
More host calls WiP.
tomusdrw ec1bc84
dedupe
tomusdrw bc8828a
use global service id
tomusdrw 0906e0f
Update src/components/HostCallDialog/handlers/FetchHostCall.tsx
tomusdrw 15f3b22
Update src/components/HostCallDialog/MemoryEditor.tsx
tomusdrw 37ebdf7
Merge branch 'main' into td-host-calls
tomusdrw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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
135 changes: 135 additions & 0 deletions
135
src/components/HostCallDialog/DefaultHostCallContent.tsx
This file contains hidden or 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,135 @@ | ||
| import { useCallback, useEffect, useState } from "react"; | ||
| import { Input } from "@/components/ui/input"; | ||
| import { valueToNumeralSystem } from "@/components/Instructions/utils"; | ||
| import { DEFAULT_GAS, DEFAULT_REGS, ExpectedState, RegistersArray } from "@/types/pvm"; | ||
| import { NumeralSystem } from "@/context/NumeralSystem"; | ||
| import { MemoryEditor } from "./MemoryEditor"; | ||
| import { HostCallActionButtons } from "./handlers/HostCallActionButtons"; | ||
|
|
||
| // Re-export for convenience | ||
| export type { MemoryEdit } from "@/store/workers/workersSlice"; | ||
| import type { HostCallResumeMode, MemoryEdit } from "@/store/workers/workersSlice"; | ||
|
|
||
| function stringToNumber<T>(value: string, cb: (x: string) => T): T { | ||
| try { | ||
| return cb(value); | ||
| } catch { | ||
| return cb("0"); | ||
| } | ||
| } | ||
|
|
||
| interface DefaultHostCallContentProps { | ||
| currentState: ExpectedState; | ||
| isLoading: boolean; | ||
| numeralSystem: NumeralSystem; | ||
| readMemory: (startAddress: number, length: number) => Promise<Uint8Array>; | ||
| onRegsChange: (regs: bigint[]) => void; | ||
| onGasChange: (gas: bigint) => void; | ||
| onMemoryChange: (memory: MemoryEdit | null) => void; | ||
| onResume: (mode: HostCallResumeMode, regs?: bigint[], gas?: bigint) => void; | ||
| } | ||
|
|
||
| export const DefaultHostCallContent: React.FC<DefaultHostCallContentProps> = ({ | ||
| currentState, | ||
| isLoading, | ||
| numeralSystem, | ||
| readMemory, | ||
| onRegsChange, | ||
| onResume, | ||
| onGasChange, | ||
| onMemoryChange, | ||
| }) => { | ||
| // Local state for editable values | ||
| const [regs, setRegs] = useState<bigint[]>([...DEFAULT_REGS]); | ||
| const [gas, setGas] = useState<bigint>(0n); | ||
|
|
||
| // Initialize local state when currentState changes | ||
| useEffect(() => { | ||
| if (currentState) { | ||
| // Copy current registers | ||
| const newRegs = currentState.regs ? [...currentState.regs] : [...DEFAULT_REGS]; | ||
| setRegs(newRegs); | ||
| onRegsChange(newRegs); | ||
|
|
||
| // Set gas to current gas - 10 (as per requirement) | ||
| const currentGas = currentState.gas ?? DEFAULT_GAS; | ||
| const newGas = currentGas > 10n ? currentGas - 10n : 0n; | ||
| setGas(newGas); | ||
| onGasChange(newGas); | ||
| } | ||
| }, [currentState, onRegsChange, onGasChange]); | ||
|
|
||
| const handleRegisterChange = (index: number, value: string) => { | ||
| const newValue = stringToNumber(value, BigInt); | ||
| setRegs((prev) => { | ||
| const newRegs = [...prev]; | ||
| newRegs[index] = newValue; | ||
| onRegsChange(newRegs); | ||
| return newRegs; | ||
| }); | ||
| }; | ||
|
|
||
| const handleGasChange = (value: string) => { | ||
| const newValue = stringToNumber(value, BigInt); | ||
| setGas(newValue); | ||
| onGasChange(newValue); | ||
| }; | ||
|
|
||
| const handleMemoryChange = useCallback( | ||
| (address: number, data: Uint8Array) => { | ||
| onMemoryChange({ address, data }); | ||
| }, | ||
| [onMemoryChange], | ||
| ); | ||
|
|
||
| return ( | ||
| <> | ||
| <div className="space-y-4 flex-1 overflow-y-auto p-2"> | ||
| {/* Two-column layout: 1fr for gas/registers, 2fr for memory */} | ||
| <div className="grid grid-cols-1 md:grid-cols-3 gap-4"> | ||
| {/* Column 1: Gas and Registers */} | ||
| <div className="space-y-4 md:col-span-1"> | ||
| {/* Gas */} | ||
| <div className="space-y-2"> | ||
| <label className="text-sm font-medium">Gas</label> | ||
| <Input | ||
| className="font-mono" | ||
| value={valueToNumeralSystem(gas, numeralSystem)} | ||
| onChange={(e) => handleGasChange(e.target.value)} | ||
| onKeyUp={(e) => e.key === "Enter" && e.currentTarget.blur()} | ||
| disabled={isLoading} | ||
| /> | ||
| </div> | ||
|
|
||
| {/* Registers */} | ||
| <div className="space-y-2"> | ||
| <label className="text-sm font-medium">Registers</label> | ||
| <div className="space-y-1"> | ||
| {(regs as RegistersArray).map((regValue, index) => ( | ||
| <div key={index} className="flex items-center gap-2"> | ||
| <span className="w-6 text-xs font-mono"> | ||
| ω<sub>{index}</sub> | ||
| </span> | ||
| <Input | ||
| className="font-mono text-sm h-7 flex-1" | ||
| value={valueToNumeralSystem(regValue, numeralSystem)} | ||
| onChange={(e) => handleRegisterChange(index, e.target.value)} | ||
| onKeyUp={(e) => e.key === "Enter" && e.currentTarget.blur()} | ||
| disabled={isLoading} | ||
| /> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| {/* Column 2: Memory Editor (2x width) */} | ||
| <div className="space-y-2 md:col-span-2"> | ||
| <MemoryEditor readMemory={readMemory} disabled={isLoading} onMemoryChange={handleMemoryChange} /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| <HostCallActionButtons onResume={onResume} disabled={isLoading} /> | ||
| </> | ||
| ); | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.