jsutils.c 23.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2008 Jacek Caban for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

19 20 21
#include "config.h"
#include "wine/port.h"

22
#include <math.h>
23
#include <assert.h>
24

25
#include "jscript.h"
26
#include "engine.h"
27 28 29 30

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(jscript);
31
WINE_DECLARE_DEBUG_CHANNEL(heap);
32

33 34
const char *debugstr_variant(const VARIANT *v)
{
35 36 37
    if(!v)
        return "(null)";

38 39
    switch(V_VT(v)) {
    case VT_EMPTY:
40
        return "{VT_EMPTY}";
41
    case VT_NULL:
42
        return "{VT_NULL}";
43 44
    case VT_I4:
        return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v));
45 46
    case VT_UI4:
        return wine_dbg_sprintf("{VT_UI4: %u}", V_UI4(v));
47 48 49 50 51 52 53 54
    case VT_R8:
        return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v));
    case VT_BSTR:
        return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v)));
    case VT_DISPATCH:
        return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v));
    case VT_BOOL:
        return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v));
55 56
    case VT_ARRAY|VT_VARIANT:
        return "{VT_ARRAY|VT_VARIANT: ...}";
57 58 59 60 61
    default:
        return wine_dbg_sprintf("{vt %d}", V_VT(v));
    }
}

62 63
const char *debugstr_jsval(const jsval_t v)
{
64
    switch(jsval_type(v)) {
65 66 67 68 69 70 71
    case JSV_UNDEFINED:
        return "undefined";
    case JSV_NULL:
        return "null";
    case JSV_OBJECT:
        return wine_dbg_sprintf("obj(%p)", get_object(v));
    case JSV_STRING:
72
        return wine_dbg_sprintf("str(%s)", debugstr_jsstr(get_string(v)));
73 74 75 76 77 78 79 80 81 82 83 84
    case JSV_NUMBER:
        return wine_dbg_sprintf("%lf", get_number(v));
    case JSV_BOOL:
        return get_bool(v) ? "true" : "false";
    case JSV_VARIANT:
        return debugstr_variant(get_variant(v));
    }

    assert(0);
    return NULL;
}

85
#define MIN_BLOCK_SIZE  128
86
#define ARENA_FREE_FILLER  0xaa
87 88 89 90 91 92

static inline DWORD block_size(DWORD block)
{
    return MIN_BLOCK_SIZE << block;
}

93
void heap_pool_init(heap_pool_t *heap)
94 95 96 97 98
{
    memset(heap, 0, sizeof(*heap));
    list_init(&heap->custom_blocks);
}

99
void *heap_pool_alloc(heap_pool_t *heap, DWORD size)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
{
    struct list *list;
    void *tmp;

    if(!heap->block_cnt) {
        if(!heap->blocks) {
            heap->blocks = heap_alloc(sizeof(void*));
            if(!heap->blocks)
                return NULL;
        }

        tmp = heap_alloc(block_size(0));
        if(!tmp)
            return NULL;

        heap->blocks[0] = tmp;
        heap->block_cnt = 1;
    }

119
    if(heap->offset + size <= block_size(heap->last_block)) {
120 121 122 123 124
        tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
        heap->offset += size;
        return tmp;
    }

125
    if(size <= block_size(heap->last_block+1)) {
126 127 128 129 130
        if(heap->last_block+1 == heap->block_cnt) {
            tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
            if(!tmp)
                return NULL;

131 132 133 134
            heap->blocks = tmp;
            heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
            if(!heap->blocks[heap->block_cnt])
                return NULL;
135

136 137
            heap->block_cnt++;
        }
138 139 140 141 142 143 144 145 146 147 148 149 150 151

        heap->last_block++;
        heap->offset = size;
        return heap->blocks[heap->last_block];
    }

    list = heap_alloc(size + sizeof(struct list));
    if(!list)
        return NULL;

    list_add_head(&heap->custom_blocks, list);
    return list+1;
}

152
void *heap_pool_grow(heap_pool_t *heap, void *mem, DWORD size, DWORD inc)
153
{
154 155
    void *ret;

156 157 158 159 160 161
    if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
       && heap->offset+inc < block_size(heap->last_block)) {
        heap->offset += inc;
        return mem;
    }

162
    ret = heap_pool_alloc(heap, size+inc);
163
    if(ret) /* FIXME: avoid copying for custom blocks */
