Class: Ruby2CExtension::Tools::SymbolManager

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

Overview

keeps track of used symbols and provides global variables that hold the corresponding ID

Instance Method Summary collapse

Constructor Details

#initializeSymbolManager

Returns a new instance of SymbolManager.



10
11
12
# File 'lib/ruby2cext/tools.rb', line 10

def initialize
  @syms = {}
end

Instance Method Details

#get(sym) ⇒ Object

returns the var name for sym



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

def get(sym)
  idx = (@syms[sym] ||= @syms.size)
  "sym[#{idx}] /* #{sym.to_s.gsub(/[^\w<=>+\-*\/!?@$|&~.]/, "_").gsub("*/", "__")} */"
end

#to_c_code(init_fun_name = "init_syms") ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/ruby2cext/tools.rb', line 20

def to_c_code(init_fun_name = "init_syms")
  res = []
  res << "static ID sym[#{@syms.size}];" unless @syms.empty?
  res << "static void #{init_fun_name}() {"
  @syms.sort_by { |sym, idx| idx }.each { |sym, idx|
    res << "sym[#{idx}] = rb_intern(#{sym.to_s.to_c_strlit});"
  }
  res << "}"
  res.join("\n")
end