LivelyKernel / LivelyKernel/lively4-core

Rewriting eval causes scoped variables to not be recognized

Open
#377 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

help wanted P4: nice to have type: bug type: RFC / discussion / question
Dominant language
JavaScript
Stars
89
Forks
25
PR merge metrics
No merged PRs in 30d

Description

Consider the following code as running example:

```javascript
(() => {
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:

```javascript
(() => {
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:

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

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

```javascript
(() => {
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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the direct eval, rewritten SequenceExpression, and local-variable examples from the issue. Inspect the Active Expressions rewriting path for global access to eval and compare how direct CallExpression eval is handled. Done means the reported scoped-variable behavior is addressed or the limitation is clearly documented with passing regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.