本文整理匯總了TypeScript中vm.runInThisContext函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript runInThisContext函數的具體用法?TypeScript runInThisContext怎麽用?TypeScript runInThisContext使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了runInThisContext函數的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: load
function load(Class:String) {
var fs = require("fs"), vm = require("vm");
if(Class.substr(Class.length-3) != ".js") Class += ".js";
var script = fs.readFileSync(Class, "utf-8");
var node = node;
vm.runInThisContext(script, Class);
};
示例2: callback
const evalCommand: repl.REPLEval = (
cmd: string,
_context: any,
_filename: string,
callback: (e: Error | null, result?: any) => void,
) => {
let result;
try {
if (transformer) {
const transformResult = transformer.process(
cmd,
jestGlobalConfig.replname || 'jest.js',
jestProjectConfig,
);
cmd =
typeof transformResult === 'string'
? transformResult
: transformResult.code;
}
result = vm.runInThisContext(cmd);
} catch (e) {
return callback(isRecoverableError(e) ? new repl.Recoverable(e) : e);
}
return callback(null, result);
};
示例3: loadScript
loadScript: function loadScript(filename: string) {
let fullPath = path.join(visPath, filename);
let data = fs.readFileSync(fullPath);
// TODO: try to enable this again once https://github.com/electron/electron/pull/7909 is merged
/* if (vm.isContext(sandbox)) {
vm.runInContext(data, sandbox, { filename });
} else {*/
vm.runInThisContext(data, {filename});
// }
}
示例4: function
const runContentScript = function (this: any, extensionId: string, url: string, code: string) {
const context: { chrome?: any } = {}
require('@electron/internal/renderer/chrome-api').injectTo(extensionId, false, context)
const wrapper = `((chrome) => {\n ${code}\n })`
const compiledWrapper = runInThisContext(wrapper, {
filename: url,
lineOffset: 1,
displayErrors: true
})
return compiledWrapper.call(this, context.chrome)
}
示例5:
const getExportsFromCodeString = (codeString: string): Object => {
const module = {
exports: {},
};
const code = `(function(require, module, exports) {
${codeString}
})`;
vm.runInThisContext(code, {
filename: `${process.cwd()}/fixtures/index.js`,
})(require, module, module.exports);
return module.exports;
};
開發者ID:morlay,項目名稱:babel-plugin-webpack-loaders-inline-exports,代碼行數:15,代碼來源:getExportsFromCodeString.ts
示例6:
getTsBinPathWithLoad = (): string => {
var typeScriptBinPath = _path.dirname(require.resolve("typescript")),
typeScriptPath = _path.resolve(typeScriptBinPath, "typescript.js"),
code: string;
if (!typeScriptBinPath) {
grunt.fail.warn("typescript.js not found. please 'npm install typescript'.");
return "";
}
code = grunt.file.read(typeScriptPath);
_vm.runInThisContext(code, typeScriptPath);
return typeScriptBinPath;
},
示例7: function
loadTypeScript = function(){
var typeScriptBinPath = _path.dirname(require.resolve("typescript")), //resolveTypeScriptBinPath(currentPath, 0),
typeScriptPath = _path.resolve(typeScriptBinPath, "typescript.js"),
code;
if (!typeScriptBinPath) {
grunt.fail.warn("typescript.js not found. please 'npm install typescript'.");
return false;
}
code = grunt.file.read(typeScriptPath);
_vm.runInThisContext(code, typeScriptPath);
return typeScriptBinPath;
};
示例8: getAPI
const runContentScript = (url: string, code: string, manifest: Manifest) => {
const context = getAPI(manifest);
const wrapper = `((wexond) => {
var chrome = wexond;
var browser = wexond;
${code}
});`;
const compiledWrapper = runInThisContext(wrapper, {
filename: url,
lineOffset: 1,
displayErrors: true,
});
return compiledWrapper.call(window, context);
};
示例9: internalEval
export function internalEval (source: string, include, isGlobalCtx: boolean = false) {
let module = currentModule;
global.include = include;
global.require = global.require;
global.exports = module.exports;
global.__filename = path_getFile(include.url);
global.__dirname = path_getDir(global.__filename);
global.module = module;
if (isGlobalCtx !== true) {
source = '(function(){ ' + source + '\n}())';
}
try {
if (!isGlobalCtx) {
let filename = global.__filename
module = currentModule = new Module(filename, module);
module.paths = (Module as any)._nodeModulePaths(path_getDir(filename));
module.filename = filename;
(module as any)._compile(source, filename);
module.loaded = true;
}
else {
module.exports = {};
vm.runInThisContext(source, global.__filename);
}
} catch(e) {
console.error('Module Evaluation Error', include.url);
console.error(e.stack);
}
if (isEmpty(include.exports)) {
var exports = module.exports;
if (typeof exports !== 'object' || isEmpty(exports) === false) {
include.exports = module.exports;
}
}
};