第一个v8程序

网上搜了半天,总算能够写出来了第一个v8的程序,主要是参考官网上的例子:http://code.google.com/intl/zh-CN/apis/v8/get_started.html,外加上官网上的在windows下build的说明:http://code.google.com/p/v8/wiki/BuildingOnWindows

开发环境:windows + vs2008
代码如下:
------------------------------------------------------------------------------------
#include "stdafx.h"
#include "v8.h"
#pragma comment(lib,"WSock32.lib")
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"winmm.lib")
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;
}
------------------------------------------------------------------------------------

但是需要修改include目录和lib目录。参考上一篇文章:使用v8写程序
posted @ 2009-12-25 10:29  dc0453  阅读(171)  评论(0)    收藏  举报