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
|| !V_DISPATCH(objv
))
2003 return throw_type_error(ctx
->parser
->script
, ei
, IDS_NOT_FUNC
, NULL
);
2005 obj
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(objv
));
2007 FIXME("throw TypeError\n");
2011 if(is_class(obj
, JSCLASS_FUNCTION
)) {
2012 hres
= jsdisp_propget_name(obj
, prototypeW
, &var
, ei
, NULL
/*FIXME*/);
2014 FIXME("throw TypeError\n");
2017 jsdisp_release(obj
);
2021 if(V_VT(&var
) == VT_DISPATCH
) {
2022 if(V_VT(inst
) == VT_DISPATCH
)
2023 tmp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(inst
));
2024 for(iter
= tmp
; iter
; iter
= iter
->prototype
) {
2025 hres
= disp_cmp(V_DISPATCH(&var
), (IDispatch
*)_IDispatchEx_(iter
), &b
);
2035 jsdisp_release(tmp
);
2037 FIXME("prototype is not an object\n");
2045 V_VT(retv
) = VT_BOOL
;
2050 /* ECMA-262 3rd Edition 11.8.6 */
2051 HRESULT
instanceof_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2053 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2057 return binary_expr_eval(ctx
, expr
, instanceof_eval
, ei
, ret
);
2060 /* ECMA-262 3rd Edition 11.8.7 */
2061 static HRESULT
in_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*obj
, jsexcept_t
*ei
, VARIANT
*retv
)
2068 if(V_VT(obj
) != VT_DISPATCH
|| !V_DISPATCH(obj
))
2069 return throw_type_error(ctx
->parser
->script
, ei
, IDS_OBJECT_EXPECTED
, NULL
);
2071 hres
= to_string(ctx
->parser
->script
, lval
, ei
, &str
);
2075 hres
= disp_get_id(ctx
->parser
->script
, V_DISPATCH(obj
), str
, 0, &id
);
2079 else if(hres
== DISP_E_UNKNOWNNAME
)
2080 ret
= VARIANT_FALSE
;
2084 V_VT(retv
) = VT_BOOL
;
2089 /* ECMA-262 3rd Edition 11.8.7 */
2090 HRESULT
in_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2092 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2096 return binary_expr_eval(ctx
, expr
, in_eval
, ei
, ret
);
2099 /* ECMA-262 3rd Edition 11.6.1 */
2100 static HRESULT
add_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2105 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &l
, NO_HINT
);
2109 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &r
, NO_HINT
);
2115 if(V_VT(&l
) == VT_BSTR
|| V_VT(&r
) == VT_BSTR
) {
2116 BSTR lstr
= NULL
, rstr
= NULL
;
2118 if(V_VT(&l
) == VT_BSTR
)
2121 hres
= to_string(ctx
->parser
->script
, &l
, ei
, &lstr
);
2123 if(SUCCEEDED(hres
)) {
2124 if(V_VT(&r
) == VT_BSTR
)
2127 hres
= to_string(ctx
->parser
->script
, &r
, ei
, &rstr
);
2130 if(SUCCEEDED(hres
)) {
2133 len1
= SysStringLen(lstr
);
2134 len2
= SysStringLen(rstr
);
2136 V_VT(retv
) = VT_BSTR
;
2137 V_BSTR(retv
) = SysAllocStringLen(NULL
, len1
+len2
);
2138 memcpy(V_BSTR(retv
), lstr
, len1
*sizeof(WCHAR
));
2139 memcpy(V_BSTR(retv
)+len1
, rstr
, (len2
+1)*sizeof(WCHAR
));
2142 if(V_VT(&l
) != VT_BSTR
)
2143 SysFreeString(lstr
);
2144 if(V_VT(&r
) != VT_BSTR
)
2145 SysFreeString(rstr
);
2149 hres
= to_number(ctx
->parser
->script
, &l
, ei
, &nl
);
2150 if(SUCCEEDED(hres
)) {
2151 hres
= to_number(ctx
->parser
->script
, &r
, ei
, &nr
);
2153 num_set_val(retv
, num_val(&nl
) + num_val(&nr
));
2162 /* ECMA-262 3rd Edition 11.6.1 */
2163 HRESULT
add_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2165 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2169 return binary_expr_eval(ctx
, expr
, add_eval
, ei
, ret
);
2172 /* ECMA-262 3rd Edition 11.6.2 */
2173 static HRESULT
sub_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2178 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2182 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2186 num_set_val(retv
, num_val(&lnum
) - num_val(&rnum
));
2190 /* ECMA-262 3rd Edition 11.6.2 */
2191 HRESULT
sub_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2193 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2197 return binary_expr_eval(ctx
, expr
, sub_eval
, ei
, ret
);
2200 /* ECMA-262 3rd Edition 11.5.1 */
2201 static HRESULT
mul_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2206 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2210 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2214 num_set_val(retv
, num_val(&lnum
) * num_val(&rnum
));
2218 /* ECMA-262 3rd Edition 11.5.1 */
2219 HRESULT
mul_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2221 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2225 return binary_expr_eval(ctx
, expr
, mul_eval
, ei
, ret
);
2228 /* ECMA-262 3rd Edition 11.5.2 */
2229 static HRESULT
div_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2234 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2238 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2242 num_set_val(retv
, num_val(&lnum
) / num_val(&rnum
));
2246 /* ECMA-262 3rd Edition 11.5.2 */
2247 HRESULT
div_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2249 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2253 return binary_expr_eval(ctx
, expr
, div_eval
, ei
, ret
);
2256 /* ECMA-262 3rd Edition 11.5.3 */
2257 static HRESULT
mod_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2262 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2266 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2270 num_set_val(retv
, fmod(num_val(&lnum
), num_val(&rnum
)));
2274 /* ECMA-262 3rd Edition 11.5.3 */
2275 HRESULT
mod_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2277 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2281 return binary_expr_eval(ctx
, expr
, mod_eval
, ei
, ret
);
2284 /* ECMA-262 3rd Edition 11.4.2 */
2285 HRESULT
delete_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2287 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2288 VARIANT_BOOL b
= VARIANT_FALSE
;
2294 hres
= expr_eval(ctx
, expr
->expression
, EXPR_STRREF
, ei
, &exprval
);
2298 switch(exprval
.type
) {
2299 case EXPRVAL_IDREF
: {
2300 IDispatchEx
*dispex
;
2302 hres
= IDispatch_QueryInterface(exprval
.u
.nameref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2303 if(SUCCEEDED(hres
)) {
2304 hres
= IDispatchEx_DeleteMemberByDispID(dispex
, exprval
.u
.idref
.id
);
2306 IDispatchEx_Release(dispex
);
2310 case EXPRVAL_NAMEREF
: {
2311 IDispatchEx
*dispex
;
2313 hres
= IDispatch_QueryInterface(exprval
.u
.nameref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2314 if(SUCCEEDED(hres
)) {
2315 hres
= IDispatchEx_DeleteMemberByName(dispex
, exprval
.u
.nameref
.name
,
2316 make_grfdex(ctx
->parser
->script
, fdexNameCaseSensitive
));
2318 IDispatchEx_Release(dispex
);
2323 FIXME("unsupported type %d\n", exprval
.type
);
2327 exprval_release(&exprval
);
2331 return return_bool(ret
, b
);
2334 /* ECMA-262 3rd Edition 11.4.2 */
2335 HRESULT
void_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2337 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2344 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2348 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &tmp
);
2349 exprval_release(&exprval
);
2355 ret
->type
= EXPRVAL_VARIANT
;
2356 V_VT(&ret
->u
.var
) = VT_EMPTY
;
2360 /* ECMA-262 3rd Edition 11.4.3 */
2361 static HRESULT
typeof_exprval(exec_ctx_t
*ctx
, exprval_t
*exprval
, jsexcept_t
*ei
, const WCHAR
**ret
)
2366 static const WCHAR booleanW
[] = {'b','o','o','l','e','a','n',0};
2367 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
2368 static const WCHAR numberW
[] = {'n','u','m','b','e','r',0};
2369 static const WCHAR objectW
[] = {'o','b','j','e','c','t',0};
2370 static const WCHAR stringW
[] = {'s','t','r','i','n','g',0};
2371 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
2373 if(exprval
->type
== EXPRVAL_INVALID
) {
2378 hres
= exprval_to_value(ctx
->parser
->script
, exprval
, ei
, &val
);
2382 switch(V_VT(&val
)) {
2402 if(V_DISPATCH(&val
) && (dispex
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(&val
)))) {
2403 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
2404 jsdisp_release(dispex
);
2411 FIXME("unhandled vt %d\n", V_VT(&val
));
2419 HRESULT
typeof_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2421 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2422 const WCHAR
*str
= NULL
;
2428 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2432 hres
= typeof_exprval(ctx
, &exprval
, ei
, &str
);
2433 exprval_release(&exprval
);
2437 ret
->type
= EXPRVAL_VARIANT
;
2438 V_VT(&ret
->u
.var
) = VT_BSTR
;
2439 V_BSTR(&ret
->u
.var
) = SysAllocString(str
);
2440 if(!V_BSTR(&ret
->u
.var
))
2441 return E_OUTOFMEMORY
;
2446 /* ECMA-262 3rd Edition 11.4.7 */
2447 HRESULT
minus_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2449 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2456 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2460 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2461 exprval_release(&exprval
);
2465 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2470 ret
->type
= EXPRVAL_VARIANT
;
2471 num_set_val(&ret
->u
.var
, -num_val(&num
));
2475 /* ECMA-262 3rd Edition 11.4.6 */
2476 HRESULT
plus_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2478 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2485 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2489 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2490 exprval_release(&exprval
);
2494 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2499 ret
->type
= EXPRVAL_VARIANT
;
2504 /* ECMA-262 3rd Edition 11.3.1 */
2505 HRESULT
post_increment_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2507 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2514 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2518 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2519 if(SUCCEEDED(hres
)) {
2520 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2524 if(SUCCEEDED(hres
)) {
2526 num_set_val(&inc
, num_val(&num
)+1.0);
2527 hres
= put_value(ctx
->parser
->script
, &exprval
, &inc
, ei
);
2530 exprval_release(&exprval
);
2534 ret
->type
= EXPRVAL_VARIANT
;
2539 /* ECMA-262 3rd Edition 11.3.2 */
2540 HRESULT
post_decrement_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2542 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2549 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2553 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2554 if(SUCCEEDED(hres
)) {
2555 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2559 if(SUCCEEDED(hres
)) {
2561 num_set_val(&dec
, num_val(&num
)-1.0);
2562 hres
= put_value(ctx
->parser
->script
, &exprval
, &dec
, ei
);
2565 exprval_release(&exprval
);
2569 ret
->type
= EXPRVAL_VARIANT
;
2574 /* ECMA-262 3rd Edition 11.4.4 */
2575 HRESULT
pre_increment_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2577 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2584 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2588 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2589 if(SUCCEEDED(hres
)) {
2590 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2594 if(SUCCEEDED(hres
)) {
2595 num_set_val(&val
, num_val(&num
)+1.0);
2596 hres
= put_value(ctx
->parser
->script
, &exprval
, &val
, ei
);
2599 exprval_release(&exprval
);
2603 ret
->type
= EXPRVAL_VARIANT
;
2608 /* ECMA-262 3rd Edition 11.4.5 */
2609 HRESULT
pre_decrement_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2611 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2618 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2622 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2623 if(SUCCEEDED(hres
)) {
2624 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2628 if(SUCCEEDED(hres
)) {
2629 num_set_val(&val
, num_val(&num
)-1.0);
2630 hres
= put_value(ctx
->parser
->script
, &exprval
, &val
, ei
);
2633 exprval_release(&exprval
);
2637 ret
->type
= EXPRVAL_VARIANT
;
2642 /* ECMA-262 3rd Edition 11.9.3 */
2643 static HRESULT
equal_values(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, BOOL
*ret
)
2645 if(V_VT(lval
) == V_VT(rval
) || (is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
))))
2646 return equal2_values(lval
, rval
, ret
);
2648 /* FIXME: NULL disps should be handled in more general way */
2649 if(V_VT(lval
) == VT_DISPATCH
&& !V_DISPATCH(lval
)) {
2652 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2655 if(V_VT(rval
) == VT_DISPATCH
&& !V_DISPATCH(rval
)) {
2658 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2661 if((V_VT(lval
) == VT_NULL
&& V_VT(rval
) == VT_EMPTY
) ||
2662 (V_VT(lval
) == VT_EMPTY
&& V_VT(rval
) == VT_NULL
)) {
2667 if(V_VT(lval
) == VT_BSTR
&& is_num_vt(V_VT(rval
))) {
2671 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &v
);
2675 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2678 if(V_VT(rval
) == VT_BSTR
&& is_num_vt(V_VT(lval
))) {
2682 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &v
);
2686 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2689 if(V_VT(rval
) == VT_BOOL
) {
2693 V_I4(&v
) = V_BOOL(rval
) ? 1 : 0;
2694 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2697 if(V_VT(lval
) == VT_BOOL
) {
2701 V_I4(&v
) = V_BOOL(lval
) ? 1 : 0;
2702 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2706 if(V_VT(rval
) == VT_DISPATCH
&& (V_VT(lval
) == VT_BSTR
|| is_num_vt(V_VT(lval
)))) {
2710 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &v
, NO_HINT
);
2714 hres
= equal_values(ctx
, lval
, &v
, ei
, ret
);
2721 if(V_VT(lval
) == VT_DISPATCH
&& (V_VT(rval
) == VT_BSTR
|| is_num_vt(V_VT(rval
)))) {
2725 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &v
, NO_HINT
);
2729 hres
= equal_values(ctx
, &v
, rval
, ei
, ret
);
2740 /* ECMA-262 3rd Edition 11.9.1 */
2741 HRESULT
equal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2743 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2750 hres
= get_binary_expr_values(ctx
, expr
, ei
, &rval
, &lval
);
2754 hres
= equal_values(ctx
, &rval
, &lval
, ei
, &b
);
2755 VariantClear(&lval
);
2756 VariantClear(&rval
);
2760 return return_bool(ret
, b
);
2763 /* ECMA-262 3rd Edition 11.9.4 */
2764 HRESULT
equal2_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2766 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2773 hres
= get_binary_expr_values(ctx
, expr
, ei
, &rval
, &lval
);
2777 hres
= equal2_values(&rval
, &lval
, &b
);
2778 VariantClear(&lval
);
2779 VariantClear(&rval
);
2783 return return_bool(ret
, b
);
2786 /* ECMA-262 3rd Edition 11.9.2 */
2787 HRESULT
not_equal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2789 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2796 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2800 hres
= equal_values(ctx
, &lval
, &rval
, ei
, &b
);
2801 VariantClear(&lval
);
2802 VariantClear(&rval
);
2806 return return_bool(ret
, !b
);
2809 /* ECMA-262 3rd Edition 11.9.5 */
2810 HRESULT
not_equal2_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2812 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2819 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2823 hres
= equal2_values(&lval
, &rval
, &b
);
2824 VariantClear(&lval
);
2825 VariantClear(&rval
);
2829 return return_bool(ret
, !b
);
2832 /* ECMA-262 3rd Edition 11.8.5 */
2833 static HRESULT
less_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, BOOL greater
, jsexcept_t
*ei
, BOOL
*ret
)
2835 VARIANT l
, r
, ln
, rn
;
2838 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &l
, NO_HINT
);
2842 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &r
, NO_HINT
);
2848 if(V_VT(&l
) == VT_BSTR
&& V_VT(&r
) == VT_BSTR
) {
2849 *ret
= (strcmpW(V_BSTR(&l
), V_BSTR(&r
)) < 0) ^ greater
;
2850 SysFreeString(V_BSTR(&l
));
2851 SysFreeString(V_BSTR(&r
));
2855 hres
= to_number(ctx
->parser
->script
, &l
, ei
, &ln
);
2858 hres
= to_number(ctx
->parser
->script
, &r
, ei
, &rn
);
2863 if(V_VT(&ln
) == VT_I4
&& V_VT(&rn
) == VT_I4
) {
2864 *ret
= (V_I4(&ln
) < V_I4(&rn
)) ^ greater
;
2866 DOUBLE ld
= num_val(&ln
);
2867 DOUBLE rd
= num_val(&rn
);
2869 *ret
= !isnan(ld
) && !isnan(rd
) && ((ld
< rd
) ^ greater
);
2875 /* ECMA-262 3rd Edition 11.8.1 */
2876 HRESULT
less_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2878 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2885 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2889 hres
= less_eval(ctx
, &lval
, &rval
, FALSE
, ei
, &b
);
2890 VariantClear(&lval
);
2891 VariantClear(&rval
);
2895 return return_bool(ret
, b
);
2898 /* ECMA-262 3rd Edition 11.8.3 */
2899 HRESULT
lesseq_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2901 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2908 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2912 hres
= less_eval(ctx
, &rval
, &lval
, TRUE
, ei
, &b
);
2913 VariantClear(&lval
);
2914 VariantClear(&rval
);
2918 return return_bool(ret
, b
);
2921 /* ECMA-262 3rd Edition 11.8.2 */
2922 HRESULT
greater_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2924 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2931 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2935 hres
= less_eval(ctx
, &rval
, &lval
, FALSE
, ei
, &b
);
2936 VariantClear(&lval
);
2937 VariantClear(&rval
);
2941 return return_bool(ret
, b
);
2944 /* ECMA-262 3rd Edition 11.8.4 */
2945 HRESULT
greatereq_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2947 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2954 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2958 hres
= less_eval(ctx
, &lval
, &rval
, TRUE
, ei
, &b
);
2959 VariantClear(&lval
);
2960 VariantClear(&rval
);
2964 return return_bool(ret
, b
);
2967 /* ECMA-262 3rd Edition 11.4.8 */
2968 HRESULT
binary_negation_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2970 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2978 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2982 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2983 exprval_release(&exprval
);
2987 hres
= to_int32(ctx
->parser
->script
, &val
, ei
, &i
);
2991 ret
->type
= EXPRVAL_VARIANT
;
2992 V_VT(&ret
->u
.var
) = VT_I4
;
2993 V_I4(&ret
->u
.var
) = ~i
;
2997 /* ECMA-262 3rd Edition 11.4.9 */
2998 HRESULT
logical_negation_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3000 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
3007 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
3011 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, ei
, &b
);
3012 exprval_release(&exprval
);
3016 return return_bool(ret
, !b
);
3019 /* ECMA-262 3rd Edition 11.7.1 */
3020 static HRESULT
lshift_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3026 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
3030 hres
= to_uint32(ctx
->parser
->script
, rval
, ei
, &ri
);
3035 V_I4(retv
) = li
<< (ri
&0x1f);
3039 /* ECMA-262 3rd Edition 11.7.1 */
3040 HRESULT
left_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3042 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3046 return binary_expr_eval(ctx
, expr
, lshift_eval
, ei
, ret
);
3049 /* ECMA-262 3rd Edition 11.7.2 */
3050 static HRESULT
rshift_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3056 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
3060 hres
= to_uint32(ctx
->parser
->script
, rval
, ei
, &ri
);
3065 V_I4(retv
) = li
>> (ri
&0x1f);
3069 /* ECMA-262 3rd Edition 11.7.2 */
3070 HRESULT
right_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3072 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3076 return binary_expr_eval(ctx
, expr
, rshift_eval
, ei
, ret
);
3079 /* ECMA-262 3rd Edition 11.7.3 */
3080 static HRESULT
rshift2_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3085 hres
= to_uint32(ctx
->parser
->script
, lval
, ei
, &li
);
3089 hres
= to_uint32(ctx
->parser
->script
, rval
, ei
, &ri
);
3094 V_I4(retv
) = li
>> (ri
&0x1f);
3098 /* ECMA-262 3rd Edition 11.7.3 */
3099 HRESULT
right2_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3101 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3105 return binary_expr_eval(ctx
, expr
, rshift2_eval
, ei
, ret
);
3108 /* ECMA-262 3rd Edition 11.13.1 */
3109 HRESULT
assign_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3111 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3112 exprval_t exprval
, exprvalr
;
3118 hres
= expr_eval(ctx
, expr
->expression1
, EXPR_NEWREF
, ei
, &exprval
);
3122 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprvalr
);
3123 if(SUCCEEDED(hres
)) {
3124 hres
= exprval_to_value(ctx
->parser
->script
, &exprvalr
, ei
, &rval
);
3125 exprval_release(&exprvalr
);
3128 if(SUCCEEDED(hres
)) {
3129 hres
= put_value(ctx
->parser
->script
, &exprval
, &rval
, ei
);
3131 VariantClear(&rval
);
3134 exprval_release(&exprval
);
3138 ret
->type
= EXPRVAL_VARIANT
;
3143 /* ECMA-262 3rd Edition 11.13.2 */
3144 HRESULT
assign_lshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3146 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3150 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, lshift_eval
, ei
, ret
);
3153 /* ECMA-262 3rd Edition 11.13.2 */
3154 HRESULT
assign_rshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3156 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3160 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, rshift_eval
, ei
, ret
);
3163 /* ECMA-262 3rd Edition 11.13.2 */
3164 HRESULT
assign_rrshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3166 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3170 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, rshift2_eval
, ei
, ret
);
3173 /* ECMA-262 3rd Edition 11.13.2 */
3174 HRESULT
assign_add_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3176 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3180 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, add_eval
, ei
, ret
);
3183 /* ECMA-262 3rd Edition 11.13.2 */
3184 HRESULT
assign_sub_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3186 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3190 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, sub_eval
, ei
, ret
);
3193 /* ECMA-262 3rd Edition 11.13.2 */
3194 HRESULT
assign_mul_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3196 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3200 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, mul_eval
, ei
, ret
);
3203 /* ECMA-262 3rd Edition 11.13.2 */
3204 HRESULT
assign_div_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3206 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3210 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, div_eval
, ei
, ret
);
3213 /* ECMA-262 3rd Edition 11.13.2 */
3214 HRESULT
assign_mod_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3216 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3220 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, mod_eval
, ei
, ret
);
3223 /* ECMA-262 3rd Edition 11.13.2 */
3224 HRESULT
assign_and_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3226 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3230 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, bitand_eval
, ei
, ret
);
3233 /* ECMA-262 3rd Edition 11.13.2 */
3234 HRESULT
assign_or_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3236 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3240 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, bitor_eval
, ei
, ret
);
3243 /* ECMA-262 3rd Edition 11.13.2 */
3244 HRESULT
assign_xor_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3246 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3250 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, xor_eval
, ei
, ret
);