msvcp: Sync num_get<>::_Getifld.
[wine/multimedia.git] / dlls / jscript / jsutils.c
blobac26ef37d42675e824a4a6bc01d5811e872703df
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 case VT_ARRAY|VT_VARIANT:
56 return "{VT_ARRAY|VT_VARIANT: ...}";
57 default:
58 return wine_dbg_sprintf("{vt %d}", V_VT(v));
62 const char *debugstr_jsval(const jsval_t v)
64 switch(jsval_type(v)) {
65 case JSV_UNDEFINED:
66 return "undefined";
67 case JSV_NULL:
68 return "null";
69 case JSV_OBJECT:
70 return wine_dbg_sprintf("obj(%p)", get_object(v));
71 case JSV_STRING:
72 return wine_dbg_sprintf("str(%s)", debugstr_jsstr(get_string(v)));
73 case JSV_NUMBER:
74 return wine_dbg_sprintf("%lf", get_number(v));
75 case JSV_BOOL:
76 return get_bool(v) ? "true" : "false";
77 case JSV_VARIANT:
78 return debugstr_variant(get_variant(v));
81 assert(0);
82 return NULL;
85 #define MIN_BLOCK_SIZE 128
86 #define ARENA_FREE_FILLER 0xaa
88 static inline DWORD block_size(DWORD block)
90 return MIN_BLOCK_SIZE << block;
93 void jsheap_init(jsheap_t *heap)
95 memset(heap, 0, sizeof(*heap));
96 list_init(&heap->custom_blocks);
99 void *jsheap_alloc(jsheap_t *heap, DWORD size)
101 struct list *list;
102 void *tmp;
104 if(!heap->block_cnt) {
105 if(!heap->blocks) {
106 heap->blocks = heap_alloc(sizeof(void*));
107 if(!heap->blocks)
108 return NULL;
111 tmp = heap_alloc(block_size(0));
112 if(!tmp)
113 return NULL;
115 heap->blocks[0] = tmp;
116 heap->block_cnt = 1;
119 if(heap->offset + size <= block_size(heap->last_block)) {
120 tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
121 heap->offset += size;
122 return tmp;
125 if(size <= block_size(heap->last_block+1)) {
126 if(heap->last_block+1 == heap->block_cnt) {
127 tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
128 if(!tmp)
129 return NULL;
131 heap->blocks = tmp;
132 heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
133 if(!heap->blocks[heap->block_cnt])
134 return NULL;
136 heap->block_cnt++;
139 heap->last_block++;
140 heap->offset = size;
141 return heap->blocks[heap->last_block];
144 list = heap_alloc(size + sizeof(struct list));
145 if(!list)
146 return NULL;
148 list_add_head(&heap->custom_blocks, list);
149 return list+1;
152 void *jsheap_grow(jsheap_t *heap, void *mem, DWORD size, DWORD inc)
154 void *ret;
156 if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
157 && heap->offset+inc < block_size(heap->last_block)) {
158 heap->offset += inc;
159 return mem;
162 ret = jsheap_alloc(heap, size+inc);
163 if(ret) /* FIXME: avoid copying for custom blocks */
164 memcpy(ret, mem, size);
165 return ret;
168 void jsheap_clear(jsheap_t *heap)
170 struct list *tmp;
172 if(!heap)
173 return;
175 while((tmp = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
176 list_remove(tmp);
177 heap_free(tmp);
180 if(WARN_ON(heap)) {
181 DWORD i;
183 for(i=0; i < heap->block_cnt; i++)
184 memset(heap->blocks[i], ARENA_FREE_FILLER, block_size(i));
187 heap->last_block = heap->offset = 0;
188 heap->mark = FALSE;
191 void jsheap_free(jsheap_t *heap)
193 DWORD i;
195 jsheap_clear(heap);
197 for(i=0; i < heap->block_cnt; i++)
198 heap_free(heap->blocks[i]);
199 heap_free(heap->blocks);
201 jsheap_init(heap);
204 jsheap_t *jsheap_mark(jsheap_t *heap)
206 if(heap->mark)
207 return NULL;
209 heap->mark = TRUE;
210 return heap;
213 void jsval_release(jsval_t val)
215 switch(jsval_type(val)) {
216 case JSV_OBJECT:
217 if(get_object(val))
218 IDispatch_Release(get_object(val));
219 break;
220 case JSV_STRING:
221 jsstr_release(get_string(val));
222 break;
223 case JSV_VARIANT:
224 VariantClear(get_variant(val));
225 heap_free(get_variant(val));
226 break;
227 default:
228 break;
232 static HRESULT jsval_variant(jsval_t *val, VARIANT *var)
234 VARIANT *v;
235 HRESULT hres;
237 __JSVAL_TYPE(*val) = JSV_VARIANT;
238 __JSVAL_VAR(*val) = v = heap_alloc(sizeof(VARIANT));
239 if(!v)
240 return E_OUTOFMEMORY;
242 V_VT(v) = VT_EMPTY;
243 hres = VariantCopy(v, var);
244 if(FAILED(hres))
245 heap_free(v);
246 return hres;
249 HRESULT jsval_copy(jsval_t v, jsval_t *r)
251 switch(jsval_type(v)) {
252 case JSV_UNDEFINED:
253 case JSV_NULL:
254 case JSV_NUMBER:
255 case JSV_BOOL:
256 *r = v;
257 return S_OK;
258 case JSV_OBJECT:
259 if(get_object(v))
260 IDispatch_AddRef(get_object(v));
261 *r = v;
262 return S_OK;
263 case JSV_STRING: {
264 jsstr_addref(get_string(v));
265 *r = v;
266 return S_OK;
268 case JSV_VARIANT:
269 return jsval_variant(r, get_variant(v));
272 assert(0);
273 return E_FAIL;
276 HRESULT variant_to_jsval(VARIANT *var, jsval_t *r)
278 switch(V_VT(var)) {
279 case VT_EMPTY:
280 *r = jsval_undefined();
281 return S_OK;
282 case VT_NULL:
283 *r = jsval_null();
284 return S_OK;
285 case VT_BOOL:
286 *r = jsval_bool(V_BOOL(var));
287 return S_OK;
288 case VT_I4:
289 *r = jsval_number(V_I4(var));
290 return S_OK;
291 case VT_R8:
292 *r = jsval_number(V_R8(var));
293 return S_OK;
294 case VT_BSTR: {
295 jsstr_t *str;
297 str = jsstr_alloc_len(V_BSTR(var), SysStringLen(V_BSTR(var)));
298 if(!str)
299 return E_OUTOFMEMORY;
300 if(!V_BSTR(var))
301 str->length_flags |= JSSTR_FLAG_NULLBSTR;
303 *r = jsval_string(str);
304 return S_OK;
306 case VT_DISPATCH: {
307 if(V_DISPATCH(var))
308 IDispatch_AddRef(V_DISPATCH(var));
309 *r = jsval_disp(V_DISPATCH(var));
310 return S_OK;
312 case VT_I2:
313 *r = jsval_number(V_I2(var));
314 return S_OK;
315 case VT_INT:
316 *r = jsval_number(V_INT(var));
317 return S_OK;
318 default:
319 return jsval_variant(r, var);
323 HRESULT jsval_to_variant(jsval_t val, VARIANT *retv)
325 switch(jsval_type(val)) {
326 case JSV_UNDEFINED:
327 V_VT(retv) = VT_EMPTY;
328 return S_OK;
329 case JSV_NULL:
330 V_VT(retv) = VT_NULL;
331 return S_OK;
332 case JSV_OBJECT:
333 V_VT(retv) = VT_DISPATCH;
334 if(get_object(val))
335 IDispatch_AddRef(get_object(val));
336 V_DISPATCH(retv) = get_object(val);
337 return S_OK;
338 case JSV_STRING: {
339 jsstr_t *str = get_string(val);
341 V_VT(retv) = VT_BSTR;
342 if(str->length_flags & JSSTR_FLAG_NULLBSTR) {
343 V_BSTR(retv) = NULL;
344 }else {
345 V_BSTR(retv) = SysAllocStringLen(str->str, jsstr_length(str));
346 if(!V_BSTR(retv))
347 return E_OUTOFMEMORY;
349 return S_OK;
351 case JSV_NUMBER: {
352 double n = get_number(val);
354 if(is_int32(n)) {
355 V_VT(retv) = VT_I4;
356 V_I4(retv) = n;
357 }else {
358 V_VT(retv) = VT_R8;
359 V_R8(retv) = n;
362 return S_OK;
364 case JSV_BOOL:
365 V_VT(retv) = VT_BOOL;
366 V_BOOL(retv) = get_bool(val) ? VARIANT_TRUE : VARIANT_FALSE;
367 return S_OK;
368 case JSV_VARIANT:
369 V_VT(retv) = VT_EMPTY;
370 return VariantCopy(retv, get_variant(val));
373 assert(0);
374 return E_FAIL;
377 /* ECMA-262 3rd Edition 9.1 */
378 HRESULT to_primitive(script_ctx_t *ctx, jsval_t val, jsval_t *ret, hint_t hint)
380 if(is_object_instance(val)) {
381 jsdisp_t *jsdisp;
382 jsval_t prim;
383 DISPID id;
384 HRESULT hres;
386 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
387 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
389 if(!get_object(val)) {
390 *ret = jsval_null();
391 return S_OK;
394 jsdisp = iface_to_jsdisp((IUnknown*)get_object(val));
395 if(!jsdisp)
396 return disp_propget(ctx, get_object(val), DISPID_VALUE, ret);
398 if(hint == NO_HINT)
399 hint = is_class(jsdisp, JSCLASS_DATE) ? HINT_STRING : HINT_NUMBER;
401 /* Native implementation doesn't throw TypeErrors, returns strange values */
403 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? toStringW : valueOfW, 0, &id);
404 if(SUCCEEDED(hres)) {
405 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
406 if(FAILED(hres)) {
407 WARN("call error - forwarding exception\n");
408 jsdisp_release(jsdisp);
409 return hres;
410 }else if(!is_object_instance(prim)) {
411 jsdisp_release(jsdisp);
412 *ret = prim;
413 return S_OK;
414 }else {
415 IDispatch_Release(get_object(prim));
419 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? valueOfW : toStringW, 0, &id);
420 if(SUCCEEDED(hres)) {
421 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
422 if(FAILED(hres)) {
423 WARN("call error - forwarding exception\n");
424 jsdisp_release(jsdisp);
425 return hres;
426 }else if(!is_object_instance(prim)) {
427 jsdisp_release(jsdisp);
428 *ret = prim;
429 return S_OK;
430 }else {
431 IDispatch_Release(get_object(prim));
435 jsdisp_release(jsdisp);
437 WARN("failed\n");
438 return throw_type_error(ctx, JS_E_TO_PRIMITIVE, NULL);
441 return jsval_copy(val, ret);
445 /* ECMA-262 3rd Edition 9.2 */
446 HRESULT to_boolean(jsval_t val, BOOL *ret)
448 switch(jsval_type(val)) {
449 case JSV_UNDEFINED:
450 case JSV_NULL:
451 *ret = FALSE;
452 return S_OK;
453 case JSV_OBJECT:
454 *ret = get_object(val) != NULL;
455 return S_OK;
456 case JSV_STRING:
457 *ret = jsstr_length(get_string(val)) != 0;
458 return S_OK;
459 case JSV_NUMBER:
460 *ret = !isnan(get_number(val)) && get_number(val);
461 return S_OK;
462 case JSV_BOOL:
463 *ret = get_bool(val);
464 return S_OK;
465 case JSV_VARIANT:
466 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
467 return E_NOTIMPL;
470 assert(0);
471 return E_FAIL;
474 static int hex_to_int(WCHAR c)
476 if('0' <= c && c <= '9')
477 return c-'0';
479 if('a' <= c && c <= 'f')
480 return c-'a'+10;
482 if('A' <= c && c <= 'F')
483 return c-'A'+10;
485 return -1;
488 /* ECMA-262 3rd Edition 9.3.1 */
489 static HRESULT str_to_number(jsstr_t *str, double *ret)
491 const WCHAR *ptr = str->str;
492 BOOL neg = FALSE;
493 DOUBLE d = 0.0;
495 static const WCHAR infinityW[] = {'I','n','f','i','n','i','t','y'};
497 if(!ptr) {
498 *ret = 0;
499 return S_OK;
502 while(isspaceW(*ptr))
503 ptr++;
505 if(*ptr == '-') {
506 neg = TRUE;
507 ptr++;
508 }else if(*ptr == '+') {
509 ptr++;
512 if(!strncmpW(ptr, infinityW, sizeof(infinityW)/sizeof(WCHAR))) {
513 ptr += sizeof(infinityW)/sizeof(WCHAR);
514 while(*ptr && isspaceW(*ptr))
515 ptr++;
517 if(*ptr)
518 *ret = NAN;
519 else
520 *ret = neg ? -INFINITY : INFINITY;
521 return S_OK;
524 if(*ptr == '0' && ptr[1] == 'x') {
525 DWORD l = 0;
527 ptr += 2;
528 while((l = hex_to_int(*ptr)) != -1) {
529 d = d*16 + l;
530 ptr++;
533 *ret = d;
534 return S_OK;
537 while(isdigitW(*ptr))
538 d = d*10 + (*ptr++ - '0');
540 if(*ptr == 'e' || *ptr == 'E') {
541 BOOL eneg = FALSE;
542 LONG l = 0;
544 ptr++;
545 if(*ptr == '-') {
546 ptr++;
547 eneg = TRUE;
548 }else if(*ptr == '+') {
549 ptr++;
552 while(isdigitW(*ptr))
553 l = l*10 + (*ptr++ - '0');
554 if(eneg)
555 l = -l;
557 d *= pow(10, l);
558 }else if(*ptr == '.') {
559 DOUBLE dec = 0.1;
561 ptr++;
562 while(isdigitW(*ptr)) {
563 d += dec * (*ptr++ - '0');
564 dec *= 0.1;
568 while(isspaceW(*ptr))
569 ptr++;
571 if(*ptr) {
572 *ret = NAN;
573 return S_OK;
576 if(neg)
577 d = -d;
579 *ret = d;
580 return S_OK;
583 /* ECMA-262 3rd Edition 9.3 */
584 HRESULT to_number(script_ctx_t *ctx, jsval_t val, double *ret)
586 switch(jsval_type(val)) {
587 case JSV_UNDEFINED:
588 *ret = NAN;
589 return S_OK;
590 case JSV_NULL:
591 *ret = 0;
592 return S_OK;
593 case JSV_NUMBER:
594 *ret = get_number(val);
595 return S_OK;
596 case JSV_STRING:
597 return str_to_number(get_string(val), ret);
598 case JSV_OBJECT: {
599 jsval_t prim;
600 HRESULT hres;
602 hres = to_primitive(ctx, val, &prim, HINT_NUMBER);
603 if(FAILED(hres))
604 return hres;
606 hres = to_number(ctx, prim, ret);
607 jsval_release(prim);
608 return hres;
610 case JSV_BOOL:
611 *ret = get_bool(val) ? 1 : 0;
612 return S_OK;
613 case JSV_VARIANT:
614 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
615 return E_NOTIMPL;
618 assert(0);
619 return E_FAIL;
622 /* ECMA-262 3rd Edition 9.4 */
623 HRESULT to_integer(script_ctx_t *ctx, jsval_t v, double *ret)
625 double n;
626 HRESULT hres;
628 hres = to_number(ctx, v, &n);
629 if(FAILED(hres))
630 return hres;
632 if(isnan(n))
633 *ret = 0;
634 else
635 *ret = n >= 0.0 ? floor(n) : -floor(-n);
636 return S_OK;
639 /* ECMA-262 3rd Edition 9.5 */
640 HRESULT to_int32(script_ctx_t *ctx, jsval_t v, INT *ret)
642 double n;
643 HRESULT hres;
645 hres = to_number(ctx, v, &n);
646 if(FAILED(hres))
647 return hres;
649 *ret = isnan(n) || isinf(n) ? 0 : n;
650 return S_OK;
653 /* ECMA-262 3rd Edition 9.6 */
654 HRESULT to_uint32(script_ctx_t *ctx, jsval_t val, DWORD *ret)
656 double n;
657 HRESULT hres;
659 hres = to_number(ctx, val, &n);
660 if(FAILED(hres))
661 return hres;
663 *ret = isnan(n) || isinf(n) ? 0 : n;
664 return S_OK;
667 static jsstr_t *int_to_string(int i)
669 WCHAR buf[12], *p;
670 BOOL neg = FALSE;
672 if(!i) {
673 static const WCHAR zeroW[] = {'0',0};
674 return jsstr_alloc(zeroW);
677 if(i < 0) {
678 neg = TRUE;
679 i = -i;
682 p = buf + sizeof(buf)/sizeof(*buf)-1;
683 *p-- = 0;
684 while(i) {
685 *p-- = i%10 + '0';
686 i /= 10;
689 if(neg)
690 *p = '-';
691 else
692 p++;
694 return jsstr_alloc(p);
697 HRESULT double_to_string(double n, jsstr_t **str)
699 const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};
701 if(isnan(n)) {
702 *str = jsstr_nan();
703 }else if(isinf(n)) {
704 *str = jsstr_alloc(n<0 ? InfinityW : InfinityW+1);
705 }else if(is_int32(n)) {
706 *str = int_to_string(n);
707 }else {
708 VARIANT strv, v;
709 HRESULT hres;
711 /* FIXME: Don't use VariantChangeTypeEx */
712 V_VT(&v) = VT_R8;
713 V_R8(&v) = n;
714 V_VT(&strv) = VT_EMPTY;
715 hres = VariantChangeTypeEx(&strv, &v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
716 if(FAILED(hres))
717 return hres;
719 *str = jsstr_alloc(V_BSTR(&strv));
720 SysFreeString(V_BSTR(&strv));
723 return *str ? S_OK : E_OUTOFMEMORY;
726 /* ECMA-262 3rd Edition 9.8 */
727 HRESULT to_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str)
729 const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
730 const WCHAR nullW[] = {'n','u','l','l',0};
731 const WCHAR trueW[] = {'t','r','u','e',0};
732 const WCHAR falseW[] = {'f','a','l','s','e',0};
734 switch(jsval_type(val)) {
735 case JSV_UNDEFINED:
736 *str = jsstr_alloc(undefinedW);
737 break;
738 case JSV_NULL:
739 *str = jsstr_alloc(nullW);
740 break;
741 case JSV_NUMBER:
742 return double_to_string(get_number(val), str);
743 case JSV_STRING:
744 *str = jsstr_addref(get_string(val));
745 break;
746 case JSV_OBJECT: {
747 jsval_t prim;
748 HRESULT hres;
750 hres = to_primitive(ctx, val, &prim, HINT_STRING);
751 if(FAILED(hres))
752 return hres;
754 hres = to_string(ctx, prim, str);
755 jsval_release(prim);
756 return hres;
758 case JSV_BOOL:
759 *str = jsstr_alloc(get_bool(val) ? trueW : falseW);
760 break;
761 default:
762 FIXME("unsupported %s\n", debugstr_jsval(val));
763 return E_NOTIMPL;
766 return *str ? S_OK : E_OUTOFMEMORY;
769 /* ECMA-262 3rd Edition 9.9 */
770 HRESULT to_object(script_ctx_t *ctx, jsval_t val, IDispatch **disp)
772 jsdisp_t *dispex;
773 HRESULT hres;
775 switch(jsval_type(val)) {
776 case JSV_STRING:
777 hres = create_string(ctx, get_string(val), &dispex);
778 if(FAILED(hres))
779 return hres;
781 *disp = to_disp(dispex);
782 break;
783 case JSV_NUMBER:
784 hres = create_number(ctx, get_number(val), &dispex);
785 if(FAILED(hres))
786 return hres;
788 *disp = to_disp(dispex);
789 break;
790 case JSV_OBJECT:
791 if(get_object(val)) {
792 *disp = get_object(val);
793 IDispatch_AddRef(*disp);
794 }else {
795 jsdisp_t *obj;
797 hres = create_object(ctx, NULL, &obj);
798 if(FAILED(hres))
799 return hres;
801 *disp = to_disp(obj);
803 break;
804 case JSV_BOOL:
805 hres = create_bool(ctx, get_bool(val), &dispex);
806 if(FAILED(hres))
807 return hres;
809 *disp = to_disp(dispex);
810 break;
811 case JSV_VARIANT:
812 switch(V_VT(get_variant(val))) {
813 case VT_ARRAY|VT_VARIANT:
814 hres = create_vbarray(ctx, V_ARRAY(get_variant(val)), &dispex);
815 if(FAILED(hres))
816 return hres;
818 *disp = to_disp(dispex);
819 break;
821 default:
822 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val)));
823 return E_NOTIMPL;
825 break;
826 default:
827 FIXME("unsupported %s\n", debugstr_jsval(val));
828 return E_NOTIMPL;
831 return S_OK;
834 HRESULT variant_change_type(script_ctx_t *ctx, VARIANT *dst, VARIANT *src, VARTYPE vt)
836 jsval_t val;
837 HRESULT hres;
839 clear_ei(ctx);
840 hres = variant_to_jsval(src, &val);
841 if(FAILED(hres))
842 return hres;
844 switch(vt) {
845 case VT_I2:
846 case VT_I4: {
847 INT i;
849 hres = to_int32(ctx, val, &i);
850 if(SUCCEEDED(hres)) {
851 if(vt == VT_I4)
852 V_I4(dst) = i;
853 else
854 V_I2(dst) = i;
856 break;
858 case VT_R8: {
859 double n;
860 hres = to_number(ctx, val, &n);
861 if(SUCCEEDED(hres))
862 V_R8(dst) = n;
863 break;
865 case VT_R4: {
866 double n;
868 hres = to_number(ctx, val, &n);
869 if(SUCCEEDED(hres))
870 V_R4(dst) = n;
871 break;
873 case VT_BOOL: {
874 BOOL b;
876 hres = to_boolean(val, &b);
877 if(SUCCEEDED(hres))
878 V_BOOL(dst) = b ? VARIANT_TRUE : VARIANT_FALSE;
879 break;
881 case VT_BSTR: {
882 jsstr_t *str;
884 hres = to_string(ctx, val, &str);
885 if(FAILED(hres))
886 break;
888 if(str->length_flags & JSSTR_FLAG_NULLBSTR) {
889 V_BSTR(dst) = NULL;
890 break;
893 V_BSTR(dst) = SysAllocStringLen(str->str, jsstr_length(str));
894 if(!V_BSTR(dst))
895 hres = E_OUTOFMEMORY;
896 break;
898 case VT_EMPTY:
899 hres = V_VT(src) == VT_EMPTY ? S_OK : E_NOTIMPL;
900 break;
901 case VT_NULL:
902 hres = V_VT(src) == VT_NULL ? S_OK : E_NOTIMPL;
903 break;
904 default:
905 FIXME("vt %d not implemented\n", vt);
906 hres = E_NOTIMPL;
909 jsval_release(val);
910 if(FAILED(hres))
911 return hres;
913 V_VT(dst) = vt;
914 return S_OK;
917 static inline JSCaller *impl_from_IServiceProvider(IServiceProvider *iface)
919 return CONTAINING_RECORD(iface, JSCaller, IServiceProvider_iface);
922 static HRESULT WINAPI JSCaller_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
924 JSCaller *This = impl_from_IServiceProvider(iface);
926 if(IsEqualGUID(&IID_IUnknown, riid)) {
927 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
928 *ppv = &This->IServiceProvider_iface;
929 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
930 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
931 *ppv = &This->IServiceProvider_iface;
932 }else {
933 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
934 *ppv = NULL;
935 return E_NOINTERFACE;
938 IUnknown_AddRef((IUnknown*)*ppv);
939 return S_OK;
942 static ULONG WINAPI JSCaller_AddRef(IServiceProvider *iface)
944 JSCaller *This = impl_from_IServiceProvider(iface);
945 LONG ref = InterlockedIncrement(&This->ref);
947 TRACE("(%p) ref=%d\n", This, ref);
949 return ref;
952 static ULONG WINAPI JSCaller_Release(IServiceProvider *iface)
954 JSCaller *This = impl_from_IServiceProvider(iface);
955 LONG ref = InterlockedIncrement(&This->ref);
957 TRACE("(%p) ref=%d\n", This, ref);
959 if(!ref) {
960 assert(!This->ctx);
961 heap_free(This);
964 return ref;
967 static HRESULT WINAPI JSCaller_QueryService(IServiceProvider *iface, REFGUID guidService,
968 REFIID riid, void **ppv)
970 JSCaller *This = impl_from_IServiceProvider(iface);
972 if(IsEqualGUID(guidService, &SID_VariantConversion) && This->ctx && This->ctx->active_script) {
973 TRACE("(%p)->(SID_VariantConversion)\n", This);
974 return IActiveScript_QueryInterface(This->ctx->active_script, riid, ppv);
977 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
979 *ppv = NULL;
980 return E_NOINTERFACE;
983 static const IServiceProviderVtbl ServiceProviderVtbl = {
984 JSCaller_QueryInterface,
985 JSCaller_AddRef,
986 JSCaller_Release,
987 JSCaller_QueryService
990 HRESULT create_jscaller(script_ctx_t *ctx)
992 JSCaller *ret;
994 ret = heap_alloc(sizeof(*ret));
995 if(!ret)
996 return E_OUTOFMEMORY;
998 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
999 ret->ref = 1;
1000 ret->ctx = ctx;
1002 ctx->jscaller = ret;
1003 return S_OK;