V8 javascript 引擎中的 http 函数

发布于 2024-12-18 16:07:21 字数 371 浏览 6 评论 0原文

我想独立使用 V8 javascript 引擎,例如,我将按照此处的说明在命令行中运行它< /a>:

$> ./v8-shell -e 'print("10*10 = " + 10*10)'

我希望 javascript 执行一些 http 请求,最好使用 jQuery API,但 XMLHttpRequest 也可以。

V8中有没有内置方法可以做到这一点?如果没有,有什么方法可以在不实现访问器/cpp 扩展的情况下实现它?

I want to use the V8 javascript engine standalone, e.g. I will run it in command line as explained here:

gt; ./v8-shell -e 'print("10*10 = " + 10*10)'

I want the javascript to perform some http requests, preferably using jQuery APIs but XMLHttpRequest is also ok.

Is there any built in method in V8 to do this? If not is there any way to achieve it without implementing accessors/cpp extensions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨小沫ゞ 2024-12-25 16:07:21

V8 中有内置方法可以做到这一点吗?

不是直接在 V8 中,但是有 NodeJS 添加了网络和文件系统功能,以及其他功能

从文档中窃取一个示例:

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();

Is there any built in method in V8 to do this?

Not in V8 directly, but there is NodeJS that adds network and file system functionality, among other features.

To steal an example from the documentation:

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();
拧巴小姐 2024-12-25 16:07:21

V8 只是一个 JavaScript 引擎,它没有像 Alert 这样的浏览器宿主方法或像 XMLHttpRequest 这样的宿主对象。

V8 is just a javascript engine, it doesn't have browser host methods like alert or host objects like XMLHttpRequest.

我的鱼塘能养鲲 2024-12-25 16:07:21

这是一个很好的例子:当您使用 Node 的 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 作为一种语言,其关注点比大多数人意识到的要紧密得多。它始终存在于在其上添加更多内容的主机环境中。

Here's a good example: when you use Node's vm.createContext() method, basically a direct binding to V8 functionality, here's what that global context has:

  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]

It doesn't even have set/clearTimeout, set/clearInternal (not native javascript functions). JavaScript as a language is much more tightly focused than most realize. It always exists in a host environment that adds more stuff on top.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文