Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / jscript / jsutils.c
blob9e49cc7b8a347abcec1b6fad33c2164a9a869a8f
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"
25 #include "engine.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
30 WINE_DECLARE_DEBUG_CHANNEL(heap);
32 const char *debugstr_variant(const VARIANT *v)
34 if(!v)
35 return "(null)";
37 switch(V_VT(v)) {
38 case VT_EMPTY:
39 return "{VT_EMPTY}";
40 case VT_NULL:
41 return "{VT_NULL}";
42 case VT_I4:
43 return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v));
44 case VT_R8:
45 return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v));
46 case VT_BSTR:
47 return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v)));
48 case VT_DISPATCH:
49 return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v));
50 case VT_BOOL:
51 return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v));
52 default:
53 return wine_dbg_sprintf("{vt %d}", V_VT(v));
57 #define MIN_BLOCK_SIZE 128
58 #define ARENA_FREE_FILLER 0xaa
60 static inline DWORD block_size(DWORD block)
62 return MIN_BLOCK_SIZE << block;
65 void jsheap_init(jsheap_t *heap)
67 memset(heap, 0, sizeof(*heap));
68 list_init(&heap->custom_blocks);
71 void *jsheap_alloc(jsheap_t *heap, DWORD size)
73 struct list *list;
74 void *tmp;
76 if(!heap->block_cnt) {
77 if(!heap->blocks) {
78 heap->blocks = heap_alloc(sizeof(void*));
79 if(!heap->blocks)
80 return NULL;
83 tmp = heap_alloc(block_size(0));
84 if(!tmp)
85 return NULL;
87 heap->blocks[0] = tmp;
88 heap->block_cnt = 1;
91 if(heap->offset + size <= block_size(heap->last_block)) {
92 tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
93 heap->offset += size;
94 return tmp;
97 if(size <= block_size(heap->last_block+1)) {
98 if(heap->last_block+1 == heap->block_cnt) {
99 tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
100 if(!tmp)
101 return NULL;
103 heap->blocks = tmp;
104 heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
105 if(!heap->blocks[heap->block_cnt])
106 return NULL;
108 heap->block_cnt++;
111 heap->last_block++;
112 heap->offset = size;
113 return heap->blocks[heap->last_block];
116 list = heap_alloc(size + sizeof(struct list));
117 if(!list)
118 return NULL;
120 list_add_head(&heap->custom_blocks, list);
121 return list+1;
124 void *jsheap_grow(jsheap_t *heap, void *mem, DWORD size, DWORD inc)
126 if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
127 && heap->offset+inc < block_size(heap->last_block)) {
128 heap->offset += inc;
129 return mem;
132 return jsheap_alloc(heap, size+inc);
135 void jsheap_clear(jsheap_t *heap)
137 struct list *tmp;
139 if(!heap)
140 return;
142 while((tmp = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
143 list_remove(tmp);
144 heap_free(tmp);
147 if(WARN_ON(heap)) {
148 DWORD i;
150 for(i=0; i < heap->block_cnt; i++)
151 memset(heap->blocks[i], ARENA_FREE_FILLER, block_size(i));
154 heap->last_block = heap->offset = 0;
155 heap->mark = FALSE;
158 void jsheap_free(jsheap_t *heap)
160 DWORD i;
162 jsheap_clear(heap);
164 for(i=0; i < heap->block_cnt; i++)
165 heap_free(heap->blocks[i]);
166 heap_free(heap->blocks);
168 jsheap_init(heap);
171 jsheap_t *jsheap_mark(jsheap_t *heap)
173 if(heap->mark)
174 return NULL;
176 heap->mark = TRUE;
177 return heap;
180 /* ECMA-262 3rd Edition 9.1 */
181 HRESULT to_primitive(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret, hint_t hint)
183 switch(V_VT(v)) {
184 case VT_EMPTY:
185 case VT_NULL:
186 case VT_BOOL:
187 case VT_I4:
188 case VT_R8:
189 *ret = *v;
190 break;
191 case VT_BSTR:
192 V_VT(ret) = VT_BSTR;
193 V_BSTR(ret) = SysAllocString(V_BSTR(v));
194 break;
195 case VT_DISPATCH: {
196 DispatchEx *jsdisp;
197 DISPID id;
198 DISPPARAMS dp = {NULL, NULL, 0, 0};
199 HRESULT hres;
201 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
202 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
204 if(!V_DISPATCH(v)) {
205 V_VT(ret) = VT_NULL;
206 break;
209 jsdisp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
210 if(!jsdisp) {
211 V_VT(ret) = VT_EMPTY;
212 return disp_propget(ctx, V_DISPATCH(v), DISPID_VALUE, ret, ei, NULL /*FIXME*/);
215 if(hint == NO_HINT)
216 hint = is_class(jsdisp, JSCLASS_DATE) ? HINT_STRING : HINT_NUMBER;
218 /* Native implementation doesn't throw TypeErrors, returns strange values */
220 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? toStringW : valueOfW, 0, &id);
221 if(SUCCEEDED(hres)) {
222 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, &dp, ret, ei, NULL /*FIXME*/);
223 if(FAILED(hres)) {
224 WARN("call error - forwarding exception\n");
225 jsdisp_release(jsdisp);
226 return hres;
228 else if(V_VT(ret) != VT_DISPATCH) {
229 jsdisp_release(jsdisp);
230 return S_OK;
232 else
233 IDispatch_Release(V_DISPATCH(ret));
236 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? valueOfW : toStringW, 0, &id);
237 if(SUCCEEDED(hres)) {
238 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, &dp, ret, ei, NULL /*FIXME*/);
239 if(FAILED(hres)) {
240 WARN("call error - forwarding exception\n");
241 jsdisp_release(jsdisp);
242 return hres;
244 else if(V_VT(ret) != VT_DISPATCH) {
245 jsdisp_release(jsdisp);
246 return S_OK;
248 else
249 IDispatch_Release(V_DISPATCH(ret));
252 jsdisp_release(jsdisp);
254 WARN("failed\n");
255 return throw_type_error(ctx, ei, IDS_TO_PRIMITIVE, NULL);
257 default:
258 FIXME("Unimplemented for vt %d\n", V_VT(v));
259 return E_NOTIMPL;
262 return S_OK;
265 /* ECMA-262 3rd Edition 9.2 */
266 HRESULT to_boolean(VARIANT *v, VARIANT_BOOL *b)
268 switch(V_VT(v)) {
269 case VT_EMPTY:
270 case VT_NULL:
271 *b = VARIANT_FALSE;
272 break;
273 case VT_I4:
274 *b = V_I4(v) ? VARIANT_TRUE : VARIANT_FALSE;
275 break;
276 case VT_R8:
277 if(isnan(V_R8(v))) *b = VARIANT_FALSE;
278 else *b = V_R8(v) ? VARIANT_TRUE : VARIANT_FALSE;
279 break;
280 case VT_BSTR:
281 *b = V_BSTR(v) && *V_BSTR(v) ? VARIANT_TRUE : VARIANT_FALSE;
282 break;
283 case VT_DISPATCH:
284 *b = V_DISPATCH(v) ? VARIANT_TRUE : VARIANT_FALSE;
285 break;
286 case VT_BOOL:
287 *b = V_BOOL(v);
288 break;
289 default:
290 FIXME("unimplemented for vt %d\n", V_VT(v));
291 return E_NOTIMPL;
294 return S_OK;
297 static int hex_to_int(WCHAR c)
299 if('0' <= c && c <= '9')
300 return c-'0';
302 if('a' <= c && c <= 'f')
303 return c-'a'+10;
305 if('A' <= c && c <= 'F')
306 return c-'A'+10;
308 return -1;
311 /* ECMA-262 3rd Edition 9.3.1 */
312 static HRESULT str_to_number(BSTR str, VARIANT *ret)
314 const WCHAR *ptr = str;
315 BOOL neg = FALSE;
316 DOUBLE d = 0.0;
318 static const WCHAR infinityW[] = {'I','n','f','i','n','i','t','y'};
320 while(isspaceW(*ptr))
321 ptr++;
323 if(*ptr == '-') {
324 neg = TRUE;
325 ptr++;
326 }else if(*ptr == '+') {
327 ptr++;
330 if(!strncmpW(ptr, infinityW, sizeof(infinityW)/sizeof(WCHAR))) {
331 ptr += sizeof(infinityW)/sizeof(WCHAR);
332 while(*ptr && isspaceW(*ptr))
333 ptr++;
335 if(*ptr)
336 num_set_nan(ret);
337 else
338 num_set_inf(ret, !neg);
339 return S_OK;
342 if(*ptr == '0' && ptr[1] == 'x') {
343 DWORD l = 0;
345 ptr += 2;
346 while((l = hex_to_int(*ptr)) != -1) {
347 d = d*16 + l;
348 ptr++;
351 num_set_val(ret, d);
352 return S_OK;
355 while(isdigitW(*ptr))
356 d = d*10 + (*ptr++ - '0');
358 if(*ptr == 'e' || *ptr == 'E') {
359 BOOL eneg = FALSE;
360 LONG l = 0;
362 ptr++;
363 if(*ptr == '-') {
364 ptr++;
365 eneg = TRUE;
366 }else if(*ptr == '+') {
367 ptr++;
370 while(isdigitW(*ptr))
371 l = l*10 + (*ptr++ - '0');
372 if(eneg)
373 l = -l;
375 d *= pow(10, l);
376 }else if(*ptr == '.') {
377 DOUBLE dec = 0.1;
379 ptr++;
380 while(isdigitW(*ptr)) {
381 d += dec * (*ptr++ - '0');
382 dec *= 0.1;
386 while(isspaceW(*ptr))
387 ptr++;
389 if(*ptr) {
390 num_set_nan(ret);
391 return S_OK;
394 if(neg)
395 d = -d;
397 num_set_val(ret, d);
398 return S_OK;
401 /* ECMA-262 3rd Edition 9.3 */
402 HRESULT to_number(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret)
404 switch(V_VT(v)) {
405 case VT_EMPTY:
406 num_set_nan(ret);
407 break;
408 case VT_NULL:
409 V_VT(ret) = VT_I4;
410 V_I4(ret) = 0;
411 break;
412 case VT_I4:
413 case VT_R8:
414 *ret = *v;
415 break;
416 case VT_BSTR:
417 return str_to_number(V_BSTR(v), ret);
418 case VT_DISPATCH: {
419 VARIANT prim;
420 HRESULT hres;
422 hres = to_primitive(ctx, v, ei, &prim, HINT_NUMBER);
423 if(FAILED(hres))
424 return hres;
426 hres = to_number(ctx, &prim, ei, ret);
427 VariantClear(&prim);
428 return hres;
430 case VT_BOOL:
431 V_VT(ret) = VT_I4;
432 V_I4(ret) = V_BOOL(v) ? 1 : 0;
433 break;
434 default:
435 FIXME("unimplemented for vt %d\n", V_VT(v));
436 return E_NOTIMPL;
439 return S_OK;
442 /* ECMA-262 3rd Edition 9.4 */
443 HRESULT to_integer(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret)
445 VARIANT num;
446 HRESULT hres;
448 hres = to_number(ctx, v, ei, &num);
449 if(FAILED(hres))
450 return hres;
452 if(V_VT(&num) == VT_I4) {
453 *ret = num;
454 }else if(isnan(V_R8(&num))) {
455 V_VT(ret) = VT_I4;
456 V_I4(ret) = 0;
457 }else {
458 num_set_val(ret, V_R8(&num) >= 0.0 ? floor(V_R8(&num)) : -floor(-V_R8(&num)));
461 return S_OK;
464 /* ECMA-262 3rd Edition 9.5 */
465 HRESULT to_int32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, INT *ret)
467 VARIANT num;
468 HRESULT hres;
470 hres = to_number(ctx, v, ei, &num);
471 if(FAILED(hres))
472 return hres;
474 if(V_VT(&num) == VT_I4)
475 *ret = V_I4(&num);
476 else
477 *ret = isnan(V_R8(&num)) || isinf(V_R8(&num)) ? 0 : (INT)V_R8(&num);
478 return S_OK;
481 /* ECMA-262 3rd Edition 9.6 */
482 HRESULT to_uint32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, DWORD *ret)
484 VARIANT num;
485 HRESULT hres;
487 hres = to_number(ctx, v, ei, &num);
488 if(FAILED(hres))
489 return hres;
491 if(V_VT(&num) == VT_I4)
492 *ret = V_I4(&num);
493 else
494 *ret = isnan(V_R8(&num)) || isinf(V_R8(&num)) ? 0 : (DWORD)V_R8(&num);
495 return S_OK;
498 static BSTR int_to_bstr(INT i)
500 WCHAR buf[12], *p;
501 BOOL neg = FALSE;
503 if(!i) {
504 static const WCHAR zeroW[] = {'0',0};
505 return SysAllocString(zeroW);
508 if(i < 0) {
509 neg = TRUE;
510 i = -i;
513 p = buf + sizeof(buf)/sizeof(*buf)-1;
514 *p-- = 0;
515 while(i) {
516 *p-- = i%10 + '0';
517 i /= 10;
520 if(neg)
521 *p = '-';
522 else
523 p++;
525 return SysAllocString(p);
528 /* ECMA-262 3rd Edition 9.8 */
529 HRESULT to_string(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, BSTR *str)
531 const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
532 const WCHAR nullW[] = {'n','u','l','l',0};
533 const WCHAR trueW[] = {'t','r','u','e',0};
534 const WCHAR falseW[] = {'f','a','l','s','e',0};
535 const WCHAR NaNW[] = {'N','a','N',0};
536 const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};
538 switch(V_VT(v)) {
539 case VT_EMPTY:
540 *str = SysAllocString(undefinedW);
541 break;
542 case VT_NULL:
543 *str = SysAllocString(nullW);
544 break;
545 case VT_I4:
546 *str = int_to_bstr(V_I4(v));
547 break;
548 case VT_R8: {
549 if(isnan(V_R8(v)))
550 *str = SysAllocString(NaNW);
551 else if(isinf(V_R8(v)))
552 *str = SysAllocString(V_R8(v)<0 ? InfinityW : InfinityW+1);
553 else {
554 VARIANT strv;
555 HRESULT hres;
557 V_VT(&strv) = VT_EMPTY;
558 hres = VariantChangeTypeEx(&strv, v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
559 if(FAILED(hres))
560 return hres;
562 *str = V_BSTR(&strv);
563 return S_OK;
565 break;
567 case VT_BSTR:
568 *str = SysAllocString(V_BSTR(v));
569 break;
570 case VT_DISPATCH: {
571 VARIANT prim;
572 HRESULT hres;
574 hres = to_primitive(ctx, v, ei, &prim, HINT_STRING);
575 if(FAILED(hres))
576 return hres;
578 hres = to_string(ctx, &prim, ei, str);
579 VariantClear(&prim);
580 return hres;
582 case VT_BOOL:
583 *str = SysAllocString(V_BOOL(v) ? trueW : falseW);
584 break;
585 default:
586 FIXME("unsupported vt %d\n", V_VT(v));
587 return E_NOTIMPL;
590 return *str ? S_OK : E_OUTOFMEMORY;
593 /* ECMA-262 3rd Edition 9.9 */
594 HRESULT to_object(script_ctx_t *ctx, VARIANT *v, IDispatch **disp)
596 DispatchEx *dispex;
597 HRESULT hres;
599 switch(V_VT(v)) {
600 case VT_BSTR:
601 hres = create_string(ctx, V_BSTR(v), SysStringLen(V_BSTR(v)), &dispex);
602 if(FAILED(hres))
603 return hres;
605 *disp = (IDispatch*)_IDispatchEx_(dispex);
606 break;
607 case VT_I4:
608 case VT_R8:
609 hres = create_number(ctx, v, &dispex);
610 if(FAILED(hres))
611 return hres;
613 *disp = (IDispatch*)_IDispatchEx_(dispex);
614 break;
615 case VT_DISPATCH:
616 if(V_DISPATCH(v)) {
617 IDispatch_AddRef(V_DISPATCH(v));
618 *disp = V_DISPATCH(v);
619 }else {
620 DispatchEx *obj;
622 hres = create_object(ctx, NULL, &obj);
623 if(FAILED(hres))
624 return hres;
626 *disp = (IDispatch*)_IDispatchEx_(obj);
628 break;
629 case VT_BOOL:
630 hres = create_bool(ctx, V_BOOL(v), &dispex);
631 if(FAILED(hres))
632 return hres;
634 *disp = (IDispatch*)_IDispatchEx_(dispex);
635 break;
636 default:
637 FIXME("unsupported vt %d\n", V_VT(v));
638 return E_NOTIMPL;
641 return S_OK;