sh1k4ku commited on
Commit
c98b824
·
verified ·
1 Parent(s): 3bb1e76

Rollback index.html to working worker version

Browse files
Files changed (1) hide show
  1. index.html +4 -30
index.html CHANGED
@@ -191,23 +191,6 @@ const slotTemplate = () => (mode === "run" ? RUN_TEMPLATE : PROBLEMS[prob].testT
191
  const saveEditor = () => { if (mode === "run") work.run = editor.getValue(); else work.test[prob] = editor.getValue(); };
192
  const loadEditor = () => editor.setValue(mode === "run" ? work.run : work.test[prob]);
193
 
194
- // Pull ONLY the TESTS = [ ... ] list out of whatever the student typed; everything
195
- // else is ignored (never executed). Balanced-bracket scan, quote-aware.
196
- function extractTestsLiteral(src) {
197
- const m = /TESTS\s*=\s*\[/.exec(src);
198
- if (!m) return null;
199
- const start = src.indexOf("[", m.index);
200
- let depth = 0, quote = null;
201
- for (let j = start; j < src.length; j++) {
202
- const ch = src[j];
203
- if (quote) { if (ch === quote && src[j - 1] !== "\\") quote = null; continue; }
204
- if (ch === '"' || ch === "'") { quote = ch; continue; }
205
- if (ch === "[") depth++;
206
- else if (ch === "]") { depth--; if (depth === 0) return src.slice(start, j + 1); }
207
- }
208
- return null;
209
- }
210
-
211
  const output = document.getElementById("output");
212
  const summary = document.getElementById("summary");
213
  const statusEl = document.getElementById("status");
@@ -249,8 +232,6 @@ function applyMode() {
249
  modeTestBtn.classList.toggle("active", mode === "test");
250
  leftLabel.textContent = mode === "test" ? "Your Test Code" : "Your Code";
251
  rightLabel.textContent = mode === "test" ? "Test Results" : "Output";
252
- const leftBadge = document.querySelector("#left .badge");
253
- if (leftBadge) leftBadge.textContent = mode === "test" ? "only TESTS = [...] is read" : "editable";
254
  modinfo.style.display = mode === "test" ? "" : "none";
255
  summary.style.display = "none";
256
  runBtn.innerHTML = (mode === "test" ? "▶ Run Tests " : "▶ Run ")
@@ -354,20 +335,13 @@ async function run() {
354
  // Run the student's code in the (clean) main interpreter to define TESTS,
355
  // then send ONLY the TESTS data to the worker. The worker holds the answer
356
  // key in its own interpreter; nothing sensitive lives in this one.
357
- // Regex out the TESTS = [ ... ] block and evaluate ONLY that literal. Student
358
- // code is NEVER executed; anything outside the list is ignored. literal_eval
359
- // accepts numbers/strings/tuples/lists/bools/None and rejects calls/names.
360
- const lit = extractTestsLiteral(code);
361
- if (!lit) {
362
- append('\nNo "TESTS = [ ... ]" found. Define your test cases as a list, e.g.\nTESTS = [ ((50, 2, 30, "Lecture"), "SEATED") ]\n', "err");
363
- return;
364
- }
365
- pyodide.globals.set("__lit", lit);
366
  let testsJson;
367
  try {
368
- testsJson = pyodide.runPython("import ast, json\njson.dumps(ast.literal_eval(__lit))");
369
  } catch (se) {
370
- append('\nTESTS must contain only literal values (numbers, strings, tuples), e.g. ((50, 2, 30, "Lecture"), "SEATED").\n' + cleanError(se.message || String(se)) + "\n", "err");
371
  return;
372
  }
373
  const rep = await gradeViaWorker(prob, code, testsJson);
 
191
  const saveEditor = () => { if (mode === "run") work.run = editor.getValue(); else work.test[prob] = editor.getValue(); };
192
  const loadEditor = () => editor.setValue(mode === "run" ? work.run : work.test[prob]);
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  const output = document.getElementById("output");
195
  const summary = document.getElementById("summary");
196
  const statusEl = document.getElementById("status");
 
232
  modeTestBtn.classList.toggle("active", mode === "test");
233
  leftLabel.textContent = mode === "test" ? "Your Test Code" : "Your Code";
234
  rightLabel.textContent = mode === "test" ? "Test Results" : "Output";
 
 
235
  modinfo.style.display = mode === "test" ? "" : "none";
236
  summary.style.display = "none";
237
  runBtn.innerHTML = (mode === "test" ? "▶ Run Tests " : "▶ Run ")
 
335
  // Run the student's code in the (clean) main interpreter to define TESTS,
336
  // then send ONLY the TESTS data to the worker. The worker holds the answer
337
  // key in its own interpreter; nothing sensitive lives in this one.
338
+ pyodide.runPython("globals().pop('TESTS', None)");
339
+ await pyodide.runPythonAsync(code); // defines TESTS (and any prints)
 
 
 
 
 
 
 
340
  let testsJson;
341
  try {
342
+ testsJson = pyodide.runPython("import json\njson.dumps(globals().get('TESTS'))");
343
  } catch (se) {
344
+ append("\nYour TESTS contains values that can't be graded: " + cleanError(se.message || String(se)) + "\n", "err");
345
  return;
346
  }
347
  const rep = await gradeViaWorker(prob, code, testsJson);