gdi32: Don't set the bitmap owner when selecting a pattern brush.
[wine.git] / dlls / jscript / number.c
blob82dd5bce2f0dad6718bfd0f99ccf03d1074c2cec
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 jsdisp_t 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, JS_E_NUMBER_EXPECTED, 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, JS_E_INVALIDARG, 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, JS_E_NUMBER_EXPECTED, 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, JS_E_FUNCTION_EXPECTED, 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 jsdisp_t *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 var_set_jsdisp(retv, obj);
306 break;
308 default:
309 FIXME("unimplemented flags %x\n", flags);
310 return E_NOTIMPL;
313 return S_OK;
316 static HRESULT alloc_number(script_ctx_t *ctx, jsdisp_t *object_prototype, NumberInstance **ret)
318 NumberInstance *number;
319 HRESULT hres;
321 number = heap_alloc_zero(sizeof(NumberInstance));
322 if(!number)
323 return E_OUTOFMEMORY;
325 if(object_prototype)
326 hres = init_dispex(&number->dispex, ctx, &Number_info, object_prototype);
327 else
328 hres = init_dispex_from_constr(&number->dispex, ctx, &Number_info, ctx->number_constr);
329 if(FAILED(hres))
330 return hres;
332 *ret = number;
333 return S_OK;
336 HRESULT create_number_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
338 NumberInstance *number;
339 HRESULT hres;
341 static const WCHAR NumberW[] = {'N','u','m','b','e','r',0};
343 hres = alloc_number(ctx, object_prototype, &number);
344 if(FAILED(hres))
345 return hres;
347 V_VT(&number->num) = VT_I4;
348 hres = create_builtin_function(ctx, NumberConstr_value, NumberW, NULL,
349 PROPF_CONSTR|1, &number->dispex, ret);
351 jsdisp_release(&number->dispex);
352 return hres;
355 HRESULT create_number(script_ctx_t *ctx, VARIANT *num, jsdisp_t **ret)
357 NumberInstance *number;
358 HRESULT hres;
360 hres = alloc_number(ctx, NULL, &number);
361 if(FAILED(hres))
362 return hres;
364 number->num = *num;
366 *ret = &number->dispex;
367 return S_OK;