-
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.
* feat: add memory preview component and hooks Add a new component called MemoryPreview that displays memory pages and ranges. Also, include the necessary hooks for managing memory. This commit introduces changes to the following files: - src/components/MemoryPreview/index.tsx - src/hooks/useMemory.ts - src/hooks/usePvmWorker.ts - src/packages/web-worker/worker.ts * Add memory * add example * radio * text * fix init page map * Add store * use store * use store * new pvm * use builder to initialize pvm memory * Add page mock * wip * Add ranges * bump pvm and simplify memory initialization * WIP * Add page integration * remove mock * fix empty page --------- Co-authored-by: Mateusz Sikora <ms1qaz@gmail.com>
- Loading branch information
1 parent
914788b
commit ae0a5cb
Showing
12 changed files
with
661 additions
and
164 deletions.
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 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,45 @@ | ||
import { createContext, useEffect, useState } from "react"; | ||
import { initialMemoryState, useMemoryFeatureState } from "./components/MemoryPreview/hooks/memoryFeature"; | ||
import { worker } from "./packages/web-worker"; | ||
import { TargetOnMessageParams } from "./packages/web-worker/worker"; | ||
|
||
export const Store = createContext({ | ||
memory: initialMemoryState, | ||
worker: { | ||
worker, | ||
lastEvent: null as MessageEvent<TargetOnMessageParams>["data"] | null, | ||
}, | ||
}); | ||
|
||
const useWorkerState = () => { | ||
const [lastEvent, setLastEvent] = useState<MessageEvent<TargetOnMessageParams>["data"] | null>(null); | ||
|
||
useEffect(() => { | ||
worker.onmessage = (e: MessageEvent<TargetOnMessageParams>) => { | ||
console.log("Main thread received message:", e.data); | ||
setLastEvent(e.data); | ||
}; | ||
worker.postMessage({ command: "load", payload: { type: "built-in" } }); | ||
}, []); | ||
|
||
return { | ||
worker, | ||
lastEvent, | ||
}; | ||
}; | ||
|
||
export const StoreProvider = ({ children }: { children: React.ReactNode }) => { | ||
const worker = useWorkerState(); | ||
const memory = useMemoryFeatureState(); | ||
|
||
return ( | ||
<Store.Provider | ||
value={{ | ||
memory, | ||
worker, | ||
}} | ||
> | ||
{children} | ||
</Store.Provider> | ||
); | ||
}; |
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,53 @@ | ||
import { Store } from "@/AppProviders"; | ||
import { Input } from "@/components/ui/input"; | ||
import { chunk } from "lodash"; | ||
import { useContext } from "react"; | ||
|
||
const SPLIT_STEP = 8 as const; | ||
const toMemoryPageTabData = (memoryPage: Uint8Array | undefined, addressStart: number) => { | ||
return chunk(memoryPage || [], SPLIT_STEP).map((chunk, index) => { | ||
return { | ||
address: (index * SPLIT_STEP + addressStart).toString().padStart(6, "0"), | ||
bits: chunk.map((byte) => byte.toString().padStart(3, "0")), | ||
}; | ||
}); | ||
}; | ||
|
||
export const MemoryTable = ({ data, addressStart }: { addressStart: number; data: Uint8Array | undefined }) => { | ||
const tableData = toMemoryPageTabData(data, addressStart); | ||
|
||
return ( | ||
<div className="mt-5 max-h-[calc(70vh-150px)] overflow-y-auto"> | ||
{tableData.map(({ address, bits }) => ( | ||
<div className="grid grid-cols-3" key={address}> | ||
<div className="opacity-40">{address}</div> | ||
<div className="col-span-2 font-semibold">{bits.join(" ")}</div> | ||
</div> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
export const PageMemory = ({ onPageChange }: { onPageChange: (page: number) => void }) => { | ||
const memory = useContext(Store).memory; | ||
|
||
return ( | ||
<div> | ||
<div className="grid grid-cols-3"> | ||
<div className="font-semibold flex items-center">Page</div> | ||
<div className="col-span-2"> | ||
<Input | ||
type="number" | ||
onChange={(ev) => { | ||
onPageChange(parseInt(ev.target.value || "-1")); | ||
}} | ||
/> | ||
</div> | ||
</div> | ||
<MemoryTable | ||
data={memory.page.state.data} | ||
addressStart={(memory.page.state.pageNumber || 0) * (memory.meta.state.pageSize || 0)} | ||
/> | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.