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.



10
11
12
13
14
15
# File 'lib/ruby2cext/scopes.rb', line 10

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.



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

def closure_tbl
  @closure_tbl
end

#need_heapObject

Returns the value of attribute need_heap.



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

def need_heap
  @need_heap
end

#tblObject (readonly)

Returns the value of attribute tbl.



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

def tbl
  @tbl
end

#vmodeObject (readonly)

Returns the value of attribute vmode.



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

def vmode
  @vmode
end

#vmode_methodsObject (readonly)

Returns the value of attribute vmode_methods.



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

def vmode_methods
  @vmode_methods
end

Instance Method Details

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

Raises:



31
32
33
# File 'lib/ruby2cext/scopes.rb', line 31

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

#get_lvar(sym) ⇒ Object

Raises:



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

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

#get_lvar_aryObject



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

def get_lvar_ary
	self.need_heap = true
	"lvar_ary"
end

#get_lvar_idx(i) ⇒ Object

Raises:



19
20
21
22
# File 'lib/ruby2cext/scopes.rb', line 19

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

#init_c_codeObject



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

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



56
57
58
# File 'lib/ruby2cext/scopes.rb', line 56

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

#var_ptr_for_wrapObject



60
61
62
# File 'lib/ruby2cext/scopes.rb', line 60

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

#vmode_def_funObject



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

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)


37
38
39
40
41
42
# File 'lib/ruby2cext/scopes.rb', line 37

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