1
0
Fork 0

ruby shell

This commit is contained in:
Frank Celler 2012-05-20 20:54:29 +02:00
parent b2dcf12c48
commit a6880ffc06
4 changed files with 135 additions and 13 deletions

View File

@ -43,6 +43,7 @@
#include "BasicsC/strings.h"
#include "Logger/Logger.h"
#include "MRuby/MRLineEditor.h"
#include "MRuby/MRLoader.h"
#include "MRuby/mr-utils.h"
#include "SimpleHttpClient/SimpleHttpClient.h"
#include "SimpleHttpClient/SimpleHttpResult.h"
@ -58,6 +59,7 @@ extern "C" {
using namespace std;
using namespace triagens::basics;
using namespace triagens::httpclient;
using namespace triagens::arango;
// -----------------------------------------------------------------------------
// --SECTION-- private variables
@ -78,6 +80,24 @@ using namespace triagens::httpclient;
// static size_t DEFAULT_RETRIES = 5;
// static double DEFAULT_CONNECTION_TIMEOUT = 1.0;
////////////////////////////////////////////////////////////////////////////////
/// @brief path for Ruby bootstrap files
////////////////////////////////////////////////////////////////////////////////
static string StartupPath = "";
////////////////////////////////////////////////////////////////////////////////
/// @brief path for Ruby modules files
////////////////////////////////////////////////////////////////////////////////
static string StartupModules = "";
////////////////////////////////////////////////////////////////////////////////
/// @brief startup JavaScript files
////////////////////////////////////////////////////////////////////////////////
static MRLoader StartupLoader;
////////////////////////////////////////////////////////////////////////////////
/// @brief server address
////////////////////////////////////////////////////////////////////////////////
@ -274,8 +294,8 @@ static void ParseProgramOptions (int argc, char* argv[]) {
("help,h", "help message")
("log.level,l", &level, "log level")
("server", &ServerAddress, "server address and port")
// ("startup.directory", &StartupPath, "startup paths containing the JavaScript files; multiple directories can be separated by cola")
// ("startup.modules-path", &StartupModules, "one or more directories separated by cola")
("startup.directory", &StartupPath, "startup paths containing the Ruby files; multiple directories can be separated by cola")
("startup.modules-path", &StartupModules, "one or more directories separated by cola")
("pager", &OutputPager, "output pager")
("use-pager", "use pager")
("pretty-print", "pretty print values")
@ -457,20 +477,36 @@ int main (int argc, char* argv[]) {
}
// create a new ruby shell
MR_state_t mrs;
MR_state_t* mrs = MR_OpenShell();
mrb_state* mrb = mrb_open();
// load java script from js/bootstrap/*.h files
if (StartupPath.empty()) {
}
else {
LOGGER_DEBUG << "using JavaScript startup files at '" << StartupPath << "'";
StartupLoader.setDirectory(StartupPath);
}
memcpy(&mrs, mrb, sizeof(mrb_state));
// load all init files
char const* files[] = {
"client/client.rb"
};
for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); ++i) {
bool ok = StartupLoader.loadScript(&mrs->_mrb, files[i]);
if (ok) {
LOGGER_TRACE << "loaded ruby file '" << files[i] << "'";
}
else {
LOGGER_ERROR << "cannot load ruby file '" << files[i] << "'";
exit(EXIT_FAILURE);
}
}
TRI_InitMRUtils(mrs);
mrs._arangoError = NULL;
TRI_InitMRUtils(&mrs);
RunShell(&mrs);
// calling dispose in V8 3.10.x causes a segfault. the v8 docs says its not necessary to call it upon program termination
// v8::V8::Dispose();
RunShell(mrs);
return ret;
}

View File

@ -186,6 +186,21 @@ static mrb_value MR_JsonParse (mrb_state* mrb, mrb_value self) {
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief opens a new context
////////////////////////////////////////////////////////////////////////////////
MR_state_t* MR_OpenShell () {
MR_state_t* mrs;
mrb_state* mrb = mrb_open();
mrs = TRI_Allocate(TRI_UNKNOWN_MEM_ZONE, sizeof(MR_state_t), true);
memcpy(mrs, mrb, sizeof(mrb_state));
return mrs;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief creates a ArangoError
////////////////////////////////////////////////////////////////////////////////
@ -210,6 +225,70 @@ mrb_value MR_ArangoError (mrb_state* mrb, int errNum, char const* errMessage) {
return exc;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief prints an exception and stacktrace
////////////////////////////////////////////////////////////////////////////////
void TRI_LogRubyException (mrb_state* mrb, struct RObject* exc) {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief executes a file in the current context
////////////////////////////////////////////////////////////////////////////////
bool TRI_ExecuteRubyFile (mrb_state* mrb, char const* filename) {
return false;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief executes all files from a directory in the current context
////////////////////////////////////////////////////////////////////////////////
bool TRI_ExecuteRubyDirectory (mrb_state* mrb, char const* path) {
return false;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief executes a string within a V8 context, optionally print the result
////////////////////////////////////////////////////////////////////////////////
bool TRI_ExecuteRubyString (mrb_state* mrb,
char const* script,
char const* name,
bool printResult,
mrb_value* result) {
struct mrb_parser_state* p;
mrb_value r;
int n;
p = mrb_parse_nstring(mrb, input, strlen(input));
if (p == 0 || p->tree == 0 || 0 < p->nerr) {
cout << "UPPS!\n";
return false;
}
n = mrb_generate_code(&mrs->_mrb, p->tree);
if (n < 0) {
cout << "UPPS: " << n << " returned by mrb_generate_code\n";
return false;
}
r = mrb_run(&mrs->_mrb,
mrb_proc_new(&mrs->_mrb, mrs->_mrb.irep[n]),
mrb_top_self(&mrs->_mrb));
if (mrb.exc) {
cout << "Caught exception:\n";
mrb_p(&mrs->_mrb, mrb_obj_value(mrs->_mrb.exc));
mrs->_mrb.exc = 0;
}
else if (! mrb_nil_p(result)) {
mrb_p(&mrs->_mrb, result);
}
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////

View File

@ -72,6 +72,12 @@ MR_state_t;
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief opens a new context
////////////////////////////////////////////////////////////////////////////////
MR_state_t* MR_OpenShell ();
////////////////////////////////////////////////////////////////////////////////
/// @brief creates a ArangoError
////////////////////////////////////////////////////////////////////////////////

View File

@ -947,6 +947,7 @@ static void RunShell (v8::Handle<v8::Context> context) {
}
string i = triagens::basics::StringUtils::trim(input);
if (i == "exit" || i == "quit") {
TRI_FreeString(TRI_CORE_MEM_ZONE, input);
break;