Class: Ruby2CExtension::Scopes::Scope

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

Constant Summary collapse

VMODES =
[:public, :private, :protected, :module_function].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tbl, vmode_methods = VMODES, private_vmode = false) ⇒ Scope

Returns a new instance of Scope.



8
9
10
11
12
13
# File 'lib/ruby2cext/scopes.rb', line 8

def initialize(tbl, vmode_methods = VMODES, private_vmode = false)
	@vmode_methods = vmode_methods
	@vmode = private_vmode ? :private : :public
	@tbl = tbl || []
	@closure_tbl = nil # must be set by user
end

Instance Attribute Details

#closure_tblObject

Returns the value of attribute closure_tbl.



15
16
17
# File 'lib/ruby2cext/scopes.rb', line 15

def closure_tbl
  @closure_tbl
end

#need_heapObject

Returns the value of attribute need_heap.



15
16
17
# File 'lib/ruby2cext/scopes.rb', line 15

def need_heap
  @need_heap
end

#tblObject (readonly)

Returns the value of attribute tbl.



14
15
16
# File 'lib/ruby2cext/scopes.rb', line 14

def tbl
  @tbl
end

#vmodeObject (readonly)

Returns the value of attribute vmode.



14
15
16
# File 'lib/ruby2cext/scopes.rb', line 14

def vmode
  @vmode
end

#vmode_methodsObject (readonly)

Returns the value of attribute vmode_methods.



14
15
16
# File 'lib/ruby2cext/scopes.rb', line 14

def vmode_methods
  @vmode_methods
end

Instance Method Details

#get_dvar(arg) ⇒ Object Also known as: get_dvar_curr, get_dvar_ary

Raises:



29
30
31
# File 'lib/ruby2cext/scopes.rb', line 29

def get_dvar(arg)
	raise Ruby2CExtError, "dvars not available here"
end

#get_lvar(sym) ⇒ Object

Raises:



21
22
23
24
# File 'lib/ruby2cext/scopes.rb', line 21

def get_lvar(sym)
	raise Ruby2CExtError, "unknown lvar: #{sym}" unless (i = tbl.index(sym))
	get_lvar_idx(i)
end

#get_lvar_aryObject



25
26
27
28
# File 'lib/ruby2cext/scopes.rb', line 25

def get_lvar_ary
	self.need_heap = true
	"lvar_ary"
end

#get_lvar_idx(i) ⇒ Object

Raises:



17
18
19
20
# File 'lib/ruby2cext/scopes.rb', line 17

def get_lvar_idx(i)
	raise Ruby2CExtError, "wrong lvar index: #{i}" unless i >= 0 && i < tbl.size
	"lvar[#{i}]"
end

#init_c_codeObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ruby2cext/scopes.rb', line 62

def init_c_code
	return nil if tbl.empty?
	res = []
	if need_heap
		res << "VALUE *lvar;"
		res << "VALUE lvar_ary = rb_ary_new2(#{tbl.size});"
		res << "rb_mem_clear(RARRAY(lvar_ary)->ptr, #{tbl.size});"
		res << "RARRAY(lvar_ary)->len = #{tbl.size};"
		res << "lvar = RARRAY(lvar_ary)->ptr;"
	else
		res << "VALUE lvar[#{tbl.size}];\nrb_mem_clear(lvar, #{tbl.size});"
	end
	res.join("\n")
end

#new_dyna_scopeObject



54
55
56
# File 'lib/ruby2cext/scopes.rb', line 54

def new_dyna_scope
	DynaScope.new(self, nil, 1)
end

#var_ptr_for_wrapObject



58
59
60
# File 'lib/ruby2cext/scopes.rb', line 58

def var_ptr_for_wrap
	tbl.empty? ? nil : "lvar"
end

#vmode_def_funObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby2cext/scopes.rb', line 41

def vmode_def_fun
	case vmode
	when :protected, :private
		"rb_define_#{vmode}_method"
	when :public
		"rb_define_method"
	when :module_function
		"rb_define_module_function"
	else
		raise Ruby2CExtError::Bug, "unknown vmode: #{@vmode}"
	end
end

#vmode_method?(method_id) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/ruby2cext/scopes.rb', line 35

def vmode_method?(method_id)
	if (res = vmode_methods.include?(method_id))
		@vmode = method_id
	end
	res
end