void V8ShellFeature::loadModules(ShellFeature::RunMode runMode) { auto context = _isolate->GetCurrentContext(); JSLoader loader; loader.setDirectory(_startupDirectory); // load all init files std::vector<std::string> files; files.push_back("common/bootstrap/scaffolding.js"); files.push_back("common/bootstrap/modules/internal.js"); // deps: - files.push_back("common/bootstrap/errors.js"); // deps: internal files.push_back("client/bootstrap/modules/internal.js"); // deps: internal files.push_back("common/bootstrap/modules/vm.js"); // deps: internal files.push_back("common/bootstrap/modules/console.js"); // deps: internal files.push_back("common/bootstrap/modules/assert.js"); // deps: - files.push_back("common/bootstrap/modules/buffer.js"); // deps: internal files.push_back( "common/bootstrap/modules/fs.js"); // deps: internal, buffer (hidden) files.push_back("common/bootstrap/modules/path.js"); // deps: internal, fs files.push_back("common/bootstrap/modules/events.js"); // deps: - files.push_back("common/bootstrap/modules/process.js"); // deps: internal, // fs, events, // console files.push_back( "common/bootstrap/modules.js"); // must come last before patches files.push_back("client/client.js"); // needs internal for (size_t i = 0; i < files.size(); ++i) { switch (loader.loadScript(_isolate, context, files[i])) { case JSLoader::eSuccess: LOG(TRACE) << "loaded JavaScript file '" << files[i] << "'"; break; case JSLoader::eFailLoad: LOG(FATAL) << "cannot load JavaScript file '" << files[i] << "'"; FATAL_ERROR_EXIT(); break; case JSLoader::eFailExecute: LOG(FATAL) << "error during execution of JavaScript file '" << files[i] << "'"; FATAL_ERROR_EXIT(); break; } } }
void AvocadoServer::executeShell () { v8::Isolate* isolate; v8::Persistent<v8::Context> context; bool ok; char const* files[] = { "bootstrap/modules.js", "bootstrap/print.js", "server/modules.js", "server/json.js", "server/aql.js", "server/shell.js" }; size_t i; // only simple logging TRI_ShutdownLogging(); TRI_InitialiseLogging(false); TRI_CreateLogAppenderFile("+"); // open the database openDatabase(); // enter a new isolate isolate = v8::Isolate::New(); isolate->Enter(); // global scope v8::HandleScope globalScope; // create the context context = v8::Context::New(0); if (context.IsEmpty()) { LOGGER_FATAL << "cannot initialize V8 engine"; cerr << "cannot initialize V8 engine\n"; exit(EXIT_FAILURE); } context->Enter(); LOGGER_INFO << "using JavaScript modules path '" << _startupModules << "'"; TRI_InitV8VocBridge(context, _vocbase); TRI_InitV8Conversions(context); TRI_InitV8Utils(context, _startupModules); TRI_InitV8Shell(context); // load all init files for (i = 0; i < sizeof(files) / sizeof(files[0]); ++i) { ok = StartupLoader.loadScript(context, files[i]); if (ok) { LOGGER_TRACE << "loaded json file '" << files[i] << "'"; } else { LOGGER_FATAL << "cannot load json file '" << files[i] << "'"; cerr << "cannot load json file '" << files[i] << "'\n"; exit(EXIT_FAILURE); } } // run the shell printf("AvocadoDB shell [V8 version %s, DB version %s]\n", v8::V8::GetVersion(), TRIAGENS_VERSION); v8::Context::Scope contextScope(context); v8::Local<v8::String> name(v8::String::New("(avocado)")); V8LineEditor* console = new V8LineEditor(context, ".avocado"); console->open(); while (true) { while(! v8::V8::IdleNotification()) { } char* input = console->prompt("avocado> "); if (input == 0) { printf("bye...\n"); TRI_FreeString(input); break; } if (*input == '\0') { TRI_FreeString(input); continue; } console->addHistory(input); v8::HandleScope scope; TRI_ExecuteStringVocBase(context, v8::String::New(input), name, true, true); TRI_FreeString(input); } console->close(); delete console; // and return from the context and isolate context->Exit(); isolate->Exit(); // close the database closeDatabase(); }