Permalink
Browse files

Fix local scope in contexts.

  • Loading branch information...
1 parent 9292ad5 commit 0600788ba05c65dd073c811dee97788a0422084b @Benvie committed Apr 12, 2012
Showing with 63 additions and 26 deletions.
  1. +63 −26 lib/Context.js
View
@@ -66,7 +66,7 @@ Context.prototype = {
initialize: function initialize(globalSettings){
this.presets = {};
this.ctx = vm.createContext();
- Object.defineProperty(this, 'global', { value: vm.runInContext('this', this.ctx), writable: true });
+ Object.defineProperty(this, 'global', desc(vm.runInContext('this', this.ctx), true));
var init = inspector.run(this.ctx)(this.settings, globalSettings, builtins, styling.inspector);
for (var k in init) this[k] = init[k];
@@ -90,38 +90,25 @@ Context.prototype = {
_module._cache = [];
_module.filename = fakename;
_module.loaded = true;
- _module.locals = this.locals = {
+ _module.locals = {
require: _require,
module: _module,
exports: _module.exports,
__dirname: path.dirname(fakename),
__filename: fakename
};
- this.define('__dirname', {
- writable: true,
- configurable: true,
- value: path.dirname(fakename),
- });
- this.define('__filename', {
- writable: true,
- configurable: true,
- value: fakename,
- });
- this.define('require', {
- writable: true,
- configurable: true,
- value: _require,
- });
- this.define('module', {
- writable: true,
- configurable: true,
- value: _module,
- });
- this.define('exports', {
- configurable: true,
- get: function(){ return _module.exports },
- set: function(v){ _module.exports = v }
+ desc.toggleHidden();
+ this.defines({
+ __dirname: desc(path.dirname(fakename)),
+ __filename: desc(fakename),
+ require: desc(_require),
+ module: desc(_module),
+ console: desc(console),
+ exports: desc(function get(){ return _module.exports },
+ function set(v){ _module.exports = v }),
});
+ desc.toggleHidden();
+ Object.defineProperty(this, 'locals', desc(function get(){ return _module.locals }));
this.refresh();
return this;
},
@@ -134,6 +121,12 @@ Context.prototype = {
vm.runInContext('global = this', this.ctx);
},
+ defines: function defines(o){
+ Object.keys(o).forEach(function(key){
+ this.define(key, o[key]);
+ }, this);
+ },
+
installPresets: function installPresets(name){
if (name in this.presets) return false;
var preset = Context.presets[name]();
@@ -214,3 +207,47 @@ Context.prototype = {
}).join('\n');
}
};
+
+function desc(v, h, r){ return new Desc(v, h, r) }
+desc.toggleHidden = function(){ Desc.prototype.enumerable = !Desc.prototype.enumerable }
+
+function Desc(v, h, r){
+ if (Object(v) === v) {
+ if (typeof v === 'function') {
+ if (v.name === 'get' || v.name === 'set') {
+ this[v.name] = v;
+ if (typeof h === 'function') {
+ this[this.get ? 'set' : 'get'] = h;
+ h = r;
+ }
+ } else if (typeof h === 'function') {
+ this.get = v;
+ this.set = h;
+ h = r;
+ } else {
+ this.value = v;
+ }
+ } else if ('value' in v) {
+ this.value = v.value;
+ if (W in v && !v[W]) this[W] = false;
+ if (E in v && !v[E]) this[E] = false;
+ if (C in v && !v[C]) this[C] = false;
+ } else if ('get' in v || 'set' in v) {
+ this.get = v.get;
+ this.set = v.set;
+ if (E in v && !v[E]) this[E] = false;
+ if (C in v && !v[C]) this[C] = false;
+ } else {
+ this.value = v;
+ }
+ } else {
+ this.value = v;
+ }
+ if (h) this[E] = false;
+ if ('value' in this) this[W] = !r;
+}
+
+Desc.prototype.enumerable = true;
+Desc.prototype.configurable = true;
+
+var W = 'writable', E='enumerable', C='configurable'

0 comments on commit 0600788

Please sign in to comment.