Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best way to pass object with Reference #49

Closed
iCyberon opened this issue Mar 3, 2018 · 1 comment
Closed

Best way to pass object with Reference #49

iCyberon opened this issue Mar 3, 2018 · 1 comment

Comments

@iCyberon
Copy link

iCyberon commented Mar 3, 2018

What's the best way to pass objects to isolate that contain Reference.

const ivm = require('isolated-vm');
const isolate = new ivm.Isolate({ memoryLimit: 128 });

const context = isolate.createContextSync();
const globalReference = context.globalReference();

const obj = {
  a: 1,
  b: 2,
  c: new ivm.Reference(function() {})
}

globalReference.setSync('obj', new ivm.ExternalCopy(obj).copyInto());

let r = isolate.compileScriptSync('obj.a + obj.b').runSync(context);
console.log(r);

This gives an error:

#<Reference> could not be cloned.

As I understand Reference can't be passed to ExternalCopy. What's the best way to transfer objects that contain functions?

@laverdet
Copy link
Owner

laverdet commented Mar 3, 2018

Right now you would have to do this manually for each object you want to pass:

global.setSync('obj', new ivm.ExternalCopy({}).copyInto());
let obj = global.getSync('obj');
obj.setSync('c', new ivm.Reference(function() {}));
// ... more

You could also call a function with an array of references using apply or applySync.

I'm going to keep this open because I think it may be possible to allow references in deep copies in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants