2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
31 #define EXPR_NOVAL 0x0001
32 #define EXPR_NEWREF 0x0002
33 #define EXPR_STRREF 0x0004
35 static inline HRESULT
stat_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
37 return stat
->eval(ctx
, stat
, rt
, ret
);
40 static inline HRESULT
expr_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
42 return _expr
->eval(ctx
, _expr
, flags
, ei
, ret
);
45 static void exprval_release(exprval_t
*val
)
49 if(V_VT(&val
->u
.var
) != VT_EMPTY
)
50 VariantClear(&val
->u
.var
);
54 IDispatch_Release(val
->u
.idref
.disp
);
57 if(val
->u
.nameref
.disp
)
58 IDispatch_Release(val
->u
.nameref
.disp
);
59 SysFreeString(val
->u
.nameref
.name
);
62 SysFreeString(val
->u
.identifier
);
66 /* ECMA-262 3rd Edition 8.7.1 */
67 static HRESULT
exprval_value(script_ctx_t
*ctx
, exprval_t
*val
, jsexcept_t
*ei
, VARIANT
*ret
)
73 return VariantCopy(ret
, &val
->u
.var
);
75 if(!val
->u
.idref
.disp
) {
76 FIXME("throw ReferenceError\n");
80 return disp_propget(ctx
, val
->u
.idref
.disp
, val
->u
.idref
.id
, ret
, ei
, NULL
/*FIXME*/);
84 return throw_type_error(ctx
, ei
, IDS_UNDEFINED
, val
->u
.identifier
);
87 ERR("type %d\n", val
->type
);
91 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*val
, jsexcept_t
*ei
, VARIANT
*ret
)
93 if(val
->type
== EXPRVAL_VARIANT
) {
95 V_VT(&val
->u
.var
) = VT_EMPTY
;
99 return exprval_value(ctx
, val
, ei
, ret
);
102 static HRESULT
exprval_to_boolean(script_ctx_t
*ctx
, exprval_t
*exprval
, jsexcept_t
*ei
, VARIANT_BOOL
*b
)
104 if(exprval
->type
!= EXPRVAL_VARIANT
) {
108 hres
= exprval_to_value(ctx
, exprval
, ei
, &val
);
112 hres
= to_boolean(&val
, b
);
117 return to_boolean(&exprval
->u
.var
, b
);
120 static void exprval_init(exprval_t
*val
)
122 val
->type
= EXPRVAL_VARIANT
;
123 V_VT(&val
->u
.var
) = VT_EMPTY
;
126 static void exprval_set_idref(exprval_t
*val
, IDispatch
*disp
, DISPID id
)
128 val
->type
= EXPRVAL_IDREF
;
129 val
->u
.idref
.disp
= disp
;
130 val
->u
.idref
.id
= id
;
133 IDispatch_AddRef(disp
);
136 HRESULT
scope_push(scope_chain_t
*scope
, DispatchEx
*obj
, scope_chain_t
**ret
)
138 scope_chain_t
*new_scope
;
140 new_scope
= heap_alloc(sizeof(scope_chain_t
));
142 return E_OUTOFMEMORY
;
146 IDispatchEx_AddRef(_IDispatchEx_(obj
));
147 new_scope
->obj
= obj
;
151 new_scope
->next
= scope
;
153 new_scope
->next
= NULL
;
160 static void scope_pop(scope_chain_t
**scope
)
169 void scope_release(scope_chain_t
*scope
)
175 scope_release(scope
->next
);
177 jsdisp_release(scope
->obj
);
181 HRESULT
create_exec_ctx(script_ctx_t
*script_ctx
, IDispatch
*this_obj
, DispatchEx
*var_disp
,
182 scope_chain_t
*scope
, exec_ctx_t
**ret
)
186 ctx
= heap_alloc_zero(sizeof(exec_ctx_t
));
188 return E_OUTOFMEMORY
;
193 ctx
->this_obj
= this_obj
;
194 else if(script_ctx
->host_global
)
195 ctx
->this_obj
= script_ctx
->host_global
;
197 ctx
->this_obj
= (IDispatch
*)_IDispatchEx_(script_ctx
->global
);
198 IDispatch_AddRef(ctx
->this_obj
);
200 IDispatchEx_AddRef(_IDispatchEx_(var_disp
));
201 ctx
->var_disp
= var_disp
;
205 ctx
->scope_chain
= scope
;
212 void exec_release(exec_ctx_t
*ctx
)
218 scope_release(ctx
->scope_chain
);
220 jsdisp_release(ctx
->var_disp
);
222 IDispatch_Release(ctx
->this_obj
);
226 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, BSTR name
, DWORD flags
, DISPID
*id
)
231 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
233 TRACE("unsing IDispatch\n");
236 return IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &name
, 1, 0, id
);
240 hres
= IDispatchEx_GetDispID(dispex
, name
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
241 IDispatchEx_Release(dispex
);
245 /* ECMA-262 3rd Edition 8.7.2 */
246 static HRESULT
put_value(script_ctx_t
*ctx
, exprval_t
*ref
, VARIANT
*v
, jsexcept_t
*ei
)
248 if(ref
->type
!= EXPRVAL_IDREF
)
249 return throw_reference_error(ctx
, ei
, IDS_ILLEGAL_ASSIGN
, NULL
);
251 return disp_propput(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, v
, ei
, NULL
/*FIXME*/);
254 static inline BOOL
is_null(const VARIANT
*v
)
256 return V_VT(v
) == VT_NULL
|| (V_VT(v
) == VT_DISPATCH
&& !V_DISPATCH(v
));
259 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
261 IObjectIdentity
*identity
;
262 IUnknown
*unk1
, *unk2
;
270 if(!disp1
|| !disp2
) {
275 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
279 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
281 IUnknown_Release(unk1
);
288 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
289 if(SUCCEEDED(hres
)) {
290 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
291 IObjectIdentity_Release(identity
);
298 IUnknown_Release(unk1
);
299 IUnknown_Release(unk2
);
303 /* ECMA-262 3rd Edition 11.9.6 */
304 static HRESULT
equal2_values(VARIANT
*lval
, VARIANT
*rval
, BOOL
*ret
)
308 if(V_VT(lval
) != V_VT(rval
)) {
309 if(is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
)))
310 *ret
= num_val(lval
) == num_val(rval
);
311 else if(is_null(lval
))
312 *ret
= is_null(rval
);
324 *ret
= V_I4(lval
) == V_I4(rval
);
327 *ret
= V_R8(lval
) == V_R8(rval
);
331 *ret
= SysStringLen(V_BSTR(rval
))?FALSE
:TRUE
;
332 else if(!V_BSTR(rval
))
333 *ret
= SysStringLen(V_BSTR(lval
))?FALSE
:TRUE
;
335 *ret
= !strcmpW(V_BSTR(lval
), V_BSTR(rval
));
338 return disp_cmp(V_DISPATCH(lval
), V_DISPATCH(rval
), ret
);
340 *ret
= !V_BOOL(lval
) == !V_BOOL(rval
);
343 FIXME("unimplemented vt %d\n", V_VT(lval
));
350 static HRESULT
literal_to_var(script_ctx_t
*ctx
, literal_t
*literal
, VARIANT
*v
)
352 switch(literal
->type
) {
358 V_I4(v
) = literal
->u
.lval
;
362 V_R8(v
) = literal
->u
.dval
;
365 BSTR str
= SysAllocString(literal
->u
.wstr
);
367 return E_OUTOFMEMORY
;
375 V_BOOL(v
) = literal
->u
.bval
;
381 hres
= create_regexp(ctx
, literal
->u
.regexp
.str
, literal
->u
.regexp
.str_len
,
382 literal
->u
.regexp
.flags
, ®exp
);
386 V_VT(v
) = VT_DISPATCH
;
387 V_DISPATCH(v
) = (IDispatch
*)_IDispatchEx_(regexp
);
394 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
400 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
401 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
402 hres
= disp_get_id(ctx
, item
->disp
, identifier
, 0, &id
);
403 if(SUCCEEDED(hres
)) {
405 exprval_set_idref(ret
, item
->disp
, id
);
414 HRESULT
exec_source(exec_ctx_t
*ctx
, parser_ctx_t
*parser
, source_elements_t
*source
, exec_type_t exec_type
,
415 jsexcept_t
*ei
, VARIANT
*retv
)
417 script_ctx_t
*script
= parser
->script
;
418 function_declaration_t
*func
;
419 parser_ctx_t
*prev_parser
;
423 exec_ctx_t
*prev_ctx
;
427 for(func
= source
->functions
; func
; func
= func
->next
) {
428 DispatchEx
*func_obj
;
431 hres
= create_source_function(parser
, func
->expr
->parameter_list
, func
->expr
->source_elements
,
432 ctx
->scope_chain
, func
->expr
->src_str
, func
->expr
->src_len
, &func_obj
);
436 V_VT(&var
) = VT_DISPATCH
;
437 V_DISPATCH(&var
) = (IDispatch
*)_IDispatchEx_(func_obj
);
438 hres
= jsdisp_propput_name(ctx
->var_disp
, func
->expr
->identifier
, &var
, ei
, NULL
);
439 jsdisp_release(func_obj
);
444 for(var
= source
->variables
; var
; var
= var
->next
) {
448 name
= SysAllocString(var
->identifier
);
450 return E_OUTOFMEMORY
;
452 if(!lookup_global_members(parser
->script
, name
, NULL
))
453 hres
= jsdisp_get_id(ctx
->var_disp
, var
->identifier
, fdexNameEnsure
, &id
);
459 prev_ctx
= script
->exec_ctx
;
460 script
->exec_ctx
= ctx
;
462 prev_parser
= ctx
->parser
;
463 ctx
->parser
= parser
;
465 V_VT(&val
) = VT_EMPTY
;
466 memset(&rt
, 0, sizeof(rt
));
469 for(stat
= source
->statement
; stat
; stat
= stat
->next
) {
470 hres
= stat_eval(ctx
, stat
, &rt
, &tmp
);
476 if(rt
.type
!= RT_NORMAL
)
480 script
->exec_ctx
= prev_ctx
;
481 ctx
->parser
= prev_parser
;
483 if(rt
.type
!= RT_NORMAL
&& rt
.type
!= RT_RETURN
) {
484 FIXME("wrong rt %d\n", rt
.type
);
494 if(retv
&& (exec_type
== EXECT_EVAL
|| rt
.type
== RT_RETURN
))
505 /* ECMA-262 3rd Edition 10.1.4 */
506 static HRESULT
identifier_eval(exec_ctx_t
*ctx
, BSTR identifier
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
508 scope_chain_t
*scope
;
513 TRACE("%s\n", debugstr_w(identifier
));
515 for(scope
= ctx
->scope_chain
; scope
; scope
= scope
->next
) {
516 hres
= jsdisp_get_id(scope
->obj
, identifier
, 0, &id
);
522 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(scope
->obj
), id
);
526 hres
= jsdisp_get_id(ctx
->parser
->script
->global
, identifier
, 0, &id
);
527 if(SUCCEEDED(hres
)) {
528 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(ctx
->parser
->script
->global
), id
);
532 for(item
= ctx
->parser
->script
->named_items
; item
; item
= item
->next
) {
533 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
537 if(!ctx
->parser
->script
->site
)
540 hres
= IActiveScriptSite_GetItemInfo(ctx
->parser
->script
->site
, identifier
,
541 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
543 WARN("GetItemInfo failed: %08x\n", hres
);
547 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
548 IUnknown_Release(unk
);
550 WARN("object does not implement IDispatch\n");
555 ret
->type
= EXPRVAL_VARIANT
;
556 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
557 V_DISPATCH(&ret
->u
.var
) = item
->disp
;
558 IDispatch_AddRef(item
->disp
);
563 if(lookup_global_members(ctx
->parser
->script
, identifier
, ret
))
566 if(flags
& EXPR_NEWREF
) {
567 hres
= jsdisp_get_id(ctx
->parser
->script
->global
, identifier
, fdexNameEnsure
, &id
);
571 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(ctx
->parser
->script
->global
), id
);
575 ret
->type
= EXPRVAL_INVALID
;
576 ret
->u
.identifier
= SysAllocString(identifier
);
577 if(!ret
->u
.identifier
)
578 return E_OUTOFMEMORY
;
583 /* ECMA-262 3rd Edition 12.1 */
584 HRESULT
block_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
586 block_statement_t
*stat
= (block_statement_t
*)_stat
;
593 V_VT(&val
) = VT_EMPTY
;
594 for(iter
= stat
->stat_list
; iter
; iter
= iter
->next
) {
595 hres
= stat_eval(ctx
, iter
, rt
, &tmp
);
601 if(rt
->type
!= RT_NORMAL
)
614 /* ECMA-262 3rd Edition 12.2 */
615 static HRESULT
variable_list_eval(exec_ctx_t
*ctx
, variable_declaration_t
*var_list
, jsexcept_t
*ei
)
617 variable_declaration_t
*iter
;
620 for(iter
= var_list
; iter
; iter
= iter
->next
) {
627 hres
= expr_eval(ctx
, iter
->expr
, 0, ei
, &exprval
);
631 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
632 exprval_release(&exprval
);
636 hres
= jsdisp_propput_name(ctx
->var_disp
, iter
->identifier
, &val
, ei
, NULL
/*FIXME*/);
645 /* ECMA-262 3rd Edition 12.2 */
646 HRESULT
var_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
648 var_statement_t
*stat
= (var_statement_t
*)_stat
;
653 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
657 V_VT(ret
) = VT_EMPTY
;
661 /* ECMA-262 3rd Edition 12.3 */
662 HRESULT
empty_statement_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
666 V_VT(ret
) = VT_EMPTY
;
670 /* ECMA-262 3rd Edition 12.4 */
671 HRESULT
expression_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
673 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
680 hres
= expr_eval(ctx
, stat
->expr
, EXPR_NOVAL
, &rt
->ei
, &exprval
);
684 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
685 exprval_release(&exprval
);
690 TRACE("= %s\n", debugstr_variant(ret
));
694 /* ECMA-262 3rd Edition 12.5 */
695 HRESULT
if_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
697 if_statement_t
*stat
= (if_statement_t
*)_stat
;
704 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
708 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, &rt
->ei
, &b
);
709 exprval_release(&exprval
);
714 hres
= stat_eval(ctx
, stat
->if_stat
, rt
, ret
);
715 else if(stat
->else_stat
)
716 hres
= stat_eval(ctx
, stat
->else_stat
, rt
, ret
);
718 V_VT(ret
) = VT_EMPTY
;
723 /* ECMA-262 3rd Edition 12.6.2 */
724 HRESULT
while_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
726 while_statement_t
*stat
= (while_statement_t
*)_stat
;
735 V_VT(&val
) = VT_EMPTY
;
736 test_expr
= !stat
->do_while
;
740 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
744 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, &rt
->ei
, &b
);
745 exprval_release(&exprval
);
746 if(FAILED(hres
) || !b
)
752 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
759 if(rt
->type
== RT_CONTINUE
)
760 rt
->type
= RT_NORMAL
;
761 if(rt
->type
!= RT_NORMAL
)
770 if(rt
->type
== RT_BREAK
)
771 rt
->type
= RT_NORMAL
;
777 /* ECMA-262 3rd Edition 12.6.3 */
778 HRESULT
for_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
780 for_statement_t
*stat
= (for_statement_t
*)_stat
;
781 VARIANT val
, tmp
, retv
;
788 if(stat
->variable_list
) {
789 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
792 }else if(stat
->begin_expr
) {
793 hres
= expr_eval(ctx
, stat
->begin_expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
797 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
798 exprval_release(&exprval
);
805 V_VT(&retv
) = VT_EMPTY
;
809 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
813 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, &rt
->ei
, &b
);
814 exprval_release(&exprval
);
815 if(FAILED(hres
) || !b
)
819 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
826 if(rt
->type
== RT_CONTINUE
)
827 rt
->type
= RT_NORMAL
;
828 else if(rt
->type
!= RT_NORMAL
)
832 hres
= expr_eval(ctx
, stat
->end_expr
, 0, &rt
->ei
, &exprval
);
836 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
837 exprval_release(&exprval
);
850 if(rt
->type
== RT_BREAK
)
851 rt
->type
= RT_NORMAL
;
857 /* ECMA-262 3rd Edition 12.6.4 */
858 HRESULT
forin_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
860 forin_statement_t
*stat
= (forin_statement_t
*)_stat
;
861 VARIANT val
, name
, retv
, tmp
;
862 DISPID id
= DISPID_STARTENUM
;
863 BSTR str
, identifier
= NULL
;
871 hres
= variable_list_eval(ctx
, stat
->variable
, &rt
->ei
);
876 hres
= expr_eval(ctx
, stat
->in_expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
880 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
881 exprval_release(&exprval
);
885 if(V_VT(&val
) != VT_DISPATCH
) {
886 TRACE("in vt %d\n", V_VT(&val
));
888 V_VT(ret
) = VT_EMPTY
;
892 hres
= IDispatch_QueryInterface(V_DISPATCH(&val
), &IID_IDispatchEx
, (void**)&in_obj
);
893 IDispatch_Release(V_DISPATCH(&val
));
895 FIXME("Object doesn't support IDispatchEx\n");
899 V_VT(&retv
) = VT_EMPTY
;
902 identifier
= SysAllocString(stat
->variable
->identifier
);
905 hres
= IDispatchEx_GetNextDispID(in_obj
, fdexEnumDefault
, id
, &id
);
906 if(FAILED(hres
) || hres
== S_FALSE
)
909 hres
= IDispatchEx_GetMemberName(in_obj
, id
, &str
);
913 TRACE("iter %s\n", debugstr_w(str
));
916 hres
= identifier_eval(ctx
, identifier
, 0, NULL
, &exprval
);
918 hres
= expr_eval(ctx
, stat
->expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
919 if(SUCCEEDED(hres
)) {
920 V_VT(&name
) = VT_BSTR
;
922 hres
= put_value(ctx
->parser
->script
, &exprval
, &name
, &rt
->ei
);
923 exprval_release(&exprval
);
929 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
936 if(rt
->type
== RT_CONTINUE
)
937 rt
->type
= RT_NORMAL
;
938 else if(rt
->type
!= RT_NORMAL
)
942 SysFreeString(identifier
);
943 IDispatchEx_Release(in_obj
);
949 if(rt
->type
== RT_BREAK
)
950 rt
->type
= RT_NORMAL
;
956 /* ECMA-262 3rd Edition 12.7 */
957 HRESULT
continue_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
959 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
963 if(stat
->identifier
) {
964 FIXME("indentifier not implemented\n");
968 rt
->type
= RT_CONTINUE
;
969 V_VT(ret
) = VT_EMPTY
;
973 /* ECMA-262 3rd Edition 12.8 */
974 HRESULT
break_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
976 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
980 if(stat
->identifier
) {
981 FIXME("indentifier not implemented\n");
986 V_VT(ret
) = VT_EMPTY
;
990 /* ECMA-262 3rd Edition 12.9 */
991 HRESULT
return_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
993 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
1001 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1005 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, ret
);
1006 exprval_release(&exprval
);
1010 V_VT(ret
) = VT_EMPTY
;
1013 TRACE("= %s\n", debugstr_variant(ret
));
1014 rt
->type
= RT_RETURN
;
1018 /* ECMA-262 3rd Edition 12.10 */
1019 HRESULT
with_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1021 with_statement_t
*stat
= (with_statement_t
*)_stat
;
1030 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1034 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
1035 exprval_release(&exprval
);
1039 hres
= to_object(ctx
->parser
->script
, &val
, &disp
);
1044 obj
= iface_to_jsdisp((IUnknown
*)disp
);
1045 IDispatch_Release(disp
);
1047 FIXME("disp id not jsdisp\n");
1051 hres
= scope_push(ctx
->scope_chain
, obj
, &ctx
->scope_chain
);
1052 jsdisp_release(obj
);
1056 hres
= stat_eval(ctx
, stat
->statement
, rt
, ret
);
1058 scope_pop(&ctx
->scope_chain
);
1062 /* ECMA-262 3rd Edition 12.12 */
1063 HRESULT
labelled_statement_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
1069 /* ECMA-262 3rd Edition 12.13 */
1070 HRESULT
switch_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1072 switch_statement_t
*stat
= (switch_statement_t
*)_stat
;
1073 case_clausule_t
*iter
, *default_clausule
= NULL
;
1074 statement_t
*stat_iter
;
1082 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1086 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
1087 exprval_release(&exprval
);
1091 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1093 default_clausule
= iter
;
1097 hres
= expr_eval(ctx
, iter
->expr
, 0, &rt
->ei
, &exprval
);
1101 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &cval
);
1102 exprval_release(&exprval
);
1106 hres
= equal2_values(&val
, &cval
, &b
);
1107 VariantClear(&cval
);
1108 if(FAILED(hres
) || b
)
1117 iter
= default_clausule
;
1119 V_VT(&val
) = VT_EMPTY
;
1123 for(stat_iter
= iter
->stat
; stat_iter
; stat_iter
= stat_iter
->next
) {
1124 hres
= stat_eval(ctx
, stat_iter
, rt
, &tmp
);
1131 if(rt
->type
!= RT_NORMAL
)
1141 if(rt
->type
== RT_BREAK
)
1142 rt
->type
= RT_NORMAL
;
1148 /* ECMA-262 3rd Edition 12.13 */
1149 HRESULT
throw_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1151 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
1158 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1162 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
1163 exprval_release(&exprval
);
1168 return DISP_E_EXCEPTION
;
1171 /* ECMA-262 3rd Edition 12.14 */
1172 static HRESULT
catch_eval(exec_ctx_t
*ctx
, catch_block_t
*block
, return_type_t
*rt
, VARIANT
*ret
)
1174 DispatchEx
*var_disp
;
1179 memset(&rt
->ei
, 0, sizeof(jsexcept_t
));
1181 hres
= create_dispex(ctx
->parser
->script
, NULL
, NULL
, &var_disp
);
1182 if(SUCCEEDED(hres
)) {
1183 hres
= jsdisp_propput_name(var_disp
, block
->identifier
, &ex
, &rt
->ei
, NULL
/*FIXME*/);
1184 if(SUCCEEDED(hres
)) {
1185 hres
= scope_push(ctx
->scope_chain
, var_disp
, &ctx
->scope_chain
);
1186 if(SUCCEEDED(hres
)) {
1187 hres
= stat_eval(ctx
, block
->statement
, rt
, &val
);
1188 scope_pop(&ctx
->scope_chain
);
1192 jsdisp_release(var_disp
);
1203 /* ECMA-262 3rd Edition 12.14 */
1204 HRESULT
try_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1206 try_statement_t
*stat
= (try_statement_t
*)_stat
;
1212 hres
= stat_eval(ctx
, stat
->try_statement
, rt
, &val
);
1214 TRACE("EXCEPTION\n");
1215 if(!stat
->catch_block
)
1218 hres
= catch_eval(ctx
, stat
->catch_block
, rt
, &val
);
1223 if(stat
->finally_statement
) {
1225 hres
= stat_eval(ctx
, stat
->finally_statement
, rt
, &val
);
1234 static HRESULT
return_bool(exprval_t
*ret
, DWORD b
)
1236 ret
->type
= EXPRVAL_VARIANT
;
1237 V_VT(&ret
->u
.var
) = VT_BOOL
;
1238 V_BOOL(&ret
->u
.var
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
1243 static HRESULT
get_binary_expr_values(exec_ctx_t
*ctx
, binary_expression_t
*expr
, jsexcept_t
*ei
, VARIANT
*lval
, VARIANT
*rval
)
1248 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1252 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, lval
);
1253 exprval_release(&exprval
);
1257 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1258 if(SUCCEEDED(hres
)) {
1259 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, rval
);
1260 exprval_release(&exprval
);
1271 typedef HRESULT (*oper_t
)(exec_ctx_t
*,VARIANT
*,VARIANT
*,jsexcept_t
*,VARIANT
*);
1273 static HRESULT
binary_expr_eval(exec_ctx_t
*ctx
, binary_expression_t
*expr
, oper_t oper
, jsexcept_t
*ei
,
1276 VARIANT lval
, rval
, retv
;
1279 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
1283 hres
= oper(ctx
, &lval
, &rval
, ei
, &retv
);
1284 VariantClear(&lval
);
1285 VariantClear(&rval
);
1289 ret
->type
= EXPRVAL_VARIANT
;
1294 /* ECMA-262 3rd Edition 11.13.2 */
1295 static HRESULT
assign_oper_eval(exec_ctx_t
*ctx
, expression_t
*lexpr
, expression_t
*rexpr
, oper_t oper
,
1296 jsexcept_t
*ei
, exprval_t
*ret
)
1298 VARIANT retv
, lval
, rval
;
1299 exprval_t exprval
, exprvalr
;
1302 hres
= expr_eval(ctx
, lexpr
, EXPR_NEWREF
, ei
, &exprval
);
1306 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &lval
);
1307 if(SUCCEEDED(hres
)) {
1308 hres
= expr_eval(ctx
, rexpr
, 0, ei
, &exprvalr
);
1309 if(SUCCEEDED(hres
)) {
1310 hres
= exprval_value(ctx
->parser
->script
, &exprvalr
, ei
, &rval
);
1311 exprval_release(&exprvalr
);
1313 if(SUCCEEDED(hres
)) {
1314 hres
= oper(ctx
, &lval
, &rval
, ei
, &retv
);
1315 VariantClear(&rval
);
1317 VariantClear(&lval
);
1320 if(SUCCEEDED(hres
)) {
1321 hres
= put_value(ctx
->parser
->script
, &exprval
, &retv
, ei
);
1323 VariantClear(&retv
);
1325 exprval_release(&exprval
);
1330 ret
->type
= EXPRVAL_VARIANT
;
1335 /* ECMA-262 3rd Edition 13 */
1336 HRESULT
function_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1338 function_expression_t
*expr
= (function_expression_t
*)_expr
;
1344 if(expr
->identifier
) {
1345 hres
= jsdisp_propget_name(ctx
->var_disp
, expr
->identifier
, &var
, ei
, NULL
/*FIXME*/);
1351 hres
= create_source_function(ctx
->parser
, expr
->parameter_list
, expr
->source_elements
, ctx
->scope_chain
,
1352 expr
->src_str
, expr
->src_len
, &dispex
);
1356 V_VT(&var
) = VT_DISPATCH
;
1357 V_DISPATCH(&var
) = (IDispatch
*)_IDispatchEx_(dispex
);
1360 ret
->type
= EXPRVAL_VARIANT
;
1365 /* ECMA-262 3rd Edition 11.12 */
1366 HRESULT
conditional_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1368 conditional_expression_t
*expr
= (conditional_expression_t
*)_expr
;
1375 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1379 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, ei
, &b
);
1380 exprval_release(&exprval
);
1384 return expr_eval(ctx
, b
? expr
->true_expression
: expr
->false_expression
, flags
, ei
, ret
);
1387 /* ECMA-262 3rd Edition 11.2.1 */
1388 HRESULT
array_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1390 array_expression_t
*expr
= (array_expression_t
*)_expr
;
1392 VARIANT member
, val
;
1395 IDispatch
*obj
= NULL
;
1400 hres
= expr_eval(ctx
, expr
->member_expr
, 0, ei
, &exprval
);
1404 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &member
);
1405 exprval_release(&exprval
);
1409 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
1410 if(SUCCEEDED(hres
)) {
1411 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1412 exprval_release(&exprval
);
1415 if(SUCCEEDED(hres
)) {
1416 hres
= to_object(ctx
->parser
->script
, &member
, &obj
);
1420 VariantClear(&member
);
1421 if(SUCCEEDED(hres
)) {
1422 hres
= to_string(ctx
->parser
->script
, &val
, ei
, &str
);
1424 if(SUCCEEDED(hres
)) {
1425 if(flags
& EXPR_STRREF
) {
1426 ret
->type
= EXPRVAL_NAMEREF
;
1427 ret
->u
.nameref
.disp
= obj
;
1428 ret
->u
.nameref
.name
= str
;
1432 hres
= disp_get_id(ctx
->parser
->script
, obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1436 if(SUCCEEDED(hres
)) {
1437 exprval_set_idref(ret
, obj
, id
);
1438 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1443 IDispatch_Release(obj
);
1449 /* ECMA-262 3rd Edition 11.2.1 */
1450 HRESULT
member_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1452 member_expression_t
*expr
= (member_expression_t
*)_expr
;
1453 IDispatch
*obj
= NULL
;
1462 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1466 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &member
);
1467 exprval_release(&exprval
);
1471 hres
= to_object(ctx
->parser
->script
, &member
, &obj
);
1472 VariantClear(&member
);
1476 str
= SysAllocString(expr
->identifier
);
1477 if(flags
& EXPR_STRREF
) {
1478 ret
->type
= EXPRVAL_NAMEREF
;
1479 ret
->u
.nameref
.disp
= obj
;
1480 ret
->u
.nameref
.name
= str
;
1484 hres
= disp_get_id(ctx
->parser
->script
, obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1486 if(SUCCEEDED(hres
)) {
1487 exprval_set_idref(ret
, obj
, id
);
1488 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1493 IDispatch_Release(obj
);
1497 static void free_dp(DISPPARAMS
*dp
)
1501 for(i
=0; i
< dp
->cArgs
; i
++)
1502 VariantClear(dp
->rgvarg
+i
);
1503 heap_free(dp
->rgvarg
);
1506 static HRESULT
args_to_param(exec_ctx_t
*ctx
, argument_t
*args
, jsexcept_t
*ei
, DISPPARAMS
*dp
)
1512 HRESULT hres
= S_OK
;
1514 memset(dp
, 0, sizeof(*dp
));
1518 for(iter
= args
; iter
; iter
= iter
->next
)
1521 vargs
= heap_alloc_zero(cnt
* sizeof(*vargs
));
1523 return E_OUTOFMEMORY
;
1525 for(i
= cnt
, iter
= args
; iter
; iter
= iter
->next
) {
1526 hres
= expr_eval(ctx
, iter
->expr
, 0, ei
, &exprval
);
1530 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, vargs
+ (--i
));
1531 exprval_release(&exprval
);
1546 /* ECMA-262 3rd Edition 11.2.2 */
1547 HRESULT
new_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1549 call_expression_t
*expr
= (call_expression_t
*)_expr
;
1551 VARIANT constr
, var
;
1557 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1561 hres
= args_to_param(ctx
, expr
->argument_list
, ei
, &dp
);
1563 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &constr
);
1564 exprval_release(&exprval
);
1568 if(V_VT(&constr
) != VT_DISPATCH
) {
1569 FIXME("throw TypeError\n");
1570 VariantClear(&constr
);
1574 hres
= disp_call(ctx
->parser
->script
, V_DISPATCH(&constr
), DISPID_VALUE
,
1575 DISPATCH_CONSTRUCT
, &dp
, &var
, ei
, NULL
/*FIXME*/);
1576 IDispatch_Release(V_DISPATCH(&constr
));
1581 ret
->type
= EXPRVAL_VARIANT
;
1586 /* ECMA-262 3rd Edition 11.2.3 */
1587 HRESULT
call_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1589 call_expression_t
*expr
= (call_expression_t
*)_expr
;
1597 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1601 hres
= args_to_param(ctx
, expr
->argument_list
, ei
, &dp
);
1602 if(SUCCEEDED(hres
)) {
1603 switch(exprval
.type
) {
1604 case EXPRVAL_VARIANT
:
1605 if(V_VT(&exprval
.u
.var
) == VT_DISPATCH
)
1606 hres
= disp_call(ctx
->parser
->script
, V_DISPATCH(&exprval
.u
.var
), DISPID_VALUE
,
1607 DISPATCH_METHOD
, &dp
, flags
& EXPR_NOVAL
? NULL
: &var
, ei
, NULL
/*FIXME*/);
1609 hres
= throw_type_error(ctx
->parser
->script
, ei
, IDS_NO_PROPERTY
, NULL
);
1612 hres
= disp_call(ctx
->parser
->script
, exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
,
1613 DISPATCH_METHOD
, &dp
, flags
& EXPR_NOVAL
? NULL
: &var
, ei
, NULL
/*FIXME*/);
1615 case EXPRVAL_INVALID
:
1616 hres
= throw_type_error(ctx
->parser
->script
, ei
, IDS_OBJECT_EXPECTED
, NULL
);
1619 FIXME("unimplemented type %d\n", exprval
.type
);
1626 exprval_release(&exprval
);
1630 ret
->type
= EXPRVAL_VARIANT
;
1631 if(flags
& EXPR_NOVAL
) {
1632 V_VT(&ret
->u
.var
) = VT_EMPTY
;
1634 TRACE("= %s\n", debugstr_variant(&var
));
1640 /* ECMA-262 3rd Edition 11.1.1 */
1641 HRESULT
this_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1645 ret
->type
= EXPRVAL_VARIANT
;
1646 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
1647 V_DISPATCH(&ret
->u
.var
) = ctx
->this_obj
;
1648 IDispatch_AddRef(ctx
->this_obj
);
1652 /* ECMA-262 3rd Edition 10.1.4 */
1653 HRESULT
identifier_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1655 identifier_expression_t
*expr
= (identifier_expression_t
*)_expr
;
1661 identifier
= SysAllocString(expr
->identifier
);
1663 return E_OUTOFMEMORY
;
1665 hres
= identifier_eval(ctx
, identifier
, flags
, ei
, ret
);
1667 SysFreeString(identifier
);
1671 /* ECMA-262 3rd Edition 7.8 */
1672 HRESULT
literal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1674 literal_expression_t
*expr
= (literal_expression_t
*)_expr
;
1680 hres
= literal_to_var(ctx
->parser
->script
, expr
->literal
, &var
);
1684 ret
->type
= EXPRVAL_VARIANT
;
1689 /* ECMA-262 3rd Edition 11.1.4 */
1690 HRESULT
array_literal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1692 array_literal_expression_t
*expr
= (array_literal_expression_t
*)_expr
;
1693 DWORD length
= 0, i
= 0;
1694 array_element_t
*elem
;
1702 for(elem
= expr
->element_list
; elem
; elem
= elem
->next
)
1703 length
+= elem
->elision
+1;
1704 length
+= expr
->length
;
1706 hres
= create_array(ctx
->parser
->script
, length
, &array
);
1710 for(elem
= expr
->element_list
; elem
; elem
= elem
->next
) {
1713 hres
= expr_eval(ctx
, elem
->expr
, 0, ei
, &exprval
);
1717 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1718 exprval_release(&exprval
);
1722 hres
= jsdisp_propput_idx(array
, i
, &val
, ei
, NULL
/*FIXME*/);
1731 jsdisp_release(array
);
1735 ret
->type
= EXPRVAL_VARIANT
;
1736 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
1737 V_DISPATCH(&ret
->u
.var
) = (IDispatch
*)_IDispatchEx_(array
);
1741 /* ECMA-262 3rd Edition 11.1.5 */
1742 HRESULT
property_value_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1744 property_value_expression_t
*expr
= (property_value_expression_t
*)_expr
;
1754 hres
= create_object(ctx
->parser
->script
, NULL
, &obj
);
1758 for(iter
= expr
->property_list
; iter
; iter
= iter
->next
) {
1759 hres
= literal_to_var(ctx
->parser
->script
, iter
->name
, &tmp
);
1763 hres
= to_string(ctx
->parser
->script
, &tmp
, ei
, &name
);
1768 hres
= expr_eval(ctx
, iter
->value
, 0, ei
, &exprval
);
1769 if(SUCCEEDED(hres
)) {
1770 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1771 exprval_release(&exprval
);
1772 if(SUCCEEDED(hres
)) {
1773 hres
= jsdisp_propput_name(obj
, name
, &val
, ei
, NULL
/*FIXME*/);
1778 SysFreeString(name
);
1784 jsdisp_release(obj
);
1788 ret
->type
= EXPRVAL_VARIANT
;
1789 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
1790 V_DISPATCH(&ret
->u
.var
) = (IDispatch
*)_IDispatchEx_(obj
);
1794 /* ECMA-262 3rd Edition 11.14 */
1795 HRESULT
comma_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1797 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1803 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
1807 VariantClear(&lval
);
1809 ret
->type
= EXPRVAL_VARIANT
;
1814 /* ECMA-262 3rd Edition 11.11 */
1815 HRESULT
logical_or_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1817 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1825 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1829 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1830 exprval_release(&exprval
);
1834 hres
= to_boolean(&val
, &b
);
1835 if(SUCCEEDED(hres
) && b
) {
1836 ret
->type
= EXPRVAL_VARIANT
;
1845 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1849 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1850 exprval_release(&exprval
);
1854 ret
->type
= EXPRVAL_VARIANT
;
1859 /* ECMA-262 3rd Edition 11.11 */
1860 HRESULT
logical_and_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1862 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1870 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1874 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1875 exprval_release(&exprval
);
1879 hres
= to_boolean(&val
, &b
);
1880 if(SUCCEEDED(hres
) && !b
) {
1881 ret
->type
= EXPRVAL_VARIANT
;
1890 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1894 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1895 exprval_release(&exprval
);
1899 ret
->type
= EXPRVAL_VARIANT
;
1904 /* ECMA-262 3rd Edition 11.10 */
1905 static HRESULT
bitor_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1910 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
1914 hres
= to_int32(ctx
->parser
->script
, rval
, ei
, &ri
);
1923 /* ECMA-262 3rd Edition 11.10 */
1924 HRESULT
binary_or_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1926 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1930 return binary_expr_eval(ctx
, expr
, bitor_eval
, ei
, ret
);
1933 /* ECMA-262 3rd Edition 11.10 */
1934 static HRESULT
xor_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1939 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
1943 hres
= to_int32(ctx
->parser
->script
, rval
, ei
, &ri
);
1952 /* ECMA-262 3rd Edition 11.10 */
1953 HRESULT
binary_xor_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1955 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1959 return binary_expr_eval(ctx
, expr
, xor_eval
, ei
, ret
);
1962 /* ECMA-262 3rd Edition 11.10 */
1963 static HRESULT
bitand_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1968 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
1972 hres
= to_int32(ctx
->parser
->script
, rval
, ei
, &ri
);
1981 /* ECMA-262 3rd Edition 11.10 */
1982 HRESULT
binary_and_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1984 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1988 return binary_expr_eval(ctx
, expr
, bitand_eval
, ei
, ret
);
1991 /* ECMA-262 3rd Edition 11.8.6 */
1992 static HRESULT
instanceof_eval(exec_ctx_t
*ctx
, VARIANT
*inst
, VARIANT
*objv
, jsexcept_t
*ei
, VARIANT
*retv
)
1994 DispatchEx
*obj
, *iter
, *tmp
= NULL
;
1995 VARIANT_BOOL ret
= VARIANT_FALSE
;
2000 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2002 if(V_VT(objv
) != VT_DISPATCH
) {
2003 FIXME("throw TypeError\n");
2007 obj
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(objv
));
2009 FIXME("throw TypeError\n");
2013 if(is_class(obj
, JSCLASS_FUNCTION
)) {
2014 hres
= jsdisp_propget_name(obj
, prototypeW
, &var
, ei
, NULL
/*FIXME*/);
2016 FIXME("throw TypeError\n");
2019 jsdisp_release(obj
);
2023 if(V_VT(&var
) == VT_DISPATCH
) {
2024 if(V_VT(inst
) == VT_DISPATCH
)
2025 tmp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(inst
));
2026 for(iter
= tmp
; iter
; iter
= iter
->prototype
) {
2027 hres
= disp_cmp(V_DISPATCH(&var
), (IDispatch
*)_IDispatchEx_(iter
), &b
);
2037 jsdisp_release(tmp
);
2039 FIXME("prototype is not an object\n");
2047 V_VT(retv
) = VT_BOOL
;
2052 /* ECMA-262 3rd Edition 11.8.6 */
2053 HRESULT
instanceof_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2055 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2059 return binary_expr_eval(ctx
, expr
, instanceof_eval
, ei
, ret
);
2062 /* ECMA-262 3rd Edition 11.8.7 */
2063 static HRESULT
in_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*obj
, jsexcept_t
*ei
, VARIANT
*retv
)
2070 if(V_VT(obj
) != VT_DISPATCH
) {
2071 FIXME("throw TypeError\n");
2075 hres
= to_string(ctx
->parser
->script
, lval
, ei
, &str
);
2079 hres
= disp_get_id(ctx
->parser
->script
, V_DISPATCH(obj
), str
, 0, &id
);
2083 else if(hres
== DISP_E_UNKNOWNNAME
)
2084 ret
= VARIANT_FALSE
;
2088 V_VT(retv
) = VT_BOOL
;
2093 /* ECMA-262 3rd Edition 11.8.7 */
2094 HRESULT
in_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2096 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2100 return binary_expr_eval(ctx
, expr
, in_eval
, ei
, ret
);
2103 /* ECMA-262 3rd Edition 11.6.1 */
2104 static HRESULT
add_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2109 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &l
, NO_HINT
);
2113 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &r
, NO_HINT
);
2119 if(V_VT(&l
) == VT_BSTR
|| V_VT(&r
) == VT_BSTR
) {
2120 BSTR lstr
= NULL
, rstr
= NULL
;
2122 if(V_VT(&l
) == VT_BSTR
)
2125 hres
= to_string(ctx
->parser
->script
, &l
, ei
, &lstr
);
2127 if(SUCCEEDED(hres
)) {
2128 if(V_VT(&r
) == VT_BSTR
)
2131 hres
= to_string(ctx
->parser
->script
, &r
, ei
, &rstr
);
2134 if(SUCCEEDED(hres
)) {
2137 len1
= SysStringLen(lstr
);
2138 len2
= SysStringLen(rstr
);
2140 V_VT(retv
) = VT_BSTR
;
2141 V_BSTR(retv
) = SysAllocStringLen(NULL
, len1
+len2
);
2142 memcpy(V_BSTR(retv
), lstr
, len1
*sizeof(WCHAR
));
2143 memcpy(V_BSTR(retv
)+len1
, rstr
, (len2
+1)*sizeof(WCHAR
));
2146 if(V_VT(&l
) != VT_BSTR
)
2147 SysFreeString(lstr
);
2148 if(V_VT(&r
) != VT_BSTR
)
2149 SysFreeString(rstr
);
2153 hres
= to_number(ctx
->parser
->script
, &l
, ei
, &nl
);
2154 if(SUCCEEDED(hres
)) {
2155 hres
= to_number(ctx
->parser
->script
, &r
, ei
, &nr
);
2157 num_set_val(retv
, num_val(&nl
) + num_val(&nr
));
2166 /* ECMA-262 3rd Edition 11.6.1 */
2167 HRESULT
add_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2169 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2173 return binary_expr_eval(ctx
, expr
, add_eval
, ei
, ret
);
2176 /* ECMA-262 3rd Edition 11.6.2 */
2177 static HRESULT
sub_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2182 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2186 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2190 num_set_val(retv
, num_val(&lnum
) - num_val(&rnum
));
2194 /* ECMA-262 3rd Edition 11.6.2 */
2195 HRESULT
sub_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2197 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2201 return binary_expr_eval(ctx
, expr
, sub_eval
, ei
, ret
);
2204 /* ECMA-262 3rd Edition 11.5.1 */
2205 static HRESULT
mul_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2210 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2214 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2218 num_set_val(retv
, num_val(&lnum
) * num_val(&rnum
));
2222 /* ECMA-262 3rd Edition 11.5.1 */
2223 HRESULT
mul_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2225 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2229 return binary_expr_eval(ctx
, expr
, mul_eval
, ei
, ret
);
2232 /* ECMA-262 3rd Edition 11.5.2 */
2233 static HRESULT
div_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2238 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2242 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2246 num_set_val(retv
, num_val(&lnum
) / num_val(&rnum
));
2250 /* ECMA-262 3rd Edition 11.5.2 */
2251 HRESULT
div_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2253 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2257 return binary_expr_eval(ctx
, expr
, div_eval
, ei
, ret
);
2260 /* ECMA-262 3rd Edition 11.5.3 */
2261 static HRESULT
mod_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2266 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2270 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2274 num_set_val(retv
, fmod(num_val(&lnum
), num_val(&rnum
)));
2278 /* ECMA-262 3rd Edition 11.5.3 */
2279 HRESULT
mod_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2281 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2285 return binary_expr_eval(ctx
, expr
, mod_eval
, ei
, ret
);
2288 /* ECMA-262 3rd Edition 11.4.2 */
2289 HRESULT
delete_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2291 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2292 VARIANT_BOOL b
= VARIANT_FALSE
;
2298 hres
= expr_eval(ctx
, expr
->expression
, EXPR_STRREF
, ei
, &exprval
);
2302 switch(exprval
.type
) {
2303 case EXPRVAL_IDREF
: {
2304 IDispatchEx
*dispex
;
2306 hres
= IDispatch_QueryInterface(exprval
.u
.nameref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2307 if(SUCCEEDED(hres
)) {
2308 hres
= IDispatchEx_DeleteMemberByDispID(dispex
, exprval
.u
.idref
.id
);
2310 IDispatchEx_Release(dispex
);
2314 case EXPRVAL_NAMEREF
: {
2315 IDispatchEx
*dispex
;
2317 hres
= IDispatch_QueryInterface(exprval
.u
.nameref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2318 if(SUCCEEDED(hres
)) {
2319 hres
= IDispatchEx_DeleteMemberByName(dispex
, exprval
.u
.nameref
.name
,
2320 make_grfdex(ctx
->parser
->script
, fdexNameCaseSensitive
));
2322 IDispatchEx_Release(dispex
);
2327 FIXME("unsupported type %d\n", exprval
.type
);
2331 exprval_release(&exprval
);
2335 return return_bool(ret
, b
);
2338 /* ECMA-262 3rd Edition 11.4.2 */
2339 HRESULT
void_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2341 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2348 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2352 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &tmp
);
2353 exprval_release(&exprval
);
2359 ret
->type
= EXPRVAL_VARIANT
;
2360 V_VT(&ret
->u
.var
) = VT_EMPTY
;
2364 /* ECMA-262 3rd Edition 11.4.3 */
2365 static HRESULT
typeof_exprval(exec_ctx_t
*ctx
, exprval_t
*exprval
, jsexcept_t
*ei
, const WCHAR
**ret
)
2370 static const WCHAR booleanW
[] = {'b','o','o','l','e','a','n',0};
2371 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
2372 static const WCHAR numberW
[] = {'n','u','m','b','e','r',0};
2373 static const WCHAR objectW
[] = {'o','b','j','e','c','t',0};
2374 static const WCHAR stringW
[] = {'s','t','r','i','n','g',0};
2375 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
2377 if(exprval
->type
== EXPRVAL_INVALID
) {
2382 hres
= exprval_to_value(ctx
->parser
->script
, exprval
, ei
, &val
);
2386 switch(V_VT(&val
)) {
2406 if(V_DISPATCH(&val
) && (dispex
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(&val
)))) {
2407 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
2408 jsdisp_release(dispex
);
2415 FIXME("unhandled vt %d\n", V_VT(&val
));
2423 HRESULT
typeof_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2425 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2426 const WCHAR
*str
= NULL
;
2432 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2436 hres
= typeof_exprval(ctx
, &exprval
, ei
, &str
);
2437 exprval_release(&exprval
);
2441 ret
->type
= EXPRVAL_VARIANT
;
2442 V_VT(&ret
->u
.var
) = VT_BSTR
;
2443 V_BSTR(&ret
->u
.var
) = SysAllocString(str
);
2444 if(!V_BSTR(&ret
->u
.var
))
2445 return E_OUTOFMEMORY
;
2450 /* ECMA-262 3rd Edition 11.4.7 */
2451 HRESULT
minus_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2453 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2460 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2464 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2465 exprval_release(&exprval
);
2469 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2474 ret
->type
= EXPRVAL_VARIANT
;
2475 num_set_val(&ret
->u
.var
, -num_val(&num
));
2479 /* ECMA-262 3rd Edition 11.4.6 */
2480 HRESULT
plus_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2482 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2489 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2493 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2494 exprval_release(&exprval
);
2498 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2503 ret
->type
= EXPRVAL_VARIANT
;
2508 /* ECMA-262 3rd Edition 11.3.1 */
2509 HRESULT
post_increment_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2511 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2518 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2522 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2523 if(SUCCEEDED(hres
)) {
2524 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2528 if(SUCCEEDED(hres
)) {
2530 num_set_val(&inc
, num_val(&num
)+1.0);
2531 hres
= put_value(ctx
->parser
->script
, &exprval
, &inc
, ei
);
2534 exprval_release(&exprval
);
2538 ret
->type
= EXPRVAL_VARIANT
;
2543 /* ECMA-262 3rd Edition 11.3.2 */
2544 HRESULT
post_decrement_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2546 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2553 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2557 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2558 if(SUCCEEDED(hres
)) {
2559 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2563 if(SUCCEEDED(hres
)) {
2565 num_set_val(&dec
, num_val(&num
)-1.0);
2566 hres
= put_value(ctx
->parser
->script
, &exprval
, &dec
, ei
);
2569 exprval_release(&exprval
);
2573 ret
->type
= EXPRVAL_VARIANT
;
2578 /* ECMA-262 3rd Edition 11.4.4 */
2579 HRESULT
pre_increment_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2581 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2588 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2592 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2593 if(SUCCEEDED(hres
)) {
2594 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2598 if(SUCCEEDED(hres
)) {
2599 num_set_val(&val
, num_val(&num
)+1.0);
2600 hres
= put_value(ctx
->parser
->script
, &exprval
, &val
, ei
);
2603 exprval_release(&exprval
);
2607 ret
->type
= EXPRVAL_VARIANT
;
2612 /* ECMA-262 3rd Edition 11.4.5 */
2613 HRESULT
pre_decrement_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2615 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2622 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2626 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2627 if(SUCCEEDED(hres
)) {
2628 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2632 if(SUCCEEDED(hres
)) {
2633 num_set_val(&val
, num_val(&num
)-1.0);
2634 hres
= put_value(ctx
->parser
->script
, &exprval
, &val
, ei
);
2637 exprval_release(&exprval
);
2641 ret
->type
= EXPRVAL_VARIANT
;
2646 /* ECMA-262 3rd Edition 11.9.3 */
2647 static HRESULT
equal_values(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, BOOL
*ret
)
2649 if(V_VT(lval
) == V_VT(rval
) || (is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
))))
2650 return equal2_values(lval
, rval
, ret
);
2652 /* FIXME: NULL disps should be handled in more general way */
2653 if(V_VT(lval
) == VT_DISPATCH
&& !V_DISPATCH(lval
)) {
2656 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2659 if(V_VT(rval
) == VT_DISPATCH
&& !V_DISPATCH(rval
)) {
2662 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2665 if((V_VT(lval
) == VT_NULL
&& V_VT(rval
) == VT_EMPTY
) ||
2666 (V_VT(lval
) == VT_EMPTY
&& V_VT(rval
) == VT_NULL
)) {
2671 if(V_VT(lval
) == VT_BSTR
&& is_num_vt(V_VT(rval
))) {
2675 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &v
);
2679 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2682 if(V_VT(rval
) == VT_BSTR
&& is_num_vt(V_VT(lval
))) {
2686 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &v
);
2690 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2693 if(V_VT(rval
) == VT_BOOL
) {
2697 V_I4(&v
) = V_BOOL(rval
) ? 1 : 0;
2698 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2701 if(V_VT(lval
) == VT_BOOL
) {
2705 V_I4(&v
) = V_BOOL(lval
) ? 1 : 0;
2706 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2710 if(V_VT(rval
) == VT_DISPATCH
&& (V_VT(lval
) == VT_BSTR
|| is_num_vt(V_VT(lval
)))) {
2714 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &v
, NO_HINT
);
2718 hres
= equal_values(ctx
, lval
, &v
, ei
, ret
);
2725 if(V_VT(lval
) == VT_DISPATCH
&& (V_VT(rval
) == VT_BSTR
|| is_num_vt(V_VT(rval
)))) {
2729 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &v
, NO_HINT
);
2733 hres
= equal_values(ctx
, &v
, rval
, ei
, ret
);
2744 /* ECMA-262 3rd Edition 11.9.1 */
2745 HRESULT
equal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2747 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2754 hres
= get_binary_expr_values(ctx
, expr
, ei
, &rval
, &lval
);
2758 hres
= equal_values(ctx
, &rval
, &lval
, ei
, &b
);
2759 VariantClear(&lval
);
2760 VariantClear(&rval
);
2764 return return_bool(ret
, b
);
2767 /* ECMA-262 3rd Edition 11.9.4 */
2768 HRESULT
equal2_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2770 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2777 hres
= get_binary_expr_values(ctx
, expr
, ei
, &rval
, &lval
);
2781 hres
= equal2_values(&rval
, &lval
, &b
);
2782 VariantClear(&lval
);
2783 VariantClear(&rval
);
2787 return return_bool(ret
, b
);
2790 /* ECMA-262 3rd Edition 11.9.2 */
2791 HRESULT
not_equal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2793 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2800 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2804 hres
= equal_values(ctx
, &lval
, &rval
, ei
, &b
);
2805 VariantClear(&lval
);
2806 VariantClear(&rval
);
2810 return return_bool(ret
, !b
);
2813 /* ECMA-262 3rd Edition 11.9.5 */
2814 HRESULT
not_equal2_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2816 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2823 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2827 hres
= equal2_values(&lval
, &rval
, &b
);
2828 VariantClear(&lval
);
2829 VariantClear(&rval
);
2833 return return_bool(ret
, !b
);
2836 /* ECMA-262 3rd Edition 11.8.5 */
2837 static HRESULT
less_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, BOOL greater
, jsexcept_t
*ei
, BOOL
*ret
)
2839 VARIANT l
, r
, ln
, rn
;
2842 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &l
, NO_HINT
);
2846 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &r
, NO_HINT
);
2852 if(V_VT(&l
) == VT_BSTR
&& V_VT(&r
) == VT_BSTR
) {
2853 *ret
= (strcmpW(V_BSTR(&l
), V_BSTR(&r
)) < 0) ^ greater
;
2854 SysFreeString(V_BSTR(&l
));
2855 SysFreeString(V_BSTR(&r
));
2859 hres
= to_number(ctx
->parser
->script
, &l
, ei
, &ln
);
2862 hres
= to_number(ctx
->parser
->script
, &r
, ei
, &rn
);
2867 if(V_VT(&ln
) == VT_I4
&& V_VT(&rn
) == VT_I4
) {
2868 *ret
= (V_I4(&ln
) < V_I4(&rn
)) ^ greater
;
2870 DOUBLE ld
= num_val(&ln
);
2871 DOUBLE rd
= num_val(&rn
);
2873 *ret
= !isnan(ld
) && !isnan(rd
) && ((ld
< rd
) ^ greater
);
2879 /* ECMA-262 3rd Edition 11.8.1 */
2880 HRESULT
less_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2882 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2889 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2893 hres
= less_eval(ctx
, &lval
, &rval
, FALSE
, ei
, &b
);
2894 VariantClear(&lval
);
2895 VariantClear(&rval
);
2899 return return_bool(ret
, b
);
2902 /* ECMA-262 3rd Edition 11.8.3 */
2903 HRESULT
lesseq_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2905 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2912 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2916 hres
= less_eval(ctx
, &rval
, &lval
, TRUE
, ei
, &b
);
2917 VariantClear(&lval
);
2918 VariantClear(&rval
);
2922 return return_bool(ret
, b
);
2925 /* ECMA-262 3rd Edition 11.8.2 */
2926 HRESULT
greater_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2928 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2935 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2939 hres
= less_eval(ctx
, &rval
, &lval
, FALSE
, ei
, &b
);
2940 VariantClear(&lval
);
2941 VariantClear(&rval
);
2945 return return_bool(ret
, b
);
2948 /* ECMA-262 3rd Edition 11.8.4 */
2949 HRESULT
greatereq_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2951 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2958 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2962 hres
= less_eval(ctx
, &lval
, &rval
, TRUE
, ei
, &b
);
2963 VariantClear(&lval
);
2964 VariantClear(&rval
);
2968 return return_bool(ret
, b
);
2971 /* ECMA-262 3rd Edition 11.4.8 */
2972 HRESULT
binary_negation_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2974 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2982 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2986 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2987 exprval_release(&exprval
);
2991 hres
= to_int32(ctx
->parser
->script
, &val
, ei
, &i
);
2995 ret
->type
= EXPRVAL_VARIANT
;
2996 V_VT(&ret
->u
.var
) = VT_I4
;
2997 V_I4(&ret
->u
.var
) = ~i
;
3001 /* ECMA-262 3rd Edition 11.4.9 */
3002 HRESULT
logical_negation_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3004 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
3011 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
3015 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, ei
, &b
);
3016 exprval_release(&exprval
);
3020 return return_bool(ret
, !b
);
3023 /* ECMA-262 3rd Edition 11.7.1 */
3024 static HRESULT
lshift_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3030 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
3034 hres
= to_uint32(ctx
->parser
->script
, rval
, ei
, &ri
);
3039 V_I4(retv
) = li
<< (ri
&0x1f);
3043 /* ECMA-262 3rd Edition 11.7.1 */
3044 HRESULT
left_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3046 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3050 return binary_expr_eval(ctx
, expr
, lshift_eval
, ei
, ret
);
3053 /* ECMA-262 3rd Edition 11.7.2 */
3054 static HRESULT
rshift_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3060 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
3064 hres
= to_uint32(ctx
->parser
->script
, rval
, ei
, &ri
);
3069 V_I4(retv
) = li
>> (ri
&0x1f);
3073 /* ECMA-262 3rd Edition 11.7.2 */
3074 HRESULT
right_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3076 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3080 return binary_expr_eval(ctx
, expr
, rshift_eval
, ei
, ret
);
3083 /* ECMA-262 3rd Edition 11.7.3 */
3084 static HRESULT
rshift2_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3089 hres
= to_uint32(ctx
->parser
->script
, lval
, ei
, &li
);
3093 hres
= to_uint32(ctx
->parser
->script
, rval
, ei
, &ri
);
3098 V_I4(retv
) = li
>> (ri
&0x1f);
3102 /* ECMA-262 3rd Edition 11.7.3 */
3103 HRESULT
right2_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3105 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3109 return binary_expr_eval(ctx
, expr
, rshift2_eval
, ei
, ret
);
3112 /* ECMA-262 3rd Edition 11.13.1 */
3113 HRESULT
assign_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3115 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3116 exprval_t exprval
, exprvalr
;
3122 hres
= expr_eval(ctx
, expr
->expression1
, EXPR_NEWREF
, ei
, &exprval
);
3126 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprvalr
);
3127 if(SUCCEEDED(hres
)) {
3128 hres
= exprval_to_value(ctx
->parser
->script
, &exprvalr
, ei
, &rval
);
3129 exprval_release(&exprvalr
);
3132 if(SUCCEEDED(hres
)) {
3133 hres
= put_value(ctx
->parser
->script
, &exprval
, &rval
, ei
);
3135 VariantClear(&rval
);
3138 exprval_release(&exprval
);
3142 ret
->type
= EXPRVAL_VARIANT
;
3147 /* ECMA-262 3rd Edition 11.13.2 */
3148 HRESULT
assign_lshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3150 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3154 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, lshift_eval
, ei
, ret
);
3157 /* ECMA-262 3rd Edition 11.13.2 */
3158 HRESULT
assign_rshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3160 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3164 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, rshift_eval
, ei
, ret
);
3167 /* ECMA-262 3rd Edition 11.13.2 */
3168 HRESULT
assign_rrshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3170 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3174 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, rshift2_eval
, ei
, ret
);
3177 /* ECMA-262 3rd Edition 11.13.2 */
3178 HRESULT
assign_add_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3180 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3184 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, add_eval
, ei
, ret
);
3187 /* ECMA-262 3rd Edition 11.13.2 */
3188 HRESULT
assign_sub_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3190 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3194 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, sub_eval
, ei
, ret
);
3197 /* ECMA-262 3rd Edition 11.13.2 */
3198 HRESULT
assign_mul_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3200 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3204 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, mul_eval
, ei
, ret
);
3207 /* ECMA-262 3rd Edition 11.13.2 */
3208 HRESULT
assign_div_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3210 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3214 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, div_eval
, ei
, ret
);
3217 /* ECMA-262 3rd Edition 11.13.2 */
3218 HRESULT
assign_mod_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3220 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3224 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, mod_eval
, ei
, ret
);
3227 /* ECMA-262 3rd Edition 11.13.2 */
3228 HRESULT
assign_and_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3230 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3234 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, bitand_eval
, ei
, ret
);
3237 /* ECMA-262 3rd Edition 11.13.2 */
3238 HRESULT
assign_or_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3240 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3244 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, bitor_eval
, ei
, ret
);
3247 /* ECMA-262 3rd Edition 11.13.2 */
3248 HRESULT
assign_xor_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3250 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3254 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, xor_eval
, ei
, ret
);