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
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
)
44 if(V_VT(&val
->u
.var
) != VT_EMPTY
)
45 VariantClear(&val
->u
.var
);
49 IDispatch_Release(val
->u
.idref
.disp
);
52 if(val
->u
.nameref
.disp
)
53 IDispatch_Release(val
->u
.nameref
.disp
);
54 SysFreeString(val
->u
.nameref
.name
);
58 /* ECMA-262 3rd Edition 8.7.1 */
59 static HRESULT
exprval_value(script_ctx_t
*ctx
, exprval_t
*val
, jsexcept_t
*ei
, VARIANT
*ret
)
65 return VariantCopy(ret
, &val
->u
.var
);
67 if(!val
->u
.idref
.disp
) {
68 FIXME("throw ReferenceError\n");
72 return disp_propget(val
->u
.idref
.disp
, val
->u
.idref
.id
, ctx
->lcid
, ret
, ei
, NULL
/*FIXME*/);
74 ERR("type %d\n", val
->type
);
79 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*val
, jsexcept_t
*ei
, VARIANT
*ret
)
81 if(val
->type
== EXPRVAL_VARIANT
) {
83 V_VT(&val
->u
.var
) = VT_EMPTY
;
87 return exprval_value(ctx
, val
, ei
, ret
);
90 static HRESULT
exprval_to_boolean(script_ctx_t
*ctx
, exprval_t
*exprval
, jsexcept_t
*ei
, VARIANT_BOOL
*b
)
92 if(exprval
->type
!= EXPRVAL_VARIANT
) {
96 hres
= exprval_to_value(ctx
, exprval
, ei
, &val
);
100 hres
= to_boolean(&val
, b
);
105 return to_boolean(&exprval
->u
.var
, b
);
108 static void exprval_init(exprval_t
*val
)
110 val
->type
= EXPRVAL_VARIANT
;
111 V_VT(&val
->u
.var
) = VT_EMPTY
;
114 static void exprval_set_idref(exprval_t
*val
, IDispatch
*disp
, DISPID id
)
116 val
->type
= EXPRVAL_IDREF
;
117 val
->u
.idref
.disp
= disp
;
118 val
->u
.idref
.id
= id
;
121 IDispatch_AddRef(disp
);
124 HRESULT
scope_push(scope_chain_t
*scope
, DispatchEx
*obj
, scope_chain_t
**ret
)
126 scope_chain_t
*new_scope
;
128 new_scope
= heap_alloc(sizeof(scope_chain_t
));
130 return E_OUTOFMEMORY
;
134 IDispatchEx_AddRef(_IDispatchEx_(obj
));
135 new_scope
->obj
= obj
;
139 new_scope
->next
= scope
;
141 new_scope
->next
= NULL
;
148 static void scope_pop(scope_chain_t
**scope
)
157 void scope_release(scope_chain_t
*scope
)
163 scope_release(scope
->next
);
165 IDispatchEx_Release(_IDispatchEx_(scope
->obj
));
169 HRESULT
create_exec_ctx(IDispatch
*this_obj
, DispatchEx
*var_disp
, scope_chain_t
*scope
, exec_ctx_t
**ret
)
173 ctx
= heap_alloc_zero(sizeof(exec_ctx_t
));
175 return E_OUTOFMEMORY
;
177 IDispatch_AddRef(this_obj
);
178 ctx
->this_obj
= this_obj
;
180 IDispatchEx_AddRef(_IDispatchEx_(var_disp
));
181 ctx
->var_disp
= var_disp
;
185 ctx
->scope_chain
= scope
;
192 void exec_release(exec_ctx_t
*ctx
)
198 scope_release(ctx
->scope_chain
);
200 IDispatchEx_Release(_IDispatchEx_(ctx
->var_disp
));
202 IDispatch_Release(ctx
->this_obj
);
206 static HRESULT
dispex_get_id(IDispatchEx
*dispex
, BSTR name
, DWORD flags
, DISPID
*id
)
210 return IDispatchEx_GetDispID(dispex
, name
, flags
|fdexNameCaseSensitive
, id
);
213 static HRESULT
disp_get_id(IDispatch
*disp
, BSTR name
, DWORD flags
, DISPID
*id
)
218 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
220 TRACE("unsing IDispatch\n");
223 return IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &name
, 1, 0, id
);
226 hres
= dispex_get_id(dispex
, name
, flags
, id
);
227 IDispatchEx_Release(dispex
);
231 /* ECMA-262 3rd Edition 8.7.2 */
232 static HRESULT
put_value(script_ctx_t
*ctx
, exprval_t
*ref
, VARIANT
*v
, jsexcept_t
*ei
)
234 if(ref
->type
!= EXPRVAL_IDREF
) {
235 FIXME("throw ReferemceError\n");
239 return disp_propput(ref
->u
.idref
.disp
, ref
->u
.idref
.id
, ctx
->lcid
, v
, ei
, NULL
/*FIXME*/);
242 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
244 IObjectIdentity
*identity
;
245 IUnknown
*unk1
, *unk2
;
253 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
257 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
259 IUnknown_Release(unk1
);
266 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
267 if(SUCCEEDED(hres
)) {
268 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
269 IObjectIdentity_Release(identity
);
276 IUnknown_Release(unk1
);
277 IUnknown_Release(unk2
);
281 static inline BOOL
is_num_vt(enum VARENUM vt
)
283 return vt
== VT_I4
|| vt
== VT_R8
;
286 static inline DOUBLE
num_val(const VARIANT
*v
)
288 return V_VT(v
) == VT_I4
? V_I4(v
) : V_R8(v
);
291 static inline void num_set_val(VARIANT
*v
, DOUBLE d
)
293 if(d
== (DOUBLE
)(INT
)d
) {
302 /* ECMA-262 3rd Edition 11.9.6 */
303 HRESULT
equal2_values(VARIANT
*lval
, VARIANT
*rval
, BOOL
*ret
)
307 if(V_VT(lval
) != V_VT(rval
)) {
308 if(is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
))) {
309 *ret
= num_val(lval
) == num_val(rval
);
323 *ret
= V_I4(lval
) == V_I4(rval
);
326 *ret
= V_R8(lval
) == V_R8(rval
);
329 *ret
= !strcmpW(V_BSTR(lval
), V_BSTR(rval
));
332 return disp_cmp(V_DISPATCH(lval
), V_DISPATCH(rval
), ret
);
334 *ret
= !V_BOOL(lval
) == !V_BOOL(rval
);
337 FIXME("unimplemented vt %d\n", V_VT(lval
));
344 static HRESULT
literal_to_var(literal_t
*literal
, VARIANT
*v
)
346 V_VT(v
) = literal
->vt
;
353 V_I4(v
) = literal
->u
.lval
;
356 V_R8(v
) = literal
->u
.dval
;
359 V_BSTR(v
) = SysAllocString(literal
->u
.wstr
);
362 V_BOOL(v
) = literal
->u
.bval
;
365 IDispatch_AddRef(literal
->u
.disp
);
366 V_DISPATCH(v
) = literal
->u
.disp
;
369 ERR("wrong type %d\n", V_VT(v
));
376 HRESULT
exec_source(exec_ctx_t
*ctx
, parser_ctx_t
*parser
, source_elements_t
*source
, jsexcept_t
*ei
, VARIANT
*retv
)
378 script_ctx_t
*script
= parser
->script
;
379 function_declaration_t
*func
;
380 parser_ctx_t
*prev_parser
;
383 exec_ctx_t
*prev_ctx
;
387 for(func
= source
->functions
; func
; func
= func
->next
) {
388 DispatchEx
*func_obj
;
391 hres
= create_source_function(parser
, func
->parameter_list
, func
->source_elements
, ctx
->scope_chain
, &func_obj
);
395 V_VT(&var
) = VT_DISPATCH
;
396 V_DISPATCH(&var
) = (IDispatch
*)_IDispatchEx_(func_obj
);
397 hres
= jsdisp_propput_name(ctx
->var_disp
, func
->identifier
, script
->lcid
, &var
, ei
, NULL
);
398 IDispatchEx_Release(_IDispatchEx_(func_obj
));
403 prev_ctx
= script
->exec_ctx
;
404 script
->exec_ctx
= ctx
;
406 prev_parser
= ctx
->parser
;
407 ctx
->parser
= parser
;
409 V_VT(&val
) = VT_EMPTY
;
410 memset(&rt
, 0, sizeof(rt
));
413 for(stat
= source
->statement
; stat
; stat
= stat
->next
) {
414 hres
= stat_eval(ctx
, stat
, &rt
, &tmp
);
420 if(rt
.type
!= RT_NORMAL
)
424 script
->exec_ctx
= prev_ctx
;
425 ctx
->parser
= prev_parser
;
427 if(rt
.type
!= RT_NORMAL
&& rt
.type
!= RT_RETURN
) {
428 FIXME("wrong rt %d\n", rt
.type
);
445 /* ECMA-262 3rd Edition 10.1.4 */
446 static HRESULT
identifier_eval(exec_ctx_t
*ctx
, BSTR identifier
, DWORD flags
, exprval_t
*ret
)
448 scope_chain_t
*scope
;
453 TRACE("%s\n", debugstr_w(identifier
));
455 for(scope
= ctx
->scope_chain
; scope
; scope
= scope
->next
) {
456 hres
= dispex_get_id(_IDispatchEx_(scope
->obj
), identifier
, 0, &id
);
462 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(scope
->obj
), id
);
466 hres
= dispex_get_id(_IDispatchEx_(ctx
->parser
->script
->global
), identifier
, 0, &id
);
467 if(SUCCEEDED(hres
)) {
468 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(ctx
->parser
->script
->global
), id
);
472 for(item
= ctx
->parser
->script
->named_items
; item
; item
= item
->next
) {
473 hres
= disp_get_id(item
->disp
, identifier
, 0, &id
);
479 exprval_set_idref(ret
, (IDispatch
*)item
->disp
, id
);
483 hres
= dispex_get_id(_IDispatchEx_(ctx
->parser
->script
->script_disp
), identifier
, 0, &id
);
484 if(SUCCEEDED(hres
)) {
485 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(ctx
->parser
->script
->script_disp
), id
);
489 if(flags
& EXPR_NEWREF
) {
490 hres
= dispex_get_id(_IDispatchEx_(ctx
->var_disp
), identifier
, fdexNameEnsure
, &id
);
494 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(ctx
->var_disp
), id
);
498 WARN("Could not find identifier %s\n", debugstr_w(identifier
));
502 /* ECMA-262 3rd Edition 12.1 */
503 HRESULT
block_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
505 block_statement_t
*stat
= (block_statement_t
*)_stat
;
512 V_VT(&val
) = VT_EMPTY
;
513 for(iter
= stat
->stat_list
; iter
; iter
= iter
->next
) {
514 hres
= stat_eval(ctx
, iter
, rt
, &tmp
);
520 if(rt
->type
!= RT_NORMAL
)
533 /* ECMA-262 3rd Edition 12.2 */
534 static HRESULT
variable_list_eval(exec_ctx_t
*ctx
, variable_declaration_t
*var_list
, jsexcept_t
*ei
)
536 variable_declaration_t
*iter
;
537 HRESULT hres
= E_FAIL
;
539 for(iter
= var_list
; iter
; iter
= iter
->next
) {
545 hres
= expr_eval(ctx
, iter
->expr
, 0, ei
, &exprval
);
549 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
550 exprval_release(&exprval
);
554 V_VT(&val
) = VT_EMPTY
;
557 hres
= jsdisp_propput_name(ctx
->var_disp
, iter
->identifier
, ctx
->parser
->script
->lcid
, &val
, ei
, NULL
/*FIXME*/);
566 /* ECMA-262 3rd Edition 12.2 */
567 HRESULT
var_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
569 var_statement_t
*stat
= (var_statement_t
*)_stat
;
574 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
578 V_VT(ret
) = VT_EMPTY
;
582 /* ECMA-262 3rd Edition 12.3 */
583 HRESULT
empty_statement_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
587 V_VT(ret
) = VT_EMPTY
;
591 /* ECMA-262 3rd Edition 12.4 */
592 HRESULT
expression_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
594 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
601 hres
= expr_eval(ctx
, stat
->expr
, EXPR_NOVAL
, &rt
->ei
, &exprval
);
605 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
606 exprval_release(&exprval
);
611 TRACE("= %s\n", debugstr_variant(ret
));
615 /* ECMA-262 3rd Edition 12.5 */
616 HRESULT
if_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
618 if_statement_t
*stat
= (if_statement_t
*)_stat
;
625 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
629 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, &rt
->ei
, &b
);
630 exprval_release(&exprval
);
635 hres
= stat_eval(ctx
, stat
->if_stat
, rt
, ret
);
636 else if(stat
->else_stat
)
637 hres
= stat_eval(ctx
, stat
->else_stat
, rt
, ret
);
639 V_VT(ret
) = VT_EMPTY
;
644 HRESULT
dowhile_statement_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
650 HRESULT
while_statement_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
656 HRESULT
for_statement_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
662 HRESULT
forin_statement_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
668 HRESULT
continue_statement_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
674 /* ECMA-262 3rd Edition 12.8 */
675 HRESULT
break_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
677 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
681 if(stat
->identifier
) {
682 FIXME("indentifier not implemented\n");
687 V_VT(ret
) = VT_EMPTY
;
691 /* ECMA-262 3rd Edition 12.9 */
692 HRESULT
return_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
694 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
702 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
706 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, ret
);
707 exprval_release(&exprval
);
711 V_VT(ret
) = VT_EMPTY
;
714 TRACE("= %s\n", debugstr_variant(ret
));
715 rt
->type
= RT_RETURN
;
719 HRESULT
with_statement_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
725 HRESULT
labelled_statement_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
731 /* ECMA-262 3rd Edition 12.13 */
732 HRESULT
switch_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
734 switch_statement_t
*stat
= (switch_statement_t
*)_stat
;
735 case_clausule_t
*iter
, *default_clausule
= NULL
;
736 statement_t
*stat_iter
;
744 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
748 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
749 exprval_release(&exprval
);
753 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
755 default_clausule
= iter
;
759 hres
= expr_eval(ctx
, iter
->expr
, 0, &rt
->ei
, &exprval
);
763 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &cval
);
764 exprval_release(&exprval
);
768 hres
= equal2_values(&val
, &cval
, &b
);
770 if(FAILED(hres
) || b
)
779 iter
= default_clausule
;
781 V_VT(&val
) = VT_EMPTY
;
785 for(stat_iter
= iter
->stat
; stat_iter
; stat_iter
= stat_iter
->next
) {
786 hres
= stat_eval(ctx
, stat_iter
, rt
, &tmp
);
793 if(rt
->type
!= RT_NORMAL
)
803 if(rt
->type
== RT_BREAK
)
804 rt
->type
= RT_NORMAL
;
810 /* ECMA-262 3rd Edition 12.13 */
811 HRESULT
throw_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
813 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
820 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
824 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
825 exprval_release(&exprval
);
830 return DISP_E_EXCEPTION
;
833 /* ECMA-262 3rd Edition 12.14 */
834 static HRESULT
catch_eval(exec_ctx_t
*ctx
, catch_block_t
*block
, return_type_t
*rt
, VARIANT
*ret
)
836 DispatchEx
*var_disp
;
841 memset(&rt
->ei
, 0, sizeof(jsexcept_t
));
843 hres
= create_dispex(ctx
->parser
->script
, NULL
, NULL
, &var_disp
);
844 if(SUCCEEDED(hres
)) {
845 hres
= jsdisp_propput_name(var_disp
, block
->identifier
, ctx
->parser
->script
->lcid
,
846 &ex
, &rt
->ei
, NULL
/*FIXME*/);
847 if(SUCCEEDED(hres
)) {
848 hres
= scope_push(ctx
->scope_chain
, var_disp
, &ctx
->scope_chain
);
849 if(SUCCEEDED(hres
)) {
850 hres
= stat_eval(ctx
, block
->statement
, rt
, &val
);
851 scope_pop(&ctx
->scope_chain
);
855 jsdisp_release(var_disp
);
866 /* ECMA-262 3rd Edition 12.14 */
867 HRESULT
try_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
869 try_statement_t
*stat
= (try_statement_t
*)_stat
;
875 hres
= stat_eval(ctx
, stat
->try_statement
, rt
, &val
);
877 TRACE("EXCEPTION\n");
878 if(!stat
->catch_block
)
881 hres
= catch_eval(ctx
, stat
->catch_block
, rt
, &val
);
886 if(stat
->finally_statement
) {
888 hres
= stat_eval(ctx
, stat
->finally_statement
, rt
, &val
);
897 static HRESULT
return_bool(exprval_t
*ret
, DWORD b
)
899 ret
->type
= EXPRVAL_VARIANT
;
900 V_VT(&ret
->u
.var
) = VT_BOOL
;
901 V_BOOL(&ret
->u
.var
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
906 static HRESULT
get_binary_expr_values(exec_ctx_t
*ctx
, binary_expression_t
*expr
, jsexcept_t
*ei
, VARIANT
*lval
, VARIANT
*rval
)
911 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
915 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, lval
);
916 exprval_release(&exprval
);
920 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
921 if(SUCCEEDED(hres
)) {
922 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, rval
);
923 exprval_release(&exprval
);
934 typedef HRESULT (*oper_t
)(exec_ctx_t
*,VARIANT
*,VARIANT
*,jsexcept_t
*,VARIANT
*);
936 static HRESULT
binary_expr_eval(exec_ctx_t
*ctx
, binary_expression_t
*expr
, oper_t oper
, jsexcept_t
*ei
,
939 VARIANT lval
, rval
, retv
;
942 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
946 hres
= oper(ctx
, &lval
, &rval
, ei
, &retv
);
952 ret
->type
= EXPRVAL_VARIANT
;
957 /* ECMA-262 3rd Edition 11.13.2 */
958 static HRESULT
assign_oper_eval(exec_ctx_t
*ctx
, expression_t
*lexpr
, expression_t
*rexpr
, oper_t oper
,
959 jsexcept_t
*ei
, exprval_t
*ret
)
961 VARIANT retv
, lval
, rval
;
962 exprval_t exprval
, exprvalr
;
965 hres
= expr_eval(ctx
, lexpr
, EXPR_NEWREF
, ei
, &exprval
);
969 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &lval
);
970 if(SUCCEEDED(hres
)) {
971 hres
= expr_eval(ctx
, rexpr
, 0, ei
, &exprvalr
);
972 if(SUCCEEDED(hres
)) {
973 hres
= exprval_value(ctx
->parser
->script
, &exprvalr
, ei
, &rval
);
974 exprval_release(&exprvalr
);
976 if(SUCCEEDED(hres
)) {
977 hres
= oper(ctx
, &lval
, &rval
, ei
, &retv
);
983 if(SUCCEEDED(hres
)) {
984 hres
= put_value(ctx
->parser
->script
, &exprval
, &retv
, ei
);
988 exprval_release(&exprval
);
993 ret
->type
= EXPRVAL_VARIANT
;
998 /* ECMA-262 3rd Edition 13 */
999 HRESULT
function_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1001 function_expression_t
*expr
= (function_expression_t
*)_expr
;
1008 hres
= create_source_function(ctx
->parser
, expr
->parameter_list
, expr
->source_elements
, ctx
->scope_chain
, &dispex
);
1012 V_VT(&var
) = VT_DISPATCH
;
1013 V_DISPATCH(&var
) = (IDispatch
*)_IDispatchEx_(dispex
);
1015 if(expr
->identifier
) {
1016 hres
= jsdisp_propput_name(ctx
->var_disp
, expr
->identifier
, ctx
->parser
->script
->lcid
, &var
, ei
, NULL
/*FIXME*/);
1018 jsdisp_release(dispex
);
1023 ret
->type
= EXPRVAL_VARIANT
;
1028 /* ECMA-262 3rd Edition 11.12 */
1029 HRESULT
conditional_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1031 conditional_expression_t
*expr
= (conditional_expression_t
*)_expr
;
1038 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1042 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, ei
, &b
);
1043 exprval_release(&exprval
);
1047 return expr_eval(ctx
, b
? expr
->true_expression
: expr
->false_expression
, flags
, ei
, ret
);
1050 /* ECMA-262 3rd Edition 11.2.1 */
1051 HRESULT
array_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1053 array_expression_t
*expr
= (array_expression_t
*)_expr
;
1055 VARIANT member
, val
;
1058 IDispatch
*obj
= NULL
;
1063 hres
= expr_eval(ctx
, expr
->member_expr
, EXPR_NEWREF
, ei
, &exprval
);
1067 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &member
);
1068 exprval_release(&exprval
);
1072 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
1073 if(SUCCEEDED(hres
)) {
1074 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1075 exprval_release(&exprval
);
1079 hres
= to_object(ctx
, &member
, &obj
);
1080 VariantClear(&member
);
1081 if(SUCCEEDED(hres
)) {
1082 hres
= to_string(ctx
->parser
->script
, &val
, ei
, &str
);
1083 if(SUCCEEDED(hres
)) {
1084 if(flags
& EXPR_STRREF
) {
1085 ret
->type
= EXPRVAL_NAMEREF
;
1086 ret
->u
.nameref
.disp
= obj
;
1087 ret
->u
.nameref
.name
= str
;
1091 hres
= disp_get_id(obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1094 if(SUCCEEDED(hres
)) {
1095 exprval_set_idref(ret
, obj
, id
);
1096 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1101 IDispatch_Release(obj
);
1107 /* ECMA-262 3rd Edition 11.2.1 */
1108 HRESULT
member_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1110 member_expression_t
*expr
= (member_expression_t
*)_expr
;
1111 IDispatch
*obj
= NULL
;
1120 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1124 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &member
);
1125 exprval_release(&exprval
);
1129 hres
= to_object(ctx
, &member
, &obj
);
1130 VariantClear(&member
);
1134 str
= SysAllocString(expr
->identifier
);
1135 if(flags
& EXPR_STRREF
) {
1136 ret
->type
= EXPRVAL_NAMEREF
;
1137 ret
->u
.nameref
.disp
= obj
;
1138 ret
->u
.nameref
.name
= str
;
1142 hres
= disp_get_id(obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1144 if(SUCCEEDED(hres
)) {
1145 exprval_set_idref(ret
, obj
, id
);
1146 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1151 IDispatch_Release(obj
);
1155 static void free_dp(DISPPARAMS
*dp
)
1159 for(i
=0; i
< dp
->cArgs
; i
++)
1160 VariantClear(dp
->rgvarg
+i
);
1161 heap_free(dp
->rgvarg
);
1164 static HRESULT
args_to_param(exec_ctx_t
*ctx
, argument_t
*args
, jsexcept_t
*ei
, DISPPARAMS
*dp
)
1170 HRESULT hres
= S_OK
;
1172 memset(dp
, 0, sizeof(*dp
));
1176 for(iter
= args
; iter
; iter
= iter
->next
)
1179 vargs
= heap_alloc_zero(cnt
* sizeof(*vargs
));
1181 return E_OUTOFMEMORY
;
1183 for(i
= cnt
, iter
= args
; iter
; iter
= iter
->next
) {
1184 hres
= expr_eval(ctx
, iter
->expr
, 0, ei
, &exprval
);
1188 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, vargs
+ (--i
));
1189 exprval_release(&exprval
);
1204 /* ECMA-262 3rd Edition 11.2.2 */
1205 HRESULT
new_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1207 call_expression_t
*expr
= (call_expression_t
*)_expr
;
1209 VARIANT constr
, var
;
1215 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1219 hres
= args_to_param(ctx
, expr
->argument_list
, ei
, &dp
);
1221 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &constr
);
1222 exprval_release(&exprval
);
1226 if(V_VT(&constr
) != VT_DISPATCH
) {
1227 FIXME("throw TypeError\n");
1228 VariantClear(&constr
);
1232 hres
= disp_call(V_DISPATCH(&constr
), DISPID_VALUE
, ctx
->parser
->script
->lcid
,
1233 DISPATCH_CONSTRUCT
, &dp
, &var
, ei
, NULL
/*FIXME*/);
1234 IDispatch_Release(V_DISPATCH(&constr
));
1238 ret
->type
= EXPRVAL_VARIANT
;
1243 HRESULT
call_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1245 call_expression_t
*expr
= (call_expression_t
*)_expr
;
1253 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1257 hres
= args_to_param(ctx
, expr
->argument_list
, ei
, &dp
);
1258 if(SUCCEEDED(hres
)) {
1259 switch(exprval
.type
) {
1261 hres
= disp_call(exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
, ctx
->parser
->script
->lcid
, DISPATCH_METHOD
,
1262 &dp
, flags
& EXPR_NOVAL
? NULL
: &var
, ei
, NULL
/*FIXME*/);
1263 if(flags
& EXPR_NOVAL
)
1264 V_VT(&var
) = VT_EMPTY
;
1267 FIXME("unimplemented type %d\n", V_VT(&func
));
1274 exprval_release(&exprval
);
1278 TRACE("= %s\n", debugstr_variant(&var
));
1279 ret
->type
= EXPRVAL_VARIANT
;
1284 HRESULT
this_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1288 ret
->type
= EXPRVAL_VARIANT
;
1289 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
1290 V_DISPATCH(&ret
->u
.var
) = ctx
->this_obj
;
1291 IDispatch_AddRef(ctx
->this_obj
);
1295 /* ECMA-262 3rd Edition 10.1.4 */
1296 HRESULT
identifier_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1298 identifier_expression_t
*expr
= (identifier_expression_t
*)_expr
;
1304 identifier
= SysAllocString(expr
->identifier
);
1306 return E_OUTOFMEMORY
;
1308 hres
= identifier_eval(ctx
, identifier
, flags
, ret
);
1310 SysFreeString(identifier
);
1314 /* ECMA-262 3rd Edition 7.8 */
1315 HRESULT
literal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1317 literal_expression_t
*expr
= (literal_expression_t
*)_expr
;
1323 hres
= literal_to_var(expr
->literal
, &var
);
1327 ret
->type
= EXPRVAL_VARIANT
;
1332 HRESULT
array_literal_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1338 /* ECMA-262 3rd Edition 11.1.5 */
1339 HRESULT
property_value_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1341 property_value_expression_t
*expr
= (property_value_expression_t
*)_expr
;
1351 hres
= create_object(ctx
->parser
->script
, NULL
, &obj
);
1355 for(iter
= expr
->property_list
; iter
; iter
= iter
->next
) {
1356 hres
= literal_to_var(iter
->name
, &tmp
);
1360 hres
= to_string(ctx
->parser
->script
, &tmp
, ei
, &name
);
1365 hres
= expr_eval(ctx
, iter
->value
, 0, ei
, &exprval
);
1366 if(SUCCEEDED(hres
)) {
1367 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1368 exprval_release(&exprval
);
1369 if(SUCCEEDED(hres
)) {
1370 hres
= jsdisp_propput_name(obj
, name
, ctx
->parser
->script
->lcid
, &val
, ei
, NULL
/*FIXME*/);
1375 SysFreeString(name
);
1381 jsdisp_release(obj
);
1385 ret
->type
= EXPRVAL_VARIANT
;
1386 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
1387 V_DISPATCH(&ret
->u
.var
) = (IDispatch
*)_IDispatchEx_(obj
);
1391 HRESULT
comma_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1397 /* ECMA-262 3rd Edition 11.11 */
1398 HRESULT
logical_or_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1400 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1408 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1412 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1413 exprval_release(&exprval
);
1417 hres
= to_boolean(&val
, &b
);
1418 if(SUCCEEDED(hres
) && b
) {
1419 ret
->type
= EXPRVAL_VARIANT
;
1428 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1432 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1433 exprval_release(&exprval
);
1437 ret
->type
= EXPRVAL_VARIANT
;
1442 /* ECMA-262 3rd Edition 11.11 */
1443 HRESULT
logical_and_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1445 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1453 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1457 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1458 exprval_release(&exprval
);
1462 hres
= to_boolean(&val
, &b
);
1463 if(SUCCEEDED(hres
) && !b
) {
1464 ret
->type
= EXPRVAL_VARIANT
;
1473 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1477 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1478 exprval_release(&exprval
);
1482 ret
->type
= EXPRVAL_VARIANT
;
1487 /* ECMA-262 3rd Edition 11.10 */
1488 static HRESULT
bitor_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1493 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
1497 hres
= to_int32(ctx
->parser
->script
, rval
, ei
, &ri
);
1506 /* ECMA-262 3rd Edition 11.10 */
1507 HRESULT
binary_or_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1509 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1513 return binary_expr_eval(ctx
, expr
, bitor_eval
, ei
, ret
);
1516 HRESULT
binary_xor_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1522 /* ECMA-262 3rd Edition 11.10 */
1523 static HRESULT
bitand_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1528 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
1532 hres
= to_int32(ctx
->parser
->script
, rval
, ei
, &ri
);
1541 /* ECMA-262 3rd Edition 11.10 */
1542 HRESULT
binary_and_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1544 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1548 return binary_expr_eval(ctx
, expr
, bitand_eval
, ei
, ret
);
1551 HRESULT
instanceof_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1557 HRESULT
in_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1563 /* ECMA-262 3rd Edition 11.6.1 */
1564 static HRESULT
add_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1569 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &l
);
1573 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &r
);
1579 if(V_VT(&l
) == VT_BSTR
|| V_VT(&r
) == VT_BSTR
) {
1580 BSTR lstr
= NULL
, rstr
= NULL
;
1582 if(V_VT(&l
) == VT_BSTR
)
1585 hres
= to_string(ctx
->parser
->script
, &l
, ei
, &lstr
);
1587 if(SUCCEEDED(hres
)) {
1588 if(V_VT(&r
) == VT_BSTR
)
1591 hres
= to_string(ctx
->parser
->script
, &r
, ei
, &rstr
);
1594 if(SUCCEEDED(hres
)) {
1597 len1
= SysStringLen(lstr
);
1598 len2
= SysStringLen(rstr
);
1600 V_VT(retv
) = VT_BSTR
;
1601 V_BSTR(retv
) = SysAllocStringLen(NULL
, len1
+len2
);
1602 memcpy(V_BSTR(retv
), lstr
, len1
*sizeof(WCHAR
));
1603 memcpy(V_BSTR(retv
)+len1
, rstr
, (len2
+1)*sizeof(WCHAR
));
1606 if(lstr
&& V_VT(&l
) != VT_BSTR
)
1607 SysFreeString(lstr
);
1608 if(rstr
&& V_VT(&r
) != VT_BSTR
)
1609 SysFreeString(rstr
);
1613 hres
= to_number(ctx
->parser
->script
, &l
, ei
, &nl
);
1614 if(SUCCEEDED(hres
)) {
1615 hres
= to_number(ctx
->parser
->script
, &r
, ei
, &nr
);
1617 num_set_val(retv
, num_val(&nl
) + num_val(&nr
));
1626 /* ECMA-262 3rd Edition 11.6.1 */
1627 HRESULT
add_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1629 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1633 return binary_expr_eval(ctx
, expr
, add_eval
, ei
, ret
);
1636 /* ECMA-262 3rd Edition 11.6.2 */
1637 static HRESULT
sub_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1642 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
1646 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
1650 num_set_val(retv
, num_val(&lnum
) - num_val(&rnum
));
1654 /* ECMA-262 3rd Edition 11.6.2 */
1655 HRESULT
sub_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1657 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1661 return binary_expr_eval(ctx
, expr
, sub_eval
, ei
, ret
);
1664 /* ECMA-262 3rd Edition 11.5.1 */
1665 static HRESULT
mul_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1670 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
1674 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
1678 num_set_val(retv
, num_val(&lnum
) * num_val(&rnum
));
1682 /* ECMA-262 3rd Edition 11.5.1 */
1683 HRESULT
mul_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1685 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1689 return binary_expr_eval(ctx
, expr
, mul_eval
, ei
, ret
);
1692 /* ECMA-262 3rd Edition 11.5.2 */
1693 static HRESULT
div_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1698 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
1702 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
1706 num_set_val(retv
, num_val(&lnum
) / num_val(&rnum
));
1710 /* ECMA-262 3rd Edition 11.5.2 */
1711 HRESULT
div_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1713 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1717 return binary_expr_eval(ctx
, expr
, div_eval
, ei
, ret
);
1720 HRESULT
mod_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1726 HRESULT
delete_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1732 HRESULT
void_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1738 HRESULT
typeof_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1740 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
1746 static const WCHAR booleanW
[] = {'b','o','o','l','e','a','n',0};
1747 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
1748 static const WCHAR numberW
[] = {'n','u','m','b','e','r',0};
1749 static const WCHAR objectW
[] = {'o','b','j','e','c','t',0};
1750 static const WCHAR stringW
[] = {'s','t','r','i','n','g',0};
1751 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
1755 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1759 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1760 exprval_release(&exprval
);
1764 switch(V_VT(&val
)) {
1784 dispex
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(&val
));
1786 str
= dispex
->builtin_info
->class == JSCLASS_FUNCTION
? functionW
: objectW
;
1787 IDispatchEx_Release(_IDispatchEx_(dispex
));
1794 FIXME("unhandled vt %d\n", V_VT(&val
));
1802 ret
->type
= EXPRVAL_VARIANT
;
1803 V_VT(&ret
->u
.var
) = VT_BSTR
;
1804 V_BSTR(&ret
->u
.var
) = SysAllocString(str
);
1808 /* ECMA-262 3rd Edition 11.4.7 */
1809 HRESULT
minus_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1811 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
1818 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1822 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1823 exprval_release(&exprval
);
1827 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
1832 ret
->type
= EXPRVAL_VARIANT
;
1833 num_set_val(&ret
->u
.var
, -num_val(&num
));
1837 HRESULT
plus_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1843 /* ECMA-262 3rd Edition 11.3.1 */
1844 HRESULT
post_increment_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1846 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
1853 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
1857 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1858 if(SUCCEEDED(hres
)) {
1859 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
1863 if(SUCCEEDED(hres
)) {
1865 num_set_val(&inc
, num_val(&num
)+1.0);
1866 hres
= put_value(ctx
->parser
->script
, &exprval
, &inc
, ei
);
1869 exprval_release(&exprval
);
1873 ret
->type
= EXPRVAL_VARIANT
;
1878 /* ECMA-262 3rd Edition 11.3.2 */
1879 HRESULT
post_decrement_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1881 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
1888 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
1892 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1893 if(SUCCEEDED(hres
)) {
1894 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
1898 if(SUCCEEDED(hres
)) {
1900 num_set_val(&dec
, num_val(&num
)-1.0);
1901 hres
= put_value(ctx
->parser
->script
, &exprval
, &dec
, ei
);
1904 exprval_release(&exprval
);
1908 ret
->type
= EXPRVAL_VARIANT
;
1913 /* ECMA-262 3rd Edition 11.4.4 */
1914 HRESULT
pre_increment_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1916 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
1923 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
1927 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1928 if(SUCCEEDED(hres
)) {
1929 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
1933 if(SUCCEEDED(hres
)) {
1934 num_set_val(&val
, num_val(&num
)+1.0);
1935 hres
= put_value(ctx
->parser
->script
, &exprval
, &val
, ei
);
1938 exprval_release(&exprval
);
1942 ret
->type
= EXPRVAL_VARIANT
;
1947 /* ECMA-262 3rd Edition 11.4.5 */
1948 HRESULT
pre_decrement_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1950 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
1957 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
1961 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1962 if(SUCCEEDED(hres
)) {
1963 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
1967 if(SUCCEEDED(hres
)) {
1968 num_set_val(&val
, num_val(&num
)-1.0);
1969 hres
= put_value(ctx
->parser
->script
, &exprval
, &val
, ei
);
1972 exprval_release(&exprval
);
1976 ret
->type
= EXPRVAL_VARIANT
;
1981 /* ECMA-262 3rd Edition 11.9.3 */
1982 static HRESULT
equal_values(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, BOOL
*ret
)
1984 if(V_VT(lval
) == V_VT(rval
) || (is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
))))
1985 return equal2_values(lval
, rval
, ret
);
1987 /* FIXME: NULL disps should be handled in more general way */
1988 if(V_VT(lval
) == VT_DISPATCH
&& !V_DISPATCH(lval
)) {
1991 return equal_values(ctx
, &v
, rval
, ei
, ret
);
1994 if(V_VT(rval
) == VT_DISPATCH
&& !V_DISPATCH(rval
)) {
1997 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2000 if((V_VT(lval
) == VT_NULL
&& V_VT(rval
) == VT_EMPTY
) ||
2001 (V_VT(lval
) == VT_EMPTY
&& V_VT(rval
) == VT_NULL
)) {
2006 if(V_VT(lval
) == VT_BSTR
&& is_num_vt(V_VT(rval
))) {
2010 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &v
);
2014 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2017 if(V_VT(rval
) == VT_BSTR
&& is_num_vt(V_VT(lval
))) {
2021 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &v
);
2025 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2028 if(V_VT(rval
) == VT_BOOL
) {
2032 V_I4(&v
) = V_BOOL(rval
) ? 1 : 0;
2033 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2036 if(V_VT(lval
) == VT_BOOL
) {
2040 V_I4(&v
) = V_BOOL(lval
) ? 1 : 0;
2041 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2045 if(V_VT(rval
) == VT_DISPATCH
&& (V_VT(lval
) == VT_BSTR
|| is_num_vt(V_VT(lval
)))) {
2049 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &v
);
2053 hres
= equal_values(ctx
, lval
, &v
, ei
, ret
);
2060 if(V_VT(lval
) == VT_DISPATCH
&& (V_VT(rval
) == VT_BSTR
|| is_num_vt(V_VT(rval
)))) {
2064 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &v
);
2068 hres
= equal_values(ctx
, &v
, rval
, ei
, ret
);
2079 /* ECMA-262 3rd Edition 11.9.1 */
2080 HRESULT
equal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2082 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2089 hres
= get_binary_expr_values(ctx
, expr
, ei
, &rval
, &lval
);
2093 hres
= equal_values(ctx
, &rval
, &lval
, ei
, &b
);
2097 return return_bool(ret
, b
);
2100 /* ECMA-262 3rd Edition 11.9.4 */
2101 HRESULT
equal2_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2103 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2110 hres
= get_binary_expr_values(ctx
, expr
, ei
, &rval
, &lval
);
2114 hres
= equal2_values(&rval
, &lval
, &b
);
2118 return return_bool(ret
, b
);
2121 /* ECMA-262 3rd Edition 11.9.2 */
2122 HRESULT
not_equal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2124 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2131 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2135 hres
= equal_values(ctx
, &lval
, &rval
, ei
, &b
);
2139 return return_bool(ret
, !b
);
2142 /* ECMA-262 3rd Edition 11.9.5 */
2143 HRESULT
not_equal2_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2145 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2152 hres
= get_binary_expr_values(ctx
, expr
, ei
, &rval
, &lval
);
2156 hres
= equal2_values(&rval
, &lval
, &b
);
2160 return return_bool(ret
, !b
);
2163 /* ECMA-262 3rd Edition 11.8.5 */
2164 static HRESULT
less_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, BOOL
*ret
)
2166 VARIANT l
, r
, ln
, rn
;
2169 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &l
);
2173 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &r
);
2179 if(V_VT(&l
) == VT_BSTR
&& V_VT(&r
) == VT_BSTR
) {
2180 *ret
= strcmpW(V_BSTR(&l
), V_BSTR(&r
)) < 0;
2181 SysFreeString(V_BSTR(&l
));
2182 SysFreeString(V_BSTR(&r
));
2186 hres
= to_number(ctx
->parser
->script
, &l
, ei
, &ln
);
2189 hres
= to_number(ctx
->parser
->script
, &r
, ei
, &rn
);
2194 if(V_VT(&ln
) == VT_I4
&& V_VT(&rn
) == VT_I4
)
2195 *ret
= V_I4(&ln
) < V_I4(&rn
);
2197 *ret
= num_val(&ln
) < num_val(&rn
);
2202 /* ECMA-262 3rd Edition 11.8.1 */
2203 HRESULT
less_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2205 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2212 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2216 hres
= less_eval(ctx
, &lval
, &rval
, ei
, &b
);
2217 VariantClear(&lval
);
2218 VariantClear(&rval
);
2222 return return_bool(ret
, b
);
2225 /* ECMA-262 3rd Edition 11.8.3 */
2226 HRESULT
lesseq_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2228 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2235 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2239 hres
= less_eval(ctx
, &rval
, &lval
, ei
, &b
);
2240 VariantClear(&lval
);
2241 VariantClear(&rval
);
2245 return return_bool(ret
, !b
);
2248 /* ECMA-262 3rd Edition 11.8.2 */
2249 HRESULT
greater_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2251 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2258 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2262 hres
= less_eval(ctx
, &rval
, &lval
, ei
, &b
);
2263 VariantClear(&lval
);
2264 VariantClear(&rval
);
2268 return return_bool(ret
, b
);
2271 /* ECMA-262 3rd Edition 11.8.4 */
2272 HRESULT
greatereq_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2274 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2281 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2285 hres
= less_eval(ctx
, &lval
, &rval
, ei
, &b
);
2286 VariantClear(&lval
);
2287 VariantClear(&rval
);
2291 return return_bool(ret
, !b
);
2294 HRESULT
binary_negation_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2300 /* ECMA-262 3rd Edition 11.4.9 */
2301 HRESULT
logical_negation_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2303 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2310 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2314 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, ei
, &b
);
2315 exprval_release(&exprval
);
2319 return return_bool(ret
, !b
);
2322 HRESULT
left_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2328 HRESULT
right_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2334 HRESULT
right2_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2340 /* ECMA-262 3rd Edition 11.13.1 */
2341 HRESULT
assign_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2343 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2344 exprval_t exprval
, exprvalr
;
2350 hres
= expr_eval(ctx
, expr
->expression1
, EXPR_NEWREF
, ei
, &exprval
);
2354 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprvalr
);
2355 if(SUCCEEDED(hres
)) {
2356 hres
= exprval_to_value(ctx
->parser
->script
, &exprvalr
, ei
, &rval
);
2357 exprval_release(&exprvalr
);
2361 hres
= put_value(ctx
->parser
->script
, &exprval
, &rval
, ei
);
2363 exprval_release(&exprval
);
2365 VariantClear(&rval
);
2369 ret
->type
= EXPRVAL_VARIANT
;
2374 HRESULT
assign_lshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2380 HRESULT
assign_rshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2386 HRESULT
assign_rrshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2392 /* ECMA-262 3rd Edition 11.13.2 */
2393 HRESULT
assign_add_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2395 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2399 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, add_eval
, ei
, ret
);
2402 /* ECMA-262 3rd Edition 11.13.2 */
2403 HRESULT
assign_sub_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2405 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2409 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, sub_eval
, ei
, ret
);
2412 /* ECMA-262 3rd Edition 11.13.2 */
2413 HRESULT
assign_mul_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2415 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2419 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, mul_eval
, ei
, ret
);
2422 /* ECMA-262 3rd Edition 11.13.2 */
2423 HRESULT
assign_div_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2425 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2429 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, div_eval
, ei
, ret
);
2432 HRESULT
assign_mod_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2438 /* ECMA-262 3rd Edition 11.13.2 */
2439 HRESULT
assign_and_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2441 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2445 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, bitand_eval
, ei
, ret
);
2448 /* ECMA-262 3rd Edition 11.13.2 */
2449 HRESULT
assign_or_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2451 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2455 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, bitor_eval
, ei
, ret
);
2458 HRESULT
assign_xor_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)