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
)
43 return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v
));
45 return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v
));
47 return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v
)));
49 return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v
));
51 return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v
));
53 return wine_dbg_sprintf("{vt %d}", V_VT(v
));
57 #define MIN_BLOCK_SIZE 128
58 #define ARENA_FREE_FILLER 0xaa
60 static inline DWORD
block_size(DWORD block
)
62 return MIN_BLOCK_SIZE
<< block
;
65 void jsheap_init(jsheap_t
*heap
)
67 memset(heap
, 0, sizeof(*heap
));
68 list_init(&heap
->custom_blocks
);
71 void *jsheap_alloc(jsheap_t
*heap
, DWORD size
)
76 if(!heap
->block_cnt
) {
78 heap
->blocks
= heap_alloc(sizeof(void*));
83 tmp
= heap_alloc(block_size(0));
87 heap
->blocks
[0] = tmp
;
91 if(heap
->offset
+ size
<= block_size(heap
->last_block
)) {
92 tmp
= ((BYTE
*)heap
->blocks
[heap
->last_block
])+heap
->offset
;
97 if(size
<= block_size(heap
->last_block
+1)) {
98 if(heap
->last_block
+1 == heap
->block_cnt
) {
99 tmp
= heap_realloc(heap
->blocks
, (heap
->block_cnt
+1)*sizeof(void*));
104 heap
->blocks
[heap
->block_cnt
] = heap_alloc(block_size(heap
->block_cnt
));
105 if(!heap
->blocks
[heap
->block_cnt
])
113 return heap
->blocks
[heap
->last_block
];
116 list
= heap_alloc(size
+ sizeof(struct list
));
120 list_add_head(&heap
->custom_blocks
, list
);
124 void *jsheap_grow(jsheap_t
*heap
, void *mem
, DWORD size
, DWORD inc
)
126 if(mem
== (BYTE
*)heap
->blocks
[heap
->last_block
] + heap
->offset
-size
127 && heap
->offset
+inc
< block_size(heap
->last_block
)) {
132 return jsheap_alloc(heap
, size
+inc
);
135 void jsheap_clear(jsheap_t
*heap
)
142 while((tmp
= list_next(&heap
->custom_blocks
, &heap
->custom_blocks
))) {
150 for(i
=0; i
< heap
->block_cnt
; i
++)
151 memset(heap
->blocks
[i
], ARENA_FREE_FILLER
, block_size(i
));
154 heap
->last_block
= heap
->offset
= 0;
158 void jsheap_free(jsheap_t
*heap
)
164 for(i
=0; i
< heap
->block_cnt
; i
++)
165 heap_free(heap
->blocks
[i
]);
166 heap_free(heap
->blocks
);
171 jsheap_t
*jsheap_mark(jsheap_t
*heap
)
180 /* ECMA-262 3rd Edition 9.1 */
181 HRESULT
to_primitive(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, VARIANT
*ret
, hint_t hint
)
193 V_BSTR(ret
) = SysAllocString(V_BSTR(v
));
198 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
201 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
202 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
209 jsdisp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(v
));
211 V_VT(ret
) = VT_EMPTY
;
212 return disp_propget(ctx
, V_DISPATCH(v
), DISPID_VALUE
, ret
, ei
, NULL
/*FIXME*/);
216 hint
= is_class(jsdisp
, JSCLASS_DATE
) ? HINT_STRING
: HINT_NUMBER
;
218 /* Native implementation doesn't throw TypeErrors, returns strange values */
220 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? toStringW
: valueOfW
, 0, &id
);
221 if(SUCCEEDED(hres
)) {
222 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, &dp
, ret
, ei
, NULL
/*FIXME*/);
224 WARN("call error - forwarding exception\n");
225 jsdisp_release(jsdisp
);
228 else if(V_VT(ret
) != VT_DISPATCH
) {
229 jsdisp_release(jsdisp
);
233 IDispatch_Release(V_DISPATCH(ret
));
236 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? valueOfW
: toStringW
, 0, &id
);
237 if(SUCCEEDED(hres
)) {
238 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, &dp
, ret
, ei
, NULL
/*FIXME*/);
240 WARN("call error - forwarding exception\n");
241 jsdisp_release(jsdisp
);
244 else if(V_VT(ret
) != VT_DISPATCH
) {
245 jsdisp_release(jsdisp
);
249 IDispatch_Release(V_DISPATCH(ret
));
252 jsdisp_release(jsdisp
);
255 return throw_type_error(ctx
, ei
, IDS_TO_PRIMITIVE
, NULL
);
258 FIXME("Unimplemented for vt %d\n", V_VT(v
));
265 /* ECMA-262 3rd Edition 9.2 */
266 HRESULT
to_boolean(VARIANT
*v
, VARIANT_BOOL
*b
)
274 *b
= V_I4(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
277 if(isnan(V_R8(v
))) *b
= VARIANT_FALSE
;
278 else *b
= V_R8(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
281 *b
= V_BSTR(v
) && *V_BSTR(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
284 *b
= V_DISPATCH(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
290 FIXME("unimplemented for vt %d\n", V_VT(v
));
297 static int hex_to_int(WCHAR c
)
299 if('0' <= c
&& c
<= '9')
302 if('a' <= c
&& c
<= 'f')
305 if('A' <= c
&& c
<= 'F')
311 /* ECMA-262 3rd Edition 9.3.1 */
312 static HRESULT
str_to_number(BSTR str
, VARIANT
*ret
)
314 const WCHAR
*ptr
= str
;
318 static const WCHAR infinityW
[] = {'I','n','f','i','n','i','t','y'};
320 while(isspaceW(*ptr
))
326 }else if(*ptr
== '+') {
330 if(!strncmpW(ptr
, infinityW
, sizeof(infinityW
)/sizeof(WCHAR
))) {
331 ptr
+= sizeof(infinityW
)/sizeof(WCHAR
);
332 while(*ptr
&& isspaceW(*ptr
))
338 num_set_inf(ret
, !neg
);
342 if(*ptr
== '0' && ptr
[1] == 'x') {
346 while((l
= hex_to_int(*ptr
)) != -1) {
355 while(isdigitW(*ptr
))
356 d
= d
*10 + (*ptr
++ - '0');
358 if(*ptr
== 'e' || *ptr
== 'E') {
366 }else if(*ptr
== '+') {
370 while(isdigitW(*ptr
))
371 l
= l
*10 + (*ptr
++ - '0');
376 }else if(*ptr
== '.') {
380 while(isdigitW(*ptr
)) {
381 d
+= dec
* (*ptr
++ - '0');
386 while(isspaceW(*ptr
))
401 /* ECMA-262 3rd Edition 9.3 */
402 HRESULT
to_number(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, VARIANT
*ret
)
417 return str_to_number(V_BSTR(v
), ret
);
422 hres
= to_primitive(ctx
, v
, ei
, &prim
, HINT_NUMBER
);
426 hres
= to_number(ctx
, &prim
, ei
, ret
);
432 V_I4(ret
) = V_BOOL(v
) ? 1 : 0;
435 FIXME("unimplemented for vt %d\n", V_VT(v
));
442 /* ECMA-262 3rd Edition 9.4 */
443 HRESULT
to_integer(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, VARIANT
*ret
)
448 hres
= to_number(ctx
, v
, ei
, &num
);
452 if(V_VT(&num
) == VT_I4
) {
454 }else if(isnan(V_R8(&num
))) {
458 num_set_val(ret
, V_R8(&num
) >= 0.0 ? floor(V_R8(&num
)) : -floor(-V_R8(&num
)));
464 /* ECMA-262 3rd Edition 9.5 */
465 HRESULT
to_int32(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, INT
*ret
)
470 hres
= to_number(ctx
, v
, ei
, &num
);
474 if(V_VT(&num
) == VT_I4
)
477 *ret
= isnan(V_R8(&num
)) || isinf(V_R8(&num
)) ? 0 : (INT
)V_R8(&num
);
481 /* ECMA-262 3rd Edition 9.6 */
482 HRESULT
to_uint32(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, DWORD
*ret
)
487 hres
= to_number(ctx
, v
, ei
, &num
);
491 if(V_VT(&num
) == VT_I4
)
494 *ret
= isnan(V_R8(&num
)) || isinf(V_R8(&num
)) ? 0 : (DWORD
)V_R8(&num
);
498 static BSTR
int_to_bstr(INT i
)
504 static const WCHAR zeroW
[] = {'0',0};
505 return SysAllocString(zeroW
);
513 p
= buf
+ sizeof(buf
)/sizeof(*buf
)-1;
525 return SysAllocString(p
);
528 /* ECMA-262 3rd Edition 9.8 */
529 HRESULT
to_string(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, BSTR
*str
)
531 const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
532 const WCHAR nullW
[] = {'n','u','l','l',0};
533 const WCHAR trueW
[] = {'t','r','u','e',0};
534 const WCHAR falseW
[] = {'f','a','l','s','e',0};
535 const WCHAR NaNW
[] = {'N','a','N',0};
536 const WCHAR InfinityW
[] = {'-','I','n','f','i','n','i','t','y',0};
540 *str
= SysAllocString(undefinedW
);
543 *str
= SysAllocString(nullW
);
546 *str
= int_to_bstr(V_I4(v
));
550 *str
= SysAllocString(NaNW
);
551 else if(isinf(V_R8(v
)))
552 *str
= SysAllocString(V_R8(v
)<0 ? InfinityW
: InfinityW
+1);
557 V_VT(&strv
) = VT_EMPTY
;
558 hres
= VariantChangeTypeEx(&strv
, v
, MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
), 0, VT_BSTR
);
562 *str
= V_BSTR(&strv
);
568 *str
= SysAllocString(V_BSTR(v
));
574 hres
= to_primitive(ctx
, v
, ei
, &prim
, HINT_STRING
);
578 hres
= to_string(ctx
, &prim
, ei
, str
);
583 *str
= SysAllocString(V_BOOL(v
) ? trueW
: falseW
);
586 FIXME("unsupported vt %d\n", V_VT(v
));
590 return *str
? S_OK
: E_OUTOFMEMORY
;
593 /* ECMA-262 3rd Edition 9.9 */
594 HRESULT
to_object(script_ctx_t
*ctx
, VARIANT
*v
, IDispatch
**disp
)
601 hres
= create_string(ctx
, V_BSTR(v
), SysStringLen(V_BSTR(v
)), &dispex
);
605 *disp
= (IDispatch
*)_IDispatchEx_(dispex
);
609 hres
= create_number(ctx
, v
, &dispex
);
613 *disp
= (IDispatch
*)_IDispatchEx_(dispex
);
617 IDispatch_AddRef(V_DISPATCH(v
));
618 *disp
= V_DISPATCH(v
);
622 hres
= create_object(ctx
, NULL
, &obj
);
626 *disp
= (IDispatch
*)_IDispatchEx_(obj
);
630 hres
= create_bool(ctx
, V_BOOL(v
), &dispex
);
634 *disp
= (IDispatch
*)_IDispatchEx_(dispex
);
637 FIXME("unsupported vt %d\n", V_VT(v
));