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
)) {
186 IDispatch_Release(get_object(val
));
189 jsstr_release(get_string(val
));
192 VariantClear(get_variant(val
));
193 heap_free(get_variant(val
));
200 static HRESULT
jsval_variant(jsval_t
*val
, VARIANT
*var
)
205 __JSVAL_TYPE(*val
) = JSV_VARIANT
;
206 __JSVAL_VAR(*val
) = v
= heap_alloc(sizeof(VARIANT
));
208 *val
= jsval_undefined();
209 return E_OUTOFMEMORY
;
213 hres
= VariantCopy(v
, var
);
215 *val
= jsval_undefined();
221 HRESULT
jsval_copy(jsval_t v
, jsval_t
*r
)
223 switch(jsval_type(v
)) {
231 IDispatch_AddRef(get_object(v
));
235 jsstr_addref(get_string(v
));
240 return jsval_variant(r
, get_variant(v
));
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
);
254 *r
= jsval_undefined();
260 *r
= jsval_bool(V_BOOL(var
));
263 *r
= jsval_number(V_I4(var
));
266 *r
= jsval_number(V_R8(var
));
272 str
= jsstr_alloc_len(V_BSTR(var
), SysStringLen(V_BSTR(var
)));
274 return E_OUTOFMEMORY
;
276 str
= jsstr_null_bstr();
279 *r
= jsval_string(str
);
283 if(!V_DISPATCH(var
)) {
284 *r
= ctx
->html_mode
? jsval_null() : jsval_null_disp();
287 IDispatch_AddRef(V_DISPATCH(var
));
288 *r
= jsval_disp(V_DISPATCH(var
));
292 *r
= jsval_number(V_I1(var
));
295 *r
= jsval_number(V_UI1(var
));
298 *r
= jsval_number(V_I2(var
));
301 *r
= jsval_number(V_UI2(var
));
304 *r
= jsval_number(V_INT(var
));
307 *r
= jsval_number(V_UI4(var
));
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
));
317 *r
= jsval_number(V_R4(var
));
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
));
330 hres
= IUnknown_QueryInterface(V_UNKNOWN(var
), &IID_IDispatch
, (void**)&disp
);
331 if(SUCCEEDED(hres
)) {
332 *r
= jsval_disp(disp
);
336 *r
= ctx
->html_mode
? jsval_null() : jsval_null_disp();
341 return jsval_variant(r
, var
);
345 HRESULT
jsval_to_variant(jsval_t val
, VARIANT
*retv
)
347 switch(jsval_type(val
)) {
349 V_VT(retv
) = VT_EMPTY
;
353 V_VT(retv
) = VT_DISPATCH
;
354 V_DISPATCH(retv
) = NULL
;
357 V_VT(retv
) = VT_NULL
;
360 V_VT(retv
) = VT_DISPATCH
;
361 V_DISPATCH(retv
) = get_object(val
);
362 IDispatch_AddRef(get_object(val
));
365 V_VT(retv
) = VT_BSTR
;
366 return jsstr_to_bstr(get_string(val
), &V_BSTR(retv
));
368 double n
= get_number(val
);
381 V_VT(retv
) = VT_BOOL
;
382 V_BOOL(retv
) = get_bool(val
) ? VARIANT_TRUE
: VARIANT_FALSE
;
385 V_VT(retv
) = VT_EMPTY
;
386 return VariantCopy(retv
, get_variant(val
));
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
)) {
402 jsdisp
= iface_to_jsdisp(get_object(val
));
404 return disp_propget(ctx
, get_object(val
), DISPID_VALUE
, ret
);
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
);
415 WARN("call error - forwarding exception\n");
416 jsdisp_release(jsdisp
);
418 }else if(!is_object_instance(prim
)) {
419 jsdisp_release(jsdisp
);
423 IDispatch_Release(get_object(prim
));
425 }else if(hres
!= DISP_E_UNKNOWNNAME
) {
426 jsdisp_release(jsdisp
);
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
);
434 WARN("call error - forwarding exception\n");
435 jsdisp_release(jsdisp
);
437 }else if(!is_object_instance(prim
)) {
438 jsdisp_release(jsdisp
);
442 IDispatch_Release(get_object(prim
));
444 }else if(hres
!= DISP_E_UNKNOWNNAME
) {
445 jsdisp_release(jsdisp
);
449 jsdisp_release(jsdisp
);
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
)) {
471 *ret
= jsstr_length(get_string(val
)) != 0;
474 *ret
= !isnan(get_number(val
)) && get_number(val
);
477 *ret
= get_bool(val
);
480 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
488 static int hex_to_int(WCHAR c
)
490 if('0' <= c
&& c
<= '9')
493 if('a' <= c
&& c
<= 'f')
496 if('A' <= c
&& c
<= 'F')
502 /* ECMA-262 3rd Edition 9.3.1 */
503 static HRESULT
str_to_number(jsstr_t
*str
, double *ret
)
509 static const WCHAR infinityW
[] = L
"Infinity";
511 ptr
= jsstr_flatten(str
);
513 return E_OUTOFMEMORY
;
515 while(iswspace(*ptr
))
521 }else if(*ptr
== '+') {
525 if(!wcsncmp(ptr
, infinityW
, ARRAY_SIZE(infinityW
)-1)) {
526 ptr
+= ARRAY_SIZE(infinityW
) - 1;
527 while(*ptr
&& iswspace(*ptr
))
533 *ret
= neg
? -INFINITY
: INFINITY
;
537 if(*ptr
== '0' && ptr
[1] == 'x') {
541 while((l
= hex_to_int(*ptr
)) != -1) {
550 while(is_digit(*ptr
))
551 d
= d
*10 + (*ptr
++ - '0');
553 if(*ptr
== 'e' || *ptr
== 'E') {
561 }else if(*ptr
== '+') {
565 while(is_digit(*ptr
))
566 l
= l
*10 + (*ptr
++ - '0');
571 }else if(*ptr
== '.') {
575 while(is_digit(*ptr
)) {
576 d
+= dec
* (*ptr
++ - '0');
581 while(iswspace(*ptr
))
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
)) {
607 *ret
= get_number(val
);
610 return str_to_number(get_string(val
), ret
);
615 hres
= to_primitive(ctx
, val
, &prim
, HINT_NUMBER
);
619 hres
= to_number(ctx
, prim
, ret
);
624 *ret
= get_bool(val
) ? 1 : 0;
627 const VARIANT
*v
= get_variant(val
);
630 return variant_date_to_number(V_DATE(v
), ret
);
632 FIXME("unimplemented for variant %s\n", debugstr_variant(v
));
642 /* ECMA-262 3rd Edition 9.4 */
643 HRESULT
to_integer(script_ctx_t
*ctx
, jsval_t v
, double *ret
)
648 hres
= to_number(ctx
, v
, &n
);
655 *ret
= n
>= 0.0 ? floor(n
) : -floor(-n
);
659 static INT32
double_to_int32(double 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)
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
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. */
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
)
707 hres
= to_number(ctx
, v
, &n
);
711 *ret
= double_to_int32(n
);
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
)
726 hres
= to_number(ctx
, val
, &n
);
730 *ret
= double_to_int32(n
);
734 HRESULT
double_to_string(double n
, jsstr_t
**str
)
739 *str
= jsstr_alloc(n
<0 ? L
"-Infinity" : L
"Infinity");
740 }else if(is_int32(n
)) {
742 _ltow_s(n
, buf
, ARRAY_SIZE(buf
), 10);
743 *str
= jsstr_alloc(buf
);
748 /* FIXME: Don't use VariantChangeTypeEx */
751 V_VT(&strv
) = VT_EMPTY
;
752 hres
= VariantChangeTypeEx(&strv
, &v
, MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
), 0, VT_BSTR
);
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
)) {
768 *str
= jsstr_undefined();
771 *str
= jsstr_alloc(L
"null");
774 return double_to_string(get_number(val
), str
);
776 *str
= jsstr_addref(get_string(val
));
782 hres
= to_primitive(ctx
, val
, &prim
, HINT_STRING
);
786 hres
= to_string(ctx
, prim
, str
);
791 *str
= jsstr_alloc(get_bool(val
) ? L
"true" : L
"false");
794 const VARIANT
*v
= get_variant(val
);
798 return variant_date_to_string(ctx
, V_DATE(v
), str
);
800 FIXME("unsupported %s\n", debugstr_variant(v
));
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
)
813 hres
= to_string(ctx
, val
, str
);
817 *ret_str
= jsstr_flatten(*str
);
820 return E_OUTOFMEMORY
;
826 /* ECMA-262 3rd Edition 9.9 */
827 HRESULT
to_object(script_ctx_t
*ctx
, jsval_t val
, IDispatch
**disp
)
832 switch(jsval_type(val
)) {
834 hres
= create_string(ctx
, get_string(val
), &dispex
);
838 *disp
= to_disp(dispex
);
841 hres
= create_number(ctx
, get_number(val
), &dispex
);
845 *disp
= to_disp(dispex
);
848 *disp
= get_object(val
);
849 IDispatch_AddRef(*disp
);
852 hres
= create_bool(ctx
, get_bool(val
), &dispex
);
856 *disp
= to_disp(dispex
);
859 if(is_null_disp(val
))
860 return JS_E_OBJECT_REQUIRED
;
863 WARN("object expected\n");
864 return JS_E_OBJECT_EXPECTED
;
866 switch(V_VT(get_variant(val
))) {
867 case VT_ARRAY
|VT_VARIANT
:
868 hres
= create_vbarray(ctx
, V_ARRAY(get_variant(val
)), &dispex
);
872 *disp
= to_disp(dispex
);
876 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val
)));
885 HRESULT
variant_change_type(script_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
)
891 hres
= variant_to_jsval(ctx
, src
, &val
);
895 enter_script(ctx
, &ei
);
902 hres
= to_int32(ctx
, val
, &i
);
903 if(SUCCEEDED(hres
)) {
914 hres
= to_uint32(ctx
, val
, &i
);
921 hres
= to_number(ctx
, val
, &n
);
929 hres
= to_number(ctx
, val
, &n
);
937 hres
= to_boolean(val
, &b
);
939 V_BOOL(dst
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
945 hres
= to_string(ctx
, val
, &str
);
949 hres
= jsstr_to_bstr(str
, &V_BSTR(dst
));
953 hres
= V_VT(src
) == VT_EMPTY
? S_OK
: E_NOTIMPL
;
956 hres
= V_VT(src
) == VT_NULL
? S_OK
: E_NOTIMPL
;
963 IUnknown_AddRef(V_UNKNOWN(src
));
964 V_UNKNOWN(dst
) = V_UNKNOWN(src
);
969 FIXME("vt %d not implemented\n", vt
);
974 leave_script(ctx
, hres
);
982 static inline JSCaller
*impl_from_IServiceProvider(IServiceProvider
*iface
)
984 return CONTAINING_RECORD(iface
, JSCaller
, IServiceProvider_iface
);
987 static HRESULT WINAPI
JSCaller_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
989 JSCaller
*This
= impl_from_IServiceProvider(iface
);
991 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
992 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
993 *ppv
= &This
->IServiceProvider_iface
;
994 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
995 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
996 *ppv
= &This
->IServiceProvider_iface
;
998 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
1000 return E_NOINTERFACE
;
1003 IUnknown_AddRef((IUnknown
*)*ppv
);
1007 static ULONG WINAPI
JSCaller_AddRef(IServiceProvider
*iface
)
1009 JSCaller
*This
= impl_from_IServiceProvider(iface
);
1010 LONG ref
= InterlockedIncrement(&This
->ref
);
1012 TRACE("(%p) ref=%ld\n", This
, ref
);
1017 static ULONG WINAPI
JSCaller_Release(IServiceProvider
*iface
)
1019 JSCaller
*This
= impl_from_IServiceProvider(iface
);
1020 LONG ref
= InterlockedIncrement(&This
->ref
);
1022 TRACE("(%p) ref=%ld\n", This
, ref
);
1032 static HRESULT WINAPI
JSCaller_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
1033 REFIID riid
, void **ppv
)
1035 JSCaller
*This
= impl_from_IServiceProvider(iface
);
1037 if(IsEqualGUID(guidService
, &SID_VariantConversion
) && This
->ctx
&& This
->ctx
->active_script
) {
1038 TRACE("(%p)->(SID_VariantConversion)\n", This
);
1039 return IActiveScript_QueryInterface(This
->ctx
->active_script
, riid
, ppv
);
1042 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
1045 return E_NOINTERFACE
;
1048 static const IServiceProviderVtbl ServiceProviderVtbl
= {
1049 JSCaller_QueryInterface
,
1052 JSCaller_QueryService
1055 HRESULT
create_jscaller(script_ctx_t
*ctx
)
1059 ret
= heap_alloc(sizeof(*ret
));
1061 return E_OUTOFMEMORY
;
1063 ret
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;
1067 ctx
->jscaller
= ret
;