Class: JavaScript::Function

Inherits:
Struct
  • Object
show all
Defined in:
lib/javascript.rb

Direct Known Subclasses

BoundFunction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFunction

Returns a new instance of Function.



234
235
236
237
# File 'lib/javascript.rb', line 234

def initialize(*)
  @scope = JavaScript.current_scope
  super
end

Instance Attribute Details

#argsObject

Returns the value of attribute args

Returns:

  • (Object)

    the current value of args



233
234
235
# File 'lib/javascript.rb', line 233

def args
  @args
end

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



233
234
235
# File 'lib/javascript.rb', line 233

def body
  @body
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



233
234
235
# File 'lib/javascript.rb', line 233

def name
  @name
end

Instance Method Details

#apply(target = nil, arg_values) ⇒ Object



247
248
249
# File 'lib/javascript.rb', line 247

def apply(target = nil, arg_values)
  call(target, *arg_values)
end

#arityObject



239
240
241
# File 'lib/javascript.rb', line 239

def arity
  args.length
end

#bind(target) ⇒ Object



251
252
253
# File 'lib/javascript.rb', line 251

def bind(target)
  BoundFunction.new(target, name, args, body)
end

#call(target = nil, *arg_values) ⇒ Object



243
244
245
# File 'lib/javascript.rb', line 243

def call(target = nil, *arg_values)
  ::Object.instance_method(:instance_exec).bind(target).call(*arg_values, &self)
end

#to_procObject



255
256
257
258
259
260
261
262
263
264
265
# File 'lib/javascript.rb', line 255

def to_proc
  parent_scope = @scope
  arg_names    = args.map(&:name)
  unwrapped    = body

  FunctionWrapper.new(arg_names) do |*arg_values|
    locals = Hash[ arg_names.zip(arg_values) ]
    locals[:arguments] = arg_values
    parent_scope.__spawn__(self, locals).__eval__(*arg_values, &unwrapped)
  end
end