164 165
        memcpy(ret, mem, size);
    return ret;
166 167
}

168
void heap_pool_clear(heap_pool_t *heap)
169 170 171
{
    struct list *tmp;

172 173 174
    if(!heap)
        return;

175 176 177 178
    while((tmp = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
        list_remove(tmp);
        heap_free(tmp);
    }
179

180 181 182 183 184 185 186
    if(WARN_ON(heap)) {
        DWORD i;

        for(i=0; i < heap->block_cnt; i++)
            memset(heap->blocks[i], ARENA_FREE_FILLER, block_size(i));
    }

187
    heap->last_block = heap->offset = 0;
188
    heap->mark = FALSE;
189 190
}

191
void heap_pool_free(heap_pool_t *heap)
192 193 194
{
    DWORD i;

195
    heap_pool_clear(heap);
196 197 198 199 200

    for(i=0; i < heap->block_cnt; i++)
        heap_free(heap->blocks[i]);
    heap_free(heap->blocks);

201
    heap_pool_init(heap);
202
}
203

204
heap_pool_t *heap_pool_mark(heap_pool_t *heap)
205 206 207 208 209 210 211 212
{
    if(heap->mark)
        return NULL;

    heap->mark = TRUE;
    return heap;
}

213 214
void jsval_release(jsval_t val)
{
215
    switch(jsval_type(val)) {
216
    case JSV_OBJECT:
217 218
        if(get_object(val))
            IDispatch_Release(get_object(val));
219 220
        break;
    case JSV_STRING:
221
        jsstr_release(get_string(val));
222 223
        break;
    case JSV_VARIANT:
224 225
        VariantClear(get_variant(val));
        heap_free(get_variant(val));
226 227 228 229 230 231
        break;
    default:
        break;
    }
}

232
static HRESULT jsval_variant(jsval_t *val, VARIANT *var)
233
{
234
    VARIANT *v;
235 236
    HRESULT hres;

237 238
    __JSVAL_TYPE(*val) = JSV_VARIANT;
    __JSVAL_VAR(*val) = v = heap_alloc(sizeof(VARIANT));
239
    if(!v)
240 241
        return E_OUTOFMEMORY;

242 243
    V_VT(v) = VT_EMPTY;
    hres = VariantCopy(v, var);
244
    if(FAILED(hres))
245
        heap_free(v);
246 247 248
    return hres;
}

249 250
HRESULT jsval_copy(jsval_t v, jsval_t *r)
{
251
    switch(jsval_type(v)) {
252 253 254 255 256 257 258
    case JSV_UNDEFINED:
    case JSV_NULL:
    case JSV_NUMBER:
    case JSV_BOOL:
        *r = v;
        return S_OK;
    case JSV_OBJECT:
259 260
        if(get_object(v))
            IDispatch_AddRef(get_object(v));
261 262 263
        *r = v;
        return S_OK;
    case JSV_STRING: {
264 265
        jsstr_addref(get_string(v));
        *r = v;
266 267 268 269 270 271 272 273 274 275
        return S_OK;
    }
    case JSV_VARIANT:
        return jsval_variant(r, get_variant(v));
    }

    assert(0);
    return E_FAIL;
}

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
HRESULT variant_to_jsval(VARIANT *var, jsval_t *r)
{
    switch(V_VT(var)) {
    case VT_EMPTY:
        *r = jsval_undefined();
        return S_OK;
    case VT_NULL:
        *r = jsval_null();
        return S_OK;
    case VT_BOOL:
        *r = jsval_bool(V_BOOL(var));
        return S_OK;
    case VT_I4:
        *r = jsval_number(V_I4(var));
        return S_OK;
    case VT_R8:
        *r = jsval_number(V_R8(var));
        return S_OK;
    case VT_BSTR: {
295 296 297 298 299 300 301
        jsstr_t *str;

        str = jsstr_alloc_len(V_BSTR(var), SysStringLen(V_BSTR(var)));
        if(!str)
            return E_OUTOFMEMORY;
        if(!V_BSTR(var))
            str->length_flags |= JSSTR_FLAG_NULLBSTR;
302

303 304 305 306
        *r = jsval_string(str);
        return S_OK;
    }
    case VT_DISPATCH: {
307 308
        if(V_DISPATCH(var))
            IDispatch_AddRef(V_DISPATCH(var));
309 310 311 312
        *r = jsval_disp(V_DISPATCH(var));
        return S_OK;
    }
    case VT_I2:
313 314
        *r = jsval_number(V_I2(var));
        return S_OK;
315
    case VT_INT:
316 317
        *r = jsval_number(V_INT(var));
        return S_OK;
318 319 320 321 322 323 324 325 326 327 328 329
    case VT_UNKNOWN:
        if(V_UNKNOWN(var)) {
            IDispatch *disp;
            HRESULT hres;

            hres = IUnknown_QueryInterface(V_UNKNOWN(var), &IID_IDispatch, (void**)&disp);
            if(SUCCEEDED(hres)) {
                *r = jsval_disp(disp);
                return S_OK;
            }
        }
        /* fall through */
330 331 332 333 334 335 336
    default:
        return jsval_variant(r, var);
    }
}

HRESULT jsval_to_variant(jsval_t val, VARIANT *retv)
{
337
    switch(jsval_type(val)) {
338 339 340 341 342 343 344 345
    case JSV_UNDEFINED:
        V_VT(retv) = VT_EMPTY;
        return S_OK;
    case JSV_NULL:
        V_VT(retv) = VT_NULL;
        return S_OK;
    case JSV_OBJECT:
        V_VT(retv) = VT_DISPATCH;
346 347 348
        if(get_object(val))
            IDispatch_AddRef(get_object(val));
        V_DISPATCH(retv) = get_object(val);
349
        return S_OK;
350 351 352
    case JSV_STRING: {
        jsstr_t *str = get_string(val);

353
        V_VT(retv) = VT_BSTR;
354 355 356
        if(str->length_flags & JSSTR_FLAG_NULLBSTR) {
            V_BSTR(retv) = NULL;
        }else {
357 358 359 360
            V_BSTR(retv) = SysAllocStringLen(NULL, jsstr_length(str));
            if(V_BSTR(retv))
                jsstr_flush(str, V_BSTR(retv));
            else
361 362
                return E_OUTOFMEMORY;
        }
363
        return S_OK;
364
    }
365 366 367 368 369 370 371 372 373 374 375
    case JSV_NUMBER: {
        double n = get_number(val);

        if(is_int32(n)) {
            V_VT(retv) = VT_I4;
            V_I4(retv) = n;
        }else {
            V_VT(retv) = VT_R8;
            V_R8(retv) = n;
        }

376
        return S_OK;
377
    }
378 379
    case JSV_BOOL:
        V_VT(retv) = VT_BOOL;
380
        V_BOOL(retv) = get_bool(val) ? VARIANT_TRUE : VARIANT_FALSE;
381 382
        return S_OK;
    case JSV_VARIANT:
383
        V_VT(retv) = VT_EMPTY;
384
        return VariantCopy(retv, get_variant(val));
385 386 387 388 389 390
    }

    assert(0);
    return E_FAIL;
}

391
/* ECMA-262 3rd Edition    9.1 */
392
HRESULT to_primitive(script_ctx_t *ctx, jsval_t val, jsval_t *ret, hint_t hint)
393
{
394
    if(is_object_instance(val)) {
395
        jsdisp_t *jsdisp;
396
        jsval_t prim;
397 398 399 400 401 402
        DISPID id;
        HRESULT hres;

        static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
        static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};

403 404 405
        if(!get_object(val)) {
            *ret = jsval_null();
            return S_OK;
406 407
        }

408 409
        jsdisp = iface_to_jsdisp((IUnknown*)get_object(val));
        if(!jsdisp)
410
            return disp_propget(ctx, get_object(val), DISPID_VALUE, ret);
411 412 413 414 415 416 417 418

        if(hint == NO_HINT)
            hint = is_class(jsdisp, JSCLASS_DATE) ? HINT_STRING : HINT_NUMBER;

        /* Native implementation doesn't throw TypeErrors, returns strange values */

        hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? toStringW : valueOfW, 0, &id);
        if(SUCCEEDED(hres)) {
419
            hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
420
            if(FAILED(hres)) {
421
                WARN("call error - forwarding exception\n");
422 423
                jsdisp_release(jsdisp);
                return hres;
424
            }else if(!is_object_instance(prim)) {
425
                jsdisp_release(jsdisp);
426
                *ret = prim;
427
                return S_OK;
428 429
            }else {
                IDispatch_Release(get_object(prim));
430 431 432 433 434
            }
        }

        hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? valueOfW : toStringW, 0, &id);
        if(SUCCEEDED(hres)) {
435
            hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
436
            if(FAILED(hres)) {
437
                WARN("call error - forwarding exception\n");
438 439
                jsdisp_release(jsdisp);
                return hres;
440
            }else if(!is_object_instance(prim)) {
441
                jsdisp_release(jsdisp);
442
                *ret = prim;
443
                return S_OK;
444 445
            }else {
                IDispatch_Release(get_object(prim));
446 447 448 449 450
            }
        }

        jsdisp_release(jsdisp);

