Class: Red::DefinitionNode::Method::Singleton

Inherits:
Red::DefinitionNode::Method show all
Defined in:
lib/red/nodes/definition_nodes.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods inherited from String

#%, #*, #+, #<<, #<=>, #==, #=~, #[], #[]=, #capitalize, #capitalize!, #casecmp, #center, #chomp, #chomp!, #chop, #chop!, #concat, #count, #crypt, #delete, #delete!, #downcase, #downcase!, #each, #each_byte, #each_line, #empty?, #eql?, #gsub, #gsub!, #hash, #hex, #include?, #index, #insert, #inspect, #intern, #length, #ljust, #lstrip, #lstrip!, #match, #next, #next!, #oct, #replace, #reverse, #reverse!, #rindex, #rjust, #rstrip, #rstrip!, #scan, #size, #slice, #slice!, #split, #squeeze, #strip, #strip!, #sub, #sub!, #succ, #succ!, #sum, #swapcase, #swapcase!, #to_f, #to_i, #to_s, #to_str, #to_sym, #tr, #tr!, #tr_s, #tr_s!, #upcase, #upcase!, #upto

Constructor Details

#initialize(object_sexp, function_name_sexp, scope_sexp, options) ⇒ Singleton

:defs, expression, :foo, [:scope, (:block, [:args, (:my_arg1, :my_arg2, …, :‘*my_args’, (:block))], (:block_arg, :my_block), expression, expression, …)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/red/nodes/definition_nodes.rb', line 117

def initialize(object_sexp, function_name_sexp, scope_sexp, options)
  return if @@red_import && !@@red_methods.include?(function_name_sexp)
  function        = (METHOD_ESCAPE[function_name_sexp] || function_name_sexp).red!
  @@red_function  = function
  object          = object_sexp.is_sexp?(:self) ? @@namespace_stack.join(".") : object_sexp.red!
  @@red_singleton = object
  singleton       = "%s.m$%s" % [object, function]
  block_sexp      = scope_sexp.assoc(:args) ? (scope_sexp << [:block, scope_sexp.delete(scope_sexp.assoc(:args)), [:nil]]).assoc(:block) : scope_sexp.assoc(:block)
  block_arg_sexp  = block_sexp.delete(block_sexp.assoc(:block_arg)) || ([:block_arg, :_block] if block_sexp.flatten.include?(:yield))
  @@red_block_arg = block_arg_sexp.last if block_arg_sexp
  block_sexp      = [[:nil]] if block_sexp.empty?
  argument_sexps  = block_sexp.assoc(:args)[1..-1] || []
  defaults_sexp   = argument_sexps.delete(argument_sexps.assoc(:block))
  splat_arg       = argument_sexps.pop.to_s[1..-1] if argument_sexps.last && argument_sexps.last.to_s.include?('*')
  argument_sexps += [block_arg_sexp.last] if block_arg_sexp
  args_array      = argument_sexps.map {|argument| argument.red! }
  splatten_args   = "for(var bg=m$blockGivenBool(arguments[arguments.length-1]),l=bg?arguments.length-1:arguments.length,i=#{block_arg_sexp ? argument_sexps.size - 1 : argument_sexps.size},#{splat_arg}=[];i<l;++i){#{splat_arg}.push(arguments[i]);};var #{block_arg_sexp.last rescue :_block}=(bg?c$Proc.m$new(arguments[arguments.length-1]):nil)" if splat_arg
  block_arg       = "var #{block_arg_sexp.last rescue :_block}=(m$blockGivenBool(arguments[arguments.length-1])?c$Proc.m$new(arguments[arguments.length-1]):nil)" if block_arg_sexp && !splat_arg
  defaults        = defaults_sexp.red!(:as_argument_default => true) if defaults_sexp
  arguments       = args_array.join(",")
  scope           = scope_sexp.red!(:force_return => function != 'initialize')
  contents        = [splatten_args, block_arg, defaults, scope].compact.join(";")
  self << "%s=function(%s){%s;}" % [singleton, arguments, contents]
  @@red_block_arg = nil
  @@red_function  = nil
  @@red_singleton = nil
end