]> git.proxmox.com Git - ceph.git/blame - ceph/src/civetweb/src/third_party/duktape-1.5.2/src-separate/duk_bi_number.c
buildsys: switch source download to quincy
[ceph.git] / ceph / src / civetweb / src / third_party / duktape-1.5.2 / src-separate / duk_bi_number.c
CommitLineData
7c673cae
FG
1/*
2 * Number built-ins
3 */
4
5#include "duk_internal.h"
6
7DUK_LOCAL duk_double_t duk__push_this_number_plain(duk_context *ctx) {
8 duk_hobject *h;
9
10 /* Number built-in accepts a plain number or a Number object (whose
11 * internal value is operated on). Other types cause TypeError.
12 */
13
14 duk_push_this(ctx);
15 if (duk_is_number(ctx, -1)) {
16 DUK_DDD(DUK_DDDPRINT("plain number value: %!T", (duk_tval *) duk_get_tval(ctx, -1)));
17 goto done;
18 }
19 h = duk_get_hobject(ctx, -1);
20 if (!h ||
21 (DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_NUMBER)) {
22 DUK_DDD(DUK_DDDPRINT("unacceptable this value: %!T", (duk_tval *) duk_get_tval(ctx, -1)));
11fdf7f2 23 DUK_ERROR_TYPE((duk_hthread *) ctx, "number expected");
7c673cae
FG
24 }
25 duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_VALUE);
26 DUK_ASSERT(duk_is_number(ctx, -1));
27 DUK_DDD(DUK_DDDPRINT("number object: %!T, internal value: %!T",
28 (duk_tval *) duk_get_tval(ctx, -2), (duk_tval *) duk_get_tval(ctx, -1)));
29 duk_remove(ctx, -2);
30
31 done:
32 return duk_get_number(ctx, -1);
33}
34
35DUK_INTERNAL duk_ret_t duk_bi_number_constructor(duk_context *ctx) {
36 duk_hthread *thr = (duk_hthread *) ctx;
37 duk_idx_t nargs;
38 duk_hobject *h_this;
39
40 DUK_UNREF(thr);
41
42 /*
43 * The Number constructor uses ToNumber(arg) for number coercion
44 * (coercing an undefined argument to NaN). However, if the
45 * argument is not given at all, +0 must be used instead. To do
46 * this, a vararg function is used.
47 */
48
49 nargs = duk_get_top(ctx);
50 if (nargs == 0) {
51 duk_push_int(ctx, 0);
52 }
53 duk_to_number(ctx, 0);
54 duk_set_top(ctx, 1);
55 DUK_ASSERT_TOP(ctx, 1);
56
57 if (!duk_is_constructor_call(ctx)) {
58 return 1;
59 }
60
61 /*
62 * E5 Section 15.7.2.1 requires that the constructed object
63 * must have the original Number.prototype as its internal
64 * prototype. However, since Number.prototype is non-writable
65 * and non-configurable, this doesn't have to be enforced here:
66 * The default object (bound to 'this') is OK, though we have
67 * to change its class.
68 *
69 * Internal value set to ToNumber(arg) or +0; if no arg given,
70 * ToNumber(undefined) = NaN, so special treatment is needed
71 * (above). String internal value is immutable.
72 */
73
74 /* XXX: helper */
75 duk_push_this(ctx);
76 h_this = duk_get_hobject(ctx, -1);
77 DUK_ASSERT(h_this != NULL);
78 DUK_HOBJECT_SET_CLASS_NUMBER(h_this, DUK_HOBJECT_CLASS_NUMBER);
79
80 DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_this) == thr->builtins[DUK_BIDX_NUMBER_PROTOTYPE]);
81 DUK_ASSERT(DUK_HOBJECT_GET_CLASS_NUMBER(h_this) == DUK_HOBJECT_CLASS_NUMBER);
82 DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(h_this));
83
84 duk_dup(ctx, 0); /* -> [ val obj val ] */
85 duk_xdef_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE);
86 return 0; /* no return value -> don't replace created value */
87}
88
89DUK_INTERNAL duk_ret_t duk_bi_number_prototype_value_of(duk_context *ctx) {
90 (void) duk__push_this_number_plain(ctx);
91 return 1;
92}
93
94DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_string(duk_context *ctx) {
95 duk_small_int_t radix;
96 duk_small_uint_t n2s_flags;
97
98 (void) duk__push_this_number_plain(ctx);
99 if (duk_is_undefined(ctx, 0)) {
100 radix = 10;
101 } else {
102 radix = (duk_small_int_t) duk_to_int_check_range(ctx, 0, 2, 36);
103 }
104 DUK_DDD(DUK_DDDPRINT("radix=%ld", (long) radix));
105
106 n2s_flags = 0;
107
108 duk_numconv_stringify(ctx,
109 radix /*radix*/,
110 0 /*digits*/,
111 n2s_flags /*flags*/);
112 return 1;
113}
114
115DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_locale_string(duk_context *ctx) {
116 /* XXX: just use toString() for now; permitted although not recommended.
117 * nargs==1, so radix is passed to toString().
118 */
119 return duk_bi_number_prototype_to_string(ctx);
120}
121
122/*
123 * toFixed(), toExponential(), toPrecision()
124 */
125
126/* XXX: shared helper for toFixed(), toExponential(), toPrecision()? */
127
128DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_fixed(duk_context *ctx) {
129 duk_small_int_t frac_digits;
130 duk_double_t d;
131 duk_small_int_t c;
132 duk_small_uint_t n2s_flags;
133
134 frac_digits = (duk_small_int_t) duk_to_int_check_range(ctx, 0, 0, 20);
135 d = duk__push_this_number_plain(ctx);
136
137 c = (duk_small_int_t) DUK_FPCLASSIFY(d);
138 if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) {
139 goto use_to_string;
140 }
141
142 if (d >= 1.0e21 || d <= -1.0e21) {
143 goto use_to_string;
144 }
145
146 n2s_flags = DUK_N2S_FLAG_FIXED_FORMAT |
147 DUK_N2S_FLAG_FRACTION_DIGITS;
148
149 duk_numconv_stringify(ctx,
150 10 /*radix*/,
151 frac_digits /*digits*/,
152 n2s_flags /*flags*/);
153 return 1;
154
155 use_to_string:
156 DUK_ASSERT_TOP(ctx, 2);
157 duk_to_string(ctx, -1);
158 return 1;
159}
160
161DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_exponential(duk_context *ctx) {
162 duk_bool_t frac_undefined;
163 duk_small_int_t frac_digits;
164 duk_double_t d;
165 duk_small_int_t c;
166 duk_small_uint_t n2s_flags;
167
168 d = duk__push_this_number_plain(ctx);
169
170 frac_undefined = duk_is_undefined(ctx, 0);
171 duk_to_int(ctx, 0); /* for side effects */
172
173 c = (duk_small_int_t) DUK_FPCLASSIFY(d);
174 if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) {
175 goto use_to_string;
176 }
177
178 frac_digits = (duk_small_int_t) duk_to_int_check_range(ctx, 0, 0, 20);
179
180 n2s_flags = DUK_N2S_FLAG_FORCE_EXP |
181 (frac_undefined ? 0 : DUK_N2S_FLAG_FIXED_FORMAT);
182
183 duk_numconv_stringify(ctx,
184 10 /*radix*/,
185 frac_digits + 1 /*leading digit + fractions*/,
186 n2s_flags /*flags*/);
187 return 1;
188
189 use_to_string:
190 DUK_ASSERT_TOP(ctx, 2);
191 duk_to_string(ctx, -1);
192 return 1;
193}
194
195DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_precision(duk_context *ctx) {
196 /* The specification has quite awkward order of coercion and
197 * checks for toPrecision(). The operations below are a bit
198 * reordered, within constraints of observable side effects.
199 */
200
201 duk_double_t d;
202 duk_small_int_t prec;
203 duk_small_int_t c;
204 duk_small_uint_t n2s_flags;
205
206 DUK_ASSERT_TOP(ctx, 1);
207
208 d = duk__push_this_number_plain(ctx);
209 if (duk_is_undefined(ctx, 0)) {
210 goto use_to_string;
211 }
212 DUK_ASSERT_TOP(ctx, 2);
213
214 duk_to_int(ctx, 0); /* for side effects */
215
216 c = (duk_small_int_t) DUK_FPCLASSIFY(d);
217 if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) {
218 goto use_to_string;
219 }
220
221 prec = (duk_small_int_t) duk_to_int_check_range(ctx, 0, 1, 21);
222
223 n2s_flags = DUK_N2S_FLAG_FIXED_FORMAT |
224 DUK_N2S_FLAG_NO_ZERO_PAD;
225
226 duk_numconv_stringify(ctx,
227 10 /*radix*/,
228 prec /*digits*/,
229 n2s_flags /*flags*/);
230 return 1;
231
232 use_to_string:
233 /* Used when precision is undefined; also used for NaN (-> "NaN"),
234 * and +/- infinity (-> "Infinity", "-Infinity").
235 */
236
237 DUK_ASSERT_TOP(ctx, 2);
238 duk_to_string(ctx, -1);
239 return 1;
240}