当前位置: 代码迷 >> 综合 >> 最新版v8运行javascript示例代码
  详细解决方案

最新版v8运行javascript示例代码

热度:21   发布时间:2024-02-08 12:42:45.0

这里提供最新版v8(v8.6.228)运行javascript简单的示例代码,仅供参考。在openSUSE编译通过。

 g++ demo.cpp -o demo -O2 -fPIC -fPIE -lv8 -pthread
./demo
My name is John. My favourite food is fish.

/****demo.cpp***/

#include <v8.h>
#include <libplatform/libplatform.h>
//using namespace v8;
int rundemoJS (v8::Local <v8::Context> context, v8::Local <v8::Function> process_, const std::string name, const std::string food);
int runMain (int argc, char * * argv);

int main (int argc, char * * argv)
{
return runMain(argc,argv);
}
int runMain (int argc, char * * argv)
{

    // Initialize V8.
v8::V8::InitializeICUDefaultLocation(argv[0]);
v8::V8::InitializeExternalStartupData(argv[0]);
auto platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();

    // Create a new Isolate and make it the current one.
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator =
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
v8::Isolate* isolate = v8::Isolate::New(create_params);
{
v8::Isolate::Scope isolate_scope(isolate);

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

        // Create a new context.
auto context = v8::Context::New(isolate);

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

        {
// Create a string containing the JavaScript source code.
auto source = v8::String::NewFromUtf8Literal(isolate,
R"(function demoJS(name,food){return "My name is " + name + ". My favourite food is " + food + ".";})"
);

            // Compile the source code.
auto script =
v8::Script::Compile(context, source).ToLocalChecked();

            script->Run(context).ToLocalChecked();

            // The script compiled and ran correctly.  Now we fetch out the
// Process function from the global object.
v8::Local<v8::String> process_name =
v8::String::NewFromUtf8Literal(isolate, "demoJS");
v8::Local<v8::Value> process_val;
// If there is no Process function, or if it is not a function,
// bail out
if (!context->Global()->Get(context, process_name).ToLocal(&process_val) ||
!process_val->IsFunction()) {
return -1;
}

            // It is a function; cast it to a Function
v8::Local<v8::Function> process_ = v8::Local<v8::Function>::Cast(process_val);

            rundemoJS(context,process_,"John","fish");

        }

    }

    // Dispose the isolate and tear down V8.
isolate->Dispose();
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete create_params.array_buffer_allocator;
return 0;
}

int rundemoJS (v8::Local <v8::Context> context, v8::Local <v8::Function> process_, const std::string name, const std::string food)
{
v8::Context::Scope context_scope(context);
v8::Isolate* isolate=context->GetIsolate();
v8::HandleScope handle_scope(isolate);


v8::Local<v8::String> p0 =
v8::String::NewFromUtf8(isolate, name.c_str()).ToLocalChecked();
v8::Local<v8::String> p1 =
v8::String::NewFromUtf8(isolate, food.c_str()).ToLocalChecked();

    const int argc = 2;
v8::Local<v8::Value> argv[argc] = {p0,p1};
v8::Local<v8::Function> process =
v8::Local<v8::Function>::New(isolate, process_);
v8::Local<v8::Value> result;
if (!process->Call(context, context->Global(), argc, argv).ToLocal(&result)) {
return -1;
}

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

/*****end demo.cpp***/

  相关解决方案