Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
duk_bi_number.c File Reference
#include "duk_internal.h"

Go to the source code of this file.

Functions

DUK_LOCAL duk_double_t duk__push_this_number_plain (duk_context *ctx)
 
DUK_INTERNAL duk_ret_t duk_bi_number_constructor (duk_context *ctx)
 
DUK_INTERNAL duk_ret_t duk_bi_number_prototype_value_of (duk_context *ctx)
 
DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_string (duk_context *ctx)
 
DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_locale_string (duk_context *ctx)
 
DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_fixed (duk_context *ctx)
 
DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_exponential (duk_context *ctx)
 
DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_precision (duk_context *ctx)
 

Function Documentation

◆ duk__push_this_number_plain()

DUK_LOCAL duk_double_t duk__push_this_number_plain ( duk_context * ctx)

Definition at line 7 of file duktape-1.5.2/src-separate/duk_bi_number.c.

7 {
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 ||
22 DUK_DDD(DUK_DDDPRINT("unacceptable this value: %!T", (duk_tval *) duk_get_tval(ctx, -1)));
23 DUK_ERROR_TYPE((duk_hthread *) ctx, "number expected");
24 }
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}
#define DUK_ERROR_TYPE(thr, msg)
DUK_EXTERNAL void duk_push_this(duk_context *ctx)
DUK_INTERNAL_DECL duk_bool_t duk_get_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx)
DUK_EXTERNAL duk_double_t duk_get_number(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_remove(duk_context *ctx, duk_idx_t index)
#define DUK_HOBJECT_CLASS_NUMBER
DUK_EXTERNAL duk_bool_t duk_is_number(duk_context *ctx, duk_idx_t index)
DUK_INTERNAL_DECL duk_hobject * duk_get_hobject(duk_context *ctx, duk_idx_t index)
#define DUK_HOBJECT_GET_CLASS_NUMBER(h)
DUK_INTERNAL_DECL duk_tval * duk_get_tval(duk_context *ctx, duk_idx_t index)

References DUK_ASSERT, DUK_DDD, DUK_DDDPRINT, DUK_ERROR_TYPE, duk_get_hobject(), duk_get_number(), duk_get_prop_stridx(), duk_get_tval(), DUK_HOBJECT_CLASS_NUMBER, DUK_HOBJECT_GET_CLASS_NUMBER, duk_is_number(), duk_push_this(), duk_remove(), and DUK_STRIDX_INT_VALUE.

Referenced by duk_bi_number_prototype_to_exponential(), duk_bi_number_prototype_to_fixed(), duk_bi_number_prototype_to_precision(), duk_bi_number_prototype_to_string(), and duk_bi_number_prototype_value_of().

◆ duk_bi_number_constructor()

DUK_INTERNAL duk_ret_t duk_bi_number_constructor ( duk_context * ctx)

Definition at line 35 of file duktape-1.5.2/src-separate/duk_bi_number.c.

35 {
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);
79
83
84 duk_dup(ctx, 0); /* -> [ val obj val ] */
86 return 0; /* no return value -> don't replace created value */
87}
#define DUK_HOBJECT_GET_PROTOTYPE(heap, h)
#define DUK_PROPDESC_FLAGS_NONE
DUK_EXTERNAL void duk_push_int(duk_context *ctx, duk_int_t val)
DUK_EXTERNAL duk_double_t duk_to_number(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL void duk_set_top(duk_context *ctx, duk_idx_t index)
#define DUK_BIDX_NUMBER_PROTOTYPE
DUK_EXTERNAL void duk_dup(duk_context *ctx, duk_idx_t from_index)
DUK_EXTERNAL duk_idx_t duk_get_top(duk_context *ctx)
DUK_EXTERNAL duk_bool_t duk_is_constructor_call(duk_context *ctx)
#define DUK_ASSERT_TOP(ctx, n)
DUK_INTERNAL_DECL void duk_xdef_prop_stridx(duk_context *ctx, duk_idx_t obj_index, duk_small_int_t stridx, duk_small_uint_t desc_flags)
#define DUK_HOBJECT_HAS_EXTENSIBLE(h)
#define DUK_HOBJECT_SET_CLASS_NUMBER(h, v)
#define NULL
Definition gmacros.h:924
duk_hobject * builtins[DUK_NUM_BUILTINS]

References duk_hthread::builtins, DUK_ASSERT, DUK_ASSERT_TOP, DUK_BIDX_NUMBER_PROTOTYPE, duk_dup(), duk_get_hobject(), duk_get_top(), DUK_HOBJECT_CLASS_NUMBER, DUK_HOBJECT_GET_CLASS_NUMBER, DUK_HOBJECT_GET_PROTOTYPE, DUK_HOBJECT_HAS_EXTENSIBLE, DUK_HOBJECT_SET_CLASS_NUMBER, duk_is_constructor_call(), DUK_PROPDESC_FLAGS_NONE, duk_push_int(), duk_push_this(), duk_set_top(), DUK_STRIDX_INT_VALUE, duk_to_number(), DUK_UNREF, duk_xdef_prop_stridx(), duk_hthread::heap, and NULL.

◆ duk_bi_number_prototype_to_exponential()

DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_exponential ( duk_context * ctx)

Definition at line 161 of file duktape-1.5.2/src-separate/duk_bi_number.c.

161 {
162 duk_bool_t frac_undefined;
163 duk_small_int_t frac_digits;
164 duk_double_t d;
166 duk_small_uint_t n2s_flags;
167
169
170 frac_undefined = duk_is_undefined(ctx, 0);
171 duk_to_int(ctx, 0); /* for side effects */
172
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
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}
unsigned int duk_small_uint_t
duk_small_int_t duk_bool_t
DUK_EXTERNAL duk_bool_t duk_is_undefined(duk_context *ctx, duk_idx_t index)
#define DUK_N2S_FLAG_FIXED_FORMAT
DUK_INTERNAL_DECL duk_int_t duk_to_int_check_range(duk_context *ctx, duk_idx_t index, duk_int_t minval, duk_int_t maxval)
DUK_INTERNAL_DECL void duk_numconv_stringify(duk_context *ctx, duk_small_int_t radix, duk_small_int_t digits, duk_small_uint_t flags)
#define DUK_N2S_FLAG_FORCE_EXP
DUK_EXTERNAL const char * duk_to_string(duk_context *ctx, duk_idx_t index)
DUK_EXTERNAL duk_int_t duk_to_int(duk_context *ctx, duk_idx_t index)
DUK_LOCAL duk_double_t duk__push_this_number_plain(duk_context *ctx)

References duk__push_this_number_plain(), DUK_ASSERT_TOP, DUK_FP_INFINITE, DUK_FP_NAN, DUK_FPCLASSIFY, duk_is_undefined(), DUK_N2S_FLAG_FIXED_FORMAT, DUK_N2S_FLAG_FORCE_EXP, duk_numconv_stringify(), duk_to_int(), duk_to_int_check_range(), and duk_to_string().

◆ duk_bi_number_prototype_to_fixed()

DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_fixed ( duk_context * ctx)

Definition at line 128 of file duktape-1.5.2/src-separate/duk_bi_number.c.

128 {
129 duk_small_int_t frac_digits;
130 duk_double_t d;
132 duk_small_uint_t n2s_flags;
133
134 frac_digits = (duk_small_int_t) duk_to_int_check_range(ctx, 0, 0, 20);
136
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 |
148
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}
#define DUK_N2S_FLAG_FRACTION_DIGITS

References duk__push_this_number_plain(), DUK_ASSERT_TOP, DUK_FP_INFINITE, DUK_FP_NAN, DUK_FPCLASSIFY, DUK_N2S_FLAG_FIXED_FORMAT, DUK_N2S_FLAG_FRACTION_DIGITS, duk_numconv_stringify(), duk_to_int_check_range(), and duk_to_string().

◆ duk_bi_number_prototype_to_locale_string()

DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_locale_string ( duk_context * ctx)

Definition at line 115 of file duktape-1.5.2/src-separate/duk_bi_number.c.

115 {
116 /* XXX: just use toString() for now; permitted although not recommended.
117 * nargs==1, so radix is passed to toString().
118 */
120}
DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_string(duk_context *ctx)

References duk_bi_number_prototype_to_string().

◆ duk_bi_number_prototype_to_precision()

DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_precision ( duk_context * ctx)

Definition at line 195 of file duktape-1.5.2/src-separate/duk_bi_number.c.

195 {
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;
204 duk_small_uint_t n2s_flags;
205
206 DUK_ASSERT_TOP(ctx, 1);
207
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
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 |
225
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}
#define DUK_N2S_FLAG_NO_ZERO_PAD

References duk__push_this_number_plain(), DUK_ASSERT_TOP, DUK_FP_INFINITE, DUK_FP_NAN, DUK_FPCLASSIFY, duk_is_undefined(), DUK_N2S_FLAG_FIXED_FORMAT, DUK_N2S_FLAG_NO_ZERO_PAD, duk_numconv_stringify(), duk_to_int(), duk_to_int_check_range(), and duk_to_string().

◆ duk_bi_number_prototype_to_string()

DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_string ( duk_context * ctx)

Definition at line 94 of file duktape-1.5.2/src-separate/duk_bi_number.c.

94 {
95 duk_small_int_t radix;
96 duk_small_uint_t n2s_flags;
97
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
109 radix /*radix*/,
110 0 /*digits*/,
111 n2s_flags /*flags*/);
112 return 1;
113}

References duk__push_this_number_plain(), DUK_DDD, DUK_DDDPRINT, duk_is_undefined(), duk_numconv_stringify(), and duk_to_int_check_range().

Referenced by duk_bi_number_prototype_to_locale_string().

◆ duk_bi_number_prototype_value_of()

DUK_INTERNAL duk_ret_t duk_bi_number_prototype_value_of ( duk_context * ctx)

Definition at line 89 of file duktape-1.5.2/src-separate/duk_bi_number.c.

89 {
91 return 1;
92}

References duk__push_this_number_plain().