Skip to content

Commit fe3d727

Browse files
committed
Ruby 1.8: Style improvements. Implement rb_str_catf & rb_hash_lookup2.
1 parent 775fe36 commit fe3d727

File tree

3 files changed

+77
-29
lines changed

3 files changed

+77
-29
lines changed

binding-mri/binding-mri.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,6 @@ static void mriBindingExecute()
576576

577577
#if RUBY_API_VERSION_MAJOR == 1
578578
ruby_init();
579-
//ruby_options(argc, argv);
580579
#else
581580
ruby_sysinit(&argc, &argv);
582581
ruby_setup();

binding-mri/binding-util.cpp

Lines changed: 76 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,16 @@ void
9191
raiseDisposedAccess(VALUE self)
9292
{
9393
#ifdef RUBY_LEGACY_VERSION
94-
// FIXME: raiseDisposedAccess not implemented
94+
const char *klassName = rb_class2name(self);
9595
#else
9696
const char *klassName = RTYPEDDATA_TYPE(self)->wrap_struct_name;
97+
#endif
9798
char buf[32];
9899

99100
strncpy(buf, klassName, sizeof(buf));
100101
buf[0] = tolower(buf[0]);
101102

102103
rb_raise(getRbData()->exc[RGSS], "disposed %s", buf);
103-
#endif
104104
}
105105

106106
int
@@ -325,72 +325,120 @@ rb_get_args(int argc, VALUE *argv, const char *format, ...)
325325
}
326326

327327
#if RUBY_API_VERSION_MAJOR == 1
328-
NORETURN(void rb_error_arity(int, int, int)) {
328+
NORETURN(void rb_error_arity(int, int, int))
329+
{
329330
assert(false);
330331
}
332+
331333
#if RUBY_API_VERSION_MINOR == 8
332-
// Functions providing Ruby 1.8 compatibililty
333-
VALUE rb_sprintf(const char* fmt, ...) {
334+
/*
335+
Functions providing Ruby 1.8 compatibililty.
336+
All encoding related functions return dummy values because mkxp only uses them
337+
to retrieve the UTF-8 encoding.
338+
*/
339+
VALUE rb_sprintf_vararg(const char* fmt, va_list args) {
340+
char buf[4096];
341+
int result = vsnprintf(buf, sizeof(buf), fmt, args);
342+
343+
if (result < 0) {
344+
buf[0] = '\0';
345+
}
346+
347+
return rb_str_new2(buf);
348+
}
349+
350+
VALUE rb_sprintf(const char* fmt, ...)
351+
{
334352
va_list args;
335353
va_start(args, fmt);
336-
char buf[4096];
337-
int const result = vsnprintf(buf, sizeof(buf), fmt, args);
354+
VALUE val = rb_sprintf_vararg(fmt, args);
338355
va_end(args);
339356

340-
return rb_str_new2(buf);
357+
return val;
341358
}
342359

343-
VALUE rb_data_typed_object_alloc(VALUE klass, void *datap, const rb_data_type_t * rbType) {
344-
return rb_data_object_alloc(klass, 0, rbType->function.dmark, rbType->function.dfree);
360+
VALUE rb_data_typed_object_alloc(VALUE klass, void *datap, const rb_data_type_t *rbType)
361+
{
362+
return rb_data_object_alloc(klass, 0, rbType->function.dmark, rbType->function.dfree);
345363
}
346364

347-
void *rb_check_typeddata(VALUE v, const rb_data_type_t *) {
348-
// FIXME: rb_check_typeddata not implemented
349-
return RTYPEDDATA_DATA(v);
365+
void *rb_check_typeddata(VALUE value, const rb_data_type_t* typed)
366+
{
367+
// FIXME: Won't work because rb_typeddata_is_kind_of is not implemented
368+
if (!rb_typeddata_is_kind_of(value, typed)) {
369+
rb_raise(getRbData()->exc[RGSS],
370+
"wrong data type %s (expected %s)", rb_class2name(value), typed->wrap_struct_name
371+
);
372+
}
373+
374+
return RTYPEDDATA_DATA(value);
350375
}
351376

352-
VALUE rb_enc_str_new(const char* ch, long len, rb_encoding*) {
377+
VALUE rb_enc_str_new(const char* ch, long len, rb_encoding*)
378+
{
379+
// Encoding is ignored
353380
return rb_str_new(ch, len);
354381
}
355382

356-
rb_encoding *rb_utf8_encoding(void) {
383+
rb_encoding *rb_utf8_encoding(void)
384+
{
385+
// not relevant
357386
return NULL;
358387
}
359388

360-
void rb_enc_set_default_external(VALUE encoding) {
361-
// no-op, assuming UTF-8
389+
void rb_enc_set_default_external(VALUE)
390+
{
391+
// not relevant
362392
}
363393

364-
VALUE rb_enc_from_encoding(rb_encoding *enc) {
394+
VALUE rb_enc_from_encoding(rb_encoding*)
395+
{
396+
// not relevant
365397
return Qnil;
366398
}
367399

368-
VALUE rb_str_catf(VALUE, const char*, ...) {
369-
return Qnil;
400+
VALUE rb_str_catf(VALUE value, const char* fmt, ...)
401+
{
402+
va_list args;
403+
va_start(args, fmt);
404+
VALUE second = rb_sprintf_vararg(fmt, args);
405+
va_end(args);
406+
407+
return rb_str_concat(value, second);
370408
}
371409

372-
VALUE rb_errinfo(void) {
410+
VALUE rb_errinfo(void)
411+
{
373412
return ruby_errinfo;
374413
}
375414

376-
int rb_typeddata_is_kind_of(VALUE, const rb_data_type_t *) {
415+
int rb_typeddata_is_kind_of(VALUE value, const rb_data_type_t* typed)
416+
{
417+
// FIXME: rb_typeddata_is_kind_of not implemented
377418
return 1;
378419
}
379420

380-
VALUE rb_file_open_str(VALUE filename, const char* mode) {
421+
VALUE rb_file_open_str(VALUE filename, const char* mode)
422+
{
381423
return rb_file_open(RB_OBJ_STRING(filename), mode);
382424
}
383425

384-
VALUE rb_enc_associate_index(VALUE, int) {
426+
VALUE rb_enc_associate_index(VALUE, int)
427+
{
428+
// not relevant
385429
return Qnil;
386430
}
387431

388-
int rb_utf8_encindex(void) {
432+
int rb_utf8_encindex(void)
433+
{
434+
// not relevant
389435
return 0;
390436
}
391437

392-
VALUE rb_hash_lookup2(VALUE, VALUE, VALUE) {
393-
return Qnil;
438+
VALUE rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
439+
{
440+
VALUE v = rb_hash_lookup(hash, key);
441+
return (v == Qnil) ? def : v;
394442
}
395443
#endif // RUBY_API_VERSION_MINOR == 8
396-
#endif // RUBY_API_VERSION_MAJOR == 1
444+
#endif // RUBY_API_VERSION_MAJOR == 1

binding-mri/filesystem-binding.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ kernelLoadDataInt(const char *filename, bool rubyExc)
132132
VALUE port = fileIntForPath(filename, rubyExc);
133133

134134
VALUE marsh = rb_const_get(rb_cObject, rb_intern("Marshal"));
135+
135136
// FIXME need to catch exceptions here with begin rescue
136137
VALUE result = rb_funcall2(marsh, rb_intern("load"), 1, &port);
137138

0 commit comments

Comments
 (0)