MRI: rewrite script eval (was: Question about $0) #24

Merged
cremno merged 7 commits from mri-rewrite-script-eval into master 2014-04-12 09:54:13 +00:00
1 changed files with 35 additions and 16 deletions
Showing only changes of commit f6ec364632 - Show all commits

View File

@ -171,9 +171,36 @@ RB_METHOD(mriDataDirectory)
return pathStr; return pathStr;
} }
static VALUE newStringUTF8(const char *string)
{
return rb_enc_str_new(string, strlen(string), rb_utf8_encoding());
}
struct evalArg {
VALUE string;
VALUE filename;
};
static VALUE evalHelper(evalArg *arg)
{
VALUE argv[] = { arg->string, Qnil, arg->filename };
return rb_funcall2(Qnil, rb_intern("eval"), ARRAY_SIZE(argv), argv);
}
static VALUE evalString(VALUE string, VALUE filename, int *state)
{
evalArg arg = { string, filename };
return rb_protect((VALUE (*)(VALUE))evalHelper, (VALUE)&arg, state);
}
static VALUE evalCString(const char *string, const char *filename, int *state)
{
return evalString(newStringUTF8(string), newStringUTF8(filename), state);
}
static void runCustomScript(const char *filename) static void runCustomScript(const char *filename)
{ {
std::string scriptData("#encoding:utf-8\n"); std::string scriptData;
if (!readFile(filename, scriptData)) if (!readFile(filename, scriptData))
{ {
@ -181,7 +208,7 @@ static void runCustomScript(const char *filename)
return; return;
} }
rb_eval_string_protect(scriptData.c_str(), 0); evalCString(scriptData.c_str(), "Section000", NULL);
} }
VALUE kernelLoadDataInt(const char *filename); VALUE kernelLoadDataInt(const char *filename);
@ -268,22 +295,14 @@ static void runRMXPScripts()
{ {
VALUE script = rb_ary_entry(scriptArray, i); VALUE script = rb_ary_entry(scriptArray, i);
VALUE scriptDecoded = rb_ary_entry(script, 3); VALUE scriptDecoded = rb_ary_entry(script, 3);
const char *string = StringValueCStr(scriptDecoded);
/* Store encoding header + the decoded script char fname[32];
* in 'sc.decData' */ snprintf(fname, sizeof(fname), "Section%03ld", i);
std::string decData = "#encoding:utf-8\n";
size_t hdSize = decData.size();
const char *scriptDecPtr;
long scriptDecLen;
RSTRING_GETMEM(scriptDecoded, scriptDecPtr, scriptDecLen);
decData.resize(hdSize + scriptDecLen);
memcpy(&decData[hdSize], scriptDecPtr, scriptDecLen);
/* Execute code */ int state;
rb_eval_string_protect(decData.c_str(), 0); evalCString(string, fname, &state);
if (state)
VALUE exc = rb_errinfo();
if (!NIL_P(exc))
break; break;
} }
} }