Module: Ruby2CExtension::CommonNodeComp

Includes:
Tools::EnsureNodeTypeMixin
Included in:
Ruby2CExtension::CFunction::Base
Defined in:
lib/ruby2cext/common_node_comp.rb

Constant Summary collapse

NON_ITER_PROC =
proc { |str, args, arg_types| str % args }

Instance Method Summary collapse

Methods included from Tools::EnsureNodeTypeMixin

#ensure_node_type

Instance Method Details

#build_args(args, one_extra = false) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/ruby2cext/common_node_comp.rb', line 207

def build_args(args, one_extra = false)
	if args.first == :array
		l "const int argc = #{args.last.size};"
		build_c_arr(args.last, "argv", one_extra ? 1 : 0)
	else
		l "int argc; VALUE *argv;"
		l "volatile VALUE argv_ary;" if in_while?
		assign_res(comp(args))
		l "if (TYPE(res) != T_ARRAY) res = rb_ary_to_ary(res);"
		l "argc = RARRAY(res)->len;"
		if in_while?
			# don't use ALLOCA_N in a while loop to avoid a stack overflow
			l "argv_ary = rb_ary_dup(res);"
			l "rb_ary_push(argv_ary, Qnil);" if one_extra
			l "argv = RARRAY(argv_ary)->ptr;"
		else
			l "argv = ALLOCA_N(VALUE, argc#{one_extra ? " + 1" : ""});"
			l "MEMCPY(argv, RARRAY(res)->ptr, VALUE, argc);"
		end
	end
end

#build_c_arr(arr, var_name, extra = 0) ⇒ Object



201
202
203
204
205
206
# File 'lib/ruby2cext/common_node_comp.rb', line 201

def build_c_arr(arr, var_name, extra = 0)
	l "VALUE #{var_name}[#{arr.size + extra}];"
	arr.each_with_index { |n, i|
		l "#{var_name}[#{i}] = #{comp(n)};"
	}
end

#c_elseObject



39
40
41
42
43
# File 'lib/ruby2cext/common_node_comp.rb', line 39

def c_else
	l "else {"
	yield
	l "}"
end

#c_for(exprs) ⇒ Object



44
45
46
47
48
# File 'lib/ruby2cext/common_node_comp.rb', line 44

def c_for(exprs)
	l "for (#{exprs}) {"
	yield
	l "}"
end

#c_if(cond) ⇒ Object



34
35
36
37
38
# File 'lib/ruby2cext/common_node_comp.rb', line 34

def c_if(cond)
	l "if (#{cond}) {"
	yield
	l "}"
end

#c_scopeObject



22
23
24
25
26
# File 'lib/ruby2cext/common_node_comp.rb', line 22

def c_scope
	l "{"
	yield
	l "}"
end

#c_scope_resObject



27
28
29
30
31
32
# File 'lib/ruby2cext/common_node_comp.rb', line 27

def c_scope_res
	l "{"
	assign_res(yield)
	l "}"
	"res"
end

#c_static_onceObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby2cext/common_node_comp.rb', line 50

def c_static_once
	c_scope_res {
		l "static VALUE static_once_value = Qundef;"
		c_if("static_once_value == Qundef") {
			assign_res(yield)
			# other thread might have been faster
			c_if("static_once_value == Qundef") {
				l "static_once_value = res;"
				l "rb_global_variable(&static_once_value);"
			}
		}
		"static_once_value"
	}
end

#comp(node) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ruby2cext/common_node_comp.rb', line 146

def comp(node)
	case node
	when false
		"Qnil"
	when String
		node
	else
		while (ntype = node.first) == :newline
			node = node.last[:next]
		end
		orig_node = node
		l "/* #{ntype} */"
		begin
			if (pps = compiler.preprocessors_for(ntype))
				# apply each preprocessor until one returns a string or
				# a different node type
				pps.each { |pp_proc|
					node = pp_proc[self, node]
					break unless Array === node && node.first == ntype
				}
			end
			if Array === node && node.first == ntype
				__send__("comp_#{ntype}", node.last)
			else
				# retry with the result of preprocessing
				comp(node)
			end
		rescue Ruby2CExtError => e
			if Hash === orig_node.last && (n = orig_node.last[:node])
				# add file and line to message
				raise "#{n.file}:#{n.line}: #{e}"
			else
				raise # reraise
			end
		end
	end
end

#comp_alias(hash) ⇒ Object



1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
# File 'lib/ruby2cext/common_node_comp.rb', line 1255

def comp_alias(hash)
	if Array === hash[:new] # 1.8.5
		c_scope {
			l "ID new_id;"
			l "new_id = rb_to_id(#{comp(hash[:new])});"
			l "rb_alias(#{get_class}, new_id, rb_to_id(#{comp(hash[:old])}));"
		}
	else
		l "rb_alias(#{get_class}, #{sym(hash[:new])}, #{sym(hash[:old])});"
	end
	"Qnil"
end

#comp_and(hash) ⇒ Object



726
727
728
729
730
731
732
# File 'lib/ruby2cext/common_node_comp.rb', line 726

def comp_and(hash)
	assign_res(comp(hash[:first]))
	c_if("RTEST(res)") {
		assign_res(comp(hash[:second]))
	}
	"res"
end

#comp_argscat(hash) ⇒ Object



342
343
344
345
346
347
348
349
350
# File 'lib/ruby2cext/common_node_comp.rb', line 342

def comp_argscat(hash)
	c_scope_res {
		l "VALUE head, body;"
		l "head = #{comp(hash[:head])};"
		l "body = #{comp(hash[:body])};"
		l "body = (NIL_P(body) ? rb_ary_new3(1, Qnil) : rb_Array(body));"
		"rb_ary_concat(head, body)"
	}
end

#comp_argspush(hash) ⇒ Object



351
352
353
354
355
356
357
358
# File 'lib/ruby2cext/common_node_comp.rb', line 351

def comp_argspush(hash)
	# argspush is used in a[2,*a]=4 for example
	c_scope_res {
		l "VALUE head;"
		l "head = rb_ary_dup(#{comp(hash[:head])});"
		"rb_ary_push(head, #{comp(hash[:body])})"
	}
end

#comp_array(arr) ⇒ Object



1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
# File 'lib/ruby2cext/common_node_comp.rb', line 1133

def comp_array(arr)
	c_scope_res {
		l "VALUE ary = rb_ary_new2(#{arr.size});"
		arr.each_with_index { |n, i|
			l "RARRAY(ary)->ptr[#{i}] = #{comp(n)};"
			l "RARRAY(ary)->len = #{i+1};"
		}
		"ary"
	}
end

#comp_attrasgn(hash) ⇒ Object



258
259
260
261
262
263
264
265
266
267
# File 'lib/ruby2cext/common_node_comp.rb', line 258

def comp_attrasgn(hash)
	fun = "rb_funcall#{hash[:recv] == 0 ? 2 : 3}"
	recv = (hash[:recv] == 0 ? get_self : comp(hash[:recv]))
	c_scope_res {
		l "VALUE recv = #{recv};"
		build_args(hash[:args])
		l "#{fun}(recv, #{sym(hash[:mid])}, argc, argv);"
		"argv[argc - 1]"
	}
end

#comp_back_ref(hash) ⇒ Object



1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
# File 'lib/ruby2cext/common_node_comp.rb', line 1118

