If you think a bug might affect users in the 57 release, please set the correct tracking and status flags for Release Management.

eval use strict error fails silently

RESOLVED INVALID

Status

()

Core
JavaScript Engine
RESOLVED INVALID
a year ago
a year ago

People

(Reporter: npbenjohnson, Unassigned)

Tracking

46 Branch
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

a year ago
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
(Reporter)

Comment 1

a year ago
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();

Updated

a year ago
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
Last Resolved: a year 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.