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_variant(const VARIANT
*v
)
44 return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v
));
46 return wine_dbg_sprintf("{VT_UI4: %u}", V_UI4(v
));
48 return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v
));
50 return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v
)));
52 return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v
));
54 return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v
));
55 case VT_ARRAY
|VT_VARIANT
:
56 return "{VT_ARRAY|VT_VARIANT: ...}";
58 return wine_dbg_sprintf("{vt %d}", V_VT(v
));
62 const char *debugstr_jsval(const jsval_t v
)
64 switch(jsval_type(v
)) {
70 return wine_dbg_sprintf("obj(%p)", get_object(v
));
72 return wine_dbg_sprintf("str(%s)", debugstr_jsstr(get_string(v
)));
74 return wine_dbg_sprintf("%lf", get_number(v
));
76 return get_bool(v
) ? "true" : "false";
78 return debugstr_variant(get_variant(v
));
85 #define MIN_BLOCK_SIZE 128
86 #define ARENA_FREE_FILLER 0xaa
88 static inline DWORD
block_size(DWORD block
)
90 return MIN_BLOCK_SIZE
<< block
;
93 void heap_pool_init(heap_pool_t
*heap
)
95 memset(heap
, 0, sizeof(*heap
));
96 list_init(&heap
->custom_blocks
);
99 void *heap_pool_alloc(heap_pool_t
*heap
, DWORD size
)
104 if(!heap
->block_cnt
) {
106 heap
->blocks
= heap_alloc(sizeof(void*));
111 tmp
= heap_alloc(block_size(0));
115 heap
->blocks
[0] = tmp
;
119 if(heap
->offset
+ size
<= block_size(heap
->last_block
)) {
120 tmp
= ((BYTE
*)heap
->blocks
[heap
->last_block
])+heap
->offset
;
121 heap
->offset
+= size
;
125 if(size
<= block_size(heap
->last_block
+1)) {
126 if(heap
->last_block
+1 == heap
->block_cnt
) {
127 tmp
= heap_realloc(heap
->blocks
, (heap
->block_cnt
+1)*sizeof(void*));
132 heap
->blocks
[heap
->block_cnt
] = heap_alloc(block_size(heap
->block_cnt
));
133 if(!heap
->blocks
[heap
->block_cnt
])
141 return heap
->blocks
[heap
->last_block
];
144 list
= heap_alloc(size
+ sizeof(struct list
));
148 list_add_head(&heap
->custom_blocks
, list
);
152 void *heap_pool_grow(heap_pool_t
*heap
, void *mem
, DWORD size
, DWORD inc
)
156 if(mem
== (BYTE
*)heap
->blocks
[heap
->last_block
] + heap
->offset
-size
157 && heap
->offset
+inc
< block_size(heap
->last_block
)) {
162 ret
= heap_pool_alloc(heap
, size
+inc
);
163 if(ret
) /* FIXME: avoid copying for custom blocks */
164 memcpy(ret
, mem
, size
);
168 void heap_pool_clear(heap_pool_t
*heap
)
175 while((tmp
= list_next(&heap
->custom_blocks
, &heap
->custom_blocks
))) {
183 for(i
=0; i
< heap
->block_cnt
; i
++)
184 memset(heap
->blocks
[i
], ARENA_FREE_FILLER
, block_size(i
));
187 heap
->last_block
= heap
->offset
= 0;
191 void heap_pool_free(heap_pool_t
*heap
)
195 heap_pool_clear(heap
);
197 for(i
=0; i
< heap
->block_cnt
; i
++)
198 heap_free(heap
->blocks
[i
]);
199 heap_free(heap
->blocks
);
201 heap_pool_init(heap
);
204 heap_pool_t
*heap_pool_mark(heap_pool_t
*heap
)
213 void jsval_release(jsval_t val
)
215 switch(jsval_type(val
)) {
218 IDispatch_Release(get_object(val
));
221 jsstr_release(get_string(val
));
224 VariantClear(get_variant(val
));
225 heap_free(get_variant(val
));
232 static HRESULT
jsval_variant(jsval_t
*val
, VARIANT
*var
)
237 __JSVAL_TYPE(*val
) = JSV_VARIANT
;
238 __JSVAL_VAR(*val
) = v
= heap_alloc(sizeof(VARIANT
));
240 return E_OUTOFMEMORY
;
243 hres
= VariantCopy(v
, var
);
249 HRESULT
jsval_copy(jsval_t v
, jsval_t
*r
)
251 switch(jsval_type(v
)) {
260 IDispatch_AddRef(get_object(v
));
264 jsstr_addref(get_string(v
));
269 return jsval_variant(r
, get_variant(v
));
276 HRESULT
variant_to_jsval(VARIANT
*var
, jsval_t
*r
)
280 *r
= jsval_undefined();
286 *r
= jsval_bool(V_BOOL(var
));
289 *r
= jsval_number(V_I4(var
));
292 *r
= jsval_number(V_R8(var
));
297 str
= jsstr_alloc_len(V_BSTR(var
), SysStringLen(V_BSTR(var
)));
299 return E_OUTOFMEMORY
;
301 str
->length_flags
|= JSSTR_FLAG_NULLBSTR
;
303 *r
= jsval_string(str
);
308 IDispatch_AddRef(V_DISPATCH(var
));
309 *r
= jsval_disp(V_DISPATCH(var
));
313 *r
= jsval_number(V_I2(var
));
316 *r
= jsval_number(V_INT(var
));
323 hres
= IUnknown_QueryInterface(V_UNKNOWN(var
), &IID_IDispatch
, (void**)&disp
);
324 if(SUCCEEDED(hres
)) {
325 *r
= jsval_disp(disp
);
331 return jsval_variant(r
, var
);
335 HRESULT
jsval_to_variant(jsval_t val
, VARIANT
*retv
)
337 switch(jsval_type(val
)) {
339 V_VT(retv
) = VT_EMPTY
;
342 V_VT(retv
) = VT_NULL
;
345 V_VT(retv
) = VT_DISPATCH
;
347 IDispatch_AddRef(get_object(val
));
348 V_DISPATCH(retv
) = get_object(val
);
351 jsstr_t
*str
= get_string(val
);
353 V_VT(retv
) = VT_BSTR
;
354 if(str
->length_flags
& JSSTR_FLAG_NULLBSTR
) {
357 V_BSTR(retv
) = SysAllocStringLen(NULL
, jsstr_length(str
));
359 jsstr_flush(str
, V_BSTR(retv
));
361 return E_OUTOFMEMORY
;
366 double n
= get_number(val
);
379 V_VT(retv
) = VT_BOOL
;
380 V_BOOL(retv
) = get_bool(val
) ? VARIANT_TRUE
: VARIANT_FALSE
;
383 V_VT(retv
) = VT_EMPTY
;
384 return VariantCopy(retv
, get_variant(val
));
391 /* ECMA-262 3rd Edition 9.1 */
392 HRESULT
to_primitive(script_ctx_t
*ctx
, jsval_t val
, jsval_t
*ret
, hint_t hint
)
394 if(is_object_instance(val
)) {
400 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
401 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
403 if(!get_object(val
)) {
408 jsdisp
= iface_to_jsdisp((IUnknown
*)get_object(val
));
410 return disp_propget(ctx
, get_object(val
), DISPID_VALUE
, ret
);
413 hint
= is_class(jsdisp
, JSCLASS_DATE
) ? HINT_STRING
: HINT_NUMBER
;
415 /* Native implementation doesn't throw TypeErrors, returns strange values */
417 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? toStringW
: valueOfW
, 0, &id
);
418 if(SUCCEEDED(hres
)) {
419 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, 0, NULL
, &prim
);
421 WARN("call error - forwarding exception\n");
422 jsdisp_release(jsdisp
);
424 }else if(!is_object_instance(prim
)) {
425 jsdisp_release(jsdisp
);
429 IDispatch_Release(get_object(prim
));
433 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? valueOfW
: toStringW
, 0, &id
);
434 if(SUCCEEDED(hres
)) {
435 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, 0, NULL
, &prim
);
437 WARN("call error - forwarding exception\n");
438 jsdisp_release(jsdisp
);
440 }else if(!is_object_instance(prim
)) {
441 jsdisp_release(jsdisp
);
445 IDispatch_Release(get_object(prim
));
449 jsdisp_release(jsdisp
);
452 return throw_type_error(ctx
, JS_E_TO_PRIMITIVE
, NULL
);
455 return jsval_copy(val
, ret
);
459 /* ECMA-262 3rd Edition 9.2 */
460 HRESULT
to_boolean(jsval_t val
, BOOL
*ret
)
462 switch(jsval_type(val
)) {
468 *ret
= get_object(val
) != NULL
;
471 *ret
= jsstr_length(get_string(val
)) != 0;
474 *ret
= !isnan(get_number(val
)) && get_number(val
);
477 *ret
= get_bool(val
);
480 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
488 static int hex_to_int(WCHAR c
)
490 if('0' <= c
&& c
<= '9')
493 if('a' <= c
&& c
<= 'f')
496 if('A' <= c
&& c
<= 'F')
502 /* ECMA-262 3rd Edition 9.3.1 */
503 static HRESULT
str_to_number(jsstr_t
*str
, double *ret
)
509 static const WCHAR infinityW
[] = {'I','n','f','i','n','i','t','y'};
511 ptr
= jsstr_flatten(str
);
513 return E_OUTOFMEMORY
;
515 while(isspaceW(*ptr
))
521 }else if(*ptr
== '+') {
525 if(!strncmpW(ptr
, infinityW
, sizeof(infinityW
)/sizeof(WCHAR
))) {
526 ptr
+= sizeof(infinityW
)/sizeof(WCHAR
);
527 while(*ptr
&& isspaceW(*ptr
))
533 *ret
= neg
? -INFINITY
: INFINITY
;
537 if(*ptr
== '0' && ptr
[1] == 'x') {
541 while((l
= hex_to_int(*ptr
)) != -1) {
550 while(isdigitW(*ptr
))
551 d
= d
*10 + (*ptr
++ - '0');
553 if(*ptr
== 'e' || *ptr
== 'E') {
561 }else if(*ptr
== '+') {
565 while(isdigitW(*ptr
))
566 l
= l
*10 + (*ptr
++ - '0');
571 }else if(*ptr
== '.') {
575 while(isdigitW(*ptr
)) {
576 d
+= dec
* (*ptr
++ - '0');
581 while(isspaceW(*ptr
))
596 /* ECMA-262 3rd Edition 9.3 */
597 HRESULT
to_number(script_ctx_t
*ctx
, jsval_t val
, double *ret
)
599 switch(jsval_type(val
)) {
607 *ret
= get_number(val
);
610 return str_to_number(get_string(val
), ret
);
615 hres
= to_primitive(ctx
, val
, &prim
, HINT_NUMBER
);
619 hres
= to_number(ctx
, prim
, ret
);
624 *ret
= get_bool(val
) ? 1 : 0;
627 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
635 /* ECMA-262 3rd Edition 9.4 */
636 HRESULT
to_integer(script_ctx_t
*ctx
, jsval_t v
, double *ret
)
641 hres
= to_number(ctx
, v
, &n
);
648 *ret
= n
>= 0.0 ? floor(n
) : -floor(-n
);
652 /* ECMA-262 3rd Edition 9.5 */
653 HRESULT
to_int32(script_ctx_t
*ctx
, jsval_t v
, INT
*ret
)
658 hres
= to_number(ctx
, v
, &n
);
662 *ret
= isnan(n
) || isinf(n
) ? 0 : n
;
666 /* ECMA-262 3rd Edition 9.6 */
667 HRESULT
to_uint32(script_ctx_t
*ctx
, jsval_t val
, DWORD
*ret
)
672 hres
= to_int32(ctx
, val
, &n
);
678 static jsstr_t
*int_to_string(int i
)
684 static const WCHAR zeroW
[] = {'0',0};
685 return jsstr_alloc(zeroW
);
693 p
= buf
+ sizeof(buf
)/sizeof(*buf
)-1;
705 return jsstr_alloc(p
);
708 HRESULT
double_to_string(double n
, jsstr_t
**str
)
710 const WCHAR InfinityW
[] = {'-','I','n','f','i','n','i','t','y',0};
715 *str
= jsstr_alloc(n
<0 ? InfinityW
: InfinityW
+1);
716 }else if(is_int32(n
)) {
717 *str
= int_to_string(n
);
722 /* FIXME: Don't use VariantChangeTypeEx */
725 V_VT(&strv
) = VT_EMPTY
;
726 hres
= VariantChangeTypeEx(&strv
, &v
, MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
), 0, VT_BSTR
);
730 *str
= jsstr_alloc(V_BSTR(&strv
));
731 SysFreeString(V_BSTR(&strv
));
734 return *str
? S_OK
: E_OUTOFMEMORY
;
737 /* ECMA-262 3rd Edition 9.8 */
738 HRESULT
to_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
)
740 const WCHAR nullW
[] = {'n','u','l','l',0};
741 const WCHAR trueW
[] = {'t','r','u','e',0};
742 const WCHAR falseW
[] = {'f','a','l','s','e',0};
744 switch(jsval_type(val
)) {
746 *str
= jsstr_undefined();
749 *str
= jsstr_alloc(nullW
);
752 return double_to_string(get_number(val
), str
);
754 *str
= jsstr_addref(get_string(val
));
760 hres
= to_primitive(ctx
, val
, &prim
, HINT_STRING
);
764 hres
= to_string(ctx
, prim
, str
);
769 *str
= jsstr_alloc(get_bool(val
) ? trueW
: falseW
);
772 FIXME("unsupported %s\n", debugstr_jsval(val
));
776 return *str
? S_OK
: E_OUTOFMEMORY
;
779 HRESULT
to_flat_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
, const WCHAR
**ret_str
)
783 hres
= to_string(ctx
, val
, str
);
787 *ret_str
= jsstr_flatten(*str
);
790 return E_OUTOFMEMORY
;
796 /* ECMA-262 3rd Edition 9.9 */
797 HRESULT
to_object(script_ctx_t
*ctx
, jsval_t val
, IDispatch
**disp
)
802 switch(jsval_type(val
)) {
804 hres
= create_string(ctx
, get_string(val
), &dispex
);
808 *disp
= to_disp(dispex
);
811 hres
= create_number(ctx
, get_number(val
), &dispex
);
815 *disp
= to_disp(dispex
);
818 if(get_object(val
)) {
819 *disp
= get_object(val
);
820 IDispatch_AddRef(*disp
);
824 hres
= create_object(ctx
, NULL
, &obj
);
828 *disp
= to_disp(obj
);
832 hres
= create_bool(ctx
, get_bool(val
), &dispex
);
836 *disp
= to_disp(dispex
);
840 WARN("object expected\n");
841 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
843 switch(V_VT(get_variant(val
))) {
844 case VT_ARRAY
|VT_VARIANT
:
845 hres
= create_vbarray(ctx
, V_ARRAY(get_variant(val
)), &dispex
);
849 *disp
= to_disp(dispex
);
853 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val
)));
862 HRESULT
variant_change_type(script_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
)
868 hres
= variant_to_jsval(src
, &val
);
877 hres
= to_int32(ctx
, val
, &i
);
878 if(SUCCEEDED(hres
)) {
888 hres
= to_number(ctx
, val
, &n
);
896 hres
= to_number(ctx
, val
, &n
);
904 hres
= to_boolean(val
, &b
);
906 V_BOOL(dst
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
912 hres
= to_string(ctx
, val
, &str
);
916 if(str
->length_flags
& JSSTR_FLAG_NULLBSTR
) {
921 V_BSTR(dst
) = SysAllocStringLen(NULL
, jsstr_length(str
));
923 jsstr_flush(str
, V_BSTR(dst
));
925 hres
= E_OUTOFMEMORY
;
929 hres
= V_VT(src
) == VT_EMPTY
? S_OK
: E_NOTIMPL
;
932 hres
= V_VT(src
) == VT_NULL
? S_OK
: E_NOTIMPL
;
935 FIXME("vt %d not implemented\n", vt
);
947 static inline JSCaller
*impl_from_IServiceProvider(IServiceProvider
*iface
)
949 return CONTAINING_RECORD(iface
, JSCaller
, IServiceProvider_iface
);
952 static HRESULT WINAPI
JSCaller_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
954 JSCaller
*This
= impl_from_IServiceProvider(iface
);
956 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
957 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
958 *ppv
= &This
->IServiceProvider_iface
;
959 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
960 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
961 *ppv
= &This
->IServiceProvider_iface
;
963 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
965 return E_NOINTERFACE
;
968 IUnknown_AddRef((IUnknown
*)*ppv
);
972 static ULONG WINAPI
JSCaller_AddRef(IServiceProvider
*iface
)
974 JSCaller
*This
= impl_from_IServiceProvider(iface
);
975 LONG ref
= InterlockedIncrement(&This
->ref
);
977 TRACE("(%p) ref=%d\n", This
, ref
);
982 static ULONG WINAPI
JSCaller_Release(IServiceProvider
*iface
)
984 JSCaller
*This
= impl_from_IServiceProvider(iface
);
985 LONG ref
= InterlockedIncrement(&This
->ref
);
987 TRACE("(%p) ref=%d\n", This
, ref
);
997 static HRESULT WINAPI
JSCaller_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
998 REFIID riid
, void **ppv
)
1000 JSCaller
*This
= impl_from_IServiceProvider(iface
);
1002 if(IsEqualGUID(guidService
, &SID_VariantConversion
) && This
->ctx
&& This
->ctx
->active_script
) {
1003 TRACE("(%p)->(SID_VariantConversion)\n", This
);
1004 return IActiveScript_QueryInterface(This
->ctx
->active_script
, riid
, ppv
);
1007 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
1010 return E_NOINTERFACE
;
1013 static const IServiceProviderVtbl ServiceProviderVtbl
= {
1014 JSCaller_QueryInterface
,
1017 JSCaller_QueryService
1020 HRESULT
create_jscaller(script_ctx_t
*ctx
)
1024 ret
= heap_alloc(sizeof(*ret
));
1026 return E_OUTOFMEMORY
;
1028 ret
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;
1032 ctx
->jscaller
= ret
;