Class: Ruby2CExtension::Scopes::Scope
- Inherits:
-
Object
- Object
- Ruby2CExtension::Scopes::Scope
- Defined in:
- lib/ruby2cext/scopes.rb
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.
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_tbl ⇒ Object
Returns the value of attribute closure_tbl.
17 18 19 |
# File 'lib/ruby2cext/scopes.rb', line 17 def closure_tbl @closure_tbl end |
#need_heap ⇒ Object
Returns the value of attribute need_heap.
17 18 19 |
# File 'lib/ruby2cext/scopes.rb', line 17 def need_heap @need_heap end |
#tbl ⇒ Object (readonly)
Returns the value of attribute tbl.
16 17 18 |
# File 'lib/ruby2cext/scopes.rb', line 16 def tbl @tbl end |
#vmode ⇒ Object (readonly)
Returns the value of attribute vmode.
16 17 18 |
# File 'lib/ruby2cext/scopes.rb', line 16 def vmode @vmode end |
#vmode_methods ⇒ Object (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
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
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_ary ⇒ Object
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
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_code ⇒ Object
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_scope ⇒ Object
56 57 58 |
# File 'lib/ruby2cext/scopes.rb', line 56 def new_dyna_scope DynaScope.new(self, nil, 1) end |
#var_ptr_for_wrap ⇒ Object
60 61 62 |
# File 'lib/ruby2cext/scopes.rb', line 60 def var_ptr_for_wrap tbl.empty? ? nil : "lvar" end |
#vmode_def_fun ⇒ Object
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
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 |