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
20 #include "wine/port.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
31 WINE_DECLARE_DEBUG_CHANNEL(heap
);
33 const char *debugstr_jsval(const jsval_t v
)
35 switch(jsval_type(v
)) {
41 return wine_dbg_sprintf("obj(%p)", get_object(v
));
43 return wine_dbg_sprintf("str(%s)", debugstr_jsstr(get_string(v
)));
45 return wine_dbg_sprintf("%lf", get_number(v
));
47 return get_bool(v
) ? "true" : "false";
49 return debugstr_variant(get_variant(v
));
56 BOOL
is_finite(double n
)
58 return !isnan(n
) && !isinf(n
);
61 #define MIN_BLOCK_SIZE 128
62 #define ARENA_FREE_FILLER 0xaa
64 static inline DWORD
block_size(DWORD block
)
66 return MIN_BLOCK_SIZE
<< block
;
69 void heap_pool_init(heap_pool_t
*heap
)
71 memset(heap
, 0, sizeof(*heap
));
72 list_init(&heap
->custom_blocks
);
75 void *heap_pool_alloc(heap_pool_t
*heap
, DWORD size
)
80 if(!heap
->block_cnt
) {
82 heap
->blocks
= heap_alloc(sizeof(void*));
87 tmp
= heap_alloc(block_size(0));
91 heap
->blocks
[0] = tmp
;
95 if(heap
->offset
+ size
<= block_size(heap
->last_block
)) {
96 tmp
= ((BYTE
*)heap
->blocks
[heap
->last_block
])+heap
->offset
;
101 if(size
<= block_size(heap
->last_block
+1)) {
102 if(heap
->last_block
+1 == heap
->block_cnt
) {
103 tmp
= heap_realloc(heap
->blocks
, (heap
->block_cnt
+1)*sizeof(void*));
108 heap
->blocks
[heap
->block_cnt
] = heap_alloc(block_size(heap
->block_cnt
));
109 if(!heap
->blocks
[heap
->block_cnt
])
117 return heap
->blocks
[heap
->last_block
];
120 list
= heap_alloc(size
+ sizeof(struct list
));
124 list_add_head(&heap
->custom_blocks
, list
);
128 void *heap_pool_grow(heap_pool_t
*heap
, void *mem
, DWORD size
, DWORD inc
)
132 if(mem
== (BYTE
*)heap
->blocks
[heap
->last_block
] + heap
->offset
-size
133 && heap
->offset
+inc
< block_size(heap
->last_block
)) {
138 ret
= heap_pool_alloc(heap
, size
+inc
);
139 if(ret
) /* FIXME: avoid copying for custom blocks */
140 memcpy(ret
, mem
, size
);
144 void heap_pool_clear(heap_pool_t
*heap
)
151 while((tmp
= list_head(&heap
->custom_blocks
))) {
159 for(i
=0; i
< heap
->block_cnt
; i
++)
160 memset(heap
->blocks
[i
], ARENA_FREE_FILLER
, block_size(i
));
163 heap
->last_block
= heap
->offset
= 0;
167 void heap_pool_free(heap_pool_t
*heap
)
171 heap_pool_clear(heap
);
173 for(i
=0; i
< heap
->block_cnt
; i
++)
174 heap_free(heap
->blocks
[i
]);
175 heap_free(heap
->blocks
);
177 heap_pool_init(heap
);
180 heap_pool_t
*heap_pool_mark(heap_pool_t
*heap
)
189 void jsval_release(jsval_t val
)
191 switch(jsval_type(val
)) {
194 IDispatch_Release(get_object(val
));
197 jsstr_release(get_string(val
));
200 VariantClear(get_variant(val
));
201 heap_free(get_variant(val
));
208 static HRESULT
jsval_variant(jsval_t
*val
, VARIANT
*var
)
213 __JSVAL_TYPE(*val
) = JSV_VARIANT
;
214 __JSVAL_VAR(*val
) = v
= heap_alloc(sizeof(VARIANT
));
216 *val
= jsval_undefined();
217 return E_OUTOFMEMORY
;
221 hres
= VariantCopy(v
, var
);
223 *val
= jsval_undefined();
229 HRESULT
jsval_copy(jsval_t v
, jsval_t
*r
)
231 switch(jsval_type(v
)) {
240 IDispatch_AddRef(get_object(v
));
244 jsstr_addref(get_string(v
));
249 return jsval_variant(r
, get_variant(v
));
256 HRESULT
variant_to_jsval(VARIANT
*var
, jsval_t
*r
)
258 if(V_VT(var
) == (VT_VARIANT
|VT_BYREF
))
259 var
= V_VARIANTREF(var
);
263 *r
= jsval_undefined();
269 *r
= jsval_bool(V_BOOL(var
));
272 *r
= jsval_number(V_I4(var
));
275 *r
= jsval_number(V_R8(var
));
281 str
= jsstr_alloc_len(V_BSTR(var
), SysStringLen(V_BSTR(var
)));
283 return E_OUTOFMEMORY
;
285 str
= jsstr_null_bstr();
288 *r
= jsval_string(str
);
293 IDispatch_AddRef(V_DISPATCH(var
));
294 *r
= jsval_disp(V_DISPATCH(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
));
324 hres
= IUnknown_QueryInterface(V_UNKNOWN(var
), &IID_IDispatch
, (void**)&disp
);
325 if(SUCCEEDED(hres
)) {
326 *r
= jsval_disp(disp
);
330 *r
= jsval_disp(NULL
);
335 return jsval_variant(r
, var
);
339 HRESULT
jsval_to_variant(jsval_t val
, VARIANT
*retv
)
341 switch(jsval_type(val
)) {
343 V_VT(retv
) = VT_EMPTY
;
346 V_VT(retv
) = VT_NULL
;
349 V_VT(retv
) = VT_DISPATCH
;
351 IDispatch_AddRef(get_object(val
));
352 V_DISPATCH(retv
) = get_object(val
);
355 jsstr_t
*str
= get_string(val
);
357 V_VT(retv
) = VT_BSTR
;
358 if(is_null_bstr(str
)) {
361 V_BSTR(retv
) = SysAllocStringLen(NULL
, jsstr_length(str
));
363 jsstr_flush(str
, V_BSTR(retv
));
365 return E_OUTOFMEMORY
;
370 double n
= get_number(val
);
383 V_VT(retv
) = VT_BOOL
;
384 V_BOOL(retv
) = get_bool(val
) ? VARIANT_TRUE
: VARIANT_FALSE
;
387 V_VT(retv
) = VT_EMPTY
;
388 return VariantCopy(retv
, get_variant(val
));
395 /* ECMA-262 3rd Edition 9.1 */
396 HRESULT
to_primitive(script_ctx_t
*ctx
, jsval_t val
, jsval_t
*ret
, hint_t hint
)
398 if(is_object_instance(val
)) {
404 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
405 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
407 if(!get_object(val
)) {
412 jsdisp
= iface_to_jsdisp(get_object(val
));
414 return disp_propget(ctx
, get_object(val
), DISPID_VALUE
, ret
);
417 hint
= is_class(jsdisp
, JSCLASS_DATE
) ? HINT_STRING
: HINT_NUMBER
;
419 /* Native implementation doesn't throw TypeErrors, returns strange values */
421 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? toStringW
: valueOfW
, 0, &id
);
422 if(SUCCEEDED(hres
)) {
423 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, 0, NULL
, &prim
);
425 WARN("call error - forwarding exception\n");
426 jsdisp_release(jsdisp
);
428 }else if(!is_object_instance(prim
)) {
429 jsdisp_release(jsdisp
);
433 IDispatch_Release(get_object(prim
));
437 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? valueOfW
: toStringW
, 0, &id
);
438 if(SUCCEEDED(hres
)) {
439 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, 0, NULL
, &prim
);
441 WARN("call error - forwarding exception\n");
442 jsdisp_release(jsdisp
);
444 }else if(!is_object_instance(prim
)) {
445 jsdisp_release(jsdisp
);
449 IDispatch_Release(get_object(prim
));
453 jsdisp_release(jsdisp
);
456 return throw_type_error(ctx
, JS_E_TO_PRIMITIVE
, NULL
);
459 return jsval_copy(val
, ret
);
463 /* ECMA-262 3rd Edition 9.2 */
464 HRESULT
to_boolean(jsval_t val
, BOOL
*ret
)
466 switch(jsval_type(val
)) {
472 *ret
= get_object(val
) != NULL
;
475 *ret
= jsstr_length(get_string(val
)) != 0;
478 *ret
= !isnan(get_number(val
)) && get_number(val
);
481 *ret
= get_bool(val
);
484 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
492 static int hex_to_int(WCHAR c
)
494 if('0' <= c
&& c
<= '9')
497 if('a' <= c
&& c
<= 'f')
500 if('A' <= c
&& c
<= 'F')
506 /* ECMA-262 3rd Edition 9.3.1 */
507 static HRESULT
str_to_number(jsstr_t
*str
, double *ret
)
513 static const WCHAR infinityW
[] = {'I','n','f','i','n','i','t','y'};
515 ptr
= jsstr_flatten(str
);
517 return E_OUTOFMEMORY
;
519 while(isspaceW(*ptr
))
525 }else if(*ptr
== '+') {
529 if(!strncmpW(ptr
, infinityW
, ARRAY_SIZE(infinityW
))) {
530 ptr
+= ARRAY_SIZE(infinityW
);
531 while(*ptr
&& isspaceW(*ptr
))
537 *ret
= neg
? -INFINITY
: INFINITY
;
541 if(*ptr
== '0' && ptr
[1] == 'x') {
545 while((l
= hex_to_int(*ptr
)) != -1) {
554 while(isdigitW(*ptr
))
555 d
= d
*10 + (*ptr
++ - '0');
557 if(*ptr
== 'e' || *ptr
== 'E') {
565 }else if(*ptr
== '+') {
569 while(isdigitW(*ptr
))
570 l
= l
*10 + (*ptr
++ - '0');
575 }else if(*ptr
== '.') {
579 while(isdigitW(*ptr
)) {
580 d
+= dec
* (*ptr
++ - '0');
585 while(isspaceW(*ptr
))
600 /* ECMA-262 3rd Edition 9.3 */
601 HRESULT
to_number(script_ctx_t
*ctx
, jsval_t val
, double *ret
)
603 switch(jsval_type(val
)) {
611 *ret
= get_number(val
);
614 return str_to_number(get_string(val
), ret
);
619 hres
= to_primitive(ctx
, val
, &prim
, HINT_NUMBER
);
623 hres
= to_number(ctx
, prim
, ret
);
628 *ret
= get_bool(val
) ? 1 : 0;
631 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
639 /* ECMA-262 3rd Edition 9.4 */
640 HRESULT
to_integer(script_ctx_t
*ctx
, jsval_t v
, double *ret
)
645 hres
= to_number(ctx
, v
, &n
);
652 *ret
= n
>= 0.0 ? floor(n
) : -floor(-n
);
656 /* ECMA-262 3rd Edition 9.5 */
657 HRESULT
to_int32(script_ctx_t
*ctx
, jsval_t v
, INT
*ret
)
662 const double p32
= (double)0xffffffff + 1;
664 hres
= to_number(ctx
, v
, &n
);
669 n
= n
> 0 ? fmod(n
, p32
) : -fmod(-n
, p32
);
677 /* ECMA-262 3rd Edition 9.6 */
678 HRESULT
to_uint32(script_ctx_t
*ctx
, jsval_t val
, DWORD
*ret
)
683 hres
= to_int32(ctx
, val
, &n
);
689 static jsstr_t
*int_to_string(int i
)
695 static const WCHAR zeroW
[] = {'0',0};
696 return jsstr_alloc(zeroW
);
704 p
= buf
+ ARRAY_SIZE(buf
)-1;
716 return jsstr_alloc(p
);
719 HRESULT
double_to_string(double n
, jsstr_t
**str
)
721 const WCHAR InfinityW
[] = {'-','I','n','f','i','n','i','t','y',0};
726 *str
= jsstr_alloc(n
<0 ? InfinityW
: InfinityW
+1);
727 }else if(is_int32(n
)) {
728 *str
= int_to_string(n
);
733 /* FIXME: Don't use VariantChangeTypeEx */
736 V_VT(&strv
) = VT_EMPTY
;
737 hres
= VariantChangeTypeEx(&strv
, &v
, MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
), 0, VT_BSTR
);
741 *str
= jsstr_alloc(V_BSTR(&strv
));
742 SysFreeString(V_BSTR(&strv
));
745 return *str
? S_OK
: E_OUTOFMEMORY
;
748 /* ECMA-262 3rd Edition 9.8 */
749 HRESULT
to_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
)
751 const WCHAR nullW
[] = {'n','u','l','l',0};
752 const WCHAR trueW
[] = {'t','r','u','e',0};
753 const WCHAR falseW
[] = {'f','a','l','s','e',0};
755 switch(jsval_type(val
)) {
757 *str
= jsstr_undefined();
760 *str
= jsstr_alloc(nullW
);
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
) ? trueW
: falseW
);
783 FIXME("unsupported %s\n", debugstr_jsval(val
));
787 return *str
? S_OK
: E_OUTOFMEMORY
;
790 HRESULT
to_flat_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
, const WCHAR
**ret_str
)
794 hres
= to_string(ctx
, val
, str
);
798 *ret_str
= jsstr_flatten(*str
);
801 return E_OUTOFMEMORY
;
807 /* ECMA-262 3rd Edition 9.9 */
808 HRESULT
to_object(script_ctx_t
*ctx
, jsval_t val
, IDispatch
**disp
)
813 switch(jsval_type(val
)) {
815 hres
= create_string(ctx
, get_string(val
), &dispex
);
819 *disp
= to_disp(dispex
);
822 hres
= create_number(ctx
, get_number(val
), &dispex
);
826 *disp
= to_disp(dispex
);
829 if(get_object(val
)) {
830 *disp
= get_object(val
);
831 IDispatch_AddRef(*disp
);
835 hres
= create_object(ctx
, NULL
, &obj
);
839 *disp
= to_disp(obj
);
843 hres
= create_bool(ctx
, get_bool(val
), &dispex
);
847 *disp
= to_disp(dispex
);
851 WARN("object expected\n");
852 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
854 switch(V_VT(get_variant(val
))) {
855 case VT_ARRAY
|VT_VARIANT
:
856 hres
= create_vbarray(ctx
, V_ARRAY(get_variant(val
)), &dispex
);
860 *disp
= to_disp(dispex
);
864 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val
)));
873 HRESULT
variant_change_type(script_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
)
879 hres
= variant_to_jsval(src
, &val
);
888 hres
= to_int32(ctx
, val
, &i
);
889 if(SUCCEEDED(hres
)) {
899 hres
= to_number(ctx
, val
, &n
);
907 hres
= to_number(ctx
, val
, &n
);
915 hres
= to_boolean(val
, &b
);
917 V_BOOL(dst
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
923 hres
= to_string(ctx
, val
, &str
);
927 if(is_null_bstr(str
)) {
932 V_BSTR(dst
) = SysAllocStringLen(NULL
, jsstr_length(str
));
934 jsstr_flush(str
, V_BSTR(dst
));
936 hres
= E_OUTOFMEMORY
;
940 hres
= V_VT(src
) == VT_EMPTY
? S_OK
: E_NOTIMPL
;
943 hres
= V_VT(src
) == VT_NULL
? S_OK
: E_NOTIMPL
;
946 FIXME("vt %d not implemented\n", vt
);
958 static inline JSCaller
*impl_from_IServiceProvider(IServiceProvider
*iface
)
960 return CONTAINING_RECORD(iface
, JSCaller
, IServiceProvider_iface
);
963 static HRESULT WINAPI
JSCaller_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
965 JSCaller
*This
= impl_from_IServiceProvider(iface
);
967 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
968 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
969 *ppv
= &This
->IServiceProvider_iface
;
970 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
971 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
972 *ppv
= &This
->IServiceProvider_iface
;
974 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
976 return E_NOINTERFACE
;
979 IUnknown_AddRef((IUnknown
*)*ppv
);
983 static ULONG WINAPI
JSCaller_AddRef(IServiceProvider
*iface
)
985 JSCaller
*This
= impl_from_IServiceProvider(iface
);
986 LONG ref
= InterlockedIncrement(&This
->ref
);
988 TRACE("(%p) ref=%d\n", This
, ref
);
993 static ULONG WINAPI
JSCaller_Release(IServiceProvider
*iface
)
995 JSCaller
*This
= impl_from_IServiceProvider(iface
);
996 LONG ref
= InterlockedIncrement(&This
->ref
);
998 TRACE("(%p) ref=%d\n", This
, ref
);
1008 static HRESULT WINAPI
JSCaller_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
1009 REFIID riid
, void **ppv
)
1011 JSCaller
*This
= impl_from_IServiceProvider(iface
);
1013 if(IsEqualGUID(guidService
, &SID_VariantConversion
) && This
->ctx
&& This
->ctx
->active_script
) {
1014 TRACE("(%p)->(SID_VariantConversion)\n", This
);
1015 return IActiveScript_QueryInterface(This
->ctx
->active_script
, riid
, ppv
);
1018 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
1021 return E_NOINTERFACE
;
1024 static const IServiceProviderVtbl ServiceProviderVtbl
= {
1025 JSCaller_QueryInterface
,
1028 JSCaller_QueryService
1031 HRESULT
create_jscaller(script_ctx_t
*ctx
)
1035 ret
= heap_alloc(sizeof(*ret
));
1037 return E_OUTOFMEMORY
;
1039 ret
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;
1043 ctx
->jscaller
= ret
;