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

"eval" in a Window is always scoped to the Window #1025

Closed
joeframbach opened this issue Aug 24, 2023 · 2 comments
Closed

"eval" in a Window is always scoped to the Window #1025

joeframbach opened this issue Aug 24, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@joeframbach
Copy link

joeframbach commented Aug 24, 2023

Describe the bug
eval has two modes -- a "direct" eval which has access to local scope, and an "indirect" eval which has access only to global scope. The eval which executes in a HappyDom document is indirect-only. Behavior in Happy-Dom is different from a real browser.

To Reproduce
Steps to reproduce the behavior:

Execute the script in node:

var HappyDom = require('happy-dom');
new HappyDom.Window().document.write(`<!doctype html>
  <script type="text/javascript">
    var globalVariable = 'globalVariableValue';
    (function () {
      var localVariable = 'localVariableValue';
      console.log(eval('globalVariable'));
      console.log(eval('localVariable'));
    })();
  </script>
`);

See output

globalVariableValue
evalmachine.<anonymous>:1
localVariable
^

ReferenceError: localVariable is not defined

Then execute this script in a browser:

    var globalVariable = 'globalVariableValue';
    (function () {
      var localVariable = 'localVariableValue';
      console.log(eval('globalVariable'));
      console.log(eval('localVariable'));
    })();

See output

globalVariableValue
localVariableValue

Expected behavior

Expected to see "globalVariableValue" and "localVariableValue" in both node and browser.

Screenshots
If applicable, add screenshots to help explain your problem.
N/A

Device:

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

happy-dom 10.11.0

Additional context
Add any other context about the problem here.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#direct_and_indirect_eval

@joeframbach joeframbach added the bug Something isn't working label Aug 24, 2023
@joeframbach joeframbach changed the title "eval" in a Window is not a "direct" eval "eval" in a Window is always scoped to the Window Aug 24, 2023
@joeframbach
Copy link
Author

I found that the eval is a "direct" eval, but the issue is that it is always scoped from the Window, not from the scope where it is called in client code.

public eval(code: string): unknown {

Test case:

<!doctype html>
<html>
  <script type="text/javascript">
    (function () {
      console.log(eval('this.location'));
    }).call({});
  </script>
</html>

In browser:

undefined

In happy-dom:

Location {
  href: 'https://example.com',
  ...

joeframbach added a commit to joeframbach/happy-dom that referenced this issue Aug 25, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
…luate.
joeframbach added a commit to joeframbach/happy-dom that referenced this issue Aug 25, 2023

Verified

This commit was signed with the committer’s verified signature.
christophehurpeau Christophe Hurpeau
…luate.
joeframbach added a commit to joeframbach/happy-dom that referenced this issue Aug 25, 2023
joeframbach added a commit to joeframbach/happy-dom that referenced this issue Aug 25, 2023
@capricorn86
Copy link
Owner

capricorn86 commented Sep 6, 2023

Thanks again for reporting and contributing @joeframbach! 🙂

There is a fix released now.

You can read more about it here:
https://github.com/capricorn86/happy-dom/releases/tag/v11.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants