我想要使用独立的V8 JavaScript引擎,例如,我将按照这里说明在命令行中运行它:
$> ./v8-shell -e 'print("10*10 = " + 10*10)'
我希望 JavaScript 可以执行一些 HTTP 请求,最好使用 jQuery 的 API,但也可以使用 XMLHttpRequest。
V8 中是否有内置方法来实现这一点?如果没有,是否有任何方法可以在不实现访问器 / C++ 扩展的情况下实现它?
我想要使用独立的V8 JavaScript引擎,例如,我将按照这里说明在命令行中运行它:
$> ./v8-shell -e 'print("10*10 = " + 10*10)'
var options = {
host: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST'
};
var req = http.request(options, function(res) {
// callback invoked when response is received
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
// 'data' event is fired whenever a chunk of the response arrives
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
V8 只是一个 JavaScript 引擎,它没有像 alert 这样的浏览器宿主方法或像 XMLHttpRequest 这样的宿主对象。
vm.createContext()
方法时,基本上是直接绑定到V8功能,这里是全局上下文的内容: Errors: [Error, EvalError, RangeError, ReferenceError,
SyntaxError, TypeError, URIError],
Types: [Array, Boolean, Date, Function, Map, Number,
Object, Proxy, RegExp, Set, String, WeakMap], //--harmony: [Map, Proxy, Set, WeakMap]
Primitives: [Infinity, NaN, undefined],
Dicts: [Math, JSON],
Methods: [decodeURI, decodeURIComponent, encodeURI, encodeURIComponent,
escape, eval, isFinite, isNaN, parseFloat, parseInt, unescape]
它甚至没有set/clearTimeout、set/clearInternal(不是本地JavaScript函数)。作为一种语言,JavaScript比大多数人意识到的更加紧密地聚焦于主题。它始终存在于一个宿主环境中,该环境在其上添加了更多的东西。
curl
或wget
这样的系统调用? - hugomg