diff --git a/sdk/lib/_internal/lib/async_patch.dart b/sdk/lib/_internal/lib/async_patch.dart index dede2552bcd..cd5890d595f 100644 --- a/sdk/lib/_internal/lib/async_patch.dart +++ b/sdk/lib/_internal/lib/async_patch.dart @@ -14,8 +14,7 @@ import 'dart:_isolate_helper' show TimerImpl, leaveJsAsync, enterJsAsync, - isWorker, - globalThis; + isWorker; import 'dart:_foreign_helper' show JS; @@ -46,7 +45,7 @@ class _AsyncRun { _initializeScheduleImmediate(); static Function _initializeScheduleImmediate() { - if (JS('', '#.scheduleImmediate', globalThis) != null) { + if (JS('', 'self.scheduleImmediate') != null) { return _scheduleImmediateJsOverride; } // TODO(9002): don't use the Timer to enqueue the immediate callback. @@ -60,8 +59,7 @@ class _AsyncRun { callback(); }; enterJsAsync(); - JS('void', '#.scheduleImmediate(#)', - globalThis, + JS('void', 'self.scheduleImmediate(#)', convertDartClosureToJS(internalCallback, 0)); } diff --git a/sdk/lib/_internal/lib/isolate_helper.dart b/sdk/lib/_internal/lib/isolate_helper.dart index d41ad0b53a3..08f4a764793 100644 --- a/sdk/lib/_internal/lib/isolate_helper.dart +++ b/sdk/lib/_internal/lib/isolate_helper.dart @@ -228,12 +228,12 @@ class _Manager { "(function (f, a) { return function (e) { f(a, e); }})(#, #)", DART_CLOSURE_TO_JS(IsolateNatives._processWorkerMessage), mainManager); - JS("void", r"#.onmessage = #", globalThis, function); + JS("void", r"self.onmessage = #", function); // We define dartPrint so that the implementation of the Dart // print method knows what to call. // TODO(ngeoffray): Should we forward to the main isolate? What if // it exited? - JS('void', r'#.dartPrint = function (object) {}', globalThis); + JS('void', r'self.dartPrint = function (object) {}'); } @@ -403,10 +403,8 @@ class _IsolateContext implements IsolateContext { // don't print it. return; } - if (JS('bool', '#.console != null && ' - 'typeof #.console.error == "function"', - globalThis, globalThis)) { - JS('void', '#.console.error(#, #)', globalThis, error, stackTrace); + if (JS('bool', '!!self.console && !!self.console.error')) { + JS('void', 'self.console.error(#, #)', error, stackTrace); } else { print(error); if (stackTrace != null) print(stackTrace); @@ -685,11 +683,9 @@ class _MainManagerStub { const String _SPAWNED_SIGNAL = "spawned"; const String _SPAWN_FAILED_SIGNAL = "spawn failed"; -var globalThis = Primitives.computeGlobalThis(); -var globalWindow = JS('', "#.window", globalThis); -var globalWorker = JS('', "#.Worker", globalThis); -bool globalPostMessageDefined = - JS('', "#.postMessage !== (void 0)", globalThis); +get globalWindow => JS('', "self.window"); +get globalWorker => JS('', "self.Worker"); +bool get globalPostMessageDefined => JS('bool', "!!self.postMessage"); typedef _MainFunction(); typedef _MainFunctionArgs(args); @@ -869,7 +865,7 @@ class IsolateNatives { } static void _consoleLog(msg) { - JS("void", r"#.console.log(#)", globalThis, msg); + JS("void", r"self.console.log(#)", msg); } static _getJSFunctionFromName(String functionName) { @@ -1712,8 +1708,7 @@ class TimerImpl implements Timer { enterJsAsync(); - _handle = JS('int', '#.setTimeout(#, #)', - globalThis, + _handle = JS('int', 'self.setTimeout(#, #)', convertDartClosureToJS(internalCallback, 0), milliseconds); } else { @@ -1726,8 +1721,7 @@ class TimerImpl implements Timer { : _once = false { if (hasTimer()) { enterJsAsync(); - _handle = JS('int', '#.setInterval(#, #)', - globalThis, + _handle = JS('int', 'self.setInterval(#, #)', convertDartClosureToJS(() { callback(this); }, 0), milliseconds); } else { @@ -1743,9 +1737,9 @@ class TimerImpl implements Timer { if (_handle == null) return; leaveJsAsync(); if (_once) { - JS('void', '#.clearTimeout(#)', globalThis, _handle); + JS('void', 'self.clearTimeout(#)', _handle); } else { - JS('void', '#.clearInterval(#)', globalThis, _handle); + JS('void', 'self.clearInterval(#)', _handle); } _handle = null; } else { @@ -1756,7 +1750,7 @@ class TimerImpl implements Timer { bool get isActive => _handle != null; } -bool hasTimer() => JS('', '#.setTimeout', globalThis) != null; +bool hasTimer() => JS('', 'self.setTimeout') != null; /** diff --git a/sdk/lib/_internal/lib/js_helper.dart b/sdk/lib/_internal/lib/js_helper.dart index 4fab0b2d7fc..8f87ac4f8cb 100644 --- a/sdk/lib/_internal/lib/js_helper.dart +++ b/sdk/lib/_internal/lib/js_helper.dart @@ -552,8 +552,6 @@ class Primitives { return JS('int', '#', hash); } - static computeGlobalThis() => JS('', 'function() { return this; }()'); - static _throwFormatException(String string) { throw new FormatException(string); } @@ -721,7 +719,7 @@ class Primitives { static String currentUri() { // In a browser return self.location.href. - if (JS('bool', 'typeof self != "undefined"')) { + if (JS('bool', '!!self.location')) { return JS('String', 'self.location.href'); } diff --git a/sdk/lib/_internal/lib/preambles/d8.js b/sdk/lib/_internal/lib/preambles/d8.js index e9650837f14..99fed2b15d3 100644 --- a/sdk/lib/_internal/lib/preambles/d8.js +++ b/sdk/lib/_internal/lib/preambles/d8.js @@ -4,14 +4,10 @@ // Javascript preamble, that lets the output of dart2js run on V8's d8 shell. -var setTimeout; -var clearTimeout; -var setInterval; -var clearInterval; -var dartMainRunner; -var scheduleImmediate; +(function(self) { + // Using strict mode to avoid accidentally defining global variables. + "use strict"; // Should be first statement of this function. -(function() { // Event loop. // Task queue as cyclic list queue. @@ -247,14 +243,17 @@ var scheduleImmediate; } } - dartMainRunner = function(main, args) { + // Global properties. "self" refers to the global object, so adding a + // property to "self" defines a global variable. + self.dartMainRunner = function(main, args) { // Initialize. var action = function() { main(args); } eventLoop(action); }; - setTimeout = addTimer; - clearTimeout = cancelTimer; - setInterval = addInterval; - clearInterval = cancelTimer; - scheduleImmediate = addTask; -})(); + self.setTimeout = addTimer; + self.clearTimeout = cancelTimer; + self.setInterval = addInterval; + self.clearInterval = cancelTimer; + self.scheduleImmediate = addTask; + self.self = self; +})(this); diff --git a/sdk/lib/_internal/lib/preambles/jsshell.js b/sdk/lib/_internal/lib/preambles/jsshell.js index f24555fd544..e3b4f1aae35 100644 --- a/sdk/lib/_internal/lib/preambles/jsshell.js +++ b/sdk/lib/_internal/lib/preambles/jsshell.js @@ -3,3 +3,12 @@ // BSD-style license that can be found in the LICENSE file. // Javascript preamble, that lets the output of dart2js run on JSShell. + +(function(self) { + // Using strict mode to avoid accidentally defining global variables. + "use strict"; // Should be first statement of this function. + + // Global properties. "self" refers to the global object, so adding a + // property to "self" defines a global variable. + self.self = self; +})(this) diff --git a/sdk/lib/js/dart2js/js_dart2js.dart b/sdk/lib/js/dart2js/js_dart2js.dart index 84ec8f35670..27fac2e1e9d 100644 --- a/sdk/lib/js/dart2js/js_dart2js.dart +++ b/sdk/lib/js/dart2js/js_dart2js.dart @@ -97,7 +97,7 @@ import 'dart:_interceptors' show JavaScriptObject, UnknownJavaScriptObject; import 'dart:_js_helper' show Primitives, convertDartClosureToJS, getIsolateAffinityTag; -final JsObject context = _wrapToDart(Primitives.computeGlobalThis()); +final JsObject context = _wrapToDart(JS('', 'self')); _convertDartFunction(Function f, {bool captureThis: false}) { return JS('',