2012-05-06 51 views
0

我很難搞清楚如何在c中重寫rb_函數(如rb_ivar_get)。 我有以下代碼:如何覆蓋ruby c擴展中的rb_函數?

#include "ruby.h" 

void Init_metaobject(); 
VALUE meta_cObject = Qnil; 
VALUE meta_ivar_get(VALUE obj, VALUE mId, VALUE mWarn); 
VALUE meta_ivar_set(VALUE obj, VALUE mId, VALUE val); 

void Init_metaobject() { 
    meta_cObject = rb_define_class("MetaObject", rb_cObject); 
    rb_define_method(meta_cObject, "meta_ivar_get", meta_ivar_get, 2); 
    rb_define_method(meta_cObject, "meta_ivar_set", meta_ivar_set, 2); 
} 

VALUE 
rb_ivar_get(obj, id) 
    VALUE obj; 
    ID id; 
{ 
    return meta_ivar_get(obj, ID2SYM(id), Qtrue); 
} 

VALUE 
rb_attr_get(obj, id) 
    VALUE obj; 
    ID id; 
{ 
    return meta_ivar_get(obj, ID2SYM(id), Qfalse); 
} 

VALUE 
rb_ivar_set(obj, id, val) 
    VALUE obj; 
    ID id; 
    VALUE val; 
{ 
    return meta_ivar_set(obj, ID2SYM(id), val); 
} 

VALUE 
meta_ivar_get(obj, mId, mWarn) 
    VALUE obj; 
    VALUE mId; 
    VALUE mWarn; 
{ 
    VALUE val; 
    ID id = rb_to_id(id); 
    int warn = RTEST(warn); 

    switch (TYPE(obj)) { 
     case T_OBJECT: 
     case T_CLASS: 
     case T_MODULE: 
    if (ROBJECT(obj)->iv_tbl && st_lookup(ROBJECT(obj)->iv_tbl, id, &val)) 
     return val; 
    break; 
     default: 
    if (FL_TEST(obj, FL_EXIVAR) || rb_special_const_p(obj)) 
     return generic_ivar_get(obj, id, warn); 
    break; 
    } 
    if (warn) { 
    rb_warning("instance variable %s not initialized", rb_id2name(id)); 
    } 
    return Qnil; 
} 

VALUE 
meta_ivar_set(obj, mId, val) 
    VALUE obj; 
    VALUE mId; 
    VALUE val; 
{ 
    ID id = rb_to_id(mId); 
    if (!OBJ_TAINTED(obj) && rb_safe_level() >= 4) 
    rb_raise(rb_eSecurityError, "Insecure: can't modify instance variable"); 
    if (OBJ_FROZEN(obj)) rb_error_frozen("object"); 
    switch (TYPE(obj)) { 
     case T_OBJECT: 
     case T_CLASS: 
     case T_MODULE: 
    if (!ROBJECT(obj)->iv_tbl) ROBJECT(obj)->iv_tbl = st_init_numtable(); 
    st_insert(ROBJECT(obj)->iv_tbl, id, val); 
    break; 
     default: 
    generic_ivar_set(obj, id, val); 
    break; 
    } 
    return val; 
} 

而且下面的測試:

require 'metaobject' 

class Tracker < MetaObject 

    attr_accessor :ivar 

    def initialize 
    @ivar = nil 
    end 

    def meta_ivar_get(symbol, warn) 
    puts "Instance variable, #{symbol}, retrieved" 
    super(symbol, warn) 
    end 

    def meta_ivar_set(symbol, obj) 
    puts "Instance variable, #{symbol}, changed to #{obj.inspect}" 
    super(symbol, obj) 
    end 

end 

obj = Tracker.new 
obj.ivar = "Modified" 
puts obj.ivar 

的輸出是唯一的:

Modified 

我的想法是,紅寶石接頭面紗我定義rb_ivar_get,rb_attr_get和rb_ivar_set,其定義在variables.c中找到。我對嗎?如果是這樣,我怎麼能改變它,我的方法面紗紅寶石,而不是其他方式。

回答

2

你不能用一個額外的.so文件來做到這一點。編輯內部Ruby函數的唯一方法是直接更改它。轉到variable.c,編輯它並重新編譯整個解釋器。您可以改爲覆蓋attr_accessor

EDIT

set_trace_func另一種解決方案。這是非常緩慢的,我不厚,這是正確的做法。總之,這裏是:

$instance_variables_table = {} 
$instance_variable_created_proc = proc do |var, value| 
    puts "Instance variable #{var} created with #{value.inspect}." 
end 
$instance_variable_changed_proc = proc do |var, new, old| 
    puts "Instance variable #{var} changed from #{old.inspect} to #{new.inspect}." 
end 

set_trace_func(proc {|type, file, line, func, binding, mod| 
    unless type == "call" 
    eval("instance_variables", binding).each do |iv| 
     value = eval("instance_variable_get(:#{iv})", binding) 
     if $instance_variables_table.has_key? iv 
     if $instance_variables_table[iv] != value 
      new = value 
      old = $instance_variables_table[iv] 
      $instance_variable_changed_proc[iv, new, old] 
     end 
     else 
     $instance_variable_created_proc[iv, value] 
     end 
    end 
    end 
    $instance_variables_table = {} 
    eval("instance_variables", binding).each do |iv| 
    $instance_variables_table[iv] = eval("instance_variable_get(:#{iv})", binding) 
    end 
}) 

測試代碼:

class A 
    def initialize 
    @test = 1 
    @test = 2 
    end 
    def a 
    @test = 3 
    end 
end 
A.new.a 

輸出:

Instance variable @test created with 1. 
Instance variable @test changed from 1 to 2. 
Instance variable @test changed from 2 to 3. 

我不知道這是否適用於所有的情況下,或者如果它可以簡化。如果您想在真實應用程序中執行此操作,請編輯variable.c

+0

我不能覆蓋'attr_accessor',因爲對我來說,添加這個擴展的主要目的是爲了能夠直接跟蹤修改'@ivar =「some value」'調用。所以你說爲了做到這一點,我將不得不編輯和重新編譯源代碼,沒有其他辦法? – tophat

+0

據我所知,沒有。 –

+0

見版本。 –