Module: FastRuby::BlockTranslator

Defined in:
lib/fastruby/translator/modules/block.rb,
lib/fastruby/translator/modules/method_group.rb

Instance Method Summary collapse

Instance Method Details

#to_c_block(tree) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fastruby/translator/modules/block.rb', line 89

def to_c_block(tree)
  if tree.size == 1
    return inline_block("return Qnil;")
  end

  str = ""
  str = tree[1..-2].map{ |subtree|
    to_c(subtree)
  }.join(";")

  if tree[-1]

    if tree[-1][0] != :return
      str = str + ";last_expression = #{to_c(tree[-1])};"
    else
      str = str + ";#{to_c(tree[-1])};"
    end
  end

  str << "return last_expression;"

  inline_block str
end

#to_c_class(tree) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fastruby/translator/modules/method_group.rb', line 25

def to_c_class(tree)
  str_class_name = get_class_name(tree[1])
  container_tree = get_container_tree(tree[1])

  if container_tree == s(:self)
    method_group("
                VALUE tmpklass = rb_define_class(
                  #{str_class_name.inspect},
                  #{tree[2] ? to_c(tree[2]) : "rb_cObject"}
              );
    ", tree[3])
  else
    method_group("
                VALUE container_klass = #{to_c(container_tree)};
                VALUE tmpklass = rb_define_class_under(
                  container_klass,
                  #{str_class_name.inspect},
                  #{tree[2] ? to_c(tree[2]) : "rb_cObject"}
              );
    ", tree[3])
  end
end

#to_c_module(tree) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fastruby/translator/modules/method_group.rb', line 48

def to_c_module(tree)
  str_class_name = get_class_name(tree[1])
  container_tree = get_container_tree(tree[1])

  if container_tree == s(:self)
    method_group("
                  VALUE tmpklass = rb_define_module(#{str_class_name.inspect});
    ", tree[2])
  else
    method_group("
                  VALUE container_klass = #{to_c(container_tree)};
                  VALUE tmpklass = rb_define_module_under(container_klass,#{str_class_name.inspect});
    ", tree[2])
  end
end

#to_c_yield(tree) ⇒ Object



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
84
85
86
87
# File 'lib/fastruby/translator/modules/block.rb', line 26

def to_c_yield(tree)

  block_code = proc { |name| "
    static VALUE #{name}(VALUE frame_param, VALUE* block_args, int size) {

      #{@locals_struct} *plocals;
      #{@frame_struct} *pframe;
      pframe = (void*)frame_param;
      plocals = (void*)pframe->plocals;

      if (FIX2LONG(plocals->block_function_address) == 0) {
        #{_raise("rb_eLocalJumpError", "no block given")};
      } else {
        return ((VALUE(*)(int,VALUE*,VALUE,VALUE))FIX2LONG(plocals->block_function_address))(size, block_args, FIX2LONG(plocals->block_function_param), (VALUE)pframe);
      }
    }
  "
  }

  splat_arg = tree.find{|x| x == :yield ? false : x[0] == :splat}
  ret = nil      
  if splat_arg
    ret = inline_block "
      VALUE splat_array = #{to_c(splat_arg[1])};
      
      if (CLASS_OF(splat_array) == rb_cArray) {
        VALUE block_args[RARRAY(splat_array)->len + #{tree.size}];
        int i;
        #{ 
          (0..tree.size-3).map{|i|
            "block_args[#{i}] = #{to_c(tree[i+1])}"
          }.join(";\n")
        };
        
        for (i=0; i<RARRAY(splat_array)->len; i++) {
          block_args[i+#{tree.size-2}] = rb_ary_entry(splat_array,i);
        }
        
        return #{anonymous_function(&block_code)}((VALUE)pframe, block_args, RARRAY(splat_array)->len + #{tree.size-2});
      } else {
        VALUE block_args[1+#{tree.size}];
        #{ 
          (0..tree.size-3).map{|i|
            "block_args[#{i}] = #{to_c(tree[i+1])}"
          }.join(";\n")
        };
        
        block_args[#{tree.size-2}] = splat_array;
        return #{anonymous_function(&block_code)}((VALUE)pframe, block_args, #{tree.size-1});
      }
      
    "
  else
    ret = if tree.size > 1
        anonymous_function(&block_code)+"((VALUE)pframe, (VALUE[]){#{tree[1..-1].map{|subtree| to_c subtree}.join(",")}},#{tree.size-1})"
      else
        anonymous_function(&block_code)+"((VALUE)pframe, (VALUE[]){}, #{tree.size-1})"
      end
  end
  
  protected_block(ret, false)
end