msvcp110: Add tr2_sys__File_size_wchar implementation and test.
[wine/multimedia.git] / dlls / jscript / jsutils.c
blob85f573fd44b9f3e3900834ec209619e4294a7473
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_jsval(const jsval_t v)
35 switch(jsval_type(v)) {
36 case JSV_UNDEFINED:
37 return "undefined";
38 case JSV_NULL:
39 return "null";
40 case JSV_OBJECT:
41 return wine_dbg_sprintf("obj(%p)", get_object(v));
42 case JSV_STRING:
43 return wine_dbg_sprintf("str(%s)", debugstr_jsstr(get_string(v)));
44 case JSV_NUMBER:
45 return wine_dbg_sprintf("%lf", get_number(v));
46 case JSV_BOOL:
47 return get_bool(v) ? "true" : "false";
48 case JSV_VARIANT:
49 return debugstr_variant(get_variant(v));
52 assert(0);
53 return NULL;
56 #define MIN_BLOCK_SIZE 128
57 #define ARENA_FREE_FILLER 0xaa
59 static inline DWORD block_size(DWORD block)
61 return MIN_BLOCK_SIZE << block;
64 void heap_pool_init(heap_pool_t *heap)
66 memset(heap, 0, sizeof(*heap));
67 list_init(&heap->custom_blocks);
70 void *heap_pool_alloc(heap_pool_t *heap, DWORD size)
72 struct list *list;
73 void *tmp;
75 if(!heap->block_cnt) {
76 if(!heap->blocks) {
77 heap->blocks = heap_alloc(sizeof(void*));
78 if(!heap->blocks)
79 return NULL;
82 tmp = heap_alloc(block_size(0));
83 if(!tmp)
84 return NULL;
86 heap->blocks[0] = tmp;
87 heap->block_cnt = 1;
90 if(heap->offset + size <= block_size(heap->last_block)) {
91 tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
92 heap->offset += size;
93 return tmp;
96 if(size <= block_size(heap->last_block+1)) {
97 if(heap->last_block+1 == heap->block_cnt) {
98 tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
99 if(!tmp)
100 return NULL;
102 heap->blocks = tmp;
103 heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
104 if(!heap->blocks[heap->block_cnt])
105 return NULL;
107 heap->block_cnt++;
110 heap->last_block++;
111 heap->offset = size;
112 return heap->blocks[heap->last_block];
115 list = heap_alloc(size + sizeof(struct list));
116 if(!list)
117 return NULL;
119 list_add_head(&heap->custom_blocks, list);
120 return list+1;
123 void *heap_pool_grow(heap_pool_t *heap, void *mem, DWORD size, DWORD inc)
125 void *ret;
127 if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
128 && heap->offset+inc < block_size(heap->last_block)) {
129 heap->offset += inc;
130 return mem;
133 ret = heap_pool_alloc(heap, size+inc);
134 if(ret) /* FIXME: avoid copying for custom blocks */
135 memcpy(ret, mem, size);
136 return ret;
139 void heap_pool_clear(heap_pool_t *heap)
141 struct list *tmp;
143 if(!heap)
144 return;
146 while((tmp = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
147 list_remove(tmp);
148 heap_free(tmp);
151 if(WARN_ON(heap)) {
152 DWORD i;
154 for(i=0; i < heap->block_cnt; i++)
155 memset(heap->blocks[i], ARENA_FREE_FILLER, block_size(i));
158 heap->last_block = heap->offset = 0;
159 heap->mark = FALSE;
162 void heap_pool_free(heap_pool_t *heap)
164 DWORD i;
166 heap_pool_clear(heap);
168 for(i=0; i < heap->block_cnt; i++)
169 heap_free(heap->blocks[i]);
170 heap_free(heap->blocks);
172 heap_pool_init(heap);
175 heap_pool_t *heap_pool_mark(heap_pool_t *heap)
177 if(heap->mark)
178 return NULL;
180 heap->mark = TRUE;
181 return heap;
184 void jsval_release(jsval_t val)
186 switch(jsval_type(val)) {
187 case JSV_OBJECT:
188 if(get_object(val))
189 IDispatch_Release(get_object(val));
190 break;
191 case JSV_STRING:
192 jsstr_release(get_string(val));
193 break;
194 case JSV_VARIANT:
195 VariantClear(get_variant(val));
196 heap_free(get_variant(val));
197 break;
198 default:
199 break;
203 static HRESULT jsval_variant(jsval_t *val, VARIANT *var)
205 VARIANT *v;
206 HRESULT hres;
208 __JSVAL_TYPE(*val) = JSV_VARIANT;
209 __JSVAL_VAR(*val) = v = heap_alloc(sizeof(VARIANT));
210 if(!v)
211 return E_OUTOFMEMORY;
213 V_VT(v) = VT_EMPTY;
214 hres = VariantCopy(v, var);
215 if(FAILED(hres))
216 heap_free(v);
217 return hres;
220 HRESULT jsval_copy(jsval_t v, jsval_t *r)
222 switch(jsval_type(v)) {
223 case JSV_UNDEFINED:
224 case JSV_NULL:
225 case JSV_NUMBER:
226 case JSV_BOOL:
227 *r = v;
228 return S_OK;
229 case JSV_OBJECT:
230 if(get_object(v))
231 IDispatch_AddRef(get_object(v));
232 *r = v;
233 return S_OK;
234 case JSV_STRING: {
235 jsstr_addref(get_string(v));
236 *r = v;
237 return S_OK;
239 case JSV_VARIANT:
240 return jsval_variant(r, get_variant(v));
243 assert(0);
244 return E_FAIL;
247 HRESULT variant_to_jsval(VARIANT *var, jsval_t *r)
249 if(V_VT(var) == (VT_VARIANT|VT_BYREF))
250 var = V_VARIANTREF(var);
252 switch(V_VT(var)) {
253 case VT_EMPTY:
254 *r = jsval_undefined();
255 return S_OK;
256 case VT_NULL:
257 *r = jsval_null();
258 return S_OK;
259 case VT_BOOL:
260 *r = jsval_bool(V_BOOL(var));
261 return S_OK;
262 case VT_I4:
263 *r = jsval_number(V_I4(var));
264 return S_OK;
265 case VT_R8:
266 *r = jsval_number(V_R8(var));
267 return S_OK;
268 case VT_BSTR: {
269 jsstr_t *str;
271 if(V_BSTR(var)) {
272 str = jsstr_alloc_len(V_BSTR(var), SysStringLen(V_BSTR(var)));
273 if(!str)
274 return E_OUTOFMEMORY;
275 }else {
276 str = jsstr_null_bstr();
279 *r = jsval_string(str);
280 return S_OK;
282 case VT_DISPATCH: {
283 if(V_DISPATCH(var))
284 IDispatch_AddRef(V_DISPATCH(var));
285 *r = jsval_disp(V_DISPATCH(var));
286 return S_OK;
288 case VT_I2:
289 *r = jsval_number(V_I2(var));
290 return S_OK;
291 case VT_INT:
292 *r = jsval_number(V_INT(var));
293 return S_OK;
294 case VT_UI4:
295 *r = jsval_number(V_UI4(var));
296 return S_OK;
297 case VT_UNKNOWN:
298 if(V_UNKNOWN(var)) {
299 IDispatch *disp;
300 HRESULT hres;
302 hres = IUnknown_QueryInterface(V_UNKNOWN(var), &IID_IDispatch, (void**)&disp);
303 if(SUCCEEDED(hres)) {
304 *r = jsval_disp(disp);
305 return S_OK;
307 }else {
308 *r = jsval_disp(NULL);
309 return S_OK;
311 /* fall through */
312 default:
313 return jsval_variant(r, var);
317 HRESULT jsval_to_variant(jsval_t val, VARIANT *retv)
319 switch(jsval_type(val)) {
320 case JSV_UNDEFINED:
321 V_VT(retv) = VT_EMPTY;
322 return S_OK;
323 case JSV_NULL:
324 V_VT(retv) = VT_NULL;
325 return S_OK;
326 case JSV_OBJECT:
327 V_VT(retv) = VT_DISPATCH;
328 if(get_object(val))
329 IDispatch_AddRef(get_object(val));
330 V_DISPATCH(retv) = get_object(val);
331 return S_OK;
332 case JSV_STRING: {
333 jsstr_t *str = get_string(val);
335 V_VT(retv) = VT_BSTR;
336 if(is_null_bstr(str)) {
337 V_BSTR(retv) = NULL;
338 }else {
339 V_BSTR(retv) = SysAllocStringLen(NULL, jsstr_length(str));
340 if(V_BSTR(retv))
341 jsstr_flush(str, V_BSTR(retv));
342 else
343 return E_OUTOFMEMORY;
345 return S_OK;
347 case JSV_NUMBER: {
348 double n = get_number(val);
350 if(is_int32(n)) {
351 V_VT(retv) = VT_I4;
352 V_I4(retv) = n;
353 }else {
354 V_VT(retv) = VT_R8;
355 V_R8(retv) = n;
358 return S_OK;
360 case JSV_BOOL:
361 V_VT(retv) = VT_BOOL;
362 V_BOOL(retv) = get_bool(val) ? VARIANT_TRUE : VARIANT_FALSE;
363 return S_OK;
364 case JSV_VARIANT:
365 V_VT(retv) = VT_EMPTY;
366 return VariantCopy(retv, get_variant(val));
369 assert(0);
370 return E_FAIL;
373 /* ECMA-262 3rd Edition 9.1 */
374 HRESULT to_primitive(script_ctx_t *ctx, jsval_t val, jsval_t *ret, hint_t hint)
376 if(is_object_instance(val)) {
377 jsdisp_t *jsdisp;
378 jsval_t prim;
379 DISPID id;
380 HRESULT hres;
382 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
383 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
385 if(!get_object(val)) {
386 *ret = jsval_null();
387 return S_OK;
390 jsdisp = iface_to_jsdisp((IUnknown*)get_object(val));
391 if(!jsdisp)
392 return disp_propget(ctx, get_object(val), DISPID_VALUE, ret);
394 if(hint == NO_HINT)
395 hint = is_class(jsdisp, JSCLASS_DATE) ? HINT_STRING : HINT_NUMBER;
397 /* Native implementation doesn't throw TypeErrors, returns strange values */
399 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? toStringW : valueOfW, 0, &id);
400 if(SUCCEEDED(hres)) {
401 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
402 if(FAILED(hres)) {
403 WARN("call error - forwarding exception\n");
404 jsdisp_release(jsdisp);
405 return hres;
406 }else if(!is_object_instance(prim)) {
407 jsdisp_release(jsdisp);
408 *ret = prim;
409 return S_OK;
410 }else {
411 IDispatch_Release(get_object(prim));
415 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? valueOfW : toStringW, 0, &id);
416 if(SUCCEEDED(hres)) {
417 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
418 if(FAILED(hres)) {
419 WARN("call error - forwarding exception\n");
420 jsdisp_release(jsdisp);
421 return hres;
422 }else if(!is_object_instance(prim)) {
423 jsdisp_release(jsdisp);
424 *ret = prim;
425 return S_OK;
426 }else {
427 IDispatch_Release(get_object(prim));
431 jsdisp_release(jsdisp);
433 WARN("failed\n");
434 return throw_type_error(ctx, JS_E_TO_PRIMITIVE, NULL);
437 return jsval_copy(val, ret);
441 /* ECMA-262 3rd Edition 9.2 */
442 HRESULT to_boolean(jsval_t val, BOOL *ret)
444 switch(jsval_type(val)) {
445 case JSV_UNDEFINED:
446 case JSV_NULL:
447 *ret = FALSE;
448 return S_OK;
449 case JSV_OBJECT:
450 *ret = get_object(val) != NULL;
451 return S_OK;
452 case JSV_STRING:
453 *ret = jsstr_length(get_string(val)) != 0;
454 return S_OK;
455 case JSV_NUMBER:
456 *ret = !isnan(get_number(val)) && get_number(val);
457 return S_OK;
458 case JSV_BOOL:
459 *ret = get_bool(val);
460 return S_OK;
461 case JSV_VARIANT:
462 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
463 return E_NOTIMPL;
466 assert(0);
467 return E_FAIL;
470 static int hex_to_int(WCHAR c)
472 if('0' <= c && c <= '9')
473 return c-'0';
475 if('a' <= c && c <= 'f')
476 return c-'a'+10;
478 if('A' <= c && c <= 'F')
479 return c-'A'+10;
481 return -1;
484 /* ECMA-262 3rd Edition 9.3.1 */
485 static HRESULT str_to_number(jsstr_t *str, double *ret)
487 const WCHAR *ptr;
488 BOOL neg = FALSE;
489 DOUBLE d = 0.0;
491 static const WCHAR infinityW[] = {'I','n','f','i','n','i','t','y'};
493 ptr = jsstr_flatten(str);
494 if(!ptr)
495 return E_OUTOFMEMORY;
497 while(isspaceW(*ptr))
498 ptr++;
500 if(*ptr == '-') {
501 neg = TRUE;
502 ptr++;
503 }else if(*ptr == '+') {
504 ptr++;
507 if(!strncmpW(ptr, infinityW, sizeof(infinityW)/sizeof(WCHAR))) {
508 ptr += sizeof(infinityW)/sizeof(WCHAR);
509 while(*ptr && isspaceW(*ptr))
510 ptr++;
512 if(*ptr)
513 *ret = NAN;
514 else
515 *ret = neg ? -INFINITY : INFINITY;
516 return S_OK;
519 if(*ptr == '0' && ptr[1] == 'x') {
520 DWORD l = 0;
522 ptr += 2;
523 while((l = hex_to_int(*ptr)) != -1) {
524 d = d*16 + l;
525 ptr++;
528 *ret = d;
529 return S_OK;
532 while(isdigitW(*ptr))
533 d = d*10 + (*ptr++ - '0');
535 if(*ptr == 'e' || *ptr == 'E') {
536 BOOL eneg = FALSE;
537 LONG l = 0;
539 ptr++;
540 if(*ptr == '-') {
541 ptr++;
542 eneg = TRUE;
543 }else if(*ptr == '+') {
544 ptr++;
547 while(isdigitW(*ptr))
548 l = l*10 + (*ptr++ - '0');
549 if(eneg)
550 l = -l;
552 d *= pow(10, l);
553 }else if(*ptr == '.') {
554 DOUBLE dec = 0.1;
556 ptr++;
557 while(isdigitW(*ptr)) {
558 d += dec * (*ptr++ - '0');
559 dec *= 0.1;
563 while(isspaceW(*ptr))
564 ptr++;
566 if(*ptr) {
567 *ret = NAN;
568 return S_OK;
571 if(neg)
572 d = -d;
574 *ret = d;
575 return S_OK;
578 /* ECMA-262 3rd Edition 9.3 */
579 HRESULT to_number(script_ctx_t *ctx, jsval_t val, double *ret)
581 switch(jsval_type(val)) {
582 case JSV_UNDEFINED:
583 *ret = NAN;
584 return S_OK;
585 case JSV_NULL:
586 *ret = 0;
587 return S_OK;
588 case JSV_NUMBER:
589 *ret = get_number(val);
590 return S_OK;
591 case JSV_STRING:
592 return str_to_number(get_string(val), ret);
593 case JSV_OBJECT: {
594 jsval_t prim;
595 HRESULT hres;
597 hres = to_primitive(ctx, val, &prim, HINT_NUMBER);
598 if(FAILED(hres))
599 return hres;
601 hres = to_number(ctx, prim, ret);
602 jsval_release(prim);
603 return hres;
605 case JSV_BOOL:
606 *ret = get_bool(val) ? 1 : 0;
607 return S_OK;
608 case JSV_VARIANT:
609 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
610 return E_NOTIMPL;
613 assert(0);
614 return E_FAIL;
617 /* ECMA-262 3rd Edition 9.4 */
618 HRESULT to_integer(script_ctx_t *ctx, jsval_t v, double *ret)
620 double n;
621 HRESULT hres;
623 hres = to_number(ctx, v, &n);
624 if(FAILED(hres))
625 return hres;
627 if(isnan(n))
628 *ret = 0;
629 else
630 *ret = n >= 0.0 ? floor(n) : -floor(-n);
631 return S_OK;
634 /* ECMA-262 3rd Edition 9.5 */
635 HRESULT to_int32(script_ctx_t *ctx, jsval_t v, INT *ret)
637 double n;
638 HRESULT hres;
640 hres = to_number(ctx, v, &n);
641 if(FAILED(hres))
642 return hres;
644 *ret = isnan(n) || isinf(n) ? 0 : n;
645 return S_OK;
648 /* ECMA-262 3rd Edition 9.6 */
649 HRESULT to_uint32(script_ctx_t *ctx, jsval_t val, DWORD *ret)
651 INT32 n;
652 HRESULT hres;
654 hres = to_int32(ctx, val, &n);
655 if(SUCCEEDED(hres))
656 *ret = n;
657 return hres;
660 static jsstr_t *int_to_string(int i)
662 WCHAR buf[12], *p;
663 BOOL neg = FALSE;
665 if(!i) {
666 static const WCHAR zeroW[] = {'0',0};
667 return jsstr_alloc(zeroW);
670 if(i < 0) {
671 neg = TRUE;
672 i = -i;
675 p = buf + sizeof(buf)/sizeof(*buf)-1;
676 *p-- = 0;
677 while(i) {
678 *p-- = i%10 + '0';
679 i /= 10;
682 if(neg)
683 *p = '-';
684 else
685 p++;
687 return jsstr_alloc(p);
690 HRESULT double_to_string(double n, jsstr_t **str)
692 const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};
694 if(isnan(n)) {
695 *str = jsstr_nan();
696 }else if(isinf(n)) {
697 *str = jsstr_alloc(n<0 ? InfinityW : InfinityW+1);
698 }else if(is_int32(n)) {
699 *str = int_to_string(n);
700 }else {
701 VARIANT strv, v;
702 HRESULT hres;
704 /* FIXME: Don't use VariantChangeTypeEx */
705 V_VT(&v) = VT_R8;
706 V_R8(&v) = n;
707 V_VT(&strv) = VT_EMPTY;
708 hres = VariantChangeTypeEx(&strv, &v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
709 if(FAILED(hres))
710 return hres;
712 *str = jsstr_alloc(V_BSTR(&strv));
713 SysFreeString(V_BSTR(&strv));
716 return *str ? S_OK : E_OUTOFMEMORY;
719 /* ECMA-262 3rd Edition 9.8 */
720 HRESULT to_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str)
722 const WCHAR nullW[] = {'n','u','l','l',0};
723 const WCHAR trueW[] = {'t','r','u','e',0};
724 const WCHAR falseW[] = {'f','a','l','s','e',0};
726 switch(jsval_type(val)) {
727 case JSV_UNDEFINED:
728 *str = jsstr_undefined();
729 return S_OK;
730 case JSV_NULL:
731 *str = jsstr_alloc(nullW);
732 break;
733 case JSV_NUMBER:
734 return double_to_string(get_number(val), str);
735 case JSV_STRING:
736 *str = jsstr_addref(get_string(val));
737 break;
738 case JSV_OBJECT: {
739 jsval_t prim;
740 HRESULT hres;
742 hres = to_primitive(ctx, val, &prim, HINT_STRING);
743 if(FAILED(hres))
744 return hres;
746 hres = to_string(ctx, prim, str);
747 jsval_release(prim);
748 return hres;
750 case JSV_BOOL:
751 *str = jsstr_alloc(get_bool(val) ? trueW : falseW);
752 break;
753 default:
754 FIXME("unsupported %s\n", debugstr_jsval(val));
755 return E_NOTIMPL;
758 return *str ? S_OK : E_OUTOFMEMORY;
761 HRESULT to_flat_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str, const WCHAR **ret_str)
763 HRESULT hres;
765 hres = to_string(ctx, val, str);
766 if(FAILED(hres))
767 return hres;
769 *ret_str = jsstr_flatten(*str);
770 if(!*ret_str) {
771 jsstr_release(*str);
772 return E_OUTOFMEMORY;
775 return S_OK;
778 /* ECMA-262 3rd Edition 9.9 */
779 HRESULT to_object(script_ctx_t *ctx, jsval_t val, IDispatch **disp)
781 jsdisp_t *dispex;
782 HRESULT hres;
784 switch(jsval_type(val)) {
785 case JSV_STRING:
786 hres = create_string(ctx, get_string(val), &dispex);
787 if(FAILED(hres))
788 return hres;
790 *disp = to_disp(dispex);
791 break;
792 case JSV_NUMBER:
793 hres = create_number(ctx, get_number(val), &dispex);
794 if(FAILED(hres))
795 return hres;
797 *disp = to_disp(dispex);
798 break;
799 case JSV_OBJECT:
800 if(get_object(val)) {
801 *disp = get_object(val);
802 IDispatch_AddRef(*disp);
803 }else {
804 jsdisp_t *obj;
806 hres = create_object(ctx, NULL, &obj);
807 if(FAILED(hres))
808 return hres;
810 *disp = to_disp(obj);
812 break;
813 case JSV_BOOL:
814 hres = create_bool(ctx, get_bool(val), &dispex);
815 if(FAILED(hres))
816 return hres;
818 *disp = to_disp(dispex);
819 break;
820 case JSV_UNDEFINED:
821 case JSV_NULL:
822 WARN("object expected\n");
823 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
824 case JSV_VARIANT:
825 switch(V_VT(get_variant(val))) {
826 case VT_ARRAY|VT_VARIANT:
827 hres = create_vbarray(ctx, V_ARRAY(get_variant(val)), &dispex);
828 if(FAILED(hres))
829 return hres;
831 *disp = to_disp(dispex);
832 break;
834 default:
835 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val)));
836 return E_NOTIMPL;
838 break;
841 return S_OK;
844 HRESULT variant_change_type(script_ctx_t *ctx, VARIANT *dst, VARIANT *src, VARTYPE vt)
846 jsval_t val;
847 HRESULT hres;
849 clear_ei(ctx);
850 hres = variant_to_jsval(src, &val);
851 if(FAILED(hres))
852 return hres;
854 switch(vt) {
855 case VT_I2:
856 case VT_I4: {
857 INT i;
859 hres = to_int32(ctx, val, &i);
860 if(SUCCEEDED(hres)) {
861 if(vt == VT_I4)
862 V_I4(dst) = i;
863 else
864 V_I2(dst) = i;
866 break;
868 case VT_R8: {
869 double n;
870 hres = to_number(ctx, val, &n);
871 if(SUCCEEDED(hres))
872 V_R8(dst) = n;
873 break;
875 case VT_R4: {
876 double n;
878 hres = to_number(ctx, val, &n);
879 if(SUCCEEDED(hres))
880 V_R4(dst) = n;
881 break;
883 case VT_BOOL: {
884 BOOL b;
886 hres = to_boolean(val, &b);
887 if(SUCCEEDED(hres))
888 V_BOOL(dst) = b ? VARIANT_TRUE : VARIANT_FALSE;
889 break;
891 case VT_BSTR: {
892 jsstr_t *str;
894 hres = to_string(ctx, val, &str);
895 if(FAILED(hres))
896 break;
898 if(is_null_bstr(str)) {
899 V_BSTR(dst) = NULL;
900 break;
903 V_BSTR(dst) = SysAllocStringLen(NULL, jsstr_length(str));
904 if(V_BSTR(dst))
905 jsstr_flush(str, V_BSTR(dst));
906 else
907 hres = E_OUTOFMEMORY;
908 break;
910 case VT_EMPTY:
911 hres = V_VT(src) == VT_EMPTY ? S_OK : E_NOTIMPL;
912 break;
913 case VT_NULL:
914 hres = V_VT(src) == VT_NULL ? S_OK : E_NOTIMPL;
915 break;
916 default:
917 FIXME("vt %d not implemented\n", vt);
918 hres = E_NOTIMPL;
921 jsval_release(val);
922 if(FAILED(hres))
923 return hres;
925 V_VT(dst) = vt;
926 return S_OK;
929 static inline JSCaller *impl_from_IServiceProvider(IServiceProvider *iface)
931 return CONTAINING_RECORD(iface, JSCaller, IServiceProvider_iface);
934 static HRESULT WINAPI JSCaller_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
936 JSCaller *This = impl_from_IServiceProvider(iface);
938 if(IsEqualGUID(&IID_IUnknown, riid)) {
939 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
940 *ppv = &This->IServiceProvider_iface;
941 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
942 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
943 *ppv = &This->IServiceProvider_iface;
944 }else {
945 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
946 *ppv = NULL;
947 return E_NOINTERFACE;
950 IUnknown_AddRef((IUnknown*)*ppv);
951 return S_OK;
954 static ULONG WINAPI JSCaller_AddRef(IServiceProvider *iface)
956 JSCaller *This = impl_from_IServiceProvider(iface);
957 LONG ref = InterlockedIncrement(&This->ref);
959 TRACE("(%p) ref=%d\n", This, ref);
961 return ref;
964 static ULONG WINAPI JSCaller_Release(IServiceProvider *iface)
966 JSCaller *This = impl_from_IServiceProvider(iface);
967 LONG ref = InterlockedIncrement(&This->ref);
969 TRACE("(%p) ref=%d\n", This, ref);
971 if(!ref) {
972 assert(!This->ctx);
973 heap_free(This);
976 return ref;
979 static HRESULT WINAPI JSCaller_QueryService(IServiceProvider *iface, REFGUID guidService,
980 REFIID riid, void **ppv)
982 JSCaller *This = impl_from_IServiceProvider(iface);
984 if(IsEqualGUID(guidService, &SID_VariantConversion) && This->ctx && This->ctx->active_script) {
985 TRACE("(%p)->(SID_VariantConversion)\n", This);
986 return IActiveScript_QueryInterface(This->ctx->active_script, riid, ppv);
989 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
991 *ppv = NULL;
992 return E_NOINTERFACE;
995 static const IServiceProviderVtbl ServiceProviderVtbl = {
996 JSCaller_QueryInterface,
997 JSCaller_AddRef,
998 JSCaller_Release,
999 JSCaller_QueryService
1002 HRESULT create_jscaller(script_ctx_t *ctx)
1004 JSCaller *ret;
1006 ret = heap_alloc(sizeof(*ret));
1007 if(!ret)
1008 return E_OUTOFMEMORY;
1010 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
1011 ret->ref = 1;
1012 ret->ctx = ctx;
1014 ctx->jscaller = ret;
1015 return S_OK;