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
));
427 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? L
"valueOf" : L
"toString", 0, &id
);
428 if(SUCCEEDED(hres
)) {
429 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, 0, NULL
, &prim
);
431 WARN("call error - forwarding exception\n");
432 jsdisp_release(jsdisp
);
434 }else if(!is_object_instance(prim
)) {
435 jsdisp_release(jsdisp
);
439 IDispatch_Release(get_object(prim
));
443 jsdisp_release(jsdisp
);
446 return JS_E_TO_PRIMITIVE
;
449 return jsval_copy(val
, ret
);
453 /* ECMA-262 3rd Edition 9.2 */
454 HRESULT
to_boolean(jsval_t val
, BOOL
*ret
)
456 switch(jsval_type(val
)) {
465 *ret
= jsstr_length(get_string(val
)) != 0;
468 *ret
= !isnan(get_number(val
)) && get_number(val
);
471 *ret
= get_bool(val
);
474 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
482 static int hex_to_int(WCHAR c
)
484 if('0' <= c
&& c
<= '9')
487 if('a' <= c
&& c
<= 'f')
490 if('A' <= c
&& c
<= 'F')
496 /* ECMA-262 3rd Edition 9.3.1 */
497 static HRESULT
str_to_number(jsstr_t
*str
, double *ret
)
503 static const WCHAR infinityW
[] = L
"Infinity";
505 ptr
= jsstr_flatten(str
);
507 return E_OUTOFMEMORY
;
509 while(iswspace(*ptr
))
515 }else if(*ptr
== '+') {
519 if(!wcsncmp(ptr
, infinityW
, ARRAY_SIZE(infinityW
)-1)) {
520 ptr
+= ARRAY_SIZE(infinityW
) - 1;
521 while(*ptr
&& iswspace(*ptr
))
527 *ret
= neg
? -INFINITY
: INFINITY
;
531 if(*ptr
== '0' && ptr
[1] == 'x') {
535 while((l
= hex_to_int(*ptr
)) != -1) {
544 while(is_digit(*ptr
))
545 d
= d
*10 + (*ptr
++ - '0');
547 if(*ptr
== 'e' || *ptr
== 'E') {
555 }else if(*ptr
== '+') {
559 while(is_digit(*ptr
))
560 l
= l
*10 + (*ptr
++ - '0');
565 }else if(*ptr
== '.') {
569 while(is_digit(*ptr
)) {
570 d
+= dec
* (*ptr
++ - '0');
575 while(iswspace(*ptr
))
590 /* ECMA-262 3rd Edition 9.3 */
591 HRESULT
to_number(script_ctx_t
*ctx
, jsval_t val
, double *ret
)
593 switch(jsval_type(val
)) {
601 *ret
= get_number(val
);
604 return str_to_number(get_string(val
), ret
);
609 hres
= to_primitive(ctx
, val
, &prim
, HINT_NUMBER
);
613 hres
= to_number(ctx
, prim
, ret
);
618 *ret
= get_bool(val
) ? 1 : 0;
621 const VARIANT
*v
= get_variant(val
);
624 return variant_date_to_number(V_DATE(v
), ret
);
626 FIXME("unimplemented for variant %s\n", debugstr_variant(v
));
636 /* ECMA-262 3rd Edition 9.4 */
637 HRESULT
to_integer(script_ctx_t
*ctx
, jsval_t v
, double *ret
)
642 hres
= to_number(ctx
, v
, &n
);
649 *ret
= n
>= 0.0 ? floor(n
) : -floor(-n
);
653 static INT32
double_to_int32(double number
)
662 exp
= ((INT32
)(bits
.n
>> 52) & 0x7ff) - 0x3ff;
664 /* If exponent < 0 there will be no bits to the left of the decimal point
665 * after rounding; if the exponent is > 83 then no bits of precision can be
666 * left in the low 32-bit range of the result (IEEE-754 doubles have 52 bits
667 * of fractional precision).
668 * Note this case handles 0, -0, and all infinite, NaN & denormal values. */
669 if(exp
< 0 || exp
> 83)
672 /* Select the appropriate 32-bits from the floating point mantissa. If the
673 * exponent is 52 then the bits we need to select are already aligned to the
674 * lowest bits of the 64-bit integer representation of the number, no need
675 * to shift. If the exponent is greater than 52 we need to shift the value
676 * left by (exp - 52), if the value is less than 52 we need to shift right
678 result
= (exp
> 52) ? bits
.n
<< (exp
- 52) : bits
.n
>> (52 - exp
);
680 /* IEEE-754 double precision values are stored omitting an implicit 1 before
681 * the decimal point; we need to reinsert this now. We may also the shifted
682 * invalid bits into the result that are not a part of the mantissa (the sign
683 * and exponent bits from the floatingpoint representation); mask these out. */
685 INT32 missing_one
= 1 << exp
;
686 result
&= missing_one
- 1;
687 result
+= missing_one
;
690 /* If the input value was negative (we could test either 'number' or 'bits',
691 * but testing 'bits' is likely faster) invert the result appropriately. */
692 return bits
.n
< 0 ? -result
: result
;
695 /* ECMA-262 3rd Edition 9.5 */
696 HRESULT
to_int32(script_ctx_t
*ctx
, jsval_t v
, INT
*ret
)
701 hres
= to_number(ctx
, v
, &n
);
705 *ret
= double_to_int32(n
);
709 HRESULT
to_long(script_ctx_t
*ctx
, jsval_t v
, LONG
*ret
)
711 return to_int32(ctx
, v
, (INT
*)ret
);
714 /* ECMA-262 3rd Edition 9.6 */
715 HRESULT
to_uint32(script_ctx_t
*ctx
, jsval_t val
, UINT32
*ret
)
720 hres
= to_number(ctx
, val
, &n
);
724 *ret
= double_to_int32(n
);
728 HRESULT
double_to_string(double n
, jsstr_t
**str
)
733 *str
= jsstr_alloc(n
<0 ? L
"-Infinity" : L
"Infinity");
734 }else if(is_int32(n
)) {
736 _ltow_s(n
, buf
, ARRAY_SIZE(buf
), 10);
737 *str
= jsstr_alloc(buf
);
742 /* FIXME: Don't use VariantChangeTypeEx */
745 V_VT(&strv
) = VT_EMPTY
;
746 hres
= VariantChangeTypeEx(&strv
, &v
, MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
), 0, VT_BSTR
);
750 *str
= jsstr_alloc(V_BSTR(&strv
));
751 SysFreeString(V_BSTR(&strv
));
754 return *str
? S_OK
: E_OUTOFMEMORY
;
757 /* ECMA-262 3rd Edition 9.8 */
758 HRESULT
to_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
)
760 switch(jsval_type(val
)) {
762 *str
= jsstr_undefined();
765 *str
= jsstr_alloc(L
"null");
768 return double_to_string(get_number(val
), str
);
770 *str
= jsstr_addref(get_string(val
));
776 hres
= to_primitive(ctx
, val
, &prim
, HINT_STRING
);
780 hres
= to_string(ctx
, prim
, str
);
785 *str
= jsstr_alloc(get_bool(val
) ? L
"true" : L
"false");
788 const VARIANT
*v
= get_variant(val
);
792 return variant_date_to_string(ctx
, V_DATE(v
), str
);
794 FIXME("unsupported %s\n", debugstr_variant(v
));
800 return *str
? S_OK
: E_OUTOFMEMORY
;
803 HRESULT
to_flat_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
, const WCHAR
**ret_str
)
807 hres
= to_string(ctx
, val
, str
);
811 *ret_str
= jsstr_flatten(*str
);
814 return E_OUTOFMEMORY
;
820 /* ECMA-262 3rd Edition 9.9 */
821 HRESULT
to_object(script_ctx_t
*ctx
, jsval_t val
, IDispatch
**disp
)
826 switch(jsval_type(val
)) {
828 hres
= create_string(ctx
, get_string(val
), &dispex
);
832 *disp
= to_disp(dispex
);
835 hres
= create_number(ctx
, get_number(val
), &dispex
);
839 *disp
= to_disp(dispex
);
842 *disp
= get_object(val
);
843 IDispatch_AddRef(*disp
);
846 hres
= create_bool(ctx
, get_bool(val
), &dispex
);
850 *disp
= to_disp(dispex
);
853 if(is_null_disp(val
))
854 return JS_E_OBJECT_REQUIRED
;
857 WARN("object expected\n");
858 return JS_E_OBJECT_EXPECTED
;
860 switch(V_VT(get_variant(val
))) {
861 case VT_ARRAY
|VT_VARIANT
:
862 hres
= create_vbarray(ctx
, V_ARRAY(get_variant(val
)), &dispex
);
866 *disp
= to_disp(dispex
);
870 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val
)));
879 HRESULT
variant_change_type(script_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
)
885 hres
= variant_to_jsval(ctx
, src
, &val
);
889 enter_script(ctx
, &ei
);
896 hres
= to_int32(ctx
, val
, &i
);
897 if(SUCCEEDED(hres
)) {
908 hres
= to_uint32(ctx
, val
, &i
);
915 hres
= to_number(ctx
, val
, &n
);
923 hres
= to_number(ctx
, val
, &n
);
931 hres
= to_boolean(val
, &b
);
933 V_BOOL(dst
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
939 hres
= to_string(ctx
, val
, &str
);
943 hres
= jsstr_to_bstr(str
, &V_BSTR(dst
));
947 hres
= V_VT(src
) == VT_EMPTY
? S_OK
: E_NOTIMPL
;
950 hres
= V_VT(src
) == VT_NULL
? S_OK
: E_NOTIMPL
;
953 FIXME("vt %d not implemented\n", vt
);
958 leave_script(ctx
, hres
);
966 static inline JSCaller
*impl_from_IServiceProvider(IServiceProvider
*iface
)
968 return CONTAINING_RECORD(iface
, JSCaller
, IServiceProvider_iface
);
971 static HRESULT WINAPI
JSCaller_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
973 JSCaller
*This
= impl_from_IServiceProvider(iface
);
975 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
976 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
977 *ppv
= &This
->IServiceProvider_iface
;
978 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
979 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
980 *ppv
= &This
->IServiceProvider_iface
;
982 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
984 return E_NOINTERFACE
;
987 IUnknown_AddRef((IUnknown
*)*ppv
);
991 static ULONG WINAPI
JSCaller_AddRef(IServiceProvider
*iface
)
993 JSCaller
*This
= impl_from_IServiceProvider(iface
);
994 LONG ref
= InterlockedIncrement(&This
->ref
);
996 TRACE("(%p) ref=%ld\n", This
, ref
);
1001 static ULONG WINAPI
JSCaller_Release(IServiceProvider
*iface
)
1003 JSCaller
*This
= impl_from_IServiceProvider(iface
);
1004 LONG ref
= InterlockedIncrement(&This
->ref
);
1006 TRACE("(%p) ref=%ld\n", This
, ref
);
1016 static HRESULT WINAPI
JSCaller_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
1017 REFIID riid
, void **ppv
)
1019 JSCaller
*This
= impl_from_IServiceProvider(iface
);
1021 if(IsEqualGUID(guidService
, &SID_VariantConversion
) && This
->ctx
&& This
->ctx
->active_script
) {
1022 TRACE("(%p)->(SID_VariantConversion)\n", This
);
1023 return IActiveScript_QueryInterface(This
->ctx
->active_script
, riid
, ppv
);
1026 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
1029 return E_NOINTERFACE
;
1032 static const IServiceProviderVtbl ServiceProviderVtbl
= {
1033 JSCaller_QueryInterface
,
1036 JSCaller_QueryService
1039 HRESULT
create_jscaller(script_ctx_t
*ctx
)
1043 ret
= heap_alloc(sizeof(*ret
));
1045 return E_OUTOFMEMORY
;
1047 ret
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;
1051 ctx
->jscaller
= ret
;