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

vm: instanceof test fails when assigning global to vm context #7351

Closed
princejwesley opened this issue Jun 21, 2016 · 3 comments
Closed

vm: instanceof test fails when assigning global to vm context #7351

princejwesley opened this issue Jun 21, 2016 · 3 comments
Labels
v8 engine Issues and PRs related to the V8 dependency. vm Issues and PRs related to the vm subsystem.

Comments

@princejwesley
Copy link
Contributor

  • Version: v6.1.0 , (master: v7.0.0-pre)
  • Platform: Darwin Princes-MacBook-Pro.local 15.5.0 Darwin Kernel Version 15.5.0: Tue Apr 19 18:36:36 PDT 2016; root:xnu-3248.50.21~8/RELEASE_X86_64 x86_64
  • Subsystem: vm

When assigning global variables to vm context, instanceof test for the assigned global variables return false

const vm = require('vm')

let ctx = vm.createContext();

let before = vm.runInContext('[] instanceof Array', ctx);
ctx.Array = Array;
let after = vm.runInContext('[] instanceof Array', ctx);

console.log('before =>', before, 'after =>', after); // before => true after => false

before = vm.runInContext('/vm/ instanceof RegExp', ctx);
ctx.RegExp = RegExp;
after = vm.runInContext('/vm/ instanceof RegExp', ctx);

console.log('before =>', before, 'after =>', after); // before => true after => false
Question

Is this a bug or an expected behaviour?

@addaleax addaleax added vm Issues and PRs related to the vm subsystem. v8 engine Issues and PRs related to the V8 dependency. labels Jun 21, 2016
@cjihrig
Copy link
Contributor

cjihrig commented Jun 21, 2016

Seems like expected behavior to me. Array references a different thing in each context.

@targos
Copy link
Member

targos commented Jun 21, 2016

Yes, that is expected behavior. You can work around this problem for arrays with Array.isArray that works across contexts.

jasonmit added a commit to jasonmit/showdown that referenced this issue Aug 25, 2017
Reason being a.constructor === Array is always falsey when you run showdown within Node's VM API.

Related to nodejs/node#7351
tivie pushed a commit to showdownjs/showdown that referenced this issue Aug 26, 2017
a.constructor === Array is always falsey when you run showdown within Node's VM API.

Related to nodejs/node#7351

Closes #425
@rubenstolk
Copy link

rubenstolk commented Jun 21, 2018

A helpful workaround is to use a custom isInstanceOf utility method, e.g.:

const isInstanceOf = (o, type) => Object.prototype.toString.call(o) === `[object ${type}]`;

isInstanceOf(new RegExp('hello world', 'g'), 'RegExp'); --> true
isInstanceOf(/hello world/ 'RegExp'); --> true
isInstanceOf([] 'Array'); --> true
isInstanceOf(new Error('Error'), 'Error'); --> true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v8 engine Issues and PRs related to the V8 dependency. vm Issues and PRs related to the vm subsystem.
Projects
None yet
Development

No branches or pull requests

5 participants