Module: RubyRunJs::JsFunctionMethods

Extended by:
Helper
Defined in:
lib/ruby_run_js/object_methods/js_function.rb

Class Method Summary collapse

Methods included from Helper

check_object, get_member, get_member_dot, is_accessor_descriptor, is_callable, is_data_descriptor, is_generic_descriptor, is_primitive, make_error, strict_equality

Methods included from ConversionHelper

#convert_to_js_type, #to_boolean, #to_int32, #to_integer, #to_number, #to_object, #to_primitive, #to_string, #to_uint16, #to_uint32

Class Method Details

.constructor(builtin, this, *args) ⇒ Object



8
9
10
# File 'lib/ruby_run_js/object_methods/js_function.rb', line 8

def constructor(builtin, this, *args)
  constructor_new(builtin, this, *args)
end

.constructor_new(builtin, this, *args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ruby_run_js/object_methods/js_function.rb', line 12

def constructor_new(builtin, this, *args)
  argCount = args.length
  body = ''
  param = ''
  if argCount > 0
    body = args[-1]
    if argCount > 1
      param = args[0, argCount - 1].join(',')
    end
  end
  body = to_string(body)

  builtin.interpreter.build_js_func_in_runtime(param, body)
end

.prototype_apply(builtin, this, this_arg, arg_array) ⇒ Object

Parameters:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby_run_js/object_methods/js_function.rb', line 46

def prototype_apply(builtin, this, this_arg, arg_array)
  unless is_callable(this)
    raise make_error('TypeError', 'Function.prototype.apply is not generic')
  end
  if arg_array == null || arg_array == undefined
    return this.call(this_arg, [])
  end
  unless arg_array.js_type == :Object
    raise make_error('TypeError', 'argList argument to Function.prototype.apply must an Object')
  end

  n = to_uint32(arg_array.get('length'))

  this.call(this_arg, n.times.map { |i| arg_array.get(to_string(i)) })
end

.prototype_bind(builtin, this, this_arg, *args) ⇒ Object

Parameters:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruby_run_js/object_methods/js_function.rb', line 63

def prototype_bind(builtin, this, this_arg, *args)
  unless is_callable(this)
    raise make_error('TypeError', 'Function.prototype.bind is not generic')
  end
  bound_method = proc { |_, _, _dummy_this, *extra_args|
    this.call(this_arg, args + extra_args)
  }
  js_bound = builtin.new_native_function(bound_method, 'boundFunc')
  js_bound.own['length'] = {
    'value' => [0, this.get('length') - args.length].max.to_f,
    'writable' => false,
    'enumerable' => false,
    'configurable' => false
  }
  js_bound
end

.prototype_call(builtin, this, this_arg, *args) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/ruby_run_js/object_methods/js_function.rb', line 37

def prototype_call(builtin, this, this_arg, *args)
  unless is_callable(this)
    raise make_error('TypeError', 'Function.prototype.call is not generic')
  end

  this.call(this_arg, args)
end

.prototype_toString(builtin, this) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/ruby_run_js/object_methods/js_function.rb', line 28

def prototype_toString(builtin, this)
  unless is_callable(this)
    raise make_error('TypeError', 'Function.prototype.toString is not generic')
  end

  args = this.params.join(',')
  "function #{this.name}(#{args}) { [native code] }"
end