451
        WARN("failed\n");
452
        return throw_type_error(ctx, JS_E_TO_PRIMITIVE, NULL);
453
    }
454 455 456 457 458

    return jsval_copy(val, ret);

}

459
/* ECMA-262 3rd Edition    9.2 */
460
HRESULT to_boolean(jsval_t val, BOOL *ret)
461
{
462
    switch(jsval_type(val)) {
463 464
    case JSV_UNDEFINED:
    case JSV_NULL:
465
        *ret = FALSE;
466 467 468 469 470
        return S_OK;
    case JSV_OBJECT:
        *ret = get_object(val) != NULL;
        return S_OK;
    case JSV_STRING:
471
        *ret = jsstr_length(get_string(val)) != 0;
472
        return S_OK;
473 474 475 476 477 478 479 480 481
    case JSV_NUMBER:
        *ret = !isnan(get_number(val)) && get_number(val);
        return S_OK;
    case JSV_BOOL:
        *ret = get_bool(val);
        return S_OK;
    case JSV_VARIANT:
        FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
        return E_NOTIMPL;
482 483
    }

484 485
    assert(0);
    return E_FAIL;
486 487
}

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
static int hex_to_int(WCHAR c)
{
    if('0' <= c && c <= '9')
        return c-'0';

    if('a' <= c && c <= 'f')
        return c-'a'+10;

    if('A' <= c && c <= 'F')
        return c-'A'+10;

    return -1;
}