def comp_back_ref(hash)
	case hash[:nth]
	when ?&
		"rb_reg_last_match(rb_backref_get())"
	when ?`
		"rb_reg_match_pre(rb_backref_get())"
	when ?'
		"rb_reg_match_post(rb_backref_get())"
	when ?+
		"rb_reg_match_last(rb_backref_get())"
	else
		raise Ruby2CExtError, "unexpected back-ref type: '#{hash[:nth].chr}'"
	end
end

#comp_begin(hash) ⇒ Object



360
361
362
# File 'lib/ruby2cext/common_node_comp.rb', line 360

def comp_begin(hash)
	comp(make_block(hash[:body]))
end

#comp_block(exprs) ⇒ Object



184
185
186
187
188
189
190
191
# File 'lib/ruby2cext/common_node_comp.rb', line 184

def comp_block(exprs)
	return "Qnil" if exprs.empty?
	last = exprs.last
	exprs[0..-2].each { |ex|
		l "#{comp(ex)};"
	}
	comp(last)
end

#comp_block_pass(hash) ⇒ Object



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/ruby2cext/common_node_comp.rb', line 523

def comp_block_pass(hash)
	# TODO: is just a workaround/hack, does not work with instance_eval etc.
	c_scope_res {
		l "VALUE proc;"
		l "proc = #{comp(hash[:body])};"
		iter = hash[:iter]
		c_if("NIL_P(proc)") {
			assign_res(__send__("comp_#{iter.first}", iter.last))
		}
		c_else {
			# rb_obj_is_proc is static in eval.c, so we just convert and hope the best...
			add_helper <<-EOC
				#define PROC_TSHIFT (FL_USHIFT+1)
				#define PROC_TMASK  (FL_USER1|FL_USER2|FL_USER3)
				static VALUE obj_to_proc(VALUE proc) {
					VALUE tmp;
					tmp = rb_check_convert_type(proc, T_DATA, "Proc", "to_proc");
					if (rb_class_real(CLASS_OF(tmp)) != rb_cProc)
						rb_raise(rb_eTypeError, "wrong argument type %s (expected Proc)", rb_obj_classname(proc));
					proc = tmp;
					if (ruby_safe_level >= 1 && OBJ_TAINTED(proc) &&
						ruby_safe_level > ((RBASIC(proc)->flags & PROC_TMASK) >> PROC_TSHIFT))
						rb_raise(rb_eSecurityError, "Insecure: tainted block value");
					return proc;
				}
			EOC
			l "proc = obj_to_proc(proc);"
			add_helper <<-EOC
				static VALUE block_pass_helper_block(VALUE bl_val, VALUE proc, VALUE self) {
					if (ruby_current_node->nd_state != 1) {
						if (bl_val == Qundef) bl_val = rb_ary_new2(0);
						else {
							VALUE tmp = rb_check_array_type(bl_val);
							bl_val = (NIL_P(tmp) ? rb_ary_new3(1, bl_val) : tmp);
						}
					}
					if (RARRAY(bl_val)->len == 0) return rb_funcall3(proc, #{sym(:call)}, 0, 0);
					else {
						int argc = RARRAY(bl_val)->len;
						VALUE *argv = ALLOCA_N(VALUE, argc);
						MEMCPY(argv, RARRAY(bl_val)->ptr, VALUE, argc);
						return rb_funcall3(proc, #{sym(:call)}, argc, argv);
					}
				}
				EOC
			assign_res(handle_iter(iter, "block_pass_helper_block", "proc"))
		}
		"res"
	}
end

#comp_break(hash) ⇒ Object



586
587
588
589
# File 'lib/ruby2cext/common_node_comp.rb', line 586

def comp_break(hash)
	# must be implemented by the "user" of CommonNodeComp
	raise Ruby2CExtError::NotSupported, "break is not supported"
end

#comp_call(hash, &iter_proc) ⇒ Object



254
255
256
# File 'lib/ruby2cext/common_node_comp.rb', line 254

def comp_call(hash, &iter_proc)
	do_funcall(comp(hash[:recv]), hash[:mid], hash[:args], false, iter_proc)
end

#comp_case(hash) ⇒ Object



450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/ruby2cext/common_node_comp.rb', line 450

def comp_case(hash)
	ensure_node_type(hash[:body], :when)
	c_scope_res {
		l "VALUE case_val;"
		l "case_val = #{comp(hash[:head])};"
		handle_when(hash[:body].last, proc { |val|
			# Ruby 1.8.4 actually uses rb_funcall2, but a :call node
			# (which uses rb_funcall3) is more correct
			comp([:call, {:mid => :===, :recv => val, :args => [:array, ["case_val"]]}])
		})
	}
end

#comp_cdecl(hash) ⇒ Object



1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
# File 'lib/ruby2cext/common_node_comp.rb', line 1043

def comp_cdecl(hash)
	c_scope_res {
		l "VALUE val;"
		l "val = #{comp(hash[:value])};"
		if Symbol === hash[:vid]
			l "rb_const_set(#{get_cbase}, #{sym(hash[:vid])}, val);"
		else
			l "rb_const_set(#{make_class_prefix(hash[:else])}, #{sym(hash[:else].last[:mid])}, val);"
		end
		"val"
	}
end

#comp_class(hash) ⇒ Object



1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
# File 'lib/ruby2cext/common_node_comp.rb', line 1272

def comp_class(hash)
	add_helper <<-EOC
		static VALUE class_prep(VALUE prefix, VALUE super, ID cname) {
			VALUE klass;
			if (rb_const_defined_at(prefix, cname)) {
				klass = rb_const_get_at(prefix, cname);
				if (TYPE(klass) != T_CLASS) rb_raise(rb_eTypeError, "%s is not a class", rb_id2name(cname));
				if (super) {
					VALUE tmp = rb_class_real(RCLASS(klass)->super);
					if (tmp != super) rb_raise(rb_eTypeError, "superclass mismatch for class %s", rb_id2name(cname));
				}
				if (ruby_safe_level >= 4) rb_raise(rb_eSecurityError, "extending class prohibited");
			}
			else {
				if (!super) super = rb_cObject;
				klass = rb_define_class_id(cname, super);
				rb_set_class_path(klass, prefix, rb_id2name(cname));
				rb_const_set(prefix, cname, klass);
				rb_class_inherited(super, klass);
			}
			return klass;
		}
	EOC
	sup = hash[:super]
	c_scope_res {
		l "VALUE prefix, tmp_class;"
		l "VALUE super;" if sup
		l "prefix = #{make_class_prefix(hash[:cpath])};"
		l "super = #{comp(sup)};" if sup
		l "tmp_class = class_prep(prefix, #{sup ? "super" : "0"}, #{sym(hash[:cpath].last[:mid])});"
		CFunction::ClassModuleScope.compile(self, hash[:body], "tmp_class", true)
	}
end

#comp_colon2(hash) ⇒ Object



1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
# File 'lib/ruby2cext/common_node_comp.rb', line 1100

def comp_colon2(hash)
	mid = hash[:mid]
	if mid.to_s[0,1].downcase != mid.to_s[0,1] # then it is a constant
		helper_class_module_check
		assign_res(comp(hash[:head]))
		l "class_module_check(res);"
		"rb_const_get_from(res, #{sym(mid)})"
	else
		"rb_funcall(#{comp(hash[:head])}, #{sym(mid)}, 0, 0)"
	end
end

#comp_colon3(hash) ⇒ Object



1111
1112
1113
# File 'lib/ruby2cext/common_node_comp.rb', line 1111

def comp_colon3(hash)
	"rb_const_get_from(rb_cObject, #{sym(hash[:mid])})"
end

#comp_const(hash) ⇒ Object



1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
# File 'lib/ruby2cext/common_node_comp.rb', line 1076

def comp_const(hash)
	add_helper <<-EOC
		static VALUE const_get(ID id, NODE *cref) {
			NODE *cbase = cref;
			VALUE result;
			while (cbase && cbase->nd_next) {
				VALUE klass = cbase->nd_clss;
				while (RCLASS(klass)->iv_tbl && st_lookup(RCLASS(klass)->iv_tbl, id, &result)) {
					if (result == Qundef) {
						if (!RTEST(rb_autoload_load(klass, id))) break;
						continue;
					}
					return result;
				}
				cbase = cbase->nd_next;
			}
			return rb_const_get(cref->nd_clss, id);
		}
	EOC
	"const_get(#{sym(hash[:vid])}, #{get_cref})"
end

#comp_cvar(hash) ⇒ Object



1097
1098
1099
# File 'lib/ruby2cext/common_node_comp.rb', line 1097

def comp_cvar(hash)
	"rb_cvar_get(#{get_cvar_cbase}, #{sym(hash[:vid])})"
end

#comp_cvasgn(hash, decl = false) ⇒ Object



1055
1056
1057
1058
1059
# File 'lib/ruby2cext/common_node_comp.rb', line 1055

def comp_cvasgn(hash, decl = false)
	assign_res(comp(hash[:value]))
	l "rb_cvar_set(#{get_cvar_cbase}, #{sym(hash[:vid])}, res, Q#{decl});"
	"res"
end

#comp_cvdecl(hash) ⇒ Object



1060
1061
1062
# File 'lib/ruby2cext/common_node_comp.rb', line 1060

def comp_cvdecl(hash)
	comp_cvasgn(hash, true)
end

#comp_dasgn(hash) ⇒ Object



996
997
998
# File 'lib/ruby2cext/common_node_comp.rb', line 996

def comp_dasgn(hash)
	"(#{scope.get_dvar(hash[:vid])} = #{comp(hash[:value])})"
end

#comp_dasgn_curr(hash) ⇒ Object



999
1000
1001
# File 'lib/ruby2cext/common_node_comp.rb', line 999

def comp_dasgn_curr(hash)
	"(#{scope.get_dvar_curr(hash[:vid])} = #{comp(hash[:value])})"
end

#comp_defined(hash) ⇒ Object



1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
# File 'lib/ruby2cext/common_node_comp.rb', line 1347

def comp_defined(hash)
	head = hash[:head]
	hhash = head.last
	res =
	case head.first
	when :match2, :match3
		'"method"'
	when :yield
		'(rb_block_given_p() ? "yield" : 0)'
	when :self, :nil, :true, :false
		head.first.to_s.to_c_strlit
	when :op_asgn1, :op_asgn2, :masgn, :lasgn, :dasgn, :dasgn_curr,
		:gasgn, :iasgn, :cdecl, :cvdecl, :cvasgn  # :attrset can never be parsed
		'"assignment"'
	when :lvar
		'"local-variable"'
	when :dvar
		'"local-variable(in-block)"'
	when :gvar
		"(rb_gvar_defined(#{get_global_entry(hhash[:vid])}) ? \"global-variable\" : 0)"
	when :ivar
		"(rb_ivar_defined(#{get_self}, #{sym(hhash[:vid])}) ? \"instance-variable\" : 0)"
	when :const
		add_helper <<-EOC
			static VALUE const_defined(ID id, NODE *cref) {
				NODE *cbase = cref;
				VALUE result;
				while (cbase && cbase->nd_next) {
					VALUE klass = cbase->nd_clss;
					if (RCLASS(klass)->iv_tbl && st_lookup(RCLASS(klass)->iv_tbl, id, &result)) {
						if (result == Qundef && NIL_P(rb_autoload_p(klass, id))) return Qfalse;
						return Qtrue;
					}
					cbase = cbase->nd_next;
				}
				return rb_const_defined(cref->nd_clss, id);
			}
		EOC
		"(const_defined(#{sym(hhash[:vid])}, #{get_cref}) ? \"constant\" : 0)"
	when :cvar
		"(rb_cvar_defined(#{get_cvar_cbase}, #{sym(hhash[:vid])}) ? \"class variable\" : 0)"
	when :colon3
		"(rb_const_defined_from(rb_cObject, #{sym(hhash[:mid])}) ? \"constant\" : 0)"
	when :nth_ref
		"(RTEST(rb_reg_nth_defined(#{hhash[:nth]}, rb_backref_get())) ? \"$#{hhash[:nth]}\" : 0)"
	when :back_ref
		"(RTEST(rb_reg_nth_defined(0, rb_backref_get())) ? \"$#{hhash[:nth].chr}\" : 0)"
	else
		raise Ruby2CExtError::NotSupported, "defined? with node type #{head.first} is not supported"
	end
	if res[0,1] == '"' # just a string
		"rb_str_new2(#{res})"
	else
		c_scope_res {
			l "char * def_desc;"
			l "def_desc = #{res};"
			"(def_desc ? rb_str_new2(def_desc) : Qnil)"
		}
	end
end

#comp_defn(hash) ⇒ Object



1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
# File 'lib/ruby2cext/common_node_comp.rb', line 1212

def comp_defn(hash)
	add_helper <<-EOC
		static void class_nil_check(VALUE klass) {
		    if (NIL_P(klass)) rb_raise(rb_eTypeError, "no class/module to add method");
		}
	EOC
	l "class_nil_check(#{get_class});" # can happen in instance_eval for Fixnum/Symbol
	CFunction::Method.compile(self, hash[:defn], scope.vmode_def_fun, get_class, hash[:mid])
	"Qnil"
end

#comp_defs(hash) ⇒ Object



1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
# File 'lib/ruby2cext/common_node_comp.rb', line 1223

def comp_defs(hash)
	add_helper <<-EOC
		static void defs_allowed(VALUE recv, ID mid) {
			if (ruby_safe_level >= 4 && !OBJ_TAINTED(recv))
				rb_raise(rb_eSecurityError, "Insecure: can't define singleton method");
			if (OBJ_FROZEN(recv)) rb_error_frozen("object");
			if (ruby_safe_level >= 4) {
				NODE *body = 0;
				VALUE klass = rb_singleton_class(recv);
				if (st_lookup(RCLASS(klass)->m_tbl, mid, (st_data_t *)&body))
				rb_raise(rb_eSecurityError, "redefining method prohibited");
			}
		}
	EOC
	c_scope_res {
		l "VALUE recv;"
		l "recv = #{comp(hash[:recv])};"
		l "defs_allowed(recv, #{sym(hash[:mid])});"
		CFunction::Method.compile(self, hash[:defn], "rb_define_singleton_method", "recv", hash[:mid])
		"Qnil"
	}
end

#comp_dot2(hash) ⇒ Object



751
752
753
# File 'lib/ruby2cext/common_node_comp.rb', line 751

def comp_dot2(hash)
	handle_dot(hash, false)
end

#comp_dot3(hash) ⇒ Object



754
755
756
# File 'lib/ruby2cext/common_node_comp.rb', line 754

def comp_dot3(hash)
	handle_dot(hash, true)
end

#comp_dregx(hash) ⇒ Object



1199
1200
1201
1202
# File 'lib/ruby2cext/common_node_comp.rb', line 1199

def comp_dregx(hash)
	handle_dyn_str(hash)
	"rb_reg_new(RSTRING(res)->ptr, RSTRING(res)->len, #{hash[:cflag]})"
end

#comp_dregx_once(hash) ⇒ Object



1203
1204
1205
1206
1207
# File 'lib/ruby2cext/common_node_comp.rb', line 1203

def comp_dregx_once(hash)
	c_static_once {
		comp_dregx(hash)
	}
end

#comp_dstr(hash) ⇒ Object



1188
1189
1190
# File 'lib/ruby2cext/common_node_comp.rb', line 1188

def comp_dstr(hash)
	handle_dyn_str(hash)
end

#comp_dsym(hash) ⇒ Object



1195
1196
1197
1198
# File 'lib/ruby2cext/common_node_comp.rb', line 1195

def comp_dsym(hash)
	handle_dyn_str(hash)
	"rb_str_intern(res)"
end

#comp_dvar(hash) ⇒ Object



1067
1068
1069
# File 'lib/ruby2cext/common_node_comp.rb', line 1067

def comp_dvar(hash)
	scope.get_dvar(hash[:vid])
end

#comp_dxstr(hash) ⇒ Object



1191
1192
1193
1194
# File 'lib/ruby2cext/common_node_comp.rb', line 1191

def comp_dxstr(hash)
	handle_dyn_str(hash)
	"rb_funcall(#{get_self}, '`', 1, res)"
