Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7639 views
1
#include "jsi.h"
2
#include "jsvalue.h"
3
#include "jsbuiltin.h"
4
5
static void jsB_new_Number(js_State *J)
6
{
7
js_newnumber(J, js_gettop(J) > 1 ? js_tonumber(J, 1) : 0);
8
}
9
10
static void jsB_Number(js_State *J)
11
{
12
js_pushnumber(J, js_gettop(J) > 1 ? js_tonumber(J, 1) : 0);
13
}
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
22
static void Np_toString(js_State *J)
23
{
24
char buf[32];
25
js_Object *self = js_toobject(J, 0);
26
int radix = js_isundefined(J, 1) ? 10 : js_tointeger(J, 1);
27
if (self->type != JS_CNUMBER) js_typeerror(J, "not a number");
28
if (radix < 2 || radix > 36)
29
js_rangeerror(J, "invalid radix");
30
if (radix != 10)
31
js_rangeerror(J, "invalid radix");
32
js_pushstring(J, jsV_numbertostring(J, buf, self->u.number));
33
}
34
35
/* Customized ToString() on a number */
36
static void numtostr(js_State *J, const char *fmt, int w, double n)
37
{
38
char buf[32], *e;
39
if (isnan(n)) js_pushliteral(J, "NaN");
40
else if (isinf(n)) js_pushliteral(J, n < 0 ? "-Infinity" : "Infinity");
41
else if (n == 0) js_pushliteral(J, "0");
42
else {
43
if (w < 1) w = 1;
44
if (w > 17) w = 17;
45
sprintf(buf, fmt, w, n);
46
e = strchr(buf, 'e');
47
if (e) {
48
int exp = atoi(e+1);
49
sprintf(e, "e%+d", exp);
50
}
51
js_pushstring(J, buf);
52
}
53
}
54
55
static void Np_toFixed(js_State *J)
56
{
57
js_Object *self = js_toobject(J, 0);
58
int width = js_tointeger(J, 1);
59
if (self->type != JS_CNUMBER) js_typeerror(J, "not a number");
60
numtostr(J, "%.*f", width, self->u.number);
61
}
62
63
static void Np_toExponential(js_State *J)
64
{
65
js_Object *self = js_toobject(J, 0);
66
int width = js_tointeger(J, 1);
67
if (self->type != JS_CNUMBER) js_typeerror(J, "not a number");
68
numtostr(J, "%.*e", width, self->u.number);
69
}
70
71
static void Np_toPrecision(js_State *J)
72
{
73
js_Object *self = js_toobject(J, 0);
74
int width = js_tointeger(J, 1);
75
if (self->type != JS_CNUMBER) js_typeerror(J, "not a number");
76
numtostr(J, "%.*g", width, self->u.number);
77
}
78
79
void jsB_initnumber(js_State *J)
80
{
81
J->Number_prototype->u.number = 0;
82
83
js_pushobject(J, J->Number_prototype);
84
{
85
jsB_propf(J, "valueOf", Np_valueOf, 0);
86
jsB_propf(J, "toString", Np_toString, 1);
87
jsB_propf(J, "toLocaleString", Np_toString, 0);
88
jsB_propf(J, "toFixed", Np_toFixed, 1);
89
jsB_propf(J, "toExponential", Np_toExponential, 1);
90
jsB_propf(J, "toPrecision", Np_toPrecision, 1);
91
}
92
js_newcconstructor(J, jsB_Number, jsB_new_Number, "Number", 1);
93
{
94
jsB_propn(J, "MAX_VALUE", 1.7976931348623157e+308);
95
jsB_propn(J, "MIN_VALUE", 5e-324);
96
jsB_propn(J, "NaN", NAN);
97
jsB_propn(J, "NEGATIVE_INFINITY", -INFINITY);
98
jsB_propn(J, "POSITIVE_INFINITY", INFINITY);
99
}
100
js_defglobal(J, "Number", JS_DONTENUM);
101
}
102
103