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 int uint32_t;
7 typedef unsigned __int64 uint64_t;
8 #else
9 #include <stdint.h>
10 #endif
11
12 #include <time.h>
13
jsM_round(double x)14 static double jsM_round(double x)
15 {
16 if (isnan(x)) return x;
17 if (isinf(x)) return x;
18 if (x == 0) return x;
19 if (x > 0 && x < 0.5) return 0;
20 if (x < 0 && x >= -0.5) return -0;
21 return floor(x + 0.5);
22 }
23
Math_abs(js_State * J)24 static void Math_abs(js_State *J)
25 {
26 js_pushnumber(J, fabs(js_tonumber(J, 1)));
27 }
28
Math_acos(js_State * J)29 static void Math_acos(js_State *J)
30 {
31 js_pushnumber(J, acos(js_tonumber(J, 1)));
32 }
33
Math_asin(js_State * J)34 static void Math_asin(js_State *J)
35 {
36 js_pushnumber(J, asin(js_tonumber(J, 1)));
37 }
38
Math_atan(js_State * J)39 static void Math_atan(js_State *J)
40 {
41 js_pushnumber(J, atan(js_tonumber(J, 1)));
42 }
43
Math_atan2(js_State * J)44 static void Math_atan2(js_State *J)
45 {
46 double y = js_tonumber(J, 1);
47 double x = js_tonumber(J, 2);
48 js_pushnumber(J, atan2(y, x));
49 }
50
Math_ceil(js_State * J)51 static void Math_ceil(js_State *J)
52 {
53 js_pushnumber(J, ceil(js_tonumber(J, 1)));
54 }
55
Math_cos(js_State * J)56 static void Math_cos(js_State *J)
57 {
58 js_pushnumber(J, cos(js_tonumber(J, 1)));
59 }
60
Math_exp(js_State * J)61 static void Math_exp(js_State *J)
62 {
63 js_pushnumber(J, exp(js_tonumber(J, 1)));
64 }
65
Math_floor(js_State * J)66 static void Math_floor(js_State *J)
67 {
68 js_pushnumber(J, floor(js_tonumber(J, 1)));
69 }
70
Math_log(js_State * J)71 static void Math_log(js_State *J)
72 {
73 js_pushnumber(J, log(js_tonumber(J, 1)));
74 }
75
Math_pow(js_State * J)76 static void Math_pow(js_State *J)
77 {
78 double x = js_tonumber(J, 1);
79 double y = js_tonumber(J, 2);
80 if (!isfinite(y) && fabs(x) == 1)
81 js_pushnumber(J, NAN);
82 else
83 js_pushnumber(J, pow(x,y));
84 }
85
Math_random(js_State * J)86 static void Math_random(js_State *J)
87 {
88 /* Lehmer generator with a=48271 and m=2^31-1 */
89 /* Park & Miller (1988). Random Number Generators: Good ones are hard to find. */
90 J->seed = (uint64_t) J->seed * 48271 % 0x7fffffff;
91 js_pushnumber(J, (double) J->seed / 0x7fffffff);
92 }
93
Math_init_random(js_State * J)94 static void Math_init_random(js_State *J)
95 {
96 /* Pick initial seed by scrambling current time with Xorshift. */
97 /* Marsaglia (2003). Xorshift RNGs. */
98 J->seed = time(0) + 123;
99 J->seed ^= J->seed << 13;
100 J->seed ^= J->seed >> 17;
101 J->seed ^= J->seed << 5;
102 J->seed %= 0x7fffffff;
103 }
104
Math_round(js_State * J)105 static void Math_round(js_State *J)
106 {
107 double x = js_tonumber(J, 1);
108 js_pushnumber(J, jsM_round(x));
109 }
110
Math_sin(js_State * J)111 static void Math_sin(js_State *J)
112 {
113 js_pushnumber(J, sin(js_tonumber(J, 1)));
114 }
115
Math_sqrt(js_State * J)116 static void Math_sqrt(js_State *J)
117 {
118 js_pushnumber(J, sqrt(js_tonumber(J, 1)));
119 }
120
Math_tan(js_State * J)121 static void Math_tan(js_State *J)
122 {
123 js_pushnumber(J, tan(js_tonumber(J, 1)));
124 }
125
Math_max(js_State * J)126 static void Math_max(js_State *J)
127 {
128 int i, n = js_gettop(J);
129 double x = -INFINITY;
130 for (i = 1; i < n; ++i) {
131 double y = js_tonumber(J, i);
132 if (isnan(y)) {
133 x = y;
134 break;
135 }
136 if (signbit(x) == signbit(y))
137 x = x > y ? x : y;
138 else if (signbit(x))
139 x = y;
140 }
141 js_pushnumber(J, x);
142 }
143
Math_min(js_State * J)144 static void Math_min(js_State *J)
145 {
146 int i, n = js_gettop(J);
147 double x = INFINITY;
148 for (i = 1; i < n; ++i) {
149 double y = js_tonumber(J, i);
150 if (isnan(y)) {
151 x = y;
152 break;
153 }
154 if (signbit(x) == signbit(y))
155 x = x < y ? x : y;
156 else if (signbit(y))
157 x = y;
158 }
159 js_pushnumber(J, x);
160 }
161
jsB_initmath(js_State * J)162 void jsB_initmath(js_State *J)
163 {
164 Math_init_random(J);
165 js_pushobject(J, jsV_newobject(J, JS_CMATH, J->Object_prototype));
166 {
167 jsB_propn(J, "E", 2.7182818284590452354);
168 jsB_propn(J, "LN10", 2.302585092994046);
169 jsB_propn(J, "LN2", 0.6931471805599453);
170 jsB_propn(J, "LOG2E", 1.4426950408889634);
171 jsB_propn(J, "LOG10E", 0.4342944819032518);
172 jsB_propn(J, "PI", 3.1415926535897932);
173 jsB_propn(J, "SQRT1_2", 0.7071067811865476);
174 jsB_propn(J, "SQRT2", 1.4142135623730951);
175
176 jsB_propf(J, "Math.abs", Math_abs, 1);
177 jsB_propf(J, "Math.acos", Math_acos, 1);
178 jsB_propf(J, "Math.asin", Math_asin, 1);
179 jsB_propf(J, "Math.atan", Math_atan, 1);
180 jsB_propf(J, "Math.atan2", Math_atan2, 2);
181 jsB_propf(J, "Math.ceil", Math_ceil, 1);
182 jsB_propf(J, "Math.cos", Math_cos, 1);
183 jsB_propf(J, "Math.exp", Math_exp, 1);
184 jsB_propf(J, "Math.floor", Math_floor, 1);
185 jsB_propf(J, "Math.log", Math_log, 1);
186 jsB_propf(J, "Math.max", Math_max, 0); /* 2 */
187 jsB_propf(J, "Math.min", Math_min, 0); /* 2 */
188 jsB_propf(J, "Math.pow", Math_pow, 2);
189 jsB_propf(J, "Math.random", Math_random, 0);
190 jsB_propf(J, "Math.round", Math_round, 1);
191 jsB_propf(J, "Math.sin", Math_sin, 1);
192 jsB_propf(J, "Math.sqrt", Math_sqrt, 1);
193 jsB_propf(J, "Math.tan", Math_tan, 1);
194 }
195 js_defglobal(J, "Math", JS_DONTENUM);
196 }
197