Google V8編程詳工具函數javascript
頭文件:utils.hjava
- #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
- 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;
- }
- void printValue(Handle<Value> result) {
-
-
-
-
-
-
- Handle<Script> script = Script::Compile(String::New("result"));
- result = script->Run();
- cout << *String::Utf8Value(result) << endl;
- }
版權申明:
轉載文章請註明原文出處,任何用於商業目的,請聯繫本人:hyman_tan@126.comios