本文整理匯總了TypeScript中vm.createScript函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript createScript函數的具體用法?TypeScript createScript怎麽用?TypeScript createScript使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了createScript函數的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: parse
function parse() {
try {
fs.statSync(cheFile);
// we have a file
} catch (e) {
console.log('No chefile defined, use default settings');
return;
}
// load the chefile script if defined
var script_code = fs.readFileSync(cheFile);
// setup the bindings for the script
che.server = {};
che.server.ip = che.hostname;
// create sandboxed object
var sandbox = { "che": che, "console": console};
var script = vm.createScript(script_code);
script.runInNewContext(sandbox);
if (debug) {
console.log('Che file parsing object is ', che);
}
}
示例2: DynamicRequire
fs.readFile(scriptFile, (err: NodeJS.ErrnoException, data: Buffer) => {
let required: { [id: number]: any } = {};
let requiredIndex: number = 0;
try {
let code = '(function (request, response, next){\ntry{\n'
+ data.toString().replace(/require\s*\(\s*[\'"]\s*\[\s*(\w+)\s*\]\s*[\'"]\s*\)/ig, (capture: string, ...args: any[]) => {
return args[0];
}).replace(/require\s*\(\s*[\'"](\.+[\/a-z_\-\s0-9\.]+)[\'"]\s*\)/ig, (capture: string, ...args: any[]) => {
//console.log('Replacing: ', capture);
let $file = pathreducer.reduce($directory + '//' + args[0] + '.js');
required[requiredIndex] = DynamicRequire(pathreducer.filename($file), pathreducer.pathname($file));
let replacement = '$__required[' + requiredIndex + ']';
requiredIndex += 1;
return replacement;
}) +
'\n}\ncatch(ex){\n\tconsole.log("Error:", ex);\n\tresponse.statusCode = 500;\n\tresponse.end(ex.toString()); \n}\n})';
//console.log(code);
let context = vm.createContext({
console: console,
require: require,
__dirname: $directory,
__filename: scriptFile,
process: process,
$__required: required
});
let _script = vm.createScript(code);
let fn: MiddlewareHandler = _script.runInContext(context);
fn(req, res, next);
}
catch (ex) {
console.log(ex);
}
});
示例3: _eval
/**
* Evaluate the code snippet.
*/
function _eval (code: string, context: any) {
const undo = evalFile.input
const isCompletion = !/\n$/.test(code)
// Increment eval constants for the compiler to pick up changes.
evalFile.input += code
evalFile.version++
let output: string
// Undo on TypeScript compilation errors.
try {
output = service().compile(evalFile.input, EVAL_PATH)
} catch (error) {
evalFile.input = undo
throw error
}
// Use `diff` to check for new JavaScript to execute.
const changes = diffLines(evalFile.output, output)
// Revert the code if running in "completion" environment. Updated the output
// to diff against future executions when evaling code.
if (isCompletion) {
evalFile.input = undo
} else {
evalFile.output = output
}
let result: any
// Iterate over the diff and evaluate `added` lines. The only removed lines
// should be the source map and lines that stay the same are ignored.
for (const change of changes) {
if (change.added) {
const script = createScript(change.value, EVAL_FILENAME)
result = script.runInNewContext(context)
}
}
return result
}
示例4: Dog
var d2: Dog = new Dog();
let code = '(function (){\ntry{\n\texports = {};\n' +
fs.readFileSync('app/dog.js').toString() +
'\n\treturn exports;\n}\ncatch(ex){\n\tconsole.log("Error:", ex);\n}\n})';
let def = require('./app/dog');
console.log('imported def', def);
console.log('code: ', code);
let context = vm.createContext({
console: console,
require: require,
__dirname: __dirname,
__filename: __filename,
process: process
});
let _script = vm.createScript(code);
let fn: Function = _script.runInContext(context);
let exported: any = fn();
console.log(exported);
var testDog: any = new exported.Dog();
testDog.Name = "tester";
console.log(testDog);
//import {Dog} from "../app/dog";
let req: http.ServerRequest = require('[request]');
let res: http.ServerResponse = require('[response]');
let next: Function = require('[next]');