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, double *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 *ret = neg ? -ret_inf() : ret_inf();
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, double *ret)
421 return str_to_number(V_BSTR(v), ret);
426 hres = to_primitive(ctx, v, ei, &prim, HINT_NUMBER);
430 hres = to_number(ctx, &prim, ei, ret);
435 *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 if(V_VT(v) == VT_I4) {
456 hres = to_number(ctx, v, ei, &n);
464 num_set_val(ret, n >= 0.0 ? floor(n) : -floor(-n));
470 /* ECMA-262 3rd Edition 9.5 */
471 HRESULT to_int32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, INT *ret)
476 if(V_VT(v) == VT_I4) {
481 hres = to_number(ctx, v, ei, &n);
485 *ret = isnan(n) || isinf(n) ? 0 : n;
489 /* ECMA-262 3rd Edition 9.6 */
490 HRESULT to_uint32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, DWORD *ret)
495 if(V_VT(v) == VT_I4) {
500 hres = to_number(ctx, v, ei, &n);
504 *ret = isnan(n) || isinf(n) ? 0 : n;
508 BSTR int_to_bstr(int i)
514 static const WCHAR zeroW[] = {'0',0};
515 return SysAllocString(zeroW);
523 p = buf + sizeof(buf)/sizeof(*buf)-1;
535 return SysAllocString(p);
538 HRESULT double_to_bstr(double n, BSTR *str)
540 const WCHAR NaNW[] = {'N','a','N',0};
541 const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};
544 *str = SysAllocString(NaNW);
546 *str = SysAllocString(n<0 ? InfinityW : InfinityW+1);
553 V_VT(&strv) = VT_EMPTY;
554 hres = VariantChangeTypeEx(&strv, &v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
558 *str = V_BSTR(&strv);
561 return *str ? S_OK : E_OUTOFMEMORY;
564 /* ECMA-262 3rd Edition 9.8 */
565 HRESULT to_string(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, BSTR *str)
567 const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
568 const WCHAR nullW[] = {'n','u','l','l',0};
569 const WCHAR trueW[] = {'t','r','u','e',0};
570 const WCHAR falseW[] = {'f','a','l','s','e',0};
574 *str = SysAllocString(undefinedW);
577 *str = SysAllocString(nullW);
580 *str = int_to_bstr(V_I4(v));
583 return double_to_bstr(V_R8(v), str);
585 *str = SysAllocString(V_BSTR(v));
591 hres = to_primitive(ctx, v, ei, &prim, HINT_STRING);
595 hres = to_string(ctx, &prim, ei, str);
600 *str = SysAllocString(V_BOOL(v) ? trueW : falseW);
603 FIXME("unsupported vt %d\n", V_VT(v));
607 return *str ? S_OK : E_OUTOFMEMORY;
610 /* ECMA-262 3rd Edition 9.9 */
611 HRESULT to_object(script_ctx_t *ctx, VARIANT *v, IDispatch **disp)
618 hres = create_string(ctx, V_BSTR(v), SysStringLen(V_BSTR(v)), &dispex);
622 *disp = to_disp(dispex);
626 hres = create_number(ctx, v, &dispex);
630 *disp = to_disp(dispex);
634 IDispatch_AddRef(V_DISPATCH(v));
635 *disp = V_DISPATCH(v);
639 hres = create_object(ctx, NULL, &obj);
643 *disp = to_disp(obj);
647 hres = create_bool(ctx, V_BOOL(v), &dispex);
651 *disp = to_disp(dispex);
653 case VT_ARRAY|VT_VARIANT:
654 hres = create_vbarray(ctx, V_ARRAY(v), &dispex);
658 *disp = to_disp(dispex);
661 FIXME("unsupported vt %d\n", V_VT(v));
668 HRESULT variant_change_type(script_ctx_t *ctx, VARIANT *dst, VARIANT *src, VARTYPE vt)
673 memset(&ei, 0, sizeof(ei));
680 hres = to_int32(ctx, src, &ei, &i);
681 if(SUCCEEDED(hres)) {
691 hres = to_number(ctx, src, &ei, &n);
699 hres = to_number(ctx, src, &ei, &n);
707 hres = to_boolean(src, &b);
715 hres = to_string(ctx, src, &ei, &str);
721 hres = V_VT(src) == VT_EMPTY ? S_OK : E_NOTIMPL;
724 hres = V_VT(src) == VT_NULL ? S_OK : E_NOTIMPL;
727 FIXME("vt %d not implemented\n", vt);
732 VariantClear(&ei.var);
740 static inline JSCaller *impl_from_IServiceProvider(IServiceProvider *iface)
742 return CONTAINING_RECORD(iface, JSCaller, IServiceProvider_iface);
745 static HRESULT WINAPI JSCaller_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
747 JSCaller *This = impl_from_IServiceProvider(iface);
749 if(IsEqualGUID(&IID_IUnknown, riid)) {
750 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
751 *ppv = &This->IServiceProvider_iface;
752 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
753 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
754 *ppv = &This->IServiceProvider_iface;
756 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
758 return E_NOINTERFACE;
761 IUnknown_AddRef((IUnknown*)*ppv);
765 static ULONG WINAPI JSCaller_AddRef(IServiceProvider *iface)
767 JSCaller *This = impl_from_IServiceProvider(iface);
768 LONG ref = InterlockedIncrement(&This->ref);
770 TRACE("(%p) ref=%d\n", This, ref);
775 static ULONG WINAPI JSCaller_Release(IServiceProvider *iface)
777 JSCaller *This = impl_from_IServiceProvider(iface);
778 LONG ref = InterlockedIncrement(&This->ref);
780 TRACE("(%p) ref=%d\n", This, ref);
790 static HRESULT WINAPI JSCaller_QueryService(IServiceProvider *iface, REFGUID guidService,
791 REFIID riid, void **ppv)
793 JSCaller *This = impl_from_IServiceProvider(iface);
795 if(IsEqualGUID(guidService, &SID_VariantConversion) && This->ctx && This->ctx->active_script) {
796 TRACE("(%p)->(SID_VariantConversion)\n", This);
797 return IActiveScript_QueryInterface(This->ctx->active_script, riid, ppv);
800 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
803 return E_NOINTERFACE;
806 static const IServiceProviderVtbl ServiceProviderVtbl = {
807 JSCaller_QueryInterface,
810 JSCaller_QueryService
813 HRESULT create_jscaller(script_ctx_t *ctx)
817 ret = heap_alloc(sizeof(*ret));
819 return E_OUTOFMEMORY;
821 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;