MRI: changes to mkxp's Marshal::load monkey patch

- force string encoding to UTF-8 only when it's ASCII-8BIT (force only valid UTF-8 strings in future?)

- support original Marshal::load proc (2nd arg)
This commit is contained in:
cremno 2013-10-22 13:45:57 +02:00
parent 10b3e04dee
commit 071ad5d961
1 changed files with 14 additions and 8 deletions

View File

@ -159,14 +159,19 @@ RB_METHOD(kernelSaveData)
static VALUE stringForceUTF8(VALUE arg) static VALUE stringForceUTF8(VALUE arg)
{ {
if (rb_type(arg) != RUBY_T_STRING) if (rb_type(arg) == RUBY_T_STRING && ENCODING_IS_ASCII8BIT(arg))
return arg; rb_enc_associate_index(arg, rb_utf8_encindex());
rb_enc_associate(arg, rb_utf8_encoding());
return arg; return arg;
} }
static VALUE marshal_load_custom_proc(VALUE arg, VALUE proc)
{
VALUE obj = stringForceUTF8(arg);
obj = rb_funcall2(proc, rb_intern("call"), 1, &obj);
return obj;
}
RB_METHOD(_marshalLoad) RB_METHOD(_marshalLoad)
{ {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
@ -175,10 +180,11 @@ RB_METHOD(_marshalLoad)
rb_get_args(argc, argv, "o|o", &port, &proc, RB_ARG_END); rb_get_args(argc, argv, "o|o", &port, &proc, RB_ARG_END);
if (rb_type(proc) != RUBY_T_NIL) VALUE utf8Proc;
rb_raise(rb_eNotImpError, "MKXP: Marshal with custom proc not (yet) implemented"); if (NIL_P(proc))
utf8Proc = rb_proc_new(RUBY_METHOD_FUNC(stringForceUTF8), Qnil);
VALUE utf8Proc = rb_proc_new(RUBY_METHOD_FUNC(stringForceUTF8), Qnil); else
utf8Proc = rb_proc_new(RUBY_METHOD_FUNC(marshal_load_custom_proc), proc);
VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal")); VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal"));