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));
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);
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 ? -ret_inf() : ret_inf();
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;
452 FIXME("unimplemented for vt %d\n", V_VT(v));
459 /* ECMA-262 3rd Edition 9.4 */
460 HRESULT to_integer(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret)
465 if(V_VT(v) == VT_I4) {
470 hres = to_number(ctx, v, ei, &n);
478 num_set_val(ret, n >= 0.0 ? floor(n) : -floor(-n));
484 /* ECMA-262 3rd Edition 9.5 */
485 HRESULT to_int32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, INT *ret)
490 if(V_VT(v) == VT_I4) {
495 hres = to_number(ctx, v, ei, &n);
499 *ret = isnan(n) || isinf(n) ? 0 : n;
503 /* ECMA-262 3rd Edition 9.6 */
504 HRESULT to_uint32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, DWORD *ret)
509 if(V_VT(v) == VT_I4) {
514 hres = to_number(ctx, v, ei, &n);
518 *ret = isnan(n) || isinf(n) ? 0 : n;
522 BSTR int_to_bstr(int i)
528 static const WCHAR zeroW[] = {'0',0};
529 return SysAllocString(zeroW);
537 p = buf + sizeof(buf)/sizeof(*buf)-1;
549 return SysAllocString(p);
552 HRESULT double_to_bstr(double n, BSTR *str)
554 const WCHAR NaNW[] = {'N','a','N',0};
555 const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};
558 *str = SysAllocString(NaNW);
560 *str = SysAllocString(n<0 ? InfinityW : InfinityW+1);
567 V_VT(&strv) = VT_EMPTY;
568 hres = VariantChangeTypeEx(&strv, &v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
572 *str = V_BSTR(&strv);
575 return *str ? S_OK : E_OUTOFMEMORY;
578 /* ECMA-262 3rd Edition 9.8 */
579 HRESULT to_string(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, BSTR *str)
581 const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
582 const WCHAR nullW[] = {'n','u','l','l',0};
583 const WCHAR trueW[] = {'t','r','u','e',0};
584 const WCHAR falseW[] = {'f','a','l','s','e',0};
588 *str = SysAllocString(undefinedW);
591 *str = SysAllocString(nullW);
594 *str = int_to_bstr(V_I4(v));
597 return double_to_bstr(V_R8(v), str);
599 *str = SysAllocString(V_BSTR(v));
605 hres = to_primitive(ctx, v, ei, &prim, HINT_STRING);
609 hres = to_string(ctx, &prim, ei, str);
614 *str = SysAllocString(V_BOOL(v) ? trueW : falseW);
617 FIXME("unsupported vt %d\n", V_VT(v));
621 return *str ? S_OK : E_OUTOFMEMORY;
624 /* ECMA-262 3rd Edition 9.9 */
625 HRESULT to_object(script_ctx_t *ctx, VARIANT *v, IDispatch **disp)
632 hres = create_string(ctx, V_BSTR(v), SysStringLen(V_BSTR(v)), &dispex);
636 *disp = to_disp(dispex);
640 hres = create_number(ctx, num_val(v), &dispex);
644 *disp = to_disp(dispex);
648 IDispatch_AddRef(V_DISPATCH(v));
649 *disp = V_DISPATCH(v);
653 hres = create_object(ctx, NULL, &obj);
657 *disp = to_disp(obj);
661 hres = create_bool(ctx, V_BOOL(v), &dispex);
665 *disp = to_disp(dispex);
667 case VT_ARRAY|VT_VARIANT:
668 hres = create_vbarray(ctx, V_ARRAY(v), &dispex);
672 *disp = to_disp(dispex);
675 FIXME("unsupported vt %d\n", V_VT(v));
682 HRESULT variant_change_type(script_ctx_t *ctx, VARIANT *dst, VARIANT *src, VARTYPE vt)
687 memset(&ei, 0, sizeof(ei));
694 hres = to_int32(ctx, src, &ei, &i);
695 if(SUCCEEDED(hres)) {
705 hres = to_number(ctx, src, &ei, &n);
713 hres = to_number(ctx, src, &ei, &n);
721 hres = to_boolean(src, &b);
729 hres = to_string(ctx, src, &ei, &str);
735 hres = V_VT(src) == VT_EMPTY ? S_OK : E_NOTIMPL;
738 hres = V_VT(src) == VT_NULL ? S_OK : E_NOTIMPL;
741 FIXME("vt %d not implemented\n", vt);
746 VariantClear(&ei.var);
754 static inline JSCaller *impl_from_IServiceProvider(IServiceProvider *iface)
756 return CONTAINING_RECORD(iface, JSCaller, IServiceProvider_iface);
759 static HRESULT WINAPI JSCaller_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
761 JSCaller *This = impl_from_IServiceProvider(iface);
763 if(IsEqualGUID(&IID_IUnknown, riid)) {
764 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
765 *ppv = &This->IServiceProvider_iface;
766 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
767 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
768 *ppv = &This->IServiceProvider_iface;
770 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
772 return E_NOINTERFACE;
775 IUnknown_AddRef((IUnknown*)*ppv);
779 static ULONG WINAPI JSCaller_AddRef(IServiceProvider *iface)
781 JSCaller *This = impl_from_IServiceProvider(iface);
782 LONG ref = InterlockedIncrement(&This->ref);
784 TRACE("(%p) ref=%d\n", This, ref);
789 static ULONG WINAPI JSCaller_Release(IServiceProvider *iface)
791 JSCaller *This = impl_from_IServiceProvider(iface);
792 LONG ref = InterlockedIncrement(&This->ref);
794 TRACE("(%p) ref=%d\n", This, ref);
804 static HRESULT WINAPI JSCaller_QueryService(IServiceProvider *iface, REFGUID guidService,
805 REFIID riid, void **ppv)
807 JSCaller *This = impl_from_IServiceProvider(iface);
809 if(IsEqualGUID(guidService, &SID_VariantConversion) && This->ctx && This->ctx->active_script) {
810 TRACE("(%p)->(SID_VariantConversion)\n", This);
811 return IActiveScript_QueryInterface(This->ctx->active_script, riid, ppv);
814 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
817 return E_NOINTERFACE;
820 static const IServiceProviderVtbl ServiceProviderVtbl = {
821 JSCaller_QueryInterface,
824 JSCaller_QueryService
827 HRESULT create_jscaller(script_ctx_t *ctx)
831 ret = heap_alloc(sizeof(*ret));
833 return E_OUTOFMEMORY;
835 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;