LivelyKernel / lively4-core

A Self-supporting, Web-based Development Environment
https://lively-kernel.org/lively4/lively4-core/start.html
MIT License
73 stars 25 forks source link

Rewriting eval causes scoped variables to not be recognized #377

Open onsetsu opened 5 years ago

onsetsu commented 5 years ago

Consider the following code as running example:

(() => {
  let x = 42;
  return eval('x')
})()

It evaluates and returns the value of the scoped variable x.

However, once we alter this example only slightly, x will not be in the scope of the evaluation:

(() => {
  let x = 42;
  return (void 0, eval)('x')
})()

Inserting this SequenceExpression causes the following error:

Error: x is not defined Evaluating workspacejs:09f31940-1ac0-43c1-bce8-132469645c6c/lively-kernel.org/lively4/aexpr/unnamed_module_9dd25e73_9d6f_4e56_b07c_398481bb3c5f Loading workspacejs:09f31940-1ac0-43c1-bce8-132469645c6c/lively-kernel.org/lively4/aexpr/unnamed_module_9dd25e73_9d6f_4e56_b07c_398481bb3c5f

Note, that we can still evaluate code not related to locally scoped variables:

(() => {
  let x = 42;
  return (void 0, eval)('window')
})()

Not only a SequenceExpression breaks scope lookup; same goes for assigning eval to a local variable:

(() => {
  let x = 42;
  let ev = self.eval
  return ev('x')
})()

My current assumption is that the concrete syntax (the CallExpression eval(code)) makes the local scope available in the evaluated code.

We may mitigate this issue related to Active Expressions by not rewriting a global assess to the eval function.