Class: Ruby2CExtension::Tools::GlobalManager

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

Instance Method Summary collapse

Constructor Details

#initializeGlobalManager

Returns a new instance of GlobalManager.



29
30
31
32
33
# File 'lib/ruby2cext/tools.rb', line 29

def initialize
	@cnt = 0
	@src = []
	@reusable = {}
end

Instance Method Details

#get(str, allow_reuse, register_gc) ⇒ Object

returns the var name for sym



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby2cext/tools.rb', line 36

def get(str, allow_reuse, register_gc)
	if allow_reuse && (name = @reusable[str])
		return name
	end
	name = "global[#{@cnt}]"
	@cnt += 1
	@src << "#{name} = #{str};"
	@src << "rb_global_variable(&(#{name}));" if register_gc
	if allow_reuse
		@reusable[str] = name
	end
	name
end

#to_c_code(init_fun_name = "init_globals") ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/ruby2cext/tools.rb', line 50

def to_c_code(init_fun_name = "init_globals")
	res = []
	res << "static VALUE global[#{@cnt}];" if @cnt > 0
	res << "static void #{init_fun_name}() {"
	res.concat(@src)
	res << "}"
	res.join("\n")
end