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 #define MIN_BLOCK_SIZE 128
57 #define ARENA_FREE_FILLER 0xaa
59 static inline DWORD
block_size(DWORD block
)
61 return MIN_BLOCK_SIZE
<< block
;
64 void heap_pool_init(heap_pool_t
*heap
)
66 memset(heap
, 0, sizeof(*heap
));
67 list_init(&heap
->custom_blocks
);
70 void *heap_pool_alloc(heap_pool_t
*heap
, DWORD size
)
75 if(!heap
->block_cnt
) {
77 heap
->blocks
= heap_alloc(sizeof(void*));
82 tmp
= heap_alloc(block_size(0));
86 heap
->blocks
[0] = tmp
;
90 if(heap
->offset
+ size
<= block_size(heap
->last_block
)) {
91 tmp
= ((BYTE
*)heap
->blocks
[heap
->last_block
])+heap
->offset
;
96 if(size
<= block_size(heap
->last_block
+1)) {
97 if(heap
->last_block
+1 == heap
->block_cnt
) {
98 tmp
= heap_realloc(heap
->blocks
, (heap
->block_cnt
+1)*sizeof(void*));
103 heap
->blocks
[heap
->block_cnt
] = heap_alloc(block_size(heap
->block_cnt
));
104 if(!heap
->blocks
[heap
->block_cnt
])
112 return heap
->blocks
[heap
->last_block
];
115 list
= heap_alloc(size
+ sizeof(struct list
));
119 list_add_head(&heap
->custom_blocks
, list
);
123 void *heap_pool_grow(heap_pool_t
*heap
, void *mem
, DWORD size
, DWORD inc
)
127 if(mem
== (BYTE
*)heap
->blocks
[heap
->last_block
] + heap
->offset
-size
128 && heap
->offset
+inc
< block_size(heap
->last_block
)) {
133 ret
= heap_pool_alloc(heap
, size
+inc
);
134 if(ret
) /* FIXME: avoid copying for custom blocks */
135 memcpy(ret
, mem
, size
);
139 void heap_pool_clear(heap_pool_t
*heap
)
146 while((tmp
= list_next(&heap
->custom_blocks
, &heap
->custom_blocks
))) {
154 for(i
=0; i
< heap
->block_cnt
; i
++)
155 memset(heap
->blocks
[i
], ARENA_FREE_FILLER
, block_size(i
));
158 heap
->last_block
= heap
->offset
= 0;
162 void heap_pool_free(heap_pool_t
*heap
)
166 heap_pool_clear(heap
);
168 for(i
=0; i
< heap
->block_cnt
; i
++)
169 heap_free(heap
->blocks
[i
]);
170 heap_free(heap
->blocks
);
172 heap_pool_init(heap
);
175 heap_pool_t
*heap_pool_mark(heap_pool_t
*heap
)
184 void jsval_release(jsval_t val
)
186 switch(jsval_type(val
)) {
189 IDispatch_Release(get_object(val
));
192 jsstr_release(get_string(val
));
195 VariantClear(get_variant(val
));
196 heap_free(get_variant(val
));
203 static HRESULT
jsval_variant(jsval_t
*val
, VARIANT
*var
)
208 __JSVAL_TYPE(*val
) = JSV_VARIANT
;
209 __JSVAL_VAR(*val
) = v
= heap_alloc(sizeof(VARIANT
));
211 return E_OUTOFMEMORY
;
214 hres
= VariantCopy(v
, var
);
220 HRESULT
jsval_copy(jsval_t v
, jsval_t
*r
)
222 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(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
);
284 IDispatch_AddRef(V_DISPATCH(var
));
285 *r
= jsval_disp(V_DISPATCH(var
));
289 *r
= jsval_number(V_I2(var
));
292 *r
= jsval_number(V_INT(var
));
295 *r
= jsval_number(V_UI4(var
));
302 hres
= IUnknown_QueryInterface(V_UNKNOWN(var
), &IID_IDispatch
, (void**)&disp
);
303 if(SUCCEEDED(hres
)) {
304 *r
= jsval_disp(disp
);
308 *r
= jsval_disp(NULL
);
313 return jsval_variant(r
, var
);
317 HRESULT
jsval_to_variant(jsval_t val
, VARIANT
*retv
)
319 switch(jsval_type(val
)) {
321 V_VT(retv
) = VT_EMPTY
;
324 V_VT(retv
) = VT_NULL
;
327 V_VT(retv
) = VT_DISPATCH
;
329 IDispatch_AddRef(get_object(val
));
330 V_DISPATCH(retv
) = get_object(val
);
333 jsstr_t
*str
= get_string(val
);
335 V_VT(retv
) = VT_BSTR
;
336 if(is_null_bstr(str
)) {
339 V_BSTR(retv
) = SysAllocStringLen(NULL
, jsstr_length(str
));
341 jsstr_flush(str
, V_BSTR(retv
));
343 return E_OUTOFMEMORY
;
348 double n
= get_number(val
);
361 V_VT(retv
) = VT_BOOL
;
362 V_BOOL(retv
) = get_bool(val
) ? VARIANT_TRUE
: VARIANT_FALSE
;
365 V_VT(retv
) = VT_EMPTY
;
366 return VariantCopy(retv
, get_variant(val
));
373 /* ECMA-262 3rd Edition 9.1 */
374 HRESULT
to_primitive(script_ctx_t
*ctx
, jsval_t val
, jsval_t
*ret
, hint_t hint
)
376 if(is_object_instance(val
)) {
382 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
383 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
385 if(!get_object(val
)) {
390 jsdisp
= iface_to_jsdisp((IUnknown
*)get_object(val
));
392 return disp_propget(ctx
, get_object(val
), DISPID_VALUE
, ret
);
395 hint
= is_class(jsdisp
, JSCLASS_DATE
) ? HINT_STRING
: HINT_NUMBER
;
397 /* Native implementation doesn't throw TypeErrors, returns strange values */
399 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? toStringW
: valueOfW
, 0, &id
);
400 if(SUCCEEDED(hres
)) {
401 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, 0, NULL
, &prim
);
403 WARN("call error - forwarding exception\n");
404 jsdisp_release(jsdisp
);
406 }else if(!is_object_instance(prim
)) {
407 jsdisp_release(jsdisp
);
411 IDispatch_Release(get_object(prim
));
415 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? valueOfW
: toStringW
, 0, &id
);
416 if(SUCCEEDED(hres
)) {
417 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, 0, NULL
, &prim
);
419 WARN("call error - forwarding exception\n");
420 jsdisp_release(jsdisp
);
422 }else if(!is_object_instance(prim
)) {
423 jsdisp_release(jsdisp
);
427 IDispatch_Release(get_object(prim
));
431 jsdisp_release(jsdisp
);
434 return throw_type_error(ctx
, JS_E_TO_PRIMITIVE
, NULL
);
437 return jsval_copy(val
, ret
);
441 /* ECMA-262 3rd Edition 9.2 */
442 HRESULT
to_boolean(jsval_t val
, BOOL
*ret
)
444 switch(jsval_type(val
)) {
450 *ret
= get_object(val
) != NULL
;
453 *ret
= jsstr_length(get_string(val
)) != 0;
456 *ret
= !isnan(get_number(val
)) && get_number(val
);
459 *ret
= get_bool(val
);
462 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
470 static int hex_to_int(WCHAR c
)
472 if('0' <= c
&& c
<= '9')
475 if('a' <= c
&& c
<= 'f')
478 if('A' <= c
&& c
<= 'F')
484 /* ECMA-262 3rd Edition 9.3.1 */
485 static HRESULT
str_to_number(jsstr_t
*str
, double *ret
)
491 static const WCHAR infinityW
[] = {'I','n','f','i','n','i','t','y'};
493 ptr
= jsstr_flatten(str
);
495 return E_OUTOFMEMORY
;
497 while(isspaceW(*ptr
))
503 }else if(*ptr
== '+') {
507 if(!strncmpW(ptr
, infinityW
, sizeof(infinityW
)/sizeof(WCHAR
))) {
508 ptr
+= sizeof(infinityW
)/sizeof(WCHAR
);
509 while(*ptr
&& isspaceW(*ptr
))
515 *ret
= neg
? -INFINITY
: INFINITY
;
519 if(*ptr
== '0' && ptr
[1] == 'x') {
523 while((l
= hex_to_int(*ptr
)) != -1) {
532 while(isdigitW(*ptr
))
533 d
= d
*10 + (*ptr
++ - '0');
535 if(*ptr
== 'e' || *ptr
== 'E') {
543 }else if(*ptr
== '+') {
547 while(isdigitW(*ptr
))
548 l
= l
*10 + (*ptr
++ - '0');
553 }else if(*ptr
== '.') {
557 while(isdigitW(*ptr
)) {
558 d
+= dec
* (*ptr
++ - '0');
563 while(isspaceW(*ptr
))
578 /* ECMA-262 3rd Edition 9.3 */
579 HRESULT
to_number(script_ctx_t
*ctx
, jsval_t val
, double *ret
)
581 switch(jsval_type(val
)) {
589 *ret
= get_number(val
);
592 return str_to_number(get_string(val
), ret
);
597 hres
= to_primitive(ctx
, val
, &prim
, HINT_NUMBER
);
601 hres
= to_number(ctx
, prim
, ret
);
606 *ret
= get_bool(val
) ? 1 : 0;
609 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
617 /* ECMA-262 3rd Edition 9.4 */
618 HRESULT
to_integer(script_ctx_t
*ctx
, jsval_t v
, double *ret
)
623 hres
= to_number(ctx
, v
, &n
);
630 *ret
= n
>= 0.0 ? floor(n
) : -floor(-n
);
634 /* ECMA-262 3rd Edition 9.5 */
635 HRESULT
to_int32(script_ctx_t
*ctx
, jsval_t v
, INT
*ret
)
640 hres
= to_number(ctx
, v
, &n
);
644 *ret
= isnan(n
) || isinf(n
) ? 0 : n
;
648 /* ECMA-262 3rd Edition 9.6 */
649 HRESULT
to_uint32(script_ctx_t
*ctx
, jsval_t val
, DWORD
*ret
)
654 hres
= to_int32(ctx
, val
, &n
);
660 static jsstr_t
*int_to_string(int i
)
666 static const WCHAR zeroW
[] = {'0',0};
667 return jsstr_alloc(zeroW
);
675 p
= buf
+ sizeof(buf
)/sizeof(*buf
)-1;
687 return jsstr_alloc(p
);
690 HRESULT
double_to_string(double n
, jsstr_t
**str
)
692 const WCHAR InfinityW
[] = {'-','I','n','f','i','n','i','t','y',0};
697 *str
= jsstr_alloc(n
<0 ? InfinityW
: InfinityW
+1);
698 }else if(is_int32(n
)) {
699 *str
= int_to_string(n
);
704 /* FIXME: Don't use VariantChangeTypeEx */
707 V_VT(&strv
) = VT_EMPTY
;
708 hres
= VariantChangeTypeEx(&strv
, &v
, MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
), 0, VT_BSTR
);
712 *str
= jsstr_alloc(V_BSTR(&strv
));
713 SysFreeString(V_BSTR(&strv
));
716 return *str
? S_OK
: E_OUTOFMEMORY
;
719 /* ECMA-262 3rd Edition 9.8 */
720 HRESULT
to_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
)
722 const WCHAR nullW
[] = {'n','u','l','l',0};
723 const WCHAR trueW
[] = {'t','r','u','e',0};
724 const WCHAR falseW
[] = {'f','a','l','s','e',0};
726 switch(jsval_type(val
)) {
728 *str
= jsstr_undefined();
731 *str
= jsstr_alloc(nullW
);
734 return double_to_string(get_number(val
), str
);
736 *str
= jsstr_addref(get_string(val
));
742 hres
= to_primitive(ctx
, val
, &prim
, HINT_STRING
);
746 hres
= to_string(ctx
, prim
, str
);
751 *str
= jsstr_alloc(get_bool(val
) ? trueW
: falseW
);
754 FIXME("unsupported %s\n", debugstr_jsval(val
));
758 return *str
? S_OK
: E_OUTOFMEMORY
;
761 HRESULT
to_flat_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
, const WCHAR
**ret_str
)
765 hres
= to_string(ctx
, val
, str
);
769 *ret_str
= jsstr_flatten(*str
);
772 return E_OUTOFMEMORY
;
778 /* ECMA-262 3rd Edition 9.9 */
779 HRESULT
to_object(script_ctx_t
*ctx
, jsval_t val
, IDispatch
**disp
)
784 switch(jsval_type(val
)) {
786 hres
= create_string(ctx
, get_string(val
), &dispex
);
790 *disp
= to_disp(dispex
);
793 hres
= create_number(ctx
, get_number(val
), &dispex
);
797 *disp
= to_disp(dispex
);
800 if(get_object(val
)) {
801 *disp
= get_object(val
);
802 IDispatch_AddRef(*disp
);
806 hres
= create_object(ctx
, NULL
, &obj
);
810 *disp
= to_disp(obj
);
814 hres
= create_bool(ctx
, get_bool(val
), &dispex
);
818 *disp
= to_disp(dispex
);
822 WARN("object expected\n");
823 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
825 switch(V_VT(get_variant(val
))) {
826 case VT_ARRAY
|VT_VARIANT
:
827 hres
= create_vbarray(ctx
, V_ARRAY(get_variant(val
)), &dispex
);
831 *disp
= to_disp(dispex
);
835 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val
)));
844 HRESULT
variant_change_type(script_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
)
850 hres
= variant_to_jsval(src
, &val
);
859 hres
= to_int32(ctx
, val
, &i
);
860 if(SUCCEEDED(hres
)) {
870 hres
= to_number(ctx
, val
, &n
);
878 hres
= to_number(ctx
, val
, &n
);
886 hres
= to_boolean(val
, &b
);
888 V_BOOL(dst
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
894 hres
= to_string(ctx
, val
, &str
);
898 if(is_null_bstr(str
)) {
903 V_BSTR(dst
) = SysAllocStringLen(NULL
, jsstr_length(str
));
905 jsstr_flush(str
, V_BSTR(dst
));
907 hres
= E_OUTOFMEMORY
;
911 hres
= V_VT(src
) == VT_EMPTY
? S_OK
: E_NOTIMPL
;
914 hres
= V_VT(src
) == VT_NULL
? S_OK
: E_NOTIMPL
;
917 FIXME("vt %d not implemented\n", vt
);
929 static inline JSCaller
*impl_from_IServiceProvider(IServiceProvider
*iface
)
931 return CONTAINING_RECORD(iface
, JSCaller
, IServiceProvider_iface
);
934 static HRESULT WINAPI
JSCaller_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
936 JSCaller
*This
= impl_from_IServiceProvider(iface
);
938 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
939 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
940 *ppv
= &This
->IServiceProvider_iface
;
941 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
942 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
943 *ppv
= &This
->IServiceProvider_iface
;
945 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
947 return E_NOINTERFACE
;
950 IUnknown_AddRef((IUnknown
*)*ppv
);
954 static ULONG WINAPI
JSCaller_AddRef(IServiceProvider
*iface
)
956 JSCaller
*This
= impl_from_IServiceProvider(iface
);
957 LONG ref
= InterlockedIncrement(&This
->ref
);
959 TRACE("(%p) ref=%d\n", This
, ref
);
964 static ULONG WINAPI
JSCaller_Release(IServiceProvider
*iface
)
966 JSCaller
*This
= impl_from_IServiceProvider(iface
);
967 LONG ref
= InterlockedIncrement(&This
->ref
);
969 TRACE("(%p) ref=%d\n", This
, ref
);
979 static HRESULT WINAPI
JSCaller_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
980 REFIID riid
, void **ppv
)
982 JSCaller
*This
= impl_from_IServiceProvider(iface
);
984 if(IsEqualGUID(guidService
, &SID_VariantConversion
) && This
->ctx
&& This
->ctx
->active_script
) {
985 TRACE("(%p)->(SID_VariantConversion)\n", This
);
986 return IActiveScript_QueryInterface(This
->ctx
->active_script
, riid
, ppv
);
989 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
992 return E_NOINTERFACE
;
995 static const IServiceProviderVtbl ServiceProviderVtbl
= {
996 JSCaller_QueryInterface
,
999 JSCaller_QueryService
1002 HRESULT
create_jscaller(script_ctx_t
*ctx
)
1006 ret
= heap_alloc(sizeof(*ret
));
1008 return E_OUTOFMEMORY
;
1010 ret
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;
1014 ctx
->jscaller
= ret
;