Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils/lib/sandbox.js
diff options
context:
space:
mode:
Diffstat (limited to 'utils/lib/sandbox.js')
-rwxr-xr-xutils/lib/sandbox.js137
1 files changed, 68 insertions, 69 deletions
diff --git a/utils/lib/sandbox.js b/utils/lib/sandbox.js
index 52394bc..93b77e0 100755
--- a/utils/lib/sandbox.js
+++ b/utils/lib/sandbox.js
@@ -1,4 +1,7 @@
+// NOTE: this file is used is the bootstrapping process,
+// so any "requires" must be accounted for in narwhal.js
+
exports.Loader = function (options) {
var loader = {};
var factories = options.factories || {};
@@ -6,22 +9,23 @@ exports.Loader = function (options) {
var extensions = options.extensions || ["", ".js"];
var timestamps = {};
- loader.resolve = function (id, baseId) {
- if (typeof id != "string")
- throw new Error("module id '" + id + "' is not a String");
- if (id.charAt(0) == ".") {
- id = dirname(baseId) + "/" + id;
- }
- return normal(id);
- };
+ loader.resolve = exports.resolve;
loader.find = function (topId) {
for (var j = 0; j < extensions.length; j++) {
- var ext = extensions[j];
- for (var i = 0; i < paths.length; i++) {
- var fileName = join(paths[i], topId + ext);
- if (system.fs.isFile(fileName))
- return fileName;
+ var extension = extensions[j];
+ if (system.fs.isAbsolute(topId)) {
+ var path = topId + extension;
+ if (system.fs.isFile(path)) {
+ return path;
+ }
+ }
+ else {
+ for (var i = 0; i < paths.length; i++) {
+ var path = system.fs.join(paths[i], topId + extension);
+ if (system.fs.isFile(path))
+ return path;
+ }
}
}
throw new Error("require error: couldn't find \"" + topId + '"');
@@ -89,26 +93,27 @@ exports.MultiLoader = function (options) {
[".js", self.loader]
];
- self.resolve = function (id, baseId) {
- if (typeof id != "string")
- throw new Error("module id '" + id + "' is not a String");
- if (id.charAt(0) == ".") {
- id = dirname(baseId) + "/" + id;
- }
- return normal(id);
- };
+ self.resolve = exports.resolve;
self.find = function (topId) {
for (var j = 0; j < self.loaders.length; j++) {
var pair = self.loaders[j];
var extension = pair[0];
var loader = pair[1];
- for (var i = 0; i < self.paths.length; i++) {
- var path = join(self.paths[i], topId + extension);
+ if (system.fs.isAbsolute(topId)) {
+ var path = topId + extension;
if (system.fs.isFile(path)) {
return [loader, path];
}
}
+ else {
+ for (var i = 0; i < self.paths.length; i++) {
+ var path = system.fs.join(self.paths[i], topId + extension);
+ if (system.fs.isFile(path)) {
+ return [loader, path];
+ }
+ }
+ }
}
throw "require error: couldn't find \"" + topId + '"';
};
@@ -131,10 +136,37 @@ exports.MultiLoader = function (options) {
return Object.prototype.hasOwnProperty.call(factories, topId);
};
-
return self;
};
+exports.AttenuatedLoader = function (loader) {
+ var self = {};
+
+ self.resolve = Object.freeze(function (id, baseId) {
+ return loader.resolve(id, baseId);
+ });
+
+ self.fetch = Object.freeze(function (topId) {
+ if (/\./.test(topId))
+ throw new Error("Invalid module identifier");
+ return loader.fetch(topId);
+ });
+
+ self.load = Object.freeze(function (topId, path) {
+ if (/\./.test(topId))
+ throw new Error("Invalid module identifier");
+ return loader.load(topId, path);
+ });
+
+ self.reload = Object.freeze(function (topId) {
+ if (/\./.test(topId))
+ throw new Error("Invalid module identifier");
+ return loader.reload(topId, path);
+ });
+
+ return Object.freeze(self);
+};
+
exports.Sandbox = function (options) {
options = options || {};
var loader = options.loader;
@@ -142,15 +174,15 @@ exports.Sandbox = function (options) {
var modules = options.modules || {};
var debug = options.debug !== undefined ? !!options.debug : system.debug;
- var debugDepth = 0;
- var mainId;
-
// managed print free variable in the sandbox forwards
// to system.print
- var print = function () {
+ var print = options.print || function () {
return sandboxSystem.print.apply(sandboxSystem, arguments);
};
+ var debugDepth = 0;
+ var mainId;
+
var sandbox = function (id, baseId, force, reload) {
id = loader.resolve(id, baseId);
@@ -333,6 +365,7 @@ exports.sandbox = function(main, system, options) {
var prefix = options['prefix'];
var loader = options['loader'] || require.loader;
var modules = options['modules'] || {};
+ var print = options['print'];
var debug = options['debug'];
if (!loader) throw new Error(
"sandbox cannot operate without a loader, either explicitly " +
@@ -345,51 +378,17 @@ exports.sandbox = function(main, system, options) {
modules: modules,
loader: loader,
system: system,
+ print: print,
debug: debug
});
return sandbox.main(main);
};
-
-
-////////////////////////////////////////////////
-// Ugh, these are duplicated from the File object, since they're required for
-// require, which is required for loading the File object.
-var dirname = function(path) {
- var raw = String(path),
- match = raw.match(/^(.*)\/[^\/]+\/?$/);
- if (match && match[1])
- return match[1]
- else if (raw.charAt(0) == "/")
- return "/"
- else
- return "."
-};
-
-var normal = function(path) {
- var original;
-
- do {
- original = path;
- path = path
- .replace(/[^\/]+\/\.\.\//g, "")
- .replace(/([^\.])\.\//g, "$1")
- .replace(/^\.\//g, "")
- .replace(/\/\/+/g, "/");
- } while (path !== original);
-
- return path;
-};
-
-var join = function (base) {
- for (var i = 1; i < arguments.length; i++) {
- var rel = arguments[i];
- if (rel.match(/^\//)) {
- base = rel;
- } else {
- base = base + '/' + rel;
- }
+exports.resolve = function (id, baseId) {
+ if (typeof id != "string")
+ throw new Error("module id '" + id + "' is not a String");
+ if (id.charAt(0) == ".") {
+ id = system.fs.dirname(baseId) + "/" + id;
}
- return normal(base);
+ return system.fs.normal(id);
};
-////////////////////////////////////////////////