-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsnadac.html
103 lines (87 loc) · 3.67 KB
/
jsnadac.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<!doctype html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/pyodide/v0.25.0/full/pyodide.js"></script>
</head>
<body style="background-color:#FFFFFF;">
<button id="button_eval" style="padding:6px;">Run Simulator on Inputs</button>
<br />
<textarea id="output" style="width: 100%;" rows="6" disabled></textarea>
<script type="module">
import init, { run, program_to_bin } from "./nada_run.js";
const runProgramSimulator = async (program, secrets, public_vars) => {
// Instantiate our wasm module
const programSimulator = await init();
console.log(`Running program simulator with arguments: ${program}, ${secrets}, ${public_vars}`)
const result = run(program, 5, 1, 128, secrets, public_vars);
return result;
};
const compileProgram = (pyodide, program_code) => {
let program_json = pyodide.runPython(program_code + "\n" + "nada_compile(nada_main())");
console.log("PROGRAM:\n " + program_json + "\n=========================\n");
return program_json;
}
const output = document.getElementById("output");
document.querySelector('#button_eval').addEventListener('click', () => {
evaluatePython();
});
let inputIndex = 1;
function addToOutput(s) {
output.value += s + "\n";
}
output.value = "Initializing...\n";
// init Pyodide
async function main() {
let pyodide = await loadPyodide();
output.value += "Ready!\n";
return pyodide;
}
let pyodideReadyPromise = main();
async function evaluatePython() {
const ins = window.parent.interpreterInputsRetrieve();
let ins_ = [[]];
for (const name in ins) {
ins_.push([name, ins[name]]);
}
output.value = "";
let pyodide = await pyodideReadyPromise;
await pyodide.loadPackage("micropip");
const micropip = pyodide.pyimport("micropip");
await micropip.install('nada_dsl-0.1.0-py3-none-any.whl');
try {
let publicInputs = {};
let secretInputs = {};
for (let i = 1; i < ins_.length; i++) {
let inputName = ins_[i][0];
let inputValue = ins_[i][1][0];
let inputType = ins_[i][1][1];
// Check if inputName is a single word
if (/\s/.test(inputName)) {
throw new Error("Input name must be a single word.");
}
if (!isNaN(inputValue)) {
inputValue = parseFloat(inputValue);
} else {
throw new Error("Input value must be a number.");
}
if (inputType === "PublicInteger") {
publicInputs[inputName] = { "type": "PublicInteger", "value": `${inputValue}` };
} else if (inputType === "SecretInteger") {
secretInputs[inputName] = { "type": "SecretInteger", "value": `${inputValue}` };
}
}
let public_vars = JSON.stringify(publicInputs);
let secrets = JSON.stringify(secretInputs);
let program_json = pyodide.runPython(window.parent.editorValue() + "\n" + "nada_compile(nada_main())");
console.log("PROGRAM:\n " + program_json + "\n=========================\n");
console.log("INPUTS:\n" + public_vars + "\n\n" + secrets+ "\n=========================\n");
// Let's run the program
let program_output = await runProgramSimulator(program_json, secrets, public_vars);
addToOutput(`RESULT: ${JSON.stringify(Array.from(program_output.entries()))}`)
} catch (err) {
addToOutput(err);
}
}
</script>
</body>
</html>