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)
131 if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
132 && heap->offset+inc < block_size(heap->last_block)) {
137 ret = jsheap_alloc(heap, size+inc);
138 if(ret) /* FIXME: avoid coppying for custom blocks */
139 memcpy(ret, mem, size);
143 void jsheap_clear(jsheap_t *heap)
150 while((tmp = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
158 for(i=0; i < heap->block_cnt; i++)
159 memset(heap->blocks[i], ARENA_FREE_FILLER, block_size(i));
162 heap->last_block = heap->offset = 0;
166 void jsheap_free(jsheap_t *heap)
172 for(i=0; i < heap->block_cnt; i++)
173 heap_free(heap->blocks[i]);
174 heap_free(heap->blocks);
179 jsheap_t *jsheap_mark(jsheap_t *heap)
188 /* ECMA-262 3rd Edition 9.1 */
189 HRESULT to_primitive(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret, hint_t hint)
201 V_BSTR(ret) = SysAllocString(V_BSTR(v));
206 DISPPARAMS dp = {NULL, NULL, 0, 0};
209 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
210 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
217 jsdisp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
219 V_VT(ret) = VT_EMPTY;
220 return disp_propget(ctx, V_DISPATCH(v), DISPID_VALUE, ret, ei);
224 hint = is_class(jsdisp, JSCLASS_DATE) ? HINT_STRING : HINT_NUMBER;
226 /* Native implementation doesn't throw TypeErrors, returns strange values */
228 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? toStringW : valueOfW, 0, &id);
229 if(SUCCEEDED(hres)) {
230 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, &dp, ret, ei);
232 WARN("call error - forwarding exception\n");
233 jsdisp_release(jsdisp);
236 else if(V_VT(ret) != VT_DISPATCH) {
237 jsdisp_release(jsdisp);
241 IDispatch_Release(V_DISPATCH(ret));
244 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? valueOfW : toStringW, 0, &id);
245 if(SUCCEEDED(hres)) {
246 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, &dp, ret, ei);
248 WARN("call error - forwarding exception\n");
249 jsdisp_release(jsdisp);
252 else if(V_VT(ret) != VT_DISPATCH) {
253 jsdisp_release(jsdisp);
257 IDispatch_Release(V_DISPATCH(ret));
260 jsdisp_release(jsdisp);
263 return throw_type_error(ctx, ei, JS_E_TO_PRIMITIVE, NULL);
266 FIXME("Unimplemented for vt %d\n", V_VT(v));
273 /* ECMA-262 3rd Edition 9.2 */
274 HRESULT to_boolean(VARIANT *v, VARIANT_BOOL *b)
282 *b = V_I4(v) ? VARIANT_TRUE : VARIANT_FALSE;
285 if(isnan(V_R8(v))) *b = VARIANT_FALSE;
286 else *b = V_R8(v) ? VARIANT_TRUE : VARIANT_FALSE;
289 *b = V_BSTR(v) && *V_BSTR(v) ? VARIANT_TRUE : VARIANT_FALSE;
292 *b = V_DISPATCH(v) ? VARIANT_TRUE : VARIANT_FALSE;
298 FIXME("unimplemented for vt %d\n", V_VT(v));
305 static int hex_to_int(WCHAR c)
307 if('0' <= c && c <= '9')
310 if('a' <= c && c <= 'f')
313 if('A' <= c && c <= 'F')
319 /* ECMA-262 3rd Edition 9.3.1 */
320 static HRESULT str_to_number(BSTR str, double *ret)
322 const WCHAR *ptr = str;
326 static const WCHAR infinityW[] = {'I','n','f','i','n','i','t','y'};
333 while(isspaceW(*ptr))
339 }else if(*ptr == '+') {
343 if(!strncmpW(ptr, infinityW, sizeof(infinityW)/sizeof(WCHAR))) {
344 ptr += sizeof(infinityW)/sizeof(WCHAR);
345 while(*ptr && isspaceW(*ptr))
351 *ret = neg ? -ret_inf() : ret_inf();
355 if(*ptr == '0' && ptr[1] == 'x') {
359 while((l = hex_to_int(*ptr)) != -1) {
368 while(isdigitW(*ptr))
369 d = d*10 + (*ptr++ - '0');
371 if(*ptr == 'e' || *ptr == 'E') {
379 }else if(*ptr == '+') {
383 while(isdigitW(*ptr))
384 l = l*10 + (*ptr++ - '0');
389 }else if(*ptr == '.') {
393 while(isdigitW(*ptr)) {
394 d += dec * (*ptr++ - '0');
399 while(isspaceW(*ptr))
414 /* ECMA-262 3rd Edition 9.3 */
415 HRESULT to_number(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, double *ret)
431 return str_to_number(V_BSTR(v), ret);
436 hres = to_primitive(ctx, v, ei, &prim, HINT_NUMBER);
440 hres = to_number(ctx, &prim, ei, ret);
445 *ret = V_BOOL(v) ? 1 : 0;
448 FIXME("unimplemented for vt %d\n", V_VT(v));
455 /* ECMA-262 3rd Edition 9.4 */
456 HRESULT to_integer(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret)
461 if(V_VT(v) == VT_I4) {
466 hres = to_number(ctx, v, ei, &n);
474 num_set_val(ret, n >= 0.0 ? floor(n) : -floor(-n));
480 /* ECMA-262 3rd Edition 9.5 */
481 HRESULT to_int32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, INT *ret)
486 if(V_VT(v) == VT_I4) {
491 hres = to_number(ctx, v, ei, &n);
495 *ret = isnan(n) || isinf(n) ? 0 : n;
499 /* ECMA-262 3rd Edition 9.6 */
500 HRESULT to_uint32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, DWORD *ret)
505 if(V_VT(v) == VT_I4) {
510 hres = to_number(ctx, v, ei, &n);
514 *ret = isnan(n) || isinf(n) ? 0 : n;
518 BSTR int_to_bstr(int i)
524 static const WCHAR zeroW[] = {'0',0};
525 return SysAllocString(zeroW);
533 p = buf + sizeof(buf)/sizeof(*buf)-1;
545 return SysAllocString(p);
548 HRESULT double_to_bstr(double n, BSTR *str)
550 const WCHAR NaNW[] = {'N','a','N',0};
551 const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};
554 *str = SysAllocString(NaNW);
556 *str = SysAllocString(n<0 ? InfinityW : InfinityW+1);
563 V_VT(&strv) = VT_EMPTY;
564 hres = VariantChangeTypeEx(&strv, &v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
568 *str = V_BSTR(&strv);
571 return *str ? S_OK : E_OUTOFMEMORY;
574 /* ECMA-262 3rd Edition 9.8 */
575 HRESULT to_string(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, BSTR *str)
577 const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
578 const WCHAR nullW[] = {'n','u','l','l',0};
579 const WCHAR trueW[] = {'t','r','u','e',0};
580 const WCHAR falseW[] = {'f','a','l','s','e',0};
584 *str = SysAllocString(undefinedW);
587 *str = SysAllocString(nullW);
590 *str = int_to_bstr(V_I4(v));
593 return double_to_bstr(V_R8(v), str);
595 *str = SysAllocString(V_BSTR(v));
601 hres = to_primitive(ctx, v, ei, &prim, HINT_STRING);
605 hres = to_string(ctx, &prim, ei, str);
610 *str = SysAllocString(V_BOOL(v) ? trueW : falseW);
613 FIXME("unsupported vt %d\n", V_VT(v));
617 return *str ? S_OK : E_OUTOFMEMORY;
620 /* ECMA-262 3rd Edition 9.9 */
621 HRESULT to_object(script_ctx_t *ctx, VARIANT *v, IDispatch **disp)
628 hres = create_string(ctx, V_BSTR(v), SysStringLen(V_BSTR(v)), &dispex);
632 *disp = to_disp(dispex);
636 hres = create_number(ctx, num_val(v), &dispex);
640 *disp = to_disp(dispex);
644 IDispatch_AddRef(V_DISPATCH(v));
645 *disp = V_DISPATCH(v);
649 hres = create_object(ctx, NULL, &obj);
653 *disp = to_disp(obj);
657 hres = create_bool(ctx, V_BOOL(v), &dispex);
661 *disp = to_disp(dispex);
663 case VT_ARRAY|VT_VARIANT:
664 hres = create_vbarray(ctx, V_ARRAY(v), &dispex);
668 *disp = to_disp(dispex);
671 FIXME("unsupported vt %d\n", V_VT(v));
678 HRESULT variant_change_type(script_ctx_t *ctx, VARIANT *dst, VARIANT *src, VARTYPE vt)
683 memset(&ei, 0, sizeof(ei));
690 hres = to_int32(ctx, src, &ei, &i);
691 if(SUCCEEDED(hres)) {
701 hres = to_number(ctx, src, &ei, &n);
709 hres = to_number(ctx, src, &ei, &n);
717 hres = to_boolean(src, &b);
725 hres = to_string(ctx, src, &ei, &str);
731 hres = V_VT(src) == VT_EMPTY ? S_OK : E_NOTIMPL;
734 hres = V_VT(src) == VT_NULL ? S_OK : E_NOTIMPL;
737 FIXME("vt %d not implemented\n", vt);
742 VariantClear(&ei.var);
750 static inline JSCaller *impl_from_IServiceProvider(IServiceProvider *iface)
752 return CONTAINING_RECORD(iface, JSCaller, IServiceProvider_iface);
755 static HRESULT WINAPI JSCaller_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
757 JSCaller *This = impl_from_IServiceProvider(iface);
759 if(IsEqualGUID(&IID_IUnknown, riid)) {
760 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
761 *ppv = &This->IServiceProvider_iface;
762 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
763 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
764 *ppv = &This->IServiceProvider_iface;
766 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
768 return E_NOINTERFACE;
771 IUnknown_AddRef((IUnknown*)*ppv);
775 static ULONG WINAPI JSCaller_AddRef(IServiceProvider *iface)
777 JSCaller *This = impl_from_IServiceProvider(iface);
778 LONG ref = InterlockedIncrement(&This->ref);
780 TRACE("(%p) ref=%d\n", This, ref);
785 static ULONG WINAPI JSCaller_Release(IServiceProvider *iface)
787 JSCaller *This = impl_from_IServiceProvider(iface);
788 LONG ref = InterlockedIncrement(&This->ref);
790 TRACE("(%p) ref=%d\n", This, ref);
800 static HRESULT WINAPI JSCaller_QueryService(IServiceProvider *iface, REFGUID guidService,
801 REFIID riid, void **ppv)
803 JSCaller *This = impl_from_IServiceProvider(iface);
805 if(IsEqualGUID(guidService, &SID_VariantConversion) && This->ctx && This->ctx->active_script) {
806 TRACE("(%p)->(SID_VariantConversion)\n", This);
807 return IActiveScript_QueryInterface(This->ctx->active_script, riid, ppv);
810 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
813 return E_NOINTERFACE;
816 static const IServiceProviderVtbl ServiceProviderVtbl = {
817 JSCaller_QueryInterface,
820 JSCaller_QueryService
823 HRESULT create_jscaller(script_ctx_t *ctx)
827 ret = heap_alloc(sizeof(*ret));
829 return E_OUTOFMEMORY;
831 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;