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 jsheap_init(jsheap_t
*heap
)
95 memset(heap
, 0, sizeof(*heap
));
96 list_init(&heap
->custom_blocks
);
99 void *jsheap_alloc(jsheap_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 *jsheap_grow(jsheap_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
= jsheap_alloc(heap
, size
+inc
);
163 if(ret
) /* FIXME: avoid copying for custom blocks */
164 memcpy(ret
, mem
, size
);
168 void jsheap_clear(jsheap_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 jsheap_free(jsheap_t
*heap
)
197 for(i
=0; i
< heap
->block_cnt
; i
++)
198 heap_free(heap
->blocks
[i
]);
199 heap_free(heap
->blocks
);
204 jsheap_t
*jsheap_mark(jsheap_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
));
319 return jsval_variant(r
, var
);
323 HRESULT
jsval_to_variant(jsval_t val
, VARIANT
*retv
)
325 switch(jsval_type(val
)) {
327 V_VT(retv
) = VT_EMPTY
;
330 V_VT(retv
) = VT_NULL
;
333 V_VT(retv
) = VT_DISPATCH
;
335 IDispatch_AddRef(get_object(val
));
336 V_DISPATCH(retv
) = get_object(val
);
339 jsstr_t
*str
= get_string(val
);
341 V_VT(retv
) = VT_BSTR
;
342 if(str
->length_flags
& JSSTR_FLAG_NULLBSTR
) {
345 V_BSTR(retv
) = SysAllocStringLen(str
->str
, jsstr_length(str
));
347 return E_OUTOFMEMORY
;
352 double n
= get_number(val
);
365 V_VT(retv
) = VT_BOOL
;
366 V_BOOL(retv
) = get_bool(val
) ? VARIANT_TRUE
: VARIANT_FALSE
;
369 V_VT(retv
) = VT_EMPTY
;
370 return VariantCopy(retv
, get_variant(val
));
377 /* ECMA-262 3rd Edition 9.1 */
378 HRESULT
to_primitive(script_ctx_t
*ctx
, jsval_t val
, jsval_t
*ret
, hint_t hint
)
380 if(is_object_instance(val
)) {
386 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
387 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
389 if(!get_object(val
)) {
394 jsdisp
= iface_to_jsdisp((IUnknown
*)get_object(val
));
396 return disp_propget(ctx
, get_object(val
), DISPID_VALUE
, ret
);
399 hint
= is_class(jsdisp
, JSCLASS_DATE
) ? HINT_STRING
: HINT_NUMBER
;
401 /* Native implementation doesn't throw TypeErrors, returns strange values */
403 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? toStringW
: valueOfW
, 0, &id
);
404 if(SUCCEEDED(hres
)) {
405 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, 0, NULL
, &prim
);
407 WARN("call error - forwarding exception\n");
408 jsdisp_release(jsdisp
);
410 }else if(!is_object_instance(prim
)) {
411 jsdisp_release(jsdisp
);
415 IDispatch_Release(get_object(prim
));
419 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? valueOfW
: toStringW
, 0, &id
);
420 if(SUCCEEDED(hres
)) {
421 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, 0, NULL
, &prim
);
423 WARN("call error - forwarding exception\n");
424 jsdisp_release(jsdisp
);
426 }else if(!is_object_instance(prim
)) {
427 jsdisp_release(jsdisp
);
431 IDispatch_Release(get_object(prim
));
435 jsdisp_release(jsdisp
);
438 return throw_type_error(ctx
, JS_E_TO_PRIMITIVE
, NULL
);
441 return jsval_copy(val
, ret
);
445 /* ECMA-262 3rd Edition 9.2 */
446 HRESULT
to_boolean(jsval_t val
, BOOL
*ret
)
448 switch(jsval_type(val
)) {
454 *ret
= get_object(val
) != NULL
;
457 *ret
= jsstr_length(get_string(val
)) != 0;
460 *ret
= !isnan(get_number(val
)) && get_number(val
);
463 *ret
= get_bool(val
);
466 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val
)));
474 static int hex_to_int(WCHAR c
)
476 if('0' <= c
&& c
<= '9')
479 if('a' <= c
&& c
<= 'f')
482 if('A' <= c
&& c
<= 'F')
488 /* ECMA-262 3rd Edition 9.3.1 */
489 static HRESULT
str_to_number(jsstr_t
*str
, double *ret
)
491 const WCHAR
*ptr
= str
->str
;
495 static const WCHAR infinityW
[] = {'I','n','f','i','n','i','t','y'};
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
= isnan(n
) || isinf(n
) ? 0 : n
;
653 /* ECMA-262 3rd Edition 9.6 */
654 HRESULT
to_uint32(script_ctx_t
*ctx
, jsval_t val
, DWORD
*ret
)
659 hres
= to_number(ctx
, val
, &n
);
663 *ret
= isnan(n
) || isinf(n
) ? 0 : n
;
667 static jsstr_t
*int_to_string(int i
)
673 static const WCHAR zeroW
[] = {'0',0};
674 return jsstr_alloc(zeroW
);
682 p
= buf
+ sizeof(buf
)/sizeof(*buf
)-1;
694 return jsstr_alloc(p
);
697 HRESULT
double_to_string(double n
, jsstr_t
**str
)
699 const WCHAR InfinityW
[] = {'-','I','n','f','i','n','i','t','y',0};
704 *str
= jsstr_alloc(n
<0 ? InfinityW
: InfinityW
+1);
705 }else if(is_int32(n
)) {
706 *str
= int_to_string(n
);
711 /* FIXME: Don't use VariantChangeTypeEx */
714 V_VT(&strv
) = VT_EMPTY
;
715 hres
= VariantChangeTypeEx(&strv
, &v
, MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
), 0, VT_BSTR
);
719 *str
= jsstr_alloc(V_BSTR(&strv
));
720 SysFreeString(V_BSTR(&strv
));
723 return *str
? S_OK
: E_OUTOFMEMORY
;
726 /* ECMA-262 3rd Edition 9.8 */
727 HRESULT
to_string(script_ctx_t
*ctx
, jsval_t val
, jsstr_t
**str
)
729 const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
730 const WCHAR nullW
[] = {'n','u','l','l',0};
731 const WCHAR trueW
[] = {'t','r','u','e',0};
732 const WCHAR falseW
[] = {'f','a','l','s','e',0};
734 switch(jsval_type(val
)) {
736 *str
= jsstr_alloc(undefinedW
);
739 *str
= jsstr_alloc(nullW
);
742 return double_to_string(get_number(val
), str
);
744 *str
= jsstr_addref(get_string(val
));
750 hres
= to_primitive(ctx
, val
, &prim
, HINT_STRING
);
754 hres
= to_string(ctx
, prim
, str
);
759 *str
= jsstr_alloc(get_bool(val
) ? trueW
: falseW
);
762 FIXME("unsupported %s\n", debugstr_jsval(val
));
766 return *str
? S_OK
: E_OUTOFMEMORY
;
769 /* ECMA-262 3rd Edition 9.9 */
770 HRESULT
to_object(script_ctx_t
*ctx
, jsval_t val
, IDispatch
**disp
)
775 switch(jsval_type(val
)) {
777 hres
= create_string(ctx
, get_string(val
), &dispex
);
781 *disp
= to_disp(dispex
);
784 hres
= create_number(ctx
, get_number(val
), &dispex
);
788 *disp
= to_disp(dispex
);
791 if(get_object(val
)) {
792 *disp
= get_object(val
);
793 IDispatch_AddRef(*disp
);
797 hres
= create_object(ctx
, NULL
, &obj
);
801 *disp
= to_disp(obj
);
805 hres
= create_bool(ctx
, get_bool(val
), &dispex
);
809 *disp
= to_disp(dispex
);
812 switch(V_VT(get_variant(val
))) {
813 case VT_ARRAY
|VT_VARIANT
:
814 hres
= create_vbarray(ctx
, V_ARRAY(get_variant(val
)), &dispex
);
818 *disp
= to_disp(dispex
);
822 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val
)));
827 FIXME("unsupported %s\n", debugstr_jsval(val
));
834 HRESULT
variant_change_type(script_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
)
840 hres
= variant_to_jsval(src
, &val
);
849 hres
= to_int32(ctx
, val
, &i
);
850 if(SUCCEEDED(hres
)) {
860 hres
= to_number(ctx
, val
, &n
);
868 hres
= to_number(ctx
, val
, &n
);
876 hres
= to_boolean(val
, &b
);
878 V_BOOL(dst
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
884 hres
= to_string(ctx
, val
, &str
);
888 if(str
->length_flags
& JSSTR_FLAG_NULLBSTR
) {
893 V_BSTR(dst
) = SysAllocStringLen(str
->str
, jsstr_length(str
));
895 hres
= E_OUTOFMEMORY
;
899 hres
= V_VT(src
) == VT_EMPTY
? S_OK
: E_NOTIMPL
;
902 hres
= V_VT(src
) == VT_NULL
? S_OK
: E_NOTIMPL
;
905 FIXME("vt %d not implemented\n", vt
);
917 static inline JSCaller
*impl_from_IServiceProvider(IServiceProvider
*iface
)
919 return CONTAINING_RECORD(iface
, JSCaller
, IServiceProvider_iface
);
922 static HRESULT WINAPI
JSCaller_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
924 JSCaller
*This
= impl_from_IServiceProvider(iface
);
926 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
927 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
928 *ppv
= &This
->IServiceProvider_iface
;
929 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
930 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
931 *ppv
= &This
->IServiceProvider_iface
;
933 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
935 return E_NOINTERFACE
;
938 IUnknown_AddRef((IUnknown
*)*ppv
);
942 static ULONG WINAPI
JSCaller_AddRef(IServiceProvider
*iface
)
944 JSCaller
*This
= impl_from_IServiceProvider(iface
);
945 LONG ref
= InterlockedIncrement(&This
->ref
);
947 TRACE("(%p) ref=%d\n", This
, ref
);
952 static ULONG WINAPI
JSCaller_Release(IServiceProvider
*iface
)
954 JSCaller
*This
= impl_from_IServiceProvider(iface
);
955 LONG ref
= InterlockedIncrement(&This
->ref
);
957 TRACE("(%p) ref=%d\n", This
, ref
);
967 static HRESULT WINAPI
JSCaller_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
968 REFIID riid
, void **ppv
)
970 JSCaller
*This
= impl_from_IServiceProvider(iface
);
972 if(IsEqualGUID(guidService
, &SID_VariantConversion
) && This
->ctx
&& This
->ctx
->active_script
) {
973 TRACE("(%p)->(SID_VariantConversion)\n", This
);
974 return IActiveScript_QueryInterface(This
->ctx
->active_script
, riid
, ppv
);
977 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
980 return E_NOINTERFACE
;
983 static const IServiceProviderVtbl ServiceProviderVtbl
= {
984 JSCaller_QueryInterface
,
987 JSCaller_QueryService
990 HRESULT
create_jscaller(script_ctx_t
*ctx
)
994 ret
= heap_alloc(sizeof(*ret
));
996 return E_OUTOFMEMORY
;
998 ret
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;
1002 ctx
->jscaller
= ret
;