Changeset 19396

Show
Ignore:
Timestamp:
09/17/08 00:01:21 (5 years ago)
Author:
tokuhirom
Message:

restructured

Location:
lang/cplusplus/llv8call/trunk
Files:
2 added
7 modified

Legend:

Unmodified
Added
Removed
  • lang/cplusplus/llv8call/trunk/Sconstruct

    r19395 r19396  
    99  "dir.cc", 
    1010  "dll.cc", 
     11  "lib.cc", 
    1112] 
    1213include = ['../v8/include/', '/opt/local/include/'] 
  • lang/cplusplus/llv8call/trunk/src/core.cc

    r19351 r19396  
    4545        v8::HandleScope handle_scope; 
    4646        v8::String::AsciiValue file(args[i]); 
    47         v8::Handle<v8::String> source = ReadFile(*file); 
     47        v8::Handle<v8::String> source = LLV8::ReadFile(*file); 
    4848        if (source.IsEmpty()) { 
    4949            return v8::ThrowException(v8::String::New("Error loading file")); 
    5050        } 
    51         ExecuteString(source, v8::String::New(*file), false); 
     51        LLV8::Exec(source, v8::String::New(*file), false); 
    5252    } 
    5353    return v8::Undefined(); 
  • lang/cplusplus/llv8call/trunk/src/dir.cc

    r19379 r19396  
    77#include <nspr/prio.h> 
    88#include <nspr/prerror.h> 
     9#include "llv8-util.h" 
    910 
    1011using namespace v8; 
  • lang/cplusplus/llv8call/trunk/src/dll.cc

    r19387 r19396  
    22#include "llv8call.h" 
    33#include <nspr/nspr.h> 
     4#include "llv8-util.h" 
    45 
    56using namespace v8; 
  • lang/cplusplus/llv8call/trunk/src/file.cc

    r19381 r19396  
    66#include <nspr/prio.h> 
    77#include "llv8call.h" 
     8#include "llv8-util.h" 
    89 
    910using namespace v8; 
  • lang/cplusplus/llv8call/trunk/src/llv8call.h

    r19385 r19396  
    11#include <v8.h> 
    2 #include <string> 
    3 #include <cassert> 
    4 #include <nspr/nspr.h> 
    5 #include <iostream> 
    6 #include <sstream> 
    72 
    83#ifdef _WIN32 
     
    127#endif 
    138 
    14 void setup_syscall(v8::Handle<v8::ObjectTemplate> global); 
    15 void setup_core(v8::Handle<v8::ObjectTemplate> global); 
    16 void setup_io(v8::Handle<v8::ObjectTemplate> global); 
    17 void setup_curl(v8::Handle<v8::ObjectTemplate> global); 
    18 void setup_sqlite3(v8::Handle<v8::ObjectTemplate> global); 
    19 void setup_dll(v8::Handle<v8::ObjectTemplate> global); 
    20 void setup_dir(v8::Handle<v8::ObjectTemplate> global); 
    21  
    22 template <class T> 
    23 inline T * handle(const v8::Arguments & args, int index) { 
    24     v8::Local<v8::Value> field = args.This()->GetInternalField(index); 
    25     assert(field->IsExternal()); 
    26     T* ret = reinterpret_cast<T *>(v8::Handle<v8::External>::Cast(field)->Value()); 
    27     assert(ret); 
    28     return ret; 
     9// libllv8call apis 
     10namespace LLV8 { 
     11    const char * Version(); 
     12    void Init(v8::Handle<v8::ObjectTemplate> global); 
     13    bool Exec(v8::Handle<v8::String> source, 
     14                v8::Handle<v8::Value> name, 
     15                bool print_result); 
     16    v8::Handle<v8::String> ReadFile(const char* name); 
    2917} 
    3018 
    31 inline v8::Handle<v8::Value> throw_nspr() { 
    32     v8::HandleScope handle_scope; 
    33     int len = PR_GetErrorTextLength(); 
    34     if (len > 0) { 
    35         char * buf = new char[len]; 
    36         assert(PR_GetErrorText(buf) > 0); 
    37         v8::Handle<v8::String> strbuf = v8::String::New(buf); 
    38         return v8::ThrowException(strbuf); 
    39     } else { 
    40         std::ostringstream e; 
    41         e << "Unknown ERROR: " << PR_GetError(); 
    42         return v8::ThrowException(v8::String::New(e.str().c_str())); 
    43     } 
    44 } 
    45  
    46  
    47 #define assert_args(args, length) do { if ((args).Length() != (length)) { return v8::ThrowException(v8::String::New("Exception: missing args")); } } while (0) 
    48  
    49 // utils 
    50 bool ExecuteString(v8::Handle<v8::String> source, 
    51                    v8::Handle<v8::Value> name, 
    52                    bool print_result); 
    53 v8::Handle<v8::String> ReadFile(const char* name); 
    54  
  • lang/cplusplus/llv8call/trunk/src/main.cc

    r19395 r19396  
    88#endif 
    99 
    10 const char * LLV8CALL_VERSION = "0.01"; 
    11  
    1210extern int     __argc; 
    1311extern char ** __argv; 
    1412 
    15 // Executes a string within the current v8 context. 
    16 bool ExecuteString(v8::Handle<v8::String> source, 
    17                    v8::Handle<v8::Value> name, 
    18                    bool print_result) { 
    19     v8::HandleScope handle_scope; 
    20     v8::TryCatch try_catch; 
    21     v8::Handle<v8::Script> script = v8::Script::Compile(source, name); 
    22     if (script.IsEmpty()) { 
    23         // Print errors that happened during compilation. 
    24         v8::String::AsciiValue error(try_catch.Exception()); 
    25         printf("%s\n", *error); 
    26         return false; 
    27     } else { 
    28         v8::Handle<v8::Value> result = script->Run(); 
    29         if (result.IsEmpty()) { 
    30         // Print errors that happened during execution. 
    31         v8::String::AsciiValue error(try_catch.Exception()); 
    32         printf("%s\n", *error); 
    33         return false; 
    34         } else { 
    35         if (print_result && !result->IsUndefined()) { 
    36             // If all went well and the result wasn't undefined then print 
    37             // the returned value. 
    38             v8::String::AsciiValue str(result); 
    39             printf("%s\n", *str); 
    40         } 
    41         return true; 
    42         } 
    43     } 
    44 } 
    45  
    46 v8::Handle<v8::String> ReadFile(const char* name) { 
    47     FILE* file = fopen(name, "rb"); 
    48     if (file == NULL) return v8::Handle<v8::String>(); 
    49  
    50     fseek(file, 0, SEEK_END); 
    51     int size = ftell(file); 
    52     rewind(file); 
    53  
    54     char* chars = new char[size + 1]; 
    55     char *buffer; 
    56     chars[size] = '\0'; 
    57     for (int i = 0; i < size;) { 
    58         int read = fread(&chars[i], 1, size - i, file); 
    59         i += read; 
    60     } 
    61     fclose(file); 
    62     if (size>2 && chars[0] == '#' && chars[1] == '!') { 
    63             // shebang hack 
    64             char *end = strchr(chars, '\n'); 
    65             if (end && end-chars > 0) { 
    66                 size -= (end-chars) + 1; 
    67                 buffer = end+1; 
    68             } 
    69     } else { 
    70         buffer = chars; 
    71     } 
    72     v8::Handle<v8::String> result = v8::String::New(buffer, size); 
    73     delete[] chars; 
    74     return result; 
    75 } 
    76  
    7713void run_shell_mode() { 
    78     printf("V8 version %s, llv8call version %s\n", v8::V8::GetVersion(), LLV8CALL_VERSION); 
     14    printf("V8 version %s, llv8call version %s\n", v8::V8::GetVersion(), LLV8::Version()); 
    7915#ifdef HAVE_READLINE 
    8016    { 
     
    8420        while (line = readline("llv8call> ")) { 
    8521            v8::HandleScope handle_scope; 
    86             ExecuteString(v8::String::New(line), v8::Undefined(), true); 
     22            LLV8::Exec(v8::String::New(line), v8::Undefined(), true); 
    8723            add_history(line); 
    8824            if (++history_count > MAX_HISTORY) { 
     
    11753    // bind functions 
    11854    v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); 
    119     setup_syscall(global); 
    120     setup_core(global); 
    121     setup_io(global); 
    122     setup_dll(global); 
    123     setup_dir(global); 
     55    LLV8::Init(global); 
    12456 
    12557    v8::Handle<v8::Context> context = v8::Context::New(NULL, global); 
     
    12961        const char *srcfile = argv[1]; 
    13062        v8::Handle<v8::String> file_name = v8::String::New(srcfile); 
    131         v8::Handle<v8::String> source = ReadFile(srcfile); 
     63        v8::Handle<v8::String> source = LLV8::ReadFile(srcfile); 
    13264        if (source.IsEmpty()) { 
    13365            printf("Error reading '%s'\n", srcfile); 
    13466            return 1; 
    13567        } 
    136         if (!ExecuteString(source, file_name, false)) { 
     68        if (!LLV8::Exec(source, file_name, false)) { 
    13769            return 1; 
    13870        }