shlwapi: Remove unnecessary internal function SHLWAPI_DupSharedHandle.
[wine/wine-gecko.git] / dlls / jscript / jsutils.c
bloba44168da1f8536c4c3c1046236bfbfd3046e9393
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 switch(V_VT(var)) {
250 case VT_EMPTY:
251 *r = jsval_undefined();
252 return S_OK;
253 case VT_NULL:
254 *r = jsval_null();
255 return S_OK;
256 case VT_BOOL:
257 *r = jsval_bool(V_BOOL(var));
258 return S_OK;
259 case VT_I4:
260 *r = jsval_number(V_I4(var));
261 return S_OK;
262 case VT_R8:
263 *r = jsval_number(V_R8(var));
264 return S_OK;
265 case VT_BSTR: {
266 jsstr_t *str;
268 if(V_BSTR(var)) {
269 str = jsstr_alloc_len(V_BSTR(var), SysStringLen(V_BSTR(var)));
270 if(!str)
271 return E_OUTOFMEMORY;
272 }else {
273 str = jsstr_null_bstr();
276 *r = jsval_string(str);
277 return S_OK;
279 case VT_DISPATCH: {
280 if(V_DISPATCH(var))
281 IDispatch_AddRef(V_DISPATCH(var));
282 *r = jsval_disp(V_DISPATCH(var));
283 return S_OK;
285 case VT_I2:
286 *r = jsval_number(V_I2(var));
287 return S_OK;
288 case VT_INT:
289 *r = jsval_number(V_INT(var));
290 return S_OK;
291 case VT_UNKNOWN:
292 if(V_UNKNOWN(var)) {
293 IDispatch *disp;
294 HRESULT hres;
296 hres = IUnknown_QueryInterface(V_UNKNOWN(var), &IID_IDispatch, (void**)&disp);
297 if(SUCCEEDED(hres)) {
298 *r = jsval_disp(disp);
299 return S_OK;
301 }else {
302 *r = jsval_disp(NULL);
303 return S_OK;
305 /* fall through */
306 default:
307 return jsval_variant(r, var);
311 HRESULT jsval_to_variant(jsval_t val, VARIANT *retv)
313 switch(jsval_type(val)) {
314 case JSV_UNDEFINED:
315 V_VT(retv) = VT_EMPTY;
316 return S_OK;
317 case JSV_NULL:
318 V_VT(retv) = VT_NULL;
319 return S_OK;
320 case JSV_OBJECT:
321 V_VT(retv) = VT_DISPATCH;
322 if(get_object(val))
323 IDispatch_AddRef(get_object(val));
324 V_DISPATCH(retv) = get_object(val);
325 return S_OK;
326 case JSV_STRING: {
327 jsstr_t *str = get_string(val);
329 V_VT(retv) = VT_BSTR;
330 if(is_null_bstr(str)) {
331 V_BSTR(retv) = NULL;
332 }else {
333 V_BSTR(retv) = SysAllocStringLen(NULL, jsstr_length(str));
334 if(V_BSTR(retv))
335 jsstr_flush(str, V_BSTR(retv));
336 else
337 return E_OUTOFMEMORY;
339 return S_OK;
341 case JSV_NUMBER: {
342 double n = get_number(val);
344 if(is_int32(n)) {
345 V_VT(retv) = VT_I4;
346 V_I4(retv) = n;
347 }else {
348 V_VT(retv) = VT_R8;
349 V_R8(retv) = n;
352 return S_OK;
354 case JSV_BOOL:
355 V_VT(retv) = VT_BOOL;
356 V_BOOL(retv) = get_bool(val) ? VARIANT_TRUE : VARIANT_FALSE;
357 return S_OK;
358 case JSV_VARIANT:
359 V_VT(retv) = VT_EMPTY;
360 return VariantCopy(retv, get_variant(val));
363 assert(0);
364 return E_FAIL;
367 /* ECMA-262 3rd Edition 9.1 */
368 HRESULT to_primitive(script_ctx_t *ctx, jsval_t val, jsval_t *ret, hint_t hint)
370 if(is_object_instance(val)) {
371 jsdisp_t *jsdisp;
372 jsval_t prim;
373 DISPID id;
374 HRESULT hres;
376 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
377 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
379 if(!get_object(val)) {
380 *ret = jsval_null();
381 return S_OK;
384 jsdisp = iface_to_jsdisp((IUnknown*)get_object(val));
385 if(!jsdisp)
386 return disp_propget(ctx, get_object(val), DISPID_VALUE, ret);
388 if(hint == NO_HINT)
389 hint = is_class(jsdisp, JSCLASS_DATE) ? HINT_STRING : HINT_NUMBER;
391 /* Native implementation doesn't throw TypeErrors, returns strange values */
393 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? toStringW : valueOfW, 0, &id);
394 if(SUCCEEDED(hres)) {
395 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
396 if(FAILED(hres)) {
397 WARN("call error - forwarding exception\n");
398 jsdisp_release(jsdisp);
399 return hres;
400 }else if(!is_object_instance(prim)) {
401 jsdisp_release(jsdisp);
402 *ret = prim;
403 return S_OK;
404 }else {
405 IDispatch_Release(get_object(prim));
409 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? valueOfW : toStringW, 0, &id);
410 if(SUCCEEDED(hres)) {
411 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
412 if(FAILED(hres)) {
413 WARN("call error - forwarding exception\n");
414 jsdisp_release(jsdisp);
415 return hres;
416 }else if(!is_object_instance(prim)) {
417 jsdisp_release(jsdisp);
418 *ret = prim;
419 return S_OK;
420 }else {
421 IDispatch_Release(get_object(prim));
425 jsdisp_release(jsdisp);
427 WARN("failed\n");
428 return throw_type_error(ctx, JS_E_TO_PRIMITIVE, NULL);
431 return jsval_copy(val, ret);
435 /* ECMA-262 3rd Edition 9.2 */
436 HRESULT to_boolean(jsval_t val, BOOL *ret)
438 switch(jsval_type(val)) {
439 case JSV_UNDEFINED:
440 case JSV_NULL:
441 *ret = FALSE;
442 return S_OK;
443 case JSV_OBJECT:
444 *ret = get_object(val) != NULL;
445 return S_OK;
446 case JSV_STRING:
447 *ret = jsstr_length(get_string(val)) != 0;
448 return S_OK;
449 case JSV_NUMBER:
450 *ret = !isnan(get_number(val)) && get_number(val);
451 return S_OK;
452 case JSV_BOOL:
453 *ret = get_bool(val);
454 return S_OK;
455 case JSV_VARIANT:
456 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
457 return E_NOTIMPL;
460 assert(0);
461 return E_FAIL;
464 static int hex_to_int(WCHAR c)
466 if('0' <= c && c <= '9')
467 return c-'0';
469 if('a' <= c && c <= 'f')
470 return c-'a'+10;
472 if('A' <= c && c <= 'F')
473 return c-'A'+10;
475 return -1;
478 /* ECMA-262 3rd Edition 9.3.1 */
479 static HRESULT str_to_number(jsstr_t *str, double *ret)
481 const WCHAR *ptr;
482 BOOL neg = FALSE;
483 DOUBLE d = 0.0;
485 static const WCHAR infinityW[] = {'I','n','f','i','n','i','t','y'};
487 ptr = jsstr_flatten(str);
488 if(!ptr)
489 return E_OUTOFMEMORY;
491 while(isspaceW(*ptr))
492 ptr++;
494 if(*ptr == '-') {
495 neg = TRUE;
496 ptr++;
497 }else if(*ptr == '+') {
498 ptr++;
501 if(!strncmpW(ptr, infinityW, sizeof(infinityW)/sizeof(WCHAR))) {
502 ptr += sizeof(infinityW)/sizeof(WCHAR);
503 while(*ptr && isspaceW(*ptr))
504 ptr++;
506 if(*ptr)
507 *ret = NAN;
508 else
509 *ret = neg ? -INFINITY : INFINITY;
510 return S_OK;
513 if(*ptr == '0' && ptr[1] == 'x') {
514 DWORD l = 0;
516 ptr += 2;
517 while((l = hex_to_int(*ptr)) != -1) {
518 d = d*16 + l;
519 ptr++;
522 *ret = d;
523 return S_OK;
526 while(isdigitW(*ptr))
527 d = d*10 + (*ptr++ - '0');
529 if(*ptr == 'e' || *ptr == 'E') {
530 BOOL eneg = FALSE;
531 LONG l = 0;
533 ptr++;
534 if(*ptr == '-') {
535 ptr++;
536 eneg = TRUE;
537 }else if(*ptr == '+') {
538 ptr++;
541 while(isdigitW(*ptr))
542 l = l*10 + (*ptr++ - '0');
543 if(eneg)
544 l = -l;
546 d *= pow(10, l);
547 }else if(*ptr == '.') {
548 DOUBLE dec = 0.1;
550 ptr++;
551 while(isdigitW(*ptr)) {
552 d += dec * (*ptr++ - '0');
553 dec *= 0.1;
557 while(isspaceW(*ptr))
558 ptr++;
560 if(*ptr) {
561 *ret = NAN;
562 return S_OK;
565 if(neg)
566 d = -d;
568 *ret = d;
569 return S_OK;
572 /* ECMA-262 3rd Edition 9.3 */
573 HRESULT to_number(script_ctx_t *ctx, jsval_t val, double *ret)
575 switch(jsval_type(val)) {
576 case JSV_UNDEFINED:
577 *ret = NAN;
578 return S_OK;
579 case JSV_NULL:
580 *ret = 0;
581 return S_OK;
582 case JSV_NUMBER:
583 *ret = get_number(val);
584 return S_OK;
585 case JSV_STRING:
586 return str_to_number(get_string(val), ret);
587 case JSV_OBJECT: {
588 jsval_t prim;
589 HRESULT hres;
591 hres = to_primitive(ctx, val, &prim, HINT_NUMBER);
592 if(FAILED(hres))
593 return hres;
595 hres = to_number(ctx, prim, ret);
596 jsval_release(prim);
597 return hres;
599 case JSV_BOOL:
600 *ret = get_bool(val) ? 1 : 0;
601 return S_OK;
602 case JSV_VARIANT:
603 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
604 return E_NOTIMPL;
607 assert(0);
608 return E_FAIL;
611 /* ECMA-262 3rd Edition 9.4 */
612 HRESULT to_integer(script_ctx_t *ctx, jsval_t v, double *ret)
614 double n;
615 HRESULT hres;
617 hres = to_number(ctx, v, &n);
618 if(FAILED(hres))
619 return hres;
621 if(isnan(n))
622 *ret = 0;
623 else
624 *ret = n >= 0.0 ? floor(n) : -floor(-n);
625 return S_OK;
628 /* ECMA-262 3rd Edition 9.5 */
629 HRESULT to_int32(script_ctx_t *ctx, jsval_t v, INT *ret)
631 double n;
632 HRESULT hres;
634 hres = to_number(ctx, v, &n);
635 if(FAILED(hres))
636 return hres;
638 *ret = isnan(n) || isinf(n) ? 0 : n;
639 return S_OK;
642 /* ECMA-262 3rd Edition 9.6 */
643 HRESULT to_uint32(script_ctx_t *ctx, jsval_t val, DWORD *ret)
645 INT32 n;
646 HRESULT hres;
648 hres = to_int32(ctx, val, &n);
649 if(SUCCEEDED(hres))
650 *ret = n;
651 return hres;
654 static jsstr_t *int_to_string(int i)
656 WCHAR buf[12], *p;
657 BOOL neg = FALSE;
659 if(!i) {
660 static const WCHAR zeroW[] = {'0',0};
661 return jsstr_alloc(zeroW);
664 if(i < 0) {
665 neg = TRUE;
666 i = -i;
669 p = buf + sizeof(buf)/sizeof(*buf)-1;
670 *p-- = 0;
671 while(i) {
672 *p-- = i%10 + '0';
673 i /= 10;
676 if(neg)
677 *p = '-';
678 else
679 p++;
681 return jsstr_alloc(p);
684 HRESULT double_to_string(double n, jsstr_t **str)
686 const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};
688 if(isnan(n)) {
689 *str = jsstr_nan();
690 }else if(isinf(n)) {
691 *str = jsstr_alloc(n<0 ? InfinityW : InfinityW+1);
692 }else if(is_int32(n)) {
693 *str = int_to_string(n);
694 }else {
695 VARIANT strv, v;
696 HRESULT hres;
698 /* FIXME: Don't use VariantChangeTypeEx */
699 V_VT(&v) = VT_R8;
700 V_R8(&v) = n;
701 V_VT(&strv) = VT_EMPTY;
702 hres = VariantChangeTypeEx(&strv, &v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
703 if(FAILED(hres))
704 return hres;
706 *str = jsstr_alloc(V_BSTR(&strv));
707 SysFreeString(V_BSTR(&strv));
710 return *str ? S_OK : E_OUTOFMEMORY;
713 /* ECMA-262 3rd Edition 9.8 */
714 HRESULT to_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str)
716 const WCHAR nullW[] = {'n','u','l','l',0};
717 const WCHAR trueW[] = {'t','r','u','e',0};
718 const WCHAR falseW[] = {'f','a','l','s','e',0};
720 switch(jsval_type(val)) {
721 case JSV_UNDEFINED:
722 *str = jsstr_undefined();
723 return S_OK;
724 case JSV_NULL:
725 *str = jsstr_alloc(nullW);
726 break;
727 case JSV_NUMBER:
728 return double_to_string(get_number(val), str);
729 case JSV_STRING:
730 *str = jsstr_addref(get_string(val));
731 break;
732 case JSV_OBJECT: {
733 jsval_t prim;
734 HRESULT hres;
736 hres = to_primitive(ctx, val, &prim, HINT_STRING);
737 if(FAILED(hres))
738 return hres;
740 hres = to_string(ctx, prim, str);
741 jsval_release(prim);
742 return hres;
744 case JSV_BOOL:
745 *str = jsstr_alloc(get_bool(val) ? trueW : falseW);
746 break;
747 default:
748 FIXME("unsupported %s\n", debugstr_jsval(val));
749 return E_NOTIMPL;
752 return *str ? S_OK : E_OUTOFMEMORY;
755 HRESULT to_flat_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str, const WCHAR **ret_str)
757 HRESULT hres;
759 hres = to_string(ctx, val, str);
760 if(FAILED(hres))
761 return hres;
763 *ret_str = jsstr_flatten(*str);
764 if(!*ret_str) {
765 jsstr_release(*str);
766 return E_OUTOFMEMORY;
769 return S_OK;
772 /* ECMA-262 3rd Edition 9.9 */
773 HRESULT to_object(script_ctx_t *ctx, jsval_t val, IDispatch **disp)
775 jsdisp_t *dispex;
776 HRESULT hres;
778 switch(jsval_type(val)) {
779 case JSV_STRING:
780 hres = create_string(ctx, get_string(val), &dispex);
781 if(FAILED(hres))
782 return hres;
784 *disp = to_disp(dispex);
785 break;
786 case JSV_NUMBER:
787 hres = create_number(ctx, get_number(val), &dispex);
788 if(FAILED(hres))
789 return hres;
791 *disp = to_disp(dispex);
792 break;
793 case JSV_OBJECT:
794 if(get_object(val)) {
795 *disp = get_object(val);
796 IDispatch_AddRef(*disp);
797 }else {
798 jsdisp_t *obj;
800 hres = create_object(ctx, NULL, &obj);
801 if(FAILED(hres))
802 return hres;
804 *disp = to_disp(obj);
806 break;
807 case JSV_BOOL:
808 hres = create_bool(ctx, get_bool(val), &dispex);
809 if(FAILED(hres))
810 return hres;
812 *disp = to_disp(dispex);
813 break;
814 case JSV_UNDEFINED:
815 case JSV_NULL:
816 WARN("object expected\n");
817 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
818 case JSV_VARIANT:
819 switch(V_VT(get_variant(val))) {
820 case VT_ARRAY|VT_VARIANT:
821 hres = create_vbarray(ctx, V_ARRAY(get_variant(val)), &dispex);
822 if(FAILED(hres))
823 return hres;
825 *disp = to_disp(dispex);
826 break;
828 default:
829 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val)));
830 return E_NOTIMPL;
832 break;
835 return S_OK;
838 HRESULT variant_change_type(script_ctx_t *ctx, VARIANT *dst, VARIANT *src, VARTYPE vt)
840 jsval_t val;
841 HRESULT hres;
843 clear_ei(ctx);
844 hres = variant_to_jsval(src, &val);
845 if(FAILED(hres))
846 return hres;
848 switch(vt) {
849 case VT_I2:
850 case VT_I4: {
851 INT i;
853 hres = to_int32(ctx, val, &i);
854 if(SUCCEEDED(hres)) {
855 if(vt == VT_I4)
856 V_I4(dst) = i;
857 else
858 V_I2(dst) = i;
860 break;
862 case VT_R8: {
863 double n;
864 hres = to_number(ctx, val, &n);
865 if(SUCCEEDED(hres))
866 V_R8(dst) = n;
867 break;
869 case VT_R4: {
870 double n;
872 hres = to_number(ctx, val, &n);
873 if(SUCCEEDED(hres))
874 V_R4(dst) = n;
875 break;
877 case VT_BOOL: {
878 BOOL b;
880 hres = to_boolean(val, &b);
881 if(SUCCEEDED(hres))
882 V_BOOL(dst) = b ? VARIANT_TRUE : VARIANT_FALSE;
883 break;
885 case VT_BSTR: {
886 jsstr_t *str;
888 hres = to_string(ctx, val, &str);
889 if(FAILED(hres))
890 break;
892 if(is_null_bstr(str)) {
893 V_BSTR(dst) = NULL;
894 break;
897 V_BSTR(dst) = SysAllocStringLen(NULL, jsstr_length(str));
898 if(V_BSTR(dst))
899 jsstr_flush(str, V_BSTR(dst));
900 else
901 hres = E_OUTOFMEMORY;
902 break;
904 case VT_EMPTY:
905 hres = V_VT(src) == VT_EMPTY ? S_OK : E_NOTIMPL;
906 break;
907 case VT_NULL:
908 hres = V_VT(src) == VT_NULL ? S_OK : E_NOTIMPL;
909 break;
910 default:
911 FIXME("vt %d not implemented\n", vt);
912 hres = E_NOTIMPL;
915 jsval_release(val);
916 if(FAILED(hres))
917 return hres;
919 V_VT(dst) = vt;
920 return S_OK;
923 static inline JSCaller *impl_from_IServiceProvider(IServiceProvider *iface)
925 return CONTAINING_RECORD(iface, JSCaller, IServiceProvider_iface);
928 static HRESULT WINAPI JSCaller_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
930 JSCaller *This = impl_from_IServiceProvider(iface);
932 if(IsEqualGUID(&IID_IUnknown, riid)) {
933 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
934 *ppv = &This->IServiceProvider_iface;
935 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
936 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
937 *ppv = &This->IServiceProvider_iface;
938 }else {
939 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
940 *ppv = NULL;
941 return E_NOINTERFACE;
944 IUnknown_AddRef((IUnknown*)*ppv);
945 return S_OK;
948 static ULONG WINAPI JSCaller_AddRef(IServiceProvider *iface)
950 JSCaller *This = impl_from_IServiceProvider(iface);
951 LONG ref = InterlockedIncrement(&This->ref);
953 TRACE("(%p) ref=%d\n", This, ref);
955 return ref;
958 static ULONG WINAPI JSCaller_Release(IServiceProvider *iface)
960 JSCaller *This = impl_from_IServiceProvider(iface);
961 LONG ref = InterlockedIncrement(&This->ref);
963 TRACE("(%p) ref=%d\n", This, ref);
965 if(!ref) {
966 assert(!This->ctx);
967 heap_free(This);
970 return ref;
973 static HRESULT WINAPI JSCaller_QueryService(IServiceProvider *iface, REFGUID guidService,
974 REFIID riid, void **ppv)
976 JSCaller *This = impl_from_IServiceProvider(iface);
978 if(IsEqualGUID(guidService, &SID_VariantConversion) && This->ctx && This->ctx->active_script) {
979 TRACE("(%p)->(SID_VariantConversion)\n", This);
980 return IActiveScript_QueryInterface(This->ctx->active_script, riid, ppv);
983 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
985 *ppv = NULL;
986 return E_NOINTERFACE;
989 static const IServiceProviderVtbl ServiceProviderVtbl = {
990 JSCaller_QueryInterface,
991 JSCaller_AddRef,
992 JSCaller_Release,
993 JSCaller_QueryService
996 HRESULT create_jscaller(script_ctx_t *ctx)
998 JSCaller *ret;
1000 ret = heap_alloc(sizeof(*ret));
1001 if(!ret)
1002 return E_OUTOFMEMORY;
1004 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
1005 ret->ref = 1;
1006 ret->ctx = ctx;
1008 ctx->jscaller = ret;
1009 return S_OK;