mirror of https://gitee.com/bigwinds/arangodb
exception and json parser
This commit is contained in:
parent
00ea15fffc
commit
6152635226
|
@ -665,7 +665,7 @@ static int input (yyscan_t yyscanner );
|
||||||
if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \
|
if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \
|
||||||
{ \
|
{ \
|
||||||
int c = '*'; \
|
int c = '*'; \
|
||||||
int n; \
|
yy_size_t n; \
|
||||||
for ( n = 0; n < max_size && \
|
for ( n = 0; n < max_size && \
|
||||||
(c = getc( yyin )) != EOF && c != '\n'; ++n ) \
|
(c = getc( yyin )) != EOF && c != '\n'; ++n ) \
|
||||||
buf[n] = (char) c; \
|
buf[n] = (char) c; \
|
||||||
|
@ -2385,12 +2385,14 @@ TRI_json_t* TRI_Json2String (TRI_memory_zone_t* zone, char const* text, char** e
|
||||||
|
|
||||||
if (c != END_OF_FILE) {
|
if (c != END_OF_FILE) {
|
||||||
object = NULL;
|
object = NULL;
|
||||||
|
yyextra._message = "failed to parse json object: expecting EOF";
|
||||||
|
|
||||||
LOG_DEBUG("failed to parse json object: expecting EOF");
|
LOG_DEBUG("failed to parse json object: expecting EOF");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error != NULL) {
|
if (error != NULL) {
|
||||||
if (yyextra._message != 0) {
|
if (yyextra._message != NULL) {
|
||||||
*error = TRI_DuplicateString(yyextra._message);
|
*error = TRI_DuplicateString(yyextra._message);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -494,12 +494,14 @@ TRI_json_t* TRI_Json2String (TRI_memory_zone_t* zone, char const* text, char** e
|
||||||
|
|
||||||
if (c != END_OF_FILE) {
|
if (c != END_OF_FILE) {
|
||||||
object = NULL;
|
object = NULL;
|
||||||
|
yyextra._message = "failed to parse json object: expecting EOF";
|
||||||
|
|
||||||
LOG_DEBUG("failed to parse json object: expecting EOF");
|
LOG_DEBUG("failed to parse json object: expecting EOF");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error != NULL) {
|
if (error != NULL) {
|
||||||
if (yyextra._message != 0) {
|
if (yyextra._message != NULL) {
|
||||||
*error = TRI_DuplicateString(yyextra._message);
|
*error = TRI_DuplicateString(yyextra._message);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -338,8 +338,8 @@ static void ParseProgramOptions (int argc, char* argv[]) {
|
||||||
/// @brief executes the shell
|
/// @brief executes the shell
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
static void RunShell (mrb_state* mrb) {
|
static void RunShell (MR_state_t* mrs) {
|
||||||
MRLineEditor* console = new MRLineEditor(mrb, ".arango-mrb");
|
MRLineEditor* console = new MRLineEditor(mrs, ".arango-mrb");
|
||||||
|
|
||||||
console->open(! NoAutoComplete);
|
console->open(! NoAutoComplete);
|
||||||
|
|
||||||
|
@ -357,7 +357,7 @@ static void RunShell (mrb_state* mrb) {
|
||||||
|
|
||||||
console->addHistory(input);
|
console->addHistory(input);
|
||||||
|
|
||||||
struct mrb_parser_state* p = mrb_parse_nstring(mrb, input, strlen(input));
|
struct mrb_parser_state* p = mrb_parse_nstring(&mrs->_mrb, input, strlen(input));
|
||||||
TRI_FreeString(TRI_CORE_MEM_ZONE, input);
|
TRI_FreeString(TRI_CORE_MEM_ZONE, input);
|
||||||
|
|
||||||
if (p == 0 || p->tree == 0 || 0 < p->nerr) {
|
if (p == 0 || p->tree == 0 || 0 < p->nerr) {
|
||||||
|
@ -365,24 +365,24 @@ static void RunShell (mrb_state* mrb) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int n = mrb_generate_code(mrb, p->tree);
|
int n = mrb_generate_code(&mrs->_mrb, p->tree);
|
||||||
|
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
cout << "UPPS: " << n << " returned by mrb_generate_code\n";
|
cout << "UPPS: " << n << " returned by mrb_generate_code\n";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
mrb_value result = mrb_run(mrb,
|
mrb_value result = mrb_run(&mrs->_mrb,
|
||||||
mrb_proc_new(mrb, mrb->irep[n]),
|
mrb_proc_new(&mrs->_mrb, mrs->_mrb.irep[n]),
|
||||||
mrb_top_self(mrb));
|
mrb_top_self(&mrs->_mrb));
|
||||||
|
|
||||||
if (mrb->exc) {
|
if (mrs->_mrb.exc) {
|
||||||
cout << "Caught exception:\n";
|
cout << "Caught exception:\n";
|
||||||
mrb_p(mrb, mrb_obj_value(mrb->exc));
|
mrb_p(&mrs->_mrb, mrb_obj_value(mrs->_mrb.exc));
|
||||||
mrb->exc = 0;
|
mrs->_mrb.exc = 0;
|
||||||
}
|
}
|
||||||
else if (! mrb_nil_p(result)) {
|
else if (! mrb_nil_p(result)) {
|
||||||
mrb_p(mrb, result);
|
mrb_p(&mrs->_mrb, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -457,11 +457,17 @@ int main (int argc, char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a new ruby shell
|
// create a new ruby shell
|
||||||
|
MR_state_t mrs;
|
||||||
|
|
||||||
mrb_state* mrb = mrb_open();
|
mrb_state* mrb = mrb_open();
|
||||||
|
|
||||||
TRI_InitMRUtils(mrb);
|
memcpy(&mrs, mrb, sizeof(mrb_state));
|
||||||
|
|
||||||
RunShell(mrb);
|
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
|
// 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();
|
// v8::V8::Dispose();
|
||||||
|
|
|
@ -237,8 +237,8 @@ static char** AttemptedCompletion (char const* text, int start, int end) {
|
||||||
/// @brief constructs a new editor
|
/// @brief constructs a new editor
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
MRLineEditor::MRLineEditor (mrb_state* mrb, string const& history)
|
MRLineEditor::MRLineEditor (MR_state_t* mrs, string const& history)
|
||||||
: LineEditor(history), _current(), _mrb(mrb) {
|
: LineEditor(history), _current(), _mrs(mrs) {
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -285,7 +285,7 @@ bool MRLineEditor::isComplete (string const& source, size_t lineno, size_t colum
|
||||||
char const* msg = "syntax error, unexpected $end";
|
char const* msg = "syntax error, unexpected $end";
|
||||||
char* text = TRI_DuplicateString(source.c_str());
|
char* text = TRI_DuplicateString(source.c_str());
|
||||||
|
|
||||||
struct mrb_parser_state* p = mrb_parse_nstring_ext(_mrb, text, source.size());
|
struct mrb_parser_state* p = mrb_parse_nstring_ext(&_mrs->_mrb, text, source.size());
|
||||||
TRI_FreeString(TRI_CORE_MEM_ZONE, text);
|
TRI_FreeString(TRI_CORE_MEM_ZONE, text);
|
||||||
|
|
||||||
// out of memory?
|
// out of memory?
|
||||||
|
|
|
@ -30,14 +30,12 @@
|
||||||
|
|
||||||
#include "Utilities/LineEditor.h"
|
#include "Utilities/LineEditor.h"
|
||||||
|
|
||||||
|
#include "MRuby/mr-utils.h"
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// --SECTION-- forward declarations
|
// --SECTION-- forward declarations
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
extern "C" {
|
|
||||||
struct mrb_state;
|
|
||||||
}
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// --SECTION-- class MRLineEditor
|
// --SECTION-- class MRLineEditor
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
@ -71,7 +69,7 @@ class MRLineEditor : public LineEditor {
|
||||||
/// @brief constructor
|
/// @brief constructor
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
MRLineEditor (struct mrb_state*, std::string const& history);
|
MRLineEditor (MR_state_t*, std::string const& history);
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
/// @}
|
/// @}
|
||||||
|
@ -138,7 +136,7 @@ class MRLineEditor : public LineEditor {
|
||||||
/// @brief ruby state
|
/// @brief ruby state
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
struct mrb_state* _mrb;
|
MR_state_t* _mrs;
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
|
@ -28,14 +28,15 @@
|
||||||
#include "mr-utils.h"
|
#include "mr-utils.h"
|
||||||
|
|
||||||
#include "BasicsC/json.h"
|
#include "BasicsC/json.h"
|
||||||
|
#include "BasicsC/strings.h"
|
||||||
|
|
||||||
#include "mruby.h"
|
|
||||||
#include "mruby/array.h"
|
#include "mruby/array.h"
|
||||||
#include "mruby/data.h"
|
#include "mruby/data.h"
|
||||||
#include "mruby/hash.h"
|
#include "mruby/hash.h"
|
||||||
#include "mruby/proc.h"
|
#include "mruby/proc.h"
|
||||||
#include "mruby/string.h"
|
#include "mruby/string.h"
|
||||||
#include "mruby/variable.h"
|
#include "mruby/variable.h"
|
||||||
|
#include "mruby/error.h"
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// --SECTION-- private functions
|
// --SECTION-- private functions
|
||||||
|
@ -124,7 +125,7 @@ static mrb_value MR_ObjectJson (mrb_state* mrb, TRI_json_t const* json) {
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// --SECTION-- private functions
|
// --SECTION-- ruby functions
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -160,7 +161,13 @@ static mrb_value MR_JsonParse (mrb_state* mrb, mrb_value self) {
|
||||||
json = TRI_Json2String(TRI_UNKNOWN_MEM_ZONE, s, &errmsg);
|
json = TRI_Json2String(TRI_UNKNOWN_MEM_ZONE, s, &errmsg);
|
||||||
|
|
||||||
if (json == NULL) {
|
if (json == NULL) {
|
||||||
return mrb_nil_value();
|
mrb_value exc;
|
||||||
|
|
||||||
|
exc = MR_ArangoError(mrb, TRI_ERROR_, errmsg);
|
||||||
|
TRI_FreeString(TRI_UNKNOWN_MEM_ZONE, errmsg);
|
||||||
|
|
||||||
|
mrb_exc_raise(mrb, exc);
|
||||||
|
assert(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return MR_ObjectJson(mrb, json);
|
return MR_ObjectJson(mrb, json);
|
||||||
|
@ -171,7 +178,7 @@ static mrb_value MR_JsonParse (mrb_state* mrb, mrb_value self) {
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// --SECTION-- private functions
|
// --SECTION-- ruby functions
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -180,25 +187,62 @@ static mrb_value MR_JsonParse (mrb_state* mrb, mrb_value self) {
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
/// @brief init utilities
|
/// @brief creates a ArangoError
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
void TRI_InitMRUtils (mrb_state* mrb) {
|
mrb_value MR_ArangoError (mrb_state* mrb, int errNum, char const* errMessage) {
|
||||||
|
MR_state_t* mrs;
|
||||||
|
mrb_value exc;
|
||||||
|
mrb_value val;
|
||||||
|
mrb_sym id;
|
||||||
|
|
||||||
|
mrs = (MR_state_t*) mrb;
|
||||||
|
exc = mrb_exc_new(mrb, mrs->_arangoError, errMessage, strlen(errMessage));
|
||||||
|
|
||||||
|
id = mrb_intern(mrb, "@error_num");
|
||||||
|
val = mrb_fixnum_value(errNum);
|
||||||
|
mrb_iv_set(mrb, exc, id, val);
|
||||||
|
|
||||||
|
id = mrb_intern(mrb, "@error_message");
|
||||||
|
val = mrb_str_new(mrb, errMessage, strlen(errMessage));
|
||||||
|
mrb_iv_set(mrb, exc, id, val);
|
||||||
|
|
||||||
|
return exc;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
/// @}
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// --SECTION-- module functions
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
/// @addtogroup ArangoDB
|
||||||
|
/// @{
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
/// @brief init mruby utilities
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
void TRI_InitMRUtils (MR_state_t* mrs) {
|
||||||
struct RClass *rcl;
|
struct RClass *rcl;
|
||||||
|
|
||||||
rcl = mrb->kernel_module;
|
rcl = mrs->_mrb.kernel_module;
|
||||||
|
|
||||||
// .............................................................................
|
// .............................................................................
|
||||||
// timing function
|
// timing function
|
||||||
// .............................................................................
|
// .............................................................................
|
||||||
|
|
||||||
mrb_define_method(mrb, rcl, "time", MR_Time, ARGS_NONE());
|
mrb_define_method(&mrs->_mrb, rcl, "time", MR_Time, ARGS_NONE());
|
||||||
|
|
||||||
// .............................................................................
|
// .............................................................................
|
||||||
// arango exception
|
// arango exception
|
||||||
// .............................................................................
|
// .............................................................................
|
||||||
|
|
||||||
rcl = mrb_define_class(mrb, "ArangoError", mrb->eStandardError_class);
|
mrs->_arangoError = mrb_define_class(&mrs->_mrb, "ArangoError", mrs->_mrb.eStandardError_class);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
mrb_value
|
mrb_value
|
||||||
|
@ -219,9 +263,9 @@ mrb_obj_ivar_set(mrb_state *mrb, mrb_value self)
|
||||||
// json parser and generator
|
// json parser and generator
|
||||||
// .............................................................................
|
// .............................................................................
|
||||||
|
|
||||||
rcl = mrb_define_class(mrb, "ArangoJson", mrb->object_class);
|
rcl = mrb_define_class(&mrs->_mrb, "ArangoJson", mrs->_mrb.object_class);
|
||||||
|
|
||||||
mrb_define_class_method(mrb, rcl, "parse", MR_JsonParse, ARGS_REQ(1));
|
mrb_define_class_method(&mrs->_mrb, rcl, "parse", MR_JsonParse, ARGS_REQ(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
|
@ -34,14 +34,14 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "mruby.h"
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// --SECTION-- forward declarations
|
// --SECTION-- forward declarations
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
struct mrb_state;
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// --SECTION-- private variables
|
// --SECTION-- private types
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -49,6 +49,16 @@ struct mrb_state;
|
||||||
/// @{
|
/// @{
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
/// @brief mruby state info
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
typedef struct MR_state_s {
|
||||||
|
struct mrb_state _mrb;
|
||||||
|
struct RClass* _arangoError;
|
||||||
|
}
|
||||||
|
MR_state_t;
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
/// @}
|
/// @}
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -66,7 +76,7 @@ struct mrb_state;
|
||||||
/// @brief init utilities
|
/// @brief init utilities
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
void TRI_InitMRUtils (struct mrb_state* mrb);
|
void TRI_InitMRUtils (MR_state_t* mrb);
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
/// @}
|
/// @}
|
||||||
|
|
|
@ -963,6 +963,7 @@ mrb_value MR_ArangoDatabase_Collection (mrb_state* mrb, mrb_value exc) {
|
||||||
|
|
||||||
int ArangoServer::executeRubyShell () {
|
int ArangoServer::executeRubyShell () {
|
||||||
struct mrb_parser_state* p;
|
struct mrb_parser_state* p;
|
||||||
|
MR_state_t mrs;
|
||||||
mrb_state* mrb;
|
mrb_state* mrb;
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
|
@ -977,8 +978,10 @@ int ArangoServer::executeRubyShell () {
|
||||||
// create a new ruby shell
|
// create a new ruby shell
|
||||||
mrb = mrb_open();
|
mrb = mrb_open();
|
||||||
|
|
||||||
|
memcpy(&mrs, mrb, sizeof(mrb_state));
|
||||||
|
|
||||||
// create a line editor
|
// create a line editor
|
||||||
MRLineEditor* console = new MRLineEditor(mrb, ".arango-mrb");
|
MRLineEditor* console = new MRLineEditor(&mrs, ".arango-mrb");
|
||||||
|
|
||||||
// setup the classes
|
// setup the classes
|
||||||
#if 0
|
#if 0
|
||||||
|
|
Loading…
Reference in New Issue