/* ECMA-262 3rd Edition    9.3.1 */
503
static HRESULT str_to_number(jsstr_t *str, double *ret)
504
{
505
    const WCHAR *ptr = str->str;
506 507 508 509 510
    BOOL neg = FALSE;
    DOUBLE d = 0.0;

    static const WCHAR infinityW[] = {'I','n','f','i','n','i','t','y'};

511 512 513 514 515
    if(!ptr) {
        *ret = 0;
        return S_OK;
    }

516 517 518
    while(isspaceW(*ptr))
        ptr++;

519 520 521 522 523 524 525
    if(*ptr == '-') {
        neg = TRUE;
        ptr++;
    }else if(*ptr == '+') {
        ptr++;
    }

526 527 528 529 530
    if(!strncmpW(ptr, infinityW, sizeof(infinityW)/sizeof(WCHAR))) {
        ptr += sizeof(infinityW)/sizeof(WCHAR);
        while(*ptr && isspaceW(*ptr))
            ptr++;

531
        if(*ptr)
532
            *ret = NAN;
533
        else
534
            *ret = neg ? -INFINITY : INFINITY;
535
        return S_OK;
536 537
    }

538
    if(*ptr == '0' && ptr[1] == 'x') {
539 540 541 542 543 544 545 546
        DWORD l = 0;

        ptr += 2;
        while((l = hex_to_int(*ptr)) != -1) {
            d = d*16 + l;
            ptr++;
        }

547
        *ret = d;
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
        return S_OK;
    }

    while(isdigitW(*ptr))
        d = d*10 + (*ptr++ - '0');

    if(*ptr == 'e' || *ptr == 'E') {
        BOOL eneg = FALSE;
        LONG l = 0;

        ptr++;
        if(*ptr == '-') {
            ptr++;
            eneg = TRUE;
        }else if(*ptr == '+') {
            ptr++;
        }

        while(isdigitW(*ptr))
            l = l*10 + (*ptr++ - '0');
        if(eneg)
            l = -l;

        d *= pow(10, l);
    }else if(*ptr == '.') {
        DOUBLE dec = 0.1;

        ptr++;
        while(isdigitW(*ptr)) {
            d += dec * (*ptr++ - '0');
            dec *= 0.1;
        }
    }

    while(isspaceW(*ptr))
        ptr++;

    if(*ptr) {
586
        *ret = NAN;
587
        return S_OK;
588 589 590 591 592
    }

    if(neg)
        d = -d;

593
    *ret = d;
594 595 596
    return S_OK;
}

