| 1 | // Copyright 2008 Google Inc. All Rights Reserved. |
|---|
| 2 | // Redistribution and use in source and binary forms, with or without |
|---|
| 3 | // modification, are permitted provided that the following conditions are |
|---|
| 4 | // met: |
|---|
| 5 | // |
|---|
| 6 | // * Redistributions of source code must retain the above copyright |
|---|
| 7 | // notice, this list of conditions and the following disclaimer. |
|---|
| 8 | // * Redistributions in binary form must reproduce the above |
|---|
| 9 | // copyright notice, this list of conditions and the following |
|---|
| 10 | // disclaimer in the documentation and/or other materials provided |
|---|
| 11 | // with the distribution. |
|---|
| 12 | // * Neither the name of Google Inc. nor the names of its |
|---|
| 13 | // contributors may be used to endorse or promote products derived |
|---|
| 14 | // from this software without specific prior written permission. |
|---|
| 15 | // |
|---|
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 27 | |
|---|
| 28 | #include <v8.h> |
|---|
| 29 | #include <cstring> |
|---|
| 30 | #include <cstdlib> |
|---|
| 31 | #include "v8-util.h" |
|---|
| 32 | #if 0 |
|---|
| 33 | # include <fcgi_stdio.h> |
|---|
| 34 | #else |
|---|
| 35 | # define FCGI_Accept() (1) |
|---|
| 36 | #endif |
|---|
| 37 | |
|---|
| 38 | void RunShell(v8::Handle<v8::Context> context); |
|---|
| 39 | bool ExecuteString(v8::Handle<v8::String> source, |
|---|
| 40 | v8::Handle<v8::Value> name, |
|---|
| 41 | bool print_result); |
|---|
| 42 | v8::Handle<v8::String> ReadFile(const char* name); |
|---|
| 43 | v8::Handle<v8::Value> FCGIAccept(const v8::Arguments& args); |
|---|
| 44 | |
|---|
| 45 | int main(int argc, char ** argv) { |
|---|
| 46 | if (argc != 2) { |
|---|
| 47 | fprintf(stderr, "Usage: %s src\n", argv[0]); |
|---|
| 48 | exit(0); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | // initialize |
|---|
| 52 | v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
|---|
| 53 | v8::HandleScope handle_scope; |
|---|
| 54 | |
|---|
| 55 | // bind functions |
|---|
| 56 | v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
|---|
| 57 | global->Set(v8::String::New("fcgi_accept"), v8::FunctionTemplate::New(FCGIAccept)); |
|---|
| 58 | setup_syscall(global); |
|---|
| 59 | setup_core(global); |
|---|
| 60 | |
|---|
| 61 | // Create a new execution environment containing the built-in |
|---|
| 62 | // functions |
|---|
| 63 | v8::Handle<v8::Context> context = v8::Context::New(NULL, global); |
|---|
| 64 | // Enter the newly created execution environment. |
|---|
| 65 | v8::Context::Scope context_scope(context); |
|---|
| 66 | |
|---|
| 67 | const char *srcfile = argv[1]; |
|---|
| 68 | v8::Handle<v8::String> file_name = v8::String::New(srcfile); |
|---|
| 69 | v8::Handle<v8::String> source = ReadFile(srcfile); |
|---|
| 70 | if (source.IsEmpty()) { |
|---|
| 71 | printf("Error reading '%s'\n", srcfile); |
|---|
| 72 | return 1; |
|---|
| 73 | } |
|---|
| 74 | if (!ExecuteString(source, file_name, false)) { |
|---|
| 75 | return 1; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | return 0; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | v8::Handle<v8::Value> FCGIAccept(const v8::Arguments& args) { |
|---|
| 82 | // return v8::Undefined(); |
|---|
| 83 | return v8::Integer::New(FCGI_Accept()); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | |
|---|