kernel32/nls: Added LOCALE_SNAN entries.
[wine.git] / dlls / jscript / jsutils.c
blobe7100ba0d71a983830bc055ab4064da74f803f52
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 BOOL is_finite(double n)
58 return !isnan(n) && !isinf(n);
61 #define MIN_BLOCK_SIZE 128
62 #define ARENA_FREE_FILLER 0xaa
64 static inline DWORD block_size(DWORD block)
66 return MIN_BLOCK_SIZE << block;
69 void heap_pool_init(heap_pool_t *heap)
71 memset(heap, 0, sizeof(*heap));
72 list_init(&heap->custom_blocks);
75 void *heap_pool_alloc(heap_pool_t *heap, DWORD size)
77 struct list *list;
78 void *tmp;
80 if(!heap->block_cnt) {
81 if(!heap->blocks) {
82 heap->blocks = heap_alloc(sizeof(void*));
83 if(!heap->blocks)
84 return NULL;
87 tmp = heap_alloc(block_size(0));
88 if(!tmp)
89 return NULL;
91 heap->blocks[0] = tmp;
92 heap->block_cnt = 1;
95 if(heap->offset + size <= block_size(heap->last_block)) {
96 tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
97 heap->offset += size;
98 return tmp;
101 if(size <= block_size(heap->last_block+1)) {
102 if(heap->last_block+1 == heap->block_cnt) {
103 tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
104 if(!tmp)
105 return NULL;
107 heap->blocks = tmp;
108 heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
109 if(!heap->blocks[heap->block_cnt])
110 return NULL;
112 heap->block_cnt++;
115 heap->last_block++;
116 heap->offset = size;
117 return heap->blocks[heap->last_block];
120 list = heap_alloc(size + sizeof(struct list));
121 if(!list)
122 return NULL;
124 list_add_head(&heap->custom_blocks, list);
125 return list+1;
128 void *heap_pool_grow(heap_pool_t *heap, void *mem, DWORD size, DWORD inc)
130 void *ret;
132 if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
133 && heap->offset+inc < block_size(heap->last_block)) {
134 heap->offset += inc;
135 return mem;
138 ret = heap_pool_alloc(heap, size+inc);
139 if(ret) /* FIXME: avoid copying for custom blocks */
140 memcpy(ret, mem, size);
141 return ret;
144 void heap_pool_clear(heap_pool_t *heap)
146 struct list *tmp;
148 if(!heap)
149 return;
151 while((tmp = list_head(&heap->custom_blocks))) {
152 list_remove(tmp);
153 heap_free(tmp);
156 if(WARN_ON(heap)) {
157 DWORD i;
159 for(i=0; i < heap->block_cnt; i++)
160 memset(heap->blocks[i], ARENA_FREE_FILLER, block_size(i));
163 heap->last_block = heap->offset = 0;
164 heap->mark = FALSE;
167 void heap_pool_free(heap_pool_t *heap)
169 DWORD i;
171 heap_pool_clear(heap);
173 for(i=0; i < heap->block_cnt; i++)
174 heap_free(heap->blocks[i]);
175 heap_free(heap->blocks);
177 heap_pool_init(heap);
180 heap_pool_t *heap_pool_mark(heap_pool_t *heap)
182 if(heap->mark)
183 return NULL;
185 heap->mark = TRUE;
186 return heap;
189 void jsval_release(jsval_t val)
191 switch(jsval_type(val)) {
192 case JSV_OBJECT:
193 if(get_object(val))
194 IDispatch_Release(get_object(val));
195 break;
196 case JSV_STRING:
197 jsstr_release(get_string(val));
198 break;
199 case JSV_VARIANT:
200 VariantClear(get_variant(val));
201 heap_free(get_variant(val));
202 break;
203 default:
204 break;
208 static HRESULT jsval_variant(jsval_t *val, VARIANT *var)
210 VARIANT *v;
211 HRESULT hres;
213 __JSVAL_TYPE(*val) = JSV_VARIANT;
214 __JSVAL_VAR(*val) = v = heap_alloc(sizeof(VARIANT));
215 if(!v) {
216 *val = jsval_undefined();
217 return E_OUTOFMEMORY;
220 V_VT(v) = VT_EMPTY;
221 hres = VariantCopy(v, var);
222 if(FAILED(hres)) {
223 *val = jsval_undefined();
224 heap_free(v);
226 return hres;
229 HRESULT jsval_copy(jsval_t v, jsval_t *r)
231 switch(jsval_type(v)) {
232 case JSV_UNDEFINED:
233 case JSV_NULL:
234 case JSV_NUMBER:
235 case JSV_BOOL:
236 *r = v;
237 return S_OK;
238 case JSV_OBJECT:
239 if(get_object(v))
240 IDispatch_AddRef(get_object(v));
241 *r = v;
242 return S_OK;
243 case JSV_STRING: {
244 jsstr_addref(get_string(v));
245 *r = v;
246 return S_OK;
248 case JSV_VARIANT:
249 return jsval_variant(r, get_variant(v));
252 assert(0);
253 return E_FAIL;
256 HRESULT variant_to_jsval(VARIANT *var, jsval_t *r)
258 if(V_VT(var) == (VT_VARIANT|VT_BYREF))
259 var = V_VARIANTREF(var);
261 switch(V_VT(var)) {
262 case VT_EMPTY:
263 *r = jsval_undefined();
264 return S_OK;
265 case VT_NULL:
266 *r = jsval_null();
267 return S_OK;
268 case VT_BOOL:
269 *r = jsval_bool(V_BOOL(var));
270 return S_OK;
271 case VT_I4:
272 *r = jsval_number(V_I4(var));
273 return S_OK;
274 case VT_R8:
275 *r = jsval_number(V_R8(var));
276 return S_OK;
277 case VT_BSTR: {
278 jsstr_t *str;
280 if(V_BSTR(var)) {
281 str = jsstr_alloc_len(V_BSTR(var), SysStringLen(V_BSTR(var)));
282 if(!str)
283 return E_OUTOFMEMORY;
284 }else {
285 str = jsstr_null_bstr();
288 *r = jsval_string(str);
289 return S_OK;
291 case VT_DISPATCH: {
292 if(V_DISPATCH(var))
293 IDispatch_AddRef(V_DISPATCH(var));
294 *r = jsval_disp(V_DISPATCH(var));
295 return S_OK;
297 case VT_I2:
298 *r = jsval_number(V_I2(var));
299 return S_OK;
300 case VT_INT:
301 *r = jsval_number(V_INT(var));
302 return S_OK;
303 case VT_UI4:
304 *r = jsval_number(V_UI4(var));
305 return S_OK;
306 case VT_UNKNOWN:
307 if(V_UNKNOWN(var)) {
308 IDispatch *disp;
309 HRESULT hres;
311 hres = IUnknown_QueryInterface(V_UNKNOWN(var), &IID_IDispatch, (void**)&disp);
312 if(SUCCEEDED(hres)) {
313 *r = jsval_disp(disp);
314 return S_OK;
316 }else {
317 *r = jsval_disp(NULL);
318 return S_OK;
320 /* fall through */
321 default:
322 return jsval_variant(r, var);
326 HRESULT jsval_to_variant(jsval_t val, VARIANT *retv)
328 switch(jsval_type(val)) {
329 case JSV_UNDEFINED:
330 V_VT(retv) = VT_EMPTY;
331 return S_OK;
332 case JSV_NULL:
333 V_VT(retv) = VT_NULL;
334 return S_OK;
335 case JSV_OBJECT:
336 V_VT(retv) = VT_DISPATCH;
337 if(get_object(val))
338 IDispatch_AddRef(get_object(val));
339 V_DISPATCH(retv) = get_object(val);
340 return S_OK;
341 case JSV_STRING: {
342 jsstr_t *str = get_string(val);
344 V_VT(retv) = VT_BSTR;
345 if(is_null_bstr(str)) {
346 V_BSTR(retv) = NULL;
347 }else {
348 V_BSTR(retv) = SysAllocStringLen(NULL, jsstr_length(str));
349 if(V_BSTR(retv))
350 jsstr_flush(str, V_BSTR(retv));
351 else
352 return E_OUTOFMEMORY;
354 return S_OK;
356 case JSV_NUMBER: {
357 double n = get_number(val);
359 if(is_int32(n)) {
360 V_VT(retv) = VT_I4;
361 V_I4(retv) = n;
362 }else {
363 V_VT(retv) = VT_R8;
364 V_R8(retv) = n;
367 return S_OK;
369 case JSV_BOOL:
370 V_VT(retv) = VT_BOOL;
371 V_BOOL(retv) = get_bool(val) ? VARIANT_TRUE : VARIANT_FALSE;
372 return S_OK;
373 case JSV_VARIANT:
374 V_VT(retv) = VT_EMPTY;
375 return VariantCopy(retv, get_variant(val));
378 assert(0);
379 return E_FAIL;
382 /* ECMA-262 3rd Edition 9.1 */
383 HRESULT to_primitive(script_ctx_t *ctx, jsval_t val, jsval_t *ret, hint_t hint)
385 if(is_object_instance(val)) {
386 jsdisp_t *jsdisp;
387 jsval_t prim;
388 DISPID id;
389 HRESULT hres;
391 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
392 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
394 if(!get_object(val)) {
395 *ret = jsval_null();
396 return S_OK;
399 jsdisp = iface_to_jsdisp(get_object(val));
400 if(!jsdisp)
401 return disp_propget(ctx, get_object(val), DISPID_VALUE, ret);
403 if(hint == NO_HINT)
404 hint = is_class(jsdisp, JSCLASS_DATE) ? HINT_STRING : HINT_NUMBER;
406 /* Native implementation doesn't throw TypeErrors, returns strange values */
408 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? toStringW : valueOfW, 0, &id);
409 if(SUCCEEDED(hres)) {
410 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
411 if(FAILED(hres)) {
412 WARN("call error - forwarding exception\n");
413 jsdisp_release(jsdisp);
414 return hres;
415 }else if(!is_object_instance(prim)) {
416 jsdisp_release(jsdisp);
417 *ret = prim;
418 return S_OK;
419 }else {
420 IDispatch_Release(get_object(prim));
424 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? valueOfW : toStringW, 0, &id);
425 if(SUCCEEDED(hres)) {
426 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
427 if(FAILED(hres)) {
428 WARN("call error - forwarding exception\n");
429 jsdisp_release(jsdisp);
430 return hres;
431 }else if(!is_object_instance(prim)) {
432 jsdisp_release(jsdisp);
433 *ret = prim;
434 return S_OK;
435 }else {
436 IDispatch_Release(get_object(prim));
440 jsdisp_release(jsdisp);
442 WARN("failed\n");
443 return throw_type_error(ctx, JS_E_TO_PRIMITIVE, NULL);
446 return jsval_copy(val, ret);
450 /* ECMA-262 3rd Edition 9.2 */
451 HRESULT to_boolean(jsval_t val, BOOL *ret)
453 switch(jsval_type(val)) {
454 case JSV_UNDEFINED:
455 case JSV_NULL:
456 *ret = FALSE;
457 return S_OK;
458 case JSV_OBJECT:
459 *ret = get_object(val) != NULL;
460 return S_OK;
461 case JSV_STRING:
462 *ret = jsstr_length(get_string(val)) != 0;
463 return S_OK;
464 case JSV_NUMBER:
465 *ret = !isnan(get_number(val)) && get_number(val);
466 return S_OK;
467 case JSV_BOOL:
468 *ret = get_bool(val);
469 return S_OK;
470 case JSV_VARIANT:
471 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
472 return E_NOTIMPL;
475 assert(0);
476 return E_FAIL;
479 static int hex_to_int(WCHAR c)
481 if('0' <= c && c <= '9')
482 return c-'0';
484 if('a' <= c && c <= 'f')
485 return c-'a'+10;
487 if('A' <= c && c <= 'F')
488 return c-'A'+10;
490 return -1;
493 /* ECMA-262 3rd Edition 9.3.1 */
494 static HRESULT str_to_number(jsstr_t *str, double *ret)
496 const WCHAR *ptr;
497 BOOL neg = FALSE;
498 DOUBLE d = 0.0;
500 static const WCHAR infinityW[] = {'I','n','f','i','n','i','t','y'};
502 ptr = jsstr_flatten(str);
503 if(!ptr)
504 return E_OUTOFMEMORY;
506 while(isspaceW(*ptr))
507 ptr++;
509 if(*ptr == '-') {
510 neg = TRUE;
511 ptr++;
512 }else if(*ptr == '+') {
513 ptr++;
516 if(!strncmpW(ptr, infinityW, sizeof(infinityW)/sizeof(WCHAR))) {
517 ptr += sizeof(infinityW)/sizeof(WCHAR);
518 while(*ptr && isspaceW(*ptr))
519 ptr++;
521 if(*ptr)
522 *ret = NAN;
523 else
524 *ret = neg ? -INFINITY : INFINITY;
525 return S_OK;
528 if(*ptr == '0' && ptr[1] == 'x') {
529 DWORD l = 0;
531 ptr += 2;
532 while((l = hex_to_int(*ptr)) != -1) {
533 d = d*16 + l;
534 ptr++;
537 *ret = d;
538 return S_OK;
541 while(isdigitW(*ptr))
542 d = d*10 + (*ptr++ - '0');
544 if(*ptr == 'e' || *ptr == 'E') {
545 BOOL eneg = FALSE;
546 LONG l = 0;
548 ptr++;
549 if(*ptr == '-') {
550 ptr++;
551 eneg = TRUE;
552 }else if(*ptr == '+') {
553 ptr++;
556 while(isdigitW(*ptr))
557 l = l*10 + (*ptr++ - '0');
558 if(eneg)
559 l = -l;
561 d *= pow(10, l);
562 }else if(*ptr == '.') {
563 DOUBLE dec = 0.1;
565 ptr++;
566 while(isdigitW(*ptr)) {
567 d += dec * (*ptr++ - '0');
568 dec *= 0.1;
572 while(isspaceW(*ptr))
573 ptr++;
575 if(*ptr) {
576 *ret = NAN;
577 return S_OK;
580 if(neg)
581 d = -d;
583 *ret = d;
584 return S_OK;
587 /* ECMA-262 3rd Edition 9.3 */
588 HRESULT to_number(script_ctx_t *ctx, jsval_t val, double *ret)
590 switch(jsval_type(val)) {
591 case JSV_UNDEFINED:
592 *ret = NAN;
593 return S_OK;
594 case JSV_NULL:
595 *ret = 0;
596 return S_OK;
597 case JSV_NUMBER:
598 *ret = get_number(val);
599 return S_OK;
600 case JSV_STRING:
601 return str_to_number(get_string(val), ret);
602 case JSV_OBJECT: {
603 jsval_t prim;
604 HRESULT hres;
606 hres = to_primitive(ctx, val, &prim, HINT_NUMBER);
607 if(FAILED(hres))
608 return hres;
610 hres = to_number(ctx, prim, ret);
611 jsval_release(prim);
612 return hres;
614 case JSV_BOOL:
615 *ret = get_bool(val) ? 1 : 0;
616 return S_OK;
617 case JSV_VARIANT:
618 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
619 return E_NOTIMPL;
622 assert(0);
623 return E_FAIL;
626 /* ECMA-262 3rd Edition 9.4 */
627 HRESULT to_integer(script_ctx_t *ctx, jsval_t v, double *ret)
629 double n;
630 HRESULT hres;
632 hres = to_number(ctx, v, &n);
633 if(FAILED(hres))
634 return hres;
636 if(isnan(n))
637 *ret = 0;
638 else
639 *ret = n >= 0.0 ? floor(n) : -floor(-n);
640 return S_OK;
643 /* ECMA-262 3rd Edition 9.5 */
644 HRESULT to_int32(script_ctx_t *ctx, jsval_t v, INT *ret)
646 double n;
647 HRESULT hres;
649 hres = to_number(ctx, v, &n);
650 if(FAILED(hres))
651 return hres;
653 *ret = is_finite(n) ? n : 0;
654 return S_OK;
657 /* ECMA-262 3rd Edition 9.6 */
658 HRESULT to_uint32(script_ctx_t *ctx, jsval_t val, DWORD *ret)
660 INT32 n;
661 HRESULT hres;
663 hres = to_int32(ctx, val, &n);
664 if(SUCCEEDED(hres))
665 *ret = n;
666 return hres;
669 static jsstr_t *int_to_string(int i)
671 WCHAR buf[12], *p;
672 BOOL neg = FALSE;
674 if(!i) {
675 static const WCHAR zeroW[] = {'0',0};
676 return jsstr_alloc(zeroW);
679 if(i < 0) {
680 neg = TRUE;
681 i = -i;
684 p = buf + sizeof(buf)/sizeof(*buf)-1;
685 *p-- = 0;
686 while(i) {
687 *p-- = i%10 + '0';
688 i /= 10;
691 if(neg)
692 *p = '-';
693 else
694 p++;
696 return jsstr_alloc(p);
699 HRESULT double_to_string(double n, jsstr_t **str)
701 const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};
703 if(isnan(n)) {
704 *str = jsstr_nan();
705 }else if(isinf(n)) {
706 *str = jsstr_alloc(n<0 ? InfinityW : InfinityW+1);
707 }else if(is_int32(n)) {
708 *str = int_to_string(n);
709 }else {
710 VARIANT strv, v;
711 HRESULT hres;
713 /* FIXME: Don't use VariantChangeTypeEx */
714 V_VT(&v) = VT_R8;
715 V_R8(&v) = n;
716 V_VT(&strv) = VT_EMPTY;
717 hres = VariantChangeTypeEx(&strv, &v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
718 if(FAILED(hres))
719 return hres;
721 *str = jsstr_alloc(V_BSTR(&strv));
722 SysFreeString(V_BSTR(&strv));
725 return *str ? S_OK : E_OUTOFMEMORY;
728 /* ECMA-262 3rd Edition 9.8 */
729 HRESULT to_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str)
731 const WCHAR nullW[] = {'n','u','l','l',0};
732 const WCHAR trueW[] = {'t','r','u','e',0};
733 const WCHAR falseW[] = {'f','a','l','s','e',0};
735 switch(jsval_type(val)) {
736 case JSV_UNDEFINED:
737 *str = jsstr_undefined();
738 return S_OK;
739 case JSV_NULL:
740 *str = jsstr_alloc(nullW);
741 break;
742 case JSV_NUMBER:
743 return double_to_string(get_number(val), str);
744 case JSV_STRING:
745 *str = jsstr_addref(get_string(val));
746 break;
747 case JSV_OBJECT: {
748 jsval_t prim;
749 HRESULT hres;
751 hres = to_primitive(ctx, val, &prim, HINT_STRING);
752 if(FAILED(hres))
753 return hres;
755 hres = to_string(ctx, prim, str);
756 jsval_release(prim);
757 return hres;
759 case JSV_BOOL:
760 *str = jsstr_alloc(get_bool(val) ? trueW : falseW);
761 break;
762 default:
763 FIXME("unsupported %s\n", debugstr_jsval(val));
764 return E_NOTIMPL;
767 return *str ? S_OK : E_OUTOFMEMORY;
770 HRESULT to_flat_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str, const WCHAR **ret_str)
772 HRESULT hres;
774 hres = to_string(ctx, val, str);
775 if(FAILED(hres))
776 return hres;
778 *ret_str = jsstr_flatten(*str);
779 if(!*ret_str) {
780 jsstr_release(*str);
781 return E_OUTOFMEMORY;
784 return S_OK;
787 /* ECMA-262 3rd Edition 9.9 */
788 HRESULT to_object(script_ctx_t *ctx, jsval_t val, IDispatch **disp)
790 jsdisp_t *dispex;
791 HRESULT hres;
793 switch(jsval_type(val)) {
794 case JSV_STRING:
795 hres = create_string(ctx, get_string(val), &dispex);
796 if(FAILED(hres))
797 return hres;
799 *disp = to_disp(dispex);
800 break;
801 case JSV_NUMBER:
802 hres = create_number(ctx, get_number(val), &dispex);
803 if(FAILED(hres))
804 return hres;
806 *disp = to_disp(dispex);
807 break;
808 case JSV_OBJECT:
809 if(get_object(val)) {
810 *disp = get_object(val);
811 IDispatch_AddRef(*disp);
812 }else {
813 jsdisp_t *obj;
815 hres = create_object(ctx, NULL, &obj);
816 if(FAILED(hres))
817 return hres;
819 *disp = to_disp(obj);
821 break;
822 case JSV_BOOL:
823 hres = create_bool(ctx, get_bool(val), &dispex);
824 if(FAILED(hres))
825 return hres;
827 *disp = to_disp(dispex);
828 break;
829 case JSV_UNDEFINED:
830 case JSV_NULL:
831 WARN("object expected\n");
832 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
833 case JSV_VARIANT:
834 switch(V_VT(get_variant(val))) {
835 case VT_ARRAY|VT_VARIANT:
836 hres = create_vbarray(ctx, V_ARRAY(get_variant(val)), &dispex);
837 if(FAILED(hres))
838 return hres;
840 *disp = to_disp(dispex);
841 break;
843 default:
844 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val)));
845 return E_NOTIMPL;
847 break;
850 return S_OK;
853 HRESULT variant_change_type(script_ctx_t *ctx, VARIANT *dst, VARIANT *src, VARTYPE vt)
855 jsval_t val;
856 HRESULT hres;
858 clear_ei(ctx);
859 hres = variant_to_jsval(src, &val);
860 if(FAILED(hres))
861 return hres;
863 switch(vt) {
864 case VT_I2:
865 case VT_I4: {
866 INT i;
868 hres = to_int32(ctx, val, &i);
869 if(SUCCEEDED(hres)) {
870 if(vt == VT_I4)
871 V_I4(dst) = i;
872 else
873 V_I2(dst) = i;
875 break;
877 case VT_R8: {
878 double n;
879 hres = to_number(ctx, val, &n);
880 if(SUCCEEDED(hres))
881 V_R8(dst) = n;
882 break;
884 case VT_R4: {
885 double n;
887 hres = to_number(ctx, val, &n);
888 if(SUCCEEDED(hres))
889 V_R4(dst) = n;
890 break;
892 case VT_BOOL: {
893 BOOL b;
895 hres = to_boolean(val, &b);
896 if(SUCCEEDED(hres))
897 V_BOOL(dst) = b ? VARIANT_TRUE : VARIANT_FALSE;
898 break;
900 case VT_BSTR: {
901 jsstr_t *str;
903 hres = to_string(ctx, val, &str);
904 if(FAILED(hres))
905 break;
907 if(is_null_bstr(str)) {
908 V_BSTR(dst) = NULL;
909 break;
912 V_BSTR(dst) = SysAllocStringLen(NULL, jsstr_length(str));
913 if(V_BSTR(dst))
914 jsstr_flush(str, V_BSTR(dst));
915 else
916 hres = E_OUTOFMEMORY;
917 break;
919 case VT_EMPTY:
920 hres = V_VT(src) == VT_EMPTY ? S_OK : E_NOTIMPL;
921 break;
922 case VT_NULL:
923 hres = V_VT(src) == VT_NULL ? S_OK : E_NOTIMPL;
924 break;
925 default:
926 FIXME("vt %d not implemented\n", vt);
927 hres = E_NOTIMPL;
930 jsval_release(val);
931 if(FAILED(hres))
932 return hres;
934 V_VT(dst) = vt;
935 return S_OK;
938 static inline JSCaller *impl_from_IServiceProvider(IServiceProvider *iface)
940 return CONTAINING_RECORD(iface, JSCaller, IServiceProvider_iface);
943 static HRESULT WINAPI JSCaller_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
945 JSCaller *This = impl_from_IServiceProvider(iface);
947 if(IsEqualGUID(&IID_IUnknown, riid)) {
948 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
949 *ppv = &This->IServiceProvider_iface;
950 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
951 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
952 *ppv = &This->IServiceProvider_iface;
953 }else {
954 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
955 *ppv = NULL;
956 return E_NOINTERFACE;
959 IUnknown_AddRef((IUnknown*)*ppv);
960 return S_OK;
963 static ULONG WINAPI JSCaller_AddRef(IServiceProvider *iface)
965 JSCaller *This = impl_from_IServiceProvider(iface);
966 LONG ref = InterlockedIncrement(&This->ref);
968 TRACE("(%p) ref=%d\n", This, ref);
970 return ref;
973 static ULONG WINAPI JSCaller_Release(IServiceProvider *iface)
975 JSCaller *This = impl_from_IServiceProvider(iface);
976 LONG ref = InterlockedIncrement(&This->ref);
978 TRACE("(%p) ref=%d\n", This, ref);
980 if(!ref) {
981 assert(!This->ctx);
982 heap_free(This);
985 return ref;
988 static HRESULT WINAPI JSCaller_QueryService(IServiceProvider *iface, REFGUID guidService,
989 REFIID riid, void **ppv)
991 JSCaller *This = impl_from_IServiceProvider(iface);
993 if(IsEqualGUID(guidService, &SID_VariantConversion) && This->ctx && This->ctx->active_script) {
994 TRACE("(%p)->(SID_VariantConversion)\n", This);
995 return IActiveScript_QueryInterface(This->ctx->active_script, riid, ppv);
998 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1000 *ppv = NULL;
1001 return E_NOINTERFACE;
1004 static const IServiceProviderVtbl ServiceProviderVtbl = {
1005 JSCaller_QueryInterface,
1006 JSCaller_AddRef,
1007 JSCaller_Release,
1008 JSCaller_QueryService
1011 HRESULT create_jscaller(script_ctx_t *ctx)
1013 JSCaller *ret;
1015 ret = heap_alloc(sizeof(*ret));
1016 if(!ret)
1017 return E_OUTOFMEMORY;
1019 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
1020 ret->ref = 1;
1021 ret->ctx = ctx;
1023 ctx->jscaller = ret;
1024 return S_OK;