597
/* ECMA-262 3rd Edition    9.3 */
598
HRESULT to_number(script_ctx_t *ctx, jsval_t val, double *ret)
599
{
600
    switch(jsval_type(val)) {
601
    case JSV_UNDEFINED:
602
        *ret = NAN;
603 604
        return S_OK;
    case JSV_NULL:
605
        *ret = 0;
606 607 608 609 610 611 612
        return S_OK;
    case JSV_NUMBER:
        *ret = get_number(val);
        return S_OK;
    case JSV_STRING:
        return str_to_number(get_string(val), ret);
    case JSV_OBJECT: {
613
        jsval_t prim;
614 615
        HRESULT hres;

616
        hres = to_primitive(ctx, val, &prim, HINT_NUMBER);
617 618 619
        if(FAILED(hres))
            return hres;

620
        hres = to_number(ctx, prim, ret);
621
        jsval_release(prim);
622 623
        return hres;
    }
624 625
    case JSV_BOOL:
        *ret = get_bool(val) ? 1 : 0;
626
        return S_OK;
627 628 629 630
    case JSV_VARIANT:
        FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
        return E_NOTIMPL;
    };
631

632 633
    assert(0);
    return E_FAIL;
634 635
}

636
/* ECMA-262 3rd Edition    9.4 */
637
HRESULT to_integer(script_ctx_t *ctx, jsval_t v, double *ret)
638
{
639
    double n;
640 641
    HRESULT hres;

642
    hres = to_number(ctx, v, &n);
643 644 645
    if(FAILED(hres))
        return hres;

646 647 648 649
    if(isnan(n))
        *ret = 0;
    else
        *ret = n >= 0.0 ? floor(n) : -floor(-n);
650 651 652
    return S_OK;
}

653
/* ECMA-262 3rd Edition    9.5 */
654
HRESULT to_int32(script_ctx_t *ctx, jsval_t v, INT *ret)
655 656 657 658
{
    double n;
    HRESULT hres;

659
    hres = to_number(ctx, v, &n);
660 661 662 663 664 665 666
    if(FAILED(hres))
        return hres;

    *ret = isnan(n) || isinf(n) ? 0 : n;
    return S_OK;
}

667
/* ECMA-262 3rd Edition    9.6 */
668
HRESULT to_uint32(script_ctx_t *ctx, jsval_t val, DWORD *ret)
669
{
670
    INT32 n;
671 672
    HRESULT hres;

673 674 675 676
    hres = to_int32(ctx, val, &n);
    if(SUCCEEDED(hres))
        *ret = n;
    return hres;
677 678
}

679
static jsstr_t *int_to_string(int i)
680 681 682 683 684 685
{
    WCHAR buf[12], *p;
    BOOL neg = FALSE;

    if(!i) {
        static const WCHAR zeroW[] = {'0',0};
686
        return jsstr_alloc(zeroW);
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
    }

    if(i < 0) {
        neg = TRUE;
        i = -i;
    }

    p = buf + sizeof(buf)/sizeof(*buf)-1;
    *p-- = 0;
    while(i) {
        *p-- = i%10 + '0';
        i /= 10;
    }

    if(neg)
        *p = '-';
    else
        p++;

706
    return jsstr_alloc(p);
707 708
}

709
HRESULT double_to_string(double n, jsstr_t **str)
710 711 712 713
{
    const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};

    if(isnan(n)) {
714
        *str = jsstr_nan();
715
    }else if(isinf(n)) {
716
        *str = jsstr_alloc(n<0 ? InfinityW : InfinityW+1);
717
    }else if(is_int32(n)) {
718
        *str = int_to_string(n);
719 720 721 722
    }else {
        VARIANT strv, v;
        HRESULT hres;

723
        /* FIXME: Don't use VariantChangeTypeEx */
724 725 726 727 728 729 730
        V_VT(&v) = VT_R8;
        V_R8(&v) = n;
        V_VT(&strv) = VT_EMPTY;
        hres = VariantChangeTypeEx(&strv, &v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
        if(FAILED(hres))
            return hres;

731 732
        *str = jsstr_alloc(V_BSTR(&strv));
        SysFreeString(V_BSTR(&strv));
733 734 735 736 737
    }

    return *str ? S_OK : E_OUTOFMEMORY;
}

