Google V8编程详解附录

Google V8编程详工具函数

头文件:utils.h

#ifndef UTILS_H_
#define UTILS_H_
#include "v8.h"
#include <iostream>
using namespace v8;
using namespace std;

v8::Handle<v8::String> ReadJS(const char* name);
void printValue(Handle<Value> result);
#endif



  • ReadJS
v8::Handle<v8::String> ReadJS(const char* name) {

        FILE* file = fopen(name, "rb");

        if (file == NULL) {
                return v8::Handle<v8::String>();
        }

        fseek(file, 0, SEEK_END);
        int size = ftell(file);
        rewind(file);

        char* chars = new char[size + 1];
        chars[size] = '\0';

        for (int i = 0; i < size;) {
                int read = fread(&chars[i], 1, size - i, file);
                i += read;
        }

        fclose(file);

        v8::Handle<v8::String> result = v8::String::New(chars, size);
        delete[] chars;
        return result;
}
  • printValue
void printValue(Handle<Value> result) {
	//感谢downmooner兄的提醒,这个String::Utf8Value str(result)的确让这段代码
	//南辕北辙
	//String::Utf8Value str(result);


	// just construct the "result" script
	Handle<Script> script = Script::Compile(String::New("result"));
	result = script->Run();
	cout << *String::Utf8Value(result) << endl;
}


版权申明:
转载文章请注明原文出处,任何用于商业目的,请联系本人:hyman_tan@126.com

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值