diff --git a/isolate.js b/isolate.js index f812009..59c4629 100644 --- a/isolate.js +++ b/isolate.js @@ -1,8 +1,8 @@ -// Generated by CoffeeScript 1.4.0 +// Generated by CoffeeScript 1.9.1 (function() { - var __hasProp = {}.hasOwnProperty, - __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, - __slice = [].slice; + var hasProp = {}.hasOwnProperty, + bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + slice = [].slice; (function(root, factory) { if (typeof exports === 'object') { @@ -13,7 +13,9 @@ return root.Isolate = factory(); } })(this, function() { - var IsolationContext, IsolationFactory, build_dependencies, getMatcherForPath, getType, passthru; + var IsolationContext, IsolationFactory, build_dependencies, getMatcherForPath, getType, isolatedContextNameNumber, isolationContextNumber, passthru; + isolatedContextNameNumber = 0; + isolationContextNumber = 0; getType = function(o) { return Object.prototype.toString.call(o); }; @@ -40,7 +42,7 @@ regex = getMatcherForPath(val); matching_dependencies = []; for (path in dependencies) { - if (!__hasProp.call(dependencies, path)) continue; + if (!hasProp.call(dependencies, path)) continue; mod = dependencies[path]; if (0 === path.indexOf('isolate!')) { continue; @@ -57,39 +59,27 @@ return dependencies; }; IsolationFactory = (function() { - - function IsolationFactory(factory) { - this.factory = factory; + function IsolationFactory(factory1) { + this.factory = factory1; } return IsolationFactory; })(); IsolationContext = (function() { - - function IsolationContext(name) { + function IsolationContext(name1) { var that; - this.name = name != null ? name : 'default'; - this.mapAsFactory = __bind(this.mapAsFactory, this); - - this.reset = __bind(this.reset, this); - - this.willRequire = __bind(this.willRequire, this); - - this.mapType = __bind(this.mapType, this); - - this.map = __bind(this.map, this); - - this.passthru = __bind(this.passthru, this); - - this.useRequire = __bind(this.useRequire, this); - - this.load = __bind(this.load, this); - - this.findMatchingHandler = __bind(this.findMatchingHandler, this); - - this.processDependency = __bind(this.processDependency, this); - + this.name = name1 != null ? name1 : 'default'; + this.mapAsFactory = bind(this.mapAsFactory, this); + this.reset = bind(this.reset, this); + this.willRequire = bind(this.willRequire, this); + this.mapType = bind(this.mapType, this); + this.map = bind(this.map, this); + this.passthru = bind(this.passthru, this); + this.useRequire = bind(this.useRequire, this); + this.load = bind(this.load, this); + this.findMatchingHandler = bind(this.findMatchingHandler, this); + this.processDependency = bind(this.processDependency, this); IsolationContext.contexts[this.name] = this; this.rules = []; this.typeHandlers = {}; @@ -111,10 +101,10 @@ }; IsolationContext.prototype.findMatchingHandler = function(path, actual) { - var fakeModule, fakeModulePath, rule, typeHandler, _i, _len, _ref; - _ref = this.rules; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - rule = _ref[_i]; + var fakeModule, fakeModulePath, i, len, ref, rule, typeHandler; + ref = this.rules; + for (i = 0, len = ref.length; i < len; i++) { + rule = ref[i]; if (rule.matcher.test(path)) { return rule.handler; } @@ -133,33 +123,34 @@ }; IsolationContext.prototype.require = function(requested_module, context) { - var actual, dependencies, full_module_path, handler, isolatedModule, path, urlForException, _i, _len, _ref, _ref1, _ref2; + var actual, dependencies, err, full_module_path, handler, i, isolatedModule, len, path, ref, ref1, ref2, urlForException; context = context || this; full_module_path = module.constructor._resolveFilename(requested_module, context); module.constructor._cache = {}; try { require(full_module_path); - } catch (err) { + } catch (_error) { + err = _error; urlForException = "https://github.com/tnwinc/Isolate/wiki/Error:-An-error-occurred-while-preparing-to-isolate-the-module"; err.message = "An error occurred while preparing to isolate the module: " + requested_module + "\nFor more information, see " + urlForException + "\nInner Exception:\n" + err.message; throw err; } delete module.constructor._cache[full_module_path]; dependencies = {}; - _ref = module.constructor._cache; - for (path in _ref) { - if (!__hasProp.call(_ref, path)) continue; - actual = _ref[path]; + ref = module.constructor._cache; + for (path in ref) { + if (!hasProp.call(ref, path)) continue; + actual = ref[path]; actual.exports = dependencies[path] = this.processDependency(path, actual.exports, full_module_path); actual.exports.actual = actual; } isolatedModule = require(full_module_path); isolatedModule.dependencies = build_dependencies(dependencies); module.constructor._cache = {}; - if ((_ref1 = this.isolateCompleteHandlers) != null ? _ref1.length : void 0) { - _ref2 = this.isolateCompleteHandlers; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - handler = _ref2[_i]; + if ((ref1 = this.isolateCompleteHandlers) != null ? ref1.length : void 0) { + ref2 = this.isolateCompleteHandlers; + for (i = 0, len = ref2.length; i < len; i++) { + handler = ref2[i]; handler(isolatedModule); } } @@ -167,17 +158,16 @@ }; IsolationContext.prototype.load = function(requested_module, req, load, config) { - var isolatedContextName, isolatedRequire, isolatedRequireCtx, isolationContextName, isolationCtx, mainCtx, modulesToLoad, urlForException, _i, _ref, _ref1, _ref2, _ref3, _ref4, - _this = this; + var _i, err, isolatedContextName, isolatedRequire, isolatedRequireCtx, isolationContextName, isolationCtx, mainCtx, modulesToLoad, ref, ref1, ref2, ref3, ref4, urlForException; IsolationContext._require || (IsolationContext._require = require); if ((_i = requested_module.indexOf(':')) > -1) { - _ref = requested_module.split(':'), isolationContextName = _ref[0], requested_module = _ref[1]; + ref = requested_module.split(':'), isolationContextName = ref[0], requested_module = ref[1]; } else { isolationContextName = 'default'; } isolationCtx = IsolationContext.contexts[isolationContextName]; - mainCtx = ((_ref1 = IsolationContext._require) != null ? (_ref2 = _ref1.s) != null ? (_ref3 = _ref2.contexts) != null ? _ref3['_'] : void 0 : void 0 : void 0) || ((_ref4 = IsolationContext._require) != null ? _ref4.context : void 0); - isolatedContextName = "isolated_" + (Math.floor(Math.random() * 100000)); + mainCtx = ((ref1 = IsolationContext._require) != null ? (ref2 = ref1.s) != null ? (ref3 = ref2.contexts) != null ? ref3['_'] : void 0 : void 0 : void 0) || ((ref4 = IsolationContext._require) != null ? ref4.context : void 0); + isolatedContextName = "isolated_" + (isolatedContextNameNumber++); isolationCtx.require = isolatedRequire = IsolationContext._require.config({ context: isolatedContextName, baseUrl: mainCtx.config.baseUrl @@ -185,53 +175,57 @@ isolatedRequireCtx = IsolationContext._require.s.contexts[isolatedContextName]; modulesToLoad = [requested_module].concat(isolationCtx.ensuredAsyncModules || []); try { - return req(modulesToLoad, function(mod) { - var dependencies, modName, modVal, reload, undef, _module, _ref5, _ref6; - undef = isolatedRequireCtx.undef || ((_ref5 = isolatedRequireCtx.require) != null ? _ref5.undef : void 0); - for (_module in isolatedRequireCtx.defined) { - undef(_module); - } - _ref6 = mainCtx.defined; - for (modName in _ref6) { - if (!__hasProp.call(_ref6, modName)) continue; - modVal = _ref6[modName]; - if (modName === requested_module || modName === 'isolate') { - continue; + return req(modulesToLoad, (function(_this) { + return function(mod) { + var _module, dependencies, modName, modVal, ref5, ref6, reload, undef; + undef = isolatedRequireCtx.undef || ((ref5 = isolatedRequireCtx.require) != null ? ref5.undef : void 0); + for (_module in isolatedRequireCtx.defined) { + undef(_module); } - isolatedRequireCtx.defined[modName] = isolationCtx.processDependency(modName, modVal, requested_module); - } - dependencies = build_dependencies(isolatedRequireCtx.defined); - reload = function(done) { - var _this = this; - undef(requested_module); - return isolatedRequire([requested_module], function(isolatedModule) { - var handler, key, reload_method_name, _j, _k, _len, _len1, _ref7, _ref8, _ref9; - if (isolatedModule == null) { - throw Error("The requested module " + requested_module + " was not found."); + ref6 = mainCtx.defined; + for (modName in ref6) { + if (!hasProp.call(ref6, modName)) continue; + modVal = ref6[modName]; + if (modName === requested_module || modName === 'isolate') { + continue; } - isolatedModule.dependencies = dependencies; - reload_method_name = isolatedModule.reload != null ? '_reload' : 'reload'; - isolatedModule[reload_method_name] = reload; - _ref7 = mainCtx.defined; - for (_j = 0, _len = _ref7.length; _j < _len; _j++) { - key = _ref7[_j]; - delete mainCtx.defined[key]; - } - if ((_ref8 = isolationCtx.isolateCompleteHandlers) != null ? _ref8.length : void 0) { - _ref9 = isolationCtx.isolateCompleteHandlers; - for (_k = 0, _len1 = _ref9.length; _k < _len1; _k++) { - handler = _ref9[_k]; - handler(isolatedModule); - } - } - return typeof done === "function" ? done(isolatedModule) : void 0; + isolatedRequireCtx.defined[modName] = isolationCtx.processDependency(modName, modVal, requested_module); + } + dependencies = build_dependencies(isolatedRequireCtx.defined); + reload = function(done) { + undef(requested_module); + return isolatedRequire([requested_module], (function(_this) { + return function(isolatedModule) { + var handler, i, j, key, len, len1, ref7, ref8, ref9, reload_method_name; + if (isolatedModule == null) { + throw Error("The requested module " + requested_module + " was not found."); + } + isolatedModule.dependencies = dependencies; + reload_method_name = isolatedModule.reload != null ? '_reload' : 'reload'; + isolatedModule[reload_method_name] = reload; + ref7 = mainCtx.defined; + for (i = 0, len = ref7.length; i < len; i++) { + key = ref7[i]; + delete mainCtx.defined[key]; + } + if ((ref8 = isolationCtx.isolateCompleteHandlers) != null ? ref8.length : void 0) { + ref9 = isolationCtx.isolateCompleteHandlers; + for (j = 0, len1 = ref9.length; j < len1; j++) { + handler = ref9[j]; + handler(isolatedModule); + } + } + return typeof done === "function" ? done(isolatedModule) : void 0; + }; + })(this)); + }; + return reload(function(isolatedModule) { + return load(isolatedModule); }); }; - return reload(function(isolatedModule) { - return load(isolatedModule); - }); - }); - } catch (err) { + })(this)); + } catch (_error) { + err = _error; urlForException = "https://github.com/tnwinc/Isolate/wiki/Error:-An-error-occurred-while-preparing-to-isolate-the-module"; err.message = "An error occurred while preparing to isolate the module: " + requested_module + "\nFor more information, see " + urlForException + "\nInner Exception:\n" + err.message; throw err; @@ -244,13 +238,13 @@ }; IsolationContext.prototype.passthru = function() { - var path, paths, _i, _len; - paths = 1 <= arguments.length ? __slice.call(arguments, 0) : []; + var i, len, path, paths; + paths = 1 <= arguments.length ? slice.call(arguments, 0) : []; if ('[object Array]' === getType(paths[0])) { paths = paths[0]; } - for (_i = 0, _len = paths.length; _i < _len; _i++) { - path = paths[_i]; + for (i = 0, len = paths.length; i < len; i++) { + path = paths[i]; this.rules.unshift({ matcher: getMatcherForPath(path), handler: passthru @@ -260,13 +254,13 @@ }; IsolationContext.prototype.map = function() { - var args, handler, path, _ref; - args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; + var args, handler, path, ref; + args = 1 <= arguments.length ? slice.call(arguments, 0) : []; if (getType(args[0]) === '[object Object]') { - _ref = args[0]; - for (path in _ref) { - if (!__hasProp.call(_ref, path)) continue; - handler = _ref[path]; + ref = args[0]; + for (path in ref) { + if (!hasProp.call(ref, path)) continue; + handler = ref[path]; this.map(path, handler); } } else { @@ -283,13 +277,13 @@ }; IsolationContext.prototype.mapType = function() { - var args, handler, type, _ref; - args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; + var args, handler, ref, type; + args = 1 <= arguments.length ? slice.call(arguments, 0) : []; if (getType(args[0]) === '[object Object]') { - _ref = args[0]; - for (type in _ref) { - if (!__hasProp.call(_ref, type)) continue; - handler = _ref[type]; + ref = args[0]; + for (type in ref) { + if (!hasProp.call(ref, type)) continue; + handler = ref[type]; this.mapType(type, handler); } } else { @@ -303,10 +297,10 @@ }; IsolationContext.prototype.willRequire = function() { - var args, _ref; - args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; + var args, ref; + args = 1 <= arguments.length ? slice.call(arguments, 0) : []; this.ensuredAsyncModules || (this.ensuredAsyncModules = []); - (_ref = this.ensuredAsyncModules).push.apply(_ref, args); + (ref = this.ensuredAsyncModules).push.apply(ref, args); return this; }; @@ -317,16 +311,16 @@ }; IsolationContext.prototype.mapAsFactory = function() { - var args, factory, path, _ref; - args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; + var args, factory, path, ref; + args = 1 <= arguments.length ? slice.call(arguments, 0) : []; if (args.length === 1) { if ('[object Function]' === getType(args[0])) { return new IsolationFactory(args[0]); } else { - _ref = args[0]; - for (path in _ref) { - if (!__hasProp.call(_ref, path)) continue; - factory = _ref[path]; + ref = args[0]; + for (path in ref) { + if (!hasProp.call(ref, path)) continue; + factory = ref[path]; this.mapAsFactory(path, factory); } } @@ -347,16 +341,16 @@ }; IsolationContext.prototype.newContext = function(name) { - var ctx, handler, type, _ref, _ref1, _ref2; - name = name || ("isolation_context_" + (Math.floor(Math.random() * 10000))); + var ctx, handler, ref, ref1, ref2, type; + name = name || ("isolation_context_" + (isolationContextNumber++)); ctx = new IsolationContext(name); - ctx.rules = (_ref = this.rules) != null ? _ref.slice(0) : void 0; - _ref1 = this.typeHandlers; - for (type in _ref1) { - handler = _ref1[type]; + ctx.rules = (ref = this.rules) != null ? ref.slice(0) : void 0; + ref1 = this.typeHandlers; + for (type in ref1) { + handler = ref1[type]; ctx.typeHandlers[type] = handler; } - ctx.isolateCompleteHandlers = (_ref2 = this.isolateCompleteHandlers) != null ? _ref2.slice(0) : void 0; + ctx.isolateCompleteHandlers = (ref2 = this.isolateCompleteHandlers) != null ? ref2.slice(0) : void 0; return ctx; }; diff --git a/src/isolate.coffee b/src/isolate.coffee index 01dfe98..77afe94 100644 --- a/src/isolate.coffee +++ b/src/isolate.coffee @@ -7,6 +7,10 @@ root.Isolate = factory() ) this, -> + # used to keep contexts unique + isolatedContextNameNumber = 0 + isolationContextNumber = 0 + #### Helpers to keep later code more readable # Get's the Object.prototype.toString "type" of an object. # It is often more informative than a simple typeof statement. @@ -174,7 +178,7 @@ # Generate a secondary require context, used to hold the # standins. - isolatedContextName = "isolated_#{Math.floor Math.random() * 100000}" + isolatedContextName = "isolated_#{isolatedContextNameNumber++}" isolationCtx.require = isolatedRequire = IsolationContext._require.config context: isolatedContextName baseUrl: mainCtx.config.baseUrl @@ -300,7 +304,7 @@ return this newContext: (name)-> - name = name or "isolation_context_#{Math.floor Math.random() * 10000}" + name = name or "isolation_context_#{isolationContextNumber++}" ctx = new IsolationContext(name) ctx.rules = this.rules?.slice 0 ctx.typeHandlers[type] = handler for type, handler of this.typeHandlers