end

#comp_ensure(hash) ⇒ Object



712
713
714
715
716
717
718
719
720
721
722
723
724
# File 'lib/ruby2cext/common_node_comp.rb', line 712

def comp_ensure(hash)
	cflow_hash = {}
	b = CFunction::Wrap.compile(self, "ensure_body", cflow_hash) { |cf| cf.comp(hash[:head]) }
	e = CFunction::Wrap.compile(self, "ensure_ensure") { |cf| cf.comp(hash[:ensr]) } # ensr without cflow
	ensr_code = "rb_ensure(#{b}, (VALUE)#{get_wrap_ptr}, #{e}, (VALUE)#{get_wrap_ptr})"
	if cflow_hash.empty?
		ensr_code
	else
		assign_res(ensr_code)
		CFunction::Wrap::handle_wrap_cflow(self, cflow_hash)
		"res"
	end
end

#comp_evstr(hash) ⇒ Object



1172
1173
1174
# File 'lib/ruby2cext/common_node_comp.rb', line 1172

def comp_evstr(hash)
	"rb_obj_as_string(#{comp(hash[:body])})"
end

#comp_false(hash) ⇒ Object



338
# File 'lib/ruby2cext/common_node_comp.rb', line 338

def comp_false(hash); "Qfalse"; end

