Skip to content

Commit

Permalink
Encapsulate global object.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbullington committed Dec 3, 2016
1 parent b3650e7 commit 327ac2e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 55 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.2.0

* Prevent encapsulation, `global.self = global` (old) vs.
`var self = Object.create(global)` (new).

## 1.1.0

* Set `global.location` so that `Uri.base()` works properly on Windows in most
Expand Down
93 changes: 47 additions & 46 deletions lib/preamble.dart
Original file line number Diff line number Diff line change
@@ -1,56 +1,57 @@
library node_preamble;

var _NODE_PREAMBLE = r"""
// TODO: This isn't really a correct transformation. For example, it will fail
// for paths that contain characters that need to be escaped in URLs. Once
// dart-lang/sdk#27979 is fixed, it should be possible to make it better.
global.location = {
href: "file://" + (function() {
var cwd = process.cwd();
if (process.platform != "win32") return cwd;
return "/" + cwd.replace("\\", "/");
})() + "/"
};
global.scheduleImmediate = setImmediate;
global.self = global;
global.require = require;
global.exports = exports;
global.process = process;
function computeCurrentScript() {
try {
throw new Error();
} catch(e) {
var stack = e.stack;
var re = new RegExp("^ *at [^(]*\\((.*):[0-9]*:[0-9]*\\)$", "mg");
var lastMatch = null;
do {
var match = re.exec(stack);
if (match != null) lastMatch = match;
} while (match != null);
return lastMatch[1];
}
var self = Object.create(global);
// TODO: This isn't really a correct transformation. For example, it will fail
// for paths that contain characters that need to be escaped in URLs. Once
// dart-lang/sdk#27979 is fixed, it should be possible to make it better.
self.location = {
href: "file://" + (function() {
var cwd = process.cwd();
if (process.platform != "win32") return cwd;
return "/" + cwd.replace("\\", "/");
})() + "/"
};
self.scheduleImmediate = setImmediate;
self.require = require;
self.exports = exports;
self.process = process;
function computeCurrentScript() {
try {
throw new Error();
} catch(e) {
var stack = e.stack;
var re = new RegExp("^ *at [^(]*\\((.*):[0-9]*:[0-9]*\\)$", "mg");
var lastMatch = null;
do {
var match = re.exec(stack);
if (match != null) lastMatch = match;
} while (match != null);
return lastMatch[1];
}
}
var cachedCurrentScript = null;
global.document = {
get currentScript() {
if (cachedCurrentScript == null) {
cachedCurrentScript = {src: computeCurrentScript()};
}
return cachedCurrentScript;
var cachedCurrentScript = null;
self.document = {
get currentScript() {
if (cachedCurrentScript == null) {
cachedCurrentScript = {src: computeCurrentScript()};
}
};
return cachedCurrentScript;
}
};
global.dartDeferredLibraryLoader = function(uri, successCallback, errorCallback) {
try {
load(uri);
successCallback();
} catch (error) {
errorCallback(error);
}
};
self.dartDeferredLibraryLoader = function(uri, successCallback, errorCallback) {
try {
load(uri);
successCallback();
} catch (error) {
errorCallback(error);
}
};
""";

String getPreamble() => _NODE_PREAMBLE;
17 changes: 9 additions & 8 deletions lib/preamble.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
var self = Object.create(global);

// TODO: This isn't really a correct transformation. For example, it will fail
// for paths that contain characters that need to be escaped in URLs. Once
// dart-lang/sdk#27979 is fixed, it should be possible to make it better.
global.location = {
self.location = {
href: "file://" + (function() {
var cwd = process.cwd();
if (process.platform != "win32") return cwd;
return "/" + cwd.replace("\\", "/");
})() + "/"
};

global.scheduleImmediate = setImmediate;
global.self = global;
global.require = require;
global.exports = exports;
global.process = process;
self.scheduleImmediate = setImmediate;
self.require = require;
self.exports = exports;
self.process = process;

function computeCurrentScript() {
try {
Expand All @@ -31,7 +32,7 @@ function computeCurrentScript() {
}

var cachedCurrentScript = null;
global.document = {
self.document = {
get currentScript() {
if (cachedCurrentScript == null) {
cachedCurrentScript = {src: computeCurrentScript()};
Expand All @@ -40,7 +41,7 @@ global.document = {
}
};

global.dartDeferredLibraryLoader = function(uri, successCallback, errorCallback) {
self.dartDeferredLibraryLoader = function(uri, successCallback, errorCallback) {
try {
load(uri);
successCallback();
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: node_preamble
author: Michael Bullington <mikebullingtn@gmail.com>
homepage: https://github.com/mbullington/node_preamble.dart
version: 1.1.0
version: 1.2.0
description: Better node.js preamble for dart2js, use it in your build system.

0 comments on commit 327ac2e

Please sign in to comment.