當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript vm.createScript函數代碼示例

本文整理匯總了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);
  }
}
開發者ID:benoitf,項目名稱:testfile,代碼行數:27,代碼來源:che.ts

示例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);
                    }
                });
開發者ID:errisy,項目名稱:ng2ts,代碼行數:34,代碼來源:cgi.ts

示例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
}
開發者ID:cartant,項目名稱:ts-node,代碼行數:47,代碼來源:_bin.ts

示例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]');
開發者ID:errisy,項目名稱:NgNode,代碼行數:30,代碼來源:dog.ts


注:本文中的vm.createScript函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。