windows.networking.hostname/tests: Add IHostNameFactory::CreateHostName() tests.
[wine.git] / dlls / jscript / jsutils.c
blob80e068fc529b9b71297d8bf9fb113121e8614d5f
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 = malloc(sizeof(void*));
76 if(!heap->blocks)
77 return NULL;
80 tmp = malloc(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 = 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] = malloc(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 = malloc(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 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 free(heap->blocks[i]);
168 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 IDispatch_Release(get_object(val));
187 break;
188 case JSV_STRING:
189 jsstr_release(get_string(val));
190 break;
191 case JSV_VARIANT:
192 VariantClear(get_variant(val));
193 free(get_variant(val));
194 break;
195 default:
196 break;
200 static HRESULT jsval_variant(jsval_t *val, VARIANT *var)
202 VARIANT *v;
203 HRESULT hres;
205 __JSVAL_TYPE(*val) = JSV_VARIANT;
206 __JSVAL_VAR(*val) = v = malloc(sizeof(VARIANT));
207 if(!v) {
208 *val = jsval_undefined();
209 return E_OUTOFMEMORY;
212 V_VT(v) = VT_EMPTY;
213 hres = VariantCopy(v, var);
214 if(FAILED(hres)) {
215 *val = jsval_undefined();
216 free(v);
218 return hres;
221 HRESULT jsval_copy(jsval_t v, jsval_t *r)
223 switch(jsval_type(v)) {
224 case JSV_UNDEFINED:
225 case JSV_NULL:
226 case JSV_NUMBER:
227 case JSV_BOOL:
228 *r = v;
229 return S_OK;
230 case JSV_OBJECT:
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(script_ctx_t *ctx, 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 *r = ctx->html_mode ? jsval_null() : jsval_null_disp();
285 return S_OK;
287 IDispatch_AddRef(V_DISPATCH(var));
288 *r = jsval_disp(V_DISPATCH(var));
289 return S_OK;
291 case VT_I1:
292 *r = jsval_number(V_I1(var));
293 return S_OK;
294 case VT_UI1:
295 *r = jsval_number(V_UI1(var));
296 return S_OK;
297 case VT_I2:
298 *r = jsval_number(V_I2(var));
299 return S_OK;
300 case VT_UI2:
301 *r = jsval_number(V_UI2(var));
302 return S_OK;
303 case VT_INT:
304 *r = jsval_number(V_INT(var));
305 return S_OK;
306 case VT_UI4:
307 *r = jsval_number(V_UI4(var));
308 return S_OK;
309 case VT_UI8:
311 * Native doesn't support VT_UI8 here, but it's needed for IE9+ APIs
312 * (native IE9 doesn't use jscript.dll for JavaScript).
314 *r = jsval_number(V_UI8(var));
315 return S_OK;
316 case VT_R4:
317 *r = jsval_number(V_R4(var));
318 return S_OK;
319 case VT_CY:
320 /* FIXME: Native converts VT_CY to a special kind number type, which is
321 * never converted to VT_I4 when it's converted back to VARIANT. */
322 *r = jsval_number((double)V_CY(var).int64 / 10000.0);
323 WARN("VT_CY: %lf\n", get_number(*r));
324 return S_OK;
325 case VT_UNKNOWN:
326 if(V_UNKNOWN(var)) {
327 IDispatch *disp;
328 HRESULT hres;
330 hres = IUnknown_QueryInterface(V_UNKNOWN(var), &IID_IDispatch, (void**)&disp);
331 if(SUCCEEDED(hres)) {
332 *r = jsval_disp(disp);
333 return S_OK;
335 }else {
336 *r = ctx->html_mode ? jsval_null() : jsval_null_disp();
337 return S_OK;
339 /* fall through */
340 default:
341 return jsval_variant(r, var);
345 HRESULT jsval_to_variant(jsval_t val, VARIANT *retv)
347 switch(jsval_type(val)) {
348 case JSV_UNDEFINED:
349 V_VT(retv) = VT_EMPTY;
350 return S_OK;
351 case JSV_NULL:
352 if(get_bool(val)) {
353 V_VT(retv) = VT_DISPATCH;
354 V_DISPATCH(retv) = NULL;
355 return S_OK;
357 V_VT(retv) = VT_NULL;
358 return S_OK;
359 case JSV_OBJECT:
360 V_VT(retv) = VT_DISPATCH;
361 V_DISPATCH(retv) = get_object(val);
362 IDispatch_AddRef(get_object(val));
363 return S_OK;
364 case JSV_STRING:
365 V_VT(retv) = VT_BSTR;
366 return jsstr_to_bstr(get_string(val), &V_BSTR(retv));
367 case JSV_NUMBER: {
368 double n = get_number(val);
370 if(is_int32(n)) {
371 V_VT(retv) = VT_I4;
372 V_I4(retv) = n;
373 }else {
374 V_VT(retv) = VT_R8;
375 V_R8(retv) = n;
378 return S_OK;
380 case JSV_BOOL:
381 V_VT(retv) = VT_BOOL;
382 V_BOOL(retv) = get_bool(val) ? VARIANT_TRUE : VARIANT_FALSE;
383 return S_OK;
384 case JSV_VARIANT:
385 V_VT(retv) = VT_EMPTY;
386 return VariantCopy(retv, get_variant(val));
389 assert(0);
390 return E_FAIL;
393 /* ECMA-262 3rd Edition 9.1 */
394 HRESULT to_primitive(script_ctx_t *ctx, jsval_t val, jsval_t *ret, hint_t hint)
396 if(is_object_instance(val)) {
397 jsdisp_t *jsdisp;
398 jsval_t prim;
399 DISPID id;
400 HRESULT hres;
402 jsdisp = iface_to_jsdisp(get_object(val));
403 if(!jsdisp)
404 return disp_propget(ctx, get_object(val), DISPID_VALUE, ret);
406 if(hint == NO_HINT)
407 hint = is_class(jsdisp, JSCLASS_DATE) ? HINT_STRING : HINT_NUMBER;
409 /* Native implementation doesn't throw TypeErrors, returns strange values */
411 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? L"toString" : L"valueOf", 0, &id);
412 if(SUCCEEDED(hres)) {
413 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
414 if(FAILED(hres)) {
415 WARN("call error - forwarding exception\n");
416 jsdisp_release(jsdisp);
417 return hres;
418 }else if(!is_object_instance(prim)) {
419 jsdisp_release(jsdisp);
420 *ret = prim;
421 return S_OK;
422 }else {
423 IDispatch_Release(get_object(prim));
425 }else if(hres != DISP_E_UNKNOWNNAME) {
426 jsdisp_release(jsdisp);
427 return hres;
430 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? L"valueOf" : L"toString", 0, &id);
431 if(SUCCEEDED(hres)) {
432 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
433 if(FAILED(hres)) {
434 WARN("call error - forwarding exception\n");
435 jsdisp_release(jsdisp);
436 return hres;
437 }else if(!is_object_instance(prim)) {
438 jsdisp_release(jsdisp);
439 *ret = prim;
440 return S_OK;
441 }else {
442 IDispatch_Release(get_object(prim));
444 }else if(hres != DISP_E_UNKNOWNNAME) {
445 jsdisp_release(jsdisp);
446 return hres;
449 jsdisp_release(jsdisp);
451 WARN("failed\n");
452 return JS_E_TO_PRIMITIVE;
455 return jsval_copy(val, ret);
459 /* ECMA-262 3rd Edition 9.2 */
460 HRESULT to_boolean(jsval_t val, BOOL *ret)
462 switch(jsval_type(val)) {
463 case JSV_UNDEFINED:
464 case JSV_NULL:
465 *ret = FALSE;
466 return S_OK;
467 case JSV_OBJECT:
468 *ret = TRUE;
469 return S_OK;
470 case JSV_STRING:
471 *ret = jsstr_length(get_string(val)) != 0;
472 return S_OK;
473 case JSV_NUMBER:
474 *ret = !isnan(get_number(val)) && get_number(val);
475 return S_OK;
476 case JSV_BOOL:
477 *ret = get_bool(val);
478 return S_OK;
479 case JSV_VARIANT:
480 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
481 return E_NOTIMPL;
484 assert(0);
485 return E_FAIL;
488 static int hex_to_int(WCHAR c)
490 if('0' <= c && c <= '9')
491 return c-'0';
493 if('a' <= c && c <= 'f')
494 return c-'a'+10;
496 if('A' <= c && c <= 'F')
497 return c-'A'+10;
499 return -1;
502 /* ECMA-262 3rd Edition 9.3.1 */
503 static HRESULT str_to_number(jsstr_t *str, double *ret)
505 const WCHAR *ptr;
506 BOOL neg = FALSE;
507 DOUBLE d = 0.0;
509 static const WCHAR infinityW[] = L"Infinity";
511 ptr = jsstr_flatten(str);
512 if(!ptr)
513 return E_OUTOFMEMORY;
515 while(iswspace(*ptr))
516 ptr++;
518 if(*ptr == '-') {
519 neg = TRUE;
520 ptr++;
521 }else if(*ptr == '+') {
522 ptr++;
525 if(!wcsncmp(ptr, infinityW, ARRAY_SIZE(infinityW)-1)) {
526 ptr += ARRAY_SIZE(infinityW) - 1;
527 while(*ptr && iswspace(*ptr))
528 ptr++;
530 if(*ptr)
531 *ret = NAN;
532 else
533 *ret = neg ? -INFINITY : INFINITY;
534 return S_OK;
537 if(*ptr == '0' && ptr[1] == 'x') {
538 DWORD l = 0;
540 ptr += 2;
541 while((l = hex_to_int(*ptr)) != -1) {
542 d = d*16 + l;
543 ptr++;
546 *ret = d;
547 return S_OK;
550 while(is_digit(*ptr))
551 d = d*10 + (*ptr++ - '0');
553 if(*ptr == 'e' || *ptr == 'E') {
554 BOOL eneg = FALSE;
555 LONG l = 0;
557 ptr++;
558 if(*ptr == '-') {
559 ptr++;
560 eneg = TRUE;
561 }else if(*ptr == '+') {
562 ptr++;
565 while(is_digit(*ptr))
566 l = l*10 + (*ptr++ - '0');
567 if(eneg)
568 l = -l;
570 d *= pow(10, l);
571 }else if(*ptr == '.') {
572 DOUBLE dec = 0.1;
574 ptr++;
575 while(is_digit(*ptr)) {
576 d += dec * (*ptr++ - '0');
577 dec *= 0.1;
581 while(iswspace(*ptr))
582 ptr++;
584 if(*ptr) {
585 *ret = NAN;
586 return S_OK;
589 if(neg)
590 d = -d;
592 *ret = d;
593 return S_OK;
596 /* ECMA-262 3rd Edition 9.3 */
597 HRESULT to_number(script_ctx_t *ctx, jsval_t val, double *ret)
599 switch(jsval_type(val)) {
600 case JSV_UNDEFINED:
601 *ret = NAN;
602 return S_OK;
603 case JSV_NULL:
604 *ret = 0;
605 return S_OK;
606 case JSV_NUMBER:
607 *ret = get_number(val);
608 return S_OK;
609 case JSV_STRING:
610 return str_to_number(get_string(val), ret);
611 case JSV_OBJECT: {
612 jsval_t prim;
613 HRESULT hres;
615 hres = to_primitive(ctx, val, &prim, HINT_NUMBER);
616 if(FAILED(hres))
617 return hres;
619 hres = to_number(ctx, prim, ret);
620 jsval_release(prim);
621 return hres;
623 case JSV_BOOL:
624 *ret = get_bool(val) ? 1 : 0;
625 return S_OK;
626 case JSV_VARIANT: {
627 const VARIANT *v = get_variant(val);
628 switch(V_VT(v)) {
629 case VT_DATE:
630 return variant_date_to_number(V_DATE(v), ret);
631 default:
632 FIXME("unimplemented for variant %s\n", debugstr_variant(v));
633 return E_NOTIMPL;
638 assert(0);
639 return E_FAIL;
642 /* ECMA-262 3rd Edition 9.4 */
643 HRESULT to_integer(script_ctx_t *ctx, jsval_t v, double *ret)
645 double n;
646 HRESULT hres;
648 hres = to_number(ctx, v, &n);
649 if(FAILED(hres))
650 return hres;
652 if(isnan(n))
653 *ret = 0;
654 else
655 *ret = n >= 0.0 ? floor(n) : -floor(-n);
656 return S_OK;
659 static INT32 double_to_int32(double number)
661 INT32 exp, result;
662 union {
663 double d;
664 INT64 n;
665 } bits;
667 bits.d = number;
668 exp = ((INT32)(bits.n >> 52) & 0x7ff) - 0x3ff;
670 /* If exponent < 0 there will be no bits to the left of the decimal point
671 * after rounding; if the exponent is > 83 then no bits of precision can be
672 * left in the low 32-bit range of the result (IEEE-754 doubles have 52 bits
673 * of fractional precision).
674 * Note this case handles 0, -0, and all infinite, NaN & denormal values. */
675 if(exp < 0 || exp > 83)
676 return 0;
678 /* Select the appropriate 32-bits from the floating point mantissa. If the
679 * exponent is 52 then the bits we need to select are already aligned to the
680 * lowest bits of the 64-bit integer representation of the number, no need
681 * to shift. If the exponent is greater than 52 we need to shift the value
682 * left by (exp - 52), if the value is less than 52 we need to shift right
683 * accordingly. */
684 result = (exp > 52) ? bits.n << (exp - 52) : bits.n >> (52 - exp);
686 /* IEEE-754 double precision values are stored omitting an implicit 1 before
687 * the decimal point; we need to reinsert this now. We may also the shifted
688 * invalid bits into the result that are not a part of the mantissa (the sign
689 * and exponent bits from the floatingpoint representation); mask these out. */
690 if(exp < 32) {
691 INT32 missing_one = 1 << exp;
692 result &= missing_one - 1;
693 result += missing_one;
696 /* If the input value was negative (we could test either 'number' or 'bits',
697 * but testing 'bits' is likely faster) invert the result appropriately. */
698 return bits.n < 0 ? -result : result;
701 /* ECMA-262 3rd Edition 9.5 */
702 HRESULT to_int32(script_ctx_t *ctx, jsval_t v, INT *ret)
704 double n;
705 HRESULT hres;
707 hres = to_number(ctx, v, &n);
708 if(FAILED(hres))
709 return hres;
711 *ret = double_to_int32(n);
712 return S_OK;
715 HRESULT to_long(script_ctx_t *ctx, jsval_t v, LONG *ret)
717 return to_int32(ctx, v, (INT*)ret);
720 /* ECMA-262 3rd Edition 9.6 */
721 HRESULT to_uint32(script_ctx_t *ctx, jsval_t val, UINT32 *ret)
723 double n;
724 HRESULT hres;
726 hres = to_number(ctx, val, &n);
727 if(FAILED(hres))
728 return hres;
730 *ret = double_to_int32(n);
731 return S_OK;
734 HRESULT double_to_string(double n, jsstr_t **str)
736 if(isnan(n)) {
737 *str = jsstr_nan();
738 }else if(isinf(n)) {
739 *str = jsstr_alloc(n<0 ? L"-Infinity" : L"Infinity");
740 }else if(is_int32(n)) {
741 WCHAR buf[12];
742 _ltow_s(n, buf, ARRAY_SIZE(buf), 10);
743 *str = jsstr_alloc(buf);
744 }else {
745 VARIANT strv, v;
746 HRESULT hres;
748 /* FIXME: Don't use VariantChangeTypeEx */
749 V_VT(&v) = VT_R8;
750 V_R8(&v) = n;
751 V_VT(&strv) = VT_EMPTY;
752 hres = VariantChangeTypeEx(&strv, &v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
753 if(FAILED(hres))
754 return hres;
756 *str = jsstr_alloc(V_BSTR(&strv));
757 SysFreeString(V_BSTR(&strv));
760 return *str ? S_OK : E_OUTOFMEMORY;
763 /* ECMA-262 3rd Edition 9.8 */
764 HRESULT to_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str)
766 switch(jsval_type(val)) {
767 case JSV_UNDEFINED:
768 *str = jsstr_undefined();
769 return S_OK;
770 case JSV_NULL:
771 *str = jsstr_alloc(L"null");
772 break;
773 case JSV_NUMBER:
774 return double_to_string(get_number(val), str);
775 case JSV_STRING:
776 *str = jsstr_addref(get_string(val));
777 break;
778 case JSV_OBJECT: {
779 jsval_t prim;
780 HRESULT hres;
782 hres = to_primitive(ctx, val, &prim, HINT_STRING);
783 if(FAILED(hres))
784 return hres;
786 hres = to_string(ctx, prim, str);
787 jsval_release(prim);
788 return hres;
790 case JSV_BOOL:
791 *str = jsstr_alloc(get_bool(val) ? L"true" : L"false");
792 break;
793 default: {
794 const VARIANT *v = get_variant(val);
795 switch(V_VT(v))
797 case VT_DATE:
798 return variant_date_to_string(ctx, V_DATE(v), str);
799 default:
800 FIXME("unsupported %s\n", debugstr_variant(v));
801 return E_NOTIMPL;
806 return *str ? S_OK : E_OUTOFMEMORY;
809 HRESULT to_flat_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str, const WCHAR **ret_str)
811 HRESULT hres;
813 hres = to_string(ctx, val, str);
814 if(FAILED(hres))
815 return hres;
817 *ret_str = jsstr_flatten(*str);
818 if(!*ret_str) {
819 jsstr_release(*str);
820 return E_OUTOFMEMORY;
823 return S_OK;
826 /* ECMA-262 3rd Edition 9.9 */
827 HRESULT to_object(script_ctx_t *ctx, jsval_t val, IDispatch **disp)
829 jsdisp_t *dispex;
830 HRESULT hres;
832 switch(jsval_type(val)) {
833 case JSV_STRING:
834 hres = create_string(ctx, get_string(val), &dispex);
835 if(FAILED(hres))
836 return hres;
838 *disp = to_disp(dispex);
839 break;
840 case JSV_NUMBER:
841 hres = create_number(ctx, get_number(val), &dispex);
842 if(FAILED(hres))
843 return hres;
845 *disp = to_disp(dispex);
846 break;
847 case JSV_OBJECT:
848 *disp = get_object(val);
849 IDispatch_AddRef(*disp);
850 break;
851 case JSV_BOOL:
852 hres = create_bool(ctx, get_bool(val), &dispex);
853 if(FAILED(hres))
854 return hres;
856 *disp = to_disp(dispex);
857 break;
858 case JSV_NULL:
859 if(is_null_disp(val))
860 return JS_E_OBJECT_REQUIRED;
861 /* fall through */
862 case JSV_UNDEFINED:
863 WARN("object expected\n");
864 return JS_E_OBJECT_EXPECTED;
865 case JSV_VARIANT:
866 switch(V_VT(get_variant(val))) {
867 case VT_ARRAY|VT_VARIANT:
868 hres = create_vbarray(ctx, V_ARRAY(get_variant(val)), &dispex);
869 if(FAILED(hres))
870 return hres;
872 *disp = to_disp(dispex);
873 break;
875 default:
876 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val)));
877 return E_NOTIMPL;
879 break;
882 return S_OK;
885 HRESULT variant_change_type(script_ctx_t *ctx, VARIANT *dst, VARIANT *src, VARTYPE vt)
887 jsexcept_t ei;
888 jsval_t val;
889 HRESULT hres;
891 hres = variant_to_jsval(ctx, src, &val);
892 if(FAILED(hres))
893 return hres;
895 enter_script(ctx, &ei);
897 switch(vt) {
898 case VT_I2:
899 case VT_I4: {
900 INT i;
902 hres = to_int32(ctx, val, &i);
903 if(SUCCEEDED(hres)) {
904 if(vt == VT_I4)
905 V_I4(dst) = i;
906 else
907 V_I2(dst) = i;
909 break;
911 case VT_UI2: {
912 UINT32 i;
914 hres = to_uint32(ctx, val, &i);
915 if(SUCCEEDED(hres))
916 V_UI2(dst) = i;
917 break;
919 case VT_R8: {
920 double n;
921 hres = to_number(ctx, val, &n);
922 if(SUCCEEDED(hres))
923 V_R8(dst) = n;
924 break;
926 case VT_R4: {
927 double n;
929 hres = to_number(ctx, val, &n);
930 if(SUCCEEDED(hres))
931 V_R4(dst) = n;
932 break;
934 case VT_BOOL: {
935 BOOL b;
937 hres = to_boolean(val, &b);
938 if(SUCCEEDED(hres))
939 V_BOOL(dst) = b ? VARIANT_TRUE : VARIANT_FALSE;
940 break;
942 case VT_BSTR: {
943 jsstr_t *str;
945 hres = to_string(ctx, val, &str);
946 if(FAILED(hres))
947 break;
949 hres = jsstr_to_bstr(str, &V_BSTR(dst));
950 jsstr_release(str);
951 break;
953 case VT_EMPTY:
954 hres = V_VT(src) == VT_EMPTY ? S_OK : E_NOTIMPL;
955 break;
956 case VT_NULL:
957 hres = V_VT(src) == VT_NULL ? S_OK : E_NOTIMPL;
958 break;
959 case VT_UNKNOWN:
960 case VT_DISPATCH:
961 if(V_VT(src) != vt)
962 hres = E_NOTIMPL;
963 else {
964 IUnknown_AddRef(V_UNKNOWN(src));
965 V_UNKNOWN(dst) = V_UNKNOWN(src);
966 hres = S_OK;
968 break;
969 default:
970 FIXME("vt %d not implemented\n", vt);
971 hres = E_NOTIMPL;
974 jsval_release(val);
975 leave_script(ctx, hres);
976 if(FAILED(hres))
977 return hres;
979 V_VT(dst) = vt;
980 return S_OK;
983 static inline JSCaller *impl_from_IServiceProvider(IServiceProvider *iface)
985 return CONTAINING_RECORD(iface, JSCaller, IServiceProvider_iface);
988 static HRESULT WINAPI JSCaller_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
990 JSCaller *This = impl_from_IServiceProvider(iface);
992 if(IsEqualGUID(&IID_IUnknown, riid)) {
993 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
994 *ppv = &This->IServiceProvider_iface;
995 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
996 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
997 *ppv = &This->IServiceProvider_iface;
998 }else {
999 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
1000 *ppv = NULL;
1001 return E_NOINTERFACE;
1004 IUnknown_AddRef((IUnknown*)*ppv);
1005 return S_OK;
1008 static ULONG WINAPI JSCaller_AddRef(IServiceProvider *iface)
1010 JSCaller *This = impl_from_IServiceProvider(iface);
1011 LONG ref = InterlockedIncrement(&This->ref);
1013 TRACE("(%p) ref=%ld\n", This, ref);
1015 return ref;
1018 static ULONG WINAPI JSCaller_Release(IServiceProvider *iface)
1020 JSCaller *This = impl_from_IServiceProvider(iface);
1021 LONG ref = InterlockedDecrement(&This->ref);
1023 TRACE("(%p) ref=%ld\n", This, ref);
1025 if(!ref) {
1026 assert(!This->ctx);
1027 free(This);
1030 return ref;
1033 static HRESULT WINAPI JSCaller_QueryService(IServiceProvider *iface, REFGUID guidService,
1034 REFIID riid, void **ppv)
1036 JSCaller *This = impl_from_IServiceProvider(iface);
1038 if(IsEqualGUID(guidService, &SID_GetCaller)) {
1039 TRACE("(%p)->(SID_GetCaller)\n", This);
1040 *ppv = NULL;
1041 if(!This->caller)
1042 return S_OK;
1043 return (This->caller == SP_CALLER_UNINITIALIZED) ? E_NOINTERFACE : IServiceProvider_QueryInterface(This->caller, riid, ppv);
1046 if(IsEqualGUID(guidService, &SID_VariantConversion) && This->ctx && This->ctx->active_script) {
1047 TRACE("(%p)->(SID_VariantConversion)\n", This);
1048 return IActiveScript_QueryInterface(This->ctx->active_script, riid, ppv);
1051 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
1053 *ppv = NULL;
1054 return E_NOINTERFACE;
1057 static const IServiceProviderVtbl ServiceProviderVtbl = {
1058 JSCaller_QueryInterface,
1059 JSCaller_AddRef,
1060 JSCaller_Release,
1061 JSCaller_QueryService
1064 HRESULT create_jscaller(script_ctx_t *ctx)
1066 JSCaller *ret;
1068 ret = malloc(sizeof(*ret));
1069 if(!ret)
1070 return E_OUTOFMEMORY;
1072 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
1073 ret->ref = 1;
1074 ret->ctx = ctx;
1075 ret->caller = SP_CALLER_UNINITIALIZED;
1077 ctx->jscaller = ret;
1078 return S_OK;