Method: Rubex::SymbolTable::Scope::BeginBlock#method_missing
- Defined in:
- lib/rubex/symbol_table/scope.rb
#method_missing(meth, *args, &block) ⇒ Object
IMPORTANT NOTE TO PROGRAMMER:
A problem with the BeginBlock is that in the Ruby world, it must share scope with the method above and below it. However, when translating to C, the begin block must be put inside its own C function, which is then sent as a callback to rb_protect().
The same as above applies to a no_gil block, which is why the NoGilBlock (defined below) inherits from this class and completely imitates its behaviour.
As a result, it is necesary to ‘upgrade’ the local variables present inside the begin block to C global variables so that they can be shared between the callback C function encapsulating the begin block and the ruby method that has a begin block defined inside it.
Now, since variables must be shared, it is also necessary to share the scopes between these functions. Therefore, the BeginBlock scope uses method_missing to capture whatever methods calls it has received and redirect those to @outer_scope, which is the scope of the method that contains the begin block. Whenever a method call to @outer_scope returns a SymbolTable::Entry object, that object is read to check if it is a variable. If yes, it is added into the @block_entries Array which stores the entries that need to be upgraded to C global variables.
Now as a side effect of this behaviour, the various *_entries Arrays of @outer_scope get carried forward into this begin block callback. Therefore these variables get declared inside the begin block callback as well. So whenever one of these Arrays is called for this particular scope, we preturn an empty array so that nothing gets declared.
313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/rubex/symbol_table/scope.rb', line 313 def method_missing meth, *args, &block return [] if meth == :var_entries ret = @outer_scope.send(meth, *args, &block) if ret.is_a?(Rubex::SymbolTable::Entry) if !ret.extern? if !ret.type.c_function? && !ret.type.ruby_method? && !ret.type.ruby_class? @block_entries << ret end end end ret end |