krnl386.exe16: Emulate 'mov Eb, Gb' instruction on x86 processor architecture.
[wine.git] / dlls / jscript / jsutils.c
blob59dbc65408f1bccc56ca3337c5d21c1b4b5fc1a6
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_next(&heap->custom_blocks, &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 return E_OUTOFMEMORY;
218 V_VT(v) = VT_EMPTY;
219 hres = VariantCopy(v, var);
220 if(FAILED(hres))
221 heap_free(v);
222 return hres;
225 HRESULT jsval_copy(jsval_t v, jsval_t *r)
227 switch(jsval_type(v)) {
228 case JSV_UNDEFINED:
229 case JSV_NULL:
230 case JSV_NUMBER:
231 case JSV_BOOL:
232 *r = v;
233 return S_OK;
234 case JSV_OBJECT:
235 if(get_object(v))
236 IDispatch_AddRef(get_object(v));
237 *r = v;
238 return S_OK;
239 case JSV_STRING: {
240 jsstr_addref(get_string(v));
241 *r = v;
242 return S_OK;
244 case JSV_VARIANT:
245 return jsval_variant(r, get_variant(v));
248 assert(0);
249 return E_FAIL;
252 HRESULT variant_to_jsval(VARIANT *var, jsval_t *r)
254 if(V_VT(var) == (VT_VARIANT|VT_BYREF))
255 var = V_VARIANTREF(var);
257 switch(V_VT(var)) {
258 case VT_EMPTY:
259 *r = jsval_undefined();
260 return S_OK;
261 case VT_NULL:
262 *r = jsval_null();
263 return S_OK;
264 case VT_BOOL:
265 *r = jsval_bool(V_BOOL(var));
266 return S_OK;
267 case VT_I4:
268 *r = jsval_number(V_I4(var));
269 return S_OK;
270 case VT_R8:
271 *r = jsval_number(V_R8(var));
272 return S_OK;
273 case VT_BSTR: {
274 jsstr_t *str;
276 if(V_BSTR(var)) {
277 str = jsstr_alloc_len(V_BSTR(var), SysStringLen(V_BSTR(var)));
278 if(!str)
279 return E_OUTOFMEMORY;
280 }else {
281 str = jsstr_null_bstr();
284 *r = jsval_string(str);
285 return S_OK;
287 case VT_DISPATCH: {
288 if(V_DISPATCH(var))
289 IDispatch_AddRef(V_DISPATCH(var));
290 *r = jsval_disp(V_DISPATCH(var));
291 return S_OK;
293 case VT_I2:
294 *r = jsval_number(V_I2(var));
295 return S_OK;
296 case VT_INT:
297 *r = jsval_number(V_INT(var));
298 return S_OK;
299 case VT_UI4:
300 *r = jsval_number(V_UI4(var));
301 return S_OK;
302 case VT_UNKNOWN:
303 if(V_UNKNOWN(var)) {
304 IDispatch *disp;
305 HRESULT hres;
307 hres = IUnknown_QueryInterface(V_UNKNOWN(var), &IID_IDispatch, (void**)&disp);
308 if(SUCCEEDED(hres)) {
309 *r = jsval_disp(disp);
310 return S_OK;
312 }else {
313 *r = jsval_disp(NULL);
314 return S_OK;
316 /* fall through */
317 default:
318 return jsval_variant(r, var);
322 HRESULT jsval_to_variant(jsval_t val, VARIANT *retv)
324 switch(jsval_type(val)) {
325 case JSV_UNDEFINED:
326 V_VT(retv) = VT_EMPTY;
327 return S_OK;
328 case JSV_NULL:
329 V_VT(retv) = VT_NULL;
330 return S_OK;
331 case JSV_OBJECT:
332 V_VT(retv) = VT_DISPATCH;
333 if(get_object(val))
334 IDispatch_AddRef(get_object(val));
335 V_DISPATCH(retv) = get_object(val);
336 return S_OK;
337 case JSV_STRING: {
338 jsstr_t *str = get_string(val);
340 V_VT(retv) = VT_BSTR;
341 if(is_null_bstr(str)) {
342 V_BSTR(retv) = NULL;
343 }else {
344 V_BSTR(retv) = SysAllocStringLen(NULL, jsstr_length(str));
345 if(V_BSTR(retv))
346 jsstr_flush(str, V_BSTR(retv));
347 else
348 return E_OUTOFMEMORY;
350 return S_OK;
352 case JSV_NUMBER: {
353 double n = get_number(val);
355 if(is_int32(n)) {
356 V_VT(retv) = VT_I4;
357 V_I4(retv) = n;
358 }else {
359 V_VT(retv) = VT_R8;
360 V_R8(retv) = n;
363 return S_OK;
365 case JSV_BOOL:
366 V_VT(retv) = VT_BOOL;
367 V_BOOL(retv) = get_bool(val) ? VARIANT_TRUE : VARIANT_FALSE;
368 return S_OK;
369 case JSV_VARIANT:
370 V_VT(retv) = VT_EMPTY;
371 return VariantCopy(retv, get_variant(val));
374 assert(0);
375 return E_FAIL;
378 /* ECMA-262 3rd Edition 9.1 */
379 HRESULT to_primitive(script_ctx_t *ctx, jsval_t val, jsval_t *ret, hint_t hint)
381 if(is_object_instance(val)) {
382 jsdisp_t *jsdisp;
383 jsval_t prim;
384 DISPID id;
385 HRESULT hres;
387 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
388 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
390 if(!get_object(val)) {
391 *ret = jsval_null();
392 return S_OK;
395 jsdisp = iface_to_jsdisp((IUnknown*)get_object(val));
396 if(!jsdisp)
397 return disp_propget(ctx, get_object(val), DISPID_VALUE, ret);
399 if(hint == NO_HINT)
400 hint = is_class(jsdisp, JSCLASS_DATE) ? HINT_STRING : HINT_NUMBER;
402 /* Native implementation doesn't throw TypeErrors, returns strange values */
404 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? toStringW : valueOfW, 0, &id);
405 if(SUCCEEDED(hres)) {
406 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
407 if(FAILED(hres)) {
408 WARN("call error - forwarding exception\n");
409 jsdisp_release(jsdisp);
410 return hres;
411 }else if(!is_object_instance(prim)) {
412 jsdisp_release(jsdisp);
413 *ret = prim;
414 return S_OK;
415 }else {
416 IDispatch_Release(get_object(prim));
420 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? valueOfW : toStringW, 0, &id);
421 if(SUCCEEDED(hres)) {
422 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
423 if(FAILED(hres)) {
424 WARN("call error - forwarding exception\n");
425 jsdisp_release(jsdisp);
426 return hres;
427 }else if(!is_object_instance(prim)) {
428 jsdisp_release(jsdisp);
429 *ret = prim;
430 return S_OK;
431 }else {
432 IDispatch_Release(get_object(prim));
436 jsdisp_release(jsdisp);
438 WARN("failed\n");
439 return throw_type_error(ctx, JS_E_TO_PRIMITIVE, NULL);
442 return jsval_copy(val, ret);
446 /* ECMA-262 3rd Edition 9.2 */
447 HRESULT to_boolean(jsval_t val, BOOL *ret)
449 switch(jsval_type(val)) {
450 case JSV_UNDEFINED:
451 case JSV_NULL:
452 *ret = FALSE;
453 return S_OK;
454 case JSV_OBJECT:
455 *ret = get_object(val) != NULL;
456 return S_OK;
457 case JSV_STRING:
458 *ret = jsstr_length(get_string(val)) != 0;
459 return S_OK;
460 case JSV_NUMBER:
461 *ret = !isnan(get_number(val)) && get_number(val);
462 return S_OK;
463 case JSV_BOOL:
464 *ret = get_bool(val);
465 return S_OK;
466 case JSV_VARIANT:
467 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
468 return E_NOTIMPL;
471 assert(0);
472 return E_FAIL;
475 static int hex_to_int(WCHAR c)
477 if('0' <= c && c <= '9')
478 return c-'0';
480 if('a' <= c && c <= 'f')
481 return c-'a'+10;
483 if('A' <= c && c <= 'F')
484 return c-'A'+10;
486 return -1;
489 /* ECMA-262 3rd Edition 9.3.1 */
490 static HRESULT str_to_number(jsstr_t *str, double *ret)
492 const WCHAR *ptr;
493 BOOL neg = FALSE;
494 DOUBLE d = 0.0;
496 static const WCHAR infinityW[] = {'I','n','f','i','n','i','t','y'};
498 ptr = jsstr_flatten(str);
499 if(!ptr)
500 return E_OUTOFMEMORY;
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 = is_finite(n) ? n : 0;
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 INT32 n;
657 HRESULT hres;
659 hres = to_int32(ctx, val, &n);
660 if(SUCCEEDED(hres))
661 *ret = n;
662 return hres;
665 static jsstr_t *int_to_string(int i)
667 WCHAR buf[12], *p;
668 BOOL neg = FALSE;
670 if(!i) {
671 static const WCHAR zeroW[] = {'0',0};
672 return jsstr_alloc(zeroW);
675 if(i < 0) {
676 neg = TRUE;
677 i = -i;
680 p = buf + sizeof(buf)/sizeof(*buf)-1;
681 *p-- = 0;
682 while(i) {
683 *p-- = i%10 + '0';
684 i /= 10;
687 if(neg)
688 *p = '-';
689 else
690 p++;
692 return jsstr_alloc(p);
695 HRESULT double_to_string(double n, jsstr_t **str)
697 const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};
699 if(isnan(n)) {
700 *str = jsstr_nan();
701 }else if(isinf(n)) {
702 *str = jsstr_alloc(n<0 ? InfinityW : InfinityW+1);
703 }else if(is_int32(n)) {
704 *str = int_to_string(n);
705 }else {
706 VARIANT strv, v;
707 HRESULT hres;
709 /* FIXME: Don't use VariantChangeTypeEx */
710 V_VT(&v) = VT_R8;
711 V_R8(&v) = n;
712 V_VT(&strv) = VT_EMPTY;
713 hres = VariantChangeTypeEx(&strv, &v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
714 if(FAILED(hres))
715 return hres;
717 *str = jsstr_alloc(V_BSTR(&strv));
718 SysFreeString(V_BSTR(&strv));
721 return *str ? S_OK : E_OUTOFMEMORY;
724 /* ECMA-262 3rd Edition 9.8 */
725 HRESULT to_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str)
727 const WCHAR nullW[] = {'n','u','l','l',0};
728 const WCHAR trueW[] = {'t','r','u','e',0};
729 const WCHAR falseW[] = {'f','a','l','s','e',0};
731 switch(jsval_type(val)) {
732 case JSV_UNDEFINED:
733 *str = jsstr_undefined();
734 return S_OK;
735 case JSV_NULL:
736 *str = jsstr_alloc(nullW);
737 break;
738 case JSV_NUMBER:
739 return double_to_string(get_number(val), str);
740 case JSV_STRING:
741 *str = jsstr_addref(get_string(val));
742 break;
743 case JSV_OBJECT: {
744 jsval_t prim;
745 HRESULT hres;
747 hres = to_primitive(ctx, val, &prim, HINT_STRING);
748 if(FAILED(hres))
749 return hres;
751 hres = to_string(ctx, prim, str);
752 jsval_release(prim);
753 return hres;
755 case JSV_BOOL:
756 *str = jsstr_alloc(get_bool(val) ? trueW : falseW);
757 break;
758 default:
759 FIXME("unsupported %s\n", debugstr_jsval(val));
760 return E_NOTIMPL;
763 return *str ? S_OK : E_OUTOFMEMORY;
766 HRESULT to_flat_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str, const WCHAR **ret_str)
768 HRESULT hres;
770 hres = to_string(ctx, val, str);
771 if(FAILED(hres))
772 return hres;
774 *ret_str = jsstr_flatten(*str);
775 if(!*ret_str) {
776 jsstr_release(*str);
777 return E_OUTOFMEMORY;
780 return S_OK;
783 /* ECMA-262 3rd Edition 9.9 */
784 HRESULT to_object(script_ctx_t *ctx, jsval_t val, IDispatch **disp)
786 jsdisp_t *dispex;
787 HRESULT hres;
789 switch(jsval_type(val)) {
790 case JSV_STRING:
791 hres = create_string(ctx, get_string(val), &dispex);
792 if(FAILED(hres))
793 return hres;
795 *disp = to_disp(dispex);
796 break;
797 case JSV_NUMBER:
798 hres = create_number(ctx, get_number(val), &dispex);
799 if(FAILED(hres))
800 return hres;
802 *disp = to_disp(dispex);
803 break;
804 case JSV_OBJECT:
805 if(get_object(val)) {
806 *disp = get_object(val);
807 IDispatch_AddRef(*disp);
808 }else {
809 jsdisp_t *obj;
811 hres = create_object(ctx, NULL, &obj);
812 if(FAILED(hres))
813 return hres;
815 *disp = to_disp(obj);
817 break;
818 case JSV_BOOL:
819 hres = create_bool(ctx, get_bool(val), &dispex);
820 if(FAILED(hres))
821 return hres;
823 *disp = to_disp(dispex);
824 break;
825 case JSV_UNDEFINED:
826 case JSV_NULL:
827 WARN("object expected\n");
828 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
829 case JSV_VARIANT:
830 switch(V_VT(get_variant(val))) {
831 case VT_ARRAY|VT_VARIANT:
832 hres = create_vbarray(ctx, V_ARRAY(get_variant(val)), &dispex);
833 if(FAILED(hres))
834 return hres;
836 *disp = to_disp(dispex);
837 break;
839 default:
840 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val)));
841 return E_NOTIMPL;
843 break;
846 return S_OK;
849 HRESULT variant_change_type(script_ctx_t *ctx, VARIANT *dst, VARIANT *src, VARTYPE vt)
851 jsval_t val;
852 HRESULT hres;
854 clear_ei(ctx);
855 hres = variant_to_jsval(src, &val);
856 if(FAILED(hres))
857 return hres;
859 switch(vt) {
860 case VT_I2:
861 case VT_I4: {
862 INT i;
864 hres = to_int32(ctx, val, &i);
865 if(SUCCEEDED(hres)) {
866 if(vt == VT_I4)
867 V_I4(dst) = i;
868 else
869 V_I2(dst) = i;
871 break;
873 case VT_R8: {
874 double n;
875 hres = to_number(ctx, val, &n);
876 if(SUCCEEDED(hres))
877 V_R8(dst) = n;
878 break;
880 case VT_R4: {
881 double n;
883 hres = to_number(ctx, val, &n);
884 if(SUCCEEDED(hres))
885 V_R4(dst) = n;
886 break;
888 case VT_BOOL: {
889 BOOL b;
891 hres = to_boolean(val, &b);
892 if(SUCCEEDED(hres))
893 V_BOOL(dst) = b ? VARIANT_TRUE : VARIANT_FALSE;
894 break;
896 case VT_BSTR: {
897 jsstr_t *str;
899 hres = to_string(ctx, val, &str);
900 if(FAILED(hres))
901 break;
903 if(is_null_bstr(str)) {
904 V_BSTR(dst) = NULL;
905 break;
908 V_BSTR(dst) = SysAllocStringLen(NULL, jsstr_length(str));
909 if(V_BSTR(dst))
910 jsstr_flush(str, V_BSTR(dst));
911 else
912 hres = E_OUTOFMEMORY;
913 break;
915 case VT_EMPTY:
916 hres = V_VT(src) == VT_EMPTY ? S_OK : E_NOTIMPL;
917 break;
918 case VT_NULL:
919 hres = V_VT(src) == VT_NULL ? S_OK : E_NOTIMPL;
920 break;
921 default:
922 FIXME("vt %d not implemented\n", vt);
923 hres = E_NOTIMPL;
926 jsval_release(val);
927 if(FAILED(hres))
928 return hres;
930 V_VT(dst) = vt;
931 return S_OK;
934 static inline JSCaller *impl_from_IServiceProvider(IServiceProvider *iface)
936 return CONTAINING_RECORD(iface, JSCaller, IServiceProvider_iface);
939 static HRESULT WINAPI JSCaller_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
941 JSCaller *This = impl_from_IServiceProvider(iface);
943 if(IsEqualGUID(&IID_IUnknown, riid)) {
944 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
945 *ppv = &This->IServiceProvider_iface;
946 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
947 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
948 *ppv = &This->IServiceProvider_iface;
949 }else {
950 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
951 *ppv = NULL;
952 return E_NOINTERFACE;
955 IUnknown_AddRef((IUnknown*)*ppv);
956 return S_OK;
959 static ULONG WINAPI JSCaller_AddRef(IServiceProvider *iface)
961 JSCaller *This = impl_from_IServiceProvider(iface);
962 LONG ref = InterlockedIncrement(&This->ref);
964 TRACE("(%p) ref=%d\n", This, ref);
966 return ref;
969 static ULONG WINAPI JSCaller_Release(IServiceProvider *iface)
971 JSCaller *This = impl_from_IServiceProvider(iface);
972 LONG ref = InterlockedIncrement(&This->ref);
974 TRACE("(%p) ref=%d\n", This, ref);
976 if(!ref) {
977 assert(!This->ctx);
978 heap_free(This);
981 return ref;
984 static HRESULT WINAPI JSCaller_QueryService(IServiceProvider *iface, REFGUID guidService,
985 REFIID riid, void **ppv)
987 JSCaller *This = impl_from_IServiceProvider(iface);
989 if(IsEqualGUID(guidService, &SID_VariantConversion) && This->ctx && This->ctx->active_script) {
990 TRACE("(%p)->(SID_VariantConversion)\n", This);
991 return IActiveScript_QueryInterface(This->ctx->active_script, riid, ppv);
994 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
996 *ppv = NULL;
997 return E_NOINTERFACE;
1000 static const IServiceProviderVtbl ServiceProviderVtbl = {
1001 JSCaller_QueryInterface,
1002 JSCaller_AddRef,
1003 JSCaller_Release,
1004 JSCaller_QueryService
1007 HRESULT create_jscaller(script_ctx_t *ctx)
1009 JSCaller *ret;
1011 ret = heap_alloc(sizeof(*ret));
1012 if(!ret)
1013 return E_OUTOFMEMORY;
1015 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
1016 ret->ref = 1;
1017 ret->ctx = ctx;
1019 ctx->jscaller = ret;
1020 return S_OK;