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
));
319 /* FIXME: Native converts VT_CY to a special kind number type, which is
320 * never converted to VT_I4 when it's converted back to VARIANT. */
321 *r
= jsval_number((double)V_CY(var
).int64
/ 10000.0);
322 WARN("VT_CY: %lf\n", get_number(*r
));
329 hres
= IUnknown_QueryInterface(V_UNKNOWN(var
), &IID_IDispatch
, (void**)&disp
);
330 if(SUCCEEDED(hres
)) {
331 *r
= jsval_disp(disp
);
335 *r
= jsval_disp(NULL
);
340 return jsval_variant(r
, var
);
344 HRESULT
jsval_to_variant(jsval_t val
, VARIANT
*retv
)
346 switch(jsval_type(val
)) {
348 V_VT(retv
) = VT_EMPTY
;
351 V_VT(retv
) = VT_NULL
;
354 V_VT(retv
) = VT_DISPATCH
;
356 IDispatch_AddRef(get_object(val
));
357 V_DISPATCH(retv
) = get_object(val
);
360 V_VT(retv
) = VT_BSTR
;
361 return jsstr_to_bstr(get_string(val
), &V_BSTR(retv
));
363 double n
= get_number(val
);
376 V_VT(retv
) = VT_BOOL
;
377 V_BOOL(retv
) = get_bool(val
) ? VARIANT_TRUE
: VARIANT_FALSE
;
380 V_VT(retv
) = VT_EMPTY
;
381 return VariantCopy(retv
, get_variant(val
));
388 /* ECMA-262 3rd Edition 9.1 */
389 HRESULT
to_primitive(script_ctx_t
*ctx
, jsval_t val
, jsval_t
*ret
, hint_t hint
)
391 if(is_object_instance(val
)) {
397 if(!get_object(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
)) {
462 *ret
= get_object(val
) != NULL
;
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 /* ECMA-262 3rd Edition 9.6 */
710 HRESULT
to_uint32(script_ctx_t
*ctx
, jsval_t val
, UINT32
*ret
)
715 hres
= to_number(ctx
, val
, &n
);
719 *ret
= double_to_int32(n
);
723 HRESULT
double_to_string(double n
, jsstr_t
**str
)
728 *str
= jsstr_alloc(n
<0 ? L
"-Infinity" : L
"Infinity");
729 }else if(is_int32(n
)) {
731 _ltow_s(n
, buf
, ARRAY_SIZE(buf
), 10);
732 *str
= jsstr_alloc(buf
);
737 /* FIXME: Don't use VariantChangeTypeEx */
740 V_VT(&strv
) = VT_EMPTY
;
741 hres
= VariantChangeTypeEx(&strv
, &v
, MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
), 0, VT_BSTR
);
745 *str
= jsstr_alloc(V_BSTR(&strv
));
746 SysFreeString(V_BSTR(&strv
));
749 return *str
? S_OK
: E_OUTOFMEMORY
;
752 /* ECMA-262 3rd Edition 9.8 */
753 HRESULT
to_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
)
755 switch(jsval_type(val
)) {
757 *str
= jsstr_undefined();
760 *str
= jsstr_alloc(L
"null");
763 return double_to_string(get_number(val
), str
);
765 *str
= jsstr_addref(get_string(val
));
771 hres
= to_primitive(ctx
, val
, &prim
, HINT_STRING
);
775 hres
= to_string(ctx
, prim
, str
);
780 *str
= jsstr_alloc(get_bool(val
) ? L
"true" : L
"false");
783 const VARIANT
*v
= get_variant(val
);
787 return variant_date_to_string(ctx
, V_DATE(v
), str
);
789 FIXME("unsupported %s\n", debugstr_variant(v
));
795 return *str
? S_OK
: E_OUTOFMEMORY
;
798 HRESULT
to_flat_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
, const WCHAR
**ret_str
)
802 hres
= to_string(ctx
, val
, str
);
806 *ret_str
= jsstr_flatten(*str
);
809 return E_OUTOFMEMORY
;
815 /* ECMA-262 3rd Edition 9.9 */
816 HRESULT
to_object(script_ctx_t
*ctx
, jsval_t val
, IDispatch
**disp
)
821 switch(jsval_type(val
)) {
823 hres
= create_string(ctx
, get_string(val
), &dispex
);
827 *disp
= to_disp(dispex
);
830 hres
= create_number(ctx
, get_number(val
), &dispex
);
834 *disp
= to_disp(dispex
);
837 if(get_object(val
)) {
838 *disp
= get_object(val
);
839 IDispatch_AddRef(*disp
);
843 hres
= create_object(ctx
, NULL
, &obj
);
847 *disp
= to_disp(obj
);
851 hres
= create_bool(ctx
, get_bool(val
), &dispex
);
855 *disp
= to_disp(dispex
);
859 WARN("object expected\n");
860 return JS_E_OBJECT_EXPECTED
;
862 switch(V_VT(get_variant(val
))) {
863 case VT_ARRAY
|VT_VARIANT
:
864 hres
= create_vbarray(ctx
, V_ARRAY(get_variant(val
)), &dispex
);
868 *disp
= to_disp(dispex
);
872 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val
)));
881 HRESULT
variant_change_type(script_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
)
887 hres
= variant_to_jsval(src
, &val
);
891 enter_script(ctx
, &ei
);
898 hres
= to_int32(ctx
, val
, &i
);
899 if(SUCCEEDED(hres
)) {
910 hres
= to_uint32(ctx
, val
, &i
);
917 hres
= to_number(ctx
, val
, &n
);
925 hres
= to_number(ctx
, val
, &n
);
933 hres
= to_boolean(val
, &b
);
935 V_BOOL(dst
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
941 hres
= to_string(ctx
, val
, &str
);
945 hres
= jsstr_to_bstr(str
, &V_BSTR(dst
));
949 hres
= V_VT(src
) == VT_EMPTY
? S_OK
: E_NOTIMPL
;
952 hres
= V_VT(src
) == VT_NULL
? S_OK
: E_NOTIMPL
;
955 FIXME("vt %d not implemented\n", vt
);
960 leave_script(ctx
, hres
);
968 static inline JSCaller
*impl_from_IServiceProvider(IServiceProvider
*iface
)
970 return CONTAINING_RECORD(iface
, JSCaller
, IServiceProvider_iface
);
973 static HRESULT WINAPI
JSCaller_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
975 JSCaller
*This
= impl_from_IServiceProvider(iface
);
977 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
978 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
979 *ppv
= &This
->IServiceProvider_iface
;
980 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
981 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
982 *ppv
= &This
->IServiceProvider_iface
;
984 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
986 return E_NOINTERFACE
;
989 IUnknown_AddRef((IUnknown
*)*ppv
);
993 static ULONG WINAPI
JSCaller_AddRef(IServiceProvider
*iface
)
995 JSCaller
*This
= impl_from_IServiceProvider(iface
);
996 LONG ref
= InterlockedIncrement(&This
->ref
);
998 TRACE("(%p) ref=%d\n", This
, ref
);
1003 static ULONG WINAPI
JSCaller_Release(IServiceProvider
*iface
)
1005 JSCaller
*This
= impl_from_IServiceProvider(iface
);
1006 LONG ref
= InterlockedIncrement(&This
->ref
);
1008 TRACE("(%p) ref=%d\n", This
, ref
);
1018 static HRESULT WINAPI
JSCaller_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
1019 REFIID riid
, void **ppv
)
1021 JSCaller
*This
= impl_from_IServiceProvider(iface
);
1023 if(IsEqualGUID(guidService
, &SID_VariantConversion
) && This
->ctx
&& This
->ctx
->active_script
) {
1024 TRACE("(%p)->(SID_VariantConversion)\n", This
);
1025 return IActiveScript_QueryInterface(This
->ctx
->active_script
, riid
, ppv
);
1028 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
1031 return E_NOINTERFACE
;
1034 static const IServiceProviderVtbl ServiceProviderVtbl
= {
1035 JSCaller_QueryInterface
,
1038 JSCaller_QueryService
1041 HRESULT
create_jscaller(script_ctx_t
*ctx
)
1045 ret
= heap_alloc(sizeof(*ret
));
1047 return E_OUTOFMEMORY
;
1049 ret
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;
1053 ctx
->jscaller
= ret
;