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