738
/* ECMA-262 3rd Edition    9.8 */
739
HRESULT to_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str)
740
{
741 742 743 744
    const WCHAR nullW[] = {'n','u','l','l',0};
    const WCHAR trueW[] = {'t','r','u','e',0};
    const WCHAR falseW[] = {'f','a','l','s','e',0};

745
    switch(jsval_type(val)) {
746
    case JSV_UNDEFINED:
747 748
        *str = jsstr_undefined();
        return S_OK;
749
    case JSV_NULL:
750
        *str = jsstr_alloc(nullW);
751
        break;
752
    case JSV_NUMBER:
753
        return double_to_string(get_number(val), str);
754
    case JSV_STRING:
755
        *str = jsstr_addref(get_string(val));
756
        break;
757
    case JSV_OBJECT: {
758
        jsval_t prim;
759
        HRESULT hres;
760

761
        hres = to_primitive(ctx, val, &prim, HINT_STRING);
762 763 764
        if(FAILED(hres))
            return hres;

765
        hres = to_string(ctx, prim, str);
766
        jsval_release(prim);
767 768
        return hres;
    }
769
    case JSV_BOOL:
770
        *str = jsstr_alloc(get_bool(val) ? trueW : falseW);
771
        break;
772
    default:
773
        FIXME("unsupported %s\n", debugstr_jsval(val));
774
        return E_NOTIMPL;
775 776
    }

777
    return *str ? S_OK : E_OUTOFMEMORY;
778 779
}

780
/* ECMA-262 3rd Edition    9.9 */
781
HRESULT to_object(script_ctx_t *ctx, jsval_t val, IDispatch **disp)
782
{
783
    jsdisp_t *dispex;
784 785
    HRESULT hres;

786
    switch(jsval_type(val)) {
787
    case JSV_STRING:
788
        hres = create_string(ctx, get_string(val), &dispex);
789 790 791
        if(FAILED(hres))
            return hres;

792
        *disp = to_disp(dispex);
793
        break;
794 795
    case JSV_NUMBER:
        hres = create_number(ctx, get_number(val), &dispex);
796 797 798
        if(FAILED(hres))
            return hres;

799
        *disp = to_disp(dispex);
800
        break;
801 802 803 804
    case JSV_OBJECT:
        if(get_object(val)) {
            *disp = get_object(val);
            IDispatch_AddRef(*disp);
805
        }else {
806
            jsdisp_t *obj;
807 808 809 810 811

            hres = create_object(ctx, NULL, &obj);
            if(FAILED(hres))
                return hres;

812
            *disp = to_disp(obj);
813
        }
814
        break;
815 816
    case JSV_BOOL:
        hres = create_bool(ctx, get_bool(val), &dispex);
817 818 819
        if(FAILED(hres))
            return hres;

820 821
        *disp = to_disp(dispex);
        break;
822 823 824 825
    case JSV_UNDEFINED:
    case JSV_NULL:
        WARN("object expected\n");
        return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
826 827 828 829 830 831
    case JSV_VARIANT:
        switch(V_VT(get_variant(val))) {
        case VT_ARRAY|VT_VARIANT:
            hres = create_vbarray(ctx, V_ARRAY(get_variant(val)), &dispex);
            if(FAILED(hres))
                return hres;
832

833 834 835 836 837 838 839
            *disp = to_disp(dispex);
            break;

        default:
            FIXME("Unsupported %s\n", debugstr_variant(get_variant(val)));
            return E_NOTIMPL;
        }
840
        break;
841 842 843 844
    }

    return S_OK;
}
845

