閱讀336 返回首頁    go 阿裏雲 go 技術社區[雲棲]


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

最後更新:2017-04-02 00:06:51

  上一篇:go android 單選按鈕 RadioButton 自定義圖片左邊距
  下一篇:go 醫療網絡營銷之如何提升對話轉化率