ntdll: Fix SMT CPU flag reporting.
[wine/zf.git] / dlls / jscript / jsutils.c
blobb12addda794ebc241c4e555b0e012f1d1b03245c
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
20 #include <math.h>
21 #include <assert.h>
23 #include "jscript.h"
24 #include "engine.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29 WINE_DECLARE_DEBUG_CHANNEL(heap);
31 const char *debugstr_jsval(const jsval_t v)
33 switch(jsval_type(v)) {
34 case JSV_UNDEFINED:
35 return "undefined";
36 case JSV_NULL:
37 return "null";
38 case JSV_OBJECT:
39 return wine_dbg_sprintf("obj(%p)", get_object(v));
40 case JSV_STRING:
41 return wine_dbg_sprintf("str(%s)", debugstr_jsstr(get_string(v)));
42 case JSV_NUMBER:
43 return wine_dbg_sprintf("%lf", get_number(v));
44 case JSV_BOOL:
45 return get_bool(v) ? "true" : "false";
46 case JSV_VARIANT:
47 return debugstr_variant(get_variant(v));
50 assert(0);
51 return NULL;
54 #define MIN_BLOCK_SIZE 128
55 #define ARENA_FREE_FILLER 0xaa
57 static inline DWORD block_size(DWORD block)
59 return MIN_BLOCK_SIZE << block;
62 void heap_pool_init(heap_pool_t *heap)
64 memset(heap, 0, sizeof(*heap));
65 list_init(&heap->custom_blocks);
68 void *heap_pool_alloc(heap_pool_t *heap, DWORD size)
70 struct list *list;
71 void *tmp;
73 if(!heap->block_cnt) {
74 if(!heap->blocks) {
75 heap->blocks = heap_alloc(sizeof(void*));
76 if(!heap->blocks)
77 return NULL;
80 tmp = heap_alloc(block_size(0));
81 if(!tmp)
82 return NULL;
84 heap->blocks[0] = tmp;
85 heap->block_cnt = 1;
88 if(heap->offset + size <= block_size(heap->last_block)) {
89 tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
90 heap->offset += size;
91 return tmp;
94 if(size <= block_size(heap->last_block+1)) {
95 if(heap->last_block+1 == heap->block_cnt) {
96 tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
97 if(!tmp)
98 return NULL;
100 heap->blocks = tmp;
101 heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
102 if(!heap->blocks[heap->block_cnt])
103 return NULL;
105 heap->block_cnt++;
108 heap->last_block++;
109 heap->offset = size;
110 return heap->blocks[heap->last_block];
113 list = heap_alloc(size + sizeof(struct list));
114 if(!list)
115 return NULL;
117 list_add_head(&heap->custom_blocks, list);
118 return list+1;
121 void *heap_pool_grow(heap_pool_t *heap, void *mem, DWORD size, DWORD inc)
123 void *ret;
125 if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
126 && heap->offset+inc < block_size(heap->last_block)) {
127 heap->offset += inc;
128 return mem;
131 ret = heap_pool_alloc(heap, size+inc);
132 if(ret) /* FIXME: avoid copying for custom blocks */
133 memcpy(ret, mem, size);
134 return ret;
137 void heap_pool_clear(heap_pool_t *heap)
139 struct list *tmp;
141 if(!heap)
142 return;
144 while((tmp = list_head(&heap->custom_blocks))) {
145 list_remove(tmp);
146 heap_free(tmp);
149 if(WARN_ON(heap)) {
150 DWORD i;
152 for(i=0; i < heap->block_cnt; i++)
153 memset(heap->blocks[i], ARENA_FREE_FILLER, block_size(i));
156 heap->last_block = heap->offset = 0;
157 heap->mark = FALSE;
160 void heap_pool_free(heap_pool_t *heap)
162 DWORD i;
164 heap_pool_clear(heap);
166 for(i=0; i < heap->block_cnt; i++)
167 heap_free(heap->blocks[i]);
168 heap_free(heap->blocks);
170 heap_pool_init(heap);
173 heap_pool_t *heap_pool_mark(heap_pool_t *heap)
175 if(heap->mark)
176 return NULL;
178 heap->mark = TRUE;
179 return heap;
182 void jsval_release(jsval_t val)
184 switch(jsval_type(val)) {
185 case JSV_OBJECT:
186 if(get_object(val))
187 IDispatch_Release(get_object(val));
188 break;
189 case JSV_STRING:
190 jsstr_release(get_string(val));
191 break;
192 case JSV_VARIANT:
193 VariantClear(get_variant(val));
194 heap_free(get_variant(val));
195 break;
196 default:
197 break;
201 static HRESULT jsval_variant(jsval_t *val, VARIANT *var)
203 VARIANT *v;
204 HRESULT hres;
206 __JSVAL_TYPE(*val) = JSV_VARIANT;
207 __JSVAL_VAR(*val) = v = heap_alloc(sizeof(VARIANT));
208 if(!v) {
209 *val = jsval_undefined();
210 return E_OUTOFMEMORY;
213 V_VT(v) = VT_EMPTY;
214 hres = VariantCopy(v, var);
215 if(FAILED(hres)) {
216 *val = jsval_undefined();
217 heap_free(v);
219 return hres;
222 HRESULT jsval_copy(jsval_t v, jsval_t *r)
224 switch(jsval_type(v)) {
225 case JSV_UNDEFINED:
226 case JSV_NULL:
227 case JSV_NUMBER:
228 case JSV_BOOL:
229 *r = v;
230 return S_OK;
231 case JSV_OBJECT:
232 if(get_object(v))
233 IDispatch_AddRef(get_object(v));
234 *r = v;
235 return S_OK;
236 case JSV_STRING: {
237 jsstr_addref(get_string(v));
238 *r = v;
239 return S_OK;
241 case JSV_VARIANT:
242 return jsval_variant(r, get_variant(v));
245 assert(0);
246 return E_FAIL;
249 HRESULT variant_to_jsval(VARIANT *var, jsval_t *r)
251 if(V_VT(var) == (VT_VARIANT|VT_BYREF))
252 var = V_VARIANTREF(var);
254 switch(V_VT(var)) {
255 case VT_EMPTY:
256 *r = jsval_undefined();
257 return S_OK;
258 case VT_NULL:
259 *r = jsval_null();
260 return S_OK;
261 case VT_BOOL:
262 *r = jsval_bool(V_BOOL(var));
263 return S_OK;
264 case VT_I4:
265 *r = jsval_number(V_I4(var));
266 return S_OK;
267 case VT_R8:
268 *r = jsval_number(V_R8(var));
269 return S_OK;
270 case VT_BSTR: {
271 jsstr_t *str;
273 if(V_BSTR(var)) {
274 str = jsstr_alloc_len(V_BSTR(var), SysStringLen(V_BSTR(var)));
275 if(!str)
276 return E_OUTOFMEMORY;
277 }else {
278 str = jsstr_null_bstr();
281 *r = jsval_string(str);
282 return S_OK;
284 case VT_DISPATCH: {
285 if(V_DISPATCH(var))
286 IDispatch_AddRef(V_DISPATCH(var));
287 *r = jsval_disp(V_DISPATCH(var));
288 return S_OK;
290 case VT_I1:
291 *r = jsval_number(V_I1(var));
292 return S_OK;
293 case VT_UI1:
294 *r = jsval_number(V_UI1(var));
295 return S_OK;
296 case VT_I2:
297 *r = jsval_number(V_I2(var));
298 return S_OK;
299 case VT_UI2:
300 *r = jsval_number(V_UI2(var));
301 return S_OK;
302 case VT_INT:
303 *r = jsval_number(V_INT(var));
304 return S_OK;
305 case VT_UI4:
306 *r = jsval_number(V_UI4(var));
307 return S_OK;
308 case VT_UI8:
310 * Native doesn't support VT_UI8 here, but it's needed for IE9+ APIs
311 * (native IE9 doesn't use jscript.dll for JavaScript).
313 *r = jsval_number(V_UI8(var));
314 return S_OK;
315 case VT_R4:
316 *r = jsval_number(V_R4(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;
328 }else {
329 *r = jsval_disp(NULL);
330 return S_OK;
332 /* fall through */
333 default:
334 return jsval_variant(r, var);
338 HRESULT jsval_to_variant(jsval_t val, VARIANT *retv)
340 switch(jsval_type(val)) {
341 case JSV_UNDEFINED:
342 V_VT(retv) = VT_EMPTY;
343 return S_OK;
344 case JSV_NULL:
345 V_VT(retv) = VT_NULL;
346 return S_OK;
347 case JSV_OBJECT:
348 V_VT(retv) = VT_DISPATCH;
349 if(get_object(val))
350 IDispatch_AddRef(get_object(val));
351 V_DISPATCH(retv) = get_object(val);
352 return S_OK;
353 case JSV_STRING:
354 V_VT(retv) = VT_BSTR;
355 return jsstr_to_bstr(get_string(val), &V_BSTR(retv));
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 JS_E_TO_PRIMITIVE;
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(iswspace(*ptr))
507 ptr++;
509 if(*ptr == '-') {
510 neg = TRUE;
511 ptr++;
512 }else if(*ptr == '+') {
513 ptr++;
516 if(!wcsncmp(ptr, infinityW, ARRAY_SIZE(infinityW))) {
517 ptr += ARRAY_SIZE(infinityW);
518 while(*ptr && iswspace(*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(is_digit(*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(is_digit(*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(is_digit(*ptr)) {
567 d += dec * (*ptr++ - '0');
568 dec *= 0.1;
572 while(iswspace(*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 static INT32 double_to_int32(double number)
645 INT32 exp, result;
646 union {
647 double d;
648 INT64 n;
649 } bits;
651 bits.d = number;
652 exp = ((INT32)(bits.n >> 52) & 0x7ff) - 0x3ff;
654 /* If exponent < 0 there will be no bits to the left of the decimal point
655 * after rounding; if the exponent is > 83 then no bits of precision can be
656 * left in the low 32-bit range of the result (IEEE-754 doubles have 52 bits
657 * of fractional precision).
658 * Note this case handles 0, -0, and all infinite, NaN & denormal values. */
659 if(exp < 0 || exp > 83)
660 return 0;
662 /* Select the appropriate 32-bits from the floating point mantissa. If the
663 * exponent is 52 then the bits we need to select are already aligned to the
664 * lowest bits of the 64-bit integer representation of the number, no need
665 * to shift. If the exponent is greater than 52 we need to shift the value
666 * left by (exp - 52), if the value is less than 52 we need to shift right
667 * accordingly. */
668 result = (exp > 52) ? bits.n << (exp - 52) : bits.n >> (52 - exp);
670 /* IEEE-754 double precision values are stored omitting an implicit 1 before
671 * the decimal point; we need to reinsert this now. We may also the shifted
672 * invalid bits into the result that are not a part of the mantissa (the sign
673 * and exponent bits from the floatingpoint representation); mask these out. */
674 if(exp < 32) {
675 INT32 missing_one = 1 << exp;
676 result &= missing_one - 1;
677 result += missing_one;
680 /* If the input value was negative (we could test either 'number' or 'bits',
681 * but testing 'bits' is likely faster) invert the result appropriately. */
682 return bits.n < 0 ? -result : result;
685 /* ECMA-262 3rd Edition 9.5 */
686 HRESULT to_int32(script_ctx_t *ctx, jsval_t v, INT *ret)
688 double n;
689 HRESULT hres;
691 hres = to_number(ctx, v, &n);
692 if(FAILED(hres))
693 return hres;
695 *ret = double_to_int32(n);
696 return S_OK;
699 /* ECMA-262 3rd Edition 9.6 */
700 HRESULT to_uint32(script_ctx_t *ctx, jsval_t val, UINT32 *ret)
702 double n;
703 HRESULT hres;
705 hres = to_number(ctx, val, &n);
706 if(FAILED(hres))
707 return hres;
709 *ret = double_to_int32(n);
710 return S_OK;
713 HRESULT double_to_string(double n, jsstr_t **str)
715 static const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};
717 if(isnan(n)) {
718 *str = jsstr_nan();
719 }else if(isinf(n)) {
720 *str = jsstr_alloc(n<0 ? InfinityW : InfinityW+1);
721 }else if(is_int32(n)) {
722 WCHAR buf[12];
723 _ltow_s(n, buf, ARRAY_SIZE(buf), 10);
724 *str = jsstr_alloc(buf);
725 }else {
726 VARIANT strv, v;
727 HRESULT hres;
729 /* FIXME: Don't use VariantChangeTypeEx */
730 V_VT(&v) = VT_R8;
731 V_R8(&v) = n;
732 V_VT(&strv) = VT_EMPTY;
733 hres = VariantChangeTypeEx(&strv, &v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
734 if(FAILED(hres))
735 return hres;
737 *str = jsstr_alloc(V_BSTR(&strv));
738 SysFreeString(V_BSTR(&strv));
741 return *str ? S_OK : E_OUTOFMEMORY;
744 /* ECMA-262 3rd Edition 9.8 */
745 HRESULT to_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str)
747 static const WCHAR nullW[] = {'n','u','l','l',0};
748 static const WCHAR trueW[] = {'t','r','u','e',0};
749 static const WCHAR falseW[] = {'f','a','l','s','e',0};
751 switch(jsval_type(val)) {
752 case JSV_UNDEFINED:
753 *str = jsstr_undefined();
754 return S_OK;
755 case JSV_NULL:
756 *str = jsstr_alloc(nullW);
757 break;
758 case JSV_NUMBER:
759 return double_to_string(get_number(val), str);
760 case JSV_STRING:
761 *str = jsstr_addref(get_string(val));
762 break;
763 case JSV_OBJECT: {
764 jsval_t prim;
765 HRESULT hres;
767 hres = to_primitive(ctx, val, &prim, HINT_STRING);
768 if(FAILED(hres))
769 return hres;
771 hres = to_string(ctx, prim, str);
772 jsval_release(prim);
773 return hres;
775 case JSV_BOOL:
776 *str = jsstr_alloc(get_bool(val) ? trueW : falseW);
777 break;
778 default:
779 FIXME("unsupported %s\n", debugstr_jsval(val));
780 return E_NOTIMPL;
783 return *str ? S_OK : E_OUTOFMEMORY;
786 HRESULT to_flat_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str, const WCHAR **ret_str)
788 HRESULT hres;
790 hres = to_string(ctx, val, str);
791 if(FAILED(hres))
792 return hres;
794 *ret_str = jsstr_flatten(*str);
795 if(!*ret_str) {
796 jsstr_release(*str);
797 return E_OUTOFMEMORY;
800 return S_OK;
803 /* ECMA-262 3rd Edition 9.9 */
804 HRESULT to_object(script_ctx_t *ctx, jsval_t val, IDispatch **disp)
806 jsdisp_t *dispex;
807 HRESULT hres;
809 switch(jsval_type(val)) {
810 case JSV_STRING:
811 hres = create_string(ctx, get_string(val), &dispex);
812 if(FAILED(hres))
813 return hres;
815 *disp = to_disp(dispex);
816 break;
817 case JSV_NUMBER:
818 hres = create_number(ctx, get_number(val), &dispex);
819 if(FAILED(hres))
820 return hres;
822 *disp = to_disp(dispex);
823 break;
824 case JSV_OBJECT:
825 if(get_object(val)) {
826 *disp = get_object(val);
827 IDispatch_AddRef(*disp);
828 }else {
829 jsdisp_t *obj;
831 hres = create_object(ctx, NULL, &obj);
832 if(FAILED(hres))
833 return hres;
835 *disp = to_disp(obj);
837 break;
838 case JSV_BOOL:
839 hres = create_bool(ctx, get_bool(val), &dispex);
840 if(FAILED(hres))
841 return hres;
843 *disp = to_disp(dispex);
844 break;
845 case JSV_UNDEFINED:
846 case JSV_NULL:
847 WARN("object expected\n");
848 return JS_E_OBJECT_EXPECTED;
849 case JSV_VARIANT:
850 switch(V_VT(get_variant(val))) {
851 case VT_ARRAY|VT_VARIANT:
852 hres = create_vbarray(ctx, V_ARRAY(get_variant(val)), &dispex);
853 if(FAILED(hres))
854 return hres;
856 *disp = to_disp(dispex);
857 break;
859 default:
860 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val)));
861 return E_NOTIMPL;
863 break;
866 return S_OK;
869 HRESULT variant_change_type(script_ctx_t *ctx, VARIANT *dst, VARIANT *src, VARTYPE vt)
871 jsexcept_t ei;
872 jsval_t val;
873 HRESULT hres;
875 hres = variant_to_jsval(src, &val);
876 if(FAILED(hres))
877 return hres;
879 enter_script(ctx, &ei);
881 switch(vt) {
882 case VT_I2:
883 case VT_I4: {
884 INT i;
886 hres = to_int32(ctx, val, &i);
887 if(SUCCEEDED(hres)) {
888 if(vt == VT_I4)
889 V_I4(dst) = i;
890 else
891 V_I2(dst) = i;
893 break;
895 case VT_UI2: {
896 UINT32 i;
898 hres = to_uint32(ctx, val, &i);
899 if(SUCCEEDED(hres))
900 V_UI2(dst) = i;
901 break;
903 case VT_R8: {
904 double n;
905 hres = to_number(ctx, val, &n);
906 if(SUCCEEDED(hres))
907 V_R8(dst) = n;
908 break;
910 case VT_R4: {
911 double n;
913 hres = to_number(ctx, val, &n);
914 if(SUCCEEDED(hres))
915 V_R4(dst) = n;
916 break;
918 case VT_BOOL: {
919 BOOL b;
921 hres = to_boolean(val, &b);
922 if(SUCCEEDED(hres))
923 V_BOOL(dst) = b ? VARIANT_TRUE : VARIANT_FALSE;
924 break;
926 case VT_BSTR: {
927 jsstr_t *str;
929 hres = to_string(ctx, val, &str);
930 if(FAILED(hres))
931 break;
933 hres = jsstr_to_bstr(str, &V_BSTR(dst));
934 break;
936 case VT_EMPTY:
937 hres = V_VT(src) == VT_EMPTY ? S_OK : E_NOTIMPL;
938 break;
939 case VT_NULL:
940 hres = V_VT(src) == VT_NULL ? S_OK : E_NOTIMPL;
941 break;
942 default:
943 FIXME("vt %d not implemented\n", vt);
944 hres = E_NOTIMPL;
947 jsval_release(val);
948 leave_script(ctx, hres);
949 if(FAILED(hres))
950 return hres;
952 V_VT(dst) = vt;
953 return S_OK;
956 static inline JSCaller *impl_from_IServiceProvider(IServiceProvider *iface)
958 return CONTAINING_RECORD(iface, JSCaller, IServiceProvider_iface);
961 static HRESULT WINAPI JSCaller_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
963 JSCaller *This = impl_from_IServiceProvider(iface);
965 if(IsEqualGUID(&IID_IUnknown, riid)) {
966 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
967 *ppv = &This->IServiceProvider_iface;
968 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
969 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
970 *ppv = &This->IServiceProvider_iface;
971 }else {
972 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
973 *ppv = NULL;
974 return E_NOINTERFACE;
977 IUnknown_AddRef((IUnknown*)*ppv);
978 return S_OK;
981 static ULONG WINAPI JSCaller_AddRef(IServiceProvider *iface)
983 JSCaller *This = impl_from_IServiceProvider(iface);
984 LONG ref = InterlockedIncrement(&This->ref);
986 TRACE("(%p) ref=%d\n", This, ref);
988 return ref;
991 static ULONG WINAPI JSCaller_Release(IServiceProvider *iface)
993 JSCaller *This = impl_from_IServiceProvider(iface);
994 LONG ref = InterlockedIncrement(&This->ref);
996 TRACE("(%p) ref=%d\n", This, ref);
998 if(!ref) {
999 assert(!This->ctx);
1000 heap_free(This);
1003 return ref;
1006 static HRESULT WINAPI JSCaller_QueryService(IServiceProvider *iface, REFGUID guidService,
1007 REFIID riid, void **ppv)
1009 JSCaller *This = impl_from_IServiceProvider(iface);
1011 if(IsEqualGUID(guidService, &SID_VariantConversion) && This->ctx && This->ctx->active_script) {
1012 TRACE("(%p)->(SID_VariantConversion)\n", This);
1013 return IActiveScript_QueryInterface(This->ctx->active_script, riid, ppv);
1016 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1018 *ppv = NULL;
1019 return E_NOINTERFACE;
1022 static const IServiceProviderVtbl ServiceProviderVtbl = {
1023 JSCaller_QueryInterface,
1024 JSCaller_AddRef,
1025 JSCaller_Release,
1026 JSCaller_QueryService
1029 HRESULT create_jscaller(script_ctx_t *ctx)
1031 JSCaller *ret;
1033 ret = heap_alloc(sizeof(*ret));
1034 if(!ret)
1035 return E_OUTOFMEMORY;
1037 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
1038 ret->ref = 1;
1039 ret->ctx = ctx;
1041 ctx->jscaller = ret;
1042 return S_OK;