jscript: Added Array constructor implementation.
[wine.git] / dlls / jscript / engine.c
blob2a1bf15580acaf19ed482f4f685ae2e74f2f421f
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 "jscript.h"
20 #include "engine.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
26 #define EXPR_NOVAL 0x0001
27 #define EXPR_NEWREF 0x0002
28 #define EXPR_STRREF 0x0004
30 static inline HRESULT stat_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
32 return stat->eval(ctx, stat, rt, ret);
35 static inline HRESULT expr_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
37 return _expr->eval(ctx, _expr, flags, ei, ret);
40 static void exprval_release(exprval_t *val)
42 switch(val->type) {
43 case EXPRVAL_VARIANT:
44 VariantClear(&val->u.var);
45 return;
46 case EXPRVAL_IDREF:
47 if(val->u.idref.disp)
48 IDispatch_Release(val->u.idref.disp);
49 return;
50 case EXPRVAL_NAMEREF:
51 if(val->u.nameref.disp)
52 IDispatch_Release(val->u.nameref.disp);
53 SysFreeString(val->u.nameref.name);
57 /* ECMA-262 3rd Edition 8.7.1 */
58 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
60 V_VT(ret) = VT_EMPTY;
62 switch(val->type) {
63 case EXPRVAL_VARIANT:
64 return VariantCopy(ret, &val->u.var);
65 case EXPRVAL_IDREF:
66 if(!val->u.idref.disp) {
67 FIXME("throw ReferenceError\n");
68 return E_FAIL;
71 return disp_propget(val->u.idref.disp, val->u.idref.id, ctx->lcid, ret, ei, NULL/*FIXME*/);
72 default:
73 ERR("type %d\n", val->type);
74 return E_FAIL;
78 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
80 if(val->type == EXPRVAL_VARIANT) {
81 *ret = val->u.var;
82 V_VT(&val->u.var) = VT_EMPTY;
83 return S_OK;
86 return exprval_value(ctx, val, ei, ret);
89 static HRESULT exprval_to_boolean(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, VARIANT_BOOL *b)
91 if(exprval->type != EXPRVAL_VARIANT) {
92 VARIANT val;
93 HRESULT hres;
95 hres = exprval_to_value(ctx, exprval, ei, &val);
96 if(FAILED(hres))
97 return hres;
99 hres = to_boolean(&val, b);
100 VariantClear(&val);
101 return hres;
104 return to_boolean(&exprval->u.var, b);
107 static void exprval_init(exprval_t *val)
109 val->type = EXPRVAL_VARIANT;
110 V_VT(&val->u.var) = VT_EMPTY;
113 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
115 val->type = EXPRVAL_IDREF;
116 val->u.idref.disp = disp;
117 val->u.idref.id = id;
119 if(disp)
120 IDispatch_AddRef(disp);
123 HRESULT scope_push(scope_chain_t *scope, DispatchEx *obj, scope_chain_t **ret)
125 scope_chain_t *new_scope;
127 new_scope = heap_alloc(sizeof(scope_chain_t));
128 if(!new_scope)
129 return E_OUTOFMEMORY;
131 new_scope->ref = 1;
133 IDispatchEx_AddRef(_IDispatchEx_(obj));
134 new_scope->obj = obj;
136 if(scope) {
137 scope_addref(scope);
138 new_scope->next = scope;
139 }else {
140 new_scope->next = NULL;
143 *ret = new_scope;
144 return S_OK;
147 void scope_release(scope_chain_t *scope)
149 if(--scope->ref)
150 return;
152 if(scope->next)
153 scope_release(scope->next);
155 IDispatchEx_Release(_IDispatchEx_(scope->obj));
156 heap_free(scope);
159 HRESULT create_exec_ctx(IDispatch *this_obj, DispatchEx *var_disp, scope_chain_t *scope, exec_ctx_t **ret)
161 exec_ctx_t *ctx;
163 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
164 if(!ctx)
165 return E_OUTOFMEMORY;
167 IDispatch_AddRef(this_obj);
168 ctx->this_obj = this_obj;
170 IDispatchEx_AddRef(_IDispatchEx_(var_disp));
171 ctx->var_disp = var_disp;
173 if(scope) {
174 scope_addref(scope);
175 ctx->scope_chain = scope;
178 *ret = ctx;
179 return S_OK;
182 void exec_release(exec_ctx_t *ctx)
184 if(--ctx->ref)
185 return;
187 if(ctx->scope_chain)
188 scope_release(ctx->scope_chain);
189 if(ctx->var_disp)
190 IDispatchEx_Release(_IDispatchEx_(ctx->var_disp));
191 if(ctx->this_obj)
192 IDispatch_Release(ctx->this_obj);
193 heap_free(ctx);
196 static HRESULT dispex_get_id(IDispatchEx *dispex, BSTR name, DWORD flags, DISPID *id)
198 *id = 0;
200 return IDispatchEx_GetDispID(dispex, name, flags|fdexNameCaseSensitive, id);
203 static HRESULT disp_get_id(IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
205 IDispatchEx *dispex;
206 HRESULT hres;
208 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
209 if(FAILED(hres)) {
210 TRACE("unsing IDispatch\n");
212 *id = 0;
213 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
216 hres = dispex_get_id(dispex, name, flags, id);
217 IDispatchEx_Release(dispex);
218 return hres;
221 /* ECMA-262 3rd Edition 8.7.2 */
222 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
224 if(ref->type != EXPRVAL_IDREF) {
225 FIXME("throw ReferemceError\n");
226 return E_FAIL;
229 return disp_propput(ref->u.idref.disp, ref->u.idref.id, ctx->lcid, v, ei, NULL/*FIXME*/);
232 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
234 IObjectIdentity *identity;
235 IUnknown *unk1, *unk2;
236 HRESULT hres;
238 if(disp1 == disp2) {
239 *ret = TRUE;
240 return S_OK;
243 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
244 if(FAILED(hres))
245 return hres;
247 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
248 if(FAILED(hres)) {
249 IUnknown_Release(unk1);
250 return hres;
253 if(unk1 == unk2) {
254 *ret = TRUE;
255 }else {
256 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
257 if(SUCCEEDED(hres)) {
258 hres = IObjectIdentity_IsEqualObject(identity, unk2);
259 IObjectIdentity_Release(identity);
260 *ret = hres == S_OK;
261 }else {
262 *ret = FALSE;
266 IUnknown_Release(unk1);
267 IUnknown_Release(unk2);
268 return S_OK;
271 static inline BOOL is_num_vt(enum VARENUM vt)
273 return vt == VT_I4 || vt == VT_R8;
276 static inline DOUBLE num_val(const VARIANT *v)
278 return V_VT(v) == VT_I4 ? V_I4(v) : V_R8(v);
281 static inline void num_set_val(VARIANT *v, DOUBLE d)
283 if(d == (DOUBLE)(INT)d) {
284 V_VT(v) = VT_I4;
285 V_I4(v) = d;
286 }else {
287 V_VT(v) = VT_R8;
288 V_R8(v) = d;
292 /* ECMA-262 3rd Edition 11.9.6 */
293 HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
295 TRACE("\n");
297 if(V_VT(lval) != V_VT(rval)) {
298 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))) {
299 *ret = num_val(lval) == num_val(rval);
300 return S_OK;
303 *ret = FALSE;
304 return S_OK;
307 switch(V_VT(lval)) {
308 case VT_EMPTY:
309 case VT_NULL:
310 *ret = VARIANT_TRUE;
311 break;
312 case VT_I4:
313 *ret = V_I4(lval) == V_I4(rval);
314 break;
315 case VT_R8:
316 *ret = V_R8(lval) == V_R8(rval);
317 break;
318 case VT_BSTR:
319 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
320 break;
321 case VT_DISPATCH:
322 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
323 case VT_BOOL:
324 *ret = !V_BOOL(lval) == !V_BOOL(rval);
325 break;
326 default:
327 FIXME("unimplemented vt %d\n", V_VT(lval));
328 return E_NOTIMPL;
331 return S_OK;
334 static HRESULT literal_to_var(literal_t *literal, VARIANT *v)
336 V_VT(v) = literal->vt;
338 switch(V_VT(v)) {
339 case VT_EMPTY:
340 case VT_NULL:
341 break;
342 case VT_I4:
343 V_I4(v) = literal->u.lval;
344 break;
345 case VT_R8:
346 V_R8(v) = literal->u.dval;
347 break;
348 case VT_BSTR:
349 V_BSTR(v) = SysAllocString(literal->u.wstr);
350 break;
351 case VT_BOOL:
352 V_BOOL(v) = literal->u.bval;
353 break;
354 case VT_DISPATCH:
355 IDispatch_AddRef(literal->u.disp);
356 V_DISPATCH(v) = literal->u.disp;
357 break;
358 default:
359 ERR("wrong type %d\n", V_VT(v));
360 return E_NOTIMPL;
363 return S_OK;
366 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, jsexcept_t *ei, VARIANT *retv)
368 script_ctx_t *script = parser->script;
369 function_declaration_t *func;
370 parser_ctx_t *prev_parser;
371 VARIANT val, tmp;
372 statement_t *stat;
373 exec_ctx_t *prev_ctx;
374 return_type_t rt;
375 HRESULT hres = S_OK;
377 for(func = source->functions; func; func = func->next) {
378 DispatchEx *func_obj;
379 VARIANT var;
381 hres = create_source_function(parser, func->parameter_list, func->source_elements, ctx->scope_chain, &func_obj);
382 if(FAILED(hres))
383 return hres;
385 V_VT(&var) = VT_DISPATCH;
386 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(func_obj);
387 hres = jsdisp_propput_name(ctx->var_disp, func->identifier, script->lcid, &var, ei, NULL);
388 IDispatchEx_Release(_IDispatchEx_(func_obj));
389 if(FAILED(hres))
390 return hres;
393 prev_ctx = script->exec_ctx;
394 script->exec_ctx = ctx;
396 prev_parser = ctx->parser;
397 ctx->parser = parser;
399 V_VT(&val) = VT_EMPTY;
400 memset(&rt, 0, sizeof(rt));
401 rt.type = RT_NORMAL;
403 for(stat = source->statement; stat; stat = stat->next) {
404 hres = stat_eval(ctx, stat, &rt, &tmp);
405 if(FAILED(hres))
406 break;
408 VariantClear(&val);
409 val = tmp;
410 if(rt.type != RT_NORMAL)
411 break;
414 script->exec_ctx = prev_ctx;
415 ctx->parser = prev_parser;
417 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
418 FIXME("wrong rt %d\n", rt.type);
419 hres = E_FAIL;
422 *ei = rt.ei;
423 if(FAILED(hres)) {
424 VariantClear(&val);
425 return hres;
428 if(retv)
429 *retv = val;
430 else
431 VariantClear(&val);
432 return S_OK;
435 /* ECMA-262 3rd Edition 10.1.4 */
436 static HRESULT identifier_eval(exec_ctx_t *ctx, BSTR identifier, DWORD flags, exprval_t *ret)
438 scope_chain_t *scope;
439 named_item_t *item;
440 DISPID id = 0;
441 HRESULT hres;
443 TRACE("%s\n", debugstr_w(identifier));
445 for(scope = ctx->scope_chain; scope; scope = scope->next) {
446 hres = dispex_get_id(_IDispatchEx_(scope->obj), identifier, 0, &id);
447 if(SUCCEEDED(hres))
448 break;
451 if(scope) {
452 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(scope->obj), id);
453 return S_OK;
456 hres = dispex_get_id(_IDispatchEx_(ctx->parser->script->global), identifier, 0, &id);
457 if(SUCCEEDED(hres)) {
458 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->global), id);
459 return S_OK;
462 for(item = ctx->parser->script->named_items; item; item = item->next) {
463 hres = disp_get_id(item->disp, identifier, 0, &id);
464 if(SUCCEEDED(hres))
465 break;
468 if(item) {
469 exprval_set_idref(ret, (IDispatch*)item->disp, id);
470 return S_OK;
473 hres = dispex_get_id(_IDispatchEx_(ctx->parser->script->script_disp), identifier, 0, &id);
474 if(SUCCEEDED(hres)) {
475 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->script_disp), id);
476 return S_OK;
479 if(flags & EXPR_NEWREF) {
480 hres = dispex_get_id(_IDispatchEx_(ctx->var_disp), identifier, fdexNameEnsure, &id);
481 if(FAILED(hres))
482 return hres;
484 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->var_disp), id);
485 return S_OK;
488 WARN("Could not find identifier %s\n", debugstr_w(identifier));
489 return E_FAIL;
492 /* ECMA-262 3rd Edition 12.1 */
493 HRESULT block_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
495 block_statement_t *stat = (block_statement_t*)_stat;
496 VARIANT val, tmp;
497 statement_t *iter;
498 HRESULT hres = S_OK;
500 TRACE("\n");
502 V_VT(&val) = VT_EMPTY;
503 for(iter = stat->stat_list; iter; iter = iter->next) {
504 hres = stat_eval(ctx, iter, rt, &tmp);
505 if(FAILED(hres))
506 break;
508 VariantClear(&val);
509 val = tmp;
510 if(rt->type != RT_NORMAL)
511 break;
514 if(FAILED(hres)) {
515 VariantClear(&val);
516 return hres;
519 *ret = val;
520 return S_OK;
523 /* ECMA-262 3rd Edition 12.2 */
524 static HRESULT variable_list_eval(exec_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
526 variable_declaration_t *iter;
527 HRESULT hres;
529 for(iter = var_list; iter; iter = iter->next) {
530 VARIANT val;
532 if(iter->expr) {
533 exprval_t exprval;
535 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
536 if(FAILED(hres))
537 break;
539 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
540 exprval_release(&exprval);
541 if(FAILED(hres))
542 break;
543 }else {
544 V_VT(&val) = VT_EMPTY;
547 hres = jsdisp_propput_name(ctx->var_disp, iter->identifier, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
548 VariantClear(&val);
549 if(FAILED(hres))
550 break;
553 return hres;
556 /* ECMA-262 3rd Edition 12.2 */
557 HRESULT var_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
559 var_statement_t *stat = (var_statement_t*)_stat;
560 HRESULT hres;
562 TRACE("\n");
564 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
565 if(FAILED(hres))
566 return hres;
568 V_VT(ret) = VT_EMPTY;
569 return S_OK;
572 /* ECMA-262 3rd Edition 12.3 */
573 HRESULT empty_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
575 TRACE("\n");
577 V_VT(ret) = VT_EMPTY;
578 return S_OK;
581 /* ECMA-262 3rd Edition 12.4 */
582 HRESULT expression_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
584 expression_statement_t *stat = (expression_statement_t*)_stat;
585 exprval_t exprval;
586 VARIANT val;
587 HRESULT hres;
589 TRACE("\n");
591 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
592 if(FAILED(hres))
593 return hres;
595 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
596 exprval_release(&exprval);
597 if(FAILED(hres))
598 return hres;
600 *ret = val;
601 TRACE("= %s\n", debugstr_variant(ret));
602 return S_OK;
605 /* ECMA-262 3rd Edition 12.5 */
606 HRESULT if_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
608 if_statement_t *stat = (if_statement_t*)_stat;
609 exprval_t exprval;
610 VARIANT_BOOL b;
611 HRESULT hres;
613 TRACE("\n");
615 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
616 if(FAILED(hres))
617 return hres;
619 hres = exprval_to_boolean(ctx->parser->script, &exprval, &rt->ei, &b);
620 exprval_release(&exprval);
621 if(FAILED(hres))
622 return hres;
624 if(b)
625 hres = stat_eval(ctx, stat->if_stat, rt, ret);
626 else if(stat->else_stat)
627 hres = stat_eval(ctx, stat->else_stat, rt, ret);
628 else
629 V_VT(ret) = VT_EMPTY;
631 return hres;
634 HRESULT dowhile_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
636 FIXME("\n");
637 return E_NOTIMPL;
640 HRESULT while_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
642 FIXME("\n");
643 return E_NOTIMPL;
646 HRESULT for_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
648 FIXME("\n");
649 return E_NOTIMPL;
652 HRESULT forin_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
654 FIXME("\n");
655 return E_NOTIMPL;
658 HRESULT continue_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
660 FIXME("\n");
661 return E_NOTIMPL;
664 HRESULT break_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
666 FIXME("\n");
667 return E_NOTIMPL;
670 /* ECMA-262 3rd Edition 12.9 */
671 HRESULT return_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
673 expression_statement_t *stat = (expression_statement_t*)_stat;
674 HRESULT hres;
676 TRACE("\n");
678 if(stat->expr) {
679 exprval_t exprval;
681 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
682 if(FAILED(hres))
683 return hres;
685 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, ret);
686 exprval_release(&exprval);
687 if(FAILED(hres))
688 return hres;
689 }else {
690 V_VT(ret) = VT_EMPTY;
693 TRACE("= %s\n", debugstr_variant(ret));
694 rt->type = RT_RETURN;
695 return S_OK;
698 HRESULT with_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
700 FIXME("\n");
701 return E_NOTIMPL;
704 HRESULT labelled_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
706 FIXME("\n");
707 return E_NOTIMPL;
710 HRESULT switch_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
712 FIXME("\n");
713 return E_NOTIMPL;
716 HRESULT throw_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
718 FIXME("\n");
719 return E_NOTIMPL;
722 HRESULT try_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
724 FIXME("\n");
725 return E_NOTIMPL;
728 static HRESULT return_bool(exprval_t *ret, DWORD b)
730 ret->type = EXPRVAL_VARIANT;
731 V_VT(&ret->u.var) = VT_BOOL;
732 V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
734 return S_OK;
737 static HRESULT get_binary_expr_values(exec_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
739 exprval_t exprval;
740 HRESULT hres;
742 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
743 if(FAILED(hres))
744 return hres;
746 hres = exprval_to_value(ctx->parser->script, &exprval, ei, lval);
747 exprval_release(&exprval);
748 if(FAILED(hres))
749 return hres;
751 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
752 if(SUCCEEDED(hres)) {
753 hres = exprval_to_value(ctx->parser->script, &exprval, ei, rval);
754 exprval_release(&exprval);
757 if(FAILED(hres)) {
758 VariantClear(lval);
759 return hres;
762 return S_OK;
765 typedef HRESULT (*oper_t)(exec_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
767 static HRESULT binary_expr_eval(exec_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
768 exprval_t *ret)
770 VARIANT lval, rval, retv;
771 HRESULT hres;
773 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
774 if(FAILED(hres))
775 return hres;
777 hres = oper(ctx, &lval, &rval, ei, &retv);
778 VariantClear(&lval);
779 VariantClear(&rval);
780 if(FAILED(hres))
781 return hres;
783 ret->type = EXPRVAL_VARIANT;
784 ret->u.var = retv;
785 return S_OK;
788 /* ECMA-262 3rd Edition 11.13.2 */
789 static HRESULT assign_oper_eval(exec_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
790 jsexcept_t *ei, exprval_t *ret)
792 VARIANT retv, lval, rval;
793 exprval_t exprval, exprvalr;
794 HRESULT hres;
796 hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
797 if(FAILED(hres))
798 return hres;
800 hres = exprval_value(ctx->parser->script, &exprval, ei, &lval);
801 if(SUCCEEDED(hres)) {
802 hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
803 if(SUCCEEDED(hres)) {
804 hres = exprval_value(ctx->parser->script, &exprvalr, ei, &rval);
805 exprval_release(&exprvalr);
807 if(SUCCEEDED(hres)) {
808 hres = oper(ctx, &lval, &rval, ei, &retv);
809 VariantClear(&rval);
811 VariantClear(&lval);
814 if(SUCCEEDED(hres)) {
815 hres = put_value(ctx->parser->script, &exprval, &retv, ei);
816 if(FAILED(hres))
817 VariantClear(&retv);
819 exprval_release(&exprval);
821 if(FAILED(hres))
822 return hres;
824 ret->type = EXPRVAL_VARIANT;
825 ret->u.var = retv;
826 return S_OK;
829 /* ECMA-262 3rd Edition 13 */
830 HRESULT function_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
832 function_expression_t *expr = (function_expression_t*)_expr;
833 DispatchEx *dispex;
834 VARIANT var;
835 HRESULT hres;
837 TRACE("\n");
839 hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain, &dispex);
840 if(FAILED(hres))
841 return hres;
843 V_VT(&var) = VT_DISPATCH;
844 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
846 if(expr->identifier) {
847 hres = jsdisp_propput_name(ctx->var_disp, expr->identifier, ctx->parser->script->lcid, &var, ei, NULL/*FIXME*/);
848 if(FAILED(hres)) {
849 jsdisp_release(dispex);
850 return hres;
854 ret->type = EXPRVAL_VARIANT;
855 ret->u.var = var;
856 return S_OK;
859 /* ECMA-262 3rd Edition 11.12 */
860 HRESULT conditional_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
862 conditional_expression_t *expr = (conditional_expression_t*)_expr;
863 exprval_t exprval;
864 VARIANT_BOOL b;
865 HRESULT hres;
867 TRACE("\n");
869 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
870 if(FAILED(hres))
871 return hres;
873 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
874 exprval_release(&exprval);
875 if(FAILED(hres))
876 return hres;
878 return expr_eval(ctx, b ? expr->true_expression : expr->false_expression, flags, ei, ret);
881 /* ECMA-262 3rd Edition 11.2.1 */
882 HRESULT array_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
884 array_expression_t *expr = (array_expression_t*)_expr;
885 exprval_t exprval;
886 VARIANT member, val;
887 DISPID id;
888 BSTR str;
889 IDispatch *obj = NULL;
890 HRESULT hres;
892 TRACE("\n");
894 hres = expr_eval(ctx, expr->member_expr, EXPR_NEWREF, ei, &exprval);
895 if(FAILED(hres))
896 return hres;
898 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
899 exprval_release(&exprval);
900 if(FAILED(hres))
901 return hres;
903 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
904 if(SUCCEEDED(hres)) {
905 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
906 exprval_release(&exprval);
909 if(SUCCEEDED(hres))
910 hres = to_object(ctx, &member, &obj);
911 VariantClear(&member);
912 if(SUCCEEDED(hres)) {
913 hres = to_string(ctx->parser->script, &val, ei, &str);
914 if(SUCCEEDED(hres)) {
915 if(flags & EXPR_STRREF) {
916 ret->type = EXPRVAL_NAMEREF;
917 ret->u.nameref.disp = obj;
918 ret->u.nameref.name = str;
919 return S_OK;
922 hres = disp_get_id(obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
925 if(SUCCEEDED(hres)) {
926 exprval_set_idref(ret, obj, id);
927 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
928 exprval_init(ret);
929 hres = S_OK;
932 IDispatch_Release(obj);
935 return hres;
938 /* ECMA-262 3rd Edition 11.2.1 */
939 HRESULT member_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
941 member_expression_t *expr = (member_expression_t*)_expr;
942 IDispatch *obj = NULL;
943 exprval_t exprval;
944 VARIANT member;
945 DISPID id;
946 BSTR str;
947 HRESULT hres;
949 TRACE("\n");
951 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
952 if(FAILED(hres))
953 return hres;
955 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
956 exprval_release(&exprval);
957 if(FAILED(hres))
958 return hres;
960 hres = to_object(ctx, &member, &obj);
961 VariantClear(&member);
962 if(FAILED(hres))
963 return hres;
965 str = SysAllocString(expr->identifier);
966 if(flags & EXPR_STRREF) {
967 ret->type = EXPRVAL_NAMEREF;
968 ret->u.nameref.disp = obj;
969 ret->u.nameref.name = str;
970 return S_OK;
973 hres = disp_get_id(obj, str, flags & EXPR_NEW ? fdexNameEnsure : 0, &id);
974 SysFreeString(str);
975 if(SUCCEEDED(hres)) {
976 exprval_set_idref(ret, obj, id);
977 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
978 exprval_init(ret);
979 hres = S_OK;
982 IDispatch_Release(obj);
983 return hres;
986 static void free_dp(DISPPARAMS *dp)
988 DWORD i;
990 for(i=0; i < dp->cArgs; i++)
991 VariantClear(dp->rgvarg+i);
992 heap_free(dp->rgvarg);
995 static HRESULT args_to_param(exec_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
997 VARIANTARG *vargs;
998 exprval_t exprval;
999 argument_t *iter;
1000 DWORD cnt = 0, i;
1001 HRESULT hres = S_OK;
1003 memset(dp, 0, sizeof(*dp));
1005 for(iter = args; iter; iter = iter->next)
1006 cnt++;
1007 if(!cnt)
1008 return S_OK;
1010 vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1011 if(!vargs)
1012 return E_OUTOFMEMORY;
1014 for(i = cnt, iter = args; iter; iter = iter->next) {
1015 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1016 if(FAILED(hres))
1017 break;
1019 hres = exprval_to_value(ctx->parser->script, &exprval, ei, vargs + (--i));
1020 exprval_release(&exprval);
1021 if(FAILED(hres))
1022 break;
1025 if(FAILED(hres)) {
1026 free_dp(dp);
1027 return hres;
1030 dp->rgvarg = vargs;
1031 dp->cArgs = cnt;
1032 return S_OK;
1035 /* ECMA-262 3rd Edition 11.2.2 */
1036 HRESULT member_new_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1038 call_expression_t *expr = (call_expression_t*)_expr;
1039 exprval_t exprval;
1040 VARIANT constr, var;
1041 DISPPARAMS dp;
1042 HRESULT hres;
1044 TRACE("\n");
1046 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1047 if(FAILED(hres))
1048 return hres;
1050 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1051 if(SUCCEEDED(hres))
1052 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &constr);
1053 exprval_release(&exprval);
1054 if(FAILED(hres))
1055 return hres;
1057 if(V_VT(&constr) != VT_DISPATCH) {
1058 FIXME("throw TypeError\n");
1059 VariantClear(&constr);
1060 return E_FAIL;
1063 hres = disp_call(V_DISPATCH(&constr), DISPID_VALUE, ctx->parser->script->lcid,
1064 DISPATCH_CONSTRUCT, &dp, &var, ei, NULL/*FIXME*/);
1065 IDispatch_Release(V_DISPATCH(&constr));
1066 if(FAILED(hres))
1067 return hres;
1069 ret->type = EXPRVAL_VARIANT;
1070 ret->u.var = var;
1071 return S_OK;
1074 HRESULT call_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1076 call_expression_t *expr = (call_expression_t*)_expr;
1077 VARIANT func, var;
1078 exprval_t exprval;
1079 DISPPARAMS dp;
1080 HRESULT hres;
1082 TRACE("\n");
1084 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1085 if(FAILED(hres))
1086 return hres;
1088 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1089 if(SUCCEEDED(hres)) {
1090 switch(exprval.type) {
1091 case EXPRVAL_IDREF:
1092 hres = disp_call(exprval.u.idref.disp, exprval.u.idref.id, ctx->parser->script->lcid, DISPATCH_METHOD,
1093 &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1094 if(flags & EXPR_NOVAL)
1095 V_VT(&var) = VT_EMPTY;
1096 break;
1097 default:
1098 FIXME("unimplemented type %d\n", V_VT(&func));
1099 hres = E_NOTIMPL;
1102 free_dp(&dp);
1105 exprval_release(&exprval);
1106 if(FAILED(hres))
1107 return hres;
1109 TRACE("= %s\n", debugstr_variant(&var));
1110 ret->type = EXPRVAL_VARIANT;
1111 ret->u.var = var;
1112 return S_OK;
1115 HRESULT this_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1117 TRACE("\n");
1119 ret->type = EXPRVAL_VARIANT;
1120 V_VT(&ret->u.var) = VT_DISPATCH;
1121 V_DISPATCH(&ret->u.var) = ctx->this_obj;
1122 IDispatch_AddRef(ctx->this_obj);
1123 return S_OK;
1126 /* ECMA-262 3rd Edition 10.1.4 */
1127 HRESULT identifier_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1129 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1130 BSTR identifier;
1131 HRESULT hres;
1133 TRACE("\n");
1135 identifier = SysAllocString(expr->identifier);
1136 if(!identifier)
1137 return E_OUTOFMEMORY;
1139 hres = identifier_eval(ctx, identifier, flags, ret);
1141 SysFreeString(identifier);
1142 return hres;
1145 /* ECMA-262 3rd Edition 7.8 */
1146 HRESULT literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1148 literal_expression_t *expr = (literal_expression_t*)_expr;
1149 VARIANT var;
1150 HRESULT hres;
1152 TRACE("\n");
1154 hres = literal_to_var(expr->literal, &var);
1155 if(FAILED(hres))
1156 return hres;
1158 ret->type = EXPRVAL_VARIANT;
1159 ret->u.var = var;
1160 return S_OK;
1163 HRESULT array_literal_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1165 FIXME("\n");
1166 return E_NOTIMPL;
1169 /* ECMA-262 3rd Edition 11.1.5 */
1170 HRESULT property_value_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1172 property_value_expression_t *expr = (property_value_expression_t*)_expr;
1173 VARIANT val, tmp;
1174 DispatchEx *obj;
1175 prop_val_t *iter;
1176 exprval_t exprval;
1177 BSTR name;
1178 HRESULT hres;
1180 TRACE("\n");
1182 hres = create_object(ctx->parser->script, NULL, &obj);
1183 if(FAILED(hres))
1184 return hres;
1186 for(iter = expr->property_list; iter; iter = iter->next) {
1187 hres = literal_to_var(iter->name, &tmp);
1188 if(FAILED(hres))
1189 break;
1191 hres = to_string(ctx->parser->script, &tmp, ei, &name);
1192 VariantClear(&tmp);
1193 if(FAILED(hres))
1194 break;
1196 hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
1197 if(SUCCEEDED(hres)) {
1198 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1199 exprval_release(&exprval);
1200 if(SUCCEEDED(hres)) {
1201 hres = jsdisp_propput_name(obj, name, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
1202 VariantClear(&val);
1206 SysFreeString(name);
1207 if(FAILED(hres))
1208 break;
1211 if(FAILED(hres)) {
1212 jsdisp_release(obj);
1213 return hres;
1216 ret->type = EXPRVAL_VARIANT;
1217 V_VT(&ret->u.var) = VT_DISPATCH;
1218 V_DISPATCH(&ret->u.var) = (IDispatch*)_IDispatchEx_(obj);
1219 return S_OK;
1222 HRESULT comma_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1224 FIXME("\n");
1225 return E_NOTIMPL;
1228 /* ECMA-262 3rd Edition 11.11 */
1229 HRESULT logical_or_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1231 binary_expression_t *expr = (binary_expression_t*)_expr;
1232 exprval_t exprval;
1233 VARIANT_BOOL b;
1234 VARIANT val;
1235 HRESULT hres;
1237 TRACE("\n");
1239 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1240 if(FAILED(hres))
1241 return hres;
1243 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1244 exprval_release(&exprval);
1245 if(FAILED(hres))
1246 return hres;
1248 hres = to_boolean(&val, &b);
1249 if(SUCCEEDED(hres) && b) {
1250 ret->type = EXPRVAL_VARIANT;
1251 ret->u.var = val;
1252 return S_OK;
1255 VariantClear(&val);
1256 if(FAILED(hres))
1257 return hres;
1259 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1260 if(FAILED(hres))
1261 return hres;
1263 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1264 exprval_release(&exprval);
1265 if(FAILED(hres))
1266 return hres;
1268 ret->type = EXPRVAL_VARIANT;
1269 ret->u.var = val;
1270 return S_OK;
1273 /* ECMA-262 3rd Edition 11.11 */
1274 HRESULT logical_and_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1276 binary_expression_t *expr = (binary_expression_t*)_expr;
1277 exprval_t exprval;
1278 VARIANT_BOOL b;
1279 VARIANT val;
1280 HRESULT hres;
1282 TRACE("\n");
1284 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1285 if(FAILED(hres))
1286 return hres;
1288 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1289 exprval_release(&exprval);
1290 if(FAILED(hres))
1291 return hres;
1293 hres = to_boolean(&val, &b);
1294 if(SUCCEEDED(hres) && !b) {
1295 ret->type = EXPRVAL_VARIANT;
1296 ret->u.var = val;
1297 return S_OK;
1300 VariantClear(&val);
1301 if(FAILED(hres))
1302 return hres;
1304 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1305 if(FAILED(hres))
1306 return hres;
1308 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1309 exprval_release(&exprval);
1310 if(FAILED(hres))
1311 return hres;
1313 ret->type = EXPRVAL_VARIANT;
1314 ret->u.var = val;
1315 return S_OK;
1318 HRESULT binary_or_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1320 FIXME("\n");
1321 return E_NOTIMPL;
1324 HRESULT binary_xor_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1326 FIXME("\n");
1327 return E_NOTIMPL;
1330 HRESULT binary_and_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1332 FIXME("\n");
1333 return E_NOTIMPL;
1336 HRESULT instanceof_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1338 FIXME("\n");
1339 return E_NOTIMPL;
1342 HRESULT in_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1344 FIXME("\n");
1345 return E_NOTIMPL;
1348 /* ECMA-262 3rd Edition 11.6.1 */
1349 static HRESULT add_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1351 VARIANT r, l;
1352 HRESULT hres;
1354 hres = to_primitive(ctx->parser->script, lval, ei, &l);
1355 if(FAILED(hres))
1356 return hres;
1358 hres = to_primitive(ctx->parser->script, rval, ei, &r);
1359 if(FAILED(hres)) {
1360 VariantClear(&l);
1361 return hres;
1364 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1365 BSTR lstr = NULL, rstr = NULL;
1367 if(V_VT(&l) == VT_BSTR)
1368 lstr = V_BSTR(&l);
1369 else
1370 hres = to_string(ctx->parser->script, &l, ei, &lstr);
1372 if(SUCCEEDED(hres)) {
1373 if(V_VT(&r) == VT_BSTR)
1374 rstr = V_BSTR(&r);
1375 else
1376 hres = to_string(ctx->parser->script, &r, ei, &rstr);
1379 if(SUCCEEDED(hres)) {
1380 int len1, len2;
1382 len1 = SysStringLen(lstr);
1383 len2 = SysStringLen(rstr);
1385 V_VT(retv) = VT_BSTR;
1386 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1387 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1388 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
1391 if(lstr && V_VT(&l) != VT_BSTR)
1392 SysFreeString(lstr);
1393 if(rstr && V_VT(&r) != VT_BSTR)
1394 SysFreeString(rstr);
1395 }else {
1396 VARIANT nl, nr;
1398 hres = to_number(ctx->parser->script, &l, ei, &nl);
1399 if(SUCCEEDED(hres)) {
1400 hres = to_number(ctx->parser->script, &r, ei, &nr);
1401 if(SUCCEEDED(hres))
1402 num_set_val(retv, num_val(&nl) + num_val(&nr));
1406 VariantClear(&r);
1407 VariantClear(&l);
1408 return hres;
1411 /* ECMA-262 3rd Edition 11.6.1 */
1412 HRESULT add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1414 binary_expression_t *expr = (binary_expression_t*)_expr;
1416 TRACE("\n");
1418 return binary_expr_eval(ctx, expr, add_eval, ei, ret);
1421 HRESULT sub_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1423 FIXME("\n");
1424 return E_NOTIMPL;
1427 HRESULT mul_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1429 FIXME("\n");
1430 return E_NOTIMPL;
1433 HRESULT div_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1435 FIXME("\n");
1436 return E_NOTIMPL;
1439 HRESULT mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1441 FIXME("\n");
1442 return E_NOTIMPL;
1445 HRESULT delete_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1447 FIXME("\n");
1448 return E_NOTIMPL;
1451 HRESULT void_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1453 FIXME("\n");
1454 return E_NOTIMPL;
1457 HRESULT typeof_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1459 unary_expression_t *expr = (unary_expression_t*)_expr;
1460 const WCHAR *str;
1461 exprval_t exprval;
1462 VARIANT val;
1463 HRESULT hres;
1465 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
1466 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
1467 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
1468 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
1469 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
1470 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1472 TRACE("\n");
1474 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1475 if(FAILED(hres))
1476 return hres;
1478 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
1479 exprval_release(&exprval);
1480 if(FAILED(hres))
1481 return hres;
1483 switch(V_VT(&val)) {
1484 case VT_EMPTY:
1485 str = undefinedW;
1486 break;
1487 case VT_NULL:
1488 str = objectW;
1489 break;
1490 case VT_BOOL:
1491 str = booleanW;
1492 break;
1493 case VT_I4:
1494 case VT_R8:
1495 str = numberW;
1496 break;
1497 case VT_BSTR:
1498 str = stringW;
1499 break;
1500 case VT_DISPATCH: {
1501 DispatchEx *dispex;
1503 dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val));
1504 if(dispex) {
1505 str = dispex->builtin_info->class == JSCLASS_FUNCTION ? functionW : objectW;
1506 IDispatchEx_Release(_IDispatchEx_(dispex));
1507 }else {
1508 str = objectW;
1510 break;
1512 default:
1513 FIXME("unhandled vt %d\n", V_VT(&val));
1514 hres = E_NOTIMPL;
1517 VariantClear(&val);
1518 if(FAILED(hres))
1519 return hres;
1521 ret->type = EXPRVAL_VARIANT;
1522 V_VT(&ret->u.var) = VT_BSTR;
1523 V_BSTR(&ret->u.var) = SysAllocString(str);
1524 return S_OK;
1527 HRESULT minus_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1529 FIXME("\n");
1530 return E_NOTIMPL;
1533 HRESULT plus_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1535 FIXME("\n");
1536 return E_NOTIMPL;
1539 /* ECMA-262 3rd Edition 11.3.1 */
1540 HRESULT post_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1542 unary_expression_t *expr = (unary_expression_t*)_expr;
1543 VARIANT val, num;
1544 exprval_t exprval;
1545 HRESULT hres;
1547 TRACE("\n");
1549 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1550 if(FAILED(hres))
1551 return hres;
1553 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
1554 if(SUCCEEDED(hres)) {
1555 hres = to_number(ctx->parser->script, &val, ei, &num);
1556 VariantClear(&val);
1559 if(SUCCEEDED(hres)) {
1560 VARIANT inc;
1561 num_set_val(&inc, num_val(&num)+1.0);
1562 hres = put_value(ctx->parser->script, &exprval, &inc, ei);
1565 exprval_release(&exprval);
1566 if(FAILED(hres))
1567 return hres;
1569 ret->type = EXPRVAL_VARIANT;
1570 ret->u.var = num;
1571 return S_OK;
1574 /* ECMA-262 3rd Edition 11.3.2 */
1575 HRESULT post_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1577 unary_expression_t *expr = (unary_expression_t*)_expr;
1578 VARIANT val, num;
1579 exprval_t exprval;
1580 HRESULT hres;
1582 TRACE("\n");
1584 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1585 if(FAILED(hres))
1586 return hres;
1588 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
1589 if(SUCCEEDED(hres)) {
1590 hres = to_number(ctx->parser->script, &val, ei, &num);
1591 VariantClear(&val);
1594 if(SUCCEEDED(hres)) {
1595 VARIANT dec;
1596 num_set_val(&dec, num_val(&num)-1.0);
1597 hres = put_value(ctx->parser->script, &exprval, &dec, ei);
1600 exprval_release(&exprval);
1601 if(FAILED(hres))
1602 return hres;
1604 ret->type = EXPRVAL_VARIANT;
1605 ret->u.var = num;
1606 return S_OK;
1609 /* ECMA-262 3rd Edition 11.4.4 */
1610 HRESULT pre_increment_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1612 unary_expression_t *expr = (unary_expression_t*)_expr;
1613 VARIANT val, num;
1614 exprval_t exprval;
1615 HRESULT hres;
1617 TRACE("\n");
1619 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1620 if(FAILED(hres))
1621 return hres;
1623 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
1624 if(SUCCEEDED(hres)) {
1625 hres = to_number(ctx->parser->script, &val, ei, &num);
1626 VariantClear(&val);
1629 if(SUCCEEDED(hres)) {
1630 num_set_val(&val, num_val(&num)+1.0);
1631 hres = put_value(ctx->parser->script, &exprval, &val, ei);
1634 exprval_release(&exprval);
1635 if(FAILED(hres))
1636 return hres;
1638 ret->type = EXPRVAL_VARIANT;
1639 ret->u.var = val;
1640 return S_OK;
1643 /* ECMA-262 3rd Edition 11.4.5 */
1644 HRESULT pre_decrement_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1646 unary_expression_t *expr = (unary_expression_t*)_expr;
1647 VARIANT val, num;
1648 exprval_t exprval;
1649 HRESULT hres;
1651 TRACE("\n");
1653 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1654 if(FAILED(hres))
1655 return hres;
1657 hres = exprval_value(ctx->parser->script, &exprval, ei, &val);
1658 if(SUCCEEDED(hres)) {
1659 hres = to_number(ctx->parser->script, &val, ei, &num);
1660 VariantClear(&val);
1663 if(SUCCEEDED(hres)) {
1664 num_set_val(&val, num_val(&num)-1.0);
1665 hres = put_value(ctx->parser->script, &exprval, &val, ei);
1668 exprval_release(&exprval);
1669 if(FAILED(hres))
1670 return hres;
1672 ret->type = EXPRVAL_VARIANT;
1673 ret->u.var = val;
1674 return S_OK;
1677 HRESULT new_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1679 FIXME("\n");
1680 return E_NOTIMPL;
1683 HRESULT equal_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1685 FIXME("\n");
1686 return E_NOTIMPL;
1689 /* ECMA-262 3rd Edition 11.9.4 */
1690 HRESULT equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1692 binary_expression_t *expr = (binary_expression_t*)_expr;
1693 VARIANT rval, lval;
1694 BOOL b;
1695 HRESULT hres;
1697 TRACE("\n");
1699 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
1700 if(FAILED(hres))
1701 return hres;
1703 hres = equal2_values(&rval, &lval, &b);
1704 if(FAILED(hres))
1705 return hres;
1707 return return_bool(ret, b);
1710 HRESULT not_equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1713 FIXME("\n");
1714 return E_NOTIMPL;
1717 /* ECMA-262 3rd Edition 11.9.5 */
1718 HRESULT not_equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1720 binary_expression_t *expr = (binary_expression_t*)_expr;
1721 VARIANT rval, lval;
1722 BOOL b;
1723 HRESULT hres;
1725 TRACE("\n");
1727 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
1728 if(FAILED(hres))
1729 return hres;
1731 hres = equal2_values(&rval, &lval, &b);
1732 if(FAILED(hres))
1733 return hres;
1735 return return_bool(ret, !b);
1738 /* ECMA-262 3rd Edition 11.8.5 */
1739 static HRESULT less_eval(exec_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
1741 VARIANT l, r, ln, rn;
1742 HRESULT hres;
1744 hres = to_primitive(ctx->parser->script, lval, ei, &l);
1745 if(FAILED(hres))
1746 return hres;
1748 hres = to_primitive(ctx->parser->script, rval, ei, &r);
1749 if(FAILED(hres)) {
1750 VariantClear(&l);
1751 return hres;
1754 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
1755 *ret = strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0;
1756 SysFreeString(V_BSTR(&l));
1757 SysFreeString(V_BSTR(&r));
1758 return S_OK;
1761 hres = to_number(ctx->parser->script, &l, ei, &ln);
1762 VariantClear(&l);
1763 if(SUCCEEDED(hres))
1764 hres = to_number(ctx->parser->script, &r, ei, &rn);
1765 VariantClear(&r);
1766 if(FAILED(hres))
1767 return hres;
1769 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4)
1770 *ret = V_I4(&ln) < V_I4(&rn);
1771 else
1772 *ret = num_val(&ln) < num_val(&rn);
1774 return S_OK;
1777 /* ECMA-262 3rd Edition 11.8.1 */
1778 HRESULT less_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1780 binary_expression_t *expr = (binary_expression_t*)_expr;
1781 VARIANT rval, lval;
1782 BOOL b;
1783 HRESULT hres;
1785 TRACE("\n");
1787 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1788 if(FAILED(hres))
1789 return hres;
1791 hres = less_eval(ctx, &lval, &rval, ei, &b);
1792 VariantClear(&lval);
1793 VariantClear(&rval);
1794 if(FAILED(hres))
1795 return hres;
1797 return return_bool(ret, b);
1800 /* ECMA-262 3rd Edition 11.8.3 */
1801 HRESULT lesseq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1803 binary_expression_t *expr = (binary_expression_t*)_expr;
1804 VARIANT rval, lval;
1805 BOOL b;
1806 HRESULT hres;
1808 TRACE("\n");
1810 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1811 if(FAILED(hres))
1812 return hres;
1814 hres = less_eval(ctx, &rval, &lval, ei, &b);
1815 VariantClear(&lval);
1816 VariantClear(&rval);
1817 if(FAILED(hres))
1818 return hres;
1820 return return_bool(ret, !b);
1823 /* ECMA-262 3rd Edition 11.8.2 */
1824 HRESULT greater_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1826 binary_expression_t *expr = (binary_expression_t*)_expr;
1827 VARIANT rval, lval;
1828 BOOL b;
1829 HRESULT hres;
1831 TRACE("\n");
1833 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1834 if(FAILED(hres))
1835 return hres;
1837 hres = less_eval(ctx, &rval, &lval, ei, &b);
1838 VariantClear(&lval);
1839 VariantClear(&rval);
1840 if(FAILED(hres))
1841 return hres;
1843 return return_bool(ret, b);
1846 /* ECMA-262 3rd Edition 11.8.4 */
1847 HRESULT greatereq_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1849 binary_expression_t *expr = (binary_expression_t*)_expr;
1850 VARIANT rval, lval;
1851 BOOL b;
1852 HRESULT hres;
1854 TRACE("\n");
1856 hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1857 if(FAILED(hres))
1858 return hres;
1860 hres = less_eval(ctx, &lval, &rval, ei, &b);
1861 VariantClear(&lval);
1862 VariantClear(&rval);
1863 if(FAILED(hres))
1864 return hres;
1866 return return_bool(ret, !b);
1869 HRESULT binary_negation_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1871 FIXME("\n");
1872 return E_NOTIMPL;
1875 /* ECMA-262 3rd Edition 11.4.9 */
1876 HRESULT logical_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1878 unary_expression_t *expr = (unary_expression_t*)_expr;
1879 exprval_t exprval;
1880 VARIANT_BOOL b;
1881 HRESULT hres;
1883 TRACE("\n");
1885 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1886 if(FAILED(hres))
1887 return hres;
1889 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
1890 exprval_release(&exprval);
1891 if(FAILED(hres))
1892 return hres;
1894 return return_bool(ret, !b);
1897 HRESULT left_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1899 FIXME("\n");
1900 return E_NOTIMPL;
1903 HRESULT right_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1905 FIXME("\n");
1906 return E_NOTIMPL;
1909 HRESULT right2_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1911 FIXME("\n");
1912 return E_NOTIMPL;
1915 /* ECMA-262 3rd Edition 11.13.1 */
1916 HRESULT assign_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1918 binary_expression_t *expr = (binary_expression_t*)_expr;
1919 exprval_t exprval, exprvalr;
1920 VARIANT rval;
1921 HRESULT hres;
1923 TRACE("\n");
1925 hres = expr_eval(ctx, expr->expression1, EXPR_NEWREF, ei, &exprval);
1926 if(FAILED(hres))
1927 return hres;
1929 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprvalr);
1930 if(SUCCEEDED(hres)) {
1931 hres = exprval_to_value(ctx->parser->script, &exprvalr, ei, &rval);
1932 exprval_release(&exprvalr);
1935 if(SUCCEEDED(hres))
1936 hres = put_value(ctx->parser->script, &exprval, &rval, ei);
1938 exprval_release(&exprval);
1939 if(FAILED(hres)) {
1940 VariantClear(&rval);
1941 return hres;
1944 ret->type = EXPRVAL_VARIANT;
1945 ret->u.var = rval;
1946 return S_OK;
1949 HRESULT assign_lshift_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1951 FIXME("\n");
1952 return E_NOTIMPL;
1955 HRESULT assign_rshift_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1957 FIXME("\n");
1958 return E_NOTIMPL;
1961 HRESULT assign_rrshift_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1963 FIXME("\n");
1964 return E_NOTIMPL;
1967 /* ECMA-262 3rd Edition 11.13.2 */
1968 HRESULT assign_add_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1970 binary_expression_t *expr = (binary_expression_t*)_expr;
1972 TRACE("\n");
1974 return assign_oper_eval(ctx, expr->expression1, expr->expression2, add_eval, ei, ret);
1977 HRESULT assign_sub_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1979 FIXME("\n");
1980 return E_NOTIMPL;
1983 HRESULT assign_mul_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1985 FIXME("\n");
1986 return E_NOTIMPL;
1989 HRESULT assign_div_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1991 FIXME("\n");
1992 return E_NOTIMPL;
1995 HRESULT assign_mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1997 FIXME("\n");
1998 return E_NOTIMPL;
2001 HRESULT assign_and_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2003 FIXME("\n");
2004 return E_NOTIMPL;
2007 HRESULT assign_or_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2009 FIXME("\n");
2010 return E_NOTIMPL;
2013 HRESULT assign_xor_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2015 FIXME("\n");
2016 return E_NOTIMPL;