简体   繁体   English

如何运行JavaScript文件-V8

[英]How to run a JavaScript file - V8

I have embedded v8 into my c++ application. 我已经将v8嵌入到我的c ++应用程序中。 Referring https://chromium.googlesource.com/v8/v8/+/master/samples/hello-world.cc I am able to run a javascript. 引用https://chromium.googlesource.com/v8/v8/+/master/samples/hello-world.cc我可以运行JavaScript。 Tested and works fine. 经过测试,工作正常。

I access the links from my c++ application, download html data, download javascript. 我从c ++应用程序访问链接,下载html数据,下载javascript。 Some embedded scripts in the html call functions in external script files. html调用函数中的某些嵌入式脚本在外部脚本文件中。 How do I ensure that the external scripts are available for the embedded ones? 如何确保外部脚本可用于嵌入式脚本?

The downloaded JavaScript files (one or more) may be of large size. 下载的JavaScript文件(一个或多个)可能很大。 In such a context, how do I execute the JavaScript api present in HTML using v8? 在这种情况下,如何使用v8执行HTML中存在的JavaScript api? Code to run a JavaScript in v8 is below, 下面是在v8中运行JavaScript的代码,

  // Create a string containing the JavaScript source code.
  v8::Local<v8::String> source =
      v8::String::NewFromUtf8(isolate, "'Hello' + ', World!'",
                              v8::NewStringType::kNormal)
          .ToLocalChecked();
  // Compile the source code.
  v8::Local<v8::Script> script =
      v8::Script::Compile(context, source).ToLocalChecked();
  // Run the script to get the result.
  v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();

Assuming downloaded javascript is 200KB, how can I feed such a large buffer to v8::Script::Compile api. 假设下载的JavaScript为200KB,如何将如此大的缓冲区提供给v8 :: Script :: Compile api。 And when I have more than one file present, how can feed I them to v8 ? 当我有多个文件时,如何将它们输入v8?

How do I ensure that the external scripts are available for the embedded ones? 如何确保外部脚本可用于嵌入式脚本?

You load the external scripts first. 您首先加载外部脚本。

How do I execute the JavaScript API present in HTML using v8? 如何使用v8执行HTML中存在的JavaScript API?

Do you mean the DOM? 您是指DOM吗? window , document and the like? windowdocument之类的? The DOM is not part of ECMAScript, so V8 knows nothing about it; DOM不是ECMAScript的一部分,因此V8对此一无所知。 it is provided by the embedder (ie usually Chrome). 它是由嵌入程序(即通常为Chrome)提供的。 In your own embedding you need to provide all those objects yourself, using V8's API. 在自己的嵌入中,您需要使用V8的API自己提供所有这些对象。 Needless to say, this is a huge amount of work. 不用说,这是一个巨大的工作量。 If what you're after is a way to render websites, then I recommend that you use some existing component/library for that, for example the Chromium Embedded Framework, or your favorite GUI toolkit's WebView (or whatever it is called). 如果您所追求的是一种呈现网站的方式,那么我建议您为此使用一些现有的组件/库,例如Chromium Embedded Framework或您最喜欢的GUI工具包的WebView(或所谓的)。

Assuming downloaded JavaScript is 200KB, how can I feed such a large buffer to v8::Script::Compile API? 假设下载的JavaScript为200KB,如何将如此大的缓冲区提供给v8 :: Script :: Compile API?

Just like you feed a small script to V8: put it into a v8::Local<v8::String> , then call v8::Script::Compile and v8::Script::Run . 就像您向V8提供一个小脚本一样:将其放入v8::Local<v8::String> ,然后调用v8::Script::Compilev8::Script::Run

And when I have more than one file present, how can feed I them to v8 ? 当我有多个文件时,如何将它们输入v8?

Call v8::Script::Compile and v8::Script::Run repeatedly, possibly using a loop. 重复调用v8::Script::Compilev8::Script::Run ,可能使用循环。 For an example, see V8's shell sample , specifically the function RunMain . 有关示例,请参见V8的shell示例 ,尤其是函数RunMain

As I receive partial JavaScript in HTTP packets (chunks), can I pass the partial JavaScript to V8? 当我在HTTP数据包(块)中收到部分JavaScript时,可以将部分JavaScript传递给V8吗?

Yes, V8 has a script streaming interface. 是的,V8具有脚本流接口。 See the API documentation for v8::ScriptCompiler::ExternalSourceStream . 请参阅API文档中的v8::ScriptCompiler::ExternalSourceStream For examples on how to use it, you can study the tests . 有关如何使用它的示例,您可以研究测试 Streaming may or may not be worth it for scripts as small as 200KB; 对于200KB的脚本来说,流传输是否值得? it is definitely not required. 绝对不是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM