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_UI4: %u}", V_UI4(v
));
47 return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v
));
49 return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v
)));
51 return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v
));
53 return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v
));
55 return wine_dbg_sprintf("{vt %d}", V_VT(v
));
59 #define MIN_BLOCK_SIZE 128
60 #define ARENA_FREE_FILLER 0xaa
62 static inline DWORD
block_size(DWORD block
)
64 return MIN_BLOCK_SIZE
<< block
;
67 void jsheap_init(jsheap_t
*heap
)
69 memset(heap
, 0, sizeof(*heap
));
70 list_init(&heap
->custom_blocks
);
73 void *jsheap_alloc(jsheap_t
*heap
, DWORD size
)
78 if(!heap
->block_cnt
) {
80 heap
->blocks
= heap_alloc(sizeof(void*));
85 tmp
= heap_alloc(block_size(0));
89 heap
->blocks
[0] = tmp
;
93 if(heap
->offset
+ size
<= block_size(heap
->last_block
)) {
94 tmp
= ((BYTE
*)heap
->blocks
[heap
->last_block
])+heap
->offset
;
99 if(size
<= block_size(heap
->last_block
+1)) {
100 if(heap
->last_block
+1 == heap
->block_cnt
) {
101 tmp
= heap_realloc(heap
->blocks
, (heap
->block_cnt
+1)*sizeof(void*));
106 heap
->blocks
[heap
->block_cnt
] = heap_alloc(block_size(heap
->block_cnt
));
107 if(!heap
->blocks
[heap
->block_cnt
])
115 return heap
->blocks
[heap
->last_block
];
118 list
= heap_alloc(size
+ sizeof(struct list
));
122 list_add_head(&heap
->custom_blocks
, list
);
126 void *jsheap_grow(jsheap_t
*heap
, void *mem
, DWORD size
, DWORD inc
)
128 if(mem
== (BYTE
*)heap
->blocks
[heap
->last_block
] + heap
->offset
-size
129 && heap
->offset
+inc
< block_size(heap
->last_block
)) {
134 return jsheap_alloc(heap
, size
+inc
);
137 void jsheap_clear(jsheap_t
*heap
)
144 while((tmp
= list_next(&heap
->custom_blocks
, &heap
->custom_blocks
))) {
152 for(i
=0; i
< heap
->block_cnt
; i
++)
153 memset(heap
->blocks
[i
], ARENA_FREE_FILLER
, block_size(i
));
156 heap
->last_block
= heap
->offset
= 0;
160 void jsheap_free(jsheap_t
*heap
)
166 for(i
=0; i
< heap
->block_cnt
; i
++)
167 heap_free(heap
->blocks
[i
]);
168 heap_free(heap
->blocks
);
173 jsheap_t
*jsheap_mark(jsheap_t
*heap
)
182 /* ECMA-262 3rd Edition 9.1 */
183 HRESULT
to_primitive(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, VARIANT
*ret
, hint_t hint
)
195 V_BSTR(ret
) = SysAllocString(V_BSTR(v
));
200 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
203 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
204 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
211 jsdisp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(v
));
213 V_VT(ret
) = VT_EMPTY
;
214 return disp_propget(ctx
, V_DISPATCH(v
), DISPID_VALUE
, ret
, ei
, NULL
/*FIXME*/);
218 hint
= is_class(jsdisp
, JSCLASS_DATE
) ? HINT_STRING
: HINT_NUMBER
;
220 /* Native implementation doesn't throw TypeErrors, returns strange values */
222 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? toStringW
: valueOfW
, 0, &id
);
223 if(SUCCEEDED(hres
)) {
224 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, &dp
, ret
, ei
, NULL
/*FIXME*/);
226 WARN("call error - forwarding exception\n");
227 jsdisp_release(jsdisp
);
230 else if(V_VT(ret
) != VT_DISPATCH
) {
231 jsdisp_release(jsdisp
);
235 IDispatch_Release(V_DISPATCH(ret
));
238 hres
= jsdisp_get_id(jsdisp
, hint
== HINT_STRING
? valueOfW
: toStringW
, 0, &id
);
239 if(SUCCEEDED(hres
)) {
240 hres
= jsdisp_call(jsdisp
, id
, DISPATCH_METHOD
, &dp
, ret
, ei
, NULL
/*FIXME*/);
242 WARN("call error - forwarding exception\n");
243 jsdisp_release(jsdisp
);
246 else if(V_VT(ret
) != VT_DISPATCH
) {
247 jsdisp_release(jsdisp
);
251 IDispatch_Release(V_DISPATCH(ret
));
254 jsdisp_release(jsdisp
);
257 return throw_type_error(ctx
, ei
, JS_E_TO_PRIMITIVE
, NULL
);
260 FIXME("Unimplemented for vt %d\n", V_VT(v
));
267 /* ECMA-262 3rd Edition 9.2 */
268 HRESULT
to_boolean(VARIANT
*v
, VARIANT_BOOL
*b
)
276 *b
= V_I4(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
279 if(isnan(V_R8(v
))) *b
= VARIANT_FALSE
;
280 else *b
= V_R8(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
283 *b
= V_BSTR(v
) && *V_BSTR(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
286 *b
= V_DISPATCH(v
) ? VARIANT_TRUE
: VARIANT_FALSE
;
292 FIXME("unimplemented for vt %d\n", V_VT(v
));
299 static int hex_to_int(WCHAR c
)
301 if('0' <= c
&& c
<= '9')
304 if('a' <= c
&& c
<= 'f')
307 if('A' <= c
&& c
<= 'F')
313 /* ECMA-262 3rd Edition 9.3.1 */
314 static HRESULT
str_to_number(BSTR str
, VARIANT
*ret
)
316 const WCHAR
*ptr
= str
;
320 static const WCHAR infinityW
[] = {'I','n','f','i','n','i','t','y'};
322 while(isspaceW(*ptr
))
328 }else if(*ptr
== '+') {
332 if(!strncmpW(ptr
, infinityW
, sizeof(infinityW
)/sizeof(WCHAR
))) {
333 ptr
+= sizeof(infinityW
)/sizeof(WCHAR
);
334 while(*ptr
&& isspaceW(*ptr
))
340 num_set_inf(ret
, !neg
);
344 if(*ptr
== '0' && ptr
[1] == 'x') {
348 while((l
= hex_to_int(*ptr
)) != -1) {
357 while(isdigitW(*ptr
))
358 d
= d
*10 + (*ptr
++ - '0');
360 if(*ptr
== 'e' || *ptr
== 'E') {
368 }else if(*ptr
== '+') {
372 while(isdigitW(*ptr
))
373 l
= l
*10 + (*ptr
++ - '0');
378 }else if(*ptr
== '.') {
382 while(isdigitW(*ptr
)) {
383 d
+= dec
* (*ptr
++ - '0');
388 while(isspaceW(*ptr
))
403 /* ECMA-262 3rd Edition 9.3 */
404 HRESULT
to_number(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, VARIANT
*ret
)
419 return str_to_number(V_BSTR(v
), ret
);
424 hres
= to_primitive(ctx
, v
, ei
, &prim
, HINT_NUMBER
);
428 hres
= to_number(ctx
, &prim
, ei
, ret
);
434 V_I4(ret
) = V_BOOL(v
) ? 1 : 0;
437 FIXME("unimplemented for vt %d\n", V_VT(v
));
444 /* ECMA-262 3rd Edition 9.4 */
445 HRESULT
to_integer(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, VARIANT
*ret
)
450 hres
= to_number(ctx
, v
, ei
, &num
);
454 if(V_VT(&num
) == VT_I4
) {
456 }else if(isnan(V_R8(&num
))) {
460 num_set_val(ret
, V_R8(&num
) >= 0.0 ? floor(V_R8(&num
)) : -floor(-V_R8(&num
)));
466 /* ECMA-262 3rd Edition 9.5 */
467 HRESULT
to_int32(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, INT
*ret
)
472 hres
= to_number(ctx
, v
, ei
, &num
);
476 if(V_VT(&num
) == VT_I4
)
479 *ret
= isnan(V_R8(&num
)) || isinf(V_R8(&num
)) ? 0 : (INT
)V_R8(&num
);
483 /* ECMA-262 3rd Edition 9.6 */
484 HRESULT
to_uint32(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, DWORD
*ret
)
489 hres
= to_number(ctx
, v
, ei
, &num
);
493 if(V_VT(&num
) == VT_I4
)
496 *ret
= isnan(V_R8(&num
)) || isinf(V_R8(&num
)) ? 0 : (DWORD
)V_R8(&num
);
500 static BSTR
int_to_bstr(INT i
)
506 static const WCHAR zeroW
[] = {'0',0};
507 return SysAllocString(zeroW
);
515 p
= buf
+ sizeof(buf
)/sizeof(*buf
)-1;
527 return SysAllocString(p
);
530 /* ECMA-262 3rd Edition 9.8 */
531 HRESULT
to_string(script_ctx_t
*ctx
, VARIANT
*v
, jsexcept_t
*ei
, BSTR
*str
)
533 const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
534 const WCHAR nullW
[] = {'n','u','l','l',0};
535 const WCHAR trueW
[] = {'t','r','u','e',0};
536 const WCHAR falseW
[] = {'f','a','l','s','e',0};
537 const WCHAR NaNW
[] = {'N','a','N',0};
538 const WCHAR InfinityW
[] = {'-','I','n','f','i','n','i','t','y',0};
542 *str
= SysAllocString(undefinedW
);
545 *str
= SysAllocString(nullW
);
548 *str
= int_to_bstr(V_I4(v
));
552 *str
= SysAllocString(NaNW
);
553 else if(isinf(V_R8(v
)))
554 *str
= SysAllocString(V_R8(v
)<0 ? InfinityW
: InfinityW
+1);
559 V_VT(&strv
) = VT_EMPTY
;
560 hres
= VariantChangeTypeEx(&strv
, v
, MAKELCID(MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),SORT_DEFAULT
), 0, VT_BSTR
);
564 *str
= V_BSTR(&strv
);
570 *str
= SysAllocString(V_BSTR(v
));
576 hres
= to_primitive(ctx
, v
, ei
, &prim
, HINT_STRING
);
580 hres
= to_string(ctx
, &prim
, ei
, str
);
585 *str
= SysAllocString(V_BOOL(v
) ? trueW
: falseW
);
588 FIXME("unsupported vt %d\n", V_VT(v
));
592 return *str
? S_OK
: E_OUTOFMEMORY
;
595 /* ECMA-262 3rd Edition 9.9 */
596 HRESULT
to_object(script_ctx_t
*ctx
, VARIANT
*v
, IDispatch
**disp
)
603 hres
= create_string(ctx
, V_BSTR(v
), SysStringLen(V_BSTR(v
)), &dispex
);
607 *disp
= to_disp(dispex
);
611 hres
= create_number(ctx
, v
, &dispex
);
615 *disp
= to_disp(dispex
);
619 IDispatch_AddRef(V_DISPATCH(v
));
620 *disp
= V_DISPATCH(v
);
624 hres
= create_object(ctx
, NULL
, &obj
);
628 *disp
= to_disp(obj
);
632 hres
= create_bool(ctx
, V_BOOL(v
), &dispex
);
636 *disp
= to_disp(dispex
);
638 case VT_ARRAY
|VT_VARIANT
:
639 hres
= create_vbarray(ctx
, V_ARRAY(v
), &dispex
);
643 *disp
= to_disp(dispex
);
646 FIXME("unsupported vt %d\n", V_VT(v
));