Class: Red::DefinitionNode::Method::Singleton
- Inherits:
-
Red::DefinitionNode::Method
- Object
- String
- Red::DefinitionNode
- Red::DefinitionNode::Method
- Red::DefinitionNode::Method::Singleton
- Defined in:
- lib/red/nodes/definition_nodes.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#initialize(object_sexp, function_name_sexp, scope_sexp, options) ⇒ Singleton
constructor
[:defs, expression, :foo, [:scope, (:block, [:args, (:my_arg1, :my_arg2, …, :‘*my_args’, (:block))], (:block_arg, :my_block), expression, expression, …)].
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!, #strip_scripts, #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, …)
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/red/nodes/definition_nodes.rb', line 121 def initialize(object_sexp, function_name_sexp, scope_sexp, ) 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! } maximum_args = args_array.length - (block_arg_sexp ? 1 : 0) minimum_args = maximum_args - (defaults_sexp ? defaults_sexp.flatten.select {|x| x == :lasgn}.length : 0) splatten_args = "for(var l=arguments.length,i=#{argument_sexps.size},#{splat_arg}=[];i<l;++i){#{splat_arg}.push(arguments[i]);}" if splat_arg && !block_arg_sexp splat_block = "for(var l=arguments.length,bg=m$block_given_bool(arguments[l-1]),l=bg?l-1:l,i=#{argument_sexps.size - 1},#{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_sexp block_arg = "var z=arguments[arguments.length-1],bg=m$block_given_bool(z),#{block_arg_sexp.last rescue :_block}=bg?c$Proc.m$new(z):nil" if block_arg_sexp && !splat_arg args_checker = "$a(%s,%s,arguments,%s)" % [minimum_args, (splat_arg ? -1 : maximum_args), (splat_block || block_arg ? "bg?1:0" : 0)]#{}"arguments[arguments.length-1]&&arguments[arguments.length-1].__block__?1:0")] 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 || splat_block || block_arg), args_checker, defaults, scope].compact.join(";") self << "%s=function(%s){%s;}" % [singleton, arguments, contents] @@red_block_arg = nil @@red_function = nil @@red_singleton = nil end |