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
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
26 const char *debugstr_variant(const VARIANT *v)
30 return wine_dbg_sprintf("{VT_EMPTY}");
32 return wine_dbg_sprintf("{VT_NULL}");
34 return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v));
36 return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v));
38 return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v)));
40 return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v));
42 return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v));
44 return wine_dbg_sprintf("{vt %d}", V_VT(v));
48 #define MIN_BLOCK_SIZE 128
50 static inline DWORD block_size(DWORD block)
52 return MIN_BLOCK_SIZE << block;
55 void jsheap_init(jsheap_t *heap)
57 memset(heap, 0, sizeof(*heap));
58 list_init(&heap->custom_blocks);
61 void *jsheap_alloc(jsheap_t *heap, DWORD size)
66 if(!heap->block_cnt) {
68 heap->blocks = heap_alloc(sizeof(void*));
73 tmp = heap_alloc(block_size(0));
77 heap->blocks[0] = tmp;
81 if(heap->offset + size < block_size(heap->last_block)) {
82 tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
87 if(size < block_size(heap->last_block+1)) {
88 if(heap->last_block+1 == heap->block_cnt) {
89 tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
95 tmp = heap_alloc(block_size(heap->block_cnt+1));
99 heap->blocks[heap->block_cnt++] = tmp;
103 return heap->blocks[heap->last_block];
106 list = heap_alloc(size + sizeof(struct list));
110 list_add_head(&heap->custom_blocks, list);
114 void *jsheap_grow(jsheap_t *heap, void *mem, DWORD size, DWORD inc)
116 if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
117 && heap->offset+inc < block_size(heap->last_block)) {
122 return jsheap_alloc(heap, size+inc);
125 void jsheap_clear(jsheap_t *heap)
132 while((tmp = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
137 heap->last_block = heap->offset = 0;
140 void jsheap_free(jsheap_t *heap)
146 for(i=0; i < heap->block_cnt; i++)
147 heap_free(heap->blocks[i]);
148 heap_free(heap->blocks);
153 jsheap_t *jsheap_mark(jsheap_t *heap)
162 /* ECMA-262 3rd Edition 9.1 */
163 HRESULT to_primitive(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret)
175 V_BSTR(ret) = SysAllocString(V_BSTR(v));
178 return disp_propget(V_DISPATCH(v), DISPID_VALUE, ctx->lcid, ret, ei, NULL /*FIXME*/);
180 FIXME("Unimplemented for vt %d\n", V_VT(v));
187 /* ECMA-262 3rd Edition 9.2 */
188 HRESULT to_boolean(VARIANT *v, VARIANT_BOOL *b)
196 *b = V_I4(v) ? VARIANT_TRUE : VARIANT_FALSE;
199 *b = V_R8(v) ? VARIANT_TRUE : VARIANT_FALSE;
202 *b = V_BSTR(v) && *V_BSTR(v) ? VARIANT_TRUE : VARIANT_FALSE;
205 *b = V_DISPATCH(v) ? VARIANT_TRUE : VARIANT_FALSE;
211 FIXME("unimplemented for vt %d\n", V_VT(v));
218 /* ECMA-262 3rd Edition 9.3 */
219 HRESULT to_number(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret)
232 V_I4(ret) = V_BOOL(v) ? 1 : 0;
235 FIXME("unimplemented for vt %d\n", V_VT(v));
242 /* ECMA-262 3rd Edition 9.5 */
243 HRESULT to_int32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, INT *ret)
248 hres = to_number(ctx, v, ei, &num);
252 *ret = V_VT(&num) == VT_I4 ? V_I4(&num) : (INT)V_R8(&num);
256 static BSTR int_to_bstr(INT i)
262 static const WCHAR zeroW[] = {'0',0};
263 return SysAllocString(zeroW);
271 p = buf + sizeof(buf)/sizeof(*buf)-1;
283 return SysAllocString(p);
286 /* ECMA-262 3rd Edition 9.8 */
287 HRESULT to_string(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, BSTR *str)
291 *str = int_to_bstr(V_I4(v));
295 *str = SysAllocString(V_BSTR(v));
299 FIXME("unsupported vt %d\n", V_VT(v));
303 return *str ? S_OK : E_OUTOFMEMORY;
306 /* ECMA-262 3rd Edition 9.9 */
307 HRESULT to_object(exec_ctx_t *ctx, VARIANT *v, IDispatch **disp)
314 hres = create_string(ctx->parser->script, V_BSTR(v), SysStringLen(V_BSTR(v)), &dispex);
318 *disp = (IDispatch*)_IDispatchEx_(dispex);
322 hres = create_number(ctx->parser->script, v, &dispex);
326 *disp = (IDispatch*)_IDispatchEx_(dispex);
329 IDispatch_AddRef(V_DISPATCH(v));
330 *disp = V_DISPATCH(v);
333 hres = create_bool(ctx->parser->script, V_BOOL(v), &dispex);
337 *disp = (IDispatch*)_IDispatchEx_(dispex);
340 FIXME("unsupported vt %d\n", V_VT(v));