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
));
56 return wine_dbg_sprintf("{vt %d}", V_VT(v
));
60 #define MIN_BLOCK_SIZE 128
61 #define ARENA_FREE_FILLER 0xaa
63 static inline DWORD
block_size(DWORD block
)
65 return MIN_BLOCK_SIZE
<< block
;
68 void jsheap_init(jsheap_t
*heap
)
70 memset(heap
, 0, sizeof(*heap
));
71 list_init(&heap
->custom_blocks
);
74 void *jsheap_alloc(jsheap_t
*heap
, DWORD size
)
79 if(!heap
->block_cnt
) {
81 heap
->blocks
= heap_alloc(sizeof(void*));
86 tmp
= heap_alloc(block_size(0));
90 heap
->blocks
[0] = tmp
;
94 if(heap
->offset
+ size
<= block_size(heap
->last_block
)) {
95 tmp
= ((BYTE
*)heap
->blocks
[heap
->last_block
])+heap
->offset
;
100 if(size
<= block_size(heap
->last_block
+1)) {
101 if(heap
->last_block
+1 == heap
->block_cnt
) {
102 tmp
= heap_realloc(heap
->blocks
, (heap
->block_cnt
+1)*sizeof(void*));
107 heap
->blocks
[heap
->block_cnt
] = heap_alloc(block_size(heap
->block_cnt
));
108 if(!heap
->blocks
[heap
->block_cnt
])
116 return heap
->blocks
[heap
->last_block
];
119 list
= heap_alloc(size
+ sizeof(struct list
));
123 list_add_head(&heap
->custom_blocks
, list
);
127 void *jsheap_grow(jsheap_t
*heap
, void *mem
, DWORD size
, DWORD inc
)
129 if(mem
== (BYTE
*)heap
->blocks
[heap
->last_block
] + heap
->offset
-size
130 && heap
->offset
+inc
< block_size(heap
->last_block
)) {
135 return jsheap_alloc(heap
, size
+inc
);
138 void jsheap_clear(jsheap_t
*heap
)
145 while((tmp
= list_next(&heap
->custom_blocks
, &heap
->custom_blocks
))) {
153 for(i
=0; i
< heap
->block_cnt
; i
++)
154 memset(heap
->blocks
[i
], ARENA_FREE_FILLER
, block_size(i
));
157 heap
->last_block
= heap
->offset
= 0;
161 void jsheap_free(jsheap_t
*heap
)
167 for(i
=0; i
< heap
->block_cnt
; i
++)
168 heap_free(heap
->blocks
[i
]);
169 heap_free(heap
->blocks
);
174 jsheap_t
*jsheap_mark(jsheap_t
*heap
)
183 /* ECMA-262 3rd Edition 9.1 */
184 HRESULT
to_primitive(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, VARIANT
*ret
, hint_t hint
)
196 V_BSTR(ret
) = SysAllocString(V_BSTR(v
));
201 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
204 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
205 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
212 jsdisp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(v
));
214 V_VT(ret
) = VT_EMPTY
;
215 return disp_propget(ctx
, V_DISPATCH(v
), DISPID_VALUE
, ret
, ei
);
219 hint
= is_class(jsdisp
, JSCLASS_DATE
) ? HINT_STRING
: HINT_NUMBER
;
221 /* Native implementation doesn't throw TypeErrors, returns strange values */
223 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? toStringW
: valueOfW
, 0, &id
);
224 if(SUCCEEDED(hres
)) {
225 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, &dp
, ret
, ei
);
227 WARN("call error - forwarding exception\n");
228 jsdisp_release(jsdisp
);
231 else if(V_VT(ret
) != VT_DISPATCH
) {
232 jsdisp_release(jsdisp
);
236 IDispatch_Release(V_DISPATCH(ret
));
239 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? valueOfW
: toStringW
, 0, &id
);
240 if(SUCCEEDED(hres
)) {
241 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, &dp
, ret
, ei
);
243 WARN("call error - forwarding exception\n");
244 jsdisp_release(jsdisp
);
247 else if(V_VT(ret
) != VT_DISPATCH
) {
248 jsdisp_release(jsdisp
);
252 IDispatch_Release(V_DISPATCH(ret
));
255 jsdisp_release(jsdisp
);
258 return throw_type_error(ctx
, ei
, JS_E_TO_PRIMITIVE
, NULL
);
261 FIXME("Unimplemented for vt %d\n", V_VT(v
));
268 /* ECMA-262 3rd Edition 9.2 */
269 HRESULT
to_boolean(VARIANT
*v
, VARIANT_BOOL
*b
)
277 *b
= V_I4(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
280 if(isnan(V_R8(v
))) *b
= VARIANT_FALSE
;
281 else *b
= V_R8(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
284 *b
= V_BSTR(v
) && *V_BSTR(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
287 *b
= V_DISPATCH(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
293 FIXME("unimplemented for vt %d\n", V_VT(v
));
300 static int hex_to_int(WCHAR c
)
302 if('0' <= c
&& c
<= '9')
305 if('a' <= c
&& c
<= 'f')
308 if('A' <= c
&& c
<= 'F')
314 /* ECMA-262 3rd Edition 9.3.1 */
315 static HRESULT
str_to_number(BSTR str
, VARIANT
*ret
)
317 const WCHAR
*ptr
= str
;
321 static const WCHAR infinityW
[] = {'I','n','f','i','n','i','t','y'};
323 while(isspaceW(*ptr
))
329 }else if(*ptr
== '+') {
333 if(!strncmpW(ptr
, infinityW
, sizeof(infinityW
)/sizeof(WCHAR
))) {
334 ptr
+= sizeof(infinityW
)/sizeof(WCHAR
);
335 while(*ptr
&& isspaceW(*ptr
))
341 num_set_inf(ret
, !neg
);
345 if(*ptr
== '0' && ptr
[1] == 'x') {
349 while((l
= hex_to_int(*ptr
)) != -1) {
358 while(isdigitW(*ptr
))
359 d
= d
*10 + (*ptr
++ - '0');
361 if(*ptr
== 'e' || *ptr
== 'E') {
369 }else if(*ptr
== '+') {
373 while(isdigitW(*ptr
))
374 l
= l
*10 + (*ptr
++ - '0');
379 }else if(*ptr
== '.') {
383 while(isdigitW(*ptr
)) {
384 d
+= dec
* (*ptr
++ - '0');
389 while(isspaceW(*ptr
))
404 /* ECMA-262 3rd Edition 9.3 */
405 HRESULT
to_number(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, VARIANT
*ret
)
420 return str_to_number(V_BSTR(v
), ret
);
425 hres
= to_primitive(ctx
, v
, ei
, &prim
, HINT_NUMBER
);
429 hres
= to_number(ctx
, &prim
, ei
, ret
);
435 V_I4(ret
) = V_BOOL(v
) ? 1 : 0;
438 FIXME("unimplemented for vt %d\n", V_VT(v
));
445 /* ECMA-262 3rd Edition 9.4 */
446 HRESULT
to_integer(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, VARIANT
*ret
)
451 hres
= to_number(ctx
, v
, ei
, &num
);
455 if(V_VT(&num
) == VT_I4
) {
457 }else if(isnan(V_R8(&num
))) {
461 num_set_val(ret
, V_R8(&num
) >= 0.0 ? floor(V_R8(&num
)) : -floor(-V_R8(&num
)));
467 /* ECMA-262 3rd Edition 9.5 */
468 HRESULT
to_int32(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, INT
*ret
)
473 hres
= to_number(ctx
, v
, ei
, &num
);
477 if(V_VT(&num
) == VT_I4
)
480 *ret
= isnan(V_R8(&num
)) || isinf(V_R8(&num
)) ? 0 : (INT
)V_R8(&num
);
484 /* ECMA-262 3rd Edition 9.6 */
485 HRESULT
to_uint32(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, DWORD
*ret
)
490 hres
= to_number(ctx
, v
, ei
, &num
);
494 if(V_VT(&num
) == VT_I4
)
497 *ret
= isnan(V_R8(&num
)) || isinf(V_R8(&num
)) ? 0 : (DWORD
)V_R8(&num
);
501 BSTR
int_to_bstr(int i
)
507 static const WCHAR zeroW
[] = {'0',0};
508 return SysAllocString(zeroW
);
516 p
= buf
+ sizeof(buf
)/sizeof(*buf
)-1;
528 return SysAllocString(p
);
531 HRESULT
double_to_bstr(double n
, BSTR
*str
)
533 const WCHAR NaNW
[] = {'N','a','N',0};
534 const WCHAR InfinityW
[] = {'-','I','n','f','i','n','i','t','y',0};
537 *str
= SysAllocString(NaNW
);
539 *str
= SysAllocString(n
<0 ? InfinityW
: InfinityW
+1);
546 V_VT(&strv
) = VT_EMPTY
;
547 hres
= VariantChangeTypeEx(&strv
, &v
, MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
), 0, VT_BSTR
);
551 *str
= V_BSTR(&strv
);
554 return *str
? S_OK
: E_OUTOFMEMORY
;
557 /* ECMA-262 3rd Edition 9.8 */
558 HRESULT
to_string(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, BSTR
*str
)
560 const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
561 const WCHAR nullW
[] = {'n','u','l','l',0};
562 const WCHAR trueW
[] = {'t','r','u','e',0};
563 const WCHAR falseW
[] = {'f','a','l','s','e',0};
567 *str
= SysAllocString(undefinedW
);
570 *str
= SysAllocString(nullW
);
573 *str
= int_to_bstr(V_I4(v
));
576 return double_to_bstr(V_R8(v
), str
);
578 *str
= SysAllocString(V_BSTR(v
));
584 hres
= to_primitive(ctx
, v
, ei
, &prim
, HINT_STRING
);
588 hres
= to_string(ctx
, &prim
, ei
, str
);
593 *str
= SysAllocString(V_BOOL(v
) ? trueW
: falseW
);
596 FIXME("unsupported vt %d\n", V_VT(v
));
600 return *str
? S_OK
: E_OUTOFMEMORY
;
603 /* ECMA-262 3rd Edition 9.9 */
604 HRESULT
to_object(script_ctx_t
*ctx
, VARIANT
*v
, IDispatch
**disp
)
611 hres
= create_string(ctx
, V_BSTR(v
), SysStringLen(V_BSTR(v
)), &dispex
);
615 *disp
= to_disp(dispex
);
619 hres
= create_number(ctx
, v
, &dispex
);
623 *disp
= to_disp(dispex
);
627 IDispatch_AddRef(V_DISPATCH(v
));
628 *disp
= V_DISPATCH(v
);
632 hres
= create_object(ctx
, NULL
, &obj
);
636 *disp
= to_disp(obj
);
640 hres
= create_bool(ctx
, V_BOOL(v
), &dispex
);
644 *disp
= to_disp(dispex
);
646 case VT_ARRAY
|VT_VARIANT
:
647 hres
= create_vbarray(ctx
, V_ARRAY(v
), &dispex
);
651 *disp
= to_disp(dispex
);
654 FIXME("unsupported vt %d\n", V_VT(v
));
661 HRESULT
variant_change_type(script_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
)
666 memset(&ei
, 0, sizeof(ei
));
673 hres
= to_int32(ctx
, src
, &ei
, &i
);
674 if(SUCCEEDED(hres
)) {
683 hres
= to_number(ctx
, src
, &ei
, dst
);
684 if(SUCCEEDED(hres
) && V_VT(dst
) == VT_I4
)
685 V_R8(dst
) = V_I4(dst
);
690 hres
= to_number(ctx
, src
, &ei
, &n
);
692 V_R4(dst
) = num_val(&n
);
698 hres
= to_boolean(src
, &b
);
706 hres
= to_string(ctx
, src
, &ei
, &str
);
712 hres
= V_VT(src
) == VT_EMPTY
? S_OK
: E_NOTIMPL
;
715 hres
= V_VT(src
) == VT_NULL
? S_OK
: E_NOTIMPL
;
718 FIXME("vt %d not implemented\n", vt
);
723 VariantClear(&ei
.var
);
731 static inline JSCaller
*impl_from_IServiceProvider(IServiceProvider
*iface
)
733 return CONTAINING_RECORD(iface
, JSCaller
, IServiceProvider_iface
);
736 static HRESULT WINAPI
JSCaller_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
738 JSCaller
*This
= impl_from_IServiceProvider(iface
);
740 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
741 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
742 *ppv
= &This
->IServiceProvider_iface
;
743 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
744 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
745 *ppv
= &This
->IServiceProvider_iface
;
747 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
749 return E_NOINTERFACE
;
752 IUnknown_AddRef((IUnknown
*)*ppv
);
756 static ULONG WINAPI
JSCaller_AddRef(IServiceProvider
*iface
)
758 JSCaller
*This
= impl_from_IServiceProvider(iface
);
759 LONG ref
= InterlockedIncrement(&This
->ref
);
761 TRACE("(%p) ref=%d\n", This
, ref
);
766 static ULONG WINAPI
JSCaller_Release(IServiceProvider
*iface
)
768 JSCaller
*This
= impl_from_IServiceProvider(iface
);
769 LONG ref
= InterlockedIncrement(&This
->ref
);
771 TRACE("(%p) ref=%d\n", This
, ref
);
781 static HRESULT WINAPI
JSCaller_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
782 REFIID riid
, void **ppv
)
784 JSCaller
*This
= impl_from_IServiceProvider(iface
);
786 if(IsEqualGUID(guidService
, &SID_VariantConversion
) && This
->ctx
&& This
->ctx
->active_script
) {
787 TRACE("(%p)->(SID_VariantConversion)\n", This
);
788 return IActiveScript_QueryInterface(This
->ctx
->active_script
, riid
, ppv
);
791 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
794 return E_NOINTERFACE
;
797 static const IServiceProviderVtbl ServiceProviderVtbl
= {
798 JSCaller_QueryInterface
,
801 JSCaller_QueryService
804 HRESULT
create_jscaller(script_ctx_t
*ctx
)
808 ret
= heap_alloc(sizeof(*ret
));
810 return E_OUTOFMEMORY
;
812 ret
->IServiceProvider_iface
.lpVtbl
= &ServiceProviderVtbl
;