V8 - C ++绑定:如何从c ++函数返回v8编译脚本并在其他函数中运行它

时间:2015-07-06 07:32:56

标签: c++ v8

我目前正在研究ubuntu 12.04。我在c ++中有简单的hello world程序,v8绑定如下:

#include "include/v8.h"
#include "include/libplatform/libplatform.h"

using namespace v8;

int main(int argc, char* argv[]) {

  // Initialize V8.
  V8::InitializeICU();
  Platform* platform = platform::CreateDefaultPlatform();
  V8::InitializePlatform(platform);
  V8::Initialize();

  // Create a new Isolate and make it the current one.
  //Isolate* isolate = Isolate::GetCurrent();
  Isolate* isolate = Isolate::New();
  {
    Isolate::Scope isolate_scope(isolate);

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

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

    // Enter the context for compiling and running the hello world script.
    Context::Scope context_scope(context);

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

    // Compile the source code.
    Local<Script> script = Script::Compile(source); 

    // Run the script to get the result.
    Local<Value> result = script->Run();

    // Convert the result to an UTF8 string and print it.
    String::Utf8Value utf8(result);
    printf("%s\n", *utf8);  

  }

  // Dispose the isolate and tear down V8.
  isolate->Dispose();  

  V8::Dispose();
  V8::ShutdownPlatform();
  delete platform;
  return 0;
}

我只想要

compilation( Local<Script> script = Script::Compile(source);)

execution(Local<Value> result = script->Run();) 

在不同的功能。可能吗?如果有,怎么样?任何人都可以帮忙

0 个答案:

没有答案