2010-12-05 07:20:34 +08:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
2010-11-16 10:37:27 +08:00
|
|
|
var Script = require('vm').Script;
|
2010-04-17 23:18:15 +08:00
|
|
|
|
2010-12-05 05:40:39 +08:00
|
|
|
common.globalCheck = false;
|
|
|
|
|
2010-07-16 02:47:25 +08:00
|
|
|
common.debug('run a string');
|
2010-12-05 06:45:52 +08:00
|
|
|
var result = Script.runInThisContext('\'passed\';');
|
2010-04-17 23:18:15 +08:00
|
|
|
assert.equal('passed', result);
|
|
|
|
|
2010-07-16 02:47:25 +08:00
|
|
|
common.debug('thrown error');
|
2010-04-17 23:18:15 +08:00
|
|
|
assert.throws(function() {
|
2010-12-05 06:45:52 +08:00
|
|
|
Script.runInThisContext('throw new Error(\'test\');');
|
2010-04-17 23:18:15 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
hello = 5;
|
|
|
|
Script.runInThisContext('hello = 2');
|
|
|
|
assert.equal(2, hello);
|
|
|
|
|
|
|
|
|
2010-12-05 06:45:52 +08:00
|
|
|
common.debug('pass values');
|
|
|
|
code = 'foo = 1;' +
|
|
|
|
'bar = 2;' +
|
|
|
|
'if (typeof baz !== \'undefined\') throw new Error(\'test fail\');';
|
2010-04-17 23:18:15 +08:00
|
|
|
foo = 2;
|
2010-12-05 06:45:52 +08:00
|
|
|
obj = { foo: 0, baz: 3 };
|
2010-04-17 23:18:15 +08:00
|
|
|
var baz = Script.runInThisContext(code);
|
|
|
|
assert.equal(0, obj.foo);
|
|
|
|
assert.equal(2, bar);
|
|
|
|
assert.equal(1, foo);
|
|
|
|
|
2010-12-05 06:45:52 +08:00
|
|
|
common.debug('call a function');
|
|
|
|
f = function() { foo = 100 };
|
|
|
|
Script.runInThisContext('f()');
|
2010-04-17 23:18:15 +08:00
|
|
|
assert.equal(100, foo);
|