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
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
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
;
57 if(!is_class(dispex
, JSCLASS_NUMBER
))
58 return throw_type_error(dispex
->ctx
, ei
, IDS_NOT_NUM
, NULL
);
60 number
= (NumberInstance
*)dispex
;
63 hres
= to_int32(dispex
->ctx
, get_arg(dp
, 0), ei
, &radix
);
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
);
74 val
= V_R8(&number
->num
);
76 if(radix
==10 || isnan(val
) || isinf(val
)) {
77 hres
= to_string(dispex
->ctx
, &number
->num
, ei
, &str
);
83 DOUBLE integ
, frac
, log_radix
= 0;
84 WCHAR buf
[NUMBER_TOSTRING_BUF_SIZE
+16];
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;
106 if(idx
<NUMBER_TOSTRING_BUF_SIZE
) {
107 INT beg
= buf
[0]=='-'?1:0;
113 buf
[beg
++] = buf
[end
];
118 if(idx
!= NUMBER_TOSTRING_BUF_SIZE
) buf
[idx
++] = '.';
120 while(frac
>0 && idx
<NUMBER_TOSTRING_BUF_SIZE
) {
122 buf
[idx
] = fmod(frac
, radix
);
124 if(buf
[idx
]<10) buf
[idx
] += '0';
125 else buf
[idx
] += 'a'-10;
129 if(idx
==NUMBER_TOSTRING_BUF_SIZE
&& !exp
) {
131 idx
= (buf
[0]=='-') ? 1 : 0;
132 log_radix
= floor(log(val
)/log(radix
));
133 val
*= pow(radix
, -log_radix
);
140 while(buf
[idx
-1] == '0') idx
--;
141 if(buf
[idx
-1] == '.') idx
--;
147 static const WCHAR formatW
[] = {'(','e','%','c','%','d',')',0};
151 log_radix
= -log_radix
;
155 sprintfW(&buf
[idx
], formatW
, ch
, (int)log_radix
);
158 else buf
[idx
] = '\0';
160 str
= SysAllocString(buf
);
162 return E_OUTOFMEMORY
;
166 V_VT(retv
) = VT_BSTR
;
174 static HRESULT
Number_toLocaleString(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
175 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
181 static HRESULT
Number_toFixed(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
182 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
188 static HRESULT
Number_toExponential(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
189 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
195 static HRESULT
Number_toPrecision(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
196 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
202 static HRESULT
Number_valueOf(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
203 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
207 if(!is_class(dispex
, JSCLASS_NUMBER
))
208 return throw_type_error(dispex
->ctx
, ei
, IDS_NOT_NUM
, NULL
);
211 NumberInstance
*number
= (NumberInstance
*)dispex
;
217 static HRESULT
Number_hasOwnProperty(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
218 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
224 static HRESULT
Number_propertyIsEnumerable(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
225 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
231 static HRESULT
Number_isPrototypeOf(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
232 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
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
;
245 return throw_type_error(dispex
->ctx
, ei
, IDS_NOT_FUNC
, NULL
);
246 case DISPATCH_PROPERTYGET
:
251 FIXME("flags %x\n", flags
);
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
= {
272 {NULL
, Number_value
, 0},
273 sizeof(Number_props
)/sizeof(*Number_props
),
279 static HRESULT
NumberConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
280 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
297 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &num
);
305 case DISPATCH_CONSTRUCT
: {
309 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &num
);
317 hres
= create_number(dispex
->ctx
, &num
, &obj
);
321 V_VT(retv
) = VT_DISPATCH
;
322 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(obj
);
326 FIXME("unimplemented flags %x\n", flags
);
333 static HRESULT
alloc_number(script_ctx_t
*ctx
, BOOL use_constr
, NumberInstance
**ret
)
335 NumberInstance
*number
;
338 number
= heap_alloc_zero(sizeof(NumberInstance
));
340 return E_OUTOFMEMORY
;
343 hres
= init_dispex_from_constr(&number
->dispex
, ctx
, &Number_info
, ctx
->number_constr
);
345 hres
= init_dispex(&number
->dispex
, ctx
, &Number_info
, NULL
);
353 HRESULT
create_number_constr(script_ctx_t
*ctx
, DispatchEx
**ret
)
355 NumberInstance
*number
;
358 hres
= alloc_number(ctx
, FALSE
, &number
);
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
);
369 HRESULT
create_number(script_ctx_t
*ctx
, VARIANT
*num
, DispatchEx
**ret
)
371 NumberInstance
*number
;
374 hres
= alloc_number(ctx
, TRUE
, &number
);
380 *ret
= &number
->dispex
;