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

转载于:https://www.cnblogs.com/MingZznet/p/3231332.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MCAL是Microcontroller Abstraction Layer的缩写,是一种用于微控制器开发的软件层。MCAL Getting Started Tutorial介绍了如何在MCAL平台上开始进行开发。 首先,在开始MCAL开发之前,我们需要了解MCAL的基本概念和特性。MCAL提供了一套抽象的API(应用程序编程接口),用于对底层硬件进行访问和控制。它的目的是将底层硬件和上层应用程序解耦,使得应用程序可以更方便地进行开发和移植。 接下来,我们需要安装所需的开发工具和环境。根据MCAL的厂商和平台的不同,安装过程也会有所差异。通常,我们需要安装MCAL的开发库、编译器、调试工具等。 安装完成后,我们可以开始编写第一个MCAL应用程序。在MCAL Getting Started Tutorial中,通常会提供一些示例代码和演示用例,以帮助我们理解MCAL的使用方法。我们可以按照教程的步骤来进行代码编写、编译和调试。 在编写MCAL应用程序时,我们需要了解MCAL提供的API和功能库。这些API和功能库包括了对GPIO、UART、SPI、I2C等外设的访问和配置接口,以及中断、时钟管理、内存管理等系统级功能。 最后,在编写和调试完成应用程序之后,我们可以将其下载到目标硬件上进行运行。为了确保应用程序的稳定性和性能,我们还可以进行性能分析和优化。 总的来说,MCAL Getting Started Tutorial为我们提供了一个学习和入门MCAL开发的入口。通过这个教程,我们可以掌握MCAL的基本概念、使用方法和开发流程,从而在微控制器开发中更高效地使用MCAL。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值