#comp_fcall(hash, &iter_proc) ⇒ Object



250
251
252
# File 'lib/ruby2cext/common_node_comp.rb', line 250

def comp_fcall(hash, &iter_proc)
	do_funcall(get_self, hash[:mid], hash[:args], true, iter_proc)
end

#comp_flip2(hash) ⇒ Object



781
782
783
# File 'lib/ruby2cext/common_node_comp.rb', line 781

def comp_flip2(hash)
	handle_flip(hash, false)
end

#comp_flip3(hash) ⇒ Object



784
785
786
# File 'lib/ruby2cext/common_node_comp.rb', line 784

def comp_flip3(hash)
	handle_flip(hash, true)
end

#comp_for(hash) ⇒ Object



574
575
576
577
578
579
# File 'lib/ruby2cext/common_node_comp.rb', line 574

def comp_for(hash)
	# transform to equivalent iter node
	hash = hash.dup
	hash[:iter] = [:call, {:args=>false, :mid=>:each, :recv=>hash[:iter]}]
	comp_iter(hash)
end

#comp_gasgn(hash) ⇒ Object



1002
1003
1004
1005
1006
# File 'lib/ruby2cext/common_node_comp.rb', line 1002

def comp_gasgn(hash)
	assign_res(comp(hash[:value]))
	l "rb_gvar_set(#{get_global_entry(hash[:vid])}, res);"
	"res"
end

#comp_gvar(hash) ⇒ Object



1070
1071
1072
# File 'lib/ruby2cext/common_node_comp.rb', line 1070

def comp_gvar(hash)
	"rb_gvar_get(#{get_global_entry(hash[:vid])})"
end

#comp_hash(hash) ⇒ Object



1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
# File 'lib/ruby2cext/common_node_comp.rb', line 1147

def comp_hash(hash)
	if (arr = hash[:head])
		ensure_node_type(arr, :array)
		arr = arr.last
		raise Ruby2CExtError, "odd number list for hash" unless arr.size % 2 == 0
		c_scope_res {
			l "VALUE key, hash = rb_hash_new();"
			arr.each_with_index { |n, i|
				if i % 2 == 0
					l "key = #{comp(n)};"
				else
					l "rb_hash_aset(hash, key, #{comp(n)});"
				end
			}
			"hash"
		}
	else
		"rb_hash_new()"
	end
end

#comp_iasgn(hash) ⇒ Object



1007
1008
1009
1010
1011
# File 'lib/ruby2cext/common_node_comp.rb', line 1007

def comp_iasgn(hash)
	assign_res(comp(hash[:value]))
	l "rb_ivar_set(#{get_self}, #{sym(hash[:vid])}, res);"
	"res"
end

#comp_if(hash) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/ruby2cext/common_node_comp.rb', line 364

def comp_if(hash)
	cond = comp(hash[:cond])
	if !hash[:body] && !hash[:else]
		l cond + ";"
		"Qnil"
	else
		c_if("RTEST(#{cond})") {
			assign_res(comp(hash[:body]))
		}
		c_else {
			assign_res(comp(hash[:else]))
		}
		"res"
	end
end

#comp_iter(hash) ⇒ Object



581
582
583
584
# File 'lib/ruby2cext/common_node_comp.rb', line 581

def comp_iter(hash)
	bl_fun, need_clos = CFunction::Block.compile(self, make_block(hash[:body]), hash[:var])
	handle_iter(hash[:iter], bl_fun, need_clos ? get_closure_ary_var : "Qnil")
end

#comp_ivar(hash) ⇒ Object



1073
1074
1075
# File 'lib/ruby2cext/common_node_comp.rb', line 1073

def comp_ivar(hash)
	"rb_ivar_get(#{get_self}, #{sym(hash[:vid])})"
end

#comp_lasgn(hash) ⇒ Object



993
994
995
# File 'lib/ruby2cext/common_node_comp.rb', line 993

def comp_lasgn(hash)
	"(#{scope.get_lvar(hash[:vid])} = #{comp(hash[:value])})"
end

#comp_lit(hash) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/ruby2cext/common_node_comp.rb', line 317

def comp_lit(hash)
	case (l = hash[:lit])
	when Fixnum
		"LONG2FIX(#{l.inspect})"
	when Symbol
		"ID2SYM(#{sym(l)})"
	when Bignum
		global_const("rb_cstr_to_inum(#{l.to_s.to_c_strlit}, 10, Qfalse)")
	when Float
		global_const("rb_float_new(%.40e)" % l)
	when Range
		global_const("rb_range_new(#{comp_lit(:lit=>l.first)}, #{comp_lit(:lit=>l.last)}, Q#{l.exclude_end?})")
	when Regexp
		s = l.source
		global_const("rb_reg_new(#{s.to_c_strlit}, #{s.size}, #{l.options})")
	else
		raise Ruby2CExtError::Bug, "unsupported literal type: #{l.inspect}"
	end
end

#comp_lvar(hash) ⇒ Object



1064
1065
1066
# File 'lib/ruby2cext/common_node_comp.rb', line 1064

def comp_lvar(hash)
	"#{scope.get_lvar(hash[:vid])}"
end

#comp_masgn(hash) ⇒ Object



923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
# File 'lib/ruby2cext/common_node_comp.rb', line 923

def comp_masgn(hash)
	c_scope_res {
		l "VALUE ma_val;"
		l "long ma_len;"
		l "ma_val = #{comp(hash[:value])};"
		l "ma_len = RARRAY(ma_val)->len;"
		head_len = 0
		if (head = hash[:head])
			ensure_node_type(head, :array)
			head_len = head.last.size
			head.last.each_with_index { |asgn, i|
				handle_assign(asgn, "(#{i} < ma_len ? RARRAY(ma_val)->ptr[#{i}] : Qnil)", false)
			}
		end
		if ((args = hash[:args]) && (args != -1))
			handle_assign(args, "(#{head_len} < ma_len ? rb_ary_new4(ma_len-#{head_len}, " +
				"RARRAY(ma_val)->ptr+#{head_len}) : rb_ary_new2(0))", false)
		end
		"ma_val"
	}
end

