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_next(&heap
->custom_blocks
, &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 return E_OUTOFMEMORY
;
219 hres
= VariantCopy(v
, var
);
225 HRESULT
jsval_copy(jsval_t v
, jsval_t
*r
)
227 switch(jsval_type(v
)) {
236 IDispatch_AddRef(get_object(v
));
240 jsstr_addref(get_string(v
));
245 return jsval_variant(r
, get_variant(v
));
252 HRESULT
variant_to_jsval(VARIANT
*var
, jsval_t
*r
)
254 if(V_VT(var
) == (VT_VARIANT
|VT_BYREF
))
255 var
= V_VARIANTREF(var
);
259 *r
= jsval_undefined();
265 *r
= jsval_bool(V_BOOL(var
));
268 *r
= jsval_number(V_I4(var
));
271 *r
= jsval_number(V_R8(var
));
277 str
= jsstr_alloc_len(V_BSTR(var
), SysStringLen(V_BSTR(var
)));
279 return E_OUTOFMEMORY
;
281 str
= jsstr_null_bstr();
284 *r
= jsval_string(str
);
289 IDispatch_AddRef(V_DISPATCH(var
));
290 *r
= jsval_disp(V_DISPATCH(var
));
294 *r
= jsval_number(V_I2(var
));
297 *r
= jsval_number(V_INT(var
));
300 *r
= jsval_number(V_UI4(var
));
307 hres
= IUnknown_QueryInterface(V_UNKNOWN(var
), &IID_IDispatch
, (void**)&disp
);
308 if(SUCCEEDED(hres
)) {
309 *r
= jsval_disp(disp
);
313 *r
= jsval_disp(NULL
);
318 return jsval_variant(r
, var
);
322 HRESULT
jsval_to_variant(jsval_t val
, VARIANT
*retv
)
324 switch(jsval_type(val
)) {
326 V_VT(retv
) = VT_EMPTY
;
329 V_VT(retv
) = VT_NULL
;
332 V_VT(retv
) = VT_DISPATCH
;
334 IDispatch_AddRef(get_object(val
));
335 V_DISPATCH(retv
) = get_object(val
);
338 jsstr_t
*str
= get_string(val
);
340 V_VT(retv
) = VT_BSTR
;
341 if(is_null_bstr(str
)) {
344 V_BSTR(retv
) = SysAllocStringLen(NULL
, jsstr_length(str
));
346 jsstr_flush(str
, V_BSTR(retv
));
348 return E_OUTOFMEMORY
;
353 double n
= get_number(val
);
366 V_VT(retv
) = VT_BOOL
;
367 V_BOOL(retv
) = get_bool(val
) ? VARIANT_TRUE
: VARIANT_FALSE
;
370 V_VT(retv
) = VT_EMPTY
;
371 return VariantCopy(retv
, get_variant(val
));
378 /* ECMA-262 3rd Edition 9.1 */
379 HRESULT
to_primitive(script_ctx_t
*ctx
, jsval_t val
, jsval_t
*ret
, hint_t hint
)
381 if(is_object_instance(val
)) {
387 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
388 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
390 if(!get_object(val
)) {
395 jsdisp
= iface_to_jsdisp((IUnknown
*)get_object(val
));
397 return disp_propget(ctx
, get_object(val
), DISPID_VALUE
, ret
);
400 hint
= is_class(jsdisp
, JSCLASS_DATE
) ? HINT_STRING
: HINT_NUMBER
;
402 /* Native implementation doesn't throw TypeErrors, returns strange values */
404 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? toStringW
: valueOfW
, 0, &id
);
405 if(SUCCEEDED(hres
)) {
406 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, 0, NULL
, &prim
);
408 WARN("call error - forwarding exception\n");
409 jsdisp_release(jsdisp
);
411 }else if(!is_object_instance(prim
)) {
412 jsdisp_release(jsdisp
);
416 IDispatch_Release(get_object(prim
));
420 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? valueOfW
: toStringW
, 0, &id
);
421 if(SUCCEEDED(hres
)) {
422 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, 0, NULL
, &prim
);
424 WARN("call error - forwarding exception\n");
425 jsdisp_release(jsdisp
);
427 }else if(!is_object_instance(prim
)) {
428 jsdisp_release(jsdisp
);
432 IDispatch_Release(get_object(prim
));
436 jsdisp_release(jsdisp
);
439 return throw_type_error(ctx
, JS_E_TO_PRIMITIVE
, NULL
);
442 return jsval_copy(val
, ret
);
446 /* ECMA-262 3rd Edition 9.2 */
447 HRESULT
to_boolean(jsval_t val
, BOOL
*ret
)
449 switch(jsval_type(val
)) {
455 *ret
= get_object(val
) != NULL
;
458 *ret
= jsstr_length(get_string(val
)) != 0;
461 *ret
= !isnan(get_number(val
)) && get_number(val
);
464 *ret
= get_bool(val
);
467 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
475 static int hex_to_int(WCHAR c
)
477 if('0' <= c
&& c
<= '9')
480 if('a' <= c
&& c
<= 'f')
483 if('A' <= c
&& c
<= 'F')
489 /* ECMA-262 3rd Edition 9.3.1 */
490 static HRESULT
str_to_number(jsstr_t
*str
, double *ret
)
496 static const WCHAR infinityW
[] = {'I','n','f','i','n','i','t','y'};
498 ptr
= jsstr_flatten(str
);
500 return E_OUTOFMEMORY
;
502 while(isspaceW(*ptr
))
508 }else if(*ptr
== '+') {
512 if(!strncmpW(ptr
, infinityW
, sizeof(infinityW
)/sizeof(WCHAR
))) {
513 ptr
+= sizeof(infinityW
)/sizeof(WCHAR
);
514 while(*ptr
&& isspaceW(*ptr
))
520 *ret
= neg
? -INFINITY
: INFINITY
;
524 if(*ptr
== '0' && ptr
[1] == 'x') {
528 while((l
= hex_to_int(*ptr
)) != -1) {
537 while(isdigitW(*ptr
))
538 d
= d
*10 + (*ptr
++ - '0');
540 if(*ptr
== 'e' || *ptr
== 'E') {
548 }else if(*ptr
== '+') {
552 while(isdigitW(*ptr
))
553 l
= l
*10 + (*ptr
++ - '0');
558 }else if(*ptr
== '.') {
562 while(isdigitW(*ptr
)) {
563 d
+= dec
* (*ptr
++ - '0');
568 while(isspaceW(*ptr
))
583 /* ECMA-262 3rd Edition 9.3 */
584 HRESULT
to_number(script_ctx_t
*ctx
, jsval_t val
, double *ret
)
586 switch(jsval_type(val
)) {
594 *ret
= get_number(val
);
597 return str_to_number(get_string(val
), ret
);
602 hres
= to_primitive(ctx
, val
, &prim
, HINT_NUMBER
);
606 hres
= to_number(ctx
, prim
, ret
);
611 *ret
= get_bool(val
) ? 1 : 0;
614 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
622 /* ECMA-262 3rd Edition 9.4 */
623 HRESULT
to_integer(script_ctx_t
*ctx
, jsval_t v
, double *ret
)
628 hres
= to_number(ctx
, v
, &n
);
635 *ret
= n
>= 0.0 ? floor(n
) : -floor(-n
);
639 /* ECMA-262 3rd Edition 9.5 */
640 HRESULT
to_int32(script_ctx_t
*ctx
, jsval_t v
, INT
*ret
)
645 hres
= to_number(ctx
, v
, &n
);
649 *ret
= is_finite(n
) ? n
: 0;
653 /* ECMA-262 3rd Edition 9.6 */
654 HRESULT
to_uint32(script_ctx_t
*ctx
, jsval_t val
, DWORD
*ret
)
659 hres
= to_int32(ctx
, val
, &n
);
665 static jsstr_t
*int_to_string(int i
)
671 static const WCHAR zeroW
[] = {'0',0};
672 return jsstr_alloc(zeroW
);
680 p
= buf
+ sizeof(buf
)/sizeof(*buf
)-1;
692 return jsstr_alloc(p
);
695 HRESULT
double_to_string(double n
, jsstr_t
**str
)
697 const WCHAR InfinityW
[] = {'-','I','n','f','i','n','i','t','y',0};
702 *str
= jsstr_alloc(n
<0 ? InfinityW
: InfinityW
+1);
703 }else if(is_int32(n
)) {
704 *str
= int_to_string(n
);
709 /* FIXME: Don't use VariantChangeTypeEx */
712 V_VT(&strv
) = VT_EMPTY
;
713 hres
= VariantChangeTypeEx(&strv
, &v
, MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
), 0, VT_BSTR
);
717 *str
= jsstr_alloc(V_BSTR(&strv
));
718 SysFreeString(V_BSTR(&strv
));
721 return *str
? S_OK
: E_OUTOFMEMORY
;
724 /* ECMA-262 3rd Edition 9.8 */
725 HRESULT
to_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
)
727 const WCHAR nullW
[] = {'n','u','l','l',0};
728 const WCHAR trueW
[] = {'t','r','u','e',0};
729 const WCHAR falseW
[] = {'f','a','l','s','e',0};
731 switch(jsval_type(val
)) {
733 *str
= jsstr_undefined();
736 *str
= jsstr_alloc(nullW
);
739 return double_to_string(get_number(val
), str
);
741 *str
= jsstr_addref(get_string(val
));
747 hres
= to_primitive(ctx
, val
, &prim
, HINT_STRING
);
751 hres
= to_string(ctx
, prim
, str
);
756 *str
= jsstr_alloc(get_bool(val
) ? trueW
: falseW
);
759 FIXME("unsupported %s\n", debugstr_jsval(val
));
763 return *str
? S_OK
: E_OUTOFMEMORY
;
766 HRESULT
to_flat_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
, const WCHAR
**ret_str
)
770 hres
= to_string(ctx
, val
, str
);
774 *ret_str
= jsstr_flatten(*str
);
777 return E_OUTOFMEMORY
;
783 /* ECMA-262 3rd Edition 9.9 */
784 HRESULT
to_object(script_ctx_t
*ctx
, jsval_t val
, IDispatch
**disp
)
789 switch(jsval_type(val
)) {
791 hres
= create_string(ctx
, get_string(val
), &dispex
);
795 *disp
= to_disp(dispex
);
798 hres
= create_number(ctx
, get_number(val
), &dispex
);
802 *disp
= to_disp(dispex
);
805 if(get_object(val
)) {
806 *disp
= get_object(val
);
807 IDispatch_AddRef(*disp
);
811 hres
= create_object(ctx
, NULL
, &obj
);
815 *disp
= to_disp(obj
);
819 hres
= create_bool(ctx
, get_bool(val
), &dispex
);
823 *disp
= to_disp(dispex
);
827 WARN("object expected\n");
828 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
830 switch(V_VT(get_variant(val
))) {
831 case VT_ARRAY
|VT_VARIANT
:
832 hres
= create_vbarray(ctx
, V_ARRAY(get_variant(val
)), &dispex
);
836 *disp
= to_disp(dispex
);
840 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val
)));
849 HRESULT
variant_change_type(script_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
)
855 hres
= variant_to_jsval(src
, &val
);
864 hres
= to_int32(ctx
, val
, &i
);
865 if(SUCCEEDED(hres
)) {
875 hres
= to_number(ctx
, val
, &n
);
883 hres
= to_number(ctx
, val
, &n
);
891 hres
= to_boolean(val
, &b
);
893 V_BOOL(dst
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
899 hres
= to_string(ctx
, val
, &str
);
903 if(is_null_bstr(str
)) {
908 V_BSTR(dst
) = SysAllocStringLen(NULL
, jsstr_length(str
));
910 jsstr_flush(str
, V_BSTR(dst
));
912 hres
= E_OUTOFMEMORY
;
916 hres
= V_VT(src
) == VT_EMPTY
? S_OK
: E_NOTIMPL
;
919 hres
= V_VT(src
) == VT_NULL
? S_OK
: E_NOTIMPL
;
922 FIXME("vt %d not implemented\n", vt
);
934 static inline JSCaller
*impl_from_IServiceProvider(IServiceProvider
*iface
)
936 return CONTAINING_RECORD(iface
, JSCaller
, IServiceProvider_iface
);
939 static HRESULT WINAPI
JSCaller_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
941 JSCaller
*This
= impl_from_IServiceProvider(iface
);
943 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
944 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
945 *ppv
= &This
->IServiceProvider_iface
;
946 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
947 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
948 *ppv
= &This
->IServiceProvider_iface
;
950 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
952 return E_NOINTERFACE
;
955 IUnknown_AddRef((IUnknown
*)*ppv
);
959 static ULONG WINAPI
JSCaller_AddRef(IServiceProvider
*iface
)
961 JSCaller
*This
= impl_from_IServiceProvider(iface
);
962 LONG ref
= InterlockedIncrement(&This
->ref
);
964 TRACE("(%p) ref=%d\n", This
, ref
);
969 static ULONG WINAPI
JSCaller_Release(IServiceProvider
*iface
)
971 JSCaller
*This
= impl_from_IServiceProvider(iface
);
972 LONG ref
= InterlockedIncrement(&This
->ref
);
974 TRACE("(%p) ref=%d\n", This
, ref
);
984 static HRESULT WINAPI
JSCaller_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
985 REFIID riid
, void **ppv
)
987 JSCaller
*This
= impl_from_IServiceProvider(iface
);
989 if(IsEqualGUID(guidService
, &SID_VariantConversion
) && This
->ctx
&& This
->ctx
->active_script
) {
990 TRACE("(%p)->(SID_VariantConversion)\n", This
);
991 return IActiveScript_QueryInterface(This
->ctx
->active_script
, riid
, ppv
);
994 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
997 return E_NOINTERFACE
;
1000 static const IServiceProviderVtbl ServiceProviderVtbl
= {
1001 JSCaller_QueryInterface
,
1004 JSCaller_QueryService
1007 HRESULT
create_jscaller(script_ctx_t
*ctx
)
1011 ret
= heap_alloc(sizeof(*ret
));
1013 return E_OUTOFMEMORY
;
1015 ret
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;
1019 ctx
->jscaller
= ret
;