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
) {
361 V_I4(v
) = literal
->u
.lval
;
365 V_R8(v
) = literal
->u
.dval
;
368 BSTR str
= SysAllocString(literal
->u
.wstr
);
370 return E_OUTOFMEMORY
;
378 V_BOOL(v
) = literal
->u
.bval
;
384 hres
= create_regexp(ctx
, literal
->u
.regexp
.str
, literal
->u
.regexp
.str_len
,
385 literal
->u
.regexp
.flags
, ®exp
);
389 V_VT(v
) = VT_DISPATCH
;
390 V_DISPATCH(v
) = (IDispatch
*)_IDispatchEx_(regexp
);
397 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
403 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
404 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
405 hres
= disp_get_id(ctx
, item
->disp
, identifier
, 0, &id
);
406 if(SUCCEEDED(hres
)) {
408 exprval_set_idref(ret
, item
->disp
, id
);
417 HRESULT
exec_source(exec_ctx_t
*ctx
, parser_ctx_t
*parser
, source_elements_t
*source
, exec_type_t exec_type
,
418 jsexcept_t
*ei
, VARIANT
*retv
)
420 script_ctx_t
*script
= parser
->script
;
421 function_declaration_t
*func
;
422 parser_ctx_t
*prev_parser
;
426 exec_ctx_t
*prev_ctx
;
430 for(func
= source
->functions
; func
; func
= func
->next
) {
431 DispatchEx
*func_obj
;
434 hres
= create_source_function(parser
, func
->expr
->parameter_list
, func
->expr
->source_elements
,
435 ctx
->scope_chain
, func
->expr
->src_str
, func
->expr
->src_len
, &func_obj
);
439 V_VT(&var
) = VT_DISPATCH
;
440 V_DISPATCH(&var
) = (IDispatch
*)_IDispatchEx_(func_obj
);
441 hres
= jsdisp_propput_name(ctx
->var_disp
, func
->expr
->identifier
, &var
, ei
, NULL
);
442 jsdisp_release(func_obj
);
447 for(var
= source
->variables
; var
; var
= var
->next
) {
451 name
= SysAllocString(var
->identifier
);
453 return E_OUTOFMEMORY
;
455 if(!lookup_global_members(parser
->script
, name
, NULL
))
456 hres
= jsdisp_get_id(ctx
->var_disp
, var
->identifier
, fdexNameEnsure
, &id
);
462 prev_ctx
= script
->exec_ctx
;
463 script
->exec_ctx
= ctx
;
465 prev_parser
= ctx
->parser
;
466 ctx
->parser
= parser
;
468 V_VT(&val
) = VT_EMPTY
;
469 memset(&rt
, 0, sizeof(rt
));
472 for(stat
= source
->statement
; stat
; stat
= stat
->next
) {
473 hres
= stat_eval(ctx
, stat
, &rt
, &tmp
);
479 if(rt
.type
!= RT_NORMAL
)
483 script
->exec_ctx
= prev_ctx
;
484 ctx
->parser
= prev_parser
;
486 if(rt
.type
!= RT_NORMAL
&& rt
.type
!= RT_RETURN
) {
487 FIXME("wrong rt %d\n", rt
.type
);
497 if(retv
&& (exec_type
== EXECT_EVAL
|| rt
.type
== RT_RETURN
))
504 /* ECMA-262 3rd Edition 10.1.4 */
505 static HRESULT
identifier_eval(exec_ctx_t
*ctx
, BSTR identifier
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
507 scope_chain_t
*scope
;
512 TRACE("%s\n", debugstr_w(identifier
));
514 for(scope
= ctx
->scope_chain
; scope
; scope
= scope
->next
) {
515 hres
= jsdisp_get_id(scope
->obj
, identifier
, 0, &id
);
521 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(scope
->obj
), id
);
525 hres
= jsdisp_get_id(ctx
->parser
->script
->global
, identifier
, 0, &id
);
526 if(SUCCEEDED(hres
)) {
527 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(ctx
->parser
->script
->global
), id
);
531 for(item
= ctx
->parser
->script
->named_items
; item
; item
= item
->next
) {
532 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
536 if(!ctx
->parser
->script
->site
)
539 hres
= IActiveScriptSite_GetItemInfo(ctx
->parser
->script
->site
, identifier
,
540 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
542 WARN("GetItemInfo failed: %08x\n", hres
);
546 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
547 IUnknown_Release(unk
);
549 WARN("object does not implement IDispatch\n");
554 ret
->type
= EXPRVAL_VARIANT
;
555 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
556 V_DISPATCH(&ret
->u
.var
) = item
->disp
;
557 IDispatch_AddRef(item
->disp
);
562 if(lookup_global_members(ctx
->parser
->script
, identifier
, ret
))
565 if(flags
& EXPR_NEWREF
) {
566 hres
= jsdisp_get_id(ctx
->parser
->script
->global
, identifier
, fdexNameEnsure
, &id
);
570 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(ctx
->parser
->script
->global
), id
);
574 ret
->type
= EXPRVAL_INVALID
;
575 ret
->u
.identifier
= SysAllocString(identifier
);
576 if(!ret
->u
.identifier
)
577 return E_OUTOFMEMORY
;
582 /* ECMA-262 3rd Edition 12.1 */
583 HRESULT
block_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
585 block_statement_t
*stat
= (block_statement_t
*)_stat
;
592 V_VT(&val
) = VT_EMPTY
;
593 for(iter
= stat
->stat_list
; iter
; iter
= iter
->next
) {
594 hres
= stat_eval(ctx
, iter
, rt
, &tmp
);
600 if(rt
->type
!= RT_NORMAL
)
613 /* ECMA-262 3rd Edition 12.2 */
614 static HRESULT
variable_list_eval(exec_ctx_t
*ctx
, variable_declaration_t
*var_list
, jsexcept_t
*ei
)
616 variable_declaration_t
*iter
;
619 for(iter
= var_list
; iter
; iter
= iter
->next
) {
626 hres
= expr_eval(ctx
, iter
->expr
, 0, ei
, &exprval
);
630 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
631 exprval_release(&exprval
);
635 hres
= jsdisp_propput_name(ctx
->var_disp
, iter
->identifier
, &val
, ei
, NULL
/*FIXME*/);
644 /* ECMA-262 3rd Edition 12.2 */
645 HRESULT
var_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
647 var_statement_t
*stat
= (var_statement_t
*)_stat
;
652 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
656 V_VT(ret
) = VT_EMPTY
;
660 /* ECMA-262 3rd Edition 12.3 */
661 HRESULT
empty_statement_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
665 V_VT(ret
) = VT_EMPTY
;
669 /* ECMA-262 3rd Edition 12.4 */
670 HRESULT
expression_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
672 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
679 hres
= expr_eval(ctx
, stat
->expr
, EXPR_NOVAL
, &rt
->ei
, &exprval
);
683 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
684 exprval_release(&exprval
);
689 TRACE("= %s\n", debugstr_variant(ret
));
693 /* ECMA-262 3rd Edition 12.5 */
694 HRESULT
if_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
696 if_statement_t
*stat
= (if_statement_t
*)_stat
;
703 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
707 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, &rt
->ei
, &b
);
708 exprval_release(&exprval
);
713 hres
= stat_eval(ctx
, stat
->if_stat
, rt
, ret
);
714 else if(stat
->else_stat
)
715 hres
= stat_eval(ctx
, stat
->else_stat
, rt
, ret
);
717 V_VT(ret
) = VT_EMPTY
;
722 /* ECMA-262 3rd Edition 12.6.2 */
723 HRESULT
while_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
725 while_statement_t
*stat
= (while_statement_t
*)_stat
;
734 V_VT(&val
) = VT_EMPTY
;
735 test_expr
= !stat
->do_while
;
739 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
743 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, &rt
->ei
, &b
);
744 exprval_release(&exprval
);
745 if(FAILED(hres
) || !b
)
751 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
758 if(rt
->type
== RT_CONTINUE
)
759 rt
->type
= RT_NORMAL
;
760 if(rt
->type
!= RT_NORMAL
)
769 if(rt
->type
== RT_BREAK
)
770 rt
->type
= RT_NORMAL
;
776 /* ECMA-262 3rd Edition 12.6.3 */
777 HRESULT
for_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
779 for_statement_t
*stat
= (for_statement_t
*)_stat
;
780 VARIANT val
, tmp
, retv
;
787 if(stat
->variable_list
) {
788 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
791 }else if(stat
->begin_expr
) {
792 hres
= expr_eval(ctx
, stat
->begin_expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
796 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
797 exprval_release(&exprval
);
804 V_VT(&retv
) = VT_EMPTY
;
808 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
812 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, &rt
->ei
, &b
);
813 exprval_release(&exprval
);
814 if(FAILED(hres
) || !b
)
818 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
825 if(rt
->type
== RT_CONTINUE
)
826 rt
->type
= RT_NORMAL
;
827 else if(rt
->type
!= RT_NORMAL
)
831 hres
= expr_eval(ctx
, stat
->end_expr
, 0, &rt
->ei
, &exprval
);
835 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
836 exprval_release(&exprval
);
849 if(rt
->type
== RT_BREAK
)
850 rt
->type
= RT_NORMAL
;
856 /* ECMA-262 3rd Edition 12.6.4 */
857 HRESULT
forin_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
859 forin_statement_t
*stat
= (forin_statement_t
*)_stat
;
860 VARIANT val
, name
, retv
, tmp
;
861 DISPID id
= DISPID_STARTENUM
;
862 BSTR str
, identifier
= NULL
;
870 hres
= variable_list_eval(ctx
, stat
->variable
, &rt
->ei
);
875 hres
= expr_eval(ctx
, stat
->in_expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
879 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
880 exprval_release(&exprval
);
884 if(V_VT(&val
) != VT_DISPATCH
) {
885 TRACE("in vt %d\n", V_VT(&val
));
887 V_VT(ret
) = VT_EMPTY
;
891 hres
= IDispatch_QueryInterface(V_DISPATCH(&val
), &IID_IDispatchEx
, (void**)&in_obj
);
892 IDispatch_Release(V_DISPATCH(&val
));
894 FIXME("Object doesn't support IDispatchEx\n");
898 V_VT(&retv
) = VT_EMPTY
;
901 identifier
= SysAllocString(stat
->variable
->identifier
);
904 hres
= IDispatchEx_GetNextDispID(in_obj
, fdexEnumDefault
, id
, &id
);
905 if(FAILED(hres
) || hres
== S_FALSE
)
908 hres
= IDispatchEx_GetMemberName(in_obj
, id
, &str
);
912 TRACE("iter %s\n", debugstr_w(str
));
915 hres
= identifier_eval(ctx
, identifier
, 0, NULL
, &exprval
);
917 hres
= expr_eval(ctx
, stat
->expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
918 if(SUCCEEDED(hres
)) {
919 V_VT(&name
) = VT_BSTR
;
921 hres
= put_value(ctx
->parser
->script
, &exprval
, &name
, &rt
->ei
);
922 exprval_release(&exprval
);
928 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
935 if(rt
->type
== RT_CONTINUE
)
936 rt
->type
= RT_NORMAL
;
937 else if(rt
->type
!= RT_NORMAL
)
941 SysFreeString(identifier
);
942 IDispatchEx_Release(in_obj
);
948 if(rt
->type
== RT_BREAK
)
949 rt
->type
= RT_NORMAL
;
955 /* ECMA-262 3rd Edition 12.7 */
956 HRESULT
continue_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
958 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
962 if(stat
->identifier
) {
963 FIXME("indentifier not implemented\n");
967 rt
->type
= RT_CONTINUE
;
968 V_VT(ret
) = VT_EMPTY
;
972 /* ECMA-262 3rd Edition 12.8 */
973 HRESULT
break_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
975 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
979 if(stat
->identifier
) {
980 FIXME("indentifier not implemented\n");
985 V_VT(ret
) = VT_EMPTY
;
989 /* ECMA-262 3rd Edition 12.9 */
990 HRESULT
return_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
992 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
1000 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1004 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, ret
);
1005 exprval_release(&exprval
);
1009 V_VT(ret
) = VT_EMPTY
;
1012 TRACE("= %s\n", debugstr_variant(ret
));
1013 rt
->type
= RT_RETURN
;
1017 /* ECMA-262 3rd Edition 12.10 */
1018 HRESULT
with_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1020 with_statement_t
*stat
= (with_statement_t
*)_stat
;
1029 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1033 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
1034 exprval_release(&exprval
);
1038 hres
= to_object(ctx
->parser
->script
, &val
, &disp
);
1043 obj
= iface_to_jsdisp((IUnknown
*)disp
);
1044 IDispatch_Release(disp
);
1046 FIXME("disp id not jsdisp\n");
1050 hres
= scope_push(ctx
->scope_chain
, obj
, &ctx
->scope_chain
);
1051 jsdisp_release(obj
);
1055 hres
= stat_eval(ctx
, stat
->statement
, rt
, ret
);
1057 scope_pop(&ctx
->scope_chain
);
1061 /* ECMA-262 3rd Edition 12.12 */
1062 HRESULT
labelled_statement_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
1068 /* ECMA-262 3rd Edition 12.13 */
1069 HRESULT
switch_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1071 switch_statement_t
*stat
= (switch_statement_t
*)_stat
;
1072 case_clausule_t
*iter
, *default_clausule
= NULL
;
1073 statement_t
*stat_iter
;
1081 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1085 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
1086 exprval_release(&exprval
);
1090 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1092 default_clausule
= iter
;
1096 hres
= expr_eval(ctx
, iter
->expr
, 0, &rt
->ei
, &exprval
);
1100 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &cval
);
1101 exprval_release(&exprval
);
1105 hres
= equal2_values(&val
, &cval
, &b
);
1106 VariantClear(&cval
);
1107 if(FAILED(hres
) || b
)
1116 iter
= default_clausule
;
1118 V_VT(&val
) = VT_EMPTY
;
1122 for(stat_iter
= iter
->stat
; stat_iter
; stat_iter
= stat_iter
->next
) {
1123 hres
= stat_eval(ctx
, stat_iter
, rt
, &tmp
);
1130 if(rt
->type
!= RT_NORMAL
)
1140 if(rt
->type
== RT_BREAK
)
1141 rt
->type
= RT_NORMAL
;
1147 /* ECMA-262 3rd Edition 12.13 */
1148 HRESULT
throw_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1150 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
1157 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1161 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
1162 exprval_release(&exprval
);
1167 return DISP_E_EXCEPTION
;
1170 /* ECMA-262 3rd Edition 12.14 */
1171 static HRESULT
catch_eval(exec_ctx_t
*ctx
, catch_block_t
*block
, return_type_t
*rt
, VARIANT
*ret
)
1173 DispatchEx
*var_disp
;
1178 memset(&rt
->ei
, 0, sizeof(jsexcept_t
));
1180 hres
= create_dispex(ctx
->parser
->script
, NULL
, NULL
, &var_disp
);
1181 if(SUCCEEDED(hres
)) {
1182 hres
= jsdisp_propput_name(var_disp
, block
->identifier
, &ex
, &rt
->ei
, NULL
/*FIXME*/);
1183 if(SUCCEEDED(hres
)) {
1184 hres
= scope_push(ctx
->scope_chain
, var_disp
, &ctx
->scope_chain
);
1185 if(SUCCEEDED(hres
)) {
1186 hres
= stat_eval(ctx
, block
->statement
, rt
, &val
);
1187 scope_pop(&ctx
->scope_chain
);
1191 jsdisp_release(var_disp
);
1202 /* ECMA-262 3rd Edition 12.14 */
1203 HRESULT
try_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1205 try_statement_t
*stat
= (try_statement_t
*)_stat
;
1211 hres
= stat_eval(ctx
, stat
->try_statement
, rt
, &val
);
1213 TRACE("EXCEPTION\n");
1214 if(!stat
->catch_block
)
1217 hres
= catch_eval(ctx
, stat
->catch_block
, rt
, &val
);
1222 if(stat
->finally_statement
) {
1224 hres
= stat_eval(ctx
, stat
->finally_statement
, rt
, &val
);
1233 static HRESULT
return_bool(exprval_t
*ret
, DWORD b
)
1235 ret
->type
= EXPRVAL_VARIANT
;
1236 V_VT(&ret
->u
.var
) = VT_BOOL
;
1237 V_BOOL(&ret
->u
.var
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
1242 static HRESULT
get_binary_expr_values(exec_ctx_t
*ctx
, binary_expression_t
*expr
, jsexcept_t
*ei
, VARIANT
*lval
, VARIANT
*rval
)
1247 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1251 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, lval
);
1252 exprval_release(&exprval
);
1256 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1257 if(SUCCEEDED(hres
)) {
1258 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, rval
);
1259 exprval_release(&exprval
);
1270 typedef HRESULT (*oper_t
)(exec_ctx_t
*,VARIANT
*,VARIANT
*,jsexcept_t
*,VARIANT
*);
1272 static HRESULT
binary_expr_eval(exec_ctx_t
*ctx
, binary_expression_t
*expr
, oper_t oper
, jsexcept_t
*ei
,
1275 VARIANT lval
, rval
, retv
;
1278 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
1282 hres
= oper(ctx
, &lval
, &rval
, ei
, &retv
);
1283 VariantClear(&lval
);
1284 VariantClear(&rval
);
1288 ret
->type
= EXPRVAL_VARIANT
;
1293 /* ECMA-262 3rd Edition 11.13.2 */
1294 static HRESULT
assign_oper_eval(exec_ctx_t
*ctx
, expression_t
*lexpr
, expression_t
*rexpr
, oper_t oper
,
1295 jsexcept_t
*ei
, exprval_t
*ret
)
1297 VARIANT retv
, lval
, rval
;
1298 exprval_t exprval
, exprvalr
;
1301 hres
= expr_eval(ctx
, lexpr
, EXPR_NEWREF
, ei
, &exprval
);
1305 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &lval
);
1306 if(SUCCEEDED(hres
)) {
1307 hres
= expr_eval(ctx
, rexpr
, 0, ei
, &exprvalr
);
1308 if(SUCCEEDED(hres
)) {
1309 hres
= exprval_value(ctx
->parser
->script
, &exprvalr
, ei
, &rval
);
1310 exprval_release(&exprvalr
);
1312 if(SUCCEEDED(hres
)) {
1313 hres
= oper(ctx
, &lval
, &rval
, ei
, &retv
);
1314 VariantClear(&rval
);
1316 VariantClear(&lval
);
1319 if(SUCCEEDED(hres
)) {
1320 hres
= put_value(ctx
->parser
->script
, &exprval
, &retv
, ei
);
1322 VariantClear(&retv
);
1324 exprval_release(&exprval
);
1329 ret
->type
= EXPRVAL_VARIANT
;
1334 /* ECMA-262 3rd Edition 13 */
1335 HRESULT
function_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1337 function_expression_t
*expr
= (function_expression_t
*)_expr
;
1343 if(expr
->identifier
) {
1344 hres
= jsdisp_propget_name(ctx
->var_disp
, expr
->identifier
, &var
, ei
, NULL
/*FIXME*/);
1350 hres
= create_source_function(ctx
->parser
, expr
->parameter_list
, expr
->source_elements
, ctx
->scope_chain
,
1351 expr
->src_str
, expr
->src_len
, &dispex
);
1355 V_VT(&var
) = VT_DISPATCH
;
1356 V_DISPATCH(&var
) = (IDispatch
*)_IDispatchEx_(dispex
);
1359 ret
->type
= EXPRVAL_VARIANT
;
1364 /* ECMA-262 3rd Edition 11.12 */
1365 HRESULT
conditional_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1367 conditional_expression_t
*expr
= (conditional_expression_t
*)_expr
;
1374 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1378 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, ei
, &b
);
1379 exprval_release(&exprval
);
1383 return expr_eval(ctx
, b
? expr
->true_expression
: expr
->false_expression
, flags
, ei
, ret
);
1386 /* ECMA-262 3rd Edition 11.2.1 */
1387 HRESULT
array_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1389 array_expression_t
*expr
= (array_expression_t
*)_expr
;
1391 VARIANT member
, val
;
1394 IDispatch
*obj
= NULL
;
1399 hres
= expr_eval(ctx
, expr
->member_expr
, EXPR_NEWREF
, ei
, &exprval
);
1403 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &member
);
1404 exprval_release(&exprval
);
1408 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
1409 if(SUCCEEDED(hres
)) {
1410 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1411 exprval_release(&exprval
);
1415 hres
= to_object(ctx
->parser
->script
, &member
, &obj
);
1416 VariantClear(&member
);
1417 if(SUCCEEDED(hres
)) {
1418 hres
= to_string(ctx
->parser
->script
, &val
, ei
, &str
);
1419 if(SUCCEEDED(hres
)) {
1420 if(flags
& EXPR_STRREF
) {
1421 ret
->type
= EXPRVAL_NAMEREF
;
1422 ret
->u
.nameref
.disp
= obj
;
1423 ret
->u
.nameref
.name
= str
;
1427 hres
= disp_get_id(ctx
->parser
->script
, obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1430 if(SUCCEEDED(hres
)) {
1431 exprval_set_idref(ret
, obj
, id
);
1432 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1437 IDispatch_Release(obj
);
1443 /* ECMA-262 3rd Edition 11.2.1 */
1444 HRESULT
member_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1446 member_expression_t
*expr
= (member_expression_t
*)_expr
;
1447 IDispatch
*obj
= NULL
;
1456 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1460 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &member
);
1461 exprval_release(&exprval
);
1465 hres
= to_object(ctx
->parser
->script
, &member
, &obj
);
1466 VariantClear(&member
);
1470 str
= SysAllocString(expr
->identifier
);
1471 if(flags
& EXPR_STRREF
) {
1472 ret
->type
= EXPRVAL_NAMEREF
;
1473 ret
->u
.nameref
.disp
= obj
;
1474 ret
->u
.nameref
.name
= str
;
1478 hres
= disp_get_id(ctx
->parser
->script
, obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1480 if(SUCCEEDED(hres
)) {
1481 exprval_set_idref(ret
, obj
, id
);
1482 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1487 IDispatch_Release(obj
);
1491 static void free_dp(DISPPARAMS
*dp
)
1495 for(i
=0; i
< dp
->cArgs
; i
++)
1496 VariantClear(dp
->rgvarg
+i
);
1497 heap_free(dp
->rgvarg
);
1500 static HRESULT
args_to_param(exec_ctx_t
*ctx
, argument_t
*args
, jsexcept_t
*ei
, DISPPARAMS
*dp
)
1506 HRESULT hres
= S_OK
;
1508 memset(dp
, 0, sizeof(*dp
));
1512 for(iter
= args
; iter
; iter
= iter
->next
)
1515 vargs
= heap_alloc_zero(cnt
* sizeof(*vargs
));
1517 return E_OUTOFMEMORY
;
1519 for(i
= cnt
, iter
= args
; iter
; iter
= iter
->next
) {
1520 hres
= expr_eval(ctx
, iter
->expr
, 0, ei
, &exprval
);
1524 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, vargs
+ (--i
));
1525 exprval_release(&exprval
);
1540 /* ECMA-262 3rd Edition 11.2.2 */
1541 HRESULT
new_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1543 call_expression_t
*expr
= (call_expression_t
*)_expr
;
1545 VARIANT constr
, var
;
1551 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1555 hres
= args_to_param(ctx
, expr
->argument_list
, ei
, &dp
);
1557 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &constr
);
1558 exprval_release(&exprval
);
1562 if(V_VT(&constr
) != VT_DISPATCH
) {
1563 FIXME("throw TypeError\n");
1564 VariantClear(&constr
);
1568 hres
= disp_call(ctx
->parser
->script
, V_DISPATCH(&constr
), DISPID_VALUE
,
1569 DISPATCH_CONSTRUCT
, &dp
, &var
, ei
, NULL
/*FIXME*/);
1570 IDispatch_Release(V_DISPATCH(&constr
));
1574 ret
->type
= EXPRVAL_VARIANT
;
1579 /* ECMA-262 3rd Edition 11.2.3 */
1580 HRESULT
call_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1582 call_expression_t
*expr
= (call_expression_t
*)_expr
;
1590 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1594 hres
= args_to_param(ctx
, expr
->argument_list
, ei
, &dp
);
1595 if(SUCCEEDED(hres
)) {
1596 switch(exprval
.type
) {
1597 case EXPRVAL_VARIANT
:
1598 if(V_VT(&exprval
.u
.var
) == VT_DISPATCH
)
1599 hres
= disp_call(ctx
->parser
->script
, V_DISPATCH(&exprval
.u
.var
), DISPID_VALUE
,
1600 DISPATCH_METHOD
, &dp
, flags
& EXPR_NOVAL
? NULL
: &var
, ei
, NULL
/*FIXME*/);
1602 hres
= throw_type_error(ctx
->parser
->script
, ei
, IDS_NO_PROPERTY
, NULL
);
1605 hres
= disp_call(ctx
->parser
->script
, exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
,
1606 DISPATCH_METHOD
, &dp
, flags
& EXPR_NOVAL
? NULL
: &var
, ei
, NULL
/*FIXME*/);
1608 case EXPRVAL_INVALID
:
1609 hres
= throw_type_error(ctx
->parser
->script
, ei
, IDS_OBJECT_EXPECTED
, NULL
);
1612 FIXME("unimplemented type %d\n", exprval
.type
);
1619 exprval_release(&exprval
);
1623 ret
->type
= EXPRVAL_VARIANT
;
1624 if(flags
& EXPR_NOVAL
) {
1625 V_VT(&ret
->u
.var
) = VT_EMPTY
;
1627 TRACE("= %s\n", debugstr_variant(&var
));
1633 /* ECMA-262 3rd Edition 11.1.1 */
1634 HRESULT
this_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1638 ret
->type
= EXPRVAL_VARIANT
;
1639 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
1640 V_DISPATCH(&ret
->u
.var
) = ctx
->this_obj
;
1641 IDispatch_AddRef(ctx
->this_obj
);
1645 /* ECMA-262 3rd Edition 10.1.4 */
1646 HRESULT
identifier_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1648 identifier_expression_t
*expr
= (identifier_expression_t
*)_expr
;
1654 identifier
= SysAllocString(expr
->identifier
);
1656 return E_OUTOFMEMORY
;
1658 hres
= identifier_eval(ctx
, identifier
, flags
, ei
, ret
);
1660 SysFreeString(identifier
);
1664 /* ECMA-262 3rd Edition 7.8 */
1665 HRESULT
literal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1667 literal_expression_t
*expr
= (literal_expression_t
*)_expr
;
1673 hres
= literal_to_var(ctx
->parser
->script
, expr
->literal
, &var
);
1677 ret
->type
= EXPRVAL_VARIANT
;
1682 /* ECMA-262 3rd Edition 11.1.4 */
1683 HRESULT
array_literal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1685 array_literal_expression_t
*expr
= (array_literal_expression_t
*)_expr
;
1686 DWORD length
= 0, i
= 0;
1687 array_element_t
*elem
;
1695 for(elem
= expr
->element_list
; elem
; elem
= elem
->next
)
1696 length
+= elem
->elision
+1;
1697 length
+= expr
->length
;
1699 hres
= create_array(ctx
->parser
->script
, length
, &array
);
1703 for(elem
= expr
->element_list
; elem
; elem
= elem
->next
) {
1706 hres
= expr_eval(ctx
, elem
->expr
, 0, ei
, &exprval
);
1710 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1711 exprval_release(&exprval
);
1715 hres
= jsdisp_propput_idx(array
, i
, &val
, ei
, NULL
/*FIXME*/);
1724 jsdisp_release(array
);
1728 ret
->type
= EXPRVAL_VARIANT
;
1729 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
1730 V_DISPATCH(&ret
->u
.var
) = (IDispatch
*)_IDispatchEx_(array
);
1734 /* ECMA-262 3rd Edition 11.1.5 */
1735 HRESULT
property_value_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1737 property_value_expression_t
*expr
= (property_value_expression_t
*)_expr
;
1747 hres
= create_object(ctx
->parser
->script
, NULL
, &obj
);
1751 for(iter
= expr
->property_list
; iter
; iter
= iter
->next
) {
1752 hres
= literal_to_var(ctx
->parser
->script
, iter
->name
, &tmp
);
1756 hres
= to_string(ctx
->parser
->script
, &tmp
, ei
, &name
);
1761 hres
= expr_eval(ctx
, iter
->value
, 0, ei
, &exprval
);
1762 if(SUCCEEDED(hres
)) {
1763 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1764 exprval_release(&exprval
);
1765 if(SUCCEEDED(hres
)) {
1766 hres
= jsdisp_propput_name(obj
, name
, &val
, ei
, NULL
/*FIXME*/);
1771 SysFreeString(name
);
1777 jsdisp_release(obj
);
1781 ret
->type
= EXPRVAL_VARIANT
;
1782 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
1783 V_DISPATCH(&ret
->u
.var
) = (IDispatch
*)_IDispatchEx_(obj
);
1787 /* ECMA-262 3rd Edition 11.14 */
1788 HRESULT
comma_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1790 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1796 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
1800 VariantClear(&lval
);
1802 ret
->type
= EXPRVAL_VARIANT
;
1807 /* ECMA-262 3rd Edition 11.11 */
1808 HRESULT
logical_or_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1810 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1818 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1822 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1823 exprval_release(&exprval
);
1827 hres
= to_boolean(&val
, &b
);
1828 if(SUCCEEDED(hres
) && b
) {
1829 ret
->type
= EXPRVAL_VARIANT
;
1838 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1842 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1843 exprval_release(&exprval
);
1847 ret
->type
= EXPRVAL_VARIANT
;
1852 /* ECMA-262 3rd Edition 11.11 */
1853 HRESULT
logical_and_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1855 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1863 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1867 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1868 exprval_release(&exprval
);
1872 hres
= to_boolean(&val
, &b
);
1873 if(SUCCEEDED(hres
) && !b
) {
1874 ret
->type
= EXPRVAL_VARIANT
;
1883 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1887 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1888 exprval_release(&exprval
);
1892 ret
->type
= EXPRVAL_VARIANT
;
1897 /* ECMA-262 3rd Edition 11.10 */
1898 static HRESULT
bitor_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1903 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
1907 hres
= to_int32(ctx
->parser
->script
, rval
, ei
, &ri
);
1916 /* ECMA-262 3rd Edition 11.10 */
1917 HRESULT
binary_or_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1919 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1923 return binary_expr_eval(ctx
, expr
, bitor_eval
, ei
, ret
);
1926 /* ECMA-262 3rd Edition 11.10 */
1927 static HRESULT
xor_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1932 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
1936 hres
= to_int32(ctx
->parser
->script
, rval
, ei
, &ri
);
1945 /* ECMA-262 3rd Edition 11.10 */
1946 HRESULT
binary_xor_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1948 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1952 return binary_expr_eval(ctx
, expr
, xor_eval
, ei
, ret
);
1955 /* ECMA-262 3rd Edition 11.10 */
1956 static HRESULT
bitand_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1961 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
1965 hres
= to_int32(ctx
->parser
->script
, rval
, ei
, &ri
);
1974 /* ECMA-262 3rd Edition 11.10 */
1975 HRESULT
binary_and_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1977 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1981 return binary_expr_eval(ctx
, expr
, bitand_eval
, ei
, ret
);
1984 /* ECMA-262 3rd Edition 11.8.6 */
1985 static HRESULT
instanceof_eval(exec_ctx_t
*ctx
, VARIANT
*inst
, VARIANT
*objv
, jsexcept_t
*ei
, VARIANT
*retv
)
1987 DispatchEx
*obj
, *iter
, *tmp
= NULL
;
1988 VARIANT_BOOL ret
= VARIANT_FALSE
;
1993 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1995 if(V_VT(objv
) != VT_DISPATCH
) {
1996 FIXME("throw TypeError\n");
2000 obj
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(objv
));
2002 FIXME("throw TypeError\n");
2006 if(is_class(obj
, JSCLASS_FUNCTION
)) {
2007 hres
= jsdisp_propget_name(obj
, prototypeW
, &var
, ei
, NULL
/*FIXME*/);
2009 FIXME("throw TypeError\n");
2012 jsdisp_release(obj
);
2016 if(V_VT(&var
) == VT_DISPATCH
) {
2017 if(V_VT(inst
) == VT_DISPATCH
)
2018 tmp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(inst
));
2019 for(iter
= tmp
; iter
; iter
= iter
->prototype
) {
2020 hres
= disp_cmp(V_DISPATCH(&var
), (IDispatch
*)_IDispatchEx_(iter
), &b
);
2030 jsdisp_release(tmp
);
2032 FIXME("prototype is not an object\n");
2040 V_VT(retv
) = VT_BOOL
;
2045 /* ECMA-262 3rd Edition 11.8.6 */
2046 HRESULT
instanceof_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2048 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2052 return binary_expr_eval(ctx
, expr
, instanceof_eval
, ei
, ret
);
2055 /* ECMA-262 3rd Edition 11.8.7 */
2056 static HRESULT
in_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*obj
, jsexcept_t
*ei
, VARIANT
*retv
)
2063 if(V_VT(obj
) != VT_DISPATCH
) {
2064 FIXME("throw TypeError\n");
2068 hres
= to_string(ctx
->parser
->script
, lval
, ei
, &str
);
2072 hres
= disp_get_id(ctx
->parser
->script
, V_DISPATCH(obj
), str
, 0, &id
);
2076 else if(hres
== DISP_E_UNKNOWNNAME
)
2077 ret
= VARIANT_FALSE
;
2081 V_VT(retv
) = VT_BOOL
;
2086 /* ECMA-262 3rd Edition 11.8.7 */
2087 HRESULT
in_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2089 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2093 return binary_expr_eval(ctx
, expr
, in_eval
, ei
, ret
);
2096 /* ECMA-262 3rd Edition 11.6.1 */
2097 static HRESULT
add_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2102 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &l
, NO_HINT
);
2106 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &r
, NO_HINT
);
2112 if(V_VT(&l
) == VT_BSTR
|| V_VT(&r
) == VT_BSTR
) {
2113 BSTR lstr
= NULL
, rstr
= NULL
;
2115 if(V_VT(&l
) == VT_BSTR
)
2118 hres
= to_string(ctx
->parser
->script
, &l
, ei
, &lstr
);
2120 if(SUCCEEDED(hres
)) {
2121 if(V_VT(&r
) == VT_BSTR
)
2124 hres
= to_string(ctx
->parser
->script
, &r
, ei
, &rstr
);
2127 if(SUCCEEDED(hres
)) {
2130 len1
= SysStringLen(lstr
);
2131 len2
= SysStringLen(rstr
);
2133 V_VT(retv
) = VT_BSTR
;
2134 V_BSTR(retv
) = SysAllocStringLen(NULL
, len1
+len2
);
2135 memcpy(V_BSTR(retv
), lstr
, len1
*sizeof(WCHAR
));
2136 memcpy(V_BSTR(retv
)+len1
, rstr
, (len2
+1)*sizeof(WCHAR
));
2139 if(V_VT(&l
) != VT_BSTR
)
2140 SysFreeString(lstr
);
2141 if(V_VT(&r
) != VT_BSTR
)
2142 SysFreeString(rstr
);
2146 hres
= to_number(ctx
->parser
->script
, &l
, ei
, &nl
);
2147 if(SUCCEEDED(hres
)) {
2148 hres
= to_number(ctx
->parser
->script
, &r
, ei
, &nr
);
2150 num_set_val(retv
, num_val(&nl
) + num_val(&nr
));
2159 /* ECMA-262 3rd Edition 11.6.1 */
2160 HRESULT
add_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2162 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2166 return binary_expr_eval(ctx
, expr
, add_eval
, ei
, ret
);
2169 /* ECMA-262 3rd Edition 11.6.2 */
2170 static HRESULT
sub_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2175 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2179 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2183 num_set_val(retv
, num_val(&lnum
) - num_val(&rnum
));
2187 /* ECMA-262 3rd Edition 11.6.2 */
2188 HRESULT
sub_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2190 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2194 return binary_expr_eval(ctx
, expr
, sub_eval
, ei
, ret
);
2197 /* ECMA-262 3rd Edition 11.5.1 */
2198 static HRESULT
mul_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2203 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2207 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2211 num_set_val(retv
, num_val(&lnum
) * num_val(&rnum
));
2215 /* ECMA-262 3rd Edition 11.5.1 */
2216 HRESULT
mul_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2218 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2222 return binary_expr_eval(ctx
, expr
, mul_eval
, ei
, ret
);
2225 /* ECMA-262 3rd Edition 11.5.2 */
2226 static HRESULT
div_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2231 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2235 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2239 num_set_val(retv
, num_val(&lnum
) / num_val(&rnum
));
2243 /* ECMA-262 3rd Edition 11.5.2 */
2244 HRESULT
div_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2246 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2250 return binary_expr_eval(ctx
, expr
, div_eval
, ei
, ret
);
2253 /* ECMA-262 3rd Edition 11.5.3 */
2254 static HRESULT
mod_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2259 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2263 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2267 num_set_val(retv
, fmod(num_val(&lnum
), num_val(&rnum
)));
2271 /* ECMA-262 3rd Edition 11.5.3 */
2272 HRESULT
mod_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2274 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2278 return binary_expr_eval(ctx
, expr
, mod_eval
, ei
, ret
);
2281 /* ECMA-262 3rd Edition 11.4.2 */
2282 HRESULT
delete_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2284 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2285 VARIANT_BOOL b
= VARIANT_FALSE
;
2291 hres
= expr_eval(ctx
, expr
->expression
, EXPR_STRREF
, ei
, &exprval
);
2295 switch(exprval
.type
) {
2296 case EXPRVAL_IDREF
: {
2297 IDispatchEx
*dispex
;
2299 hres
= IDispatch_QueryInterface(exprval
.u
.nameref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2300 if(SUCCEEDED(hres
)) {
2301 hres
= IDispatchEx_DeleteMemberByDispID(dispex
, exprval
.u
.idref
.id
);
2303 IDispatchEx_Release(dispex
);
2307 case EXPRVAL_NAMEREF
: {
2308 IDispatchEx
*dispex
;
2310 hres
= IDispatch_QueryInterface(exprval
.u
.nameref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2311 if(SUCCEEDED(hres
)) {
2312 hres
= IDispatchEx_DeleteMemberByName(dispex
, exprval
.u
.nameref
.name
,
2313 make_grfdex(ctx
->parser
->script
, fdexNameCaseSensitive
));
2315 IDispatchEx_Release(dispex
);
2320 FIXME("unsupported type %d\n", exprval
.type
);
2324 exprval_release(&exprval
);
2328 return return_bool(ret
, b
);
2331 /* ECMA-262 3rd Edition 11.4.2 */
2332 HRESULT
void_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2334 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2341 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2345 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &tmp
);
2346 exprval_release(&exprval
);
2352 ret
->type
= EXPRVAL_VARIANT
;
2353 V_VT(&ret
->u
.var
) = VT_EMPTY
;
2357 /* ECMA-262 3rd Edition 11.4.3 */
2358 static HRESULT
typeof_exprval(exec_ctx_t
*ctx
, exprval_t
*exprval
, jsexcept_t
*ei
, const WCHAR
**ret
)
2363 static const WCHAR booleanW
[] = {'b','o','o','l','e','a','n',0};
2364 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
2365 static const WCHAR numberW
[] = {'n','u','m','b','e','r',0};
2366 static const WCHAR objectW
[] = {'o','b','j','e','c','t',0};
2367 static const WCHAR stringW
[] = {'s','t','r','i','n','g',0};
2368 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
2370 if(exprval
->type
== EXPRVAL_INVALID
) {
2375 hres
= exprval_to_value(ctx
->parser
->script
, exprval
, ei
, &val
);
2379 switch(V_VT(&val
)) {
2399 if(V_DISPATCH(&val
) && (dispex
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(&val
)))) {
2400 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
2401 jsdisp_release(dispex
);
2408 FIXME("unhandled vt %d\n", V_VT(&val
));
2416 HRESULT
typeof_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2418 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2419 const WCHAR
*str
= NULL
;
2425 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2429 hres
= typeof_exprval(ctx
, &exprval
, ei
, &str
);
2430 exprval_release(&exprval
);
2434 ret
->type
= EXPRVAL_VARIANT
;
2435 V_VT(&ret
->u
.var
) = VT_BSTR
;
2436 V_BSTR(&ret
->u
.var
) = SysAllocString(str
);
2437 if(!V_BSTR(&ret
->u
.var
))
2438 return E_OUTOFMEMORY
;
2443 /* ECMA-262 3rd Edition 11.4.7 */
2444 HRESULT
minus_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2446 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2453 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2457 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2458 exprval_release(&exprval
);
2462 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2467 ret
->type
= EXPRVAL_VARIANT
;
2468 num_set_val(&ret
->u
.var
, -num_val(&num
));
2472 /* ECMA-262 3rd Edition 11.4.6 */
2473 HRESULT
plus_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2475 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2482 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2486 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2487 exprval_release(&exprval
);
2491 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2495 ret
->type
= EXPRVAL_VARIANT
;
2500 /* ECMA-262 3rd Edition 11.3.1 */
2501 HRESULT
post_increment_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2503 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2510 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2514 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2515 if(SUCCEEDED(hres
)) {
2516 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2520 if(SUCCEEDED(hres
)) {
2522 num_set_val(&inc
, num_val(&num
)+1.0);
2523 hres
= put_value(ctx
->parser
->script
, &exprval
, &inc
, ei
);
2526 exprval_release(&exprval
);
2530 ret
->type
= EXPRVAL_VARIANT
;
2535 /* ECMA-262 3rd Edition 11.3.2 */
2536 HRESULT
post_decrement_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2538 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2545 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2549 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2550 if(SUCCEEDED(hres
)) {
2551 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2555 if(SUCCEEDED(hres
)) {
2557 num_set_val(&dec
, num_val(&num
)-1.0);
2558 hres
= put_value(ctx
->parser
->script
, &exprval
, &dec
, ei
);
2561 exprval_release(&exprval
);
2565 ret
->type
= EXPRVAL_VARIANT
;
2570 /* ECMA-262 3rd Edition 11.4.4 */
2571 HRESULT
pre_increment_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2573 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2580 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2584 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2585 if(SUCCEEDED(hres
)) {
2586 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2590 if(SUCCEEDED(hres
)) {
2591 num_set_val(&val
, num_val(&num
)+1.0);
2592 hres
= put_value(ctx
->parser
->script
, &exprval
, &val
, ei
);
2595 exprval_release(&exprval
);
2599 ret
->type
= EXPRVAL_VARIANT
;
2604 /* ECMA-262 3rd Edition 11.4.5 */
2605 HRESULT
pre_decrement_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2607 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2614 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2618 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2619 if(SUCCEEDED(hres
)) {
2620 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2624 if(SUCCEEDED(hres
)) {
2625 num_set_val(&val
, num_val(&num
)-1.0);
2626 hres
= put_value(ctx
->parser
->script
, &exprval
, &val
, ei
);
2629 exprval_release(&exprval
);
2633 ret
->type
= EXPRVAL_VARIANT
;
2638 /* ECMA-262 3rd Edition 11.9.3 */
2639 static HRESULT
equal_values(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, BOOL
*ret
)
2641 if(V_VT(lval
) == V_VT(rval
) || (is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
))))
2642 return equal2_values(lval
, rval
, ret
);
2644 /* FIXME: NULL disps should be handled in more general way */
2645 if(V_VT(lval
) == VT_DISPATCH
&& !V_DISPATCH(lval
)) {
2648 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2651 if(V_VT(rval
) == VT_DISPATCH
&& !V_DISPATCH(rval
)) {
2654 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2657 if((V_VT(lval
) == VT_NULL
&& V_VT(rval
) == VT_EMPTY
) ||
2658 (V_VT(lval
) == VT_EMPTY
&& V_VT(rval
) == VT_NULL
)) {
2663 if(V_VT(lval
) == VT_BSTR
&& is_num_vt(V_VT(rval
))) {
2667 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &v
);
2671 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2674 if(V_VT(rval
) == VT_BSTR
&& is_num_vt(V_VT(lval
))) {
2678 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &v
);
2682 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2685 if(V_VT(rval
) == VT_BOOL
) {
2689 V_I4(&v
) = V_BOOL(rval
) ? 1 : 0;
2690 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2693 if(V_VT(lval
) == VT_BOOL
) {
2697 V_I4(&v
) = V_BOOL(lval
) ? 1 : 0;
2698 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2702 if(V_VT(rval
) == VT_DISPATCH
&& (V_VT(lval
) == VT_BSTR
|| is_num_vt(V_VT(lval
)))) {
2706 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &v
, NO_HINT
);
2710 hres
= equal_values(ctx
, lval
, &v
, ei
, ret
);
2717 if(V_VT(lval
) == VT_DISPATCH
&& (V_VT(rval
) == VT_BSTR
|| is_num_vt(V_VT(rval
)))) {
2721 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &v
, NO_HINT
);
2725 hres
= equal_values(ctx
, &v
, rval
, ei
, ret
);
2736 /* ECMA-262 3rd Edition 11.9.1 */
2737 HRESULT
equal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2739 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2746 hres
= get_binary_expr_values(ctx
, expr
, ei
, &rval
, &lval
);
2750 hres
= equal_values(ctx
, &rval
, &lval
, ei
, &b
);
2754 return return_bool(ret
, b
);
2757 /* ECMA-262 3rd Edition 11.9.4 */
2758 HRESULT
equal2_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2760 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2767 hres
= get_binary_expr_values(ctx
, expr
, ei
, &rval
, &lval
);
2771 hres
= equal2_values(&rval
, &lval
, &b
);
2775 return return_bool(ret
, b
);
2778 /* ECMA-262 3rd Edition 11.9.2 */
2779 HRESULT
not_equal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2781 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2788 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2792 hres
= equal_values(ctx
, &lval
, &rval
, ei
, &b
);
2796 return return_bool(ret
, !b
);
2799 /* ECMA-262 3rd Edition 11.9.5 */
2800 HRESULT
not_equal2_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2802 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2809 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2813 hres
= equal2_values(&lval
, &rval
, &b
);
2817 return return_bool(ret
, !b
);
2820 /* ECMA-262 3rd Edition 11.8.5 */
2821 static HRESULT
less_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, BOOL greater
, jsexcept_t
*ei
, BOOL
*ret
)
2823 VARIANT l
, r
, ln
, rn
;
2826 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &l
, NO_HINT
);
2830 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &r
, NO_HINT
);
2836 if(V_VT(&l
) == VT_BSTR
&& V_VT(&r
) == VT_BSTR
) {
2837 *ret
= (strcmpW(V_BSTR(&l
), V_BSTR(&r
)) < 0) ^ greater
;
2838 SysFreeString(V_BSTR(&l
));
2839 SysFreeString(V_BSTR(&r
));
2843 hres
= to_number(ctx
->parser
->script
, &l
, ei
, &ln
);
2846 hres
= to_number(ctx
->parser
->script
, &r
, ei
, &rn
);
2851 if(V_VT(&ln
) == VT_I4
&& V_VT(&rn
) == VT_I4
) {
2852 *ret
= (V_I4(&ln
) < V_I4(&rn
)) ^ greater
;
2854 DOUBLE ld
= num_val(&ln
);
2855 DOUBLE rd
= num_val(&rn
);
2857 *ret
= !isnan(ld
) && !isnan(rd
) && ((ld
< rd
) ^ greater
);
2863 /* ECMA-262 3rd Edition 11.8.1 */
2864 HRESULT
less_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2866 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2873 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2877 hres
= less_eval(ctx
, &lval
, &rval
, FALSE
, ei
, &b
);
2878 VariantClear(&lval
);
2879 VariantClear(&rval
);
2883 return return_bool(ret
, b
);
2886 /* ECMA-262 3rd Edition 11.8.3 */
2887 HRESULT
lesseq_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2889 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2896 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2900 hres
= less_eval(ctx
, &rval
, &lval
, TRUE
, ei
, &b
);
2901 VariantClear(&lval
);
2902 VariantClear(&rval
);
2906 return return_bool(ret
, b
);
2909 /* ECMA-262 3rd Edition 11.8.2 */
2910 HRESULT
greater_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2912 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2919 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2923 hres
= less_eval(ctx
, &rval
, &lval
, FALSE
, ei
, &b
);
2924 VariantClear(&lval
);
2925 VariantClear(&rval
);
2929 return return_bool(ret
, b
);
2932 /* ECMA-262 3rd Edition 11.8.4 */
2933 HRESULT
greatereq_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2935 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2942 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2946 hres
= less_eval(ctx
, &lval
, &rval
, TRUE
, ei
, &b
);
2947 VariantClear(&lval
);
2948 VariantClear(&rval
);
2952 return return_bool(ret
, b
);
2955 /* ECMA-262 3rd Edition 11.4.8 */
2956 HRESULT
binary_negation_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2958 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2966 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2970 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2971 exprval_release(&exprval
);
2975 hres
= to_int32(ctx
->parser
->script
, &val
, ei
, &i
);
2979 ret
->type
= EXPRVAL_VARIANT
;
2980 V_VT(&ret
->u
.var
) = VT_I4
;
2981 V_I4(&ret
->u
.var
) = ~i
;
2985 /* ECMA-262 3rd Edition 11.4.9 */
2986 HRESULT
logical_negation_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2988 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2995 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2999 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, ei
, &b
);
3000 exprval_release(&exprval
);
3004 return return_bool(ret
, !b
);
3007 /* ECMA-262 3rd Edition 11.7.1 */
3008 static HRESULT
lshift_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3014 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
3018 hres
= to_uint32(ctx
->parser
->script
, rval
, ei
, &ri
);
3023 V_I4(retv
) = li
<< (ri
&0x1f);
3027 /* ECMA-262 3rd Edition 11.7.1 */
3028 HRESULT
left_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3030 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3034 return binary_expr_eval(ctx
, expr
, lshift_eval
, ei
, ret
);
3037 /* ECMA-262 3rd Edition 11.7.2 */
3038 static HRESULT
rshift_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3044 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
3048 hres
= to_uint32(ctx
->parser
->script
, rval
, ei
, &ri
);
3053 V_I4(retv
) = li
>> (ri
&0x1f);
3057 /* ECMA-262 3rd Edition 11.7.2 */
3058 HRESULT
right_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3060 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3064 return binary_expr_eval(ctx
, expr
, rshift_eval
, ei
, ret
);
3067 /* ECMA-262 3rd Edition 11.7.3 */
3068 static HRESULT
rshift2_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3073 hres
= to_uint32(ctx
->parser
->script
, lval
, ei
, &li
);
3077 hres
= to_uint32(ctx
->parser
->script
, rval
, ei
, &ri
);
3082 V_I4(retv
) = li
>> (ri
&0x1f);
3086 /* ECMA-262 3rd Edition 11.7.3 */
3087 HRESULT
right2_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3089 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3093 return binary_expr_eval(ctx
, expr
, rshift2_eval
, ei
, ret
);
3096 /* ECMA-262 3rd Edition 11.13.1 */
3097 HRESULT
assign_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3099 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3100 exprval_t exprval
, exprvalr
;
3106 hres
= expr_eval(ctx
, expr
->expression1
, EXPR_NEWREF
, ei
, &exprval
);
3110 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprvalr
);
3111 if(SUCCEEDED(hres
)) {
3112 hres
= exprval_to_value(ctx
->parser
->script
, &exprvalr
, ei
, &rval
);
3113 exprval_release(&exprvalr
);
3117 hres
= put_value(ctx
->parser
->script
, &exprval
, &rval
, ei
);
3119 exprval_release(&exprval
);
3123 ret
->type
= EXPRVAL_VARIANT
;
3128 /* ECMA-262 3rd Edition 11.13.2 */
3129 HRESULT
assign_lshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3131 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3135 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, lshift_eval
, ei
, ret
);
3138 /* ECMA-262 3rd Edition 11.13.2 */
3139 HRESULT
assign_rshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3141 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3145 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, rshift_eval
, ei
, ret
);
3148 /* ECMA-262 3rd Edition 11.13.2 */
3149 HRESULT
assign_rrshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3151 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3155 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, rshift2_eval
, ei
, ret
);
3158 /* ECMA-262 3rd Edition 11.13.2 */
3159 HRESULT
assign_add_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3161 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3165 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, add_eval
, ei
, ret
);
3168 /* ECMA-262 3rd Edition 11.13.2 */
3169 HRESULT
assign_sub_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3171 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3175 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, sub_eval
, ei
, ret
);
3178 /* ECMA-262 3rd Edition 11.13.2 */
3179 HRESULT
assign_mul_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3181 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3185 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, mul_eval
, ei
, ret
);
3188 /* ECMA-262 3rd Edition 11.13.2 */
3189 HRESULT
assign_div_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3191 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3195 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, div_eval
, ei
, ret
);
3198 /* ECMA-262 3rd Edition 11.13.2 */
3199 HRESULT
assign_mod_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3201 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3205 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, mod_eval
, ei
, ret
);
3208 /* ECMA-262 3rd Edition 11.13.2 */
3209 HRESULT
assign_and_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3211 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3215 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, bitand_eval
, ei
, ret
);
3218 /* ECMA-262 3rd Edition 11.13.2 */
3219 HRESULT
assign_or_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3221 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3225 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, bitor_eval
, ei
, ret
);
3228 /* ECMA-262 3rd Edition 11.13.2 */
3229 HRESULT
assign_xor_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3231 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3235 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, xor_eval
, ei
, ret
);