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
)
251 *r
= jsval_undefined();
257 *r
= jsval_bool(V_BOOL(var
));
260 *r
= jsval_number(V_I4(var
));
263 *r
= jsval_number(V_R8(var
));
269 str
= jsstr_alloc_len(V_BSTR(var
), SysStringLen(V_BSTR(var
)));
271 return E_OUTOFMEMORY
;
273 str
= jsstr_null_bstr();
276 *r
= jsval_string(str
);
281 IDispatch_AddRef(V_DISPATCH(var
));
282 *r
= jsval_disp(V_DISPATCH(var
));
286 *r
= jsval_number(V_I2(var
));
289 *r
= jsval_number(V_INT(var
));
296 hres
= IUnknown_QueryInterface(V_UNKNOWN(var
), &IID_IDispatch
, (void**)&disp
);
297 if(SUCCEEDED(hres
)) {
298 *r
= jsval_disp(disp
);
302 *r
= jsval_disp(NULL
);
307 return jsval_variant(r
, var
);
311 HRESULT
jsval_to_variant(jsval_t val
, VARIANT
*retv
)
313 switch(jsval_type(val
)) {
315 V_VT(retv
) = VT_EMPTY
;
318 V_VT(retv
) = VT_NULL
;
321 V_VT(retv
) = VT_DISPATCH
;
323 IDispatch_AddRef(get_object(val
));
324 V_DISPATCH(retv
) = get_object(val
);
327 jsstr_t
*str
= get_string(val
);
329 V_VT(retv
) = VT_BSTR
;
330 if(is_null_bstr(str
)) {
333 V_BSTR(retv
) = SysAllocStringLen(NULL
, jsstr_length(str
));
335 jsstr_flush(str
, V_BSTR(retv
));
337 return E_OUTOFMEMORY
;
342 double n
= get_number(val
);
355 V_VT(retv
) = VT_BOOL
;
356 V_BOOL(retv
) = get_bool(val
) ? VARIANT_TRUE
: VARIANT_FALSE
;
359 V_VT(retv
) = VT_EMPTY
;
360 return VariantCopy(retv
, get_variant(val
));
367 /* ECMA-262 3rd Edition 9.1 */
368 HRESULT
to_primitive(script_ctx_t
*ctx
, jsval_t val
, jsval_t
*ret
, hint_t hint
)
370 if(is_object_instance(val
)) {
376 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
377 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
379 if(!get_object(val
)) {
384 jsdisp
= iface_to_jsdisp((IUnknown
*)get_object(val
));
386 return disp_propget(ctx
, get_object(val
), DISPID_VALUE
, ret
);
389 hint
= is_class(jsdisp
, JSCLASS_DATE
) ? HINT_STRING
: HINT_NUMBER
;
391 /* Native implementation doesn't throw TypeErrors, returns strange values */
393 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? toStringW
: valueOfW
, 0, &id
);
394 if(SUCCEEDED(hres
)) {
395 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, 0, NULL
, &prim
);
397 WARN("call error - forwarding exception\n");
398 jsdisp_release(jsdisp
);
400 }else if(!is_object_instance(prim
)) {
401 jsdisp_release(jsdisp
);
405 IDispatch_Release(get_object(prim
));
409 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? valueOfW
: toStringW
, 0, &id
);
410 if(SUCCEEDED(hres
)) {
411 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, 0, NULL
, &prim
);
413 WARN("call error - forwarding exception\n");
414 jsdisp_release(jsdisp
);
416 }else if(!is_object_instance(prim
)) {
417 jsdisp_release(jsdisp
);
421 IDispatch_Release(get_object(prim
));
425 jsdisp_release(jsdisp
);
428 return throw_type_error(ctx
, JS_E_TO_PRIMITIVE
, NULL
);
431 return jsval_copy(val
, ret
);
435 /* ECMA-262 3rd Edition 9.2 */
436 HRESULT
to_boolean(jsval_t val
, BOOL
*ret
)
438 switch(jsval_type(val
)) {
444 *ret
= get_object(val
) != NULL
;
447 *ret
= jsstr_length(get_string(val
)) != 0;
450 *ret
= !isnan(get_number(val
)) && get_number(val
);
453 *ret
= get_bool(val
);
456 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
464 static int hex_to_int(WCHAR c
)
466 if('0' <= c
&& c
<= '9')
469 if('a' <= c
&& c
<= 'f')
472 if('A' <= c
&& c
<= 'F')
478 /* ECMA-262 3rd Edition 9.3.1 */
479 static HRESULT
str_to_number(jsstr_t
*str
, double *ret
)
485 static const WCHAR infinityW
[] = {'I','n','f','i','n','i','t','y'};
487 ptr
= jsstr_flatten(str
);
489 return E_OUTOFMEMORY
;
491 while(isspaceW(*ptr
))
497 }else if(*ptr
== '+') {
501 if(!strncmpW(ptr
, infinityW
, sizeof(infinityW
)/sizeof(WCHAR
))) {
502 ptr
+= sizeof(infinityW
)/sizeof(WCHAR
);
503 while(*ptr
&& isspaceW(*ptr
))
509 *ret
= neg
? -INFINITY
: INFINITY
;
513 if(*ptr
== '0' && ptr
[1] == 'x') {
517 while((l
= hex_to_int(*ptr
)) != -1) {
526 while(isdigitW(*ptr
))
527 d
= d
*10 + (*ptr
++ - '0');
529 if(*ptr
== 'e' || *ptr
== 'E') {
537 }else if(*ptr
== '+') {
541 while(isdigitW(*ptr
))
542 l
= l
*10 + (*ptr
++ - '0');
547 }else if(*ptr
== '.') {
551 while(isdigitW(*ptr
)) {
552 d
+= dec
* (*ptr
++ - '0');
557 while(isspaceW(*ptr
))
572 /* ECMA-262 3rd Edition 9.3 */
573 HRESULT
to_number(script_ctx_t
*ctx
, jsval_t val
, double *ret
)
575 switch(jsval_type(val
)) {
583 *ret
= get_number(val
);
586 return str_to_number(get_string(val
), ret
);
591 hres
= to_primitive(ctx
, val
, &prim
, HINT_NUMBER
);
595 hres
= to_number(ctx
, prim
, ret
);
600 *ret
= get_bool(val
) ? 1 : 0;
603 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
611 /* ECMA-262 3rd Edition 9.4 */
612 HRESULT
to_integer(script_ctx_t
*ctx
, jsval_t v
, double *ret
)
617 hres
= to_number(ctx
, v
, &n
);
624 *ret
= n
>= 0.0 ? floor(n
) : -floor(-n
);
628 /* ECMA-262 3rd Edition 9.5 */
629 HRESULT
to_int32(script_ctx_t
*ctx
, jsval_t v
, INT
*ret
)
634 hres
= to_number(ctx
, v
, &n
);
638 *ret
= isnan(n
) || isinf(n
) ? 0 : n
;
642 /* ECMA-262 3rd Edition 9.6 */
643 HRESULT
to_uint32(script_ctx_t
*ctx
, jsval_t val
, DWORD
*ret
)
648 hres
= to_int32(ctx
, val
, &n
);
654 static jsstr_t
*int_to_string(int i
)
660 static const WCHAR zeroW
[] = {'0',0};
661 return jsstr_alloc(zeroW
);
669 p
= buf
+ sizeof(buf
)/sizeof(*buf
)-1;
681 return jsstr_alloc(p
);
684 HRESULT
double_to_string(double n
, jsstr_t
**str
)
686 const WCHAR InfinityW
[] = {'-','I','n','f','i','n','i','t','y',0};
691 *str
= jsstr_alloc(n
<0 ? InfinityW
: InfinityW
+1);
692 }else if(is_int32(n
)) {
693 *str
= int_to_string(n
);
698 /* FIXME: Don't use VariantChangeTypeEx */
701 V_VT(&strv
) = VT_EMPTY
;
702 hres
= VariantChangeTypeEx(&strv
, &v
, MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
), 0, VT_BSTR
);
706 *str
= jsstr_alloc(V_BSTR(&strv
));
707 SysFreeString(V_BSTR(&strv
));
710 return *str
? S_OK
: E_OUTOFMEMORY
;
713 /* ECMA-262 3rd Edition 9.8 */
714 HRESULT
to_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
)
716 const WCHAR nullW
[] = {'n','u','l','l',0};
717 const WCHAR trueW
[] = {'t','r','u','e',0};
718 const WCHAR falseW
[] = {'f','a','l','s','e',0};
720 switch(jsval_type(val
)) {
722 *str
= jsstr_undefined();
725 *str
= jsstr_alloc(nullW
);
728 return double_to_string(get_number(val
), str
);
730 *str
= jsstr_addref(get_string(val
));
736 hres
= to_primitive(ctx
, val
, &prim
, HINT_STRING
);
740 hres
= to_string(ctx
, prim
, str
);
745 *str
= jsstr_alloc(get_bool(val
) ? trueW
: falseW
);
748 FIXME("unsupported %s\n", debugstr_jsval(val
));
752 return *str
? S_OK
: E_OUTOFMEMORY
;
755 HRESULT
to_flat_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
, const WCHAR
**ret_str
)
759 hres
= to_string(ctx
, val
, str
);
763 *ret_str
= jsstr_flatten(*str
);
766 return E_OUTOFMEMORY
;
772 /* ECMA-262 3rd Edition 9.9 */
773 HRESULT
to_object(script_ctx_t
*ctx
, jsval_t val
, IDispatch
**disp
)
778 switch(jsval_type(val
)) {
780 hres
= create_string(ctx
, get_string(val
), &dispex
);
784 *disp
= to_disp(dispex
);
787 hres
= create_number(ctx
, get_number(val
), &dispex
);
791 *disp
= to_disp(dispex
);
794 if(get_object(val
)) {
795 *disp
= get_object(val
);
796 IDispatch_AddRef(*disp
);
800 hres
= create_object(ctx
, NULL
, &obj
);
804 *disp
= to_disp(obj
);
808 hres
= create_bool(ctx
, get_bool(val
), &dispex
);
812 *disp
= to_disp(dispex
);
816 WARN("object expected\n");
817 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
819 switch(V_VT(get_variant(val
))) {
820 case VT_ARRAY
|VT_VARIANT
:
821 hres
= create_vbarray(ctx
, V_ARRAY(get_variant(val
)), &dispex
);
825 *disp
= to_disp(dispex
);
829 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val
)));
838 HRESULT
variant_change_type(script_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
)
844 hres
= variant_to_jsval(src
, &val
);
853 hres
= to_int32(ctx
, val
, &i
);
854 if(SUCCEEDED(hres
)) {
864 hres
= to_number(ctx
, val
, &n
);
872 hres
= to_number(ctx
, val
, &n
);
880 hres
= to_boolean(val
, &b
);
882 V_BOOL(dst
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
888 hres
= to_string(ctx
, val
, &str
);
892 if(is_null_bstr(str
)) {
897 V_BSTR(dst
) = SysAllocStringLen(NULL
, jsstr_length(str
));
899 jsstr_flush(str
, V_BSTR(dst
));
901 hres
= E_OUTOFMEMORY
;
905 hres
= V_VT(src
) == VT_EMPTY
? S_OK
: E_NOTIMPL
;
908 hres
= V_VT(src
) == VT_NULL
? S_OK
: E_NOTIMPL
;
911 FIXME("vt %d not implemented\n", vt
);
923 static inline JSCaller
*impl_from_IServiceProvider(IServiceProvider
*iface
)
925 return CONTAINING_RECORD(iface
, JSCaller
, IServiceProvider_iface
);
928 static HRESULT WINAPI
JSCaller_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
930 JSCaller
*This
= impl_from_IServiceProvider(iface
);
932 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
933 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
934 *ppv
= &This
->IServiceProvider_iface
;
935 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
936 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
937 *ppv
= &This
->IServiceProvider_iface
;
939 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
941 return E_NOINTERFACE
;
944 IUnknown_AddRef((IUnknown
*)*ppv
);
948 static ULONG WINAPI
JSCaller_AddRef(IServiceProvider
*iface
)
950 JSCaller
*This
= impl_from_IServiceProvider(iface
);
951 LONG ref
= InterlockedIncrement(&This
->ref
);
953 TRACE("(%p) ref=%d\n", This
, ref
);
958 static ULONG WINAPI
JSCaller_Release(IServiceProvider
*iface
)
960 JSCaller
*This
= impl_from_IServiceProvider(iface
);
961 LONG ref
= InterlockedIncrement(&This
->ref
);
963 TRACE("(%p) ref=%d\n", This
, ref
);
973 static HRESULT WINAPI
JSCaller_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
974 REFIID riid
, void **ppv
)
976 JSCaller
*This
= impl_from_IServiceProvider(iface
);
978 if(IsEqualGUID(guidService
, &SID_VariantConversion
) && This
->ctx
&& This
->ctx
->active_script
) {
979 TRACE("(%p)->(SID_VariantConversion)\n", This
);
980 return IActiveScript_QueryInterface(This
->ctx
->active_script
, riid
, ppv
);
983 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
986 return E_NOINTERFACE
;
989 static const IServiceProviderVtbl ServiceProviderVtbl
= {
990 JSCaller_QueryInterface
,
993 JSCaller_QueryService
996 HRESULT
create_jscaller(script_ctx_t
*ctx
)
1000 ret
= heap_alloc(sizeof(*ret
));
1002 return E_OUTOFMEMORY
;
1004 ret
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;
1008 ctx
->jscaller
= ret
;