push ab8f8bd6525a0f7af95fd0a2a63931cd3569f61f
[wine/hacks.git] / dlls / jscript / engine.c
blobc15b05b1e3e60ae366ad72a78358e598da753e83
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 void scope_release(scope_chain_t *scope)
125 if(--scope->ref)
126 return;
128 if(scope->next)
129 scope_release(scope->next);
131 IDispatchEx_Release(_IDispatchEx_(scope->obj));
132 heap_free(scope);
135 HRESULT create_exec_ctx(DispatchEx *var_disp, scope_chain_t *scope, exec_ctx_t **ret)
137 exec_ctx_t *ctx;
139 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
140 if(!ctx)
141 return E_OUTOFMEMORY;
143 IDispatchEx_AddRef(_IDispatchEx_(var_disp));
144 ctx->var_disp = var_disp;
146 if(scope) {
147 scope_addref(scope);
148 ctx->scope_chain = scope;
151 *ret = ctx;
152 return S_OK;
155 void exec_release(exec_ctx_t *ctx)
157 if(--ctx->ref)
158 return;
160 if(ctx->scope_chain)
161 scope_release(ctx->scope_chain);
162 if(ctx->var_disp)
163 IDispatchEx_Release(_IDispatchEx_(ctx->var_disp));
164 heap_free(ctx);
167 static HRESULT dispex_get_id(IDispatchEx *dispex, BSTR name, DWORD flags, DISPID *id)
169 *id = 0;
171 return IDispatchEx_GetDispID(dispex, name, flags|fdexNameCaseSensitive, id);
174 static HRESULT disp_get_id(IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
176 IDispatchEx *dispex;
177 HRESULT hres;
179 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
180 if(FAILED(hres)) {
181 TRACE("unsing IDispatch\n");
183 *id = 0;
184 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
187 hres = dispex_get_id(dispex, name, flags, id);
188 IDispatchEx_Release(dispex);
189 return hres;
192 /* ECMA-262 3rd Edition 8.7.2 */
193 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
195 if(ref->type != EXPRVAL_IDREF) {
196 FIXME("throw ReferemceError\n");
197 return E_FAIL;
200 return disp_propput(ref->u.idref.disp, ref->u.idref.id, ctx->lcid, v, ei, NULL/*FIXME*/);
203 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
205 IObjectIdentity *identity;
206 IUnknown *unk1, *unk2;
207 HRESULT hres;
209 if(disp1 == disp2) {
210 *ret = TRUE;
211 return S_OK;
214 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
215 if(FAILED(hres))
216 return hres;
218 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
219 if(FAILED(hres)) {
220 IUnknown_Release(unk1);
221 return hres;
224 if(unk1 == unk2) {
225 *ret = TRUE;
226 }else {
227 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
228 if(SUCCEEDED(hres)) {
229 hres = IObjectIdentity_IsEqualObject(identity, unk2);
230 IObjectIdentity_Release(identity);
231 *ret = hres == S_OK;
232 }else {
233 *ret = FALSE;
237 IUnknown_Release(unk1);
238 IUnknown_Release(unk2);
239 return S_OK;
242 static inline BOOL is_num_vt(enum VARENUM vt)
244 return vt == VT_I4 || vt == VT_R8;
247 static inline DOUBLE num_val(const VARIANT *v)
249 return V_VT(v) == VT_I4 ? V_I4(v) : V_R8(v);
252 /* ECMA-262 3rd Edition 11.9.6 */
253 HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
255 TRACE("\n");
257 if(V_VT(lval) != V_VT(rval)) {
258 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))) {
259 *ret = num_val(lval) == num_val(rval);
260 return S_OK;
263 *ret = FALSE;
264 return S_OK;
267 switch(V_VT(lval)) {
268 case VT_EMPTY:
269 case VT_NULL:
270 *ret = VARIANT_TRUE;
271 break;
272 case VT_I4:
273 *ret = V_I4(lval) == V_I4(rval);
274 break;
275 case VT_R8:
276 *ret = V_R8(lval) == V_R8(rval);
277 break;
278 case VT_BSTR:
279 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
280 break;
281 case VT_DISPATCH:
282 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
283 case VT_BOOL:
284 *ret = !V_BOOL(lval) == !V_BOOL(rval);
285 break;
286 default:
287 FIXME("unimplemented vt %d\n", V_VT(lval));
288 return E_NOTIMPL;
291 return S_OK;
294 static HRESULT literal_to_var(literal_t *literal, VARIANT *v)
296 V_VT(v) = literal->vt;
298 switch(V_VT(v)) {
299 case VT_EMPTY:
300 case VT_NULL:
301 break;
302 case VT_I4:
303 V_I4(v) = literal->u.lval;
304 break;
305 case VT_R8:
306 V_R8(v) = literal->u.dval;
307 break;
308 case VT_BSTR:
309 V_BSTR(v) = SysAllocString(literal->u.wstr);
310 break;
311 case VT_BOOL:
312 V_BOOL(v) = literal->u.bval;
313 break;
314 case VT_DISPATCH:
315 IDispatch_AddRef(literal->u.disp);
316 V_DISPATCH(v) = literal->u.disp;
317 break;
318 default:
319 ERR("wrong type %d\n", V_VT(v));
320 return E_NOTIMPL;
323 return S_OK;
326 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, jsexcept_t *ei, VARIANT *retv)
328 script_ctx_t *script = parser->script;
329 function_declaration_t *func;
330 parser_ctx_t *prev_parser;
331 VARIANT val, tmp;
332 statement_t *stat;
333 exec_ctx_t *prev_ctx;
334 return_type_t rt;
335 HRESULT hres = S_OK;
337 for(func = source->functions; func; func = func->next) {
338 DispatchEx *func_obj;
339 VARIANT var;
341 hres = create_source_function(parser, func->parameter_list, func->source_elements, ctx->scope_chain, &func_obj);
342 if(FAILED(hres))
343 return hres;
345 V_VT(&var) = VT_DISPATCH;
346 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(func_obj);
347 hres = jsdisp_propput_name(ctx->var_disp, func->identifier, script->lcid, &var, ei, NULL);
348 IDispatchEx_Release(_IDispatchEx_(func_obj));
349 if(FAILED(hres))
350 return hres;
353 prev_ctx = script->exec_ctx;
354 script->exec_ctx = ctx;
356 prev_parser = ctx->parser;
357 ctx->parser = parser;
359 V_VT(&val) = VT_EMPTY;
360 memset(&rt, 0, sizeof(rt));
361 rt.type = RT_NORMAL;
363 for(stat = source->statement; stat; stat = stat->next) {
364 hres = stat_eval(ctx, stat, &rt, &tmp);
365 if(FAILED(hres))
366 break;
368 VariantClear(&val);
369 val = tmp;
370 if(rt.type != RT_NORMAL)
371 break;
374 script->exec_ctx = prev_ctx;
375 ctx->parser = prev_parser;
377 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
378 FIXME("wrong rt %d\n", rt.type);
379 hres = E_FAIL;
382 *ei = rt.ei;
383 if(FAILED(hres)) {
384 VariantClear(&val);
385 return hres;
388 if(retv)
389 *retv = val;
390 else
391 VariantClear(&val);
392 return S_OK;
395 /* ECMA-262 3rd Edition 10.1.4 */
396 static HRESULT identifier_eval(exec_ctx_t *ctx, BSTR identifier, DWORD flags, exprval_t *ret)
398 scope_chain_t *scope;
399 named_item_t *item;
400 DISPID id = 0;
401 HRESULT hres;
403 TRACE("%s\n", debugstr_w(identifier));
405 for(scope = ctx->scope_chain; scope; scope = scope->next) {
406 hres = dispex_get_id(_IDispatchEx_(scope->obj), identifier, 0, &id);
407 if(SUCCEEDED(hres))
408 break;
411 if(scope) {
412 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(scope->obj), id);
413 return S_OK;
416 hres = dispex_get_id(_IDispatchEx_(ctx->parser->script->global), identifier, 0, &id);
417 if(SUCCEEDED(hres)) {
418 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->global), id);
419 return S_OK;
422 for(item = ctx->parser->script->named_items; item; item = item->next) {
423 hres = disp_get_id(item->disp, identifier, 0, &id);
424 if(SUCCEEDED(hres))
425 break;
428 if(item) {
429 exprval_set_idref(ret, (IDispatch*)item->disp, id);
430 return S_OK;
433 hres = dispex_get_id(_IDispatchEx_(ctx->parser->script->script_disp), identifier, 0, &id);
434 if(SUCCEEDED(hres)) {
435 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->parser->script->script_disp), id);
436 return S_OK;
439 if(flags & EXPR_NEWREF) {
440 hres = dispex_get_id(_IDispatchEx_(ctx->var_disp), identifier, fdexNameEnsure, &id);
441 if(FAILED(hres))
442 return hres;
444 exprval_set_idref(ret, (IDispatch*)_IDispatchEx_(ctx->var_disp), id);
445 return S_OK;
448 WARN("Could not find identifier %s\n", debugstr_w(identifier));
449 return E_FAIL;
452 HRESULT block_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
454 FIXME("\n");
455 return E_NOTIMPL;
458 /* ECMA-262 3rd Edition 12.2 */
459 static HRESULT variable_list_eval(exec_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
461 variable_declaration_t *iter;
462 HRESULT hres;
464 for(iter = var_list; iter; iter = iter->next) {
465 VARIANT val;
467 if(iter->expr) {
468 exprval_t exprval;
470 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
471 if(FAILED(hres))
472 break;
474 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
475 exprval_release(&exprval);
476 if(FAILED(hres))
477 break;
478 }else {
479 V_VT(&val) = VT_EMPTY;
482 hres = jsdisp_propput_name(ctx->var_disp, iter->identifier, ctx->parser->script->lcid, &val, ei, NULL/*FIXME*/);
483 VariantClear(&val);
484 if(FAILED(hres))
485 break;
488 return hres;
491 /* ECMA-262 3rd Edition 12.2 */
492 HRESULT var_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
494 var_statement_t *stat = (var_statement_t*)_stat;
495 HRESULT hres;
497 TRACE("\n");
499 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
500 if(FAILED(hres))
501 return hres;
503 V_VT(ret) = VT_EMPTY;
504 return S_OK;
507 /* ECMA-262 3rd Edition 12.3 */
508 HRESULT empty_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
510 TRACE("\n");
512 V_VT(ret) = VT_EMPTY;
513 return S_OK;
516 /* ECMA-262 3rd Edition 12.4 */
517 HRESULT expression_statement_eval(exec_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
519 expression_statement_t *stat = (expression_statement_t*)_stat;
520 exprval_t exprval;
521 VARIANT val;
522 HRESULT hres;
524 TRACE("\n");
526 hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
527 if(FAILED(hres))
528 return hres;
530 hres = exprval_to_value(ctx->parser->script, &exprval, &rt->ei, &val);
531 exprval_release(&exprval);
532 if(FAILED(hres))
533 return hres;
535 *ret = val;
536 TRACE("= %s\n", debugstr_variant(ret));
537 return S_OK;
540 HRESULT if_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
542 FIXME("\n");
543 return E_NOTIMPL;
546 HRESULT dowhile_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
548 FIXME("\n");
549 return E_NOTIMPL;
552 HRESULT while_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
554 FIXME("\n");
555 return E_NOTIMPL;
558 HRESULT for_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
560 FIXME("\n");
561 return E_NOTIMPL;
564 HRESULT forin_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
566 FIXME("\n");
567 return E_NOTIMPL;
570 HRESULT continue_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
572 FIXME("\n");
573 return E_NOTIMPL;
576 HRESULT break_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
578 FIXME("\n");
579 return E_NOTIMPL;
582 HRESULT return_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
584 FIXME("\n");
585 return E_NOTIMPL;
588 HRESULT with_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
590 FIXME("\n");
591 return E_NOTIMPL;
594 HRESULT labelled_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
596 FIXME("\n");
597 return E_NOTIMPL;
600 HRESULT switch_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
602 FIXME("\n");
603 return E_NOTIMPL;
606 HRESULT throw_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
608 FIXME("\n");
609 return E_NOTIMPL;
612 HRESULT try_statement_eval(exec_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
614 FIXME("\n");
615 return E_NOTIMPL;
618 static HRESULT return_bool(exprval_t *ret, DWORD b)
620 ret->type = EXPRVAL_VARIANT;
621 V_VT(&ret->u.var) = VT_BOOL;
622 V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
624 return S_OK;
627 static HRESULT get_binary_expr_values(exec_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
629 exprval_t exprval;
630 HRESULT hres;
632 hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
633 if(FAILED(hres))
634 return hres;
636 hres = exprval_to_value(ctx->parser->script, &exprval, ei, lval);
637 exprval_release(&exprval);
638 if(FAILED(hres))
639 return hres;
641 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
642 if(SUCCEEDED(hres)) {
643 hres = exprval_to_value(ctx->parser->script, &exprval, ei, rval);
644 exprval_release(&exprval);
647 if(FAILED(hres)) {
648 VariantClear(lval);
649 return hres;
652 return S_OK;
655 HRESULT function_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
657 FIXME("\n");
658 return E_NOTIMPL;
661 HRESULT conditional_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
663 FIXME("\n");
664 return E_NOTIMPL;
667 HRESULT array_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
669 FIXME("\n");
670 return E_NOTIMPL;
673 /* ECMA-262 3rd Edition 11.2.1 */
674 HRESULT member_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
676 member_expression_t *expr = (member_expression_t*)_expr;
677 IDispatch *obj = NULL;
678 exprval_t exprval;
679 VARIANT member;
680 DISPID id;
681 BSTR str;
682 HRESULT hres;
684 TRACE("\n");
686 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
687 if(FAILED(hres))
688 return hres;
690 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &member);
691 exprval_release(&exprval);
692 if(FAILED(hres))
693 return hres;
695 hres = to_object(ctx, &member, &obj);
696 VariantClear(&member);
697 if(FAILED(hres))
698 return hres;
700 str = SysAllocString(expr->identifier);
701 if(flags & EXPR_STRREF) {
702 ret->type = EXPRVAL_NAMEREF;
703 ret->u.nameref.disp = obj;
704 ret->u.nameref.name = str;
705 return S_OK;
708 hres = disp_get_id(obj, str, flags & EXPR_NEW ? fdexNameEnsure : 0, &id);
709 SysFreeString(str);
710 if(SUCCEEDED(hres)) {
711 exprval_set_idref(ret, obj, id);
712 }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
713 exprval_init(ret);
714 hres = S_OK;
717 IDispatch_Release(obj);
718 return hres;
721 static void free_dp(DISPPARAMS *dp)
723 DWORD i;
725 for(i=0; i < dp->cArgs; i++)
726 VariantClear(dp->rgvarg+i);
727 heap_free(dp->rgvarg);
730 static HRESULT args_to_param(exec_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
732 VARIANTARG *vargs;
733 exprval_t exprval;
734 argument_t *iter;
735 DWORD cnt = 0, i;
736 HRESULT hres = S_OK;
738 memset(dp, 0, sizeof(*dp));
740 for(iter = args; iter; iter = iter->next)
741 cnt++;
742 if(!cnt)
743 return S_OK;
745 vargs = heap_alloc_zero(cnt * sizeof(*vargs));
746 if(!vargs)
747 return E_OUTOFMEMORY;
749 for(i = cnt, iter = args; iter; iter = iter->next) {
750 hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
751 if(FAILED(hres))
752 break;
754 hres = exprval_to_value(ctx->parser->script, &exprval, ei, vargs + (--i));
755 exprval_release(&exprval);
756 if(FAILED(hres))
757 break;
760 if(FAILED(hres)) {
761 free_dp(dp);
762 return hres;
765 dp->rgvarg = vargs;
766 dp->cArgs = cnt;
767 return S_OK;
770 HRESULT member_new_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
772 FIXME("\n");
773 return E_NOTIMPL;
776 HRESULT call_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
778 call_expression_t *expr = (call_expression_t*)_expr;
779 VARIANT func, var;
780 exprval_t exprval;
781 DISPPARAMS dp;
782 HRESULT hres;
784 TRACE("\n");
786 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
787 if(FAILED(hres))
788 return hres;
790 hres = args_to_param(ctx, expr->argument_list, ei, &dp);
791 if(SUCCEEDED(hres)) {
792 switch(exprval.type) {
793 case EXPRVAL_IDREF:
794 hres = disp_call(exprval.u.idref.disp, exprval.u.idref.id, ctx->parser->script->lcid, DISPATCH_METHOD,
795 &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
796 if(flags & EXPR_NOVAL)
797 V_VT(&var) = VT_EMPTY;
798 break;
799 default:
800 FIXME("unimplemented type %d\n", V_VT(&func));
801 hres = E_NOTIMPL;
804 free_dp(&dp);
807 exprval_release(&exprval);
808 if(FAILED(hres))
809 return hres;
811 TRACE("= %s\n", debugstr_variant(&var));
812 ret->type = EXPRVAL_VARIANT;
813 ret->u.var = var;
814 return S_OK;
817 HRESULT this_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
819 FIXME("\n");
820 return E_NOTIMPL;
823 /* ECMA-262 3rd Edition 10.1.4 */
824 HRESULT identifier_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
826 identifier_expression_t *expr = (identifier_expression_t*)_expr;
827 BSTR identifier;
828 HRESULT hres;
830 TRACE("\n");
832 identifier = SysAllocString(expr->identifier);
833 if(!identifier)
834 return E_OUTOFMEMORY;
836 hres = identifier_eval(ctx, identifier, flags, ret);
838 SysFreeString(identifier);
839 return hres;
842 /* ECMA-262 3rd Edition 7.8 */
843 HRESULT literal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
845 literal_expression_t *expr = (literal_expression_t*)_expr;
846 VARIANT var;
847 HRESULT hres;
849 TRACE("\n");
851 hres = literal_to_var(expr->literal, &var);
852 if(FAILED(hres))
853 return hres;
855 ret->type = EXPRVAL_VARIANT;
856 ret->u.var = var;
857 return S_OK;
860 HRESULT array_literal_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
862 FIXME("\n");
863 return E_NOTIMPL;
866 HRESULT property_value_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
868 FIXME("\n");
869 return E_NOTIMPL;
872 HRESULT comma_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
874 FIXME("\n");
875 return E_NOTIMPL;
878 HRESULT logical_or_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
880 FIXME("\n");
881 return E_NOTIMPL;
884 HRESULT logical_and_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
886 FIXME("\n");
887 return E_NOTIMPL;
890 HRESULT binary_or_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
892 FIXME("\n");
893 return E_NOTIMPL;
896 HRESULT binary_xor_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
898 FIXME("\n");
899 return E_NOTIMPL;
902 HRESULT binary_and_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
904 FIXME("\n");
905 return E_NOTIMPL;
908 HRESULT instanceof_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
910 FIXME("\n");
911 return E_NOTIMPL;
914 HRESULT in_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
916 FIXME("\n");
917 return E_NOTIMPL;
920 HRESULT add_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
922 FIXME("\n");
923 return E_NOTIMPL;
926 HRESULT sub_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
928 FIXME("\n");
929 return E_NOTIMPL;
932 HRESULT mul_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
934 FIXME("\n");
935 return E_NOTIMPL;
938 HRESULT div_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
940 FIXME("\n");
941 return E_NOTIMPL;
944 HRESULT mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
946 FIXME("\n");
947 return E_NOTIMPL;
950 HRESULT delete_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
952 FIXME("\n");
953 return E_NOTIMPL;
956 HRESULT void_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
958 FIXME("\n");
959 return E_NOTIMPL;
962 HRESULT typeof_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
964 unary_expression_t *expr = (unary_expression_t*)_expr;
965 const WCHAR *str;
966 exprval_t exprval;
967 VARIANT val;
968 HRESULT hres;
970 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
971 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
972 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
973 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
974 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
975 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
977 TRACE("\n");
979 hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
980 if(FAILED(hres))
981 return hres;
983 hres = exprval_to_value(ctx->parser->script, &exprval, ei, &val);
984 exprval_release(&exprval);
985 if(FAILED(hres))
986 return hres;
988 switch(V_VT(&val)) {
989 case VT_EMPTY:
990 str = undefinedW;
991 break;
992 case VT_NULL:
993 str = objectW;
994 break;
995 case VT_BOOL:
996 str = booleanW;
997 break;
998 case VT_I4:
999 case VT_R8:
1000 str = numberW;
1001 break;
1002 case VT_BSTR:
1003 str = stringW;
1004 break;
1005 case VT_DISPATCH: {
1006 DispatchEx *dispex;
1008 dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val));
1009 if(dispex) {
1010 str = dispex->builtin_info->class == JSCLASS_FUNCTION ? functionW : objectW;
1011 IDispatchEx_Release(_IDispatchEx_(dispex));
1012 }else {
1013 str = objectW;
1015 break;
1017 default:
1018 FIXME("unhandled vt %d\n", V_VT(&val));
1019 hres = E_NOTIMPL;
1022 VariantClear(&val);
1023 if(FAILED(hres))
1024 return hres;
1026 ret->type = EXPRVAL_VARIANT;
1027 V_VT(&ret->u.var) = VT_BSTR;
1028 V_BSTR(&ret->u.var) = SysAllocString(str);
1029 return S_OK;
1032 HRESULT minus_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1034 FIXME("\n");
1035 return E_NOTIMPL;
1038 HRESULT plus_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1040 FIXME("\n");
1041 return E_NOTIMPL;
1044 HRESULT post_increment_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1046 FIXME("\n");
1047 return E_NOTIMPL;
1050 HRESULT post_decrement_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1052 FIXME("\n");
1053 return E_NOTIMPL;
1056 HRESULT pre_increment_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1058 FIXME("\n");
1059 return E_NOTIMPL;
1062 HRESULT pre_decrement_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1064 FIXME("\n");
1065 return E_NOTIMPL;
1068 HRESULT new_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1070 FIXME("\n");
1071 return E_NOTIMPL;
1074 HRESULT equal_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1076 FIXME("\n");
1077 return E_NOTIMPL;
1080 /* ECMA-262 3rd Edition 11.9.4 */
1081 HRESULT equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1083 binary_expression_t *expr = (binary_expression_t*)_expr;
1084 VARIANT rval, lval;
1085 BOOL b;
1086 HRESULT hres;
1088 TRACE("\n");
1090 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
1091 if(FAILED(hres))
1092 return hres;
1094 hres = equal2_values(&rval, &lval, &b);
1095 if(FAILED(hres))
1096 return hres;
1098 return return_bool(ret, b);
1101 HRESULT not_equal_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1104 FIXME("\n");
1105 return E_NOTIMPL;
1108 /* ECMA-262 3rd Edition 11.9.5 */
1109 HRESULT not_equal2_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1111 binary_expression_t *expr = (binary_expression_t*)_expr;
1112 VARIANT rval, lval;
1113 BOOL b;
1114 HRESULT hres;
1116 TRACE("\n");
1118 hres = get_binary_expr_values(ctx, expr, ei, &rval, &lval);
1119 if(FAILED(hres))
1120 return hres;
1122 hres = equal2_values(&rval, &lval, &b);
1123 if(FAILED(hres))
1124 return hres;
1126 return return_bool(ret, !b);
1129 HRESULT less_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1131 FIXME("\n");
1132 return E_NOTIMPL;
1135 HRESULT lesseq_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1137 FIXME("\n");
1138 return E_NOTIMPL;
1141 HRESULT greater_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1143 FIXME("\n");
1144 return E_NOTIMPL;
1147 HRESULT greatereq_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1149 FIXME("\n");
1150 return E_NOTIMPL;
1153 HRESULT binary_negation_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1155 FIXME("\n");
1156 return E_NOTIMPL;
1159 /* ECMA-262 3rd Edition 11.4.9 */
1160 HRESULT logical_negation_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1162 unary_expression_t *expr = (unary_expression_t*)_expr;
1163 exprval_t exprval;
1164 VARIANT_BOOL b;
1165 HRESULT hres;
1167 TRACE("\n");
1169 hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1170 if(FAILED(hres))
1171 return hres;
1173 hres = exprval_to_boolean(ctx->parser->script, &exprval, ei, &b);
1174 exprval_release(&exprval);
1175 if(FAILED(hres))
1176 return hres;
1178 return return_bool(ret, !b);
1181 HRESULT left_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1183 FIXME("\n");
1184 return E_NOTIMPL;
1187 HRESULT right_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1189 FIXME("\n");
1190 return E_NOTIMPL;
1193 HRESULT right2_shift_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1195 FIXME("\n");
1196 return E_NOTIMPL;
1199 /* ECMA-262 3rd Edition 11.13.1 */
1200 HRESULT assign_expression_eval(exec_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1202 binary_expression_t *expr = (binary_expression_t*)_expr;
1203 exprval_t exprval, exprvalr;
1204 VARIANT rval;
1205 HRESULT hres;
1207 TRACE("\n");
1209 hres = expr_eval(ctx, expr->expression1, EXPR_NEWREF, ei, &exprval);
1210 if(FAILED(hres))
1211 return hres;
1213 hres = expr_eval(ctx, expr->expression2, 0, ei, &exprvalr);
1214 if(SUCCEEDED(hres)) {
1215 hres = exprval_to_value(ctx->parser->script, &exprvalr, ei, &rval);
1216 exprval_release(&exprvalr);
1219 if(SUCCEEDED(hres))
1220 hres = put_value(ctx->parser->script, &exprval, &rval, ei);
1222 exprval_release(&exprval);
1223 if(FAILED(hres)) {
1224 VariantClear(&rval);
1225 return hres;
1228 ret->type = EXPRVAL_VARIANT;
1229 ret->u.var = rval;
1230 return S_OK;
1233 HRESULT assign_lshift_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1235 FIXME("\n");
1236 return E_NOTIMPL;
1239 HRESULT assign_rshift_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1241 FIXME("\n");
1242 return E_NOTIMPL;
1245 HRESULT assign_rrshift_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1247 FIXME("\n");
1248 return E_NOTIMPL;
1251 HRESULT assign_add_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1253 FIXME("\n");
1254 return E_NOTIMPL;
1257 HRESULT assign_sub_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1259 FIXME("\n");
1260 return E_NOTIMPL;
1263 HRESULT assign_mul_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1265 FIXME("\n");
1266 return E_NOTIMPL;
1269 HRESULT assign_div_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1271 FIXME("\n");
1272 return E_NOTIMPL;
1275 HRESULT assign_mod_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1277 FIXME("\n");
1278 return E_NOTIMPL;
1281 HRESULT assign_and_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1283 FIXME("\n");
1284 return E_NOTIMPL;
1287 HRESULT assign_or_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1289 FIXME("\n");
1290 return E_NOTIMPL;
1293 HRESULT assign_xor_expression_eval(exec_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1295 FIXME("\n");
1296 return E_NOTIMPL;