Class: Opal::Nodes::BaseFlipFlop

Inherits:
Base
  • Object
show all
Defined in:
lib/opal/nodes/if.rb

Direct Known Subclasses

EFlipFlop, IFlipFlop

Instance Attribute Summary

Attributes inherited from Base

#compiler, #sexp, #type

Attributes included from Closure::NodeSupport

#closure

Instance Method Summary collapse

Methods inherited from Base

#add_gvar, #add_ivar, #add_local, #add_temp, children, #children, #class_variable_owner, #class_variable_owner_nesting_level, #comments, #compile_to_fragments, #error, #expr, #expr?, #expr_or_empty, #expr_or_nil, #fragment, handle, handlers, #has_rescue_else?, #helper, #in_ensure, #in_ensure?, #in_resbody, #in_resbody?, #in_rescue, #in_while?, #initialize, #process, #push, #recv, #recv?, #s, #scope, #source_location, #stmt, #stmt?, #top_scope, truthy_optimize?, #unshift, #while_loop, #with_temp, #wrap

Methods included from Closure::NodeSupport

#closure_is?, #compile_catcher, #generate_thrower, #generate_thrower_without_catcher, #in_closure, #pop_closure, #push_closure, #select_closure, #thrower

Methods included from Helpers

#current_indent, #empty_line, #indent, #js_truthy, #js_truthy_optimize, #line, #mid_to_jsid, #property, #valid_name?

Constructor Details

This class inherits a constructor from Opal::Nodes::Base

Instance Method Details

#compileObject



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/opal/nodes/if.rb', line 398

def compile
  helper :truthy

  func_name = top_scope.new_temp
  flip_flop_state = "#{func_name}.$$ff"

  # Start function definition, checking and initializing it if necessary
  push "(#{func_name} = #{func_name} || function(_start_func, _end_func){"

  # If flip flop state is not defined, set it to false
  push "  var flip_flop = #{flip_flop_state} || false;"

  # If flip flop state is false, call the 'start_condition' function and store its truthy result into flip flop state
  push "  if (!flip_flop) #{flip_flop_state} = flip_flop = $truthy(_start_func());"

  # If flip flop state is true, call the 'end_condition' function and set flip flop state to false if 'end_condition' is truthy
  push "  #{excl}if (flip_flop && $truthy(_end_func())) #{flip_flop_state} = false;"

  # Return current state of flip flop
  push "  return flip_flop;"

  # End function definition
  push "})("

  # Call the function with 'start_condition' and 'end_condition' arguments wrapped in functions to ensure correct binding and delay evaluation
  push "  function() { ", stmt(compiler.returns(start_condition)), " },"
  push "  function() { ", stmt(compiler.returns(end_condition)), " }"
  push ")"
end