Class: Ruby2CExtension::CFunction::Wrap

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby2cext/c_function.rb

Constant Summary collapse

BREAK_FLAG =
1 << 20
NEXT_FLAG =
1 << 21
REDO_FLAG =
1 << 22
RETURN_FLAG =
1 << 23

Constants included from Ruby2CExtension::CommonNodeComp

Ruby2CExtension::CommonNodeComp::NON_ITER_PROC

Instance Attribute Summary collapse

Attributes inherited from Base

#closure_tbl, #compiler, #need_class, #need_cref, #need_res, #need_self, #need_wrap, #scope

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#add_closure_need, #add_helper, #assign_res, #closure_buid_c_code, #comp_retry, #get_cbase, #get_cref, #get_cvar_cbase, #get_lines, #global_const, #global_var, #in_while?, #l, #need_closure_ptr, #pop_while, #push_while, #sym, #un, #wrap_buid_c_code

Methods included from Tools::EnsureNodeTypeMixin

#ensure_node_type

Methods included from Ruby2CExtension::CommonNodeComp

#build_args, #build_c_arr, #c_else, #c_for, #c_if, #c_scope, #c_scope_res, #c_static_once, #comp, #comp_alias, #comp_and, #comp_argscat, #comp_argspush, #comp_array, #comp_attrasgn, #comp_back_ref, #comp_begin, #comp_block, #comp_block_pass, #comp_call, #comp_case, #comp_cdecl, #comp_class, #comp_colon2, #comp_colon3, #comp_const, #comp_cvar, #comp_cvasgn, #comp_cvdecl, #comp_dasgn, #comp_dasgn_curr, #comp_defined, #comp_defn, #comp_defs, #comp_dot2, #comp_dot3, #comp_dregx, #comp_dregx_once, #comp_dstr, #comp_dsym, #comp_dvar, #comp_dxstr, #comp_ensure, #comp_evstr, #comp_false, #comp_fcall, #comp_flip2, #comp_flip3, #comp_for, #comp_gasgn, #comp_gvar, #comp_hash, #comp_iasgn, #comp_if, #comp_iter, #comp_ivar, #comp_lasgn, #comp_lit, #comp_lvar, #comp_masgn, #comp_match, #comp_match2, #comp_match3, #comp_module, #comp_nil, #comp_not, #comp_nth_ref, #comp_op_asgn1, #comp_op_asgn2, #comp_op_asgn_and, #comp_op_asgn_or, #comp_or, #comp_postexe, #comp_rescue, #comp_retry, #comp_sclass, #comp_self, #comp_splat, #comp_str, #comp_super, #comp_svalue, #comp_to_ary, #comp_true, #comp_undef, #comp_until, #comp_valias, #comp_vcall, #comp_when, #comp_while, #comp_xstr, #comp_yield, #comp_zarray, #comp_zsuper, #do_funcall, #get_global_entry, #handle_assign, #handle_dot, #handle_dyn_str, #handle_flip, #handle_iter, #handle_method_args, #handle_when, #helper_class_module_check, #helper_super_allowed_check, #make_block, #make_class_prefix

Constructor Details

#initialize(outer_cfun, cflow_hash = nil) ⇒ Wrap

cflow_hash can either be nil or a hash, if it is nil then break, next, redo and return are not possible from the wrap to outer_cfun.

If cflow_hash is a hash then those are possible if outer_cfun allows them. For this to work only the bits 0-19 of wrap_ptr->state may be modified by code inside this wrap and after the call the code generated by Wrap.handle_wrap_cflow has to be inserted.



476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/ruby2cext/c_function.rb', line 476

def initialize(outer_cfun, cflow_hash = nil)
	@outer_cfun = outer_cfun
	if Wrap === outer_cfun
		@base_cfun = outer_cfun.base_cfun
	else
		@base_cfun = outer_cfun
	end
	@cflow_hash = cflow_hash
	@compiler = base_cfun.compiler
	@scope = Scopes::WrappedScope.new(base_cfun.scope)
	@closure_tbl = base_cfun.closure_tbl
	@lines = []
	@while_stack = []
end

Instance Attribute Details

#base_cfunObject (readonly)

Returns the value of attribute base_cfun.



458
459
460
# File 'lib/ruby2cext/c_function.rb', line 458

def base_cfun
  @base_cfun
end

#cflow_hashObject (readonly)

Returns the value of attribute cflow_hash.



458
459
460
# File 'lib/ruby2cext/c_function.rb', line 458

def cflow_hash
  @cflow_hash
end

#outer_cfunObject (readonly)

Returns the value of attribute outer_cfun.



458
459
460
# File 'lib/ruby2cext/c_function.rb', line 458

def outer_cfun
  @outer_cfun
end

Class Method Details

.compile(outer, base_name, cflow_hash = nil) ⇒ Object



444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/ruby2cext/c_function.rb', line 444

def self.compile(outer, base_name, cflow_hash = nil)
	cf = self.new(outer, cflow_hash)
	cf.instance_eval {
		if cf.cflow_hash
			l "#{get_wrap_ptr}->state &= (1<<20)-1;" # set bits 20+ to 0
		end
		l "return #{yield cf};"
	}
	body = "#{cf.init_c_code}\n#{cf.get_lines}"
	sig = "static VALUE FUNNAME(struct wrap *wrap_ptr) {"
	outer.need_wrap = true
	cf.compiler.add_fun("#{sig}\n#{body}\n}", base_name) # and return the function name
