zoukankan      html  css  js  c++  java
  • 从V8引擎编程理解javascript执行环境

    一、V8简介 

        google code上对它的解释如下:

         V8 is Google's open source JavaScript engine.

        V8 is written in C++ and is used in Google Chrome, the open source browser from Google.

        V8 implements ECMAScript as specified in ECMA-262, 5th edition, and runs on Windows (XP or newer), Mac OS X (10.5 or newer), and Linux     systems that use IA-32, x64, or ARM processors.

        V8 can run standalone, or can be embedded into any C++ application.

        翻译过来重点就是:V8引擎是一个google开发的开源javascript引擎,它是由C++编写而成,被用在google的开源游览器chrome上。

        

        二、Hello World

        学习任何一门语言,“hello world”往往是我们的第一步,这里也不例外,代码如下:

    #include <v8.h>

    using namespace v8;

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

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

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

    // 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();

    // Dispose the persistent context.
    context.Dispose();

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

        具体的编译运行方法可以参见,如下文章:http://code.google.com/intl/zh-CN/apis/v8/get_started.html

        作为一名nodejs 的开发者,我要补充一个已经安装好node之后如何正常编译运行该代码,众所周知,nodejs是以来v8引擎的,也就是说当你成功安装好nodejs之后就已经成功安装好v8了,具体方法:

    // v8 在安装nodejs时已经安装完毕
    // 比如在自己目录(/home/zhujiadun.pt/)中下载了node-v0.6.10,默认安装
    // 具体:
    // 1. v8.h和v8stdint.h 两个头文件在~/node-v0.6.10/deps/v8/include/和/usr/local/include/node/都有一份
    // 2. libv8.a可以在~/node-v0.6.10/out/Release/libv8.a找到
    // 运行方法
    // 1. 将v8.h和v8stdint.h 拷贝到 /usr/include
    // 2. 将libv8.a拷贝到这个文件目录
    // 3. g++ hello_world.cpp -o hello_world libv8.a -lpthread
    // 4. ./hello_world

        

          三、执行环境讲解

          js作用域的基础知识参见:《javascript高级程序设计》读书笔记——作用域
          强调了这么多,终于可以进入正题,通过v8的编程来体验一把javascript执行环境相关知识。

          以上这幅图的运行原理过程图(图片直接来源google的v8介绍):

        

        讲解几个概念:

        执行环境:

     Persistent<Context> context = Context::New();
     Context::Scope context_scope(context);
     context.Dispose();

        context就是这个函数的执行环境,在javascript中执行环境是一个非常重要的概念,每个函数被在调用时都会创建自己的执行环境,由它来定义代码在哪个环境被执行。当函数执行完毕时被销毁。

        handle:

        v8引擎如jvm一样,会自动对不用的内存进行垃圾回收,handle就是告诉v8的垃圾回收器这个函数在堆中位置。

        文本只是对v8的相关知识进行初步的讲解,v8还是比较复杂的,很多内容还在学习当中,先简单分享些自己的学习心得吧。

        参考资料:

        http://code.google.com/intl/zh-CN/apis/v8/intro.html

        http://izs.me/v8-docs/main.html

  • 相关阅读:
    Python 知识要点:函数
    软件测试
    软件测试
    软件测试
    SpringBoot框架
    Redis存储系统
    Linux系统
    maven
    shiro安全框架
    Mybatis框架
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2355821.html
Copyright © 2011-2022 走看看