include/wine/test.h: Fix tests compilation with a PSDK compiler.
[wine/multimedia.git] / dlls / jscript / jsutils.c
blob4773fc9987dbf9502bc830023b470042d4230fa8
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>
23 #include <assert.h>
25 #include "jscript.h"
26 #include "engine.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
31 WINE_DECLARE_DEBUG_CHANNEL(heap);
33 const char *debugstr_variant(const VARIANT *v)
35 if(!v)
36 return "(null)";
38 switch(V_VT(v)) {
39 case VT_EMPTY:
40 return "{VT_EMPTY}";
41 case VT_NULL:
42 return "{VT_NULL}";
43 case VT_I4:
44 return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v));
45 case VT_UI4:
46 return wine_dbg_sprintf("{VT_UI4: %u}", V_UI4(v));
47 case VT_R8:
48 return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v));
49 case VT_BSTR:
50 return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v)));
51 case VT_DISPATCH:
52 return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v));
53 case VT_BOOL:
54 return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v));
55 default:
56 return wine_dbg_sprintf("{vt %d}", V_VT(v));
60 #define MIN_BLOCK_SIZE 128
61 #define ARENA_FREE_FILLER 0xaa
63 static inline DWORD block_size(DWORD block)
65 return MIN_BLOCK_SIZE << block;
68 void jsheap_init(jsheap_t *heap)
70 memset(heap, 0, sizeof(*heap));
71 list_init(&heap->custom_blocks);
74 void *jsheap_alloc(jsheap_t *heap, DWORD size)
76 struct list *list;
77 void *tmp;
79 if(!heap->block_cnt) {
80 if(!heap->blocks) {
81 heap->blocks = heap_alloc(sizeof(void*));
82 if(!heap->blocks)
83 return NULL;
86 tmp = heap_alloc(block_size(0));
87 if(!tmp)
88 return NULL;
90 heap->blocks[0] = tmp;
91 heap->block_cnt = 1;
94 if(heap->offset + size <= block_size(heap->last_block)) {
95 tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
96 heap->offset += size;
97 return tmp;
100 if(size <= block_size(heap->last_block+1)) {
101 if(heap->last_block+1 == heap->block_cnt) {
102 tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
103 if(!tmp)
104 return NULL;
106 heap->blocks = tmp;
107 heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
108 if(!heap->blocks[heap->block_cnt])
109 return NULL;
111 heap->block_cnt++;
114 heap->last_block++;
115 heap->offset = size;
116 return heap->blocks[heap->last_block];
119 list = heap_alloc(size + sizeof(struct list));
120 if(!list)
121 return NULL;
123 list_add_head(&heap->custom_blocks, list);
124 return list+1;
127 void *jsheap_grow(jsheap_t *heap, void *mem, DWORD size, DWORD inc)
129 void *ret;
131 if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
132 && heap->offset+inc < block_size(heap->last_block)) {
133 heap->offset += inc;
134 return mem;
137 ret = jsheap_alloc(heap, size+inc);
138 if(ret) /* FIXME: avoid copying for custom blocks */
139 memcpy(ret, mem, size);
140 return ret;
143 void jsheap_clear(jsheap_t *heap)
145 struct list *tmp;
147 if(!heap)
148 return;
150 while((tmp = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
151 list_remove(tmp);
152 heap_free(tmp);
155 if(WARN_ON(heap)) {
156 DWORD i;
158 for(i=0; i < heap->block_cnt; i++)
159 memset(heap->blocks[i], ARENA_FREE_FILLER, block_size(i));
162 heap->last_block = heap->offset = 0;
163 heap->mark = FALSE;
166 void jsheap_free(jsheap_t *heap)
168 DWORD i;
170 jsheap_clear(heap);
172 for(i=0; i < heap->block_cnt; i++)
173 heap_free(heap->blocks[i]);
174 heap_free(heap->blocks);
176 jsheap_init(heap);
179 jsheap_t *jsheap_mark(jsheap_t *heap)
181 if(heap->mark)
182 return NULL;
184 heap->mark = TRUE;
185 return heap;
188 /* ECMA-262 3rd Edition 9.1 */
189 HRESULT to_primitive(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret, hint_t hint)
191 switch(V_VT(v)) {
192 case VT_EMPTY:
193 case VT_NULL:
194 case VT_BOOL:
195 case VT_I4:
196 case VT_R8:
197 *ret = *v;
198 break;
199 case VT_BSTR:
200 V_VT(ret) = VT_BSTR;
201 V_BSTR(ret) = SysAllocString(V_BSTR(v));
202 break;
203 case VT_DISPATCH: {
204 jsdisp_t *jsdisp;
205 DISPID id;
206 DISPPARAMS dp = {NULL, NULL, 0, 0};
207 HRESULT hres;
209 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
210 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
212 if(!V_DISPATCH(v)) {
213 V_VT(ret) = VT_NULL;
214 break;
217 jsdisp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
218 if(!jsdisp) {
219 V_VT(ret) = VT_EMPTY;
220 return disp_propget(ctx, V_DISPATCH(v), DISPID_VALUE, ret, ei);
223 if(hint == NO_HINT)
224 hint = is_class(jsdisp, JSCLASS_DATE) ? HINT_STRING : HINT_NUMBER;
226 /* Native implementation doesn't throw TypeErrors, returns strange values */
228 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? toStringW : valueOfW, 0, &id);
229 if(SUCCEEDED(hres)) {
230 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, &dp, ret, ei);
231 if(FAILED(hres)) {
232 WARN("call error - forwarding exception\n");
233 jsdisp_release(jsdisp);
234 return hres;
236 else if(V_VT(ret) != VT_DISPATCH) {
237 jsdisp_release(jsdisp);
238 return S_OK;
240 else
241 IDispatch_Release(V_DISPATCH(ret));
244 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? valueOfW : toStringW, 0, &id);
245 if(SUCCEEDED(hres)) {
246 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, &dp, ret, ei);
247 if(FAILED(hres)) {
248 WARN("call error - forwarding exception\n");
249 jsdisp_release(jsdisp);
250 return hres;
252 else if(V_VT(ret) != VT_DISPATCH) {
253 jsdisp_release(jsdisp);
254 return S_OK;
256 else
257 IDispatch_Release(V_DISPATCH(ret));
260 jsdisp_release(jsdisp);
262 WARN("failed\n");
263 return throw_type_error(ctx, ei, JS_E_TO_PRIMITIVE, NULL);
265 default:
266 FIXME("Unimplemented for vt %d\n", V_VT(v));
267 return E_NOTIMPL;
270 return S_OK;
273 /* ECMA-262 3rd Edition 9.2 */
274 HRESULT to_boolean(VARIANT *v, VARIANT_BOOL *b)
276 switch(V_VT(v)) {
277 case VT_EMPTY:
278 case VT_NULL:
279 *b = VARIANT_FALSE;
280 break;
281 case VT_I4:
282 *b = V_I4(v) ? VARIANT_TRUE : VARIANT_FALSE;
283 break;
284 case VT_R8:
285 if(isnan(V_R8(v))) *b = VARIANT_FALSE;
286 else *b = V_R8(v) ? VARIANT_TRUE : VARIANT_FALSE;
287 break;
288 case VT_BSTR:
289 *b = V_BSTR(v) && *V_BSTR(v) ? VARIANT_TRUE : VARIANT_FALSE;
290 break;
291 case VT_DISPATCH:
292 *b = V_DISPATCH(v) ? VARIANT_TRUE : VARIANT_FALSE;
293 break;
294 case VT_BOOL:
295 *b = V_BOOL(v);
296 break;
297 default:
298 FIXME("unimplemented for vt %d\n", V_VT(v));
299 return E_NOTIMPL;
302 return S_OK;
305 static int hex_to_int(WCHAR c)
307 if('0' <= c && c <= '9')
308 return c-'0';
310 if('a' <= c && c <= 'f')
311 return c-'a'+10;
313 if('A' <= c && c <= 'F')
314 return c-'A'+10;
316 return -1;
319 /* ECMA-262 3rd Edition 9.3.1 */
320 static HRESULT str_to_number(BSTR str, double *ret)
322 const WCHAR *ptr = str;
323 BOOL neg = FALSE;
324 DOUBLE d = 0.0;
326 static const WCHAR infinityW[] = {'I','n','f','i','n','i','t','y'};
328 if(!ptr) {
329 *ret = 0;
330 return S_OK;
333 while(isspaceW(*ptr))
334 ptr++;
336 if(*ptr == '-') {
337 neg = TRUE;
338 ptr++;
339 }else if(*ptr == '+') {
340 ptr++;
343 if(!strncmpW(ptr, infinityW, sizeof(infinityW)/sizeof(WCHAR))) {
344 ptr += sizeof(infinityW)/sizeof(WCHAR);
345 while(*ptr && isspaceW(*ptr))
346 ptr++;
348 if(*ptr)
349 *ret = ret_nan();
350 else
351 *ret = neg ? -ret_inf() : ret_inf();
352 return S_OK;
355 if(*ptr == '0' && ptr[1] == 'x') {
356 DWORD l = 0;
358 ptr += 2;
359 while((l = hex_to_int(*ptr)) != -1) {
360 d = d*16 + l;
361 ptr++;
364 *ret = d;
365 return S_OK;
368 while(isdigitW(*ptr))
369 d = d*10 + (*ptr++ - '0');
371 if(*ptr == 'e' || *ptr == 'E') {
372 BOOL eneg = FALSE;
373 LONG l = 0;
375 ptr++;
376 if(*ptr == '-') {
377 ptr++;
378 eneg = TRUE;
379 }else if(*ptr == '+') {
380 ptr++;
383 while(isdigitW(*ptr))
384 l = l*10 + (*ptr++ - '0');
385 if(eneg)
386 l = -l;
388 d *= pow(10, l);
389 }else if(*ptr == '.') {
390 DOUBLE dec = 0.1;
392 ptr++;
393 while(isdigitW(*ptr)) {
394 d += dec * (*ptr++ - '0');
395 dec *= 0.1;
399 while(isspaceW(*ptr))
400 ptr++;
402 if(*ptr) {
403 *ret = ret_nan();
404 return S_OK;
407 if(neg)
408 d = -d;
410 *ret = d;
411 return S_OK;
414 /* ECMA-262 3rd Edition 9.3 */
415 HRESULT to_number(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, double *ret)
417 switch(V_VT(v)) {
418 case VT_EMPTY:
419 *ret = ret_nan();
420 break;
421 case VT_NULL:
422 *ret = 0;
423 break;
424 case VT_I4:
425 *ret = V_I4(v);
426 break;
427 case VT_R8:
428 *ret = V_R8(v);
429 break;
430 case VT_BSTR:
431 return str_to_number(V_BSTR(v), ret);
432 case VT_DISPATCH: {
433 VARIANT prim;
434 HRESULT hres;
436 hres = to_primitive(ctx, v, ei, &prim, HINT_NUMBER);
437 if(FAILED(hres))
438 return hres;
440 hres = to_number(ctx, &prim, ei, ret);
441 VariantClear(&prim);
442 return hres;
444 case VT_BOOL:
445 *ret = V_BOOL(v) ? 1 : 0;
446 break;
447 default:
448 FIXME("unimplemented for vt %d\n", V_VT(v));
449 return E_NOTIMPL;
452 return S_OK;
455 /* ECMA-262 3rd Edition 9.4 */
456 HRESULT to_integer(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret)
458 double n;
459 HRESULT hres;
461 if(V_VT(v) == VT_I4) {
462 *ret = *v;
463 return S_OK;
466 hres = to_number(ctx, v, ei, &n);
467 if(FAILED(hres))
468 return hres;
470 if(isnan(n)) {
471 V_VT(ret) = VT_I4;
472 V_I4(ret) = 0;
473 }else {
474 num_set_val(ret, n >= 0.0 ? floor(n) : -floor(-n));
477 return S_OK;
480 /* ECMA-262 3rd Edition 9.5 */
481 HRESULT to_int32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, INT *ret)
483 double n;
484 HRESULT hres;
486 if(V_VT(v) == VT_I4) {
487 *ret = V_I4(v);
488 return S_OK;
491 hres = to_number(ctx, v, ei, &n);
492 if(FAILED(hres))
493 return hres;
495 *ret = isnan(n) || isinf(n) ? 0 : n;
496 return S_OK;
499 /* ECMA-262 3rd Edition 9.6 */
500 HRESULT to_uint32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, DWORD *ret)
502 double n;
503 HRESULT hres;
505 if(V_VT(v) == VT_I4) {
506 *ret = V_I4(v);
507 return S_OK;
510 hres = to_number(ctx, v, ei, &n);
511 if(FAILED(hres))
512 return hres;
514 *ret = isnan(n) || isinf(n) ? 0 : n;
515 return S_OK;
518 BSTR int_to_bstr(int i)
520 WCHAR buf[12], *p;
521 BOOL neg = FALSE;
523 if(!i) {
524 static const WCHAR zeroW[] = {'0',0};
525 return SysAllocString(zeroW);
528 if(i < 0) {
529 neg = TRUE;
530 i = -i;
533 p = buf + sizeof(buf)/sizeof(*buf)-1;
534 *p-- = 0;
535 while(i) {
536 *p-- = i%10 + '0';
537 i /= 10;
540 if(neg)
541 *p = '-';
542 else
543 p++;
545 return SysAllocString(p);
548 HRESULT double_to_bstr(double n, BSTR *str)
550 const WCHAR NaNW[] = {'N','a','N',0};
551 const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};
553 if(isnan(n)) {
554 *str = SysAllocString(NaNW);
555 }else if(isinf(n)) {
556 *str = SysAllocString(n<0 ? InfinityW : InfinityW+1);
557 }else {
558 VARIANT strv, v;
559 HRESULT hres;
561 V_VT(&v) = VT_R8;
562 V_R8(&v) = n;
563 V_VT(&strv) = VT_EMPTY;
564 hres = VariantChangeTypeEx(&strv, &v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
565 if(FAILED(hres))
566 return hres;
568 *str = V_BSTR(&strv);
571 return *str ? S_OK : E_OUTOFMEMORY;
574 /* ECMA-262 3rd Edition 9.8 */
575 HRESULT to_string(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, BSTR *str)
577 const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
578 const WCHAR nullW[] = {'n','u','l','l',0};
579 const WCHAR trueW[] = {'t','r','u','e',0};
580 const WCHAR falseW[] = {'f','a','l','s','e',0};
582 switch(V_VT(v)) {
583 case VT_EMPTY:
584 *str = SysAllocString(undefinedW);
585 break;
586 case VT_NULL:
587 *str = SysAllocString(nullW);
588 break;
589 case VT_I4:
590 *str = int_to_bstr(V_I4(v));
591 break;
592 case VT_R8:
593 return double_to_bstr(V_R8(v), str);
594 case VT_BSTR:
595 *str = SysAllocString(V_BSTR(v));
596 break;
597 case VT_DISPATCH: {
598 VARIANT prim;
599 HRESULT hres;
601 hres = to_primitive(ctx, v, ei, &prim, HINT_STRING);
602 if(FAILED(hres))
603 return hres;
605 hres = to_string(ctx, &prim, ei, str);
606 VariantClear(&prim);
607 return hres;
609 case VT_BOOL:
610 *str = SysAllocString(V_BOOL(v) ? trueW : falseW);
611 break;
612 default:
613 FIXME("unsupported vt %d\n", V_VT(v));
614 return E_NOTIMPL;
617 return *str ? S_OK : E_OUTOFMEMORY;
620 /* ECMA-262 3rd Edition 9.9 */
621 HRESULT to_object(script_ctx_t *ctx, VARIANT *v, IDispatch **disp)
623 jsdisp_t *dispex;
624 HRESULT hres;
626 switch(V_VT(v)) {
627 case VT_BSTR:
628 hres = create_string(ctx, V_BSTR(v), SysStringLen(V_BSTR(v)), &dispex);
629 if(FAILED(hres))
630 return hres;
632 *disp = to_disp(dispex);
633 break;
634 case VT_I4:
635 case VT_R8:
636 hres = create_number(ctx, num_val(v), &dispex);
637 if(FAILED(hres))
638 return hres;
640 *disp = to_disp(dispex);
641 break;
642 case VT_DISPATCH:
643 if(V_DISPATCH(v)) {
644 IDispatch_AddRef(V_DISPATCH(v));
645 *disp = V_DISPATCH(v);
646 }else {
647 jsdisp_t *obj;
649 hres = create_object(ctx, NULL, &obj);
650 if(FAILED(hres))
651 return hres;
653 *disp = to_disp(obj);
655 break;
656 case VT_BOOL:
657 hres = create_bool(ctx, V_BOOL(v), &dispex);
658 if(FAILED(hres))
659 return hres;
661 *disp = to_disp(dispex);
662 break;
663 case VT_ARRAY|VT_VARIANT:
664 hres = create_vbarray(ctx, V_ARRAY(v), &dispex);
665 if(FAILED(hres))
666 return hres;
668 *disp = to_disp(dispex);
669 break;
670 default:
671 FIXME("unsupported vt %d\n", V_VT(v));
672 return E_NOTIMPL;
675 return S_OK;
678 HRESULT variant_change_type(script_ctx_t *ctx, VARIANT *dst, VARIANT *src, VARTYPE vt)
680 jsexcept_t ei;
681 HRESULT hres;
683 memset(&ei, 0, sizeof(ei));
685 switch(vt) {
686 case VT_I2:
687 case VT_I4: {
688 INT i;
690 hres = to_int32(ctx, src, &ei, &i);
691 if(SUCCEEDED(hres)) {
692 if(vt == VT_I4)
693 V_I4(dst) = i;
694 else
695 V_I2(dst) = i;
697 break;
699 case VT_R8: {
700 double n;
701 hres = to_number(ctx, src, &ei, &n);
702 if(SUCCEEDED(hres))
703 V_R8(dst) = n;
704 break;
706 case VT_R4: {
707 double n;
709 hres = to_number(ctx, src, &ei, &n);
710 if(SUCCEEDED(hres))
711 V_R4(dst) = n;
712 break;
714 case VT_BOOL: {
715 VARIANT_BOOL b;
717 hres = to_boolean(src, &b);
718 if(SUCCEEDED(hres))
719 V_BOOL(dst) = b;
720 break;
722 case VT_BSTR: {
723 BSTR str;
725 hres = to_string(ctx, src, &ei, &str);
726 if(SUCCEEDED(hres))
727 V_BSTR(dst) = str;
728 break;
730 case VT_EMPTY:
731 hres = V_VT(src) == VT_EMPTY ? S_OK : E_NOTIMPL;
732 break;
733 case VT_NULL:
734 hres = V_VT(src) == VT_NULL ? S_OK : E_NOTIMPL;
735 break;
736 default:
737 FIXME("vt %d not implemented\n", vt);
738 hres = E_NOTIMPL;
741 if(FAILED(hres)) {
742 VariantClear(&ei.var);
743 return hres;
746 V_VT(dst) = vt;
747 return S_OK;
750 static inline JSCaller *impl_from_IServiceProvider(IServiceProvider *iface)
752 return CONTAINING_RECORD(iface, JSCaller, IServiceProvider_iface);
755 static HRESULT WINAPI JSCaller_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
757 JSCaller *This = impl_from_IServiceProvider(iface);
759 if(IsEqualGUID(&IID_IUnknown, riid)) {
760 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
761 *ppv = &This->IServiceProvider_iface;
762 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
763 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
764 *ppv = &This->IServiceProvider_iface;
765 }else {
766 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
767 *ppv = NULL;
768 return E_NOINTERFACE;
771 IUnknown_AddRef((IUnknown*)*ppv);
772 return S_OK;
775 static ULONG WINAPI JSCaller_AddRef(IServiceProvider *iface)
777 JSCaller *This = impl_from_IServiceProvider(iface);
778 LONG ref = InterlockedIncrement(&This->ref);
780 TRACE("(%p) ref=%d\n", This, ref);
782 return ref;
785 static ULONG WINAPI JSCaller_Release(IServiceProvider *iface)
787 JSCaller *This = impl_from_IServiceProvider(iface);
788 LONG ref = InterlockedIncrement(&This->ref);
790 TRACE("(%p) ref=%d\n", This, ref);
792 if(!ref) {
793 assert(!This->ctx);
794 heap_free(This);
797 return ref;
800 static HRESULT WINAPI JSCaller_QueryService(IServiceProvider *iface, REFGUID guidService,
801 REFIID riid, void **ppv)
803 JSCaller *This = impl_from_IServiceProvider(iface);
805 if(IsEqualGUID(guidService, &SID_VariantConversion) && This->ctx && This->ctx->active_script) {
806 TRACE("(%p)->(SID_VariantConversion)\n", This);
807 return IActiveScript_QueryInterface(This->ctx->active_script, riid, ppv);
810 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
812 *ppv = NULL;
813 return E_NOINTERFACE;
816 static const IServiceProviderVtbl ServiceProviderVtbl = {
817 JSCaller_QueryInterface,
818 JSCaller_AddRef,
819 JSCaller_Release,
820 JSCaller_QueryService
823 HRESULT create_jscaller(script_ctx_t *ctx)
825 JSCaller *ret;
827 ret = heap_alloc(sizeof(*ret));
828 if(!ret)
829 return E_OUTOFMEMORY;
831 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
832 ret->ref = 1;
833 ret->ctx = ctx;
835 ctx->jscaller = ret;
836 return S_OK;