Skip to content

Sharing values between contexts in the same runtime #218

@mshick

Description

@mshick

I see this functionality mentioned in the readme, and in a couple of issues, notably #152 , but I can't seem to make it work.

My situation, simplified, is as below. I happen to be using the async variant, in case that has something to do with my inability to share across contexts.

async main() {
  const runtime = module.newRuntime();
  const context1 = runtime.newContext();

  await context1.evalCodeAsync(`
  function add(x) {
    return x + 50;
  };
  `);

    const test1Res = context.unwrapResult(await context1.evalCodeAsync(`add(50)`));
    console.log('add result1 (should be 100):', context1.getNumber(test1Res));
    test1Res.dispose();

    const contextPointer = context1.ctx.value;

   const context2 = runtime.newContext({contextPointer});
  
    // Throws with 'add' is not defined.
    const test2Res = context2.unwrapResult(await context2.evalCodeAsync(`add(10)`));
    console.log('add result2 (should be 60):', context2.getNumber(test2Res));
    test2Res.dispose();
}

Any help would be greatly appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions