winemaker: Warn on include path starting with drive letter.
[wine/multimedia.git] / dlls / jscript / number.c
blob00de9abba85f75f3eb9e90e3c8611f1764c8ac67
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 "config.h"
20 #include "wine/port.h"
22 #include <math.h>
24 #include "jscript.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
30 typedef struct {
31 DispatchEx dispex;
33 VARIANT num;
34 } NumberInstance;
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, IServiceProvider *sp)
59 NumberInstance *number;
60 INT radix = 10;
61 DOUBLE val;
62 BSTR str;
63 HRESULT hres;
65 TRACE("\n");
67 if(!(number = number_this(jsthis)))
68 return throw_type_error(ctx, ei, IDS_NOT_NUM, NULL);
70 if(arg_cnt(dp)) {
71 hres = to_int32(ctx, get_arg(dp, 0), ei, &radix);
72 if(FAILED(hres))
73 return hres;
75 if(radix<2 || radix>36)
76 return throw_type_error(ctx, ei, IDS_INVALID_CALL_ARG, NULL);
79 if(V_VT(&number->num) == VT_I4)
80 val = V_I4(&number->num);
81 else
82 val = V_R8(&number->num);
84 if(radix==10 || isnan(val) || isinf(val)) {
85 hres = to_string(ctx, &number->num, ei, &str);
86 if(FAILED(hres))
87 return hres;
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;
95 if(val<0) {
96 val = -val;
97 buf[idx++] = '-';
100 while(1) {
101 integ = floor(val);
102 frac = val-integ;
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++;
114 if(idx<NUMBER_TOSTRING_BUF_SIZE) {
115 INT beg = buf[0]=='-'?1:0;
116 INT end = idx-1;
117 WCHAR wch;
119 while(end > beg) {
120 wch = buf[beg];
121 buf[beg++] = buf[end];
122 buf[end--] = wch;
126 if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.';
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++;
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;
145 break;
148 while(buf[idx-1] == '0') idx--;
149 if(buf[idx-1] == '.') idx--;
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;
158 if(log_radix<0) {
159 log_radix = -log_radix;
160 ch = '-';
162 else ch = '+';
163 sprintfW(&buf[idx], formatW, ch, (int)log_radix);
166 else buf[idx] = '\0';
168 str = SysAllocString(buf);
169 if(!str)
170 return E_OUTOFMEMORY;
173 if(retv) {
174 V_VT(retv) = VT_BSTR;
175 V_BSTR(retv) = str;
176 }else {
177 SysFreeString(str);
179 return S_OK;
182 static HRESULT Number_toLocaleString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
183 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
185 FIXME("\n");
186 return E_NOTIMPL;
189 static HRESULT Number_toFixed(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
190 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
192 FIXME("\n");
193 return E_NOTIMPL;
196 static HRESULT Number_toExponential(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
197 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
199 FIXME("\n");
200 return E_NOTIMPL;
203 static HRESULT Number_toPrecision(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
204 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
206 FIXME("\n");
207 return E_NOTIMPL;
210 static HRESULT Number_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
211 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
213 NumberInstance *number;
215 TRACE("\n");
217 if(!(number = number_this(jsthis)))
218 return throw_type_error(ctx, ei, IDS_NOT_NUM, NULL);
220 if(retv)
221 *retv = number->num;
222 return S_OK;
225 static HRESULT Number_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
226 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
228 NumberInstance *number = number_from_vdisp(jsthis);
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;
237 default:
238 FIXME("flags %x\n", flags);
239 return E_NOTIMPL;
242 return S_OK;
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}
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
263 static HRESULT NumberConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
264 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
266 VARIANT num;
267 HRESULT hres;
269 TRACE("\n");
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;
278 return S_OK;
281 hres = to_number(ctx, get_arg(dp, 0), ei, &num);
282 if(FAILED(hres))
283 return hres;
285 if(retv)
286 *retv = num;
287 break;
289 case DISPATCH_CONSTRUCT: {
290 DispatchEx *obj;
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;
301 hres = create_number(ctx, &num, &obj);
302 if(FAILED(hres))
303 return hres;
305 V_VT(retv) = VT_DISPATCH;
306 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
307 break;
309 default:
310 FIXME("unimplemented flags %x\n", flags);
311 return E_NOTIMPL;
314 return S_OK;
317 static HRESULT alloc_number(script_ctx_t *ctx, DispatchEx *object_prototype, NumberInstance **ret)
319 NumberInstance *number;
320 HRESULT hres;
322 number = heap_alloc_zero(sizeof(NumberInstance));
323 if(!number)
324 return E_OUTOFMEMORY;
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;
333 *ret = number;
334 return S_OK;
337 HRESULT create_number_constr(script_ctx_t *ctx, DispatchEx *object_prototype, DispatchEx **ret)
339 NumberInstance *number;
340 HRESULT hres;
342 static const WCHAR NumberW[] = {'N','u','m','b','e','r',0};
344 hres = alloc_number(ctx, object_prototype, &number);
345 if(FAILED(hres))
346 return hres;
348 V_VT(&number->num) = VT_I4;
349 hres = create_builtin_function(ctx, NumberConstr_value, NumberW, NULL,
350 PROPF_CONSTR|1, &number->dispex, ret);
352 jsdisp_release(&number->dispex);
353 return hres;
356 HRESULT create_number(script_ctx_t *ctx, VARIANT *num, DispatchEx **ret)
358 NumberInstance *number;
359 HRESULT hres;
361 hres = alloc_number(ctx, NULL, &number);
362 if(FAILED(hres))
363 return hres;
365 number->num = *num;
367 *ret = &number->dispex;
368 return S_OK;