oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.3k stars 2.78k forks source link

node:vm globalThis / this !== context #4205

Open bytespider opened 1 year ago

bytespider commented 1 year ago

What version of Bun is running?

0.7.3

What platform is your computer?

Darwin 22.6.0 arm64 arm

What steps can reproduce the bug?

Properties from the context are not available on the globalThis or this properties.

import vm from 'node:vm';

const context = {};
vm.createContext(context);

context.a = 123
context.console = console;

vm.runInContext('console.log(this.a); console.log(a); console.log(a === this.a)', context);

What is the expected behavior?

In Node v18.7.0

❯ node test.js
123
123
true

What do you see instead?

In Bun 0.7.3

❯ bun test.js
undefined
123
false

Additional information

No response

Jarred-Sumner commented 1 year ago

Looked at this for a little, some notes:

bytespider commented 1 year ago

Looking at the Node code, it seems like they add getters/setters to the global scope that proxy back. Other than that research I have no idea how any of this works 😀

shelterit commented 9 months ago

Just wanted to follow-up to this as I've been doing a lot of happy-dom stuff lately, and it uses the VM's context to run eval code in a simulated browser way, but as soon as the object is passed to createContext() the 'this' at the end of execution is altered (rendering errors galore). Ref: https://github.com/capricorn86/happy-dom/issues/1225

Latest Bun 1.0.26, latest happy-dom 13.3.8

import VM from 'vm';
class Test {
   test() { // just a test method }
   constructor() {
      this.test();
      this.init();
      // "this" is no longer 'this', and throws a tantrum ....
      this.test();
   }
   init() {
      if (!VM.isContext(this)) {
         VM.createContext(this);
      }
   }
}
test = new Test();
ohroy commented 7 months ago

any news ?

lukasbash commented 5 months ago

Still happening on version 1.1.9 😢

mrobokit commented 4 months ago

Any news? Still happening in v1.1.20

lockieluke commented 3 months ago

this is blocking use of happydom

ianzone commented 1 month ago

happy-dom is not happy anymore, could bun team rise the priority?

lukasbash commented 4 weeks ago

Still not working on the latest canary. How can we get the priority up in here?