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));
55 case VT_ARRAY|VT_VARIANT:
56 return "{VT_ARRAY|VT_VARIANT: ...}";
58 return wine_dbg_sprintf("{vt %d}", V_VT(v));
62 const char *debugstr_jsval(const jsval_t v)
64 switch(jsval_type(v)) {
70 return wine_dbg_sprintf("obj(%p)", get_object(v));
72 return wine_dbg_sprintf("str(%s)", debugstr_jsstr(get_string(v)));
74 return wine_dbg_sprintf("%lf", get_number(v));
76 return get_bool(v) ? "true" : "false";
78 return debugstr_variant(get_variant(v));
85 #define MIN_BLOCK_SIZE 128
86 #define ARENA_FREE_FILLER 0xaa
88 static inline DWORD block_size(DWORD block)
90 return MIN_BLOCK_SIZE << block;
93 void heap_pool_init(heap_pool_t *heap)
95 memset(heap, 0, sizeof(*heap));
96 list_init(&heap->custom_blocks);
99 void *heap_pool_alloc(heap_pool_t *heap, DWORD size)
104 if(!heap->block_cnt) {
106 heap->blocks = heap_alloc(sizeof(void*));
111 tmp = heap_alloc(block_size(0));
115 heap->blocks[0] = tmp;
119 if(heap->offset + size <= block_size(heap->last_block)) {
120 tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
121 heap->offset += size;
125 if(size <= block_size(heap->last_block+1)) {
126 if(heap->last_block+1 == heap->block_cnt) {
127 tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
132 heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
133 if(!heap->blocks[heap->block_cnt])
141 return heap->blocks[heap->last_block];
144 list = heap_alloc(size + sizeof(struct list));
148 list_add_head(&heap->custom_blocks, list);
152 void *heap_pool_grow(heap_pool_t *heap, void *mem, DWORD size, DWORD inc)
156 if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
157 && heap->offset+inc < block_size(heap->last_block)) {
162 ret = heap_pool_alloc(heap, size+inc);
163 if(ret) /* FIXME: avoid copying for custom blocks */
164 memcpy(ret, mem, size);
168 void heap_pool_clear(heap_pool_t *heap)
175 while((tmp = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
183 for(i=0; i < heap->block_cnt; i++)
184 memset(heap->blocks[i], ARENA_FREE_FILLER, block_size(i));
187 heap->last_block = heap->offset = 0;
191 void heap_pool_free(heap_pool_t *heap)
195 heap_pool_clear(heap);
197 for(i=0; i < heap->block_cnt; i++)
198 heap_free(heap->blocks[i]);
199 heap_free(heap->blocks);
201 heap_pool_init(heap);
204 heap_pool_t *heap_pool_mark(heap_pool_t *heap)
213 void jsval_release(jsval_t val)
215 switch(jsval_type(val)) {
218 IDispatch_Release(get_object(val));
221 jsstr_release(get_string(val));
224 VariantClear(get_variant(val));
225 heap_free(get_variant(val));
232 static HRESULT jsval_variant(jsval_t *val, VARIANT *var)
237 __JSVAL_TYPE(*val) = JSV_VARIANT;
238 __JSVAL_VAR(*val) = v = heap_alloc(sizeof(VARIANT));
240 return E_OUTOFMEMORY;
243 hres = VariantCopy(v, var);
249 HRESULT jsval_copy(jsval_t v, jsval_t *r)
251 switch(jsval_type(v)) {
260 IDispatch_AddRef(get_object(v));
264 jsstr_addref(get_string(v));
269 return jsval_variant(r, get_variant(v));
276 HRESULT variant_to_jsval(VARIANT *var, jsval_t *r)
280 *r = jsval_undefined();
286 *r = jsval_bool(V_BOOL(var));
289 *r = jsval_number(V_I4(var));
292 *r = jsval_number(V_R8(var));
297 str = jsstr_alloc_len(V_BSTR(var), SysStringLen(V_BSTR(var)));
299 return E_OUTOFMEMORY;
301 str->length_flags |= JSSTR_FLAG_NULLBSTR;
303 *r = jsval_string(str);
308 IDispatch_AddRef(V_DISPATCH(var));
309 *r = jsval_disp(V_DISPATCH(var));
313 *r = jsval_number(V_I2(var));
316 *r = jsval_number(V_INT(var));
323 hres = IUnknown_QueryInterface(V_UNKNOWN(var), &IID_IDispatch, (void**)&disp);
324 if(SUCCEEDED(hres)) {
325 *r = jsval_disp(disp);
331 return jsval_variant(r, var);
335 HRESULT jsval_to_variant(jsval_t val, VARIANT *retv)
337 switch(jsval_type(val)) {
339 V_VT(retv) = VT_EMPTY;
342 V_VT(retv) = VT_NULL;
345 V_VT(retv) = VT_DISPATCH;
347 IDispatch_AddRef(get_object(val));
348 V_DISPATCH(retv) = get_object(val);
351 jsstr_t *str = get_string(val);
353 V_VT(retv) = VT_BSTR;
354 if(str->length_flags & JSSTR_FLAG_NULLBSTR) {
357 V_BSTR(retv) = SysAllocStringLen(NULL, jsstr_length(str));
359 jsstr_flush(str, V_BSTR(retv));
361 return E_OUTOFMEMORY;
366 double n = get_number(val);
379 V_VT(retv) = VT_BOOL;
380 V_BOOL(retv) = get_bool(val) ? VARIANT_TRUE : VARIANT_FALSE;
383 V_VT(retv) = VT_EMPTY;
384 return VariantCopy(retv, get_variant(val));
391 /* ECMA-262 3rd Edition 9.1 */
392 HRESULT to_primitive(script_ctx_t *ctx, jsval_t val, jsval_t *ret, hint_t hint)
394 if(is_object_instance(val)) {
400 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
401 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
403 if(!get_object(val)) {
408 jsdisp = iface_to_jsdisp((IUnknown*)get_object(val));
410 return disp_propget(ctx, get_object(val), DISPID_VALUE, ret);
413 hint = is_class(jsdisp, JSCLASS_DATE) ? HINT_STRING : HINT_NUMBER;
415 /* Native implementation doesn't throw TypeErrors, returns strange values */
417 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? toStringW : valueOfW, 0, &id);
418 if(SUCCEEDED(hres)) {
419 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
421 WARN("call error - forwarding exception\n");
422 jsdisp_release(jsdisp);
424 }else if(!is_object_instance(prim)) {
425 jsdisp_release(jsdisp);
429 IDispatch_Release(get_object(prim));
433 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? valueOfW : toStringW, 0, &id);
434 if(SUCCEEDED(hres)) {
435 hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim);
437 WARN("call error - forwarding exception\n");
438 jsdisp_release(jsdisp);
440 }else if(!is_object_instance(prim)) {
441 jsdisp_release(jsdisp);
445 IDispatch_Release(get_object(prim));
449 jsdisp_release(jsdisp);
452 return throw_type_error(ctx, JS_E_TO_PRIMITIVE, NULL);
455 return jsval_copy(val, ret);
459 /* ECMA-262 3rd Edition 9.2 */
460 HRESULT to_boolean(jsval_t val, BOOL *ret)
462 switch(jsval_type(val)) {
468 *ret = get_object(val) != NULL;
471 *ret = jsstr_length(get_string(val)) != 0;
474 *ret = !isnan(get_number(val)) && get_number(val);
477 *ret = get_bool(val);
480 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
488 static int hex_to_int(WCHAR c)
490 if('0' <= c && c <= '9')
493 if('a' <= c && c <= 'f')
496 if('A' <= c && c <= 'F')
502 /* ECMA-262 3rd Edition 9.3.1 */
503 static HRESULT str_to_number(jsstr_t *str, double *ret)
505 const WCHAR *ptr = str->str;
509 static const WCHAR infinityW[] = {'I','n','f','i','n','i','t','y'};
516 while(isspaceW(*ptr))
522 }else if(*ptr == '+') {
526 if(!strncmpW(ptr, infinityW, sizeof(infinityW)/sizeof(WCHAR))) {
527 ptr += sizeof(infinityW)/sizeof(WCHAR);
528 while(*ptr && isspaceW(*ptr))
534 *ret = neg ? -INFINITY : INFINITY;
538 if(*ptr == '0' && ptr[1] == 'x') {
542 while((l = hex_to_int(*ptr)) != -1) {
551 while(isdigitW(*ptr))
552 d = d*10 + (*ptr++ - '0');
554 if(*ptr == 'e' || *ptr == 'E') {
562 }else if(*ptr == '+') {
566 while(isdigitW(*ptr))
567 l = l*10 + (*ptr++ - '0');
572 }else if(*ptr == '.') {
576 while(isdigitW(*ptr)) {
577 d += dec * (*ptr++ - '0');
582 while(isspaceW(*ptr))
597 /* ECMA-262 3rd Edition 9.3 */
598 HRESULT to_number(script_ctx_t *ctx, jsval_t val, double *ret)
600 switch(jsval_type(val)) {
608 *ret = get_number(val);
611 return str_to_number(get_string(val), ret);
616 hres = to_primitive(ctx, val, &prim, HINT_NUMBER);
620 hres = to_number(ctx, prim, ret);
625 *ret = get_bool(val) ? 1 : 0;
628 FIXME("unimplemented for variant %s\n", debugstr_variant(get_variant(val)));
636 /* ECMA-262 3rd Edition 9.4 */
637 HRESULT to_integer(script_ctx_t *ctx, jsval_t v, double *ret)
642 hres = to_number(ctx, v, &n);
649 *ret = n >= 0.0 ? floor(n) : -floor(-n);
653 /* ECMA-262 3rd Edition 9.5 */
654 HRESULT to_int32(script_ctx_t *ctx, jsval_t v, INT *ret)
659 hres = to_number(ctx, v, &n);
663 *ret = isnan(n) || isinf(n) ? 0 : n;
667 /* ECMA-262 3rd Edition 9.6 */
668 HRESULT to_uint32(script_ctx_t *ctx, jsval_t val, DWORD *ret)
673 hres = to_int32(ctx, val, &n);
679 static jsstr_t *int_to_string(int i)
685 static const WCHAR zeroW[] = {'0',0};
686 return jsstr_alloc(zeroW);
694 p = buf + sizeof(buf)/sizeof(*buf)-1;
706 return jsstr_alloc(p);
709 HRESULT double_to_string(double n, jsstr_t **str)
711 const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};
716 *str = jsstr_alloc(n<0 ? InfinityW : InfinityW+1);
717 }else if(is_int32(n)) {
718 *str = int_to_string(n);
723 /* FIXME: Don't use VariantChangeTypeEx */
726 V_VT(&strv) = VT_EMPTY;
727 hres = VariantChangeTypeEx(&strv, &v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
731 *str = jsstr_alloc(V_BSTR(&strv));
732 SysFreeString(V_BSTR(&strv));
735 return *str ? S_OK : E_OUTOFMEMORY;
738 /* ECMA-262 3rd Edition 9.8 */
739 HRESULT to_string(script_ctx_t *ctx, jsval_t val, jsstr_t **str)
741 const WCHAR nullW[] = {'n','u','l','l',0};
742 const WCHAR trueW[] = {'t','r','u','e',0};
743 const WCHAR falseW[] = {'f','a','l','s','e',0};
745 switch(jsval_type(val)) {
747 *str = jsstr_undefined();
750 *str = jsstr_alloc(nullW);
753 return double_to_string(get_number(val), str);
755 *str = jsstr_addref(get_string(val));
761 hres = to_primitive(ctx, val, &prim, HINT_STRING);
765 hres = to_string(ctx, prim, str);
770 *str = jsstr_alloc(get_bool(val) ? trueW : falseW);
773 FIXME("unsupported %s\n", debugstr_jsval(val));
777 return *str ? S_OK : E_OUTOFMEMORY;
780 /* ECMA-262 3rd Edition 9.9 */
781 HRESULT to_object(script_ctx_t *ctx, jsval_t val, IDispatch **disp)
786 switch(jsval_type(val)) {
788 hres = create_string(ctx, get_string(val), &dispex);
792 *disp = to_disp(dispex);
795 hres = create_number(ctx, get_number(val), &dispex);
799 *disp = to_disp(dispex);
802 if(get_object(val)) {
803 *disp = get_object(val);
804 IDispatch_AddRef(*disp);
808 hres = create_object(ctx, NULL, &obj);
812 *disp = to_disp(obj);
816 hres = create_bool(ctx, get_bool(val), &dispex);
820 *disp = to_disp(dispex);
824 WARN("object expected\n");
825 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
827 switch(V_VT(get_variant(val))) {
828 case VT_ARRAY|VT_VARIANT:
829 hres = create_vbarray(ctx, V_ARRAY(get_variant(val)), &dispex);
833 *disp = to_disp(dispex);
837 FIXME("Unsupported %s\n", debugstr_variant(get_variant(val)));
846 HRESULT variant_change_type(script_ctx_t *ctx, VARIANT *dst, VARIANT *src, VARTYPE vt)
852 hres = variant_to_jsval(src, &val);
861 hres = to_int32(ctx, val, &i);
862 if(SUCCEEDED(hres)) {
872 hres = to_number(ctx, val, &n);
880 hres = to_number(ctx, val, &n);
888 hres = to_boolean(val, &b);
890 V_BOOL(dst) = b ? VARIANT_TRUE : VARIANT_FALSE;
896 hres = to_string(ctx, val, &str);
900 if(str->length_flags & JSSTR_FLAG_NULLBSTR) {
905 V_BSTR(dst) = SysAllocStringLen(NULL, jsstr_length(str));
907 jsstr_flush(str, V_BSTR(dst));
909 hres = E_OUTOFMEMORY;
913 hres = V_VT(src) == VT_EMPTY ? S_OK : E_NOTIMPL;
916 hres = V_VT(src) == VT_NULL ? S_OK : E_NOTIMPL;
919 FIXME("vt %d not implemented\n", vt);
931 static inline JSCaller *impl_from_IServiceProvider(IServiceProvider *iface)
933 return CONTAINING_RECORD(iface, JSCaller, IServiceProvider_iface);
936 static HRESULT WINAPI JSCaller_QueryInterface(IServiceProvider *iface, REFIID riid, void **ppv)
938 JSCaller *This = impl_from_IServiceProvider(iface);
940 if(IsEqualGUID(&IID_IUnknown, riid)) {
941 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
942 *ppv = &This->IServiceProvider_iface;
943 }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
944 TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
945 *ppv = &This->IServiceProvider_iface;
947 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
949 return E_NOINTERFACE;
952 IUnknown_AddRef((IUnknown*)*ppv);
956 static ULONG WINAPI JSCaller_AddRef(IServiceProvider *iface)
958 JSCaller *This = impl_from_IServiceProvider(iface);
959 LONG ref = InterlockedIncrement(&This->ref);
961 TRACE("(%p) ref=%d\n", This, ref);
966 static ULONG WINAPI JSCaller_Release(IServiceProvider *iface)
968 JSCaller *This = impl_from_IServiceProvider(iface);
969 LONG ref = InterlockedIncrement(&This->ref);
971 TRACE("(%p) ref=%d\n", This, ref);
981 static HRESULT WINAPI JSCaller_QueryService(IServiceProvider *iface, REFGUID guidService,
982 REFIID riid, void **ppv)
984 JSCaller *This = impl_from_IServiceProvider(iface);
986 if(IsEqualGUID(guidService, &SID_VariantConversion) && This->ctx && This->ctx->active_script) {
987 TRACE("(%p)->(SID_VariantConversion)\n", This);
988 return IActiveScript_QueryInterface(This->ctx->active_script, riid, ppv);
991 FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(guidService), debugstr_guid(riid), ppv);
994 return E_NOINTERFACE;
997 static const IServiceProviderVtbl ServiceProviderVtbl = {
998 JSCaller_QueryInterface,
1001 JSCaller_QueryService
1004 HRESULT create_jscaller(script_ctx_t *ctx)
1008 ret = heap_alloc(sizeof(*ret));
1010 return E_OUTOFMEMORY;
1012 ret->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
1016 ctx->jscaller = ret;