end

.handle_wrap_cflow(cfun, cflow_hash) ⇒ Object

the result of the call to the wrap cfun is expected in “res”



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/ruby2cext/c_function.rb', line 561

def self.handle_wrap_cflow(cfun, cflow_hash)
	cfun.instance_eval {
		if cflow_hash.has_key?(:break)
			c_if("#{get_wrap_ptr}->state & #{BREAK_FLAG}") {
				l comp_break(:stts => (cflow_hash[:break] ? "res" : false))
			}
		end
		if cflow_hash.has_key?(:next)
			c_if("#{get_wrap_ptr}->state & #{NEXT_FLAG}") {
				l comp_next(:stts => "res")
			}
		end
		if cflow_hash.has_key?(:redo)
			c_if("#{get_wrap_ptr}->state & #{REDO_FLAG}") {
				l comp_redo({})
			}
		end
		if cflow_hash.has_key?(:return)
			c_if("#{get_wrap_ptr}->state & #{RETURN_FLAG}") {
				l comp_return(:stts => "res")
			}
		end
	}
end

Instance Method Details

#break_allowed?(with_value) ⇒ Boolean

Returns:

  • (Boolean)


496
497
498
# File 'lib/ruby2cext/c_function.rb', line 496

def break_allowed?(with_value)
	super || (cflow_hash && outer_cfun.break_allowed?(with_value))
end

#comp_break(hash) ⇒ Object



499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/ruby2cext/c_function.rb', line 499

def comp_break(hash)
	if in_while?(:break)
		super
	elsif break_allowed?(hash[:stts])
		@cflow_hash[:break] ||= hash[:stts]
		l "#{get_wrap_ptr}->state |= #{BREAK_FLAG};"
		l "return #{comp(hash[:stts])};"
		"Qnil"
	else
		raise Ruby2CExtError::NotSupported, "break #{hash[:stts] ? "with a value " : ""}is not supported here"
	end
end

#comp_next(hash) ⇒ Object



515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/ruby2cext/c_function.rb', line 515

def comp_next(hash)
	if in_while?(:next)
		super
	elsif next_allowed?
		@cflow_hash[:next] = true
		l "#{get_wrap_ptr}->state |= #{NEXT_FLAG};"
		# hash[:stts] might be evaluated unnecessarily (because it
		# is ignored in while loops), but oh well ...
		l "return #{comp(hash[:stts])};"
		"Qnil"
	else
		raise Ruby2CExtError::NotSupported, "next is not supported here"
	end
end

#comp_redo(hash) ⇒ Object



533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/ruby2cext/c_function.rb', line 533

def comp_redo(hash)
	if in_while?(:redo)
		super
	elsif redo_allowed?
		@cflow_hash[:redo] = true
		l "#{get_wrap_ptr}->state |= #{REDO_FLAG};"
		l "return Qnil;"
		"Qnil"
	else
		raise Ruby2CExtError::NotSupported, "redo is not supported here"
	end
end

#comp_return(hash) ⇒ Object



549
550
551
552
553
554
555
556
557
558
# File 'lib/ruby2cext/c_function.rb', line 549

def comp_return(hash)
	if return_allowed?
		@cflow_hash[:return] = true
		l "#{get_wrap_ptr}->state |= #{RETURN_FLAG};"
		l "return #{comp(hash[:stts])};"
		"Qnil"
	else
		raise Ruby2CExtError::NotSupported, "return is not supported here"
	end
end

#get_classObject



593
594
595
596
# File 'lib/ruby2cext/c_function.rb', line 593

def get_class
	self.need_class = true
	"(wrap_ptr->s_class)"
end

#get_closure_ary_varObject



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

def get_closure_ary_var
	"(wrap_ptr->my_closure_ary)"
end

#get_cref_implObject



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

def get_cref_impl
	"(wrap_ptr->cref)"
end

#get_selfObject



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

def get_self
	self.need_self = true
	"(wrap_ptr->self)"
end

#get_wrap_ptrObject



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

def get_wrap_ptr
	"wrap_ptr"
end

#init_c_codeObject



612
613
614
615
616
# File 'lib/ruby2cext/c_function.rb', line 612

def init_c_code
	res = []
	res << "VALUE res;" if need_res
	res.compact.join("\n")
end

#next_allowed?Boolean

Returns:

  • (Boolean)


512
513
514
# File 'lib/ruby2cext/c_function.rb', line 512

def next_allowed?
	super || (cflow_hash && outer_cfun.next_allowed?)
end

#redo_allowed?Boolean

Returns:

  • (Boolean)


530
531
532
# File 'lib/ruby2cext/c_function.rb', line 530

def redo_allowed?
	super || (cflow_hash && outer_cfun.redo_allowed?)
end

#return_allowed?Boolean

Returns:

  • (Boolean)


546
547
548
# File 'lib/ruby2cext/c_function.rb', line 546

def return_allowed?
	cflow_hash && outer_cfun.return_allowed?
end