2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
36 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
37 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
38 static const WCHAR toFixedW[] = {'t','o','F','i','x','e','d',0};
39 static const WCHAR toExponentialW[] = {'t','o','E','x','p','o','n','e','n','t','i','a','l',0};
40 static const WCHAR toPrecisionW[] = {'t','o','P','r','e','c','i','s','i','o','n',0};
41 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
43 #define NUMBER_TOSTRING_BUF_SIZE 64
45 static inline NumberInstance *number_from_vdisp(vdisp_t *vdisp)
47 return (NumberInstance*)vdisp->u.jsdisp;
50 static inline NumberInstance *number_this(vdisp_t *jsthis)
52 return is_vclass(jsthis, JSCLASS_NUMBER) ? number_from_vdisp(jsthis) : NULL;
55 /* ECMA-262 3rd Edition 15.7.4.2 */
56 static HRESULT Number_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
57 VARIANT *retv, jsexcept_t *ei)
59 NumberInstance *number;
67 if(!(number = number_this(jsthis)))
68 return throw_type_error(ctx, ei, JS_E_NUMBER_EXPECTED, NULL);
71 hres = to_int32(ctx, get_arg(dp, 0), ei, &radix);
75 if(radix<2 || radix>36)
76 return throw_type_error(ctx, ei, JS_E_INVALIDARG, NULL);
81 if(radix==10 || isnan(val) || isinf(val)) {
85 hres = to_string(ctx, &v, ei, &str);
90 DOUBLE integ, frac, log_radix = 0;
91 WCHAR buf[NUMBER_TOSTRING_BUF_SIZE+16];
105 while(integ>=1 && idx<NUMBER_TOSTRING_BUF_SIZE) {
106 buf[idx] = fmod(integ, radix);
107 if(buf[idx]<10) buf[idx] += '0';
108 else buf[idx] += 'a'-10;
113 if(idx<NUMBER_TOSTRING_BUF_SIZE) {
114 INT beg = buf[0]=='-'?1:0;
120 buf[beg++] = buf[end];
125 if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.';
127 while(frac>0 && idx<NUMBER_TOSTRING_BUF_SIZE) {
129 buf[idx] = fmod(frac, radix);
131 if(buf[idx]<10) buf[idx] += '0';
132 else buf[idx] += 'a'-10;
136 if(idx==NUMBER_TOSTRING_BUF_SIZE && !exp) {
138 idx = (buf[0]=='-') ? 1 : 0;
139 log_radix = floor(log(val)/log(radix));
140 val *= pow(radix, -log_radix);
147 while(buf[idx-1] == '0') idx--;
148 if(buf[idx-1] == '.') idx--;
154 static const WCHAR formatW[] = {'(','e','%','c','%','d',')',0};
158 log_radix = -log_radix;
162 sprintfW(&buf[idx], formatW, ch, (int)log_radix);
165 else buf[idx] = '\0';
167 str = SysAllocString(buf);
169 return E_OUTOFMEMORY;
173 V_VT(retv) = VT_BSTR;
181 static HRESULT Number_toLocaleString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
182 VARIANT *retv, jsexcept_t *ei)
188 static HRESULT Number_toFixed(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
189 VARIANT *retv, jsexcept_t *ei)
195 static HRESULT Number_toExponential(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
196 VARIANT *retv, jsexcept_t *ei)
202 static HRESULT Number_toPrecision(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
203 VARIANT *retv, jsexcept_t *ei)
209 static HRESULT Number_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
210 VARIANT *retv, jsexcept_t *ei)
212 NumberInstance *number;
216 if(!(number = number_this(jsthis)))
217 return throw_type_error(ctx, ei, JS_E_NUMBER_EXPECTED, NULL);
220 num_set_val(retv, number->value);
224 static HRESULT Number_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
225 VARIANT *retv, jsexcept_t *ei)
227 NumberInstance *number = number_from_vdisp(jsthis);
231 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
232 case DISPATCH_PROPERTYGET:
233 num_set_val(retv, number->value);
237 FIXME("flags %x\n", flags);
244 static const builtin_prop_t Number_props[] = {
245 {toExponentialW, Number_toExponential, PROPF_METHOD|1},
246 {toFixedW, Number_toFixed, PROPF_METHOD},
247 {toLocaleStringW, Number_toLocaleString, PROPF_METHOD},
248 {toPrecisionW, Number_toPrecision, PROPF_METHOD|1},
249 {toStringW, Number_toString, PROPF_METHOD|1},
250 {valueOfW, Number_valueOf, PROPF_METHOD}
253 static const builtin_info_t Number_info = {
255 {NULL, Number_value, 0},
256 sizeof(Number_props)/sizeof(*Number_props),
262 static HRESULT NumberConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
263 VARIANT *retv, jsexcept_t *ei)
280 hres = to_number(ctx, get_arg(dp, 0), ei, &n);
285 num_set_val(retv, n);
288 case DISPATCH_CONSTRUCT: {
292 hres = to_number(ctx, get_arg(dp, 0), ei, &n);
299 hres = create_number(ctx, n, &obj);
303 var_set_jsdisp(retv, obj);
307 FIXME("unimplemented flags %x\n", flags);
314 static HRESULT alloc_number(script_ctx_t *ctx, jsdisp_t *object_prototype, NumberInstance **ret)
316 NumberInstance *number;
319 number = heap_alloc_zero(sizeof(NumberInstance));
321 return E_OUTOFMEMORY;
324 hres = init_dispex(&number->dispex, ctx, &Number_info, object_prototype);
326 hres = init_dispex_from_constr(&number->dispex, ctx, &Number_info, ctx->number_constr);
334 HRESULT create_number_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
336 NumberInstance *number;
339 static const WCHAR NumberW[] = {'N','u','m','b','e','r',0};
341 hres = alloc_number(ctx, object_prototype, &number);
346 hres = create_builtin_function(ctx, NumberConstr_value, NumberW, NULL,
347 PROPF_CONSTR|1, &number->dispex, ret);
349 jsdisp_release(&number->dispex);
353 HRESULT create_number(script_ctx_t *ctx, double value, jsdisp_t **ret)
355 NumberInstance *number;
358 hres = alloc_number(ctx, NULL, &number);
362 number->value = value;
364 *ret = &number->dispex;