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
);
30 WINE_DECLARE_DEBUG_CHANNEL(heap
);
32 const char *debugstr_variant(const VARIANT
*v
)
36 return wine_dbg_sprintf("{VT_EMPTY}");
38 return wine_dbg_sprintf("{VT_NULL}");
40 return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v
));
42 return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v
));
44 return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v
)));
46 return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v
));
48 return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v
));
50 return wine_dbg_sprintf("{vt %d}", V_VT(v
));
54 #define MIN_BLOCK_SIZE 128
55 #define ARENA_FREE_FILLER 0xaa
57 static inline DWORD
block_size(DWORD block
)
59 return MIN_BLOCK_SIZE
<< block
;
62 void jsheap_init(jsheap_t
*heap
)
64 memset(heap
, 0, sizeof(*heap
));
65 list_init(&heap
->custom_blocks
);
68 void *jsheap_alloc(jsheap_t
*heap
, DWORD size
)
73 if(!heap
->block_cnt
) {
75 heap
->blocks
= heap_alloc(sizeof(void*));
80 tmp
= heap_alloc(block_size(0));
84 heap
->blocks
[0] = tmp
;
88 if(heap
->offset
+ size
<= block_size(heap
->last_block
)) {
89 tmp
= ((BYTE
*)heap
->blocks
[heap
->last_block
])+heap
->offset
;
94 if(size
<= block_size(heap
->last_block
+1)) {
95 if(heap
->last_block
+1 == heap
->block_cnt
) {
96 tmp
= heap_realloc(heap
->blocks
, (heap
->block_cnt
+1)*sizeof(void*));
101 heap
->blocks
[heap
->block_cnt
] = heap_alloc(block_size(heap
->block_cnt
));
102 if(!heap
->blocks
[heap
->block_cnt
])
110 return heap
->blocks
[heap
->last_block
];
113 list
= heap_alloc(size
+ sizeof(struct list
));
117 list_add_head(&heap
->custom_blocks
, list
);
121 void *jsheap_grow(jsheap_t
*heap
, void *mem
, DWORD size
, DWORD inc
)
123 if(mem
== (BYTE
*)heap
->blocks
[heap
->last_block
] + heap
->offset
-size
124 && heap
->offset
+inc
< block_size(heap
->last_block
)) {
129 return jsheap_alloc(heap
, size
+inc
);
132 void jsheap_clear(jsheap_t
*heap
)
139 while((tmp
= list_next(&heap
->custom_blocks
, &heap
->custom_blocks
))) {
147 for(i
=0; i
< heap
->block_cnt
; i
++)
148 memset(heap
->blocks
[i
], ARENA_FREE_FILLER
, block_size(i
));
151 heap
->last_block
= heap
->offset
= 0;
155 void jsheap_free(jsheap_t
*heap
)
161 for(i
=0; i
< heap
->block_cnt
; i
++)
162 heap_free(heap
->blocks
[i
]);
163 heap_free(heap
->blocks
);
168 jsheap_t
*jsheap_mark(jsheap_t
*heap
)
177 /* ECMA-262 3rd Edition 9.1 */
178 HRESULT
to_primitive(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, VARIANT
*ret
)
190 V_BSTR(ret
) = SysAllocString(V_BSTR(v
));
193 return disp_propget(V_DISPATCH(v
), DISPID_VALUE
, ctx
->lcid
, ret
, ei
, NULL
/*FIXME*/);
195 FIXME("Unimplemented for vt %d\n", V_VT(v
));
202 /* ECMA-262 3rd Edition 9.2 */
203 HRESULT
to_boolean(VARIANT
*v
, VARIANT_BOOL
*b
)
211 *b
= V_I4(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
214 *b
= V_R8(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
217 *b
= V_BSTR(v
) && *V_BSTR(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
220 *b
= V_DISPATCH(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
226 FIXME("unimplemented for vt %d\n", V_VT(v
));
233 static int hex_to_int(WCHAR c
)
235 if('0' <= c
&& c
<= '9')
238 if('a' <= c
&& c
<= 'f')
241 if('A' <= c
&& c
<= 'F')
247 /* ECMA-262 3rd Edition 9.3.1 */
248 static HRESULT
str_to_number(BSTR str
, VARIANT
*ret
)
250 const WCHAR
*ptr
= str
;
254 static const WCHAR infinityW
[] = {'I','n','f','i','n','i','t','y'};
256 while(isspaceW(*ptr
))
262 }else if(*ptr
== '+') {
266 if(!strncmpW(ptr
, infinityW
, sizeof(infinityW
)/sizeof(WCHAR
))) {
267 ptr
+= sizeof(infinityW
)/sizeof(WCHAR
);
268 while(*ptr
&& isspaceW(*ptr
))
274 num_set_inf(ret
, !neg
);
278 if(*ptr
== '0' && ptr
[1] == 'x') {
282 while((l
= hex_to_int(*ptr
)) != -1) {
291 while(isdigitW(*ptr
))
292 d
= d
*10 + (*ptr
++ - '0');
294 if(*ptr
== 'e' || *ptr
== 'E') {
302 }else if(*ptr
== '+') {
306 while(isdigitW(*ptr
))
307 l
= l
*10 + (*ptr
++ - '0');
312 }else if(*ptr
== '.') {
316 while(isdigitW(*ptr
)) {
317 d
+= dec
* (*ptr
++ - '0');
322 while(isspaceW(*ptr
))
337 /* ECMA-262 3rd Edition 9.3 */
338 HRESULT
to_number(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, VARIANT
*ret
)
353 return str_to_number(V_BSTR(v
), ret
);
358 hres
= to_primitive(ctx
, v
, ei
, &prim
);
362 hres
= to_number(ctx
, &prim
, ei
, ret
);
368 V_I4(ret
) = V_BOOL(v
) ? 1 : 0;
371 FIXME("unimplemented for vt %d\n", V_VT(v
));
378 /* ECMA-262 3rd Edition 9.4 */
379 HRESULT
to_integer(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, VARIANT
*ret
)
384 hres
= to_number(ctx
, v
, ei
, &num
);
388 if(V_VT(&num
) == VT_I4
)
391 num_set_val(ret
, V_R8(&num
) >= 0.0 ? floor(V_R8(&num
)) : -floor(-V_R8(&num
)));
396 /* ECMA-262 3rd Edition 9.5 */
397 HRESULT
to_int32(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, INT
*ret
)
402 hres
= to_number(ctx
, v
, ei
, &num
);
406 *ret
= V_VT(&num
) == VT_I4
? V_I4(&num
) : (INT
)V_R8(&num
);
410 /* ECMA-262 3rd Edition 9.6 */
411 HRESULT
to_uint32(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, DWORD
*ret
)
416 hres
= to_number(ctx
, v
, ei
, &num
);
420 *ret
= V_VT(&num
) == VT_I4
? V_I4(&num
) : (DWORD
)V_R8(&num
);
424 static BSTR
int_to_bstr(INT i
)
430 static const WCHAR zeroW
[] = {'0',0};
431 return SysAllocString(zeroW
);
439 p
= buf
+ sizeof(buf
)/sizeof(*buf
)-1;
451 return SysAllocString(p
);
454 /* ECMA-262 3rd Edition 9.8 */
455 HRESULT
to_string(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, BSTR
*str
)
457 const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
458 const WCHAR nullW
[] = {'n','u','l','l',0};
459 const WCHAR trueW
[] = {'t','r','u','e',0};
460 const WCHAR falseW
[] = {'f','a','l','s','e',0};
464 *str
= SysAllocString(undefinedW
);
467 *str
= SysAllocString(nullW
);
470 *str
= int_to_bstr(V_I4(v
));
476 V_VT(&strv
) = VT_EMPTY
;
477 hres
= VariantChangeTypeEx(&strv
, v
, MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
), 0, VT_BSTR
);
481 *str
= V_BSTR(&strv
);
485 *str
= SysAllocString(V_BSTR(v
));
491 hres
= to_primitive(ctx
, v
, ei
, &prim
);
495 hres
= to_string(ctx
, &prim
, ei
, str
);
500 *str
= SysAllocString(V_BOOL(v
) ? trueW
: falseW
);
503 FIXME("unsupported vt %d\n", V_VT(v
));
507 return *str
? S_OK
: E_OUTOFMEMORY
;
510 /* ECMA-262 3rd Edition 9.9 */
511 HRESULT
to_object(exec_ctx_t
*ctx
, VARIANT
*v
, IDispatch
**disp
)
518 hres
= create_string(ctx
->parser
->script
, V_BSTR(v
), SysStringLen(V_BSTR(v
)), &dispex
);
522 *disp
= (IDispatch
*)_IDispatchEx_(dispex
);
526 hres
= create_number(ctx
->parser
->script
, v
, &dispex
);
530 *disp
= (IDispatch
*)_IDispatchEx_(dispex
);
533 IDispatch_AddRef(V_DISPATCH(v
));
534 *disp
= V_DISPATCH(v
);
537 hres
= create_bool(ctx
->parser
->script
, V_BOOL(v
), &dispex
);
541 *disp
= (IDispatch
*)_IDispatchEx_(dispex
);
544 FIXME("unsupported vt %d\n", V_VT(v
));