From c64000606968a532cc188ee52a613de91cf406bd Mon Sep 17 00:00:00 2001 From: jj Date: Fri, 28 Oct 2016 16:40:14 +0200 Subject: [PATCH] ruby: some more updates for x64, fix df_flagarray size --- plugins/ruby/codegen.pl | 12 +++++--- plugins/ruby/ruby.cpp | 65 ++++++++++++----------------------------- plugins/ruby/ruby.rb | 5 ++-- 3 files changed, 30 insertions(+), 52 deletions(-) diff --git a/plugins/ruby/codegen.pl b/plugins/ruby/codegen.pl index 3a9db2c61..3f5ea4ffe 100755 --- a/plugins/ruby/codegen.pl +++ b/plugins/ruby/codegen.pl @@ -577,10 +577,14 @@ sub render_global_objects { # define friendlier accessors, eg df.world -> DFHack::GlobalObjects.new._at(0).world indent_rb { push @lines_rb, "Global = GlobalObjects.new._at(0)"; - for my $obj (@global_objects) + for my $oname (@global_objects) { - push @lines_rb, "def self.$obj ; Global.$obj ; end"; - push @lines_rb, "def self.$obj=(v) ; Global.$obj = v ; end"; + push @lines_rb, "if DFHack.get_global_address('$oname') != 0"; + indent_rb { + push @lines_rb, "def self.$oname ; Global.$oname ; end"; + push @lines_rb, "def self.$oname=(v) ; Global.$oname = v ; end"; + }; + push @lines_rb, "end"; } }; } @@ -743,7 +747,7 @@ sub sizeof { } elsif ($subtype eq 'df-linked-list') { return 3 * $SIZEOF_PTR; } elsif ($subtype eq 'df-flagarray') { - return 4 + $SIZEOF_PTR; + return 2 * $SIZEOF_PTR; # XXX length may be 4 on windows? } elsif ($subtype eq 'df-static-flagarray') { return $field->getAttribute('count'); } elsif ($subtype eq 'df-array') { diff --git a/plugins/ruby/ruby.cpp b/plugins/ruby/ruby.cpp index 97a4a4553..f9368fdf6 100644 --- a/plugins/ruby/ruby.cpp +++ b/plugins/ruby/ruby.cpp @@ -214,17 +214,13 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) if (!onupdate_active) return CR_OK; - using namespace df::global; - - if (cur_year && (*cur_year < onupdate_minyear)) + if (df::global::cur_year && *df::global::cur_year < onupdate_minyear) return CR_OK; - if (cur_year && cur_year_tick && onupdate_minyeartick >= 0 && - (*cur_year == onupdate_minyear && - *cur_year_tick < onupdate_minyeartick)) + if (df::global::cur_year_tick && onupdate_minyeartick >= 0 && + *df::global::cur_year_tick < onupdate_minyeartick) return CR_OK; - if (cur_year && cur_year_tick_advmode && onupdate_minyeartickadv >= 0 && - (*cur_year == onupdate_minyear && - *cur_year_tick_advmode < onupdate_minyeartickadv)) + if (df::global::cur_year_tick_advmode && onupdate_minyeartickadv >= 0 && + *df::global::cur_year_tick_advmode < onupdate_minyeartickadv) return CR_OK; return plugin_eval_ruby(out, "DFHack.onupdate"); @@ -283,21 +279,13 @@ static command_result df_rubyeval(color_ostream &out, std::vector // - ruby.h with gcc -m32 on linux 64 is broken // so we dynamically load libruby with dlopen/LoadLibrary // lib path is hardcoded here, and by default downloaded by cmake -// this code should work with ruby1.9, but ruby1.9 doesn't like running -// in a dedicated non-main thread, so use ruby1.8 binaries only for now typedef uintptr_t VALUE; typedef uintptr_t ID; -static struct { - int major; - int minor; - int teeny; -} libruby_version; - -static VALUE Qfalse; -static VALUE Qtrue; -static VALUE Qnil; +static VALUE Qfalse = 0; +static VALUE Qtrue = 2; +static VALUE Qnil = 4; #define INT2FIX(i) ((VALUE)((((intptr_t)i) << 1) | 1)) #define FIX2INT(i) (((intptr_t)i) >> 1) @@ -345,26 +333,6 @@ static int df_loadruby(void) return 0; } - const char *ruby_version = (const char*)LookupPlugin(libruby_handle, "ruby_version"); - if (!ruby_version) - return 0; - sscanf(ruby_version, "%d.%d.%d", - &libruby_version.major, &libruby_version.minor, &libruby_version.teeny); - - if (libruby_version.major >= 2 && sizeof(VALUE) >= sizeof(double)) - { - // USE_FLONUM defined on x64 - Qfalse = (VALUE)0; - Qtrue = (VALUE)0x14; - Qnil = (VALUE)0x08; - } - else - { - Qfalse = (VALUE)0; - Qtrue = (VALUE)2; - Qnil = (VALUE)4; - } - // ruby_sysinit is optional (ruby1.9 only) ruby_sysinit = (decltype(ruby_sysinit))LookupPlugin(libruby_handle, "ruby_sysinit"); #define rbloadsym(s) if (!(s = (decltype(s))LookupPlugin(libruby_handle, #s))) return 0 @@ -465,6 +433,11 @@ static void df_rubythread(void *p) r_result = CR_OK; r_type = RB_IDLE; + // initialize ruby constants (may depend on libruby compilation flags/version) + Qnil = rb_eval_string_protect("nil", &state); + Qtrue = rb_eval_string_protect("true", &state); + Qfalse = rb_eval_string_protect("false", &state); + // load the default ruby-level definitions in the background state=0; rb_eval_string_protect("require './hack/ruby/ruby'", &state); @@ -1085,8 +1058,8 @@ static VALUE rb_dfmemory_set_clear(VALUE self, VALUE set) /* call an arbitrary object virtual method */ #if defined(_WIN32) && !defined(_WIN64) -__declspec(naked) static int raw_vcall(void *that, void *fptr, unsigned long a0, - unsigned long a1, unsigned long a2, unsigned long a3, unsigned long a4, unsigned long a5) +__declspec(naked) static intptr_t raw_vcall(void *that, void *fptr, uintptr_t a0, + uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5) { // __thiscall requires that the callee cleans up the stack // here we dont know how many arguments it will take, so @@ -1112,11 +1085,11 @@ __declspec(naked) static int raw_vcall(void *that, void *fptr, unsigned long a0, } } #else -static int raw_vcall(void *that, void *fptr, unsigned long a0, - unsigned long a1, unsigned long a2, unsigned long a3, unsigned long a4, unsigned long a5) +static intptr_t raw_vcall(void *that, void *fptr, uintptr_t a0, + uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5) { - int (*t_fptr)(void *me, unsigned long, unsigned long, unsigned long, - unsigned long, unsigned long, unsigned long); + intptr_t (*t_fptr)(void *me, uintptr_t, uintptr_t, uintptr_t, + uintptr_t, uintptr_t, uintptr_t); t_fptr = (decltype(t_fptr))fptr; return t_fptr(that, a0, a1, a2, a3, a4, a5); } diff --git a/plugins/ruby/ruby.rb b/plugins/ruby/ruby.rb index 7709276c8..c696c23e5 100644 --- a/plugins/ruby/ruby.rb +++ b/plugins/ruby/ruby.rb @@ -109,13 +109,14 @@ module DFHack def onupdate @onupdate_list ||= [] - y = cur_year + y = yt = 0 + y = cur_year rescue 0 ytmax = TICKS_PER_YEAR if df.gamemode == :ADVENTURE and df.respond_to?(:cur_year_tick_advmode) yt = cur_year_tick_advmode ytmax *= 144 else - yt = cur_year_tick + yt = cur_year_tick rescue 0 end @onupdate_list.each { |o|