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

上一篇:SLB负载均衡实践


下一篇:Java配置----JDK开发环境搭建及环境变量配置