Use "self" instead of globalThis.

Main benefits:

* Simpler code.

* Smaller generated code (no use of lazy statics).

* One step closer to "use strict" as Primitives.computeGlobalThis() wouldn't work in strict mode.

R=johnniwinther@google.com

Review URL: https://codereview.chromium.org//345533004

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@37483 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
ahe@google.com
2014-06-19 09:46:32 +00:00
parent 306b3af9f0
commit ecd3ed39ec
6 changed files with 40 additions and 42 deletions
+3 -5
View File
@@ -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));
}
+13 -19
View File
@@ -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;
/**
+1 -3
View File
@@ -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');
}
+13 -14
View File
@@ -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);
@@ -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)
+1 -1
View File
@@ -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('',