757316cee8635ce3a9605bf51d75b5ddfc416f9d
[reactos.git] / reactos / dll / win32 / jscript / number.c
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "config.h"
20 #include "wine/port.h"
21
22 #include <math.h>
23
24 #include "jscript.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29
30 typedef struct {
31 DispatchEx dispex;
32
33 VARIANT num;
34 } NumberInstance;
35
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};
42
43 #define NUMBER_TOSTRING_BUF_SIZE 64
44
45 static inline NumberInstance *number_from_vdisp(vdisp_t *vdisp)
46 {
47 return (NumberInstance*)vdisp->u.jsdisp;
48 }
49
50 static inline NumberInstance *number_this(vdisp_t *jsthis)
51 {
52 return is_vclass(jsthis, JSCLASS_NUMBER) ? number_from_vdisp(jsthis) : NULL;
53 }
54
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, IServiceProvider *sp)
58 {
59 NumberInstance *number;
60 INT radix = 10;
61 DOUBLE val;
62 BSTR str;
63 HRESULT hres;
64
65 TRACE("\n");
66
67 if(!(number = number_this(jsthis)))
68 return throw_type_error(ctx, ei, IDS_NOT_NUM, NULL);
69
70 if(arg_cnt(dp)) {
71 hres = to_int32(ctx, get_arg(dp, 0), ei, &radix);
72 if(FAILED(hres))
73 return hres;
74
75 if(radix<2 || radix>36)
76 return throw_type_error(ctx, ei, IDS_INVALID_CALL_ARG, NULL);
77 }
78
79 if(V_VT(&number->num) == VT_I4)
80 val = V_I4(&number->num);
81 else
82 val = V_R8(&number->num);
83
84 if(radix==10 || isnan(val) || isinf(val)) {
85 hres = to_string(ctx, &number->num, ei, &str);
86 if(FAILED(hres))
87 return hres;
88 }
89 else {
90 INT idx = 0;
91 DOUBLE integ, frac, log_radix = 0;
92 WCHAR buf[NUMBER_TOSTRING_BUF_SIZE+16];
93 BOOL exp = FALSE;
94
95 if(val<0) {
96 val = -val;
97 buf[idx++] = '-';
98 }
99
100 while(1) {
101 integ = floor(val);
102 frac = val-integ;
103
104 if(integ == 0)
105 buf[idx++] = '0';
106 while(integ>=1 && idx<NUMBER_TOSTRING_BUF_SIZE) {
107 buf[idx] = fmod(integ, radix);
108 if(buf[idx]<10) buf[idx] += '0';
109 else buf[idx] += 'a'-10;
110 integ /= radix;
111 idx++;
112 }
113
114 if(idx<NUMBER_TOSTRING_BUF_SIZE) {
115 INT beg = buf[0]=='-'?1:0;
116 INT end = idx-1;
117 WCHAR wch;
118
119 while(end > beg) {
120 wch = buf[beg];
121 buf[beg++] = buf[end];
122 buf[end--] = wch;
123 }
124 }
125
126 if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.';
127
128 while(frac>0 && idx<NUMBER_TOSTRING_BUF_SIZE) {
129 frac *= radix;
130 buf[idx] = fmod(frac, radix);
131 frac -= buf[idx];
132 if(buf[idx]<10) buf[idx] += '0';
133 else buf[idx] += 'a'-10;
134 idx++;
135 }
136
137 if(idx==NUMBER_TOSTRING_BUF_SIZE && !exp) {
138 exp = TRUE;
139 idx = (buf[0]=='-') ? 1 : 0;
140 log_radix = floor(log(val)/log(radix));
141 val *= pow(radix, -log_radix);
142 continue;
143 }
144
145 break;
146 }
147
148 while(buf[idx-1] == '0') idx--;
149 if(buf[idx-1] == '.') idx--;
150
151 if(exp) {
152 if(log_radix==0)
153 buf[idx++] = '\0';
154 else {
155 static const WCHAR formatW[] = {'(','e','%','c','%','d',')',0};
156 WCHAR ch;
157
158 if(log_radix<0) {
159 log_radix = -log_radix;
160 ch = '-';
161 }
162 else ch = '+';
163 sprintfW(&buf[idx], formatW, ch, (int)log_radix);
164 }
165 }
166 else buf[idx] = '\0';
167
168 str = SysAllocString(buf);
169 if(!str)
170 return E_OUTOFMEMORY;
171 }
172
173 if(retv) {
174 V_VT(retv) = VT_BSTR;
175 V_BSTR(retv) = str;
176 }else {
177 SysFreeString(str);
178 }
179 return S_OK;
180 }
181
182 static HRESULT Number_toLocaleString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
183 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
184 {
185 FIXME("\n");
186 return E_NOTIMPL;
187 }
188
189 static HRESULT Number_toFixed(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
190 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
191 {
192 FIXME("\n");
193 return E_NOTIMPL;
194 }
195
196 static HRESULT Number_toExponential(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
197 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
198 {
199 FIXME("\n");
200 return E_NOTIMPL;
201 }
202
203 static HRESULT Number_toPrecision(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
204 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
205 {
206 FIXME("\n");
207 return E_NOTIMPL;
208 }
209
210 static HRESULT Number_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
211 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
212 {
213 NumberInstance *number;
214
215 TRACE("\n");
216
217 if(!(number = number_this(jsthis)))
218 return throw_type_error(ctx, ei, IDS_NOT_NUM, NULL);
219
220 if(retv)
221 *retv = number->num;
222 return S_OK;
223 }
224
225 static HRESULT Number_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
226 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
227 {
228 NumberInstance *number = number_from_vdisp(jsthis);
229
230 switch(flags) {
231 case INVOKE_FUNC:
232 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
233 case DISPATCH_PROPERTYGET:
234 *retv = number->num;
235 break;
236
237 default:
238 FIXME("flags %x\n", flags);
239 return E_NOTIMPL;
240 }
241
242 return S_OK;
243 }
244
245 static const builtin_prop_t Number_props[] = {
246 {toExponentialW, Number_toExponential, PROPF_METHOD|1},
247 {toFixedW, Number_toFixed, PROPF_METHOD},
248 {toLocaleStringW, Number_toLocaleString, PROPF_METHOD},
249 {toPrecisionW, Number_toPrecision, PROPF_METHOD|1},
250 {toStringW, Number_toString, PROPF_METHOD|1},
251 {valueOfW, Number_valueOf, PROPF_METHOD}
252 };
253
254 static const builtin_info_t Number_info = {
255 JSCLASS_NUMBER,
256 {NULL, Number_value, 0},
257 sizeof(Number_props)/sizeof(*Number_props),
258 Number_props,
259 NULL,
260 NULL
261 };
262
263 static HRESULT NumberConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
264 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
265 {
266 VARIANT num;
267 HRESULT hres;
268
269 TRACE("\n");
270
271 switch(flags) {
272 case INVOKE_FUNC:
273 if(!arg_cnt(dp)) {
274 if(retv) {
275 V_VT(retv) = VT_I4;
276 V_I4(retv) = 0;
277 }
278 return S_OK;
279 }
280
281 hres = to_number(ctx, get_arg(dp, 0), ei, &num);
282 if(FAILED(hres))
283 return hres;
284
285 if(retv)
286 *retv = num;
287 break;
288
289 case DISPATCH_CONSTRUCT: {
290 DispatchEx *obj;
291
292 if(arg_cnt(dp)) {
293 hres = to_number(ctx, get_arg(dp, 0), ei, &num);
294 if(FAILED(hres))
295 return hres;
296 }else {
297 V_VT(&num) = VT_I4;
298 V_I4(&num) = 0;
299 }
300
301 hres = create_number(ctx, &num, &obj);
302 if(FAILED(hres))
303 return hres;
304
305 V_VT(retv) = VT_DISPATCH;
306 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
307 break;
308 }
309 default:
310 FIXME("unimplemented flags %x\n", flags);
311 return E_NOTIMPL;
312 }
313
314 return S_OK;
315 }
316
317 static HRESULT alloc_number(script_ctx_t *ctx, DispatchEx *object_prototype, NumberInstance **ret)
318 {
319 NumberInstance *number;
320 HRESULT hres;
321
322 number = heap_alloc_zero(sizeof(NumberInstance));
323 if(!number)
324 return E_OUTOFMEMORY;
325
326 if(object_prototype)
327 hres = init_dispex(&number->dispex, ctx, &Number_info, object_prototype);
328 else
329 hres = init_dispex_from_constr(&number->dispex, ctx, &Number_info, ctx->number_constr);
330 if(FAILED(hres))
331 return hres;
332
333 *ret = number;
334 return S_OK;
335 }
336
337 HRESULT create_number_constr(script_ctx_t *ctx, DispatchEx *object_prototype, DispatchEx **ret)
338 {
339 NumberInstance *number;
340 HRESULT hres;
341
342 static const WCHAR NumberW[] = {'N','u','m','b','e','r',0};
343
344 hres = alloc_number(ctx, object_prototype, &number);
345 if(FAILED(hres))
346 return hres;
347
348 V_VT(&number->num) = VT_I4;
349 hres = create_builtin_function(ctx, NumberConstr_value, NumberW, NULL, PROPF_CONSTR, &number->dispex, ret);
350
351 jsdisp_release(&number->dispex);
352 return hres;
353 }
354
355 HRESULT create_number(script_ctx_t *ctx, VARIANT *num, DispatchEx **ret)
356 {
357 NumberInstance *number;
358 HRESULT hres;
359
360 hres = alloc_number(ctx, NULL, &number);
361 if(FAILED(hres))
362 return hres;
363
364 number->num = *num;
365
366 *ret = &number->dispex;
367 return S_OK;
368 }