Google V8 编程入门(二) - 使用c++访问js脚本对象
2013-03-18 14:28
531 查看
// v8test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <v8.h> #pragma comment(lib, "v8_base.lib") #pragma comment(lib, "v8_snapshot.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. Handle<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("" "function MyObj() " "{" "this.myArray = [1,2,2,2,2,2]; " "this.myDouble = Math.PI; " "}" "MyObj.prototype.myFunction = function(arg1,arg2)" "{" "return (this.myDouble + arg1 + arg2); " "};" "var globalObject = new MyObj();" ); String * str = *source; // Compile the source code. Handle<Script> script = Script::Compile(source); // Run the script to get the result. Handle<Value> result = script->Run(); // Convert the result to an ASCII string and print it. String::AsciiValue ascii(result); printf("%s\n", *ascii); // 通过变量名获取对象,全局变量隶属于Global()对象 Local<Object> globalObject = Local<Object>::Cast(context->Global()->Get(String::New("globalObject"))); // 获取globalObject对象的myArray属性 Handle<Array> arrayproperty = Handle<Array>::Cast(globalObject->Get(String::New("myArray"))); String::AsciiValue ascii2(arrayproperty); printf("%s\n", *ascii2); // 获取globalObject对象的myDouble属性 Handle<Object> doubleproperty = Handle<Object>::Cast(globalObject->Get(String::New("myDouble"))); String::AsciiValue ascii3(doubleproperty); printf("%s\n", *ascii3); // 获取globalObject对象的myFunction属性 Local<Function> func = Local<Function>::Cast(globalObject->Get(String::New("myFunction"))); Local<Value> argv2 [2] = {v8::Number::New(1.123123), v8::Number::New(2.234234)}; // 调用myFunction函数对象 String::AsciiValue ascii4(func->Call(globalObject, 2, argv2)); printf("%s\n", *ascii4); // Dispose the persistent context. (Persistent<Context>(context)).Dispose(); return 0; }
运行结果:
function (arg1,arg2){return (this.myDouble + arg1 + arg2); } 1,2,2,2,2,2 3.141592653589793 6.498949653589793 请按任意键继续. . .
相关文章推荐
- Google V8 编程入门(三) - 使用js访问c++宿主对象
- Google V8 编程入门(一) - V8对象概览
- Google C++测试框架系列入门篇:第一章 介绍:为什么使用GTest?
- C#数据库编程入门 使用command参数对象
- 在C++中嵌入JavaScript——Google V8 JavaScript Engine使用
- 如何:通过使用 Visual C# 2010 功能访问 Office 互操作对象(C# 编程指南)
- 【PHP面向对象(OOP)编程入门教程】6.如何去使用对象中的成员
- 在C++中嵌入JavaScript——Google V8 JavaScript Engine使用体验
- 使用 acl 库针对 C++ 对象进行序列化及反序列编程
- c++ 多线程编程入门 信号量使用
- Google V8编程详解(五)JS调用C++
- MFC开发IM-第二十四篇、使用 acl 库针对 C++ 对象进行序列化及反序列编程
- 在C++中嵌入JavaScript——Google V8 JavaScript Engine使用体验
- 【PHP面向对象(OOP)编程入门教程】如何去使用对象中的成员
- 在C++中嵌入JavaScript——Google V8 JavaScript Engine使用体验
- 使用C++访问Google API
- 在C++中嵌入JavaScript——Google V8 JavaScript Engine使用体验
- 使用c/c++进行socket编程基础入门
- c++中使用基类指针Vector 存放子类对象(0xcccccccc 处未处理的异常: 0xC0000005: 读取位置 0xcccccccc 时发生访问冲突 )
- ZZ:使用.NET语言开发Silverlight应用程序入门(二):界面和对象模型