class NScript::CodeNode

Constants

UPPERCASE

Attributes

body[R]
bound[R]
name[RW]
params[R]
proto[RW]

Public Class Methods

new(params, body, tag=nil) click to toggle source
# File lib/nscript/parser/nodes.rb, line 572
def initialize(params, body, tag=nil)
  @params = params
  @body   = body
  @bound  = tag == :boundfunc
end

Public Instance Methods

compile_node(o) click to toggle source
# File lib/nscript/parser/nodes.rb, line 582
def compile_node(o)
  shared_scope = o.delete(:shared_scope)
  top          = o.delete(:top)
  o[:scope]    = shared_scope || Scope.new(o[:scope], @body, self)
  o[:return]   = true
  o[:top]      = true
  o[:indent]   = idt(@bound ? 2 : 1)
  o.delete(:no_wrap)
  o.delete(:globals)
  if @params.last.is_a?(SplatNode)
    splat = @params.pop
    splat.index = @params.length
    @body.unshift(splat)
  end
  @params.each {|id| o[:scope].parameter(id.to_s) }
  code  = @body.empty? ? "" : "\n#{@body.compile_with_declarations(o)}\n"
  name_part = @name ? " #{@name}" : ''
  func  = "function#{@bound ? '' : name_part}(#{@params.join(', ')}) {#{code}#{idt(@bound ? 1 : 0)}}"
  func  = "(#{func})" if top && !@bound
  return write(func) unless @bound
  inner = "(function#{name_part}() {\n#{idt(2)}return __func.apply(__this, arguments);\n#{idt(1)}});"
  write("(function(__this) {\n#{idt(1)}var __func = #{func};\n#{idt(1)}return #{inner}\n#{idt}})(this)")
end
constructor?() click to toggle source
# File lib/nscript/parser/nodes.rb, line 578
def constructor?
  @name && @name[0..0][UPPERCASE]
end