jsnumber.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "jsi.h"
  2. #if defined(_MSC_VER) && (_MSC_VER < 1700) /* VS2012 has stdint.h */
  3. typedef unsigned __int64 uint64_t;
  4. #else
  5. #include <stdint.h>
  6. #endif
  7. static void jsB_new_Number(js_State *J)
  8. {
  9. js_newnumber(J, js_gettop(J) > 1 ? js_tonumber(J, 1) : 0);
  10. }
  11. static void jsB_Number(js_State *J)
  12. {
  13. js_pushnumber(J, js_gettop(J) > 1 ? js_tonumber(J, 1) : 0);
  14. }
  15. static void Np_valueOf(js_State *J)
  16. {
  17. js_Object *self = js_toobject(J, 0);
  18. if (self->type != JS_CNUMBER) js_typeerror(J, "not a number");
  19. js_pushnumber(J, self->u.number);
  20. }
  21. static void Np_toString(js_State *J)
  22. {
  23. char buf[100];
  24. js_Object *self = js_toobject(J, 0);
  25. int radix = js_isundefined(J, 1) ? 10 : js_tointeger(J, 1);
  26. double x = 0;
  27. if (self->type != JS_CNUMBER)
  28. js_typeerror(J, "not a number");
  29. x = self->u.number;
  30. if (radix == 10) {
  31. js_pushstring(J, jsV_numbertostring(J, buf, x));
  32. return;
  33. }
  34. if (radix < 2 || radix > 36)
  35. js_rangeerror(J, "invalid radix");
  36. /* lame number to string conversion for any radix from 2 to 36 */
  37. {
  38. static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  39. double number = x;
  40. int sign = x < 0;
  41. js_Buffer *sb = NULL;
  42. uint64_t u, limit = ((uint64_t)1<<52);
  43. int ndigits, exp, point;
  44. if (number == 0) { js_pushstring(J, "0"); return; }
  45. if (isnan(number)) { js_pushstring(J, "NaN"); return; }
  46. if (isinf(number)) { js_pushstring(J, sign ? "-Infinity" : "Infinity"); return; }
  47. if (sign)
  48. number = -number;
  49. /* fit as many digits as we want in an int */
  50. exp = 0;
  51. while (number * pow(radix, exp) > limit)
  52. --exp;
  53. while (number * pow(radix, exp+1) < limit)
  54. ++exp;
  55. u = number * pow(radix, exp) + 0.5;
  56. /* trim trailing zeros */
  57. while (u > 0 && (u % radix) == 0) {
  58. u /= radix;
  59. --exp;
  60. }
  61. /* serialize digits */
  62. ndigits = 0;
  63. while (u > 0) {
  64. buf[ndigits++] = digits[u % radix];
  65. u /= radix;
  66. }
  67. point = ndigits - exp;
  68. if (js_try(J)) {
  69. js_free(J, sb);
  70. js_throw(J);
  71. }
  72. if (sign)
  73. js_putc(J, &sb, '-');
  74. if (point <= 0) {
  75. js_putc(J, &sb, '0');
  76. js_putc(J, &sb, '.');
  77. while (point++ < 0)
  78. js_putc(J, &sb, '0');
  79. while (ndigits-- > 0)
  80. js_putc(J, &sb, buf[ndigits]);
  81. } else {
  82. while (ndigits-- > 0) {
  83. js_putc(J, &sb, buf[ndigits]);
  84. if (--point == 0 && ndigits > 0)
  85. js_putc(J, &sb, '.');
  86. }
  87. while (point-- > 0)
  88. js_putc(J, &sb, '0');
  89. }
  90. js_putc(J, &sb, 0);
  91. js_pushstring(J, sb->s);
  92. js_endtry(J);
  93. js_free(J, sb);
  94. }
  95. }
  96. /* Customized ToString() on a number */
  97. static void numtostr(js_State *J, const char *fmt, int w, double n)
  98. {
  99. /* buf needs to fit printf("%.20f", 1e20) */
  100. char buf[50], *e;
  101. sprintf(buf, fmt, w, n);
  102. e = strchr(buf, 'e');
  103. if (e) {
  104. int exp = atoi(e+1);
  105. sprintf(e, "e%+d", exp);
  106. }
  107. js_pushstring(J, buf);
  108. }
  109. static void Np_toFixed(js_State *J)
  110. {
  111. js_Object *self = js_toobject(J, 0);
  112. int width = js_tointeger(J, 1);
  113. char buf[32];
  114. double x;
  115. if (self->type != JS_CNUMBER) js_typeerror(J, "not a number");
  116. if (width < 0) js_rangeerror(J, "precision %d out of range", width);
  117. if (width > 20) js_rangeerror(J, "precision %d out of range", width);
  118. x = self->u.number;
  119. if (isnan(x) || isinf(x) || x <= -1e21 || x >= 1e21)
  120. js_pushstring(J, jsV_numbertostring(J, buf, x));
  121. else
  122. numtostr(J, "%.*f", width, x);
  123. }
  124. static void Np_toExponential(js_State *J)
  125. {
  126. js_Object *self = js_toobject(J, 0);
  127. int width = js_tointeger(J, 1);
  128. char buf[32];
  129. double x;
  130. if (self->type != JS_CNUMBER) js_typeerror(J, "not a number");
  131. if (width < 0) js_rangeerror(J, "precision %d out of range", width);
  132. if (width > 20) js_rangeerror(J, "precision %d out of range", width);
  133. x = self->u.number;
  134. if (isnan(x) || isinf(x))
  135. js_pushstring(J, jsV_numbertostring(J, buf, x));
  136. else
  137. numtostr(J, "%.*e", width, x);
  138. }
  139. static void Np_toPrecision(js_State *J)
  140. {
  141. js_Object *self = js_toobject(J, 0);
  142. int width = js_tointeger(J, 1);
  143. char buf[32];
  144. double x;
  145. if (self->type != JS_CNUMBER) js_typeerror(J, "not a number");
  146. if (width < 1) js_rangeerror(J, "precision %d out of range", width);
  147. if (width > 21) js_rangeerror(J, "precision %d out of range", width);
  148. x = self->u.number;
  149. if (isnan(x) || isinf(x))
  150. js_pushstring(J, jsV_numbertostring(J, buf, x));
  151. else
  152. numtostr(J, "%.*g", width, x);
  153. }
  154. void jsB_initnumber(js_State *J)
  155. {
  156. J->Number_prototype->u.number = 0;
  157. js_pushobject(J, J->Number_prototype);
  158. {
  159. jsB_propf(J, "Number.prototype.valueOf", Np_valueOf, 0);
  160. jsB_propf(J, "Number.prototype.toString", Np_toString, 1);
  161. jsB_propf(J, "Number.prototype.toLocaleString", Np_toString, 0);
  162. jsB_propf(J, "Number.prototype.toFixed", Np_toFixed, 1);
  163. jsB_propf(J, "Number.prototype.toExponential", Np_toExponential, 1);
  164. jsB_propf(J, "Number.prototype.toPrecision", Np_toPrecision, 1);
  165. }
  166. js_newcconstructor(J, jsB_Number, jsB_new_Number, "Number", 0); /* 1 */
  167. {
  168. jsB_propn(J, "MAX_VALUE", 1.7976931348623157e+308);
  169. jsB_propn(J, "MIN_VALUE", 5e-324);
  170. jsB_propn(J, "NaN", NAN);
  171. jsB_propn(J, "NEGATIVE_INFINITY", -INFINITY);
  172. jsB_propn(J, "POSITIVE_INFINITY", INFINITY);
  173. }
  174. js_defglobal(J, "Number", JS_DONTENUM);
  175. }