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"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
31 const char *debugstr_variant(const VARIANT *v)
35 return wine_dbg_sprintf("{VT_EMPTY}");
37 return wine_dbg_sprintf("{VT_NULL}");
39 return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v));
41 return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v));
43 return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v)));
45 return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v));
47 return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v));
49 return wine_dbg_sprintf("{vt %d}", V_VT(v));
53 #define MIN_BLOCK_SIZE 128
55 static inline DWORD block_size(DWORD block)
57 return MIN_BLOCK_SIZE << block;
60 void jsheap_init(jsheap_t *heap)
62 memset(heap, 0, sizeof(*heap));
63 list_init(&heap->custom_blocks);
66 void *jsheap_alloc(jsheap_t *heap, DWORD size)
71 if(!heap->block_cnt) {
73 heap->blocks = heap_alloc(sizeof(void*));
78 tmp = heap_alloc(block_size(0));
82 heap->blocks[0] = tmp;
86 if(heap->offset + size <= block_size(heap->last_block)) {
87 tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
92 if(size <= block_size(heap->last_block+1)) {
93 if(heap->last_block+1 == heap->block_cnt) {
94 tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
99 heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
100 if(!heap->blocks[heap->block_cnt])
108 return heap->blocks[heap->last_block];
111 list = heap_alloc(size + sizeof(struct list));
115 list_add_head(&heap->custom_blocks, list);
119 void *jsheap_grow(jsheap_t *heap, void *mem, DWORD size, DWORD inc)
121 if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
122 && heap->offset+inc < block_size(heap->last_block)) {
127 return jsheap_alloc(heap, size+inc);
130 void jsheap_clear(jsheap_t *heap)
137 while((tmp = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
142 heap->last_block = heap->offset = 0;
146 void jsheap_free(jsheap_t *heap)
152 for(i=0; i < heap->block_cnt; i++)
153 heap_free(heap->blocks[i]);
154 heap_free(heap->blocks);
159 jsheap_t *jsheap_mark(jsheap_t *heap)
168 /* ECMA-262 3rd Edition 9.1 */
169 HRESULT to_primitive(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret)
181 V_BSTR(ret) = SysAllocString(V_BSTR(v));
184 return disp_propget(V_DISPATCH(v), DISPID_VALUE, ctx->lcid, ret, ei, NULL /*FIXME*/);
186 FIXME("Unimplemented for vt %d\n", V_VT(v));
193 /* ECMA-262 3rd Edition 9.2 */
194 HRESULT to_boolean(VARIANT *v, VARIANT_BOOL *b)
202 *b = V_I4(v) ? VARIANT_TRUE : VARIANT_FALSE;
205 *b = V_R8(v) ? VARIANT_TRUE : VARIANT_FALSE;
208 *b = V_BSTR(v) && *V_BSTR(v) ? VARIANT_TRUE : VARIANT_FALSE;
211 *b = V_DISPATCH(v) ? VARIANT_TRUE : VARIANT_FALSE;
217 FIXME("unimplemented for vt %d\n", V_VT(v));
224 static int hex_to_int(WCHAR c)
226 if('0' <= c && c <= '9')
229 if('a' <= c && c <= 'f')
232 if('A' <= c && c <= 'F')
238 /* ECMA-262 3rd Edition 9.3.1 */
239 static HRESULT str_to_number(BSTR str, VARIANT *ret)
241 const WCHAR *ptr = str;
245 static const WCHAR infinityW[] = {'I','n','f','i','n','i','t','y'};
247 while(isspaceW(*ptr))
253 }else if(*ptr == '+') {
257 if(!strncmpW(ptr, infinityW, sizeof(infinityW)/sizeof(WCHAR))) {
258 ptr += sizeof(infinityW)/sizeof(WCHAR);
259 while(*ptr && isspaceW(*ptr))
265 num_set_inf(ret, !neg);
269 if(*ptr == '0' && ptr[1] == 'x') {
273 while((l = hex_to_int(*ptr)) != -1) {
282 while(isdigitW(*ptr))
283 d = d*10 + (*ptr++ - '0');
285 if(*ptr == 'e' || *ptr == 'E') {
293 }else if(*ptr == '+') {
297 while(isdigitW(*ptr))
298 l = l*10 + (*ptr++ - '0');
303 }else if(*ptr == '.') {
307 while(isdigitW(*ptr)) {
308 d += dec * (*ptr++ - '0');
313 while(isspaceW(*ptr))
328 /* ECMA-262 3rd Edition 9.3 */
329 HRESULT to_number(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret)
344 return str_to_number(V_BSTR(v), ret);
349 hres = to_primitive(ctx, v, ei, &prim);
353 hres = to_number(ctx, &prim, ei, ret);
359 V_I4(ret) = V_BOOL(v) ? 1 : 0;
362 FIXME("unimplemented for vt %d\n", V_VT(v));
369 /* ECMA-262 3rd Edition 9.4 */
370 HRESULT to_integer(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret)
375 hres = to_number(ctx, v, ei, &num);
379 if(V_VT(&num) == VT_I4)
382 num_set_val(ret, V_R8(&num) >= 0.0 ? floor(V_R8(&num)) : -floor(-V_R8(&num)));
387 /* ECMA-262 3rd Edition 9.5 */
388 HRESULT to_int32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, INT *ret)
393 hres = to_number(ctx, v, ei, &num);
397 *ret = V_VT(&num) == VT_I4 ? V_I4(&num) : (INT)V_R8(&num);
401 /* ECMA-262 3rd Edition 9.6 */
402 HRESULT to_uint32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, DWORD *ret)
407 hres = to_number(ctx, v, ei, &num);
411 *ret = V_VT(&num) == VT_I4 ? V_I4(&num) : (DWORD)V_R8(&num);
415 static BSTR int_to_bstr(INT i)
421 static const WCHAR zeroW[] = {'0',0};
422 return SysAllocString(zeroW);
430 p = buf + sizeof(buf)/sizeof(*buf)-1;
442 return SysAllocString(p);
445 /* ECMA-262 3rd Edition 9.8 */
446 HRESULT to_string(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, BSTR *str)
448 const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
449 const WCHAR nullW[] = {'n','u','l','l',0};
450 const WCHAR trueW[] = {'t','r','u','e',0};
451 const WCHAR falseW[] = {'f','a','l','s','e',0};
455 *str = SysAllocString(undefinedW);
458 *str = SysAllocString(nullW);
461 *str = int_to_bstr(V_I4(v));
467 V_VT(&strv) = VT_EMPTY;
468 hres = VariantChangeTypeEx(&strv, v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
472 *str = V_BSTR(&strv);
476 *str = SysAllocString(V_BSTR(v));
482 hres = to_primitive(ctx, v, ei, &prim);
486 hres = to_string(ctx, &prim, ei, str);
491 *str = SysAllocString(V_BOOL(v) ? trueW : falseW);
494 FIXME("unsupported vt %d\n", V_VT(v));
498 return *str ? S_OK : E_OUTOFMEMORY;
501 /* ECMA-262 3rd Edition 9.9 */
502 HRESULT to_object(exec_ctx_t *ctx, VARIANT *v, IDispatch **disp)
509 hres = create_string(ctx->parser->script, V_BSTR(v), SysStringLen(V_BSTR(v)), &dispex);
513 *disp = (IDispatch*)_IDispatchEx_(dispex);
517 hres = create_number(ctx->parser->script, v, &dispex);
521 *disp = (IDispatch*)_IDispatchEx_(dispex);
524 IDispatch_AddRef(V_DISPATCH(v));
525 *disp = V_DISPATCH(v);
528 hres = create_bool(ctx->parser->script, V_BOOL(v), &dispex);
532 *disp = (IDispatch*)_IDispatchEx_(dispex);
535 FIXME("unsupported vt %d\n", V_VT(v));