Closed Bug 1268326 Opened 8 years ago Closed 8 years ago

eval use strict error fails silently

Categories

(Core :: JavaScript Engine, defect)

46 Branch
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: npbenjohnson, Unassigned)

Details

User Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36

Steps to reproduce:

This only occurs when dev console is open, and I noticed it because systemjs will fail in strange ways if its injected modules run in to this situation:
try{
  eval('"use strict";function test();console.log("foo");return;return;}');
} catch (ex) {
  console.log('error');
}
test();


Actual results:

console output:
ReferenceError: test is not defined


Expected results:

expected console output:
error
ReferenceError: test is not defined

or just "foo" and a warning about unreachable code
There is a typo in the steps to reproduce:
This behaves incorrectly:
try{
  eval('"use strict";function test(){console.log("foo");return;return;}');
} catch (ex) {
  console.log('error');
}
test();

This behaves as expected:
try{
  eval('function test(){console.log("foo");return;return;}');
} catch (ex) {
  console.log('error');
}
test();
Component: Untriaged → JavaScript Engine
Product: Firefox → Core
What it is that you expect to happen in the first case? Both Firefox (tested in Nightly 49) and Chromium (current release, 49) show that "test" is undefined.

From the spec: " Strict mode eval code cannot instantiate variables or functions in the variable environment of the caller to eval. Instead, a new variable environment is created and that environment is used for declaration binding instantiation for the eval code (18.2.1). "

https://tc39.github.io/ecma262/#sec-strict-mode-of-ecmascript

So I think both Firefox and Blink follow the spec here by throwing when trying to execute "test()", because it's undefined. No errors are thrown in the try/catch block, though, and it sounds about right. Feel free to re-open if I am misreading the spec or if your original intent was different.
Status: UNCONFIRMED → RESOLVED
Closed: 8 years ago
Resolution: --- → INVALID
Right, the eval() runs fine and the function is created, but in strict mode, its scope is just the scope of that eval call --- not the global scope. So the function is discarded at the end of the eval().

There is no way in script mode to eliminate the eval scope. It's kind of a pain sometimes.
You need to log in before you can comment on or make changes to this bug.