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