jscript: Run some SunSpider benchmarks in interactive mode.
[wine.git] / dlls / jscript / jsutils.c
blob60ca4740de359aed501b26ee4031d6cc64a3ce8f
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 heap_pool_init(heap_pool_t *heap)
95 memset(heap, 0, sizeof(*heap));
96 list_init(&heap->custom_blocks);
99 void *heap_pool_alloc(heap_pool_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 *heap_pool_grow(heap_pool_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 = heap_pool_alloc(heap, size+inc);
163 if(ret) /* FIXME: avoid copying for custom blocks */
164 memcpy(ret, mem, size);
165 return ret;
168 void heap_pool_clear(heap_pool_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 heap_pool_free(heap_pool_t *heap)
193 DWORD i;
195 heap_pool_clear(heap);
197 for(i=0; i < heap->block_cnt; i++)
198 heap_free(heap->blocks[i]);
199 heap_free(heap->blocks);
201 heap_pool_init(heap);
204 heap_pool_t *heap_pool_mark(heap_pool_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 case VT_UNKNOWN:
319 if(V_UNKNOWN(var)) {
320 IDispatch *disp;
321 HRESULT hres;
323 hres = IUnknown_QueryInterface(V_UNKNOWN(var), &IID_IDispatch, (void**)&disp);
324 if(SUCCEEDED(hres)) {
325 *r = jsval_disp(disp);
326 return S_OK;
329 /* fall through */
330 default:
331 return jsval_variant(r, var);
335 HRESULT jsval_to_variant(jsval_t val, VARIANT *retv)
337 switch(jsval_type(val)) {
338 case JSV_UNDEFINED:
339 V_VT(retv) = VT_EMPTY;
340 return S_OK;
341 case JSV_NULL:
342 V_VT(retv) = VT_NULL;
343 return S_OK;
344 case JSV_OBJECT:
345 V_VT(retv) = VT_DISPATCH;
346 if(get_object(val))
347 IDispatch_AddRef(get_object(val));
348 V_DISPATCH(retv) = get_object(val);
349 return S_OK;
350 case JSV_STRING: {
351 jsstr_t *str = get_string(val);
353 V_VT(retv) = VT_BSTR;
354 if(str->length_flags & JSSTR_FLAG_NULLBSTR) {
355 V_BSTR(retv) = NULL;
356 }else {
357 V_BSTR(retv) = SysAllocStringLen(NULL, jsstr_length(str));
358 if(V_BSTR(retv))
359 jsstr_flush(str, V_BSTR(retv));
360 else
361 return E_OUTOFMEMORY;
363 return S_OK;
365 case JSV_NUMBER: {
366 double n = get_number(val);
368 if(is_int32(n)) {
369 V_VT(retv) = VT_I4;
370 V_I4(retv) = n;
371 }else {
372 V_VT(retv) = VT_R8;
373 V_R8(retv) = n;
376 return S_OK;
378 case JSV_BOOL:
379 V_VT(retv) = VT_BOOL;
380 V_BOOL(retv) = get_bool(val) ? VARIANT_TRUE : VARIANT_FALSE;
381 return S_OK;
382 case JSV_VARIANT:
383 V_VT(retv) = VT_EMPTY;
384 return VariantCopy(retv, get_variant(val));
387 assert(0);
388 return E_FAIL;
391 /* ECMA-262 3rd Edition 9.1 */
392 HRESULT to_primitive(script_ctx_t *ctx, jsval_t val, jsval_t *ret, hint_t hint)
394 if(is_object_instance(val)) {
395 jsdisp_t *jsdisp;
396 jsval_t prim;
397 DISPID id;
398 HRESULT hres;
400 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
401 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
403 if(!get_object(val)) {
404 *ret = jsval_null();
405 return S_OK;
408 jsdisp = iface_to_jsdisp((IUnknown*)get_object(val));
409 if(!jsdisp)
410 return disp_propget(ctx, get_object(val), DISPID_VALUE, ret);
412 if(hint == NO_HINT)
413 hint = is_class(jsdisp, JSCLASS_DATE) ? HINT_STRING : HINT_NUMBER;
415 /* Native implementation doesn't throw TypeErrors, returns strange values */
417 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? toStringW : valueOfW, 0, &id);
418 if(SUCCEEDED(hres)) {
419 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
420 if(FAILED(hres)) {
421 WARN("call error - forwarding exception\n");
422 jsdisp_release(jsdisp);
423 return hres;
424 }else if(!is_object_instance(prim)) {
425 jsdisp_release(jsdisp);
426 *ret = prim;
427 return S_OK;
428 }else {
429 IDispatch_Release(get_object(prim));
433 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? valueOfW : toStringW, 0, &id);
434 if(SUCCEEDED(hres)) {
435 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
436 if(FAILED(hres)) {
437 WARN("call error - forwarding exception\n");
438 jsdisp_release(jsdisp);
439 return hres;
440 }else if(!is_object_instance(prim)) {
441 jsdisp_release(jsdisp);
442 *ret = prim;
443 return S_OK;
444 }else {
445 IDispatch_Release(get_object(prim));
449 jsdisp_release(jsdisp);
451 WARN("failed\n");
452 return throw_type_error(ctx, JS_E_TO_PRIMITIVE, NULL);
455 return jsval_copy(val, ret);
459 /* ECMA-262 3rd Edition 9.2 */
460 HRESULT to_boolean(jsval_t val, BOOL *ret)
462 switch(jsval_type(val)) {
463 case JSV_UNDEFINED:
464 case JSV_NULL:
465 *ret = FALSE;
466 return S_OK;
467 case JSV_OBJECT:
468 *ret = get_object(val) != NULL;
469 return S_OK;
470 case JSV_STRING:
471 *ret = jsstr_length(get_string(val)) != 0;
472 return S_OK;
473 case JSV_NUMBER:
474 *ret = !isnan(get_number(val)) && get_number(val);
475 return S_OK;
476 case JSV_BOOL:
477 *ret = get_bool(val);
478 return S_OK;
479 case JSV_VARIANT:
480 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
481 return E_NOTIMPL;
484 assert(0);
485 return E_FAIL;
488 static int hex_to_int(WCHAR c)
490 if('0' <= c && c <= '9')
491 return c-'0';
493 if('a' <= c && c <= 'f')
494 return c-'a'+10;
496 if('A' <= c && c <= 'F')
497 return c-'A'+10;
499 return -1;
502 /* ECMA-262 3rd Edition 9.3.1 */
503 static HRESULT str_to_number(jsstr_t *str, double *ret)
505 const WCHAR *ptr;
506 BOOL neg = FALSE;
507 DOUBLE d = 0.0;
509 static const WCHAR infinityW[] = {'I','n','f','i','n','i','t','y'};
511 ptr = jsstr_flatten(str);
512 if(!ptr)
513 return E_OUTOFMEMORY;
515 while(isspaceW(*ptr))
516 ptr++;
518 if(*ptr == '-') {
519 neg = TRUE;
520 ptr++;
521 }else if(*ptr == '+') {
522 ptr++;
525 if(!strncmpW(ptr, infinityW, sizeof(infinityW)/sizeof(WCHAR))) {
526 ptr += sizeof(infinityW)/sizeof(WCHAR);
527 while(*ptr && isspaceW(*ptr))
528 ptr++;
530 if(*ptr)
531 *ret = NAN;
532 else
533 *ret = neg ? -INFINITY : INFINITY;
534 return S_OK;
537 if(*ptr == '0' && ptr[1] == 'x') {
538 DWORD l = 0;
540 ptr += 2;
541 while((l = hex_to_int(*ptr)) != -1) {
542 d = d*16 + l;
543 ptr++;
546 *ret = d;
547 return S_OK;
550 while(isdigitW(*ptr))
551 d = d*10 + (*ptr++ - '0');
553 if(*ptr == 'e' || *ptr == 'E') {
554 BOOL eneg = FALSE;
555 LONG l = 0;
557 ptr++;
558 if(*ptr == '-') {
559 ptr++;
560 eneg = TRUE;
561 }else if(*ptr == '+') {
562 ptr++;
565 while(isdigitW(*ptr))
566 l = l*10 + (*ptr++ - '0');
567 if(eneg)
568 l = -l;
570 d *= pow(10, l);
571 }else if(*ptr == '.') {
572 DOUBLE dec = 0.1;
574 ptr++;
575 while(isdigitW(*ptr)) {
576 d += dec * (*ptr++ - '0');
577 dec *= 0.1;
581 while(isspaceW(*ptr))
582 ptr++;
584 if(*ptr) {
585 *ret = NAN;
586 return S_OK;
589 if(neg)
590 d = -d;
592 *ret = d;
593 return S_OK;
596 /* ECMA-262 3rd Edition 9.3 */
597 HRESULT to_number(script_ctx_t *ctx, jsval_t val, double *ret)
599 switch(jsval_type(val)) {
600 case JSV_UNDEFINED:
601 *ret = NAN;
602 return S_OK;
603 case JSV_NULL:
604 *ret = 0;
605 return S_OK;
606 case JSV_NUMBER:
607 *ret = get_number(val);
608 return S_OK;
609 case JSV_STRING:
610 return str_to_number(get_string(val), ret);
611 case JSV_OBJECT: {
612 jsval_t prim;
613 HRESULT hres;
615 hres = to_primitive(ctx, val, &prim, HINT_NUMBER);
616 if(FAILED(hres))
617 return hres;
619 hres = to_number(ctx, prim, ret);
620 jsval_release(prim);
621 return hres;
623 case JSV_BOOL:
624 *ret = get_bool(val) ? 1 : 0;
625 return S_OK;
626 case JSV_VARIANT:
627 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
628 return E_NOTIMPL;
631 assert(0);
632 return E_FAIL;
635 /* ECMA-262 3rd Edition 9.4 */
636 HRESULT to_integer(script_ctx_t *ctx, jsval_t v, double *ret)
638 double n;
639 HRESULT hres;
641 hres = to_number(ctx, v, &n);
642 if(FAILED(hres))
643 return hres;
645 if(isnan(n))
646 *ret = 0;
647 else
648 *ret = n >= 0.0 ? floor(n) : -floor(-n);
649 return S_OK;
652 /* ECMA-262 3rd Edition 9.5 */
653 HRESULT to_int32(script_ctx_t *ctx, jsval_t v, INT *ret)
655 double n;
656 HRESULT hres;
658 hres = to_number(ctx, v, &n);
659 if(FAILED(hres))
660 return hres;
662 *ret = isnan(n) || isinf(n) ? 0 : n;
663 return S_OK;
666 /* ECMA-262 3rd Edition 9.6 */
667 HRESULT to_uint32(script_ctx_t *ctx, jsval_t val, DWORD *ret)
669 INT32 n;
670 HRESULT hres;
672 hres = to_int32(ctx, val, &n);
673 if(SUCCEEDED(hres))
674 *ret = n;
675 return hres;
678 static jsstr_t *int_to_string(int i)
680 WCHAR buf[12], *p;
681 BOOL neg = FALSE;
683 if(!i) {
684 static const WCHAR zeroW[] = {'0',0};
685 return jsstr_alloc(zeroW);
688 if(i < 0) {
689 neg = TRUE;
690 i = -i;
693 p = buf + sizeof(buf)/sizeof(*buf)-1;
694 *p-- = 0;
695 while(i) {
696 *p-- = i%10 + '0';
697 i /= 10;
700 if(neg)
701 *p = '-';
702 else
703 p++;
705 return jsstr_alloc(p);
708 HRESULT double_to_string(double n, jsstr_t **str)
710 const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};
712 if(isnan(n)) {
713 *str = jsstr_nan();
714 }else if(isinf(n)) {
715 *str = jsstr_alloc(n<0 ? InfinityW : InfinityW+1);
716 }else if(is_int32(n)) {
717 *str = int_to_string(n);
718 }else {
719 VARIANT strv, v;
720 HRESULT hres;
722 /* FIXME: Don't use VariantChangeTypeEx */
723 V_VT(&v) = VT_R8;
724 V_R8(&v) = n;
725 V_VT(&strv) = VT_EMPTY;
726 hres = VariantChangeTypeEx(&strv, &v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
727 if(FAILED(hres))
728 return hres;
730 *str = jsstr_alloc(V_BSTR(&strv));
731 SysFreeString(V_BSTR(&strv));
734 return *str ? S_OK : E_OUTOFMEMORY;
737 /* ECMA-262 3rd Edition 9.8 */
738 HRESULT to_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str)
740 const WCHAR nullW[] = {'n','u','l','l',0};
741 const WCHAR trueW[] = {'t','r','u','e',0};
742 const WCHAR falseW[] = {'f','a','l','s','e',0};
744 switch(jsval_type(val)) {
745 case JSV_UNDEFINED:
746 *str = jsstr_undefined();
747 return S_OK;
748 case JSV_NULL:
749 *str = jsstr_alloc(nullW);
750 break;
751 case JSV_NUMBER:
752 return double_to_string(get_number(val), str);
753 case JSV_STRING:
754 *str = jsstr_addref(get_string(val));
755 break;
756 case JSV_OBJECT: {
757 jsval_t prim;
758 HRESULT hres;
760 hres = to_primitive(ctx, val, &prim, HINT_STRING);
761 if(FAILED(hres))
762 return hres;
764 hres = to_string(ctx, prim, str);
765 jsval_release(prim);
766 return hres;
768 case JSV_BOOL:
769 *str = jsstr_alloc(get_bool(val) ? trueW : falseW);
770 break;
771 default:
772 FIXME("unsupported %s\n", debugstr_jsval(val));
773 return E_NOTIMPL;
776 return *str ? S_OK : E_OUTOFMEMORY;
779 HRESULT to_flat_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str, const WCHAR **ret_str)
781 HRESULT hres;
783 hres = to_string(ctx, val, str);
784 if(FAILED(hres))
785 return hres;
787 *ret_str = jsstr_flatten(*str);
788 if(!*ret_str) {
789 jsstr_release(*str);
790 return E_OUTOFMEMORY;
793 return S_OK;
796 /* ECMA-262 3rd Edition 9.9 */
797 HRESULT to_object(script_ctx_t *ctx, jsval_t val, IDispatch **disp)
799 jsdisp_t *dispex;
800 HRESULT hres;
802 switch(jsval_type(val)) {
803 case JSV_STRING:
804 hres = create_string(ctx, get_string(val), &dispex);
805 if(FAILED(hres))
806 return hres;
808 *disp = to_disp(dispex);
809 break;
810 case JSV_NUMBER:
811 hres = create_number(ctx, get_number(val), &dispex);
812 if(FAILED(hres))
813 return hres;
815 *disp = to_disp(dispex);
816 break;
817 case JSV_OBJECT:
818 if(get_object(val)) {
819 *disp = get_object(val);
820 IDispatch_AddRef(*disp);
821 }else {
822 jsdisp_t *obj;
824 hres = create_object(ctx, NULL, &obj);
825 if(FAILED(hres))
826 return hres;
828 *disp = to_disp(obj);
830 break;
831 case JSV_BOOL:
832 hres = create_bool(ctx, get_bool(val), &dispex);
833 if(FAILED(hres))
834 return hres;
836 *disp = to_disp(dispex);
837 break;
838 case JSV_UNDEFINED:
839 case JSV_NULL:
840 WARN("object expected\n");
841 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
842 case JSV_VARIANT:
843 switch(V_VT(get_variant(val))) {
844 case VT_ARRAY|VT_VARIANT:
845 hres = create_vbarray(ctx, V_ARRAY(get_variant(val)), &dispex);
846 if(FAILED(hres))
847 return hres;
849 *disp = to_disp(dispex);
850 break;
852 default:
853 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val)));
854 return E_NOTIMPL;
856 break;
859 return S_OK;
862 HRESULT variant_change_type(script_ctx_t *ctx, VARIANT *dst, VARIANT *src, VARTYPE vt)
864 jsval_t val;
865 HRESULT hres;
867 clear_ei(ctx);
868 hres = variant_to_jsval(src, &val);
869 if(FAILED(hres))
870 return hres;
872 switch(vt) {
873 case VT_I2:
874 case VT_I4: {
875 INT i;
877 hres = to_int32(ctx, val, &i);
878 if(SUCCEEDED(hres)) {
879 if(vt == VT_I4)
880 V_I4(dst) = i;
881 else
882 V_I2(dst) = i;
884 break;
886 case VT_R8: {
887 double n;
888 hres = to_number(ctx, val, &n);
889 if(SUCCEEDED(hres))
890 V_R8(dst) = n;
891 break;
893 case VT_R4: {
894 double n;
896 hres = to_number(ctx, val, &n);
897 if(SUCCEEDED(hres))
898 V_R4(dst) = n;
899 break;
901 case VT_BOOL: {
902 BOOL b;
904 hres = to_boolean(val, &b);
905 if(SUCCEEDED(hres))
906 V_BOOL(dst) = b ? VARIANT_TRUE : VARIANT_FALSE;
907 break;
909 case VT_BSTR: {
910 jsstr_t *str;
912 hres = to_string(ctx, val, &str);
913 if(FAILED(hres))
914 break;
916 if(str->length_flags & JSSTR_FLAG_NULLBSTR) {
917 V_BSTR(dst) = NULL;
918 break;
921 V_BSTR(dst) = SysAllocStringLen(NULL, jsstr_length(str));
922 if(V_BSTR(dst))
923 jsstr_flush(str, V_BSTR(dst));
924 else
925 hres = E_OUTOFMEMORY;
926 break;
928 case VT_EMPTY:
929 hres = V_VT(src) == VT_EMPTY ? S_OK : E_NOTIMPL;
930 break;
931 case VT_NULL:
932 hres = V_VT(src) == VT_NULL ? S_OK : E_NOTIMPL;
933 break;
934 default:
935 FIXME("vt %d not implemented\n", vt);
936 hres = E_NOTIMPL;
939 jsval_release(val);
940 if(FAILED(hres))
941 return hres;
943 V_VT(dst) = vt;
944 return S_OK;
947 static inline JSCaller *impl_from_IServiceProvider(IServiceProvider *iface)
949 return CONTAINING_RECORD(iface, JSCaller, IServiceProvider_iface);
952 static HRESULT WINAPI JSCaller_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
954 JSCaller *This = impl_from_IServiceProvider(iface);
956 if(IsEqualGUID(&IID_IUnknown, riid)) {
957 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
958 *ppv = &This->IServiceProvider_iface;
959 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
960 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
961 *ppv = &This->IServiceProvider_iface;
962 }else {
963 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
964 *ppv = NULL;
965 return E_NOINTERFACE;
968 IUnknown_AddRef((IUnknown*)*ppv);
969 return S_OK;
972 static ULONG WINAPI JSCaller_AddRef(IServiceProvider *iface)
974 JSCaller *This = impl_from_IServiceProvider(iface);
975 LONG ref = InterlockedIncrement(&This->ref);
977 TRACE("(%p) ref=%d\n", This, ref);
979 return ref;
982 static ULONG WINAPI JSCaller_Release(IServiceProvider *iface)
984 JSCaller *This = impl_from_IServiceProvider(iface);
985 LONG ref = InterlockedIncrement(&This->ref);
987 TRACE("(%p) ref=%d\n", This, ref);
989 if(!ref) {
990 assert(!This->ctx);
991 heap_free(This);
994 return ref;
997 static HRESULT WINAPI JSCaller_QueryService(IServiceProvider *iface, REFGUID guidService,
998 REFIID riid, void **ppv)
1000 JSCaller *This = impl_from_IServiceProvider(iface);
1002 if(IsEqualGUID(guidService, &SID_VariantConversion) && This->ctx && This->ctx->active_script) {
1003 TRACE("(%p)->(SID_VariantConversion)\n", This);
1004 return IActiveScript_QueryInterface(This->ctx->active_script, riid, ppv);
1007 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1009 *ppv = NULL;
1010 return E_NOINTERFACE;
1013 static const IServiceProviderVtbl ServiceProviderVtbl = {
1014 JSCaller_QueryInterface,
1015 JSCaller_AddRef,
1016 JSCaller_Release,
1017 JSCaller_QueryService
1020 HRESULT create_jscaller(script_ctx_t *ctx)
1022 JSCaller *ret;
1024 ret = heap_alloc(sizeof(*ret));
1025 if(!ret)
1026 return E_OUTOFMEMORY;
1028 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
1029 ret->ref = 1;
1030 ret->ctx = ctx;
1032 ctx->jscaller = ret;
1033 return S_OK;