Module: RubyRunJs::JsNumberMethods

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

Constant Summary collapse

RADIX_SYMBOLS =
{
  0 => '0',
  1 => '1',
  2 => '2',
  3 => '3',
  4 => '4',
  5 => '5',
  6 => '6',
  7 => '7',
  8 => '8',
  9 => '9',
  10 => 'a',
  11 => 'b',
  12 => 'c',
  13 => 'd',
  14 => 'e',
  15 => 'f',
  16 => 'g',
  17 => 'h',
  18 => 'i',
  19 => 'j',
  20 => 'k',
  21 => 'l',
  22 => 'm',
  23 => 'n',
  24 => 'o',
  25 => 'p',
  26 => 'q',
  27 => 'r',
  28 => 's',
  29 => 't',
  30 => 'u',
  31 => 'v',
  32 => 'w',
  33 => 'x',
  34 => 'y',
  35 => 'z'
}.freeze

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



59
60
61
62
63
64
# File 'lib/ruby_run_js/object_methods/js_number.rb', line 59

def constructor(builtin, this, *args)
  if args.length == 0
    return 0.0
  end
  to_number(args[0])
end

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



66
67
68
# File 'lib/ruby_run_js/object_methods/js_number.rb', line 66

def constructor_new(builtin, this, *args)
  builtin.new_number(args.length == 0 ? 0.0 : args[0])
end

.property_valuesObject



49
50
51
52
53
54
55
56
57
# File 'lib/ruby_run_js/object_methods/js_number.rb', line 49

def property_values
  {
    'MAX_VALUE' => Float::MAX,
    'MIN_VALUE' => Float::MIN,
    'NAN' => Float::NAN,
    'NEGATIVE_INFINITY' => -Float::INFINITY,
    'POSITIVE_INFINITY' => Float::INFINITY
  }
end

.prototype_toExponential(builtin, this, fraction_digits) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ruby_run_js/object_methods/js_number.rb', line 131

def prototype_toExponential(builtin, this, fraction_digits)
  if this.js_class != 'Number'
    raise make_error('TypeError', 'Number.prototype.toExponential is not generic')
  end

  f = to_integer(fraction_digits)
  if f < 0 || f > 20
    raise make_error('RangeError', 'toExponential() digits argument must be between 0 and 20')
  end

  if this.js_type == :Object
    this = this.value
  end

  if this.infinite?
    return this > 0 ? 'Infinity' : '-Infinity'
  end

  if this.nan?
    return 'NaN'
  end

  "%0.#{f}e" % this
end

.prototype_toFixed(builtin, this, fraction_digits) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ruby_run_js/object_methods/js_number.rb', line 106

def prototype_toFixed(builtin, this, fraction_digits)
  if this.js_class != 'Number'
    raise make_error('TypeError', 'Number.prototype.toFixed is not generic')
  end

  f = to_integer(fraction_digits)
  if f < 0 || f > 20
    raise make_error('RangeError', 'toFixed() digits argument must be between 0 and 20')
  end

  if this.js_type == :Object
    this = this.value
  end

  if this.infinite?
    return this > 0 ? 'Infinity' : '-Infinity'
  end

  if this.nan?
    return 'NaN'
  end

  "%0.#{f}f" % this
end

.prototype_toPrecision(builtin, this, precision) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ruby_run_js/object_methods/js_number.rb', line 156

def prototype_toPrecision(builtin, this, precision)
  if this.js_class != 'Number'
    raise make_error('TypeError', 'Number.prototype.toPrecision is not generic')
  end

  if this.js_type == :Object
    this = this.value
  end

  return to_string(this) if precision == undefined

  f = to_integer(precision)

  if this.infinite?
    return this > 0 ? 'Infinity' : '-Infinity'
  end

  if this.nan?
    return 'NaN'
  end

  if f < 1 || f > 20
    raise make_error('RangeError', 'toPrecision() digits argument must be between 1 and 20')
  end

  digs = f - this.to_i.to_s.length

  digs >= 0 ? ("%0.#{digs}f" % this) : ("%0.#{f - 1}f" % this)
end

.prototype_toString(builtin, this, radix) ⇒ Object Also known as: prototype_toLocaleString



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ruby_run_js/object_methods/js_number.rb', line 70

def prototype_toString(builtin, this, radix)
  if this.js_class != 'Number'
    raise make_error('TypeError', 'Number.prototype.toString is not generic')
  end

  radix = radix == undefined ? 10 : to_integer(radix)
  if radix < 2 || radix > 36
    raise make_error('RangeError', 'Number.prototype.toString() radix argument must be an integer between 2 and 36')
  end

  num = to_integer(this)

  sign = ''
  if num < 0
    sign = '-'
    num = -num
  end
  result = ''
  while num > 0
    s = RADIX_SYMBOLS[num % radix]
    num = num / radix
    result = s + result
  end
  sign + (result == '' ? '0' : result)
end

.prototype_valueOf(builtin, this) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/ruby_run_js/object_methods/js_number.rb', line 96

def prototype_valueOf(builtin, this)
  if this.js_class != 'Number'
    raise make_error('TypeError', 'Number.prototype.valueOf is not generic')
  end
  if this.js_type == :Object
    this = this.value
  end
  this
end