#comp_match(hash) ⇒ Object



393
394
395
# File 'lib/ruby2cext/common_node_comp.rb', line 393

def comp_match(hash)
	"rb_reg_match2(#{comp_lit(hash)})"
end

#comp_match2(hash) ⇒ Object



396
397
398
399
400
401
402
# File 'lib/ruby2cext/common_node_comp.rb', line 396

def comp_match2(hash)
	c_scope_res {
		l "VALUE recv;"
		l "recv = #{comp(hash[:recv])};"
		"rb_reg_match(recv, #{comp(hash[:value])})"
	}
end

#comp_match3(hash) ⇒ Object



403
404
405
406
407
408
409
410
# File 'lib/ruby2cext/common_node_comp.rb', line 403

def comp_match3(hash)
	c_scope_res {
		l "VALUE recv, val;"
		l "recv = #{comp(hash[:recv])};"
		l "val = #{comp(hash[:value])};"
		"(TYPE(val) == T_STRING ? rb_reg_match(recv, val) : rb_funcall(val, #{sym(:=~)}, 1, recv))"
	}
end

#comp_module(hash) ⇒ Object



1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
# File 'lib/ruby2cext/common_node_comp.rb', line 1306

def comp_module(hash)
	add_helper <<-EOC
		static VALUE module_prep(VALUE prefix, ID cname) {
			VALUE module;
			if (rb_const_defined_at(prefix, cname)) {
				module = rb_const_get_at(prefix, cname);
				if (TYPE(module) != T_MODULE) rb_raise(rb_eTypeError, "%s is not a module", rb_id2name(cname));
				if (ruby_safe_level >= 4) rb_raise(rb_eSecurityError, "extending module prohibited");
			}
			else {
				module = rb_define_module_id(cname);
				rb_set_class_path(module, prefix, rb_id2name(cname));
				rb_const_set(prefix, cname, module);
			}
			return module;
		}
	EOC
	c_scope_res {
		l "VALUE prefix, tmp_module;"
		l "prefix = #{make_class_prefix(hash[:cpath])};"
		l "tmp_module = module_prep(prefix, #{sym(hash[:cpath].last[:mid])});"
		CFunction::ClassModuleScope.compile(self, hash[:body], "tmp_module", false)
	}
end

#comp_next(hash) ⇒ Object



590
591
592
593
# File 'lib/ruby2cext/common_node_comp.rb', line 590

def comp_next(hash)
	# must be implemented by the "user" of CommonNodeComp
	raise Ruby2CExtError::NotSupported, "next is not supported"
end

#comp_nil(hash) ⇒ Object



337
# File 'lib/ruby2cext/common_node_comp.rb', line 337

def comp_nil(hash); "Qnil"; end

#comp_not(hash) ⇒ Object



740
741
742
# File 'lib/ruby2cext/common_node_comp.rb', line 740

def comp_not(hash)
	"(RTEST(#{comp(hash[:body])}) ? Qfalse : Qtrue)"
end

#comp_nth_ref(hash) ⇒ Object



1115
1116
1117
# File 'lib/ruby2cext/common_node_comp.rb', line 1115

def comp_nth_ref(hash)
	"rb_reg_nth_match(#{hash[:nth]}, rb_backref_get())"
end

#comp_op_asgn1(hash) ⇒ Object

Ruby 1.8.4



790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
# File 'lib/ruby2cext/common_node_comp.rb', line 790

def comp_op_asgn1(hash)
	ensure_node_type(hash[:args], :argscat)
	c_scope_res {
		l "VALUE oa1_recv, oa1_val;"
		l "oa1_recv = #{comp(hash[:recv])};"
		c_scope_res {
			build_args(hash[:args].last[:body], true)
			l "oa1_val = rb_funcall3(oa1_recv, #{sym(:[])}, argc, argv);"
			mid = hash[:mid]
			rval = hash[:args].last[:head]
			if Symbol === mid
				call = [:call, {:recv=>"oa1_val", :mid=>mid, :args=>[:array, [rval]]}]
				l "oa1_val = #{comp(call)};"
				l "{"
			else
				# mid == 0 is OR, mid == 1 is AND
				l "if (#{mid == 0 ? "!" : ""}RTEST(oa1_val)) {"
				l "oa1_val = #{comp(rval)};"
			end
			l "argv[argc] = oa1_val;"
			l "rb_funcall2(oa1_recv, #{sym(:[]=)}, argc + 1, argv);"
			l "}"
			"oa1_val"
		}
	}
end

#comp_op_asgn2(hash) ⇒ Object



816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
# File 'lib/ruby2cext/common_node_comp.rb', line 816

def comp_op_asgn2(hash)
	ensure_node_type(ids = hash[:next], :op_asgn2)
	ids = ids.last
	c_scope_res {
		l "VALUE oa2_recv, oa2_val;"
		l "oa2_recv = #{comp(hash[:recv])};"
		call = [:call, {:recv=>"oa2_recv", :mid=>ids[:vid], :args=>false}]
		l "oa2_val = #{comp(call)};"
		mid = ids[:mid]
		if Symbol === mid
			call = [:call, {:recv=>"oa2_val", :mid=>mid, :args=>[:array, [hash[:value]]]}]
			l "oa2_val = #{comp(call)};"
			l "{"
		else
			# mid == 0 is OR, mid == 1 is AND
			l "if (#{mid == 0 ? "!" : ""}RTEST(oa2_val)) {"
			l "oa2_val = #{comp(hash[:value])};"
		end
		l "rb_funcall2(oa2_recv, #{sym(ids[:aid])}, 1, &oa2_val);"
		l "}"
		"oa2_val"
	}
end

#comp_op_asgn_and(hash) ⇒ Object



888
889
890
891
892
893
894
# File 'lib/ruby2cext/common_node_comp.rb', line 888

def comp_op_asgn_and(hash)
	assign_res(comp(hash[:head]))
	c_if("RTEST(res)") {
		assign_res(comp(hash[:value]))
	}
	"res"
end

#comp_op_asgn_or(hash) ⇒ Object



895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
# File 'lib/ruby2cext/common_node_comp.rb', line 895

def comp_op_asgn_or(hash)
	if Symbol === (aid = hash[:aid])
		def_test =
			case aid.to_s
			when /\A@@/
				"rb_cvar_defined(#{get_cvar_cbase}, #{sym(aid)})"
			when /\A@/
				"rb_ivar_defined(#{get_self}, #{sym(aid)})"
			when /\A\$/
				"rb_gvar_defined(#{get_global_entry(aid)})"
			else
				raise Ruby2CExtError::Bug, "unexpected aid for op_asgn_or: #{aid.inspect}"
			end
		c_if(def_test) {
			assign_res(comp(hash[:head]))
		}
		c_else {
			assign_res("Qnil")
		}
	else
		assign_res(comp(hash[:head]))
	end
	c_if("!RTEST(res)") {
		assign_res(comp(hash[:value]))
	}
	"res"
end

#comp_or(hash) ⇒ Object



733
734
735
736
737
738
739
# File 'lib/ruby2cext/common_node_comp.rb', line 733

def comp_or(hash)
	assign_res(comp(hash[:first]))
	c_if("!RTEST(res)") {
		assign_res(comp(hash[:second]))
	}
	"res"
end

#comp_postexe(hash, &iter_proc) ⇒ Object

Raises:



380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/ruby2cext/common_node_comp.rb', line 380

def comp_postexe(hash, &iter_proc)
	raise Ruby2CExtError, "postexe only allowed with iter" unless iter_proc
	c_scope {
		l "static int done = 0;"
		c_if("!done") {
			l "done = 1;"
			# compile as at_exit (rb_f_END() is static)
			l do_funcall(get_self, :at_exit, false, true, iter_proc)
		}
	}
	"Qnil"
