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
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
28 const char *debugstr_variant(const VARIANT
*v
)
32 return wine_dbg_sprintf("{VT_EMPTY}");
34 return wine_dbg_sprintf("{VT_NULL}");
36 return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v
));
38 return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v
));
40 return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v
)));
42 return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v
));
44 return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v
));
46 return wine_dbg_sprintf("{vt %d}", V_VT(v
));
50 #define MIN_BLOCK_SIZE 128
52 static inline DWORD
block_size(DWORD block
)
54 return MIN_BLOCK_SIZE
<< block
;
57 void jsheap_init(jsheap_t
*heap
)
59 memset(heap
, 0, sizeof(*heap
));
60 list_init(&heap
->custom_blocks
);
63 void *jsheap_alloc(jsheap_t
*heap
, DWORD size
)
68 if(!heap
->block_cnt
) {
70 heap
->blocks
= heap_alloc(sizeof(void*));
75 tmp
= heap_alloc(block_size(0));
79 heap
->blocks
[0] = tmp
;
83 if(heap
->offset
+ size
< block_size(heap
->last_block
)) {
84 tmp
= ((BYTE
*)heap
->blocks
[heap
->last_block
])+heap
->offset
;
89 if(size
< block_size(heap
->last_block
+1)) {
90 if(heap
->last_block
+1 == heap
->block_cnt
) {
91 tmp
= heap_realloc(heap
->blocks
, (heap
->block_cnt
+1)*sizeof(void*));
97 tmp
= heap_alloc(block_size(heap
->block_cnt
+1));
101 heap
->blocks
[heap
->block_cnt
++] = tmp
;
105 return heap
->blocks
[heap
->last_block
];
108 list
= heap_alloc(size
+ sizeof(struct list
));
112 list_add_head(&heap
->custom_blocks
, list
);
116 void *jsheap_grow(jsheap_t
*heap
, void *mem
, DWORD size
, DWORD inc
)
118 if(mem
== (BYTE
*)heap
->blocks
[heap
->last_block
] + heap
->offset
-size
119 && heap
->offset
+inc
< block_size(heap
->last_block
)) {
124 return jsheap_alloc(heap
, size
+inc
);
127 void jsheap_clear(jsheap_t
*heap
)
134 while((tmp
= list_next(&heap
->custom_blocks
, &heap
->custom_blocks
))) {
139 heap
->last_block
= heap
->offset
= 0;
142 void jsheap_free(jsheap_t
*heap
)
148 for(i
=0; i
< heap
->block_cnt
; i
++)
149 heap_free(heap
->blocks
[i
]);
150 heap_free(heap
->blocks
);
155 jsheap_t
*jsheap_mark(jsheap_t
*heap
)
164 /* ECMA-262 3rd Edition 9.1 */
165 HRESULT
to_primitive(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, VARIANT
*ret
)
177 V_BSTR(ret
) = SysAllocString(V_BSTR(v
));
180 return disp_propget(V_DISPATCH(v
), DISPID_VALUE
, ctx
->lcid
, ret
, ei
, NULL
/*FIXME*/);
182 FIXME("Unimplemented for vt %d\n", V_VT(v
));
189 /* ECMA-262 3rd Edition 9.2 */
190 HRESULT
to_boolean(VARIANT
*v
, VARIANT_BOOL
*b
)
198 *b
= V_I4(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
201 *b
= V_R8(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
204 *b
= V_BSTR(v
) && *V_BSTR(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
207 *b
= V_DISPATCH(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
213 FIXME("unimplemented for vt %d\n", V_VT(v
));
220 /* ECMA-262 3rd Edition 9.3 */
221 HRESULT
to_number(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, VARIANT
*ret
)
234 V_I4(ret
) = V_BOOL(v
) ? 1 : 0;
237 FIXME("unimplemented for vt %d\n", V_VT(v
));
244 /* ECMA-262 3rd Edition 9.4 */
245 HRESULT
to_integer(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, VARIANT
*ret
)
250 hres
= to_number(ctx
, v
, ei
, &num
);
254 if(V_VT(&num
) == VT_I4
)
257 num_set_val(ret
, V_R8(&num
) >= 0.0 ? floor(V_R8(&num
)) : -floor(-V_R8(&num
)));
262 /* ECMA-262 3rd Edition 9.5 */
263 HRESULT
to_int32(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, INT
*ret
)
268 hres
= to_number(ctx
, v
, ei
, &num
);
272 *ret
= V_VT(&num
) == VT_I4
? V_I4(&num
) : (INT
)V_R8(&num
);
276 /* ECMA-262 3rd Edition 9.6 */
277 HRESULT
to_uint32(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, DWORD
*ret
)
282 hres
= to_number(ctx
, v
, ei
, &num
);
286 *ret
= V_VT(&num
) == VT_I4
? V_I4(&num
) : (DWORD
)V_R8(&num
);
290 static BSTR
int_to_bstr(INT i
)
296 static const WCHAR zeroW
[] = {'0',0};
297 return SysAllocString(zeroW
);
305 p
= buf
+ sizeof(buf
)/sizeof(*buf
)-1;
317 return SysAllocString(p
);
320 /* ECMA-262 3rd Edition 9.8 */
321 HRESULT
to_string(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, BSTR
*str
)
323 const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
324 const WCHAR nullW
[] = {'n','u','l','l',0};
325 const WCHAR trueW
[] = {'t','r','u','e',0};
326 const WCHAR falseW
[] = {'f','a','l','s','e',0};
330 *str
= SysAllocString(undefinedW
);
333 *str
= SysAllocString(nullW
);
336 *str
= int_to_bstr(V_I4(v
));
339 *str
= SysAllocString(V_BSTR(v
));
345 hres
= to_primitive(ctx
, v
, ei
, &prim
);
349 hres
= to_string(ctx
, &prim
, ei
, str
);
354 *str
= SysAllocString(V_BOOL(v
) ? trueW
: falseW
);
357 FIXME("unsupported vt %d\n", V_VT(v
));
361 return *str
? S_OK
: E_OUTOFMEMORY
;
364 /* ECMA-262 3rd Edition 9.9 */
365 HRESULT
to_object(exec_ctx_t
*ctx
, VARIANT
*v
, IDispatch
**disp
)
372 hres
= create_string(ctx
->parser
->script
, V_BSTR(v
), SysStringLen(V_BSTR(v
)), &dispex
);
376 *disp
= (IDispatch
*)_IDispatchEx_(dispex
);
380 hres
= create_number(ctx
->parser
->script
, v
, &dispex
);
384 *disp
= (IDispatch
*)_IDispatchEx_(dispex
);
387 IDispatch_AddRef(V_DISPATCH(v
));
388 *disp
= V_DISPATCH(v
);
391 hres
= create_bool(ctx
->parser
->script
, V_BOOL(v
), &dispex
);
395 *disp
= (IDispatch
*)_IDispatchEx_(dispex
);
398 FIXME("unsupported vt %d\n", V_VT(v
));