Class: StyleScript::CallNode

Inherits:
Node
  • Object
show all
Defined in:
lib/style_script/nodes.rb

Overview

Node for a function invocation. Takes care of converting super() calls into calls against the prototype’s function of the same name.

Constant Summary

Constants inherited from Node

Node::TAB

Instance Method Summary collapse

Methods inherited from Node

#children, children, #compile, #compile_closure, #contains?, #idt, statement, #statement?, statement_only, #statement_only?, top_sensitive, #top_sensitive?, #unwrap, #write

Constructor Details

#initialize(variable, arguments = []) ⇒ CallNode

Returns a new instance of CallNode.



324
325
326
327
# File 'lib/style_script/nodes.rb', line 324

def initialize(variable, arguments=[])
  @variable, @arguments = variable, arguments
  @prefix = ''
end

Instance Method Details

#<<(argument) ⇒ Object



334
335
336
337
# File 'lib/style_script/nodes.rb', line 334

def <<(argument)
  @arguments << argument
  self
end

#compile_node(o) ⇒ Object

Compile a vanilla function call.



340
341
342
343
344
345
# File 'lib/style_script/nodes.rb', line 340

def compile_node(o)
  return write(compile_splat(o)) if @arguments.any? {|a| a.is_a?(SplatNode) }
  args = @arguments.map{|a| a.compile(o) }.join(', ')
  return write(compile_super(args, o)) if @variable == 'super'
  write("#{@prefix}#{@variable.compile(o)}(#{args})")
end

#compile_reference(o) ⇒ Object

If the code generation wished to use the result of a function call in multiple places, ensure that the function is only ever called once.



371
372
373
374
375
# File 'lib/style_script/nodes.rb', line 371

def compile_reference(o)
  reference = o[:scope].free_variable
  call = ParentheticalNode.new(AssignNode.new(reference, self))
  return call, reference
end

#compile_splat(o) ⇒ Object

Compile a function call being passed variable arguments.



358
359
360
361
362
363
364
365
366
367
# File 'lib/style_script/nodes.rb', line 358

def compile_splat(o)
  meth = @variable.compile(o)
  obj  = @variable.source || 'this'
  args = @arguments.map do |arg|
    code = arg.compile(o)
    code = arg.is_a?(SplatNode) ? code : "[#{code}]"
    arg.equal?(@arguments.first) ? code : ".concat(#{code})"
  end
  "#{@prefix}#{meth}.apply(#{obj}, #{args.join('')})"
end

#compile_super(args, o) ⇒ Object

Compile a call against the superclass’s implementation of the current function.



348
349
350
351
352
353
354
355
# File 'lib/style_script/nodes.rb', line 348

def compile_super(args, o)
  methname = o[:scope].function.name
  arg_part = args.empty? ? '' : ", #{args}"
  meth     = o[:scope].function.proto ?
                "#{o[:scope].function.proto}.__superClass__.#{methname}" :
                "#{methname}.__superClass__.constructor"
  "#{meth}.call(this#{arg_part})"
end

#new_instanceObject



329
330
331
332
# File 'lib/style_script/nodes.rb', line 329

def new_instance
  @prefix = "new "
  self
end