Getting Started

时间:2023-03-08 23:59:24
Getting Started

https://developers.google.com/v8/get_started

Getting Started

This document introduces some key V8 concepts and provides a hello world example to get you started with V8 code.

Contents

Audience

This document is intended for C++ programmers who want to embed the V8 JavaScript engine within a C++ application.

Back to top

Hello World

Let's look at a Hello World example that takes a JavaScript statement as a string argument, executes it as JavaScript code, and prints the result to standard out.

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

  // Create a string containing the JavaScript source code.
  String source =String::New("'Hello' + ', World'");   // Compile the source code.
  Script script =Script::Compile(source);
 
  // Run the script to get the result.
  Value result = script->Run();   // Convert the result to an ASCII string and print it.
  String::AsciiValue ascii(result);
  printf("%s\n",*ascii);
  return0;
}

To actually run this example in V8, you also need to add handles, a handle scope, and a context:

  • handle is a pointer to an object. All V8 objects are accessed using handles, they are necessary because of the way the V8 garbage collector works.
  • scope can be thought of as a container for any number of handles. When you've finished with your handles, instead of deleting each one individually you can simply delete their scope.
  • context is an execution environment that allows separate, unrelated, JavaScript code to run in a single instance of V8. You must explicitly specify the context in which you want any JavaScript code to be run.

These concepts are discussed in greater detail in the Embedder's Guide.

The following example is the same as the one above, except now it includes handles, a context, and a scope - it also includes a namespace and the v8 header file:

#include<v8.h>

usingnamespace 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);
  return0;
}

Back to top

Run the Example

Follow the steps below to run the example yourself:

  1. Download the V8 source code and build V8 by following the download and build instructions.
  2. Copy the complete code from the previous section (the second code snippet), paste it into your favorite text editor, and save as hello_world.cpp in the V8 directory that was created during your V8 build.
  3. Compile hello_world.cpp, linking to the static libraries created in the build process. For example, on 64bit Linux using the GNU compiler:
    g++ -Iinclude hello_world.cc -o hello_world out/x64.release/obj.target/tools/gyp/libv8_{base,snapshot}.a -lpthread
  4. Run the hello_world executable file at the command line.
    For example, on Linux, still in the V8 directory, type the following at the command line:
    ./hello_world
  5. You will see Hello, World!.

Of course this is a very simple example and it's likely you'll want to do more than just execute scripts as strings! For more information see the Embedder's Guide.

Back to top

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License.

Last updated June 19, 2013.

随机推荐

  1. WCF部署于IIS使用的几个问题总结

    Q:wcf 远程服务器返回错误: (405) 不允许的方法. A: new SqlServerOperateClient("BasicHttpBinding_ISqlServerOperat ...

  2. git学习1:git安装和配置

    Git是什么?世界上最先进的分布式版本控制系统,记录了一个文本文件的每次一修改信息,比如,一篇散文,从草稿到最终出版,经历过无数次修改,修改了标点符号形成一个版本,老师帮助修改形成一个版本,同学帮忙修 ...

  3. ylbtech-Miscellaneos

    ylbtech-Miscellaneos: A,返回顶部 1, 2, B返回顶部 1, 2 作者:ylbtech出处:http://ylbtech.cnblogs.com/本文版权归作者和博客园共有, ...

  4. git bash下对文件的操作

    window下的e盘中webpack文件夹操作 新建文件夹:mkdir wpdemo: 新建文件:touch index.html; 删除文件夹:rm -r wpdemo; 删除文件:rm index ...

  5. Django 之 models的 F() 和 Q() 函数

    前提: app名称为core,models.py 如下: #coding: utf8 import datetime from django.db import models class Order( ...

  6. mvc4 发布,遇到 403.14 问题,并且iis提示打开目录浏览。。。解决办法

    经测试,只需要在web.config的system.web的同级节点,添加如下代码,即可完美运行.原理参考文章:一.<validation validateIntegratedModeConfi ...

  7. 问题解决_(转载)VS2015无法启动 IIS Express Web解决办法

    将“重写应用程序根URL”的勾选去掉,然后就可正常运行 参考资料:http://www.qi88.com/edu/os/2015/0103/87648.html

  8. MyEclipse10建立Maven Webapp项目并通过git传到GitHub

    先创建Maven Webapp项目 图文详解MyEclipse中新建Maven webapp项目的步骤(很详细) 在web项目的路径中右键(前提是你机器已经装了git)“Git Init Here”, ...

  9. Windows消息过滤

    在C#编程中,经常会遇到一些场景,如禁止鼠标拖动窗体,启用某些快捷键,禁止鼠标移动等.遇到这些需求,可以通过窗体的MouseMove事件,OnDragDrop,OnMove等事件来解决问题, 但是该方 ...

  10. Url路径重写的原理

    ASP.net的地址重写(URLRewriter)实现原理及代码示例 吴剑 2007-01-01 原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian/ 概述 访问 ...