| 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 | | } |
| 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 | | |