end

#comp_redo(hash) ⇒ Object



594
595
596
597
# File 'lib/ruby2cext/common_node_comp.rb', line 594

def comp_redo(hash)
	# must be implemented by the "user" of CommonNodeComp
	raise Ruby2CExtError::NotSupported, "redo is not supported"
end

#comp_rescue(hash) ⇒ Object



624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# File 'lib/ruby2cext/common_node_comp.rb', line 624

def comp_rescue(hash)
	ensure_node_type(resb = hash[:resq], :resbody)
	# compile the real body
	cflow_hash = {}
	body = CFunction::Wrap.compile(self, "rescue_body", cflow_hash) { |cf|
		cf.instance_eval {
			l "#{get_wrap_ptr}->state |= 1;"
			comp(hash[:head])
		}
	}
	# now all the resbodies in one c function
	res_bodies = CFunction::Wrap.compile(self, "rescue_resbodies", cflow_hash) { |cf|
		cf.instance_eval {
			cnt = 0
			while resb
				cnt += 1
				args = resb.last[:args]
				unless args
					l "if (rb_obj_is_kind_of(ruby_errinfo, rb_eStandardError)) {"
				else
					add_helper <<-EOC
						static void rescue_mod_check(VALUE mod) {
							if (!rb_obj_is_kind_of(mod, rb_cModule)) {
								rb_raise(rb_eTypeError, "class or module required for rescue clause");
							}
						}
					EOC
					if args.first == :array # TODO
						# ruby doesn't handle this case specially, but it might be faster this way
						# (on the other side: we might miss exceptions (for not evaluated entries of the array))
						args.last.each { |ex|
							assign_res(comp(ex))
							l "rescue_mod_check(res);"
							l "if (!RTEST(rb_funcall(res, #{sym(:===)}, 1, ruby_errinfo))) {"
						}
						# non did match
						assign_res("Qfalse")
						# close all ifs
						args.last.size.times { l "}" }
					else
						c_scope {
							l "int i = 0;"
							# TODO: ruby has BEGIN_CALLARGS protection here, is this really necessary???
							build_args(args)
							assign_res("Qfalse")
							c_for("; i < argc; ++i") {
								l "rescue_mod_check(argv[i]);"
								c_if("RTEST(rb_funcall(argv[i], #{sym(:===)}, 1, ruby_errinfo))") {
									assign_res("Qtrue");
									l "break;"
								}
							}
						}
					end
					l "if (res) {"
				end
				l "#{get_wrap_ptr}->state &= ~1;" # set first bit = 0
				assign_res(comp(resb.last[:body]))
				l "}"
				l "else {"
				resb = resb.last[:head]
			end
			# we are in the last else, if the exception wasn't handled, then reraise
			l "rb_jump_tag(0x6 /* TAG_RAISE */);"
			# close all elses
			cnt.times { l "}" }
		}
		"res"
	}
	# now call rb_rescue2 with the two bodies (and handle else if necessary)
	c_scope_res {
		else_node = hash[:else]
		l "long save_state = #{get_wrap_ptr}->state;"
		l "long do_else;" if else_node
		wp = "(VALUE)#{get_wrap_ptr}"
		assign_res("rb_rescue2(#{body}, #{wp}, #{res_bodies}, #{wp}, rb_eException, (VALUE)0)")
		l "do_else = #{get_wrap_ptr}->state & 1;" if else_node
		l "#{get_wrap_ptr}->state = (#{get_wrap_ptr}->state & ~1) | (save_state & 1);" # restore the 1st bit of save_state
		CFunction::Wrap::handle_wrap_cflow(self, cflow_hash)
		if else_node
			c_if("do_else") {
				assign_res(comp(else_node))
			}
		end
		"res"
	}
end

#comp_retry(hash) ⇒ Object



602
603
604
605
# File 'lib/ruby2cext/common_node_comp.rb', line 602

def comp_retry(hash)
	# must be implemented by the "user" of CommonNodeComp
	raise Ruby2CExtError::NotSupported, "retry is not supported"
end

#comp_return(hash) ⇒ Object



598
599
600
601
# File 'lib/ruby2cext/common_node_comp.rb', line 598

def comp_return(hash)
	# must be implemented by the "user" of CommonNodeComp
	raise Ruby2CExtError::NotSupported, "return is not supported"
end

#comp_sclass(hash) ⇒ Object



1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
# File 'lib/ruby2cext/common_node_comp.rb', line 1331

def comp_sclass(hash)
	add_helper <<-EOC
		static void sclass_check(VALUE obj) {
			if (FIXNUM_P(obj) || SYMBOL_P(obj)) rb_raise(rb_eTypeError, "no virtual class for %s", rb_obj_classname(obj));
			if (ruby_safe_level >= 4 && !OBJ_TAINTED(obj)) rb_raise(rb_eSecurityError, "Insecure: can't extend object");
		}
	EOC
	c_scope_res {
		l "VALUE tmp_sclass;"
		l "tmp_sclass = #{comp(hash[:recv])};"
		l "sclass_check(tmp_sclass);"
		l "tmp_sclass = rb_singleton_class(tmp_sclass);"
		CFunction::ClassModuleScope.compile(self, hash[:body], "tmp_sclass", true)
	}
end

#comp_self(hash) ⇒ Object



340
# File 'lib/ruby2cext/common_node_comp.rb', line 340

def comp_self(hash); get_self; end

#comp_splat(hash) ⇒ Object



607
608
609
610
# File 'lib/ruby2cext/common_node_comp.rb', line 607

def comp_splat(hash)
	assign_res(comp(hash[:head]))
	"(NIL_P(res) ? rb_ary_new3(1, Qnil) : rb_Array(res))"
end

#comp_str(hash) ⇒ Object



1168
1169
1170
1171
# File 'lib/ruby2cext/common_node_comp.rb', line 1168

def comp_str(hash)
	lit = global_const("rb_str_new(#{hash[:lit].to_c_strlit}, #{hash[:lit].size})")
	"rb_str_new3(#{lit})"
end

#comp_super(hash, &iter_proc) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/ruby2cext/common_node_comp.rb', line 302

def comp_super(hash, &iter_proc)
	iter_proc ||= NON_ITER_PROC
	helper_super_allowed_check
	l "super_allowed_check();"
	args = hash[:args]
	if args
		c_scope_res {
			build_args(args)
			iter_proc["rb_call_super(%s, %s)", %w[argc argv], %w[int VALUE*]]
		}
	else
		iter_proc["rb_call_super(0, 0)", [], []]
	end
end

#comp_svalue(hash) ⇒ Object



614
615
616
617
# File 'lib/ruby2cext/common_node_comp.rb', line 614

def comp_svalue(hash)
	assign_res(comp(hash[:head]))
	"(RARRAY(res)->len == 1 ? RARRAY(res)->ptr[0] : (RARRAY(res)->len == 0 ? Qnil : res))"
end

#comp_to_ary(hash) ⇒ Object



611
612
613
# File 'lib/ruby2cext/common_node_comp.rb', line 611

def comp_to_ary(hash)
	"rb_ary_to_ary(#{comp(hash[:head])})"
end

#comp_true(hash) ⇒ Object



339
# File 'lib/ruby2cext/common_node_comp.rb', line 339

def comp_true(hash); "Qtrue"; end

#comp_undef(hash) ⇒ Object



1246
1247
1248
1249
1250
1251
1252
1253
# File 'lib/ruby2cext/common_node_comp.rb', line 1246

def comp_undef(hash)
	if Array === (mid = hash[:mid]) # 1.8.5
		l "rb_undef(#{get_class}, rb_to_id(#{comp(mid)}));"
	else
		l "rb_undef(#{get_class}, #{sym(mid)});"
	end
	"Qnil"
end

#comp_until(hash) ⇒ Object



