From c65d9ad28741cb3728dc391234e8298dab969f7f Mon Sep 17 00:00:00 2001 From: Ghabry Date: Mon, 22 May 2017 19:22:14 +0200 Subject: [PATCH] Ruby 1.8: Style improvements. Implement rb_str_catf & rb_hash_lookup2. --- binding-mri/binding-mri.cpp | 1 - binding-mri/binding-util.cpp | 106 +++++++++++++++++++++-------- binding-mri/filesystem-binding.cpp | 1 + 3 files changed, 78 insertions(+), 30 deletions(-) diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp index a6d9685..a4f0960 100644 --- a/binding-mri/binding-mri.cpp +++ b/binding-mri/binding-mri.cpp @@ -585,7 +585,6 @@ static void mriBindingExecute() #if RUBY_API_VERSION_MAJOR == 1 ruby_init(); - //ruby_options(argc, argv); #else ruby_sysinit(&argc, &argv); ruby_setup(); diff --git a/binding-mri/binding-util.cpp b/binding-mri/binding-util.cpp index 6203ec8..1bc2b49 100644 --- a/binding-mri/binding-util.cpp +++ b/binding-mri/binding-util.cpp @@ -91,16 +91,16 @@ void raiseDisposedAccess(VALUE self) { #ifdef RUBY_LEGACY_VERSION -// FIXME: raiseDisposedAccess not implemented + const char *klassName = rb_class2name(self); #else const char *klassName = RTYPEDDATA_TYPE(self)->wrap_struct_name; +#endif char buf[32]; strncpy(buf, klassName, sizeof(buf)); buf[0] = tolower(buf[0]); rb_raise(getRbData()->exc[RGSS], "disposed %s", buf); -#endif } int @@ -325,72 +325,120 @@ rb_get_args(int argc, VALUE *argv, const char *format, ...) } #if RUBY_API_VERSION_MAJOR == 1 -NORETURN(void rb_error_arity(int, int, int)) { +NORETURN(void rb_error_arity(int, int, int)) +{ assert(false); } + #if RUBY_API_VERSION_MINOR == 8 -// Functions providing Ruby 1.8 compatibililty -VALUE rb_sprintf(const char* fmt, ...) { - va_list args; - va_start(args, fmt); +/* +Functions providing Ruby 1.8 compatibililty. +All encoding related functions return dummy values because mkxp only uses them +to retrieve the UTF-8 encoding. +*/ +VALUE rb_sprintf_vararg(const char* fmt, va_list args) { char buf[4096]; - int const result = vsnprintf(buf, sizeof(buf), fmt, args); - va_end(args); + int result = vsnprintf(buf, sizeof(buf), fmt, args); + + if (result < 0) { + buf[0] = '\0'; + } return rb_str_new2(buf); } -VALUE rb_data_typed_object_alloc(VALUE klass, void *datap, const rb_data_type_t * rbType) { - return rb_data_object_alloc(klass, 0, rbType->function.dmark, rbType->function.dfree); +VALUE rb_sprintf(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + VALUE val = rb_sprintf_vararg(fmt, args); + va_end(args); + + return val; } -void *rb_check_typeddata(VALUE v, const rb_data_type_t *) { -// FIXME: rb_check_typeddata not implemented - return RTYPEDDATA_DATA(v); +VALUE rb_data_typed_object_alloc(VALUE klass, void *datap, const rb_data_type_t *rbType) +{ + return rb_data_object_alloc(klass, 0, rbType->function.dmark, rbType->function.dfree); } -VALUE rb_enc_str_new(const char* ch, long len, rb_encoding*) { +void *rb_check_typeddata(VALUE value, const rb_data_type_t* typed) +{ +// FIXME: Won't work because rb_typeddata_is_kind_of is not implemented + if (!rb_typeddata_is_kind_of(value, typed)) { + rb_raise(getRbData()->exc[RGSS], + "wrong data type %s (expected %s)", rb_class2name(value), typed->wrap_struct_name + ); + } + + return RTYPEDDATA_DATA(value); +} + +VALUE rb_enc_str_new(const char* ch, long len, rb_encoding*) +{ + // Encoding is ignored return rb_str_new(ch, len); } -rb_encoding *rb_utf8_encoding(void) { +rb_encoding *rb_utf8_encoding(void) +{ + // not relevant return NULL; } -void rb_enc_set_default_external(VALUE encoding) { - // no-op, assuming UTF-8 +void rb_enc_set_default_external(VALUE) +{ + // not relevant } -VALUE rb_enc_from_encoding(rb_encoding *enc) { +VALUE rb_enc_from_encoding(rb_encoding*) +{ + // not relevant return Qnil; } -VALUE rb_str_catf(VALUE, const char*, ...) { - return Qnil; +VALUE rb_str_catf(VALUE value, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + VALUE second = rb_sprintf_vararg(fmt, args); + va_end(args); + + return rb_str_concat(value, second); } -VALUE rb_errinfo(void) { +VALUE rb_errinfo(void) +{ return ruby_errinfo; } -int rb_typeddata_is_kind_of(VALUE, const rb_data_type_t *) { +int rb_typeddata_is_kind_of(VALUE value, const rb_data_type_t* typed) +{ +// FIXME: rb_typeddata_is_kind_of not implemented return 1; } -VALUE rb_file_open_str(VALUE filename, const char* mode) { +VALUE rb_file_open_str(VALUE filename, const char* mode) +{ return rb_file_open(RB_OBJ_STRING(filename), mode); } -VALUE rb_enc_associate_index(VALUE, int) { +VALUE rb_enc_associate_index(VALUE, int) +{ + // not relevant return Qnil; } -int rb_utf8_encindex(void) { +int rb_utf8_encindex(void) +{ + // not relevant return 0; } -VALUE rb_hash_lookup2(VALUE, VALUE, VALUE) { - return Qnil; +VALUE rb_hash_lookup2(VALUE hash, VALUE key, VALUE def) +{ + VALUE v = rb_hash_lookup(hash, key); + return (v == Qnil) ? def : v; } #endif // RUBY_API_VERSION_MINOR == 8 -#endif // RUBY_API_VERSION_MAJOR == 1 \ No newline at end of file +#endif // RUBY_API_VERSION_MAJOR == 1 diff --git a/binding-mri/filesystem-binding.cpp b/binding-mri/filesystem-binding.cpp index 9a92c52..512ce4f 100644 --- a/binding-mri/filesystem-binding.cpp +++ b/binding-mri/filesystem-binding.cpp @@ -132,6 +132,7 @@ kernelLoadDataInt(const char *filename, bool rubyExc) VALUE port = fileIntForPath(filename, rubyExc); VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal")); + // FIXME need to catch exceptions here with begin rescue VALUE result = rb_funcall2(marsh, rb_intern("load"), 1, &port);