t2embed: Implement TTLoadEmbeddedFont as a stub.
[wine/multimedia.git] / dlls / jscript / number.c
blob5399c91615a76d8a076852c5f1d582cabac683aa
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 <math.h>
21 #include "jscript.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
27 typedef struct {
28 DispatchEx dispex;
30 VARIANT num;
31 } NumberInstance;
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;
50 INT radix = 10;
51 DOUBLE val;
52 BSTR str;
53 HRESULT hres;
55 TRACE("\n");
57 if(!is_class(dispex, JSCLASS_NUMBER)) {
58 FIXME("throw TypeError\n");
59 return E_FAIL;
62 number = (NumberInstance*)dispex;
64 if(arg_cnt(dp)) {
65 hres = to_int32(dispex->ctx, get_arg(dp, 0), ei, &radix);
66 if(FAILED(hres))
67 return hres;
69 if(radix<2 || radix>36) {
70 FIXME("throw TypeError\n");
71 return E_FAIL;
75 if(V_VT(&number->num) == VT_I4)
76 val = V_I4(&number->num);
77 else
78 val = V_R8(&number->num);
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;
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;
91 if(val<0) {
92 val = -val;
93 buf[idx++] = '-';
96 while(1) {
97 integ = floor(val);
98 frac = val-integ;
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++;
110 if(idx<NUMBER_TOSTRING_BUF_SIZE) {
111 INT beg = buf[0]=='-'?1:0;
112 INT end = idx-1;
113 WCHAR wch;
115 while(end > beg) {
116 wch = buf[beg];
117 buf[beg++] = buf[end];
118 buf[end--] = wch;
122 if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.';
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++;
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;
141 break;
144 while(buf[idx-1] == '0') idx--;
145 if(buf[idx-1] == '.') idx--;
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;
154 if(log_radix<0) {
155 log_radix = -log_radix;
156 ch = '-';
158 else ch = '+';
159 sprintfW(&buf[idx], formatW, ch, (int)log_radix);
162 else buf[idx] = '\0';
164 str = SysAllocString(buf);
165 if(!str)
166 return E_OUTOFMEMORY;
169 if(retv) {
170 V_VT(retv) = VT_BSTR;
171 V_BSTR(retv) = str;
172 }else {
173 SysFreeString(str);
175 return S_OK;
178 static HRESULT Number_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
179 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
181 FIXME("\n");
182 return E_NOTIMPL;
185 static HRESULT Number_toFixed(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
186 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
188 FIXME("\n");
189 return E_NOTIMPL;
192 static HRESULT Number_toExponential(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
193 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
195 FIXME("\n");
196 return E_NOTIMPL;
199 static HRESULT Number_toPrecision(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
200 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
202 FIXME("\n");
203 return E_NOTIMPL;
206 static HRESULT Number_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
207 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
209 TRACE("\n");
211 if(!is_class(dispex, JSCLASS_NUMBER)) {
212 FIXME("throw TypeError\n");
213 return E_FAIL;
216 if(retv) {
217 NumberInstance *number = (NumberInstance*)dispex;
218 *retv = number->num;
220 return S_OK;
223 static HRESULT Number_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
224 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
226 FIXME("\n");
227 return E_NOTIMPL;
230 static HRESULT Number_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
231 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
233 FIXME("\n");
234 return E_NOTIMPL;
237 static HRESULT Number_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
238 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
240 FIXME("\n");
241 return E_NOTIMPL;
244 static HRESULT Number_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
245 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
247 NumberInstance *number = (NumberInstance*)dispex;
249 switch(flags) {
250 case DISPATCH_PROPERTYGET:
251 *retv = number->num;
252 break;
254 default:
255 FIXME("flags %x\n", flags);
256 return E_NOTIMPL;
259 return S_OK;
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}
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
283 static HRESULT NumberConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
284 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
286 VARIANT num;
287 HRESULT hres;
289 TRACE("\n");
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;
298 return S_OK;
301 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &num);
302 if(FAILED(hres))
303 return hres;
305 if(retv)
306 *retv = num;
307 break;
309 case DISPATCH_CONSTRUCT: {
310 DispatchEx *obj;
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;
321 hres = create_number(dispex->ctx, &num, &obj);
322 if(FAILED(hres))
323 return hres;
325 V_VT(retv) = VT_DISPATCH;
326 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
327 break;
329 default:
330 FIXME("unimplemented flags %x\n", flags);
331 return E_NOTIMPL;
334 return S_OK;
337 static HRESULT alloc_number(script_ctx_t *ctx, BOOL use_constr, NumberInstance **ret)
339 NumberInstance *number;
340 HRESULT hres;
342 number = heap_alloc_zero(sizeof(NumberInstance));
343 if(!number)
344 return E_OUTOFMEMORY;
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;
353 *ret = number;
354 return S_OK;
357 HRESULT create_number_constr(script_ctx_t *ctx, DispatchEx **ret)
359 NumberInstance *number;
360 HRESULT hres;
362 hres = alloc_number(ctx, FALSE, &number);
363 if(FAILED(hres))
364 return hres;
366 V_VT(&number->num) = VT_I4;
367 hres = create_builtin_function(ctx, NumberConstr_value, NULL, PROPF_CONSTR, &number->dispex, ret);
369 jsdisp_release(&number->dispex);
370 return hres;
373 HRESULT create_number(script_ctx_t *ctx, VARIANT *num, DispatchEx **ret)
375 NumberInstance *number;
376 HRESULT hres;
378 hres = alloc_number(ctx, TRUE, &number);
379 if(FAILED(hres))
380 return hres;
382 number->num = *num;
384 *ret = &number->dispex;
385 return S_OK;