482
483
484
# File 'lib/ruby2cext/common_node_comp.rb', line 482

def comp_until(hash)
	comp_while(hash, true)
end

#comp_valias(hash) ⇒ Object



1267
1268
1269
1270
# File 'lib/ruby2cext/common_node_comp.rb', line 1267

def comp_valias(hash)
	l "rb_alias_variable(#{sym(hash[:new])}, #{sym(hash[:old])});"
	"Qnil"
end

#comp_vcall(hash) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/ruby2cext/common_node_comp.rb', line 193

def comp_vcall(hash)
	if scope.vmode_method?(hash[:mid])
		"Qnil"
	else
		"rb_funcall2(#{get_self}, #{sym(hash[:mid])}, 0, 0)"
	end
end

#comp_when(hash) ⇒ Object



447
448
449
# File 'lib/ruby2cext/common_node_comp.rb', line 447

def comp_when(hash)
	handle_when(hash, proc { |val| comp(val) })
end

#comp_while(hash, is_until = false) ⇒ Object



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/ruby2cext/common_node_comp.rb', line 463

def comp_while(hash, is_until = false)
	redo_lbl = un("while_redo")
	next_lbl = un("while_next")
	break_lbl = un("while_break")
	c_scope_res {
		l "VALUE while_res = Qnil;"
		c_for(";;") {
			l "if (#{is_until ? "" : "!"}RTEST(#{comp(hash[:cond])})) break;" if hash[:state] != 0
			l "#{redo_lbl}:"
			push_while(redo_lbl, next_lbl, break_lbl)
			l "#{comp(hash[:body])};"
			pop_while
			l "#{next_lbl}: ;"
			l "if (#{is_until ? "" : "!"}RTEST(#{comp(hash[:cond])})) break;" if hash[:state] == 0
		}
		l "#{break_lbl}:"
		"while_res"
	}
end

#comp_xstr(hash) ⇒ Object



1208
1209
1210
# File 'lib/ruby2cext/common_node_comp.rb', line 1208

def comp_xstr(hash)
	"rb_funcall(#{get_self}, '`', 1, #{comp_str(hash)})"
end

#comp_yield(hash) ⇒ Object



619
620
621
622
# File 'lib/ruby2cext/common_node_comp.rb', line 619

def comp_yield(hash)
	val = (hash[:head] ? comp(hash[:head]) : "Qundef")
	"rb_yield#{hash[:state] == 0 ? "" : "_splat"}(#{val})"
end

#comp_zarray(hash) ⇒ Object



1143
1144
1145
# File 'lib/ruby2cext/common_node_comp.rb', line 1143

def comp_zarray(hash)
	"rb_ary_new()"
end

#comp_zsuper(hash, &iter_proc) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/ruby2cext/common_node_comp.rb', line 281

def comp_zsuper(hash, &iter_proc)
	if CFunction::Method === self
		# super zsuper only in a method in 1.8.5
		iter_proc ||= NON_ITER_PROC
		helper_super_allowed_check
		l "super_allowed_check();"
		# this is not 100% equivalent to 1.8.5 behavior ...
		iter_proc["rb_call_super(%s, %s)", %w[meth_argc meth_argv], %w[int VALUE*]]
	else
		raise Ruby2CExtError::NotSupported, "super without explicit arguments is not supported here"
	end
end

#do_funcall(recv, mid, args, allow_private, iter_proc) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/ruby2cext/common_node_comp.rb', line 231

def do_funcall(recv, mid, args, allow_private, iter_proc)
	iter_proc ||= NON_ITER_PROC
	fun = "rb_funcall#{allow_private ? 2 : 3}"
	if args
		c_scope_res {
			l "VALUE recv = #{recv};"
			build_args(args)
			iter_proc["#{fun}(%s, #{sym(mid)}, %s, %s)", %w[recv argc argv], %w[VALUE int VALUE*]]
		}
	else
		if allow_private && iter_proc == NON_ITER_PROC
			if scope.vmode_method?(mid)
				return "Qnil"
			end
		end
		iter_proc["#{fun}(%s, #{sym(mid)}, 0, 0)", [recv], %w[VALUE]]
	end
end

#get_global_entry(vid) ⇒ Object



65
66
67
68
# File 'lib/ruby2cext/common_node_comp.rb', line 65

def get_global_entry(vid)
	g_entry = "(VALUE)rb_global_entry(#{sym(vid)})"
	"(struct global_entry*)(#{global_const(g_entry, false)})"
end

#handle_assign(asgn_node, val, undef_check = true) ⇒ Object



945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
# File 'lib/ruby2cext/common_node_comp.rb', line 945

def handle_assign(asgn_node, val, undef_check = true)
	c_scope {
		if undef_check
			l "VALUE as_val;"
			l "as_val = #{comp(val)};"
			l "if (as_val == Qundef) as_val = Qnil;"
			val = "as_val"
		end
		dup_node = [asgn_node.first, asgn_node.last.dup]
		case asgn_node.first
		when :lasgn, :dasgn, :dasgn_curr, :iasgn, :gasgn, :cdecl, :cvdecl, :cvasgn
			if dup_node.last[:value]
				raise Ruby2CExtError, "unexpected value in #{asgn_node.first} node in handle_assign"
			end
			dup_node.last[:value] = val
			l "#{comp(dup_node)};"
		when :masgn
			c_scope {
				l "VALUE as_ma_val;"
				l "VALUE tmp;" if (ma_head = asgn_node.last[:head])
				l "as_ma_val = #{comp(val)};"
				# adapted from svalue_to_mrhs()
				if ma_head
					l "tmp = rb_check_array_type(as_ma_val);"
					l "as_ma_val = (NIL_P(tmp) ? rb_ary_new3(1, as_ma_val) : tmp);"
				else
					l "as_ma_val = rb_ary_new3(1, as_ma_val);"
				end
				dup_node.last[:value] = "as_ma_val"
				l "#{comp(dup_node)};"
			}
		when :attrasgn # TODO: can :call also appear here ???
			c_scope {
				l "VALUE as_aa_val;"
				l "as_aa_val = #{comp(val)};"
				if asgn_node.last[:args]
					dup_node.last[:args] = [:argspush, {:body => "as_aa_val", :head => asgn_node.last[:args]}]
				else
					dup_node.last[:args] = [:array, ["as_aa_val"]]
				end
				l "#{comp(dup_node)};"
			}
		else
			raise Ruby2CExtError::Bug, "unexpected assign node type: #{asgn_node.first}"
		end
	}
end

#handle_dot(hash, dot3) ⇒ Object



744
745
746
747
748
749
750
# File 'lib/ruby2cext/common_node_comp.rb', line 744

def handle_dot(hash, dot3)
	c_scope_res {
		l "VALUE beg;"
		l "beg = #{comp(hash[:beg])};"
		"rb_range_new(beg, #{comp(hash[:end])}, Q#{dot3})"
	}
end

#handle_dyn_str(hash) ⇒ Object



1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
# File 'lib/ruby2cext/common_node_comp.rb', line 1175

def handle_dyn_str(hash)
	ensure_node_type(hash[:next], :array)
	c_scope_res {
		l "VALUE str, str2;"
		l "str = #{comp_str(hash)};"
		hash[:next].last.each { |node|
			l "str2 = #{comp(node)};"
			l "rb_str_append(str, str2);"
			l "OBJ_INFECT(str, str2);"
		}
		"str"
	}
end

#handle_flip(hash, flip3) ⇒ Object



758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'lib/ruby2cext/common_node_comp.rb', line 758

