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.



33
34
35
36
37
# File 'lib/ruby2cext/tools.rb', line 33

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

Instance Method Details

#get(str, allow_reuse, register_gc) ⇒ Object

returns the var name for sym



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

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



54
55
56
57
58
59
60
61
# File 'lib/ruby2cext/tools.rb', line 54

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