Module: FastRuby::DefnTranslator

Defined in:
lib/fastruby/translator/modules/defn.rb

Instance Method Summary collapse

Instance Method Details

#to_c_defn(tree, result_var = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fastruby/translator/modules/defn.rb', line 25

def to_c_defn(tree, result_var = nil)

  method_name = tree[1]
  args_tree = tree[2].select{|x| x.to_s[0] != ?&}
  
  global_klass_variable = add_global_name("VALUE", "Qnil");

  hash = Hash.new
  value_cast = ( ["VALUE"]*(args_tree.size+2) ).join(",")
  
  strmethodargs = "self,block,(VALUE)&frame"
  
  anonymous_method_name = anonymous_dispatcher(global_klass_variable, method_name)
  alt_options = options.dup

  alt_options.delete(:self)
  alt_options.delete(:main)

  code = "
  
  
    if (rb_obj_is_kind_of(plocals->self, rb_cClass) || rb_obj_is_kind_of(plocals->self, rb_cModule)) {
      rb_define_method(plocals->self, #{method_name.to_s.inspect}, #{anonymous_method_name}, -1);
      
      #{global_klass_variable} = plocals->self;
      // set tree
      rb_funcall(#{literal_value FastRuby}, #{intern_num :set_tree}, 5,
              #{global_klass_variable},
              rb_str_new2(#{method_name.to_s.inspect}),
              #{literal_value tree},
              #{literal_value snippet_hash},
              #{literal_value alt_options}
  
              );
      
    } else {
      VALUE obj = plocals->self;
      rb_define_singleton_method(obj, #{method_name.to_s.inspect}, #{anonymous_method_name}, -1 );
      
      #{global_klass_variable} = CLASS_OF(obj);
      // set tree
      rb_funcall(#{literal_value FastRuby}, #{intern_num :set_tree}, 5,
              #{global_klass_variable},
              rb_str_new2(#{method_name.to_s.inspect}),
              #{literal_value tree},
              #{literal_value snippet_hash},
              #{literal_value alt_options}
  
              );
    }
    
    "

  if result_var
    code + "\n#{result_var} = Qnil;"
  else
    inline_block code + "\nreturn Qnil;\n"
  end
end

#to_c_defs(tree, result_var = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/fastruby/translator/modules/defn.rb', line 85

def to_c_defs(tree, result_var = nil)
  method_name = tree[2]
  args_tree = tree[3].select{|x| x.to_s[0] != ?&}
  
  global_klass_variable = add_global_name("VALUE", "Qnil");

  hash = Hash.new
  value_cast = ( ["VALUE"]*(args_tree.size+2) ).join(",")
  
  strmethodargs = "self,block,(VALUE)&frame"
  
  anonymous_method_name = anonymous_dispatcher(global_klass_variable, method_name)

  alt_options = options.dup

  alt_options.delete(:self)
  alt_options.delete(:main)

  code = "
  
    VALUE obj = Qnil;
    #{to_c tree[1], "obj"};
    rb_define_singleton_method(obj, #{method_name.to_s.inspect}, #{anonymous_method_name}, -1 );
    
    #{global_klass_variable} = CLASS_OF(obj);
    // set tree
    rb_funcall(#{literal_value FastRuby}, #{intern_num :set_tree}, 5,
            #{global_klass_variable},
            rb_str_new2(#{method_name.to_s.inspect}),
            #{literal_value tree},
            #{literal_value snippet_hash},
            #{literal_value alt_options}

            );

    "

  if result_var
    code + "\n#{result_var} = Qnil;"
  else
    inline_block code + "\nreturn Qnil;\n"
  end
    
end

#to_c_scope(tree, result_var = nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/fastruby/translator/modules/defn.rb', line 130

def to_c_scope(tree, result_var = nil)
  if tree[1]
    if result_var
      to_c(tree[1], result_var)
    else
      to_c(tree[1])
    end
  else
    "Qnil"
  end
end