Class: Ruby2CExtension::Scopes::Scope
Constant Summary collapse
- VMODES =
[:public, :private, :protected, :module_function].freeze
Instance Attribute Summary collapse
-
#closure_tbl ⇒ Object
Returns the value of attribute closure_tbl.
-
#need_heap ⇒ Object
Returns the value of attribute need_heap.
-
#tbl ⇒ Object
readonly
Returns the value of attribute tbl.
-
#vmode ⇒ Object
readonly
Returns the value of attribute vmode.
-
#vmode_methods ⇒ Object
readonly
Returns the value of attribute vmode_methods.
Instance Method Summary collapse
- #get_dvar(arg) ⇒ Object (also: #get_dvar_curr, #get_dvar_ary)
- #get_lvar(sym) ⇒ Object
- #get_lvar_ary ⇒ Object
- #get_lvar_idx(i) ⇒ Object
- #init_c_code ⇒ Object
-
#initialize(tbl, vmode_methods = VMODES, private_vmode = false) ⇒ Scope
constructor
A new instance of Scope.
- #new_dyna_scope ⇒ Object
- #var_ptr_for_wrap ⇒ Object
- #vmode_def_fun ⇒ Object
- #vmode_method?(method_id) ⇒ Boolean
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_tbl ⇒ Object
Returns the value of attribute closure_tbl.
15 16 17 |
# File 'lib/ruby2cext/scopes.rb', line 15 def closure_tbl @closure_tbl end |
#need_heap ⇒ Object
Returns the value of attribute need_heap.
15 16 17 |
# File 'lib/ruby2cext/scopes.rb', line 15 def need_heap @need_heap end |
#tbl ⇒ Object (readonly)
Returns the value of attribute tbl.
14 15 16 |
# File 'lib/ruby2cext/scopes.rb', line 14 def tbl @tbl end |
#vmode ⇒ Object (readonly)
Returns the value of attribute vmode.
14 15 16 |
# File 'lib/ruby2cext/scopes.rb', line 14 def vmode @vmode end |
#vmode_methods ⇒ Object (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
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
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_ary ⇒ Object
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
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_code ⇒ Object
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_scope ⇒ Object
54 55 56 |
# File 'lib/ruby2cext/scopes.rb', line 54 def new_dyna_scope DynaScope.new(self, nil, 1) end |
#var_ptr_for_wrap ⇒ Object
58 59 60 |
# File 'lib/ruby2cext/scopes.rb', line 58 def var_ptr_for_wrap tbl.empty? ? nil : "lvar" end |
#vmode_def_fun ⇒ Object
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
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 |