Skip to content

Loading WasmMemory into module #152

Open
@ppedziwiatr

Description

@ppedziwiatr

Hey,

as a follow-up of #146 - here's code that I'm trying to run:

import { getQuickJS, newQuickJSWASMModule, newVariant, RELEASE_SYNC } from "quickjs-emscripten";

async function main() {
  // module 1
  const QuickJS1 = await getQuickJS();
  const vm1 = QuickJS1.newContext();
  const res1 = vm1.evalCode(`const x = 100;
  function test() {
    return x;
  };
  `);
  res1.value.dispose();

  const testRes = vm1.unwrapResult(vm1.evalCode(`test()`))
  console.log("test result:", vm1.getNumber(testRes));
  const mem1 = QuickJS1.getWasmMemory();
  vm1.dispose();

  // module 2
  const variant = newVariant(RELEASE_SYNC, {
    wasmMemory: mem1
  });
  const QuickJS2 = await newQuickJSWASMModule(variant);
  const vm2 = QuickJS2.newContext();

  // getting " 'test' is not defined" here..
  const testRes2 = vm2.unwrapResult(vm2.evalCode(`test()`))
  console.log("test result:", vm2.getNumber(testRes2));
  testRes2.dispose();
  vm2.dispose();
}

main().catch(e => console.error(e)).finally();

What it does - it simply creates one quickjs module, evals some code, stores the module's Wasm memory - and then a second module is created with a Wasm memory from the first one. I would expect that all the code evaluated in the first one will be available in the second - but that's not the case. The result is:

test result: 100
y [ReferenceError]: 'test' is not defined
    at <eval> (eval.js)
Host: QuickJSUnwrapError
    at T.unwrapResult (file:///Users/ppe/warp/warp/node_modules/quickjs-emscripten-core/dist/chunk-VABBOY7Z.mjs:4:12568)
    at main (file:///Users/ppe/warp/warp/tools/quickjs-memory.mjs:26:24) {
  cause: {
    name: 'ReferenceError',
    message: "'test' is not defined",
    stack: '    at <eval> (eval.js)\n'
  },

Is this expected behaviour? If so, is it possible to somehow save a state of a module and resume the evaluation later with this exact state?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions