builtin_number.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package goja
  2. import (
  3. "math"
  4. "github.com/dop251/goja/ftoa"
  5. )
  6. func (r *Runtime) numberproto_valueOf(call FunctionCall) Value {
  7. this := call.This
  8. if !isNumber(this) {
  9. r.typeErrorResult(true, "Value is not a number")
  10. }
  11. switch t := this.(type) {
  12. case valueInt, valueFloat:
  13. return this
  14. case *Object:
  15. if v, ok := t.self.(*primitiveValueObject); ok {
  16. return v.pValue
  17. }
  18. }
  19. panic(r.NewTypeError("Number.prototype.valueOf is not generic"))
  20. }
  21. func isNumber(v Value) bool {
  22. switch t := v.(type) {
  23. case valueFloat, valueInt:
  24. return true
  25. case *Object:
  26. switch t := t.self.(type) {
  27. case *primitiveValueObject:
  28. return isNumber(t.pValue)
  29. }
  30. }
  31. return false
  32. }
  33. func (r *Runtime) numberproto_toString(call FunctionCall) Value {
  34. if !isNumber(call.This) {
  35. r.typeErrorResult(true, "Value is not a number")
  36. }
  37. var radix int
  38. if arg := call.Argument(0); arg != _undefined {
  39. radix = int(arg.ToInteger())
  40. } else {
  41. radix = 10
  42. }
  43. if radix < 2 || radix > 36 {
  44. panic(r.newError(r.global.RangeError, "toString() radix argument must be between 2 and 36"))
  45. }
  46. num := call.This.ToFloat()
  47. if math.IsNaN(num) {
  48. return stringNaN
  49. }
  50. if math.IsInf(num, 1) {
  51. return stringInfinity
  52. }
  53. if math.IsInf(num, -1) {
  54. return stringNegInfinity
  55. }
  56. if radix == 10 {
  57. return asciiString(fToStr(num, ftoa.ModeStandard, 0))
  58. }
  59. return asciiString(ftoa.FToBaseStr(num, radix))
  60. }
  61. func (r *Runtime) numberproto_toFixed(call FunctionCall) Value {
  62. num := r.toNumber(call.This).ToFloat()
  63. prec := call.Argument(0).ToInteger()
  64. if prec < 0 || prec > 100 {
  65. panic(r.newError(r.global.RangeError, "toFixed() precision must be between 0 and 100"))
  66. }
  67. if math.IsNaN(num) {
  68. return stringNaN
  69. }
  70. return asciiString(fToStr(num, ftoa.ModeFixed, int(prec)))
  71. }
  72. func (r *Runtime) numberproto_toExponential(call FunctionCall) Value {
  73. num := r.toNumber(call.This).ToFloat()
  74. precVal := call.Argument(0)
  75. var prec int64
  76. if precVal == _undefined {
  77. return asciiString(fToStr(num, ftoa.ModeStandardExponential, 0))
  78. } else {
  79. prec = precVal.ToInteger()
  80. }
  81. if math.IsNaN(num) {
  82. return stringNaN
  83. }
  84. if math.IsInf(num, 1) {
  85. return stringInfinity
  86. }
  87. if math.IsInf(num, -1) {
  88. return stringNegInfinity
  89. }
  90. if prec < 0 || prec > 100 {
  91. panic(r.newError(r.global.RangeError, "toExponential() precision must be between 0 and 100"))
  92. }
  93. return asciiString(fToStr(num, ftoa.ModeExponential, int(prec+1)))
  94. }
  95. func (r *Runtime) numberproto_toPrecision(call FunctionCall) Value {
  96. numVal := r.toNumber(call.This)
  97. precVal := call.Argument(0)
  98. if precVal == _undefined {
  99. return numVal.toString()
  100. }
  101. num := numVal.ToFloat()
  102. prec := precVal.ToInteger()
  103. if math.IsNaN(num) {
  104. return stringNaN
  105. }
  106. if math.IsInf(num, 1) {
  107. return stringInfinity
  108. }
  109. if math.IsInf(num, -1) {
  110. return stringNegInfinity
  111. }
  112. if prec < 1 || prec > 100 {
  113. panic(r.newError(r.global.RangeError, "toPrecision() precision must be between 1 and 100"))
  114. }
  115. return asciiString(fToStr(num, ftoa.ModePrecision, int(prec)))
  116. }
  117. func (r *Runtime) number_isFinite(call FunctionCall) Value {
  118. switch arg := call.Argument(0).(type) {
  119. case valueInt:
  120. return valueTrue
  121. case valueFloat:
  122. f := float64(arg)
  123. return r.toBoolean(!math.IsInf(f, 0) && !math.IsNaN(f))
  124. default:
  125. return valueFalse
  126. }
  127. }
  128. func (r *Runtime) number_isInteger(call FunctionCall) Value {
  129. switch arg := call.Argument(0).(type) {
  130. case valueInt:
  131. return valueTrue
  132. case valueFloat:
  133. f := float64(arg)
  134. return r.toBoolean(!math.IsNaN(f) && !math.IsInf(f, 0) && math.Floor(f) == f)
  135. default:
  136. return valueFalse
  137. }
  138. }
  139. func (r *Runtime) number_isNaN(call FunctionCall) Value {
  140. if f, ok := call.Argument(0).(valueFloat); ok && math.IsNaN(float64(f)) {
  141. return valueTrue
  142. }
  143. return valueFalse
  144. }
  145. func (r *Runtime) number_isSafeInteger(call FunctionCall) Value {
  146. arg := call.Argument(0)
  147. if i, ok := arg.(valueInt); ok && i >= -(maxInt-1) && i <= maxInt-1 {
  148. return valueTrue
  149. }
  150. if arg == _negativeZero {
  151. return valueTrue
  152. }
  153. return valueFalse
  154. }
  155. func (r *Runtime) initNumber() {
  156. r.global.NumberPrototype = r.newPrimitiveObject(valueInt(0), r.global.ObjectPrototype, classNumber)
  157. o := r.global.NumberPrototype.self
  158. o._putProp("toExponential", r.newNativeFunc(r.numberproto_toExponential, nil, "toExponential", nil, 1), true, false, true)
  159. o._putProp("toFixed", r.newNativeFunc(r.numberproto_toFixed, nil, "toFixed", nil, 1), true, false, true)
  160. o._putProp("toLocaleString", r.newNativeFunc(r.numberproto_toString, nil, "toLocaleString", nil, 0), true, false, true)
  161. o._putProp("toPrecision", r.newNativeFunc(r.numberproto_toPrecision, nil, "toPrecision", nil, 1), true, false, true)
  162. o._putProp("toString", r.newNativeFunc(r.numberproto_toString, nil, "toString", nil, 1), true, false, true)
  163. o._putProp("valueOf", r.newNativeFunc(r.numberproto_valueOf, nil, "valueOf", nil, 0), true, false, true)
  164. r.global.Number = r.newNativeFunc(r.builtin_Number, r.builtin_newNumber, "Number", r.global.NumberPrototype, 1)
  165. o = r.global.Number.self
  166. o._putProp("EPSILON", _epsilon, false, false, false)
  167. o._putProp("isFinite", r.newNativeFunc(r.number_isFinite, nil, "isFinite", nil, 1), true, false, true)
  168. o._putProp("isInteger", r.newNativeFunc(r.number_isInteger, nil, "isInteger", nil, 1), true, false, true)
  169. o._putProp("isNaN", r.newNativeFunc(r.number_isNaN, nil, "isNaN", nil, 1), true, false, true)
  170. o._putProp("isSafeInteger", r.newNativeFunc(r.number_isSafeInteger, nil, "isSafeInteger", nil, 1), true, false, true)
  171. o._putProp("MAX_SAFE_INTEGER", valueInt(maxInt-1), false, false, false)
  172. o._putProp("MIN_SAFE_INTEGER", valueInt(-(maxInt - 1)), false, false, false)
  173. o._putProp("MIN_VALUE", valueFloat(math.SmallestNonzeroFloat64), false, false, false)
  174. o._putProp("MAX_VALUE", valueFloat(math.MaxFloat64), false, false, false)
  175. o._putProp("NaN", _NaN, false, false, false)
  176. o._putProp("NEGATIVE_INFINITY", _negativeInf, false, false, false)
  177. o._putProp("parseFloat", r.Get("parseFloat"), true, false, true)
  178. o._putProp("parseInt", r.Get("parseInt"), true, false, true)
  179. o._putProp("POSITIVE_INFINITY", _positiveInf, false, false, false)
  180. r.addToGlobal("Number", r.global.Number)
  181. }