def handle_flip(hash, flip3)
	flip_var = scope.get_lvar_idx(hash[:cnt] - 2)
	c_if("RTEST(#{flip_var})") {
		l "if (RTEST(#{comp(hash[:end])})) #{flip_var} = Qfalse;"
		assign_res("Qtrue")
	}
	c_else {
		if flip3
			assign_res("(RTEST(#{comp(hash[:beg])}) ? Qtrue : Qfalse)")
			l "#{flip_var} = res;"
		else
			assign_res(c_scope_res {
				l "VALUE beg_res;"
				l "beg_res = (RTEST(#{comp(hash[:beg])}) ? Qtrue : Qfalse);"
				c_if("beg_res") {
					l "#{flip_var} = (RTEST(#{comp(hash[:end])}) ? Qfalse : Qtrue);"
				}
				"beg_res"
			})
		end
	}
	"res"
end

#handle_iter(iter, bl_fun, closure) ⇒ Object



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/ruby2cext/common_node_comp.rb', line 486

def handle_iter(iter, bl_fun, closure)
	ensure_node_type(iter, [:call, :fcall, :super, :zsuper, :postexe]) # attrasgn ???
	l "do {"
	block_calls = 0
	assign_res(__send__("comp_#{iter.first}", iter.last) { |str, args, arg_types|
		block_calls += 1
		c_scope_res {
			l "VALUE iter_data[#{args.size + 1}];"
			l "iter_data[0] = Qfalse;"
			args.each_with_index { |arg, i|
				l "iter_data[#{i + 1}] = (VALUE)(#{arg});"
			}
			iter_data_cast = []
			arg_types.each_with_index { |arg_type, i|
				iter_data_cast << "(#{arg_type})(iter_data[#{i + 1}])"
			}
			it_fun = compiler.add_fun(<<-EOC.chomp % iter_data_cast, "iterate")
				static VALUE FUNNAME(VALUE data) {
					VALUE *iter_data = (VALUE*)data;
					ruby_top_self = org_ruby_top_self;
					if (iter_data[0]) return Qundef;
					iter_data[0] = Qtrue;
					return #{str};
				}
			EOC
			# hack to make instance_eval etc. work (rb_iterate() uses ruby_top_self as block.self)
			# this _should_ be save, because rb_trap_immediate should always be 0 here ...
			l "ruby_top_self = Qundef;"
			"rb_iterate(#{it_fun}, (VALUE)iter_data, #{bl_fun}, #{closure})"
		}
	})
	raise Ruby2CExtError::Bug, "internal error while compiling iter" unless block_calls == 1
	l "}"
	l "while (res == Qundef);"
	"res"
end

#handle_method_args(arg, block_arg) ⇒ Object



78
79
80
81
82
83
84
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ruby2cext/common_node_comp.rb', line 78

def handle_method_args(arg, block_arg)
	ensure_node_type(arg, :args)
	# handle arg
	arg = arg.last
	cnt = arg[:cnt]
	opt = make_block(arg[:opt]).last
	rest = arg[:rest] || -1
	if Array === rest # 1.8.5 change
		ensure_node_type(rest, :lasgn)
		if rest.last[:vid] == 0 # e.g. def foo(*); end
			rest = -2
		else
			rest = rest.last[:cnt]
		end
	end
	rest = rest - 2
	need_wrong_arg_num_helper = false
	if opt.empty? && rest == -3 # then it was rest == -1, which means no rest_arg
		l "if (meth_argc != #{cnt}) wrong_arg_num(meth_argc, #{cnt});"
		need_wrong_arg_num_helper = true
	else
		if cnt > 0
			l "if (meth_argc < #{cnt}) wrong_arg_num(meth_argc, #{cnt});"
			need_wrong_arg_num_helper = true
		end
		if rest == -3 # then it was rest == -1, which means no rest_arg
			l "if (meth_argc > #{cnt + opt.size}) wrong_arg_num(meth_argc, #{cnt + opt.size});"
			need_wrong_arg_num_helper = true
		end
	end
	if need_wrong_arg_num_helper
		add_helper <<-EOC
			static void wrong_arg_num(int argc, int exp) {
				rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", argc, exp);
			}
		EOC
	end
	(0...cnt).each { |i|
		l "#{scope.get_lvar_idx(i)} = meth_argv[#{i}];"
	}
	opt.each_with_index { |asgn, i|
		c_if("meth_argc > #{cnt + i}") {
			l "#{scope.get_lvar_idx(cnt + i)} = meth_argv[#{cnt + i}];"
		}
		c_else {
			l "#{comp(asgn)};"
		}
	}
	if rest >= 0
		sum = cnt + opt.size
		c_if("meth_argc > #{sum}") {
			l "#{scope.get_lvar_idx(rest)} = rb_ary_new4(meth_argc-#{sum}, meth_argv+#{sum});"
		}
		c_else {
			l "#{scope.get_lvar_idx(rest)} = rb_ary_new2(0);"
		}
	end

	# handle block arg if available
	if block_arg
		ensure_node_type(block_arg, :block_arg)
		c_if("rb_block_given_p()") {
			l "#{scope.get_lvar(block_arg.last[:vid])} = rb_block_proc();"
		}
		# no else, lvars are initialized to nil
	end
end

#handle_when(hash, test_proc) ⇒ Object



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/ruby2cext/common_node_comp.rb', line 412

def handle_when(hash, test_proc)
	ensure_node_type(head = hash[:head], :array)
	c_scope_res {
		l "int when_handle = 1;"
		head.last.each { |check|
			if check.first == :when
				l "VALUE arr; long i;"
				l "arr = #{comp(check.last[:head])};"
				l "if (TYPE(arr) != T_ARRAY) arr = rb_ary_to_ary(arr);"
				c_for("i = 0; i < RARRAY(arr)->len; ++i") {
					c_if("RTEST(#{test_proc["RARRAY(arr)->ptr[i]"]})") {
						l "arr = Qfalse;"
						l "break;"
					}
				}
				l "if (RTEST(arr)) {"
			else
				l "if (!RTEST(#{test_proc[check]})) {"
			end
		}
		l "when_handle = 0;" # here all checks failed
		head.last.size.times { l "}" }
		c_if("when_handle") {
			assign_res(comp(hash[:body]))
		}
		c_else {
			if Array === hash[:next] && hash[:next].first == :when
				assign_res(handle_when(hash[:next].last, test_proc))
			else
				assign_res(comp(hash[:next]))
			end
		}
		"res"
	}
end

#helper_class_module_checkObject



1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
# File 'lib/ruby2cext/common_node_comp.rb', line 1013

def helper_class_module_check
	add_helper <<-EOC
		static void class_module_check(VALUE klass) {
			switch (TYPE(klass)) {
				case T_CLASS:
				case T_MODULE:
					break;
				default:
					rb_raise(rb_eTypeError, "%s is not a class/module", RSTRING(rb_obj_as_string(klass))->ptr);
			}
		}
	EOC
end

#helper_super_allowed_checkObject



269
270
271
272
273
274
275
276
277
278
# File 'lib/ruby2cext/common_node_comp.rb', line 269

def helper_super_allowed_check
	# last_func is set to 0 by rb_require_safe
	add_helper <<-EOC
		static void super_allowed_check() {
			if (!ruby_frame->last_func) {
				rb_raise(rb_eNoMethodError, "super called outside of method");
			}
		}
	EOC
end

#make_block(block) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/ruby2cext/common_node_comp.rb', line 70

def make_block(block)
	if block
		block.first == :block ? block : [:block, [block]]
	else
		[:block, []]
	end
end

#make_class_prefix(node) ⇒ Object



1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'lib/ruby2cext/common_node_comp.rb', line 1027

def make_class_prefix(node)
	ensure_node_type(node, [:colon2, :colon3])
	if node.first == :colon2
		hash = node.last
		if hash[:head]
			assign_res(comp(hash[:head]))
			l "class_module_check(res);"
			"res"
		else
			get_cbase
		end
	else
		"rb_cObject"
	end
end