Changeset 18741

Show
Ignore:
Timestamp:
09/03/08 19:44:16 (5 years ago)
Author:
tokuhirom
Message:

refactoring

Location:
lang/cplusplus/fcgi-v8/trunk
Files:
3 added
1 removed
2 modified

Legend:

Unmodified
Added
Removed
  • lang/cplusplus/fcgi-v8/trunk/fcgi-v8.cc

    r18731 r18741  
    2929#include <cstring> 
    3030#include <cstdlib> 
    31 #include "syscall.h" 
    32 #if 1 
     31#include "v8-util.h" 
     32#if 0 
    3333# include <fcgi_stdio.h> 
    3434#else 
     
    4040                   v8::Handle<v8::Value> name, 
    4141                   bool print_result); 
    42 v8::Handle<v8::Value> Print(const v8::Arguments& args); 
    43 v8::Handle<v8::Value> Load(const v8::Arguments& args); 
    44 v8::Handle<v8::Value> Quit(const v8::Arguments& args); 
    45 v8::Handle<v8::Value> Version(const v8::Arguments& args); 
    4642v8::Handle<v8::String> ReadFile(const char* name); 
    4743v8::Handle<v8::Value> FCGIAccept(const v8::Arguments& args); 
     
    5955    // bind functions 
    6056    v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); 
    61     global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print)); 
    62     global->Set(v8::String::New("load"), v8::FunctionTemplate::New(Load)); 
    63     global->Set(v8::String::New("quit"), v8::FunctionTemplate::New(Quit)); 
    64     global->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version)); 
    6557    global->Set(v8::String::New("fcgi_accept"), v8::FunctionTemplate::New(FCGIAccept)); 
    6658    setup_syscall(global); 
     59    setup_core(global); 
    6760 
    6861    // Create a new execution environment containing the built-in 
     
    8679} 
    8780 
    88 // The callback that is invoked by v8 whenever the JavaScript 'print' 
    89 // function is called.  Prints its arguments on stdout separated by 
    90 // spaces and ending with a newline. 
    91 v8::Handle<v8::Value> Print(const v8::Arguments& args) { 
    92   for (int i = 0; i < args.Length(); i++) { 
    93     v8::HandleScope handle_scope; 
    94     v8::String::AsciiValue str(args[i]); 
    95     printf("%s", *str); 
    96   } 
    97   printf("\n"); 
    98   return v8::Undefined(); 
    99 } 
    100  
    10181v8::Handle<v8::Value> FCGIAccept(const v8::Arguments& args) { 
    10282    // return v8::Undefined(); 
     
    10484} 
    10585 
    106 // The callback that is invoked by v8 whenever the JavaScript 'load' 
    107 // function is called.  Loads, compiles and executes its argument 
    108 // JavaScript file. 
    109 v8::Handle<v8::Value> Load(const v8::Arguments& args) { 
    110   for (int i = 0; i < args.Length(); i++) { 
    111     v8::HandleScope handle_scope; 
    112     v8::String::AsciiValue file(args[i]); 
    113     v8::Handle<v8::String> source = ReadFile(*file); 
    114     if (source.IsEmpty()) { 
    115       return v8::ThrowException(v8::String::New("Error loading file")); 
    116     } 
    117     ExecuteString(source, v8::String::New(*file), false); 
    118   } 
    119   return v8::Undefined(); 
    120 } 
    12186 
    122  
    123 // The callback that is invoked by v8 whenever the JavaScript 'quit' 
    124 // function is called.  Quits. 
    125 v8::Handle<v8::Value> Quit(const v8::Arguments& args) { 
    126   // If not arguments are given args[0] will yield undefined which 
    127   // converts to the integer value 0. 
    128   int exit_code = args[0]->Int32Value(); 
    129   exit(exit_code); 
    130   return v8::Undefined(); 
    131 } 
    132  
    133  
    134 v8::Handle<v8::Value> Version(const v8::Arguments& args) { 
    135   return v8::String::New(v8::V8::GetVersion()); 
    136 } 
    137  
    138  
    139 // Reads a file into a v8 string. 
    140 v8::Handle<v8::String> ReadFile(const char* name) { 
    141   FILE* file = fopen(name, "rb"); 
    142   if (file == NULL) return v8::Handle<v8::String>(); 
    143  
    144   fseek(file, 0, SEEK_END); 
    145   int size = ftell(file); 
    146   rewind(file); 
    147  
    148   char* chars = new char[size + 1]; 
    149   chars[size] = '\0'; 
    150   for (int i = 0; i < size;) { 
    151     int read = fread(&chars[i], 1, size - i, file); 
    152     i += read; 
    153   } 
    154   fclose(file); 
    155   if (size>2 && chars[0] == '#' && chars[1] == '!') { 
    156         // shebang hack 
    157         char *end = strchr(chars, '\n'); 
    158         if (end && end-chars > 0) { 
    159             size -= (end-chars) + 1; 
    160             chars = end+1; 
    161         } 
    162   } 
    163   v8::Handle<v8::String> result = v8::String::New(chars, size); 
    164   delete[] chars; 
    165   return result; 
    166 } 
    167  
    168 // Executes a string within the current v8 context. 
    169 bool ExecuteString(v8::Handle<v8::String> source, 
    170                    v8::Handle<v8::Value> name, 
    171                    bool print_result) { 
    172   v8::HandleScope handle_scope; 
    173   v8::TryCatch try_catch; 
    174   v8::Handle<v8::Script> script = v8::Script::Compile(source, name); 
    175   if (script.IsEmpty()) { 
    176     // Print errors that happened during compilation. 
    177     v8::String::AsciiValue error(try_catch.Exception()); 
    178     printf("%s\n", *error); 
    179     return false; 
    180   } else { 
    181     v8::Handle<v8::Value> result = script->Run(); 
    182     if (result.IsEmpty()) { 
    183       // Print errors that happened during execution. 
    184       v8::String::AsciiValue error(try_catch.Exception()); 
    185       printf("%s\n", *error); 
    186       return false; 
    187     } else { 
    188       if (print_result && !result->IsUndefined()) { 
    189         // If all went well and the result wasn't undefined then print 
    190         // the returned value. 
    191         v8::String::AsciiValue str(result); 
    192         printf("%s\n", *str); 
    193       } 
    194       return true; 
    195     } 
    196   } 
    197 } 
    198  
  • lang/cplusplus/fcgi-v8/trunk/syscall-linux.cc

    r18731 r18741  
    11#include <v8.h> 
    2 #include "syscall.h" 
     2#include "v8-util.h" 
    33#include <sys/stat.h> 
    44#include <sys/types.h>