Google V8編程詳解附錄

Google V8編程詳工具函數javascript

頭文件:utils.hjava

 

[cpp]  view plain copy print ?
 
  1. #ifndef UTILS_H_  
  2. #define UTILS_H_  
  3. #include "v8.h"  
  4. #include <iostream>  
  5. using namespace v8;  
  6. using namespace std;  
  7.   
  8. v8::Handle<v8::String> ReadJS(const char* name);  
  9. void printValue(Handle<Value> result);  
  10. #endif  



 

 

  • ReadJS
[cpp]  view plain copy print ?
 
  1. v8::Handle<v8::String> ReadJS(const char* name) {  
  2.   
  3.         FILE* file = fopen(name, "rb");  
  4.   
  5.         if (file == NULL) {  
  6.                 return v8::Handle<v8::String>();  
  7.         }  
  8.   
  9.         fseek(file, 0, SEEK_END);  
  10.         int size = ftell(file);  
  11.         rewind(file);  
  12.   
  13.         char* chars = new char[size + 1];  
  14.         chars[size] = '\0';  
  15.   
  16.         for (int i = 0; i < size;) {  
  17.                 int read = fread(&chars[i], 1, size - i, file);  
  18.                 i += read;  
  19.         }  
  20.   
  21.         fclose(file);  
  22.   
  23.         v8::Handle<v8::String> result = v8::String::New(chars, size);  
  24.         delete[] chars;  
  25.         return result;  
  26. }  
  • printValue
[javascript]  view plain copy print ?
 
  1. void printValue(Handle<Value> result) {  
  2.     //感謝downmooner兄的提醒,這個String::Utf8Value str(result)的確讓這段代碼  
  3.     //南轅北轍  
  4.     //String::Utf8Value str(result);  
  5.   
  6.   
  7.     // just construct the "result" script  
  8.     Handle<Script> script = Script::Compile(String::New("result"));  
  9.     result = script->Run();  
  10.     cout << *String::Utf8Value(result) << endl;  
  11. }  



版權申明:
轉載文章請註明原文出處,任何用於商業目的,請聯繫本人:hyman_tan@126.comios

相關文章
相關標籤/搜索