1 #include "jsi.h"
2 #include "jsvalue.h"
3 #include "jsbuiltin.h"
4
Math_abs(js_State * J)5 static void Math_abs(js_State *J)
6 {
7 js_pushnumber(J, fabs(js_tonumber(J, 1)));
8 }
9
Math_acos(js_State * J)10 static void Math_acos(js_State *J)
11 {
12 js_pushnumber(J, acos(js_tonumber(J, 1)));
13 }
14
Math_asin(js_State * J)15 static void Math_asin(js_State *J)
16 {
17 js_pushnumber(J, asin(js_tonumber(J, 1)));
18 }
19
Math_atan(js_State * J)20 static void Math_atan(js_State *J)
21 {
22 js_pushnumber(J, atan(js_tonumber(J, 1)));
23 }
24
Math_atan2(js_State * J)25 static void Math_atan2(js_State *J)
26 {
27 double y = js_tonumber(J, 1);
28 double x = js_tonumber(J, 2);
29 js_pushnumber(J, atan2(y, x));
30 }
31
Math_ceil(js_State * J)32 static void Math_ceil(js_State *J)
33 {
34 js_pushnumber(J, ceil(js_tonumber(J, 1)));
35 }
36
Math_cos(js_State * J)37 static void Math_cos(js_State *J)
38 {
39 js_pushnumber(J, cos(js_tonumber(J, 1)));
40 }
41
Math_exp(js_State * J)42 static void Math_exp(js_State *J)
43 {
44 js_pushnumber(J, exp(js_tonumber(J, 1)));
45 }
46
Math_floor(js_State * J)47 static void Math_floor(js_State *J)
48 {
49 js_pushnumber(J, floor(js_tonumber(J, 1)));
50 }
51
Math_log(js_State * J)52 static void Math_log(js_State *J)
53 {
54 js_pushnumber(J, log(js_tonumber(J, 1)));
55 }
56
Math_pow(js_State * J)57 static void Math_pow(js_State *J)
58 {
59 double x = js_tonumber(J, 1);
60 double y = js_tonumber(J, 2);
61 if (!isfinite(y) && fabs(x) == 1)
62 js_pushnumber(J, NAN);
63 else
64 js_pushnumber(J, pow(x,y));
65 }
66
Math_random(js_State * J)67 static void Math_random(js_State *J)
68 {
69 js_pushnumber(J, rand() / (RAND_MAX + 1.0));
70 }
71
do_round(double x)72 static double do_round(double x)
73 {
74 if (isnan(x)) return x;
75 if (isinf(x)) return x;
76 if (x == 0) return x;
77 if (x > 0 && x < 0.5) return 0;
78 if (x < 0 && x >= -0.5) return -0;
79 return floor(x + 0.5);
80 }
81
Math_round(js_State * J)82 static void Math_round(js_State *J)
83 {
84 double x = js_tonumber(J, 1);
85 js_pushnumber(J, do_round(x));
86 }
87
Math_sin(js_State * J)88 static void Math_sin(js_State *J)
89 {
90 js_pushnumber(J, sin(js_tonumber(J, 1)));
91 }
92
Math_sqrt(js_State * J)93 static void Math_sqrt(js_State *J)
94 {
95 js_pushnumber(J, sqrt(js_tonumber(J, 1)));
96 }
97
Math_tan(js_State * J)98 static void Math_tan(js_State *J)
99 {
100 js_pushnumber(J, tan(js_tonumber(J, 1)));
101 }
102
Math_max(js_State * J)103 static void Math_max(js_State *J)
104 {
105 int i, n = js_gettop(J);
106 double x = -INFINITY;
107 for (i = 1; i < n; ++i) {
108 double y = js_tonumber(J, i);
109 if (isnan(y)) {
110 x = y;
111 break;
112 }
113 if (signbit(x) == signbit(y))
114 x = x > y ? x : y;
115 else if (signbit(x))
116 x = y;
117 }
118 js_pushnumber(J, x);
119 }
120
Math_min(js_State * J)121 static void Math_min(js_State *J)
122 {
123 int i, n = js_gettop(J);
124 double x = INFINITY;
125 for (i = 1; i < n; ++i) {
126 double y = js_tonumber(J, i);
127 if (isnan(y)) {
128 x = y;
129 break;
130 }
131 if (signbit(x) == signbit(y))
132 x = x < y ? x : y;
133 else if (signbit(y))
134 x = y;
135 }
136 js_pushnumber(J, x);
137 }
138
jsB_initmath(js_State * J)139 void jsB_initmath(js_State *J)
140 {
141 js_pushobject(J, jsV_newobject(J, JS_CMATH, J->Object_prototype));
142 {
143 jsB_propn(J, "E", 2.7182818284590452354);
144 jsB_propn(J, "LN10", 2.302585092994046);
145 jsB_propn(J, "LN2", 0.6931471805599453);
146 jsB_propn(J, "LOG2E", 1.4426950408889634);
147 jsB_propn(J, "LOG10E", 0.4342944819032518);
148 jsB_propn(J, "PI", 3.1415926535897932);
149 jsB_propn(J, "SQRT1_2", 0.7071067811865476);
150 jsB_propn(J, "SQRT2", 1.4142135623730951);
151
152 jsB_propf(J, "Math.abs", Math_abs, 1);
153 jsB_propf(J, "Math.acos", Math_acos, 1);
154 jsB_propf(J, "Math.asin", Math_asin, 1);
155 jsB_propf(J, "Math.atan", Math_atan, 1);
156 jsB_propf(J, "Math.atan2", Math_atan2, 2);
157 jsB_propf(J, "Math.ceil", Math_ceil, 1);
158 jsB_propf(J, "Math.cos", Math_cos, 1);
159 jsB_propf(J, "Math.exp", Math_exp, 1);
160 jsB_propf(J, "Math.floor", Math_floor, 1);
161 jsB_propf(J, "Math.log", Math_log, 1);
162 jsB_propf(J, "Math.max", Math_max, 0); /* 2 */
163 jsB_propf(J, "Math.min", Math_min, 0); /* 2 */
164 jsB_propf(J, "Math.pow", Math_pow, 2);
165 jsB_propf(J, "Math.random", Math_random, 0);
166 jsB_propf(J, "Math.round", Math_round, 1);
167 jsB_propf(J, "Math.sin", Math_sin, 1);
168 jsB_propf(J, "Math.sqrt", Math_sqrt, 1);
169 jsB_propf(J, "Math.tan", Math_tan, 1);
170 }
171 js_defglobal(J, "Math", JS_DONTENUM);
172 }
173