jscript: Assorted spelling 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 "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     jsdisp_t dispex;
32
33     double value;
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)
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, JS_E_NUMBER_EXPECTED, 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, JS_E_INVALIDARG, NULL);
77     }
78
79     val = number->value;
80
81     if(radix==10 || isnan(val) || isinf(val)) {
82         VARIANT v;
83
84         num_set_val(&v, val);
85         hres = to_string(ctx, &v, ei, &str);
86         if(FAILED(hres))
87             return hres;
88     }else {
89         INT idx = 0;
90         DOUBLE integ, frac, log_radix = 0;
91         WCHAR buf[NUMBER_TOSTRING_BUF_SIZE+16];
92         BOOL exp = FALSE;
93
94         if(val<0) {
95             val = -val;
96             buf[idx++] = '-';
97         }
98
99         while(1) {
100             integ = floor(val);
101             frac = val-integ;
102
103             if(integ == 0)
104                 buf[idx++] = '0';
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;
109                 integ /= radix;
110                 idx++;
111             }
112
113             if(idx<NUMBER_TOSTRING_BUF_SIZE) {
114                 INT beg = buf[0]=='-'?1:0;
115                 INT end = idx-1;
116                 WCHAR wch;
117
118                 while(end > beg) {
119                     wch = buf[beg];
120                     buf[beg++] = buf[end];
121                     buf[end--] = wch;
122                 }
123             }
124
125             if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.';
126
127             while(frac>0 && idx<NUMBER_TOSTRING_BUF_SIZE) {
128                 frac *= radix;
129                 buf[idx] = fmod(frac, radix);
130                 frac -= buf[idx];
131                 if(buf[idx]<10) buf[idx] += '0';
132                 else buf[idx] += 'a'-10;
133                 idx++;
134             }
135
136             if(idx==NUMBER_TOSTRING_BUF_SIZE && !exp) {
137                 exp = TRUE;
138                 idx = (buf[0]=='-') ? 1 : 0;
139                 log_radix = floor(log(val)/log(radix));
140                 val *= pow(radix, -log_radix);
141                 continue;
142             }
143
144             break;
145         }
146
147         while(buf[idx-1] == '0') idx--;
148         if(buf[idx-1] == '.') idx--;
149
150         if(exp) {
151             if(log_radix==0)
152                 buf[idx] = 0;
153             else {
154                 static const WCHAR formatW[] = {'(','e','%','c','%','d',')',0};
155                 WCHAR ch;
156
157                 if(log_radix<0) {
158                     log_radix = -log_radix;
159                     ch = '-';
160                 }
161                 else ch = '+';
162                 sprintfW(&buf[idx], formatW, ch, (int)log_radix);
163             }
164         }
165         else buf[idx] = '\0';
166
167         str = SysAllocString(buf);
168         if(!str)
169             return E_OUTOFMEMORY;
170     }
171
172     if(retv) {
173         V_VT(retv) = VT_BSTR;
174         V_BSTR(retv) = str;
175     }else {
176         SysFreeString(str);
177     }
178     return S_OK;
179 }
180
181 static HRESULT Number_toLocaleString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
182         VARIANT *retv, jsexcept_t *ei)
183 {
184     FIXME("\n");
185     return E_NOTIMPL;
186 }
187
188 static HRESULT Number_toFixed(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
189         VARIANT *retv, jsexcept_t *ei)
190 {
191     FIXME("\n");
192     return E_NOTIMPL;
193 }
194
195 static HRESULT Number_toExponential(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
196         VARIANT *retv, jsexcept_t *ei)
197 {
198     FIXME("\n");
199     return E_NOTIMPL;
200 }
201
202 static HRESULT Number_toPrecision(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
203         VARIANT *retv, jsexcept_t *ei)
204 {
205     FIXME("\n");
206     return E_NOTIMPL;
207 }
208
209 static HRESULT Number_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
210         VARIANT *retv, jsexcept_t *ei)
211 {
212     NumberInstance *number;
213
214     TRACE("\n");
215
216     if(!(number = number_this(jsthis)))
217         return throw_type_error(ctx, ei, JS_E_NUMBER_EXPECTED, NULL);
218
219     if(retv)
220         num_set_val(retv, number->value);
221     return S_OK;
222 }
223
224 static HRESULT Number_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
225         VARIANT *retv, jsexcept_t *ei)
226 {
227     NumberInstance *number = number_from_vdisp(jsthis);
228
229     switch(flags) {
230     case INVOKE_FUNC:
231         return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
232     case DISPATCH_PROPERTYGET:
233         num_set_val(retv, number->value);
234         break;
235
236     default:
237         FIXME("flags %x\n", flags);
238         return E_NOTIMPL;
239     }
240
241     return S_OK;
242 }
243
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}
251 };
252
253 static const builtin_info_t Number_info = {
254     JSCLASS_NUMBER,
255     {NULL, Number_value, 0},
256     sizeof(Number_props)/sizeof(*Number_props),
257     Number_props,
258     NULL,
259     NULL
260 };
261
262 static HRESULT NumberConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
263         VARIANT *retv, jsexcept_t *ei)
264 {
265     double n;
266     HRESULT hres;
267
268     TRACE("\n");
269
270     switch(flags) {
271     case INVOKE_FUNC:
272         if(!arg_cnt(dp)) {
273             if(retv) {
274                 V_VT(retv) = VT_I4;
275                 V_I4(retv) = 0;
276             }
277             return S_OK;
278         }
279
280         hres = to_number(ctx, get_arg(dp, 0), ei, &n);
281         if(FAILED(hres))
282             return hres;
283
284         if(retv)
285             num_set_val(retv, n);
286         break;
287
288     case DISPATCH_CONSTRUCT: {
289         jsdisp_t *obj;
290
291         if(arg_cnt(dp)) {
292             hres = to_number(ctx, get_arg(dp, 0), ei, &n);
293             if(FAILED(hres))
294                 return hres;
295         }else {
296             n = 0;
297         }
298
299         hres = create_number(ctx, n, &obj);
300         if(FAILED(hres))
301             return hres;
302
303         var_set_jsdisp(retv, obj);
304         break;
305     }
306     default:
307         FIXME("unimplemented flags %x\n", flags);
308         return E_NOTIMPL;
309     }
310
311     return S_OK;
312 }
313
314 static HRESULT alloc_number(script_ctx_t *ctx, jsdisp_t *object_prototype, NumberInstance **ret)
315 {
316     NumberInstance *number;
317     HRESULT hres;
318
319     number = heap_alloc_zero(sizeof(NumberInstance));
320     if(!number)
321         return E_OUTOFMEMORY;
322
323     if(object_prototype)
324         hres = init_dispex(&number->dispex, ctx, &Number_info, object_prototype);
325     else
326         hres = init_dispex_from_constr(&number->dispex, ctx, &Number_info, ctx->number_constr);
327     if(FAILED(hres))
328         return hres;
329
330     *ret = number;
331     return S_OK;
332 }
333
334 HRESULT create_number_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
335 {
336     NumberInstance *number;
337     HRESULT hres;
338
339     static const WCHAR NumberW[] = {'N','u','m','b','e','r',0};
340
341     hres = alloc_number(ctx, object_prototype, &number);
342     if(FAILED(hres))
343         return hres;
344
345     number->value = 0;
346     hres = create_builtin_function(ctx, NumberConstr_value, NumberW, NULL,
347             PROPF_CONSTR|1, &number->dispex, ret);
348
349     jsdisp_release(&number->dispex);
350     return hres;
351 }
352
353 HRESULT create_number(script_ctx_t *ctx, double value, jsdisp_t **ret)
354 {
355     NumberInstance *number;
356     HRESULT hres;
357
358     hres = alloc_number(ctx, NULL, &number);
359     if(FAILED(hres))
360         return hres;
361
362     number->value = value;
363
364     *ret = &number->dispex;
365     return S_OK;
366 }