Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

3.1 KB
Raw Blame
<!doctype html>
<meta charset="utf-8">
<script src="/wpt/resources/testharness.js"></script>
<script src="/wpt/resources/testharnessreport.js"></script>
<script src="/wpt/resources/moli-wpt-adapter.js"></script>
<script>
function worker_url(source) {
  return "data:text/javascript," + encodeURIComponent(source);
}

function next_message(worker) {
  return new Promise(function (resolve) {
    worker.addEventListener("message", resolve, { once: true });
  });
}

promise_test(async function () {
  const depUrl = worker_url([
    "export let counter = 1;",
    "export { counter as default };",
    "export function setCounter(value) { counter = value; }",
  ].join("\n"));
  const source = [
    "import current, { counter, setCounter } from " + JSON.stringify(depUrl) + ";",
    "const before = counter;",
    "const defaultBefore = current;",
    "const topLevelThisUndefined = this === undefined;",
    "function functionThis() { return this; }",
    "let implicitGlobalError = 'none';",
    "try {",
    "  namedImportGlobal = 7;",
    "} catch (error) {",
    "  implicitGlobalError = error && error.name;",
    "}",
    "let namedAssignment = 'none';",
    "try {",
    "  counter = 11;",
    "} catch (error) {",
    "  namedAssignment = error && error.name;",
    "}",
    "let defaultAssignment = 'none';",
    "try {",
    "  current = 12;",
    "} catch (error) {",
    "  defaultAssignment = error && error.name;",
    "}",
    "setCounter(5);",
    "const afterSet = counter;",
    "const defaultAfterSet = current;",
    "postMessage({",
    "  before,",
    "  defaultBefore,",
    "  afterSet,",
    "  defaultAfterSet,",
    "  topLevelThisUndefined,",
    "  functionThisUndefined: functionThis() === undefined,",
    "  implicitGlobalError,",
    "  leakedGlobal: Object.prototype.hasOwnProperty.call(self, 'namedImportGlobal'),",
    "  namedAssignment,",
    "  defaultAssignment",
    "});",
  ].join("\n");
  const worker = new Worker(worker_url(source), { type: "module" });

  try {
    const message = await next_message(worker);
    assert_equals(message.data.before, 1, "named import should expose initial value");
    assert_equals(message.data.defaultBefore, 1, "default import should expose initial value");
    assert_equals(message.data.afterSet, 5, "named import should remain live after mutation");
    assert_equals(message.data.defaultAfterSet, 5, "default import should remain live after mutation");
    assert_true(message.data.topLevelThisUndefined, "module top-level this should be undefined");
    assert_true(message.data.functionThisUndefined, "module functions should run in strict mode");
    assert_equals(message.data.implicitGlobalError, "ReferenceError", "implicit global assignment should throw");
    assert_false(message.data.leakedGlobal, "implicit global assignment should not leak onto self");
    assert_equals(message.data.namedAssignment, "TypeError", "assigning to a named import should throw");
    assert_equals(message.data.defaultAssignment, "TypeError", "assigning to a default import should throw");
  } finally {
    worker.terminate();
  }
}, "Dedicated named/default-import module workers run with strict-mode semantics");
</script>