846 847
HRESULT variant_change_type(script_ctx_t *ctx, VARIANT *dst, VARIANT *src, VARTYPE vt)
{
848
    jsval_t val;
849 850
    HRESULT hres;

851
    clear_ei(ctx);
852 853 854 855
    hres = variant_to_jsval(src, &val);
    if(FAILED(hres))
        return hres;

856 857 858 859 860
    switch(vt) {
    case VT_I2:
    case VT_I4: {
        INT i;

861
        hres = to_int32(ctx, val, &i);
862 863 864 865 866 867 868 869
        if(SUCCEEDED(hres)) {
            if(vt == VT_I4)
                V_I4(dst) = i;
            else
                V_I2(dst) = i;
        }
        break;
    }
870 871
    case VT_R8: {
        double n;
872
        hres = to_number(ctx, val, &n);
873 874
        if(SUCCEEDED(hres))
            V_R8(dst) = n;
875
        break;
876
    }
877
    case VT_R4: {
878
        double n;
879

880
        hres = to_number(ctx, val, &n);
881
        if(SUCCEEDED(hres))
882
            V_R4(dst) = n;
883 884 885
        break;
    }
    case VT_BOOL: {
886 887 888
        BOOL b;

        hres = to_boolean(val, &b);
889
        if(SUCCEEDED(hres))
890
            V_BOOL(dst) = b ? VARIANT_TRUE : VARIANT_FALSE;
891 892 893
        break;
    }
    case VT_BSTR: {
894
        jsstr_t *str;
895

896
        hres = to_string(ctx, val, &str);
897 898 899 900 901 902 903 904
        if(FAILED(hres))
            break;

        if(str->length_flags & JSSTR_FLAG_NULLBSTR) {
            V_BSTR(dst) = NULL;
            break;
        }

905 906 907 908
        V_BSTR(dst) = SysAllocStringLen(NULL, jsstr_length(str));
        if(V_BSTR(dst))
            jsstr_flush(str, V_BSTR(dst));
        else
909
            hres = E_OUTOFMEMORY;
910 911 912 913 914 915 916 917 918 919 920 921 922
        break;
    }
    case VT_EMPTY:
        hres = V_VT(src) == VT_EMPTY ? S_OK : E_NOTIMPL;
        break;
    case VT_NULL:
        hres = V_VT(src) == VT_NULL ? S_OK : E_NOTIMPL;
        break;
    default:
        FIXME("vt %d not implemented\n", vt);
        hres = E_NOTIMPL;
    }

923
    jsval_release(val);
924
    if(FAILED(hres))
925 926 927 928 929 930
        return hres;

    V_VT(dst) = vt;
    return S_OK;
}

931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
static inline JSCaller *impl_from_IServiceProvider(IServiceProvider *iface)
{
    return CONTAINING_RECORD(iface, JSCaller, IServiceProvider_iface);
}

static HRESULT WINAPI JSCaller_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
{
    JSCaller *This = impl_from_IServiceProvider(iface);

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
        *ppv = &This->IServiceProvider_iface;
    }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
        TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
        *ppv = &This->IServiceProvider_iface;
    }else {
        WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
        *ppv = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown*)*ppv);
    return S_OK;
}

static ULONG WINAPI JSCaller_AddRef(IServiceProvider *iface)
{
    JSCaller *This = impl_from_IServiceProvider(iface);
    LONG ref = InterlockedIncrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

    return ref;
}

static ULONG WINAPI JSCaller_Release(IServiceProvider *iface)
{
    JSCaller *This = impl_from_IServiceProvider(iface);
    LONG ref = InterlockedIncrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

973 974
    if(!ref) {
        assert(!This->ctx);
975
        heap_free(This);
976
    }
977 978 979 980 981 982 983 984 985

    return ref;
}

static HRESULT WINAPI JSCaller_QueryService(IServiceProvider *iface, REFGUID guidService,
        REFIID riid, void **ppv)
{
    JSCaller *This = impl_from_IServiceProvider(iface);

986 987 988 989 990
    if(IsEqualGUID(guidService, &SID_VariantConversion) && This->ctx && This->ctx->active_script) {
        TRACE("(%p)->(SID_VariantConversion)\n", This);
        return IActiveScript_QueryInterface(This->ctx->active_script, riid, ppv);
    }

991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);

    *ppv = NULL;
    return E_NOINTERFACE;
}

static const IServiceProviderVtbl ServiceProviderVtbl = {
    JSCaller_QueryInterface,
    JSCaller_AddRef,
    JSCaller_Release,
    JSCaller_QueryService
};

HRESULT create_jscaller(script_ctx_t *ctx)
{
    JSCaller *ret;

    ret = heap_alloc(sizeof(*ret));
    if(!ret)
        return E_OUTOFMEMORY;

    ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
    ret->ref = 1;
1014
    ret->ctx = ctx;
1015 1016 1017 1018

    ctx->jscaller = ret;
    return S_OK;
}