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
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
)) {
39 return wine_dbg_sprintf("obj(%p)", get_object(v
));
41 return wine_dbg_sprintf("str(%s)", debugstr_jsstr(get_string(v
)));
43 return wine_dbg_sprintf("%lf", get_number(v
));
45 return get_bool(v
) ? "true" : "false";
47 return debugstr_variant(get_variant(v
));
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
)
73 if(!heap
->block_cnt
) {
75 heap
->blocks
= heap_alloc(sizeof(void*));
80 tmp
= heap_alloc(block_size(0));
84 heap
->blocks
[0] = tmp
;
88 if(heap
->offset
+ size
<= block_size(heap
->last_block
)) {
89 tmp
= ((BYTE
*)heap
->blocks
[heap
->last_block
])+heap
->offset
;
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*));
101 heap
->blocks
[heap
->block_cnt
] = heap_alloc(block_size(heap
->block_cnt
));
102 if(!heap
->blocks
[heap
->block_cnt
])
110 return heap
->blocks
[heap
->last_block
];
113 list
= heap_alloc(size
+ sizeof(struct list
));
117 list_add_head(&heap
->custom_blocks
, list
);
121 void *heap_pool_grow(heap_pool_t
*heap
, void *mem
, DWORD size
, DWORD inc
)
125 if(mem
== (BYTE
*)heap
->blocks
[heap
->last_block
] + heap
->offset
-size
126 && heap
->offset
+inc
< block_size(heap
->last_block
)) {
131 ret
= heap_pool_alloc(heap
, size
+inc
);
132 if(ret
) /* FIXME: avoid copying for custom blocks */
133 memcpy(ret
, mem
, size
);
137 void heap_pool_clear(heap_pool_t
*heap
)
144 while((tmp
= list_head(&heap
->custom_blocks
))) {
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;
160 void heap_pool_free(heap_pool_t
*heap
)
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
)
182 void jsval_release(jsval_t val
)
184 switch(jsval_type(val
)) {
187 IDispatch_Release(get_object(val
));
190 jsstr_release(get_string(val
));
193 VariantClear(get_variant(val
));
194 heap_free(get_variant(val
));
201 static HRESULT
jsval_variant(jsval_t
*val
, VARIANT
*var
)
206 __JSVAL_TYPE(*val
) = JSV_VARIANT
;
207 __JSVAL_VAR(*val
) = v
= heap_alloc(sizeof(VARIANT
));
209 *val
= jsval_undefined();
210 return E_OUTOFMEMORY
;
214 hres
= VariantCopy(v
, var
);
216 *val
= jsval_undefined();
222 HRESULT
jsval_copy(jsval_t v
, jsval_t
*r
)
224 switch(jsval_type(v
)) {
233 IDispatch_AddRef(get_object(v
));
237 jsstr_addref(get_string(v
));
242 return jsval_variant(r
, get_variant(v
));
249 HRESULT
variant_to_jsval(VARIANT
*var
, jsval_t
*r
)
251 if(V_VT(var
) == (VT_VARIANT
|VT_BYREF
))
252 var
= V_VARIANTREF(var
);
256 *r
= jsval_undefined();
262 *r
= jsval_bool(V_BOOL(var
));
265 *r
= jsval_number(V_I4(var
));
268 *r
= jsval_number(V_R8(var
));
274 str
= jsstr_alloc_len(V_BSTR(var
), SysStringLen(V_BSTR(var
)));
276 return E_OUTOFMEMORY
;
278 str
= jsstr_null_bstr();
281 *r
= jsval_string(str
);
286 IDispatch_AddRef(V_DISPATCH(var
));
287 *r
= jsval_disp(V_DISPATCH(var
));
291 *r
= jsval_number(V_I1(var
));
294 *r
= jsval_number(V_UI1(var
));
297 *r
= jsval_number(V_I2(var
));
300 *r
= jsval_number(V_UI2(var
));
303 *r
= jsval_number(V_INT(var
));
306 *r
= jsval_number(V_UI4(var
));
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
));
316 *r
= jsval_number(V_R4(var
));
323 hres
= IUnknown_QueryInterface(V_UNKNOWN(var
), &IID_IDispatch
, (void**)&disp
);
324 if(SUCCEEDED(hres
)) {
325 *r
= jsval_disp(disp
);
329 *r
= jsval_disp(NULL
);
334 return jsval_variant(r
, var
);
338 HRESULT
jsval_to_variant(jsval_t val
, VARIANT
*retv
)
340 switch(jsval_type(val
)) {
342 V_VT(retv
) = VT_EMPTY
;
345 V_VT(retv
) = VT_NULL
;
348 V_VT(retv
) = VT_DISPATCH
;
350 IDispatch_AddRef(get_object(val
));
351 V_DISPATCH(retv
) = get_object(val
);
354 V_VT(retv
) = VT_BSTR
;
355 return jsstr_to_bstr(get_string(val
), &V_BSTR(retv
));
357 double n
= get_number(val
);
370 V_VT(retv
) = VT_BOOL
;
371 V_BOOL(retv
) = get_bool(val
) ? VARIANT_TRUE
: VARIANT_FALSE
;
374 V_VT(retv
) = VT_EMPTY
;
375 return VariantCopy(retv
, get_variant(val
));
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
)) {
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
)) {
399 jsdisp
= iface_to_jsdisp(get_object(val
));
401 return disp_propget(ctx
, get_object(val
), DISPID_VALUE
, ret
);
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
);
412 WARN("call error - forwarding exception\n");
413 jsdisp_release(jsdisp
);
415 }else if(!is_object_instance(prim
)) {
416 jsdisp_release(jsdisp
);
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
);
428 WARN("call error - forwarding exception\n");
429 jsdisp_release(jsdisp
);
431 }else if(!is_object_instance(prim
)) {
432 jsdisp_release(jsdisp
);
436 IDispatch_Release(get_object(prim
));
440 jsdisp_release(jsdisp
);
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
)) {
459 *ret
= get_object(val
) != NULL
;
462 *ret
= jsstr_length(get_string(val
)) != 0;
465 *ret
= !isnan(get_number(val
)) && get_number(val
);
468 *ret
= get_bool(val
);
471 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
479 static int hex_to_int(WCHAR c
)
481 if('0' <= c
&& c
<= '9')
484 if('a' <= c
&& c
<= 'f')
487 if('A' <= c
&& c
<= 'F')
493 /* ECMA-262 3rd Edition 9.3.1 */
494 static HRESULT
str_to_number(jsstr_t
*str
, double *ret
)
500 static const WCHAR infinityW
[] = {'I','n','f','i','n','i','t','y'};
502 ptr
= jsstr_flatten(str
);
504 return E_OUTOFMEMORY
;
506 while(iswspace(*ptr
))
512 }else if(*ptr
== '+') {
516 if(!wcsncmp(ptr
, infinityW
, ARRAY_SIZE(infinityW
))) {
517 ptr
+= ARRAY_SIZE(infinityW
);
518 while(*ptr
&& iswspace(*ptr
))
524 *ret
= neg
? -INFINITY
: INFINITY
;
528 if(*ptr
== '0' && ptr
[1] == 'x') {
532 while((l
= hex_to_int(*ptr
)) != -1) {
541 while(is_digit(*ptr
))
542 d
= d
*10 + (*ptr
++ - '0');
544 if(*ptr
== 'e' || *ptr
== 'E') {
552 }else if(*ptr
== '+') {
556 while(is_digit(*ptr
))
557 l
= l
*10 + (*ptr
++ - '0');
562 }else if(*ptr
== '.') {
566 while(is_digit(*ptr
)) {
567 d
+= dec
* (*ptr
++ - '0');
572 while(iswspace(*ptr
))
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
)) {
598 *ret
= get_number(val
);
601 return str_to_number(get_string(val
), ret
);
606 hres
= to_primitive(ctx
, val
, &prim
, HINT_NUMBER
);
610 hres
= to_number(ctx
, prim
, ret
);
615 *ret
= get_bool(val
) ? 1 : 0;
618 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
626 /* ECMA-262 3rd Edition 9.4 */
627 HRESULT
to_integer(script_ctx_t
*ctx
, jsval_t v
, double *ret
)
632 hres
= to_number(ctx
, v
, &n
);
639 *ret
= n
>= 0.0 ? floor(n
) : -floor(-n
);
643 static INT32
double_to_int32(double 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)
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
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. */
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
)
691 hres
= to_number(ctx
, v
, &n
);
695 *ret
= double_to_int32(n
);
699 /* ECMA-262 3rd Edition 9.6 */
700 HRESULT
to_uint32(script_ctx_t
*ctx
, jsval_t val
, UINT32
*ret
)
705 hres
= to_number(ctx
, val
, &n
);
709 *ret
= double_to_int32(n
);
713 HRESULT
double_to_string(double n
, jsstr_t
**str
)
715 static const WCHAR InfinityW
[] = {'-','I','n','f','i','n','i','t','y',0};
720 *str
= jsstr_alloc(n
<0 ? InfinityW
: InfinityW
+1);
721 }else if(is_int32(n
)) {
723 _ltow_s(n
, buf
, ARRAY_SIZE(buf
), 10);
724 *str
= jsstr_alloc(buf
);
729 /* FIXME: Don't use VariantChangeTypeEx */
732 V_VT(&strv
) = VT_EMPTY
;
733 hres
= VariantChangeTypeEx(&strv
, &v
, MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
), 0, VT_BSTR
);
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
)) {
753 *str
= jsstr_undefined();
756 *str
= jsstr_alloc(nullW
);
759 return double_to_string(get_number(val
), str
);
761 *str
= jsstr_addref(get_string(val
));
767 hres
= to_primitive(ctx
, val
, &prim
, HINT_STRING
);
771 hres
= to_string(ctx
, prim
, str
);
776 *str
= jsstr_alloc(get_bool(val
) ? trueW
: falseW
);
779 FIXME("unsupported %s\n", debugstr_jsval(val
));
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
)
790 hres
= to_string(ctx
, val
, str
);
794 *ret_str
= jsstr_flatten(*str
);
797 return E_OUTOFMEMORY
;
803 /* ECMA-262 3rd Edition 9.9 */
804 HRESULT
to_object(script_ctx_t
*ctx
, jsval_t val
, IDispatch
**disp
)
809 switch(jsval_type(val
)) {
811 hres
= create_string(ctx
, get_string(val
), &dispex
);
815 *disp
= to_disp(dispex
);
818 hres
= create_number(ctx
, get_number(val
), &dispex
);
822 *disp
= to_disp(dispex
);
825 if(get_object(val
)) {
826 *disp
= get_object(val
);
827 IDispatch_AddRef(*disp
);
831 hres
= create_object(ctx
, NULL
, &obj
);
835 *disp
= to_disp(obj
);
839 hres
= create_bool(ctx
, get_bool(val
), &dispex
);
843 *disp
= to_disp(dispex
);
847 WARN("object expected\n");
848 return JS_E_OBJECT_EXPECTED
;
850 switch(V_VT(get_variant(val
))) {
851 case VT_ARRAY
|VT_VARIANT
:
852 hres
= create_vbarray(ctx
, V_ARRAY(get_variant(val
)), &dispex
);
856 *disp
= to_disp(dispex
);
860 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val
)));
869 HRESULT
variant_change_type(script_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
)
875 hres
= variant_to_jsval(src
, &val
);
879 enter_script(ctx
, &ei
);
886 hres
= to_int32(ctx
, val
, &i
);
887 if(SUCCEEDED(hres
)) {
898 hres
= to_uint32(ctx
, val
, &i
);
905 hres
= to_number(ctx
, val
, &n
);
913 hres
= to_number(ctx
, val
, &n
);
921 hres
= to_boolean(val
, &b
);
923 V_BOOL(dst
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
929 hres
= to_string(ctx
, val
, &str
);
933 hres
= jsstr_to_bstr(str
, &V_BSTR(dst
));
937 hres
= V_VT(src
) == VT_EMPTY
? S_OK
: E_NOTIMPL
;
940 hres
= V_VT(src
) == VT_NULL
? S_OK
: E_NOTIMPL
;
943 FIXME("vt %d not implemented\n", vt
);
948 leave_script(ctx
, hres
);
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
;
972 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
974 return E_NOINTERFACE
;
977 IUnknown_AddRef((IUnknown
*)*ppv
);
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
);
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
);
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
);
1019 return E_NOINTERFACE
;
1022 static const IServiceProviderVtbl ServiceProviderVtbl
= {
1023 JSCaller_QueryInterface
,
1026 JSCaller_QueryService
1029 HRESULT
create_jscaller(script_ctx_t
*ctx
)
1033 ret
= heap_alloc(sizeof(*ret
));
1035 return E_OUTOFMEMORY
;
1037 ret
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;
1041 ctx
->jscaller
= ret
;