push 6377a023768d7480eb1b17e57456824888936b43
[wine/hacks.git] / dlls / jscript / number.c
blob0aeeab6386aab54dbfce096e1d35b127209a5900
1 /*
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
19 #include <math.h>
21 #include "jscript.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
27 typedef struct {
28 DispatchEx dispex;
30 VARIANT num;
31 } NumberInstance;
33 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
34 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
35 static const WCHAR toFixedW[] = {'t','o','F','i','x','e','d',0};
36 static const WCHAR toExponentialW[] = {'t','o','E','x','p','o','n','e','n','t','i','a','l',0};
37 static const WCHAR toPrecisionW[] = {'t','o','P','r','e','c','i','s','i','o','n',0};
38 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
39 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
40 static const WCHAR propertyIsEnumerableW[] =
41 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
42 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
44 #define NUMBER_TOSTRING_BUF_SIZE 64
45 /* ECMA-262 3rd Edition 15.7.4.2 */
46 static HRESULT Number_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
47 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
49 NumberInstance *number;
50 INT radix = 10;
51 DOUBLE val;
52 BSTR str;
53 HRESULT hres;
55 TRACE("\n");
57 if(!is_class(dispex, JSCLASS_NUMBER))
58 return throw_type_error(dispex->ctx, ei, IDS_NOT_NUM, NULL);
60 number = (NumberInstance*)dispex;
62 if(arg_cnt(dp)) {
63 hres = to_int32(dispex->ctx, get_arg(dp, 0), ei, &radix);
64 if(FAILED(hres))
65 return hres;
67 if(radix<2 || radix>36)
68 return throw_type_error(dispex->ctx, ei, IDS_INVALID_CALL_ARG, NULL);
71 if(V_VT(&number->num) == VT_I4)
72 val = V_I4(&number->num);
73 else
74 val = V_R8(&number->num);
76 if(radix==10 || isnan(val) || isinf(val)) {
77 hres = to_string(dispex->ctx, &number->num, ei, &str);
78 if(FAILED(hres))
79 return hres;
81 else {
82 INT idx = 0;
83 DOUBLE integ, frac, log_radix = 0;
84 WCHAR buf[NUMBER_TOSTRING_BUF_SIZE+16];
85 BOOL exp = FALSE;
87 if(val<0) {
88 val = -val;
89 buf[idx++] = '-';
92 while(1) {
93 integ = floor(val);
94 frac = val-integ;
96 if(integ == 0)
97 buf[idx++] = '0';
98 while(integ>=1 && idx<NUMBER_TOSTRING_BUF_SIZE) {
99 buf[idx] = fmod(integ, radix);
100 if(buf[idx]<10) buf[idx] += '0';
101 else buf[idx] += 'a'-10;
102 integ /= radix;
103 idx++;
106 if(idx<NUMBER_TOSTRING_BUF_SIZE) {
107 INT beg = buf[0]=='-'?1:0;
108 INT end = idx-1;
109 WCHAR wch;
111 while(end > beg) {
112 wch = buf[beg];
113 buf[beg++] = buf[end];
114 buf[end--] = wch;
118 if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.';
120 while(frac>0 && idx<NUMBER_TOSTRING_BUF_SIZE) {
121 frac *= radix;
122 buf[idx] = fmod(frac, radix);
123 frac -= buf[idx];
124 if(buf[idx]<10) buf[idx] += '0';
125 else buf[idx] += 'a'-10;
126 idx++;
129 if(idx==NUMBER_TOSTRING_BUF_SIZE && !exp) {
130 exp = TRUE;
131 idx = (buf[0]=='-') ? 1 : 0;
132 log_radix = floor(log(val)/log(radix));
133 val *= pow(radix, -log_radix);
134 continue;
137 break;
140 while(buf[idx-1] == '0') idx--;
141 if(buf[idx-1] == '.') idx--;
143 if(exp) {
144 if(log_radix==0)
145 buf[idx++] = '\0';
146 else {
147 static const WCHAR formatW[] = {'(','e','%','c','%','d',')',0};
148 WCHAR ch;
150 if(log_radix<0) {
151 log_radix = -log_radix;
152 ch = '-';
154 else ch = '+';
155 sprintfW(&buf[idx], formatW, ch, (int)log_radix);
158 else buf[idx] = '\0';
160 str = SysAllocString(buf);
161 if(!str)
162 return E_OUTOFMEMORY;
165 if(retv) {
166 V_VT(retv) = VT_BSTR;
167 V_BSTR(retv) = str;
168 }else {
169 SysFreeString(str);
171 return S_OK;
174 static HRESULT Number_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
175 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
177 FIXME("\n");
178 return E_NOTIMPL;
181 static HRESULT Number_toFixed(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
182 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
184 FIXME("\n");
185 return E_NOTIMPL;
188 static HRESULT Number_toExponential(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
189 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
191 FIXME("\n");
192 return E_NOTIMPL;
195 static HRESULT Number_toPrecision(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
196 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
198 FIXME("\n");
199 return E_NOTIMPL;
202 static HRESULT Number_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
203 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
205 TRACE("\n");
207 if(!is_class(dispex, JSCLASS_NUMBER))
208 return throw_type_error(dispex->ctx, ei, IDS_NOT_NUM, NULL);
210 if(retv) {
211 NumberInstance *number = (NumberInstance*)dispex;
212 *retv = number->num;
214 return S_OK;
217 static HRESULT Number_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
218 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
220 FIXME("\n");
221 return E_NOTIMPL;
224 static HRESULT Number_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
225 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
227 FIXME("\n");
228 return E_NOTIMPL;
231 static HRESULT Number_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
232 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
234 FIXME("\n");
235 return E_NOTIMPL;
238 static HRESULT Number_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
239 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
241 NumberInstance *number = (NumberInstance*)dispex;
243 switch(flags) {
244 case INVOKE_FUNC:
245 return throw_type_error(dispex->ctx, ei, IDS_NOT_FUNC, NULL);
246 case DISPATCH_PROPERTYGET:
247 *retv = number->num;
248 break;
250 default:
251 FIXME("flags %x\n", flags);
252 return E_NOTIMPL;
255 return S_OK;
258 static const builtin_prop_t Number_props[] = {
259 {hasOwnPropertyW, Number_hasOwnProperty, PROPF_METHOD},
260 {isPrototypeOfW, Number_isPrototypeOf, PROPF_METHOD},
261 {propertyIsEnumerableW, Number_propertyIsEnumerable, PROPF_METHOD},
262 {toExponentialW, Number_toExponential, PROPF_METHOD},
263 {toFixedW, Number_toFixed, PROPF_METHOD},
264 {toLocaleStringW, Number_toLocaleString, PROPF_METHOD},
265 {toPrecisionW, Number_toPrecision, PROPF_METHOD},
266 {toStringW, Number_toString, PROPF_METHOD},
267 {valueOfW, Number_valueOf, PROPF_METHOD}
270 static const builtin_info_t Number_info = {
271 JSCLASS_NUMBER,
272 {NULL, Number_value, 0},
273 sizeof(Number_props)/sizeof(*Number_props),
274 Number_props,
275 NULL,
276 NULL
279 static HRESULT NumberConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
280 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
282 VARIANT num;
283 HRESULT hres;
285 TRACE("\n");
287 switch(flags) {
288 case INVOKE_FUNC:
289 if(!arg_cnt(dp)) {
290 if(retv) {
291 V_VT(retv) = VT_I4;
292 V_I4(retv) = 0;
294 return S_OK;
297 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &num);
298 if(FAILED(hres))
299 return hres;
301 if(retv)
302 *retv = num;
303 break;
305 case DISPATCH_CONSTRUCT: {
306 DispatchEx *obj;
308 if(arg_cnt(dp)) {
309 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &num);
310 if(FAILED(hres))
311 return hres;
312 }else {
313 V_VT(&num) = VT_I4;
314 V_I4(&num) = 0;
317 hres = create_number(dispex->ctx, &num, &obj);
318 if(FAILED(hres))
319 return hres;
321 V_VT(retv) = VT_DISPATCH;
322 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
323 break;
325 default:
326 FIXME("unimplemented flags %x\n", flags);
327 return E_NOTIMPL;
330 return S_OK;
333 static HRESULT alloc_number(script_ctx_t *ctx, BOOL use_constr, NumberInstance **ret)
335 NumberInstance *number;
336 HRESULT hres;
338 number = heap_alloc_zero(sizeof(NumberInstance));
339 if(!number)
340 return E_OUTOFMEMORY;
342 if(use_constr)
343 hres = init_dispex_from_constr(&number->dispex, ctx, &Number_info, ctx->number_constr);
344 else
345 hres = init_dispex(&number->dispex, ctx, &Number_info, NULL);
346 if(FAILED(hres))
347 return hres;
349 *ret = number;
350 return S_OK;
353 HRESULT create_number_constr(script_ctx_t *ctx, DispatchEx **ret)
355 NumberInstance *number;
356 HRESULT hres;
358 hres = alloc_number(ctx, FALSE, &number);
359 if(FAILED(hres))
360 return hres;
362 V_VT(&number->num) = VT_I4;
363 hres = create_builtin_function(ctx, NumberConstr_value, NULL, PROPF_CONSTR, &number->dispex, ret);
365 jsdisp_release(&number->dispex);
366 return hres;
369 HRESULT create_number(script_ctx_t *ctx, VARIANT *num, DispatchEx **ret)
371 NumberInstance *number;
372 HRESULT hres;
374 hres = alloc_number(ctx, TRUE, &number);
375 if(FAILED(hres))
376 return hres;
378 number->num = *num;
380 *ret = &number->dispex;
381 return S_OK;