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 copying 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));
208 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
209 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
216 jsdisp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
218 V_VT(ret) = VT_EMPTY;
219 return disp_propget(ctx, V_DISPATCH(v), DISPID_VALUE, ret, ei);
223 hint = is_class(jsdisp, JSCLASS_DATE) ? HINT_STRING : HINT_NUMBER;
225 /* Native implementation doesn't throw TypeErrors, returns strange values */
227 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? toStringW : valueOfW, 0, &id);
228 if(SUCCEEDED(hres)) {
229 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, ret, ei);
231 WARN("call error - forwarding exception\n");
232 jsdisp_release(jsdisp);
235 else if(V_VT(ret) != VT_DISPATCH) {
236 jsdisp_release(jsdisp);
240 IDispatch_Release(V_DISPATCH(ret));
243 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? valueOfW : toStringW, 0, &id);
244 if(SUCCEEDED(hres)) {
245 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, ret, ei);
247 WARN("call error - forwarding exception\n");
248 jsdisp_release(jsdisp);
251 else if(V_VT(ret) != VT_DISPATCH) {
252 jsdisp_release(jsdisp);
256 IDispatch_Release(V_DISPATCH(ret));
259 jsdisp_release(jsdisp);
262 return throw_type_error(ctx, ei, JS_E_TO_PRIMITIVE, NULL);
268 FIXME("Unimplemented for vt %d\n", V_VT(v));
275 /* ECMA-262 3rd Edition 9.2 */
276 HRESULT to_boolean(VARIANT *v, VARIANT_BOOL *b)
284 *b = V_I4(v) ? VARIANT_TRUE : VARIANT_FALSE;
287 if(isnan(V_R8(v))) *b = VARIANT_FALSE;
288 else *b = V_R8(v) ? VARIANT_TRUE : VARIANT_FALSE;
291 *b = V_BSTR(v) && *V_BSTR(v) ? VARIANT_TRUE : VARIANT_FALSE;
294 *b = V_DISPATCH(v) ? VARIANT_TRUE : VARIANT_FALSE;
300 FIXME("unimplemented for vt %d\n", V_VT(v));
307 static int hex_to_int(WCHAR c)
309 if('0' <= c && c <= '9')
312 if('a' <= c && c <= 'f')
315 if('A' <= c && c <= 'F')
321 /* ECMA-262 3rd Edition 9.3.1 */
322 static HRESULT str_to_number(BSTR str, double *ret)
324 const WCHAR *ptr = str;
328 static const WCHAR infinityW[] = {'I','n','f','i','n','i','t','y'};
335 while(isspaceW(*ptr))
341 }else if(*ptr == '+') {
345 if(!strncmpW(ptr, infinityW, sizeof(infinityW)/sizeof(WCHAR))) {
346 ptr += sizeof(infinityW)/sizeof(WCHAR);
347 while(*ptr && isspaceW(*ptr))
353 *ret = neg ? -INFINITY : INFINITY;
357 if(*ptr == '0' && ptr[1] == 'x') {
361 while((l = hex_to_int(*ptr)) != -1) {
370 while(isdigitW(*ptr))
371 d = d*10 + (*ptr++ - '0');
373 if(*ptr == 'e' || *ptr == 'E') {
381 }else if(*ptr == '+') {
385 while(isdigitW(*ptr))
386 l = l*10 + (*ptr++ - '0');
391 }else if(*ptr == '.') {
395 while(isdigitW(*ptr)) {
396 d += dec * (*ptr++ - '0');
401 while(isspaceW(*ptr))
416 /* ECMA-262 3rd Edition 9.3 */
417 HRESULT to_number(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, double *ret)
433 return str_to_number(V_BSTR(v), ret);
438 hres = to_primitive(ctx, v, ei, &prim, HINT_NUMBER);
442 hres = to_number(ctx, &prim, ei, ret);
447 *ret = V_BOOL(v) ? 1 : 0;
453 FIXME("unimplemented for vt %d\n", V_VT(v));
460 /* ECMA-262 3rd Edition 9.4 */
461 HRESULT to_integer(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, double *ret)
466 if(V_VT(v) == VT_I4) {
471 hres = to_number(ctx, v, ei, &n);
478 *ret = n >= 0.0 ? floor(n) : -floor(-n);
482 /* ECMA-262 3rd Edition 9.5 */
483 HRESULT to_int32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, INT *ret)
488 if(V_VT(v) == VT_I4) {
493 hres = to_number(ctx, v, ei, &n);
497 *ret = isnan(n) || isinf(n) ? 0 : n;
501 /* ECMA-262 3rd Edition 9.6 */
502 HRESULT to_uint32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, DWORD *ret)
507 if(V_VT(v) == VT_I4) {
512 hres = to_number(ctx, v, ei, &n);
516 *ret = isnan(n) || isinf(n) ? 0 : n;
520 static BSTR int_to_bstr(int i)
526 static const WCHAR zeroW[] = {'0',0};
527 return SysAllocString(zeroW);
535 p = buf + sizeof(buf)/sizeof(*buf)-1;
547 return SysAllocString(p);
550 HRESULT double_to_bstr(double n, BSTR *str)
552 const WCHAR NaNW[] = {'N','a','N',0};
553 const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};
556 *str = SysAllocString(NaNW);
558 *str = SysAllocString(n<0 ? InfinityW : InfinityW+1);
565 V_VT(&strv) = VT_EMPTY;
566 hres = VariantChangeTypeEx(&strv, &v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
570 *str = V_BSTR(&strv);
573 return *str ? S_OK : E_OUTOFMEMORY;
576 /* ECMA-262 3rd Edition 9.8 */
577 HRESULT to_string(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, BSTR *str)
579 const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
580 const WCHAR nullW[] = {'n','u','l','l',0};
581 const WCHAR trueW[] = {'t','r','u','e',0};
582 const WCHAR falseW[] = {'f','a','l','s','e',0};
586 *str = SysAllocString(undefinedW);
589 *str = SysAllocString(nullW);
592 *str = int_to_bstr(V_I4(v));
595 return double_to_bstr(V_R8(v), str);
597 *str = SysAllocString(V_BSTR(v));
603 hres = to_primitive(ctx, v, ei, &prim, HINT_STRING);
607 hres = to_string(ctx, &prim, ei, str);
612 *str = SysAllocString(V_BOOL(v) ? trueW : falseW);
615 FIXME("unsupported vt %d\n", V_VT(v));
619 return *str ? S_OK : E_OUTOFMEMORY;
622 /* ECMA-262 3rd Edition 9.9 */
623 HRESULT to_object(script_ctx_t *ctx, VARIANT *v, IDispatch **disp)
630 hres = create_string(ctx, V_BSTR(v), SysStringLen(V_BSTR(v)), &dispex);
634 *disp = to_disp(dispex);
638 hres = create_number(ctx, num_val(v), &dispex);
642 *disp = to_disp(dispex);
646 IDispatch_AddRef(V_DISPATCH(v));
647 *disp = V_DISPATCH(v);
651 hres = create_object(ctx, NULL, &obj);
655 *disp = to_disp(obj);
659 hres = create_bool(ctx, V_BOOL(v), &dispex);
663 *disp = to_disp(dispex);
665 case VT_ARRAY|VT_VARIANT:
666 hres = create_vbarray(ctx, V_ARRAY(v), &dispex);
670 *disp = to_disp(dispex);
673 FIXME("unsupported vt %d\n", V_VT(v));
680 HRESULT variant_change_type(script_ctx_t *ctx, VARIANT *dst, VARIANT *src, VARTYPE vt)
685 memset(&ei, 0, sizeof(ei));
692 hres = to_int32(ctx, src, &ei, &i);
693 if(SUCCEEDED(hres)) {
703 hres = to_number(ctx, src, &ei, &n);
711 hres = to_number(ctx, src, &ei, &n);
719 hres = to_boolean(src, &b);
727 hres = to_string(ctx, src, &ei, &str);
733 hres = V_VT(src) == VT_EMPTY ? S_OK : E_NOTIMPL;
736 hres = V_VT(src) == VT_NULL ? S_OK : E_NOTIMPL;
739 FIXME("vt %d not implemented\n", vt);
744 VariantClear(&ei.var);
752 static inline JSCaller *impl_from_IServiceProvider(IServiceProvider *iface)
754 return CONTAINING_RECORD(iface, JSCaller, IServiceProvider_iface);
757 static HRESULT WINAPI JSCaller_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
759 JSCaller *This = impl_from_IServiceProvider(iface);
761 if(IsEqualGUID(&IID_IUnknown, riid)) {
762 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
763 *ppv = &This->IServiceProvider_iface;
764 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
765 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
766 *ppv = &This->IServiceProvider_iface;
768 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
770 return E_NOINTERFACE;
773 IUnknown_AddRef((IUnknown*)*ppv);
777 static ULONG WINAPI JSCaller_AddRef(IServiceProvider *iface)
779 JSCaller *This = impl_from_IServiceProvider(iface);
780 LONG ref = InterlockedIncrement(&This->ref);
782 TRACE("(%p) ref=%d\n", This, ref);
787 static ULONG WINAPI JSCaller_Release(IServiceProvider *iface)
789 JSCaller *This = impl_from_IServiceProvider(iface);
790 LONG ref = InterlockedIncrement(&This->ref);
792 TRACE("(%p) ref=%d\n", This, ref);
802 static HRESULT WINAPI JSCaller_QueryService(IServiceProvider *iface, REFGUID guidService,
803 REFIID riid, void **ppv)
805 JSCaller *This = impl_from_IServiceProvider(iface);
807 if(IsEqualGUID(guidService, &SID_VariantConversion) && This->ctx && This->ctx->active_script) {
808 TRACE("(%p)->(SID_VariantConversion)\n", This);
809 return IActiveScript_QueryInterface(This->ctx->active_script, riid, ppv);
812 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
815 return E_NOINTERFACE;
818 static const IServiceProviderVtbl ServiceProviderVtbl = {
819 JSCaller_QueryInterface,
822 JSCaller_QueryService
825 HRESULT create_jscaller(script_ctx_t *ctx)
829 ret = heap_alloc(sizeof(*ret));
831 return E_OUTOFMEMORY;
833 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;