Skip to content

Commit 2bb799e

Browse files
committed
Allow both 0x and just a number to be parsed as a hex program; improve error handling
1 parent de2ae07 commit 2bb799e

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

src/components/ProgramLoader/Loader.tsx

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,37 +54,44 @@ export const Loader = ({ setIsDialogOpen }: { setIsDialogOpen?: (val: boolean) =
5454

5555
if (searchParams.get("program")) {
5656
const program = searchParams.get("program");
57-
const parsedBlob = bytes.BytesBlob.parseBlob(program ?? "");
58-
const parsedBlobArray = Array.prototype.slice.call(parsedBlob.raw);
5957

60-
if (searchParams.get("flavour") === "jam") {
61-
try {
62-
const { code, /*memory,*/ registers } = decodeStandardProgram(parsedBlob.raw, new Uint8Array());
58+
try {
59+
// Add 0x prefix if it's not there - we're assuming it's the hex program either way
60+
const hexProgram = program?.startsWith("0x") ? program : `0x${program}`;
61+
const parsedBlob = bytes.BytesBlob.parseBlob(hexProgram ?? "");
62+
const parsedBlobArray = Array.prototype.slice.call(parsedBlob.raw);
63+
64+
if (searchParams.get("flavour") === "jam") {
65+
try {
66+
const { code, /*memory,*/ registers } = decodeStandardProgram(parsedBlob.raw, new Uint8Array());
6367

64-
handleLoad({
65-
program: Array.from(code),
68+
handleLoad({
69+
program: Array.from(code),
70+
name: "custom",
71+
initial: {
72+
regs: Array.from(registers) as RegistersArray,
73+
pc: 0,
74+
pageMap: [],
75+
// TODO: map memory properly
76+
// memory: [...memory],
77+
gas: 10000n,
78+
},
79+
});
80+
} catch (e) {
81+
console.warn("Could not load the program from URL", e);
82+
}
83+
} else {
84+
handleLoad(undefined, {
85+
program: parsedBlobArray,
6686
name: "custom",
67-
initial: {
68-
regs: Array.from(registers) as RegistersArray,
69-
pc: 0,
70-
pageMap: [],
71-
// TODO: map memory properly
72-
// memory: [...memory],
73-
gas: 10000n,
74-
},
87+
initial: initialState,
7588
});
76-
} catch (e) {
77-
console.warn("Could not load the program from URL");
7889
}
79-
} else {
80-
handleLoad(undefined, {
81-
program: parsedBlobArray,
82-
name: "custom",
83-
initial: initialState,
84-
});
85-
}
8690

87-
window.history.replaceState({}, document.title, "/");
91+
window.history.replaceState({}, document.title, "/");
92+
} catch (e) {
93+
console.warn("Could not parse the program from URL", e);
94+
}
8895
}
8996
// eslint-disable-next-line react-hooks/exhaustive-deps
9097
}, []);

0 commit comments

Comments
 (0)