// Copyright 2012 the V8 project authors. All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following // disclaimer in the documentation and/or other materials provided // with the distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include <assert.h> #include <fcntl.h> #include <include/libplatform/libplatform.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "include/v8-context.h" #include "include/v8-exception.h" #include "include/v8-initialization.h" #include "include/v8-isolate.h" #include "include/v8-local-handle.h" #include "include/v8-script.h" #include "include/v8-template.h" /** * This sample program shows how to implement a simple javascript shell * based on V8. This includes initializing V8 with command line options, * creating global functions, compiling and executing strings. * * For a more sophisticated shell, consider using the debug shell D8. */ v8::Global<v8::Context> CreateShellContext(v8::Isolate* isolate); void RunShell(v8::Isolate* isolate, const v8::Global<v8::Context>& context, v8::Platform* platform); int RunMain(v8::Isolate* isolate, const v8::Global<v8::Context>& context, v8::Platform* platform, int argc, char* argv[]); bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source, v8::Local<v8::Value> name, bool print_result, bool report_exceptions); void Print(const v8::FunctionCallbackInfo<v8::Value>& info); void Read(const v8::FunctionCallbackInfo<v8::Value>& info); void Load(const v8::FunctionCallbackInfo<v8::Value>& info); void Quit(const v8::FunctionCallbackInfo<v8::Value>& info); void Version(const v8::FunctionCallbackInfo<v8::Value>& info); v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name); void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); static bool run_shell; int main(int argc, char* argv[]) { … } // Extracts a C string from a V8 Utf8Value. const char* ToCString(const v8::String::Utf8Value& value) { … } // Creates a new execution environment containing the built-in // functions. v8::Global<v8::Context> CreateShellContext(v8::Isolate* isolate) { … } // The callback that is invoked by v8 whenever the JavaScript 'print' // function is called. Prints its arguments on stdout separated by // spaces and ending with a newline. void Print(const v8::FunctionCallbackInfo<v8::Value>& info) { … } // The callback that is invoked by v8 whenever the JavaScript 'read' // function is called. This function loads the content of the file named in // the argument into a JavaScript string. void Read(const v8::FunctionCallbackInfo<v8::Value>& info) { … } // The callback that is invoked by v8 whenever the JavaScript 'load' // function is called. Loads, compiles and executes its argument // JavaScript file. void Load(const v8::FunctionCallbackInfo<v8::Value>& info) { … } // The callback that is invoked by v8 whenever the JavaScript 'quit' // function is called. Quits. void Quit(const v8::FunctionCallbackInfo<v8::Value>& info) { … } // The callback that is invoked by v8 whenever the JavaScript 'version' // function is called. Returns a string containing the current V8 version. void Version(const v8::FunctionCallbackInfo<v8::Value>& info) { … } // Reads a file into a v8 string. v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name) { … } // Process remaining command line arguments and execute files int RunMain(v8::Isolate* isolate, const v8::Global<v8::Context>& context, v8::Platform* platform, int argc, char* argv[]) { … } // The read-eval-execute loop of the shell. void RunShell(v8::Isolate* isolate, const v8::Global<v8::Context>& context, v8::Platform* platform) { … } // Executes a string within the current v8 context. bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source, v8::Local<v8::Value> name, bool print_result, bool report_exceptions) { … } void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) { … }