lukehaas / RunJS

RunJS is a JavaScript playground for macOS, Windows and Linux. Write code with instant feedback and access to Node.js and browser APIs.
https://runjs.app
2k stars 43 forks source link

Show Runtime Values vs. defineProperty on globals #578

Open oculus42 opened 9 months ago

oculus42 commented 9 months ago

Hello! This is very minor and is only an issue with Show Runtime Values - Experimental checked, but I thought it would be worth reporting, mostly in case others come across it.

The conditions I've encountered may not be everything, but are fairly specific:

When using defineProperty on a global like Array.prototype I encounter TypeError: Cannot redefine property: and execution stops, but only if there is code following the defineProperty call:

// Code before executes as expected and displays output
a = 2;

Object.defineProperty(Array.prototype, 'not', {
  get() { return false; },
});

// TypeError: Cannot redefine property: not
//  happens and execution stops if any code is after define
//  with valid syntax, even if it would throw an error
b;

The error does not happen with a local variable:

const a = { is: true };
// No error when defineProperty is on a local variable
Object.defineProperty(a, 'not', {
  get() { return !this.is; },
});

a.is; // true
a.not; // false

And an image demonstrating the error for completeness: image

I'm not sure if this is a worth further research as it works properly when you turn off Show Runtime Values.

Thank you so much for making this amazing tool. I love it and regularly recommend it to others.

lukehaas commented 3 months ago

@oculus42 this seems to be working ok now in the latest version of RunJS.