使用C++调用V8

文章来自:https://developers.google.com/v8/get_started


Hello World

下面的C++代码将调用V8的解释器,运行一段js代码。

#include <v8.h>

using namespace v8;

int main(int argc, char* argv[]) {
  // Get the default Isolate created at startup.
  Isolate* isolate = Isolate::GetCurrent();

  // Create a stack-allocated handle scope.
  HandleScope handle_scope(isolate);

  // Create a new context.
  Handle<Context> context = Context::New(isolate);

  // Here's how you could create a Persistent handle to the context, if needed.
  Persistent<Context> persistent_context(isolate, context);
  
  // Enter the created context for compiling and
  // running the hello world script. 
  Context::Scope context_scope(context);

  // Create a string containing the JavaScript source code.
  Handle<String> source = String::New("'Hello' + ', World!'");

  // Compile the source code.
  Handle<Script> script = Script::Compile(source);
  
  // Run the script to get the result.
  Handle<Value> result = script->Run();
  
  // The persistent handle needs to be eventually disposed.
  persistent_context.Dispose();

  // Convert the result to an ASCII string and print it.
  String::AsciiValue ascii(result);
  printf("%s\n", *ascii);
  return 0;
}


在V8中运行js代码,需要一个handles, handle scope和context:

  • handle 是指向js对象的指针。所有的v8对象都关联到一个handle指针,这个指针被V8的垃圾收集器使用;
  • scope可以认为是一组handle的容器。当你使用完这些handle后,你可以简单的删除这个scope,而不必删除每个handle;
  • context是一个可执行环境,可以将js代码分离的运行在不同的v8实例中。要运行js代码,必须创建一个context。

运行

1. 下载和编译V8download and build instructions.

2. 拷贝上面的代码,并存储到一个文本文件hello_world.cpp

3. 编译hello_world.cpp,如在linux上可以这样:

g++ -Iinclude hello_world.cc -o hello_world out/x64.release/obj.target/tools/gyp/libv8_{base,snapshot}.a -lpthread

4. 运行hello_world


在window运行(这部分是本人原创)

注意,我是用VS2010编译和运行的。 v8的可执行库从 http://download.csdn.net/detail/doon/6442033下载

下面是步骤:

1. 创建一个VS2010 控制台工程,注意,以空项目的方式创建;

2. 添加一个新文件: hello_world.cpp,并把上面的代码拷贝到该文件中,保存;

3. 下载v8_sdk.rar ,解压并放在一个指定位置

4. 打开工程属性页,配置C++头文件:( <path>\v8_sdk\include)


5. 配置lib库的路径(<path>\v8_sdk\lib)



6. 增加库

v8_base.ia32.lib
v8_nosnapshot.ia32.lib
v8_snapshot.lib
icui18n.lib
icuuc.lib
Ws2_32.lib
winmm.lib



7. 编译解决方案并运行。



  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在C++使用V8调用带有`require`调用的字符串函数,你需要将包含JavaScript代码的字符串作为输入传递给V8引擎,并在V8上下文中执行该代码。下面是一个示例: ```cpp #include <iostream> #include <v8.h> int main() { // 创建V8引擎实例 v8::Isolate* isolate = v8::Isolate::New(); v8::Isolate::Scope isolate_scope(isolate); v8::HandleScope handle_scope(isolate); // 创建一个上下文环境 v8::Local<v8::Context> context = v8::Context::New(isolate); v8::Context::Scope context_scope(context); // 定义JavaScript代码 const char* js_code = R"( const _ = require('lodash'); function hello() { return _.capitalize('hello world'); } hello(); )"; // 在上下文中执行JavaScript代码 v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, js_code); v8::Local<v8::Script> script = v8::Script::Compile(source); script->Run(); // 在C++中获取JavaScript函数的返回值 v8::Local<v8::Value> result = context->Global()->Get(context, v8::String::NewFromUtf8(isolate, "result")); // 将返回值转换为C++字符串并打印 v8::String::Utf8Value utf8_value(result); std::cout << "Result: " << *utf8_value << std::endl; // 释放资源 isolate->Dispose(); return 0; } ``` 上述示例中,我们定义了一个包含JavaScript代码的字符串,其中包含了对`require`函数的调用和一个名为`hello`的函数。然后,我们将该字符串作为输入传递给V8引擎,并在V8上下文中执行该代码。最后,我们获取了`hello`函数的返回值,并将其转换为C++字符串进行打印。 请注意,为了使示例代码正常工作,你需要先安装和配置V8引擎。具体安装和配置方法请参考V8引擎的文档和示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值