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
21 #include "wine/debug.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
25 const char *debugstr_variant(const VARIANT *v)
29 return wine_dbg_sprintf("{VT_EMPTY}");
31 return wine_dbg_sprintf("{VT_NULL}");
33 return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v));
35 return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v));
37 return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v)));
39 return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v));
41 return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v));
43 return wine_dbg_sprintf("{vt %d}", V_VT(v));
47 #define MIN_BLOCK_SIZE 128
49 static inline DWORD block_size(DWORD block)
51 return MIN_BLOCK_SIZE << block;
54 void jsheap_init(jsheap_t *heap)
56 memset(heap, 0, sizeof(*heap));
57 list_init(&heap->custom_blocks);
60 void *jsheap_alloc(jsheap_t *heap, DWORD size)
65 if(!heap->block_cnt) {
67 heap->blocks = heap_alloc(sizeof(void*));
72 tmp = heap_alloc(block_size(0));
76 heap->blocks[0] = tmp;
80 if(heap->offset + size < block_size(heap->last_block)) {
81 tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
86 if(size < block_size(heap->last_block+1)) {
87 if(heap->last_block+1 == heap->block_cnt) {
88 tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
94 tmp = heap_alloc(block_size(heap->block_cnt+1));
98 heap->blocks[heap->block_cnt++] = tmp;
102 return heap->blocks[heap->last_block];
105 list = heap_alloc(size + sizeof(struct list));
109 list_add_head(&heap->custom_blocks, list);
113 void *jsheap_grow(jsheap_t *heap, void *mem, DWORD size, DWORD inc)
115 if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
116 && heap->offset+inc < block_size(heap->last_block)) {
121 return jsheap_alloc(heap, size+inc);
124 void jsheap_clear(jsheap_t *heap)
131 while((tmp = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
136 heap->last_block = heap->offset = 0;
139 void jsheap_free(jsheap_t *heap)
145 for(i=0; i < heap->block_cnt; i++)
146 heap_free(heap->blocks[i]);
147 heap_free(heap->blocks);
152 jsheap_t *jsheap_mark(jsheap_t *heap)
161 /* ECMA-262 3rd Edition 9.1 */
162 HRESULT to_primitive(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret)
174 V_BSTR(ret) = SysAllocString(V_BSTR(v));
177 return disp_propget(V_DISPATCH(v), DISPID_VALUE, ctx->lcid, ret, ei, NULL /*FIXME*/);
179 FIXME("Unimplemented for vt %d\n", V_VT(v));
186 /* ECMA-262 3rd Edition 9.2 */
187 HRESULT to_boolean(VARIANT *v, VARIANT_BOOL *b)
195 *b = V_I4(v) ? VARIANT_TRUE : VARIANT_FALSE;
198 *b = V_R8(v) ? VARIANT_TRUE : VARIANT_FALSE;
201 *b = V_BSTR(v) && *V_BSTR(v) ? VARIANT_TRUE : VARIANT_FALSE;
204 *b = V_DISPATCH(v) ? VARIANT_TRUE : VARIANT_FALSE;
210 FIXME("unimplemented for vt %d\n", V_VT(v));
217 /* ECMA-262 3rd Edition 9.3 */
218 HRESULT to_number(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret)
226 FIXME("unimplemented for vt %d\n", V_VT(v));
233 /* ECMA-262 3rd Edition 9.8 */
234 HRESULT to_string(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, BSTR *str)
238 *str = SysAllocString(V_BSTR(v));
242 FIXME("unsupported vt %d\n", V_VT(v));
248 /* ECMA-262 3rd Edition 9.9 */
249 HRESULT to_object(exec_ctx_t *ctx, VARIANT *v, IDispatch **disp)
253 IDispatch_AddRef(V_DISPATCH(v));
254 *disp = V_DISPATCH(v);
257 FIXME("unsupported vt %d\n", V_VT(v));