From 417356c3cb1be379806f0754b6eb98b0eff32d3d Mon Sep 17 00:00:00 2001 From: jj Date: Fri, 20 Apr 2012 18:20:24 +0200 Subject: [PATCH] ruby: add df-linked-list support (also, xml could be simplified) --- plugins/ruby/codegen.pl | 11 ++++++---- plugins/ruby/ruby-memstruct.rb | 38 +++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/plugins/ruby/codegen.pl b/plugins/ruby/codegen.pl index 72ef06156..82bff1137 100755 --- a/plugins/ruby/codegen.pl +++ b/plugins/ruby/codegen.pl @@ -309,10 +309,13 @@ sub render_item_container { my $subtype = $item->getAttribute('ld:subtype'); my $rbmethod = join('_', split('-', $subtype)); my $tg = $item->findnodes('child::ld:item')->[0]; - # df-linked-list has no list[0] - if ($tg and $rbmethod ne 'df_linked_list') { - my $tglen = get_tglen($tg, $cppvar); - push @lines_rb, "$rbmethod($tglen) {"; + if ($tg) { + if ($rbmethod eq 'df_linked_list') { + push @lines_rb, "$rbmethod {"; + } else { + my $tglen = get_tglen($tg, $cppvar); + push @lines_rb, "$rbmethod($tglen) {"; + } indent_rb { render_item($tg, "${cppvar}[0]"); }; diff --git a/plugins/ruby/ruby-memstruct.rb b/plugins/ruby/ruby-memstruct.rb index 54bf18669..bc387de68 100644 --- a/plugins/ruby/ruby-memstruct.rb +++ b/plugins/ruby/ruby-memstruct.rb @@ -2,7 +2,7 @@ module DFHack module MemHack class MemStruct attr_accessor :_memaddr - def _at(addr) ; @_memaddr = addr ; dup ; end + def _at(addr) ; d = dup ; d._memaddr = addr ; d ; end def _get ; self ; end def inspect ; _get.inspect ; end end @@ -68,7 +68,7 @@ class Compound < MemStruct DfArray.new(tglen, (yield if tglen)) end def df_linked_list - DfLinkedList.new((yield if block_given?)) + DfLinkedList.new(yield) end def global(glob) @@ -474,13 +474,41 @@ class DfArray < Compound def each ; (0...length).each { |i| yield self[i] } ; end def inspect ; to_a.inspect ; end end -class DfLinkedList < MemStruct +class DfLinkedList < Compound attr_accessor :_tg def initialize(tg) @_tg = tg end - # TODO - def inspect ; "#" ; end + + field(:_ptr, 0) { number 32, false } + field(:_prev, 4) { number 32, false } + field(:_next, 8) { number 32, false } + + def item + addr = _ptr + return if addr == 0 + @_tg._at(addr)._get + end + + def item=(v) + addr = _ptr + raise 'null pointer' if addr == 0 + @_tg.at(addr)._set(v) + end + + def prev + addr = _prev + return if addr == 0 + _at(addr) + end + + def next + addr = _next + return if addr == 0 + _at(addr) + end + + def inspect ; "#" ; end end class Global < MemStruct