Fix #1832: Browser compiler should run code using globally-scoped eval (#5047)

* Explicitly turn off minify mangling, because it was previously implicitly disabled by the existence of the `eval` call we’re about to remove

* Update babel-core

* Run browser-compiled scripts using window.eval rather than just eval, to use a clean scope (many times faster)

* Update output

* Fix browser-based test broken by #5028

* Update output
This commit is contained in:
Geoffrey Booth 2018-05-01 08:11:40 -07:00 committed by GitHub
parent fe7554828a
commit d82272b6c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 28 additions and 15 deletions

View File

@ -72,7 +72,7 @@ transpile = (code) ->
# Exclude the `modules` plugin in order to not break the `}(this));`
# at the end of the `build:browser` code block.
presets.push ['env', {modules: no}] unless process.env.TRANSFORM is 'false'
presets.push 'minify' unless process.env.MINIFY is 'false'
presets.push ['minify', {mangle: no}] unless process.env.MINIFY is 'false'
babelOptions =
compact: process.env.MINIFY isnt 'false'
presets: presets

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -148,6 +148,8 @@ diffOutput = (expectedOutput, actualOutput) ->
ok yes
else if e.toString().indexOf('[stdin]') is 0 # Handle comparing error messages
ok err e
else if err instanceof RegExp
ok err.test e
else
eq e, err
else

View File

@ -143,7 +143,7 @@ $(document).ready ->
else
$(textareas[index]).val()
js = "#{js}\nalert(#{unescape run});" unless run is yes
eval js
window.eval js
gtag 'event', 'run_code',
event_category: 'engagement'
event_label: $(@).closest('[data-example]').data('example')

View File

@ -108,6 +108,8 @@ onFail = (description, fn, err) ->
ok yes
else if e.toString().indexOf('[stdin]') is 0 # Handle comparing error messages
ok err e
else if err instanceof RegExp
ok err.test e
else
eq e, err
else

View File

@ -9,14 +9,19 @@
CoffeeScript = require('./coffeescript');
compile = CoffeeScript.compile;
({compile} = CoffeeScript);
// Use standard JavaScript `eval` to eval code.
// Use `window.eval` to evaluate code, rather than just `eval`, to run the
// script in a clean global scope rather than inheriting the scope of the
// CoffeeScript compiler. (So that `cake test:browser` also works in Node,
// use either `window.eval` or `global.eval` as appropriate).
CoffeeScript.eval = function(code, options = {}) {
var globalRoot;
if (options.bare == null) {
options.bare = true;
}
return eval(compile(code, options));
globalRoot = typeof window !== "undefined" && window !== null ? window : global;
return globalRoot['eval'](compile(code, options));
};
// Running code does not provide access to this scope.

8
package-lock.json generated
View File

@ -167,9 +167,9 @@
}
},
"babel-core": {
"version": "6.26.2",
"resolved": "https://registry.npmjs.org/babel-core/-/babel-core-6.26.2.tgz",
"integrity": "sha512-rFKFnHY8sbRSqja2O5eTx0z0Na5hukdtsFt7X9xdBFXMurrJ5YoY78Y/2/EuNZIaDQKEJSfxSMePfsymxt0CZg==",
"version": "6.26.3",
"resolved": "https://registry.npmjs.org/babel-core/-/babel-core-6.26.3.tgz",
"integrity": "sha512-6jyFLuDmeidKmUEb3NM+/yawG0M2bDZ9Z1qbZP59cyHLz8kYGKYwpJP0UwUKKUiTRNvxfLesJnTedqczP7cTDA==",
"dev": true,
"requires": {
"babel-code-frame": "6.26.0",
@ -1509,7 +1509,7 @@
"integrity": "sha1-btAhFz4vy0htestFxgCahW9kcHE=",
"dev": true,
"requires": {
"babel-core": "6.26.2",
"babel-core": "6.26.3",
"babel-runtime": "6.26.0",
"core-js": "2.5.5",
"home-or-tmp": "2.0.0",

View File

@ -39,7 +39,7 @@
"url": "git://github.com/jashkenas/coffeescript.git"
},
"devDependencies": {
"babel-core": "~6.26.2",
"babel-core": "~6.26.3",
"babel-preset-babili": "~0.1.4",
"babel-preset-env": "~1.6.1",
"babel-preset-minify": "^0.4.0",

View File

@ -4,12 +4,16 @@
# `text/coffeescript` script tags, source maps via data-URLs, and so on.
CoffeeScript = require './coffeescript'
compile = CoffeeScript.compile
{ compile } = CoffeeScript
# Use standard JavaScript `eval` to eval code.
# Use `window.eval` to evaluate code, rather than just `eval`, to run the
# script in a clean global scope rather than inheriting the scope of the
# CoffeeScript compiler. (So that `cake test:browser` also works in Node,
# use either `window.eval` or `global.eval` as appropriate).
CoffeeScript.eval = (code, options = {}) ->
options.bare ?= on
eval compile code, options
globalRoot = if window? then window else global
globalRoot['eval'] compile code, options
# Running code does not provide access to this scope.
CoffeeScript.run = (code, options = {}) ->