ws2_32: Add a fixme for IPPROTO_IPV6.
[wine/multimedia.git] / dlls / jscript / jsutils.c
bloba93fb652d71265f293bf0ccdbc079f3fb562203b
1 /*
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <math.h>
24 #include "jscript.h"
25 #include "engine.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)
34 switch(V_VT(v)) {
35 case VT_EMPTY:
36 return wine_dbg_sprintf("{VT_EMPTY}");
37 case VT_NULL:
38 return wine_dbg_sprintf("{VT_NULL}");
39 case VT_I4:
40 return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v));
41 case VT_R8:
42 return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v));
43 case VT_BSTR:
44 return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v)));
45 case VT_DISPATCH:
46 return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v));
47 case VT_BOOL:
48 return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v));
49 default:
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)
70 struct list *list;
71 void *tmp;
73 if(!heap->block_cnt) {
74 if(!heap->blocks) {
75 heap->blocks = heap_alloc(sizeof(void*));
76 if(!heap->blocks)
77 return NULL;
80 tmp = heap_alloc(block_size(0));
81 if(!tmp)
82 return NULL;
84 heap->blocks[0] = tmp;
85 heap->block_cnt = 1;
88 if(heap->offset + size <= block_size(heap->last_block)) {
89 tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
90 heap->offset += size;
91 return tmp;
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*));
97 if(!tmp)
98 return NULL;
100 heap->blocks = tmp;
101 heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
102 if(!heap->blocks[heap->block_cnt])
103 return NULL;
105 heap->block_cnt++;
108 heap->last_block++;
109 heap->offset = size;
110 return heap->blocks[heap->last_block];
113 list = heap_alloc(size + sizeof(struct list));
114 if(!list)
115 return NULL;
117 list_add_head(&heap->custom_blocks, list);
118 return list+1;
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)) {
125 heap->offset += inc;
126 return mem;
129 return jsheap_alloc(heap, size+inc);
132 void jsheap_clear(jsheap_t *heap)
134 struct list *tmp;
136 if(!heap)
137 return;
139 while((tmp = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
140 list_remove(tmp);
141 heap_free(tmp);
144 if(WARN_ON(heap)) {
145 DWORD i;
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;
152 heap->mark = FALSE;
155 void jsheap_free(jsheap_t *heap)
157 DWORD i;
159 jsheap_clear(heap);
161 for(i=0; i < heap->block_cnt; i++)
162 heap_free(heap->blocks[i]);
163 heap_free(heap->blocks);
165 jsheap_init(heap);
168 jsheap_t *jsheap_mark(jsheap_t *heap)
170 if(heap->mark)
171 return NULL;
173 heap->mark = TRUE;
174 return heap;
177 /* ECMA-262 3rd Edition 9.1 */
178 HRESULT to_primitive(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret, hint_t hint)
180 switch(V_VT(v)) {
181 case VT_EMPTY:
182 case VT_NULL:
183 case VT_BOOL:
184 case VT_I4:
185 case VT_R8:
186 *ret = *v;
187 break;
188 case VT_BSTR:
189 V_VT(ret) = VT_BSTR;
190 V_BSTR(ret) = SysAllocString(V_BSTR(v));
191 break;
192 case VT_DISPATCH: {
193 DispatchEx *jsdisp;
194 DISPID id;
195 DISPPARAMS dp = {NULL, NULL, 0, 0};
196 HRESULT hres;
198 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
199 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
201 jsdisp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
202 if(!jsdisp)
203 return disp_propget(V_DISPATCH(v), DISPID_VALUE, ctx->lcid, ret, ei, NULL /*FIXME*/);
205 if(hint == NO_HINT)
206 hint = is_class(jsdisp, JSCLASS_DATE) ? HINT_STRING : HINT_NUMBER;
208 /* Native implementation doesn't throw TypeErrors, returns strange values */
210 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? toStringW : valueOfW, 0, &id);
211 if(SUCCEEDED(hres)) {
212 hres = jsdisp_call(jsdisp, id, ctx->lcid, DISPATCH_METHOD, &dp, ret, ei, NULL /*FIXME*/);
213 if(FAILED(hres)) {
214 WARN("call error - forwarding exception\n");
215 jsdisp_release(jsdisp);
216 return hres;
218 else if(V_VT(ret) != VT_DISPATCH) {
219 jsdisp_release(jsdisp);
220 return S_OK;
222 else
223 IDispatch_Release(V_DISPATCH(ret));
226 hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? valueOfW : toStringW, 0, &id);
227 if(SUCCEEDED(hres)) {
228 hres = jsdisp_call(jsdisp, id, ctx->lcid, DISPATCH_METHOD, &dp, ret, ei, NULL /*FIXME*/);
229 if(FAILED(hres)) {
230 WARN("call error - forwarding exception\n");
231 jsdisp_release(jsdisp);
232 return hres;
234 else if(V_VT(ret) != VT_DISPATCH) {
235 jsdisp_release(jsdisp);
236 return S_OK;
238 else
239 IDispatch_Release(V_DISPATCH(ret));
242 jsdisp_release(jsdisp);
244 WARN("failed\n");
245 return throw_type_error(ctx, ei, IDS_TO_PRIMITIVE, NULL);
247 default:
248 FIXME("Unimplemented for vt %d\n", V_VT(v));
249 return E_NOTIMPL;
252 return S_OK;
255 /* ECMA-262 3rd Edition 9.2 */
256 HRESULT to_boolean(VARIANT *v, VARIANT_BOOL *b)
258 switch(V_VT(v)) {
259 case VT_EMPTY:
260 case VT_NULL:
261 *b = VARIANT_FALSE;
262 break;
263 case VT_I4:
264 *b = V_I4(v) ? VARIANT_TRUE : VARIANT_FALSE;
265 break;
266 case VT_R8:
267 if(isnan(V_R8(v))) *b = VARIANT_FALSE;
268 else *b = V_R8(v) ? VARIANT_TRUE : VARIANT_FALSE;
269 break;
270 case VT_BSTR:
271 *b = V_BSTR(v) && *V_BSTR(v) ? VARIANT_TRUE : VARIANT_FALSE;
272 break;
273 case VT_DISPATCH:
274 *b = V_DISPATCH(v) ? VARIANT_TRUE : VARIANT_FALSE;
275 break;
276 case VT_BOOL:
277 *b = V_BOOL(v);
278 break;
279 default:
280 FIXME("unimplemented for vt %d\n", V_VT(v));
281 return E_NOTIMPL;
284 return S_OK;
287 static int hex_to_int(WCHAR c)
289 if('0' <= c && c <= '9')
290 return c-'0';
292 if('a' <= c && c <= 'f')
293 return c-'a'+10;
295 if('A' <= c && c <= 'F')
296 return c-'A'+10;
298 return -1;
301 /* ECMA-262 3rd Edition 9.3.1 */
302 static HRESULT str_to_number(BSTR str, VARIANT *ret)
304 const WCHAR *ptr = str;
305 BOOL neg = FALSE;
306 DOUBLE d = 0.0;
308 static const WCHAR infinityW[] = {'I','n','f','i','n','i','t','y'};
310 while(isspaceW(*ptr))
311 ptr++;
313 if(*ptr == '-') {
314 neg = TRUE;
315 ptr++;
316 }else if(*ptr == '+') {
317 ptr++;
320 if(!strncmpW(ptr, infinityW, sizeof(infinityW)/sizeof(WCHAR))) {
321 ptr += sizeof(infinityW)/sizeof(WCHAR);
322 while(*ptr && isspaceW(*ptr))
323 ptr++;
325 if(*ptr)
326 num_set_nan(ret);
327 else
328 num_set_inf(ret, !neg);
329 return S_OK;
332 if(*ptr == '0' && ptr[1] == 'x') {
333 DWORD l = 0;
335 ptr += 2;
336 while((l = hex_to_int(*ptr)) != -1) {
337 d = d*16 + l;
338 ptr++;
341 num_set_val(ret, d);
342 return S_OK;
345 while(isdigitW(*ptr))
346 d = d*10 + (*ptr++ - '0');
348 if(*ptr == 'e' || *ptr == 'E') {
349 BOOL eneg = FALSE;
350 LONG l = 0;
352 ptr++;
353 if(*ptr == '-') {
354 ptr++;
355 eneg = TRUE;
356 }else if(*ptr == '+') {
357 ptr++;
360 while(isdigitW(*ptr))
361 l = l*10 + (*ptr++ - '0');
362 if(eneg)
363 l = -l;
365 d *= pow(10, l);
366 }else if(*ptr == '.') {
367 DOUBLE dec = 0.1;
369 ptr++;
370 while(isdigitW(*ptr)) {
371 d += dec * (*ptr++ - '0');
372 dec *= 0.1;
376 while(isspaceW(*ptr))
377 ptr++;
379 if(*ptr) {
380 num_set_nan(ret);
381 return S_OK;
384 if(neg)
385 d = -d;
387 num_set_val(ret, d);
388 return S_OK;
391 /* ECMA-262 3rd Edition 9.3 */
392 HRESULT to_number(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret)
394 switch(V_VT(v)) {
395 case VT_EMPTY:
396 num_set_nan(ret);
397 break;
398 case VT_NULL:
399 V_VT(ret) = VT_I4;
400 V_I4(ret) = 0;
401 break;
402 case VT_I4:
403 case VT_R8:
404 *ret = *v;
405 break;
406 case VT_BSTR:
407 return str_to_number(V_BSTR(v), ret);
408 case VT_DISPATCH: {
409 VARIANT prim;
410 HRESULT hres;
412 hres = to_primitive(ctx, v, ei, &prim, HINT_NUMBER);
413 if(FAILED(hres))
414 return hres;
416 hres = to_number(ctx, &prim, ei, ret);
417 VariantClear(&prim);
418 return hres;
420 case VT_BOOL:
421 V_VT(ret) = VT_I4;
422 V_I4(ret) = V_BOOL(v) ? 1 : 0;
423 break;
424 default:
425 FIXME("unimplemented for vt %d\n", V_VT(v));
426 return E_NOTIMPL;
429 return S_OK;
432 /* ECMA-262 3rd Edition 9.4 */
433 HRESULT to_integer(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, VARIANT *ret)
435 VARIANT num;
436 HRESULT hres;
438 hres = to_number(ctx, v, ei, &num);
439 if(FAILED(hres))
440 return hres;
442 if(V_VT(&num) == VT_I4)
443 *ret = num;
444 else
445 num_set_val(ret, V_R8(&num) >= 0.0 ? floor(V_R8(&num)) : -floor(-V_R8(&num)));
447 return S_OK;
450 /* ECMA-262 3rd Edition 9.5 */
451 HRESULT to_int32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, INT *ret)
453 VARIANT num;
454 HRESULT hres;
456 hres = to_number(ctx, v, ei, &num);
457 if(FAILED(hres))
458 return hres;
460 *ret = V_VT(&num) == VT_I4 ? V_I4(&num) : (INT)V_R8(&num);
461 return S_OK;
464 /* ECMA-262 3rd Edition 9.6 */
465 HRESULT to_uint32(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, DWORD *ret)
467 VARIANT num;
468 HRESULT hres;
470 hres = to_number(ctx, v, ei, &num);
471 if(FAILED(hres))
472 return hres;
474 *ret = V_VT(&num) == VT_I4 ? V_I4(&num) : (DWORD)V_R8(&num);
475 return S_OK;
478 static BSTR int_to_bstr(INT i)
480 WCHAR buf[12], *p;
481 BOOL neg = FALSE;
483 if(!i) {
484 static const WCHAR zeroW[] = {'0',0};
485 return SysAllocString(zeroW);
488 if(i < 0) {
489 neg = TRUE;
490 i = -i;
493 p = buf + sizeof(buf)/sizeof(*buf)-1;
494 *p-- = 0;
495 while(i) {
496 *p-- = i%10 + '0';
497 i /= 10;
500 if(neg)
501 *p = '-';
502 else
503 p++;
505 return SysAllocString(p);
508 /* ECMA-262 3rd Edition 9.8 */
509 HRESULT to_string(script_ctx_t *ctx, VARIANT *v, jsexcept_t *ei, BSTR *str)
511 const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
512 const WCHAR nullW[] = {'n','u','l','l',0};
513 const WCHAR trueW[] = {'t','r','u','e',0};
514 const WCHAR falseW[] = {'f','a','l','s','e',0};
515 const WCHAR NaNW[] = {'N','a','N',0};
516 const WCHAR InfinityW[] = {'-','I','n','f','i','n','i','t','y',0};
518 switch(V_VT(v)) {
519 case VT_EMPTY:
520 *str = SysAllocString(undefinedW);
521 break;
522 case VT_NULL:
523 *str = SysAllocString(nullW);
524 break;
525 case VT_I4:
526 *str = int_to_bstr(V_I4(v));
527 break;
528 case VT_R8: {
529 if(isnan(V_R8(v)))
530 *str = SysAllocString(NaNW);
531 else if(isinf(V_R8(v)))
532 *str = SysAllocString(V_R8(v)<0 ? InfinityW : InfinityW+1);
533 else {
534 VARIANT strv;
535 HRESULT hres;
537 V_VT(&strv) = VT_EMPTY;
538 hres = VariantChangeTypeEx(&strv, v, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
539 if(FAILED(hres))
540 return hres;
542 *str = V_BSTR(&strv);
543 return S_OK;
545 break;
547 case VT_BSTR:
548 *str = SysAllocString(V_BSTR(v));
549 break;
550 case VT_DISPATCH: {
551 VARIANT prim;
552 HRESULT hres;
554 hres = to_primitive(ctx, v, ei, &prim, HINT_STRING);
555 if(FAILED(hres))
556 return hres;
558 hres = to_string(ctx, &prim, ei, str);
559 VariantClear(&prim);
560 return hres;
562 case VT_BOOL:
563 *str = SysAllocString(V_BOOL(v) ? trueW : falseW);
564 break;
565 default:
566 FIXME("unsupported vt %d\n", V_VT(v));
567 return E_NOTIMPL;
570 return *str ? S_OK : E_OUTOFMEMORY;
573 /* ECMA-262 3rd Edition 9.9 */
574 HRESULT to_object(script_ctx_t *ctx, VARIANT *v, IDispatch **disp)
576 DispatchEx *dispex;
577 HRESULT hres;
579 switch(V_VT(v)) {
580 case VT_BSTR:
581 hres = create_string(ctx, V_BSTR(v), SysStringLen(V_BSTR(v)), &dispex);
582 if(FAILED(hres))
583 return hres;
585 *disp = (IDispatch*)_IDispatchEx_(dispex);
586 break;
587 case VT_I4:
588 case VT_R8:
589 hres = create_number(ctx, v, &dispex);
590 if(FAILED(hres))
591 return hres;
593 *disp = (IDispatch*)_IDispatchEx_(dispex);
594 break;
595 case VT_DISPATCH:
596 IDispatch_AddRef(V_DISPATCH(v));
597 *disp = V_DISPATCH(v);
598 break;
599 case VT_BOOL:
600 hres = create_bool(ctx, V_BOOL(v), &dispex);
601 if(FAILED(hres))
602 return hres;
604 *disp = (IDispatch*)_IDispatchEx_(dispex);
605 break;
606 default:
607 FIXME("unsupported vt %d\n", V_VT(v));
608 return E_NOTIMPL;
611 return S_OK;