Changeset 19396
- Timestamp:
- 09/17/08 00:01:21 (5 years ago)
- Location:
- lang/cplusplus/llv8call/trunk
- Files:
-
- 2 added
- 7 modified
-
Sconstruct (modified) (1 diff)
-
src/core.cc (modified) (1 diff)
-
src/dir.cc (modified) (1 diff)
-
src/dll.cc (modified) (1 diff)
-
src/file.cc (modified) (1 diff)
-
src/lib.cc (added)
-
src/llv8-util.h (added)
-
src/llv8call.h (modified) (2 diffs)
-
src/main.cc (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/cplusplus/llv8call/trunk/Sconstruct
r19395 r19396 9 9 "dir.cc", 10 10 "dll.cc", 11 "lib.cc", 11 12 ] 12 13 include = ['../v8/include/', '/opt/local/include/'] -
lang/cplusplus/llv8call/trunk/src/core.cc
r19351 r19396 45 45 v8::HandleScope handle_scope; 46 46 v8::String::AsciiValue file(args[i]); 47 v8::Handle<v8::String> source = ReadFile(*file);47 v8::Handle<v8::String> source = LLV8::ReadFile(*file); 48 48 if (source.IsEmpty()) { 49 49 return v8::ThrowException(v8::String::New("Error loading file")); 50 50 } 51 ExecuteString(source, v8::String::New(*file), false);51 LLV8::Exec(source, v8::String::New(*file), false); 52 52 } 53 53 return v8::Undefined(); -
lang/cplusplus/llv8call/trunk/src/dir.cc
r19379 r19396 7 7 #include <nspr/prio.h> 8 8 #include <nspr/prerror.h> 9 #include "llv8-util.h" 9 10 10 11 using namespace v8; -
lang/cplusplus/llv8call/trunk/src/dll.cc
r19387 r19396 2 2 #include "llv8call.h" 3 3 #include <nspr/nspr.h> 4 #include "llv8-util.h" 4 5 5 6 using namespace v8; -
lang/cplusplus/llv8call/trunk/src/file.cc
r19381 r19396 6 6 #include <nspr/prio.h> 7 7 #include "llv8call.h" 8 #include "llv8-util.h" 8 9 9 10 using namespace v8; -
lang/cplusplus/llv8call/trunk/src/llv8call.h
r19385 r19396 1 1 #include <v8.h> 2 #include <string>3 #include <cassert>4 #include <nspr/nspr.h>5 #include <iostream>6 #include <sstream>7 2 8 3 #ifdef _WIN32 … … 12 7 #endif 13 8 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 10 namespace 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); 29 17 } 30 18 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 // utils50 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 8 8 #endif 9 9 10 const char * LLV8CALL_VERSION = "0.01";11 12 10 extern int __argc; 13 11 extern char ** __argv; 14 12 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 print37 // 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 hack64 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 77 13 void run_shell_mode() { 78 printf("V8 version %s, llv8call version %s\n", v8::V8::GetVersion(), LLV8 CALL_VERSION);14 printf("V8 version %s, llv8call version %s\n", v8::V8::GetVersion(), LLV8::Version()); 79 15 #ifdef HAVE_READLINE 80 16 { … … 84 20 while (line = readline("llv8call> ")) { 85 21 v8::HandleScope handle_scope; 86 ExecuteString(v8::String::New(line), v8::Undefined(), true);22 LLV8::Exec(v8::String::New(line), v8::Undefined(), true); 87 23 add_history(line); 88 24 if (++history_count > MAX_HISTORY) { … … 117 53 // bind functions 118 54 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); 124 56 125 57 v8::Handle<v8::Context> context = v8::Context::New(NULL, global); … … 129 61 const char *srcfile = argv[1]; 130 62 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); 132 64 if (source.IsEmpty()) { 133 65 printf("Error reading '%s'\n", srcfile); 134 66 return 1; 135 67 } 136 if (! ExecuteString(source, file_name, false)) {68 if (!LLV8::Exec(source, file_name, false)) { 137 69 return 1; 138 70 }
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)