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_INT(var
));
304 *r
= jsval_number(V_UI4(var
));
311 hres
= IUnknown_QueryInterface(V_UNKNOWN(var
), &IID_IDispatch
, (void**)&disp
);
312 if(SUCCEEDED(hres
)) {
313 *r
= jsval_disp(disp
);
317 *r
= jsval_disp(NULL
);
322 return jsval_variant(r
, var
);
326 HRESULT
jsval_to_variant(jsval_t val
, VARIANT
*retv
)
328 switch(jsval_type(val
)) {
330 V_VT(retv
) = VT_EMPTY
;
333 V_VT(retv
) = VT_NULL
;
336 V_VT(retv
) = VT_DISPATCH
;
338 IDispatch_AddRef(get_object(val
));
339 V_DISPATCH(retv
) = get_object(val
);
342 jsstr_t
*str
= get_string(val
);
344 V_VT(retv
) = VT_BSTR
;
345 if(is_null_bstr(str
)) {
348 V_BSTR(retv
) = SysAllocStringLen(NULL
, jsstr_length(str
));
350 jsstr_flush(str
, V_BSTR(retv
));
352 return E_OUTOFMEMORY
;
357 double n
= get_number(val
);
370 V_VT(retv
) = VT_BOOL
;
371 V_BOOL(retv
) = get_bool(val
) ? VARIANT_TRUE
: VARIANT_FALSE
;
374 V_VT(retv
) = VT_EMPTY
;
375 return VariantCopy(retv
, get_variant(val
));
382 /* ECMA-262 3rd Edition 9.1 */
383 HRESULT
to_primitive(script_ctx_t
*ctx
, jsval_t val
, jsval_t
*ret
, hint_t hint
)
385 if(is_object_instance(val
)) {
391 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
392 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
394 if(!get_object(val
)) {
399 jsdisp
= iface_to_jsdisp(get_object(val
));
401 return disp_propget(ctx
, get_object(val
), DISPID_VALUE
, ret
);
404 hint
= is_class(jsdisp
, JSCLASS_DATE
) ? HINT_STRING
: HINT_NUMBER
;
406 /* Native implementation doesn't throw TypeErrors, returns strange values */
408 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? toStringW
: valueOfW
, 0, &id
);
409 if(SUCCEEDED(hres
)) {
410 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, 0, NULL
, &prim
);
412 WARN("call error - forwarding exception\n");
413 jsdisp_release(jsdisp
);
415 }else if(!is_object_instance(prim
)) {
416 jsdisp_release(jsdisp
);
420 IDispatch_Release(get_object(prim
));
424 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? valueOfW
: toStringW
, 0, &id
);
425 if(SUCCEEDED(hres
)) {
426 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, 0, NULL
, &prim
);
428 WARN("call error - forwarding exception\n");
429 jsdisp_release(jsdisp
);
431 }else if(!is_object_instance(prim
)) {
432 jsdisp_release(jsdisp
);
436 IDispatch_Release(get_object(prim
));
440 jsdisp_release(jsdisp
);
443 return throw_type_error(ctx
, JS_E_TO_PRIMITIVE
, NULL
);
446 return jsval_copy(val
, ret
);
450 /* ECMA-262 3rd Edition 9.2 */
451 HRESULT
to_boolean(jsval_t val
, BOOL
*ret
)
453 switch(jsval_type(val
)) {
459 *ret
= get_object(val
) != NULL
;
462 *ret
= jsstr_length(get_string(val
)) != 0;
465 *ret
= !isnan(get_number(val
)) && get_number(val
);
468 *ret
= get_bool(val
);
471 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
479 static int hex_to_int(WCHAR c
)
481 if('0' <= c
&& c
<= '9')
484 if('a' <= c
&& c
<= 'f')
487 if('A' <= c
&& c
<= 'F')
493 /* ECMA-262 3rd Edition 9.3.1 */
494 static HRESULT
str_to_number(jsstr_t
*str
, double *ret
)
500 static const WCHAR infinityW
[] = {'I','n','f','i','n','i','t','y'};
502 ptr
= jsstr_flatten(str
);
504 return E_OUTOFMEMORY
;
506 while(isspaceW(*ptr
))
512 }else if(*ptr
== '+') {
516 if(!strncmpW(ptr
, infinityW
, sizeof(infinityW
)/sizeof(WCHAR
))) {
517 ptr
+= sizeof(infinityW
)/sizeof(WCHAR
);
518 while(*ptr
&& isspaceW(*ptr
))
524 *ret
= neg
? -INFINITY
: INFINITY
;
528 if(*ptr
== '0' && ptr
[1] == 'x') {
532 while((l
= hex_to_int(*ptr
)) != -1) {
541 while(isdigitW(*ptr
))
542 d
= d
*10 + (*ptr
++ - '0');
544 if(*ptr
== 'e' || *ptr
== 'E') {
552 }else if(*ptr
== '+') {
556 while(isdigitW(*ptr
))
557 l
= l
*10 + (*ptr
++ - '0');
562 }else if(*ptr
== '.') {
566 while(isdigitW(*ptr
)) {
567 d
+= dec
* (*ptr
++ - '0');
572 while(isspaceW(*ptr
))
587 /* ECMA-262 3rd Edition 9.3 */
588 HRESULT
to_number(script_ctx_t
*ctx
, jsval_t val
, double *ret
)
590 switch(jsval_type(val
)) {
598 *ret
= get_number(val
);
601 return str_to_number(get_string(val
), ret
);
606 hres
= to_primitive(ctx
, val
, &prim
, HINT_NUMBER
);
610 hres
= to_number(ctx
, prim
, ret
);
615 *ret
= get_bool(val
) ? 1 : 0;
618 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
626 /* ECMA-262 3rd Edition 9.4 */
627 HRESULT
to_integer(script_ctx_t
*ctx
, jsval_t v
, double *ret
)
632 hres
= to_number(ctx
, v
, &n
);
639 *ret
= n
>= 0.0 ? floor(n
) : -floor(-n
);
643 /* ECMA-262 3rd Edition 9.5 */
644 HRESULT
to_int32(script_ctx_t
*ctx
, jsval_t v
, INT
*ret
)
649 hres
= to_number(ctx
, v
, &n
);
653 *ret
= is_finite(n
) ? n
: 0;
657 /* ECMA-262 3rd Edition 9.6 */
658 HRESULT
to_uint32(script_ctx_t
*ctx
, jsval_t val
, DWORD
*ret
)
663 hres
= to_int32(ctx
, val
, &n
);
669 static jsstr_t
*int_to_string(int i
)
675 static const WCHAR zeroW
[] = {'0',0};
676 return jsstr_alloc(zeroW
);
684 p
= buf
+ sizeof(buf
)/sizeof(*buf
)-1;
696 return jsstr_alloc(p
);
699 HRESULT
double_to_string(double n
, jsstr_t
**str
)
701 const WCHAR InfinityW
[] = {'-','I','n','f','i','n','i','t','y',0};
706 *str
= jsstr_alloc(n
<0 ? InfinityW
: InfinityW
+1);
707 }else if(is_int32(n
)) {
708 *str
= int_to_string(n
);
713 /* FIXME: Don't use VariantChangeTypeEx */
716 V_VT(&strv
) = VT_EMPTY
;
717 hres
= VariantChangeTypeEx(&strv
, &v
, MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
), 0, VT_BSTR
);
721 *str
= jsstr_alloc(V_BSTR(&strv
));
722 SysFreeString(V_BSTR(&strv
));
725 return *str
? S_OK
: E_OUTOFMEMORY
;
728 /* ECMA-262 3rd Edition 9.8 */
729 HRESULT
to_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
)
731 const WCHAR nullW
[] = {'n','u','l','l',0};
732 const WCHAR trueW
[] = {'t','r','u','e',0};
733 const WCHAR falseW
[] = {'f','a','l','s','e',0};
735 switch(jsval_type(val
)) {
737 *str
= jsstr_undefined();
740 *str
= jsstr_alloc(nullW
);
743 return double_to_string(get_number(val
), str
);
745 *str
= jsstr_addref(get_string(val
));
751 hres
= to_primitive(ctx
, val
, &prim
, HINT_STRING
);
755 hres
= to_string(ctx
, prim
, str
);
760 *str
= jsstr_alloc(get_bool(val
) ? trueW
: falseW
);
763 FIXME("unsupported %s\n", debugstr_jsval(val
));
767 return *str
? S_OK
: E_OUTOFMEMORY
;
770 HRESULT
to_flat_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
, const WCHAR
**ret_str
)
774 hres
= to_string(ctx
, val
, str
);
778 *ret_str
= jsstr_flatten(*str
);
781 return E_OUTOFMEMORY
;
787 /* ECMA-262 3rd Edition 9.9 */
788 HRESULT
to_object(script_ctx_t
*ctx
, jsval_t val
, IDispatch
**disp
)
793 switch(jsval_type(val
)) {
795 hres
= create_string(ctx
, get_string(val
), &dispex
);
799 *disp
= to_disp(dispex
);
802 hres
= create_number(ctx
, get_number(val
), &dispex
);
806 *disp
= to_disp(dispex
);
809 if(get_object(val
)) {
810 *disp
= get_object(val
);
811 IDispatch_AddRef(*disp
);
815 hres
= create_object(ctx
, NULL
, &obj
);
819 *disp
= to_disp(obj
);
823 hres
= create_bool(ctx
, get_bool(val
), &dispex
);
827 *disp
= to_disp(dispex
);
831 WARN("object expected\n");
832 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
834 switch(V_VT(get_variant(val
))) {
835 case VT_ARRAY
|VT_VARIANT
:
836 hres
= create_vbarray(ctx
, V_ARRAY(get_variant(val
)), &dispex
);
840 *disp
= to_disp(dispex
);
844 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val
)));
853 HRESULT
variant_change_type(script_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
)
859 hres
= variant_to_jsval(src
, &val
);
868 hres
= to_int32(ctx
, val
, &i
);
869 if(SUCCEEDED(hres
)) {
879 hres
= to_number(ctx
, val
, &n
);
887 hres
= to_number(ctx
, val
, &n
);
895 hres
= to_boolean(val
, &b
);
897 V_BOOL(dst
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
903 hres
= to_string(ctx
, val
, &str
);
907 if(is_null_bstr(str
)) {
912 V_BSTR(dst
) = SysAllocStringLen(NULL
, jsstr_length(str
));
914 jsstr_flush(str
, V_BSTR(dst
));
916 hres
= E_OUTOFMEMORY
;
920 hres
= V_VT(src
) == VT_EMPTY
? S_OK
: E_NOTIMPL
;
923 hres
= V_VT(src
) == VT_NULL
? S_OK
: E_NOTIMPL
;
926 FIXME("vt %d not implemented\n", vt
);
938 static inline JSCaller
*impl_from_IServiceProvider(IServiceProvider
*iface
)
940 return CONTAINING_RECORD(iface
, JSCaller
, IServiceProvider_iface
);
943 static HRESULT WINAPI
JSCaller_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
945 JSCaller
*This
= impl_from_IServiceProvider(iface
);
947 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
948 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
949 *ppv
= &This
->IServiceProvider_iface
;
950 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
951 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
952 *ppv
= &This
->IServiceProvider_iface
;
954 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
956 return E_NOINTERFACE
;
959 IUnknown_AddRef((IUnknown
*)*ppv
);
963 static ULONG WINAPI
JSCaller_AddRef(IServiceProvider
*iface
)
965 JSCaller
*This
= impl_from_IServiceProvider(iface
);
966 LONG ref
= InterlockedIncrement(&This
->ref
);
968 TRACE("(%p) ref=%d\n", This
, ref
);
973 static ULONG WINAPI
JSCaller_Release(IServiceProvider
*iface
)
975 JSCaller
*This
= impl_from_IServiceProvider(iface
);
976 LONG ref
= InterlockedIncrement(&This
->ref
);
978 TRACE("(%p) ref=%d\n", This
, ref
);
988 static HRESULT WINAPI
JSCaller_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
989 REFIID riid
, void **ppv
)
991 JSCaller
*This
= impl_from_IServiceProvider(iface
);
993 if(IsEqualGUID(guidService
, &SID_VariantConversion
) && This
->ctx
&& This
->ctx
->active_script
) {
994 TRACE("(%p)->(SID_VariantConversion)\n", This
);
995 return IActiveScript_QueryInterface(This
->ctx
->active_script
, riid
, ppv
);
998 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
1001 return E_NOINTERFACE
;
1004 static const IServiceProviderVtbl ServiceProviderVtbl
= {
1005 JSCaller_QueryInterface
,
1008 JSCaller_QueryService
1011 HRESULT
create_jscaller(script_ctx_t
*ctx
)
1015 ret
= heap_alloc(sizeof(*ret
));
1017 return E_OUTOFMEMORY
;
1019 ret
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;
1023 ctx
->jscaller
= ret
;