diff options
-rw-r--r-- | jstests/core/loadserverscripts.js | 12 | ||||
-rw-r--r-- | src/mongo/shell/db.js | 5 |
2 files changed, 14 insertions, 3 deletions
diff --git a/jstests/core/loadserverscripts.js b/jstests/core/loadserverscripts.js index aa8d1dea486..c0b5a4065a1 100644 --- a/jstests/core/loadserverscripts.js +++ b/jstests/core/loadserverscripts.js @@ -20,12 +20,20 @@ assert.eq( typeof myfunc2, "undefined", "Checking that myfunc2() is undefined" ) // Insert a function in the context of this process: make sure it's in the collection testdb.system.js.insert( { _id: "myfunc", "value": function(){ return "myfunc"; } } ); +testdb.system.js.insert( { _id: "mystring", "value": "var root = this;" } ); +testdb.system.js.insert( { _id: "changeme", "value": false }); + x = testdb.system.js.count(); -assert.eq( x, 1, "Should now be one function in the system.js collection"); +assert.eq( x, 3, "Should now be one function in the system.js collection"); + +// Set a global variable that will be over-written +var changeme = true // Load that function testdb.loadServerScripts(); assert.eq( typeof myfunc, "function", "Checking that myfunc() loaded correctly" ); +assert.eq( typeof mystring, "string", "Checking that mystring round-tripped correctly" ); +assert.eq( changeme, false, "Checking that global var was overwritten" ); // Make sure it works x = myfunc(); @@ -41,7 +49,7 @@ coproc(); // Make sure the collection's been updated x = testdb.system.js.count(); -assert.eq( x, 2, "Should now be two functions in the system.js collection"); +assert.eq( x, 4, "Should now be two functions in the system.js collection"); // Load the new functions: test them as above diff --git a/src/mongo/shell/db.js b/src/mongo/shell/db.js index 94df6284213..5f58cfec23f 100644 --- a/src/mongo/shell/db.js +++ b/src/mongo/shell/db.js @@ -1155,7 +1155,10 @@ DB.prototype.getQueryOptions = function() { /* Loads any scripts contained in system.js into the client shell. */ DB.prototype.loadServerScripts = function(){ - this.system.js.find().forEach(function(u){eval(u._id + " = " + u.value);}); + var global = Function('return this')(); + this.system.js.find().forEach(function(u) { + global[u._id] = u.value; + }); } |