• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript vm.createScript函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript vm.runInContext函数代码示例发布时间:2022-05-25
下一篇:
TypeScript vm.createContext函数代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap