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
))
501 /* ECMA-262 3rd Edition 10.1.4 */
502 static HRESULT
identifier_eval(exec_ctx_t
*ctx
, BSTR identifier
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
504 scope_chain_t
*scope
;
509 TRACE("%s\n", debugstr_w(identifier
));
511 for(scope
= ctx
->scope_chain
; scope
; scope
= scope
->next
) {
512 hres
= jsdisp_get_id(scope
->obj
, identifier
, 0, &id
);
518 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(scope
->obj
), id
);
522 hres
= jsdisp_get_id(ctx
->parser
->script
->global
, identifier
, 0, &id
);
523 if(SUCCEEDED(hres
)) {
524 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(ctx
->parser
->script
->global
), id
);
528 for(item
= ctx
->parser
->script
->named_items
; item
; item
= item
->next
) {
529 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
533 if(!ctx
->parser
->script
->site
)
536 hres
= IActiveScriptSite_GetItemInfo(ctx
->parser
->script
->site
, identifier
,
537 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
539 WARN("GetItemInfo failed: %08x\n", hres
);
543 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
544 IUnknown_Release(unk
);
546 WARN("object does not implement IDispatch\n");
551 ret
->type
= EXPRVAL_VARIANT
;
552 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
553 V_DISPATCH(&ret
->u
.var
) = item
->disp
;
554 IDispatch_AddRef(item
->disp
);
559 if(lookup_global_members(ctx
->parser
->script
, identifier
, ret
))
562 if(flags
& EXPR_NEWREF
) {
563 hres
= jsdisp_get_id(ctx
->parser
->script
->global
, identifier
, fdexNameEnsure
, &id
);
567 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(ctx
->parser
->script
->global
), id
);
571 ret
->type
= EXPRVAL_INVALID
;
572 ret
->u
.identifier
= SysAllocString(identifier
);
573 if(!ret
->u
.identifier
)
574 return E_OUTOFMEMORY
;
579 /* ECMA-262 3rd Edition 12.1 */
580 HRESULT
block_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
582 block_statement_t
*stat
= (block_statement_t
*)_stat
;
589 V_VT(&val
) = VT_EMPTY
;
590 for(iter
= stat
->stat_list
; iter
; iter
= iter
->next
) {
591 hres
= stat_eval(ctx
, iter
, rt
, &tmp
);
597 if(rt
->type
!= RT_NORMAL
)
610 /* ECMA-262 3rd Edition 12.2 */
611 static HRESULT
variable_list_eval(exec_ctx_t
*ctx
, variable_declaration_t
*var_list
, jsexcept_t
*ei
)
613 variable_declaration_t
*iter
;
616 for(iter
= var_list
; iter
; iter
= iter
->next
) {
623 hres
= expr_eval(ctx
, iter
->expr
, 0, ei
, &exprval
);
627 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
628 exprval_release(&exprval
);
632 hres
= jsdisp_propput_name(ctx
->var_disp
, iter
->identifier
, &val
, ei
, NULL
/*FIXME*/);
641 /* ECMA-262 3rd Edition 12.2 */
642 HRESULT
var_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
644 var_statement_t
*stat
= (var_statement_t
*)_stat
;
649 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
653 V_VT(ret
) = VT_EMPTY
;
657 /* ECMA-262 3rd Edition 12.3 */
658 HRESULT
empty_statement_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
662 V_VT(ret
) = VT_EMPTY
;
666 /* ECMA-262 3rd Edition 12.4 */
667 HRESULT
expression_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
669 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
676 hres
= expr_eval(ctx
, stat
->expr
, EXPR_NOVAL
, &rt
->ei
, &exprval
);
680 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
681 exprval_release(&exprval
);
686 TRACE("= %s\n", debugstr_variant(ret
));
690 /* ECMA-262 3rd Edition 12.5 */
691 HRESULT
if_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
693 if_statement_t
*stat
= (if_statement_t
*)_stat
;
700 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
704 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, &rt
->ei
, &b
);
705 exprval_release(&exprval
);
710 hres
= stat_eval(ctx
, stat
->if_stat
, rt
, ret
);
711 else if(stat
->else_stat
)
712 hres
= stat_eval(ctx
, stat
->else_stat
, rt
, ret
);
714 V_VT(ret
) = VT_EMPTY
;
719 /* ECMA-262 3rd Edition 12.6.2 */
720 HRESULT
while_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
722 while_statement_t
*stat
= (while_statement_t
*)_stat
;
731 V_VT(&val
) = VT_EMPTY
;
732 test_expr
= !stat
->do_while
;
736 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
740 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, &rt
->ei
, &b
);
741 exprval_release(&exprval
);
742 if(FAILED(hres
) || !b
)
748 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
755 if(rt
->type
== RT_CONTINUE
)
756 rt
->type
= RT_NORMAL
;
757 if(rt
->type
!= RT_NORMAL
)
766 if(rt
->type
== RT_BREAK
)
767 rt
->type
= RT_NORMAL
;
773 /* ECMA-262 3rd Edition 12.6.3 */
774 HRESULT
for_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
776 for_statement_t
*stat
= (for_statement_t
*)_stat
;
777 VARIANT val
, tmp
, retv
;
784 if(stat
->variable_list
) {
785 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
788 }else if(stat
->begin_expr
) {
789 hres
= expr_eval(ctx
, stat
->begin_expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
793 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
794 exprval_release(&exprval
);
801 V_VT(&retv
) = VT_EMPTY
;
805 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
809 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, &rt
->ei
, &b
);
810 exprval_release(&exprval
);
811 if(FAILED(hres
) || !b
)
815 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
822 if(rt
->type
== RT_CONTINUE
)
823 rt
->type
= RT_NORMAL
;
824 else if(rt
->type
!= RT_NORMAL
)
828 hres
= expr_eval(ctx
, stat
->end_expr
, 0, &rt
->ei
, &exprval
);
832 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
833 exprval_release(&exprval
);
846 if(rt
->type
== RT_BREAK
)
847 rt
->type
= RT_NORMAL
;
853 /* ECMA-262 3rd Edition 12.6.4 */
854 HRESULT
forin_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
856 forin_statement_t
*stat
= (forin_statement_t
*)_stat
;
857 VARIANT val
, name
, retv
, tmp
;
858 DISPID id
= DISPID_STARTENUM
;
859 BSTR str
, identifier
= NULL
;
867 hres
= variable_list_eval(ctx
, stat
->variable
, &rt
->ei
);
872 hres
= expr_eval(ctx
, stat
->in_expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
876 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
877 exprval_release(&exprval
);
881 if(V_VT(&val
) != VT_DISPATCH
) {
882 TRACE("in vt %d\n", V_VT(&val
));
884 V_VT(ret
) = VT_EMPTY
;
888 hres
= IDispatch_QueryInterface(V_DISPATCH(&val
), &IID_IDispatchEx
, (void**)&in_obj
);
889 IDispatch_Release(V_DISPATCH(&val
));
891 FIXME("Object doesn't support IDispatchEx\n");
895 V_VT(&retv
) = VT_EMPTY
;
898 identifier
= SysAllocString(stat
->variable
->identifier
);
901 hres
= IDispatchEx_GetNextDispID(in_obj
, fdexEnumDefault
, id
, &id
);
902 if(FAILED(hres
) || hres
== S_FALSE
)
905 hres
= IDispatchEx_GetMemberName(in_obj
, id
, &str
);
909 TRACE("iter %s\n", debugstr_w(str
));
912 hres
= identifier_eval(ctx
, identifier
, 0, NULL
, &exprval
);
914 hres
= expr_eval(ctx
, stat
->expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
915 if(SUCCEEDED(hres
)) {
916 V_VT(&name
) = VT_BSTR
;
918 hres
= put_value(ctx
->parser
->script
, &exprval
, &name
, &rt
->ei
);
919 exprval_release(&exprval
);
925 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
932 if(rt
->type
== RT_CONTINUE
)
933 rt
->type
= RT_NORMAL
;
934 else if(rt
->type
!= RT_NORMAL
)
938 SysFreeString(identifier
);
939 IDispatchEx_Release(in_obj
);
945 if(rt
->type
== RT_BREAK
)
946 rt
->type
= RT_NORMAL
;
952 /* ECMA-262 3rd Edition 12.7 */
953 HRESULT
continue_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
955 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
959 if(stat
->identifier
) {
960 FIXME("indentifier not implemented\n");
964 rt
->type
= RT_CONTINUE
;
965 V_VT(ret
) = VT_EMPTY
;
969 /* ECMA-262 3rd Edition 12.8 */
970 HRESULT
break_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
972 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
976 if(stat
->identifier
) {
977 FIXME("indentifier not implemented\n");
982 V_VT(ret
) = VT_EMPTY
;
986 /* ECMA-262 3rd Edition 12.9 */
987 HRESULT
return_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
989 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
997 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1001 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, ret
);
1002 exprval_release(&exprval
);
1006 V_VT(ret
) = VT_EMPTY
;
1009 TRACE("= %s\n", debugstr_variant(ret
));
1010 rt
->type
= RT_RETURN
;
1014 /* ECMA-262 3rd Edition 12.10 */
1015 HRESULT
with_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1017 with_statement_t
*stat
= (with_statement_t
*)_stat
;
1026 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1030 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
1031 exprval_release(&exprval
);
1035 hres
= to_object(ctx
->parser
->script
, &val
, &disp
);
1040 obj
= iface_to_jsdisp((IUnknown
*)disp
);
1041 IDispatch_Release(disp
);
1043 FIXME("disp id not jsdisp\n");
1047 hres
= scope_push(ctx
->scope_chain
, obj
, &ctx
->scope_chain
);
1048 jsdisp_release(obj
);
1052 hres
= stat_eval(ctx
, stat
->statement
, rt
, ret
);
1054 scope_pop(&ctx
->scope_chain
);
1058 /* ECMA-262 3rd Edition 12.12 */
1059 HRESULT
labelled_statement_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
1065 /* ECMA-262 3rd Edition 12.13 */
1066 HRESULT
switch_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1068 switch_statement_t
*stat
= (switch_statement_t
*)_stat
;
1069 case_clausule_t
*iter
, *default_clausule
= NULL
;
1070 statement_t
*stat_iter
;
1078 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1082 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
1083 exprval_release(&exprval
);
1087 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1089 default_clausule
= iter
;
1093 hres
= expr_eval(ctx
, iter
->expr
, 0, &rt
->ei
, &exprval
);
1097 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &cval
);
1098 exprval_release(&exprval
);
1102 hres
= equal2_values(&val
, &cval
, &b
);
1103 VariantClear(&cval
);
1104 if(FAILED(hres
) || b
)
1113 iter
= default_clausule
;
1115 V_VT(&val
) = VT_EMPTY
;
1119 for(stat_iter
= iter
->stat
; stat_iter
; stat_iter
= stat_iter
->next
) {
1120 hres
= stat_eval(ctx
, stat_iter
, rt
, &tmp
);
1127 if(rt
->type
!= RT_NORMAL
)
1137 if(rt
->type
== RT_BREAK
)
1138 rt
->type
= RT_NORMAL
;
1144 /* ECMA-262 3rd Edition 12.13 */
1145 HRESULT
throw_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1147 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
1154 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1158 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
1159 exprval_release(&exprval
);
1164 return DISP_E_EXCEPTION
;
1167 /* ECMA-262 3rd Edition 12.14 */
1168 static HRESULT
catch_eval(exec_ctx_t
*ctx
, catch_block_t
*block
, return_type_t
*rt
, VARIANT
*ret
)
1170 DispatchEx
*var_disp
;
1175 memset(&rt
->ei
, 0, sizeof(jsexcept_t
));
1177 hres
= create_dispex(ctx
->parser
->script
, NULL
, NULL
, &var_disp
);
1178 if(SUCCEEDED(hres
)) {
1179 hres
= jsdisp_propput_name(var_disp
, block
->identifier
, &ex
, &rt
->ei
, NULL
/*FIXME*/);
1180 if(SUCCEEDED(hres
)) {
1181 hres
= scope_push(ctx
->scope_chain
, var_disp
, &ctx
->scope_chain
);
1182 if(SUCCEEDED(hres
)) {
1183 hres
= stat_eval(ctx
, block
->statement
, rt
, &val
);
1184 scope_pop(&ctx
->scope_chain
);
1188 jsdisp_release(var_disp
);
1199 /* ECMA-262 3rd Edition 12.14 */
1200 HRESULT
try_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1202 try_statement_t
*stat
= (try_statement_t
*)_stat
;
1208 hres
= stat_eval(ctx
, stat
->try_statement
, rt
, &val
);
1210 TRACE("EXCEPTION\n");
1211 if(!stat
->catch_block
)
1214 hres
= catch_eval(ctx
, stat
->catch_block
, rt
, &val
);
1219 if(stat
->finally_statement
) {
1221 hres
= stat_eval(ctx
, stat
->finally_statement
, rt
, &val
);
1230 static HRESULT
return_bool(exprval_t
*ret
, DWORD b
)
1232 ret
->type
= EXPRVAL_VARIANT
;
1233 V_VT(&ret
->u
.var
) = VT_BOOL
;
1234 V_BOOL(&ret
->u
.var
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
1239 static HRESULT
get_binary_expr_values(exec_ctx_t
*ctx
, binary_expression_t
*expr
, jsexcept_t
*ei
, VARIANT
*lval
, VARIANT
*rval
)
1244 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1248 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, lval
);
1249 exprval_release(&exprval
);
1253 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1254 if(SUCCEEDED(hres
)) {
1255 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, rval
);
1256 exprval_release(&exprval
);
1267 typedef HRESULT (*oper_t
)(exec_ctx_t
*,VARIANT
*,VARIANT
*,jsexcept_t
*,VARIANT
*);
1269 static HRESULT
binary_expr_eval(exec_ctx_t
*ctx
, binary_expression_t
*expr
, oper_t oper
, jsexcept_t
*ei
,
1272 VARIANT lval
, rval
, retv
;
1275 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
1279 hres
= oper(ctx
, &lval
, &rval
, ei
, &retv
);
1280 VariantClear(&lval
);
1281 VariantClear(&rval
);
1285 ret
->type
= EXPRVAL_VARIANT
;
1290 /* ECMA-262 3rd Edition 11.13.2 */
1291 static HRESULT
assign_oper_eval(exec_ctx_t
*ctx
, expression_t
*lexpr
, expression_t
*rexpr
, oper_t oper
,
1292 jsexcept_t
*ei
, exprval_t
*ret
)
1294 VARIANT retv
, lval
, rval
;
1295 exprval_t exprval
, exprvalr
;
1298 hres
= expr_eval(ctx
, lexpr
, EXPR_NEWREF
, ei
, &exprval
);
1302 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &lval
);
1303 if(SUCCEEDED(hres
)) {
1304 hres
= expr_eval(ctx
, rexpr
, 0, ei
, &exprvalr
);
1305 if(SUCCEEDED(hres
)) {
1306 hres
= exprval_value(ctx
->parser
->script
, &exprvalr
, ei
, &rval
);
1307 exprval_release(&exprvalr
);
1309 if(SUCCEEDED(hres
)) {
1310 hres
= oper(ctx
, &lval
, &rval
, ei
, &retv
);
1311 VariantClear(&rval
);
1313 VariantClear(&lval
);
1316 if(SUCCEEDED(hres
)) {
1317 hres
= put_value(ctx
->parser
->script
, &exprval
, &retv
, ei
);
1319 VariantClear(&retv
);
1321 exprval_release(&exprval
);
1326 ret
->type
= EXPRVAL_VARIANT
;
1331 /* ECMA-262 3rd Edition 13 */
1332 HRESULT
function_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1334 function_expression_t
*expr
= (function_expression_t
*)_expr
;
1340 if(expr
->identifier
) {
1341 hres
= jsdisp_propget_name(ctx
->var_disp
, expr
->identifier
, &var
, ei
, NULL
/*FIXME*/);
1347 hres
= create_source_function(ctx
->parser
, expr
->parameter_list
, expr
->source_elements
, ctx
->scope_chain
,
1348 expr
->src_str
, expr
->src_len
, &dispex
);
1352 V_VT(&var
) = VT_DISPATCH
;
1353 V_DISPATCH(&var
) = (IDispatch
*)_IDispatchEx_(dispex
);
1356 ret
->type
= EXPRVAL_VARIANT
;
1361 /* ECMA-262 3rd Edition 11.12 */
1362 HRESULT
conditional_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1364 conditional_expression_t
*expr
= (conditional_expression_t
*)_expr
;
1371 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1375 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, ei
, &b
);
1376 exprval_release(&exprval
);
1380 return expr_eval(ctx
, b
? expr
->true_expression
: expr
->false_expression
, flags
, ei
, ret
);
1383 /* ECMA-262 3rd Edition 11.2.1 */
1384 HRESULT
array_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1386 array_expression_t
*expr
= (array_expression_t
*)_expr
;
1388 VARIANT member
, val
;
1391 IDispatch
*obj
= NULL
;
1396 hres
= expr_eval(ctx
, expr
->member_expr
, 0, ei
, &exprval
);
1400 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &member
);
1401 exprval_release(&exprval
);
1405 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
1406 if(SUCCEEDED(hres
)) {
1407 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1408 exprval_release(&exprval
);
1412 hres
= to_object(ctx
->parser
->script
, &member
, &obj
);
1413 VariantClear(&member
);
1414 if(SUCCEEDED(hres
)) {
1415 hres
= to_string(ctx
->parser
->script
, &val
, ei
, &str
);
1416 if(SUCCEEDED(hres
)) {
1417 if(flags
& EXPR_STRREF
) {
1418 ret
->type
= EXPRVAL_NAMEREF
;
1419 ret
->u
.nameref
.disp
= obj
;
1420 ret
->u
.nameref
.name
= str
;
1424 hres
= disp_get_id(ctx
->parser
->script
, obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1427 if(SUCCEEDED(hres
)) {
1428 exprval_set_idref(ret
, obj
, id
);
1429 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1434 IDispatch_Release(obj
);
1440 /* ECMA-262 3rd Edition 11.2.1 */
1441 HRESULT
member_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1443 member_expression_t
*expr
= (member_expression_t
*)_expr
;
1444 IDispatch
*obj
= NULL
;
1453 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1457 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &member
);
1458 exprval_release(&exprval
);
1462 hres
= to_object(ctx
->parser
->script
, &member
, &obj
);
1463 VariantClear(&member
);
1467 str
= SysAllocString(expr
->identifier
);
1468 if(flags
& EXPR_STRREF
) {
1469 ret
->type
= EXPRVAL_NAMEREF
;
1470 ret
->u
.nameref
.disp
= obj
;
1471 ret
->u
.nameref
.name
= str
;
1475 hres
= disp_get_id(ctx
->parser
->script
, obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1477 if(SUCCEEDED(hres
)) {
1478 exprval_set_idref(ret
, obj
, id
);
1479 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1484 IDispatch_Release(obj
);
1488 static void free_dp(DISPPARAMS
*dp
)
1492 for(i
=0; i
< dp
->cArgs
; i
++)
1493 VariantClear(dp
->rgvarg
+i
);
1494 heap_free(dp
->rgvarg
);
1497 static HRESULT
args_to_param(exec_ctx_t
*ctx
, argument_t
*args
, jsexcept_t
*ei
, DISPPARAMS
*dp
)
1503 HRESULT hres
= S_OK
;
1505 memset(dp
, 0, sizeof(*dp
));
1509 for(iter
= args
; iter
; iter
= iter
->next
)
1512 vargs
= heap_alloc_zero(cnt
* sizeof(*vargs
));
1514 return E_OUTOFMEMORY
;
1516 for(i
= cnt
, iter
= args
; iter
; iter
= iter
->next
) {
1517 hres
= expr_eval(ctx
, iter
->expr
, 0, ei
, &exprval
);
1521 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, vargs
+ (--i
));
1522 exprval_release(&exprval
);
1537 /* ECMA-262 3rd Edition 11.2.2 */
1538 HRESULT
new_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1540 call_expression_t
*expr
= (call_expression_t
*)_expr
;
1542 VARIANT constr
, var
;
1548 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1552 hres
= args_to_param(ctx
, expr
->argument_list
, ei
, &dp
);
1554 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &constr
);
1555 exprval_release(&exprval
);
1559 if(V_VT(&constr
) != VT_DISPATCH
) {
1560 FIXME("throw TypeError\n");
1561 VariantClear(&constr
);
1565 hres
= disp_call(ctx
->parser
->script
, V_DISPATCH(&constr
), DISPID_VALUE
,
1566 DISPATCH_CONSTRUCT
, &dp
, &var
, ei
, NULL
/*FIXME*/);
1567 IDispatch_Release(V_DISPATCH(&constr
));
1571 ret
->type
= EXPRVAL_VARIANT
;
1576 /* ECMA-262 3rd Edition 11.2.3 */
1577 HRESULT
call_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1579 call_expression_t
*expr
= (call_expression_t
*)_expr
;
1587 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1591 hres
= args_to_param(ctx
, expr
->argument_list
, ei
, &dp
);
1592 if(SUCCEEDED(hres
)) {
1593 switch(exprval
.type
) {
1594 case EXPRVAL_VARIANT
:
1595 if(V_VT(&exprval
.u
.var
) == VT_DISPATCH
)
1596 hres
= disp_call(ctx
->parser
->script
, V_DISPATCH(&exprval
.u
.var
), DISPID_VALUE
,
1597 DISPATCH_METHOD
, &dp
, flags
& EXPR_NOVAL
? NULL
: &var
, ei
, NULL
/*FIXME*/);
1599 hres
= throw_type_error(ctx
->parser
->script
, ei
, IDS_NO_PROPERTY
, NULL
);
1602 hres
= disp_call(ctx
->parser
->script
, exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
,
1603 DISPATCH_METHOD
, &dp
, flags
& EXPR_NOVAL
? NULL
: &var
, ei
, NULL
/*FIXME*/);
1605 case EXPRVAL_INVALID
:
1606 hres
= throw_type_error(ctx
->parser
->script
, ei
, IDS_OBJECT_EXPECTED
, NULL
);
1609 FIXME("unimplemented type %d\n", exprval
.type
);
1616 exprval_release(&exprval
);
1620 ret
->type
= EXPRVAL_VARIANT
;
1621 if(flags
& EXPR_NOVAL
) {
1622 V_VT(&ret
->u
.var
) = VT_EMPTY
;
1624 TRACE("= %s\n", debugstr_variant(&var
));
1630 /* ECMA-262 3rd Edition 11.1.1 */
1631 HRESULT
this_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1635 ret
->type
= EXPRVAL_VARIANT
;
1636 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
1637 V_DISPATCH(&ret
->u
.var
) = ctx
->this_obj
;
1638 IDispatch_AddRef(ctx
->this_obj
);
1642 /* ECMA-262 3rd Edition 10.1.4 */
1643 HRESULT
identifier_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1645 identifier_expression_t
*expr
= (identifier_expression_t
*)_expr
;
1651 identifier
= SysAllocString(expr
->identifier
);
1653 return E_OUTOFMEMORY
;
1655 hres
= identifier_eval(ctx
, identifier
, flags
, ei
, ret
);
1657 SysFreeString(identifier
);
1661 /* ECMA-262 3rd Edition 7.8 */
1662 HRESULT
literal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1664 literal_expression_t
*expr
= (literal_expression_t
*)_expr
;
1670 hres
= literal_to_var(ctx
->parser
->script
, expr
->literal
, &var
);
1674 ret
->type
= EXPRVAL_VARIANT
;
1679 /* ECMA-262 3rd Edition 11.1.4 */
1680 HRESULT
array_literal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1682 array_literal_expression_t
*expr
= (array_literal_expression_t
*)_expr
;
1683 DWORD length
= 0, i
= 0;
1684 array_element_t
*elem
;
1692 for(elem
= expr
->element_list
; elem
; elem
= elem
->next
)
1693 length
+= elem
->elision
+1;
1694 length
+= expr
->length
;
1696 hres
= create_array(ctx
->parser
->script
, length
, &array
);
1700 for(elem
= expr
->element_list
; elem
; elem
= elem
->next
) {
1703 hres
= expr_eval(ctx
, elem
->expr
, 0, ei
, &exprval
);
1707 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1708 exprval_release(&exprval
);
1712 hres
= jsdisp_propput_idx(array
, i
, &val
, ei
, NULL
/*FIXME*/);
1721 jsdisp_release(array
);
1725 ret
->type
= EXPRVAL_VARIANT
;
1726 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
1727 V_DISPATCH(&ret
->u
.var
) = (IDispatch
*)_IDispatchEx_(array
);
1731 /* ECMA-262 3rd Edition 11.1.5 */
1732 HRESULT
property_value_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1734 property_value_expression_t
*expr
= (property_value_expression_t
*)_expr
;
1744 hres
= create_object(ctx
->parser
->script
, NULL
, &obj
);
1748 for(iter
= expr
->property_list
; iter
; iter
= iter
->next
) {
1749 hres
= literal_to_var(ctx
->parser
->script
, iter
->name
, &tmp
);
1753 hres
= to_string(ctx
->parser
->script
, &tmp
, ei
, &name
);
1758 hres
= expr_eval(ctx
, iter
->value
, 0, ei
, &exprval
);
1759 if(SUCCEEDED(hres
)) {
1760 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1761 exprval_release(&exprval
);
1762 if(SUCCEEDED(hres
)) {
1763 hres
= jsdisp_propput_name(obj
, name
, &val
, ei
, NULL
/*FIXME*/);
1768 SysFreeString(name
);
1774 jsdisp_release(obj
);
1778 ret
->type
= EXPRVAL_VARIANT
;
1779 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
1780 V_DISPATCH(&ret
->u
.var
) = (IDispatch
*)_IDispatchEx_(obj
);
1784 /* ECMA-262 3rd Edition 11.14 */
1785 HRESULT
comma_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1787 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1793 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
1797 VariantClear(&lval
);
1799 ret
->type
= EXPRVAL_VARIANT
;
1804 /* ECMA-262 3rd Edition 11.11 */
1805 HRESULT
logical_or_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1807 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1815 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1819 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1820 exprval_release(&exprval
);
1824 hres
= to_boolean(&val
, &b
);
1825 if(SUCCEEDED(hres
) && b
) {
1826 ret
->type
= EXPRVAL_VARIANT
;
1835 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1839 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1840 exprval_release(&exprval
);
1844 ret
->type
= EXPRVAL_VARIANT
;
1849 /* ECMA-262 3rd Edition 11.11 */
1850 HRESULT
logical_and_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1852 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1860 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1864 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1865 exprval_release(&exprval
);
1869 hres
= to_boolean(&val
, &b
);
1870 if(SUCCEEDED(hres
) && !b
) {
1871 ret
->type
= EXPRVAL_VARIANT
;
1880 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1884 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1885 exprval_release(&exprval
);
1889 ret
->type
= EXPRVAL_VARIANT
;
1894 /* ECMA-262 3rd Edition 11.10 */
1895 static HRESULT
bitor_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1900 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
1904 hres
= to_int32(ctx
->parser
->script
, rval
, ei
, &ri
);
1913 /* ECMA-262 3rd Edition 11.10 */
1914 HRESULT
binary_or_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1916 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1920 return binary_expr_eval(ctx
, expr
, bitor_eval
, ei
, ret
);
1923 /* ECMA-262 3rd Edition 11.10 */
1924 static HRESULT
xor_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1929 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
1933 hres
= to_int32(ctx
->parser
->script
, rval
, ei
, &ri
);
1942 /* ECMA-262 3rd Edition 11.10 */
1943 HRESULT
binary_xor_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1945 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1949 return binary_expr_eval(ctx
, expr
, xor_eval
, ei
, ret
);
1952 /* ECMA-262 3rd Edition 11.10 */
1953 static HRESULT
bitand_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1958 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
1962 hres
= to_int32(ctx
->parser
->script
, rval
, ei
, &ri
);
1971 /* ECMA-262 3rd Edition 11.10 */
1972 HRESULT
binary_and_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1974 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1978 return binary_expr_eval(ctx
, expr
, bitand_eval
, ei
, ret
);
1981 /* ECMA-262 3rd Edition 11.8.6 */
1982 static HRESULT
instanceof_eval(exec_ctx_t
*ctx
, VARIANT
*inst
, VARIANT
*objv
, jsexcept_t
*ei
, VARIANT
*retv
)
1984 DispatchEx
*obj
, *iter
, *tmp
= NULL
;
1985 VARIANT_BOOL ret
= VARIANT_FALSE
;
1990 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1992 if(V_VT(objv
) != VT_DISPATCH
) {
1993 FIXME("throw TypeError\n");
1997 obj
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(objv
));
1999 FIXME("throw TypeError\n");
2003 if(is_class(obj
, JSCLASS_FUNCTION
)) {
2004 hres
= jsdisp_propget_name(obj
, prototypeW
, &var
, ei
, NULL
/*FIXME*/);
2006 FIXME("throw TypeError\n");
2009 jsdisp_release(obj
);
2013 if(V_VT(&var
) == VT_DISPATCH
) {
2014 if(V_VT(inst
) == VT_DISPATCH
)
2015 tmp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(inst
));
2016 for(iter
= tmp
; iter
; iter
= iter
->prototype
) {
2017 hres
= disp_cmp(V_DISPATCH(&var
), (IDispatch
*)_IDispatchEx_(iter
), &b
);
2027 jsdisp_release(tmp
);
2029 FIXME("prototype is not an object\n");
2037 V_VT(retv
) = VT_BOOL
;
2042 /* ECMA-262 3rd Edition 11.8.6 */
2043 HRESULT
instanceof_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2045 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2049 return binary_expr_eval(ctx
, expr
, instanceof_eval
, ei
, ret
);
2052 /* ECMA-262 3rd Edition 11.8.7 */
2053 static HRESULT
in_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*obj
, jsexcept_t
*ei
, VARIANT
*retv
)
2060 if(V_VT(obj
) != VT_DISPATCH
) {
2061 FIXME("throw TypeError\n");
2065 hres
= to_string(ctx
->parser
->script
, lval
, ei
, &str
);
2069 hres
= disp_get_id(ctx
->parser
->script
, V_DISPATCH(obj
), str
, 0, &id
);
2073 else if(hres
== DISP_E_UNKNOWNNAME
)
2074 ret
= VARIANT_FALSE
;
2078 V_VT(retv
) = VT_BOOL
;
2083 /* ECMA-262 3rd Edition 11.8.7 */
2084 HRESULT
in_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2086 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2090 return binary_expr_eval(ctx
, expr
, in_eval
, ei
, ret
);
2093 /* ECMA-262 3rd Edition 11.6.1 */
2094 static HRESULT
add_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2099 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &l
, NO_HINT
);
2103 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &r
, NO_HINT
);
2109 if(V_VT(&l
) == VT_BSTR
|| V_VT(&r
) == VT_BSTR
) {
2110 BSTR lstr
= NULL
, rstr
= NULL
;
2112 if(V_VT(&l
) == VT_BSTR
)
2115 hres
= to_string(ctx
->parser
->script
, &l
, ei
, &lstr
);
2117 if(SUCCEEDED(hres
)) {
2118 if(V_VT(&r
) == VT_BSTR
)
2121 hres
= to_string(ctx
->parser
->script
, &r
, ei
, &rstr
);
2124 if(SUCCEEDED(hres
)) {
2127 len1
= SysStringLen(lstr
);
2128 len2
= SysStringLen(rstr
);
2130 V_VT(retv
) = VT_BSTR
;
2131 V_BSTR(retv
) = SysAllocStringLen(NULL
, len1
+len2
);
2132 memcpy(V_BSTR(retv
), lstr
, len1
*sizeof(WCHAR
));
2133 memcpy(V_BSTR(retv
)+len1
, rstr
, (len2
+1)*sizeof(WCHAR
));
2136 if(V_VT(&l
) != VT_BSTR
)
2137 SysFreeString(lstr
);
2138 if(V_VT(&r
) != VT_BSTR
)
2139 SysFreeString(rstr
);
2143 hres
= to_number(ctx
->parser
->script
, &l
, ei
, &nl
);
2144 if(SUCCEEDED(hres
)) {
2145 hres
= to_number(ctx
->parser
->script
, &r
, ei
, &nr
);
2147 num_set_val(retv
, num_val(&nl
) + num_val(&nr
));
2156 /* ECMA-262 3rd Edition 11.6.1 */
2157 HRESULT
add_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2159 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2163 return binary_expr_eval(ctx
, expr
, add_eval
, ei
, ret
);
2166 /* ECMA-262 3rd Edition 11.6.2 */
2167 static HRESULT
sub_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2172 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2176 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2180 num_set_val(retv
, num_val(&lnum
) - num_val(&rnum
));
2184 /* ECMA-262 3rd Edition 11.6.2 */
2185 HRESULT
sub_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2187 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2191 return binary_expr_eval(ctx
, expr
, sub_eval
, ei
, ret
);
2194 /* ECMA-262 3rd Edition 11.5.1 */
2195 static HRESULT
mul_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2200 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2204 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2208 num_set_val(retv
, num_val(&lnum
) * num_val(&rnum
));
2212 /* ECMA-262 3rd Edition 11.5.1 */
2213 HRESULT
mul_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2215 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2219 return binary_expr_eval(ctx
, expr
, mul_eval
, ei
, ret
);
2222 /* ECMA-262 3rd Edition 11.5.2 */
2223 static HRESULT
div_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2228 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2232 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2236 num_set_val(retv
, num_val(&lnum
) / num_val(&rnum
));
2240 /* ECMA-262 3rd Edition 11.5.2 */
2241 HRESULT
div_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2243 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2247 return binary_expr_eval(ctx
, expr
, div_eval
, ei
, ret
);
2250 /* ECMA-262 3rd Edition 11.5.3 */
2251 static HRESULT
mod_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2256 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2260 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2264 num_set_val(retv
, fmod(num_val(&lnum
), num_val(&rnum
)));
2268 /* ECMA-262 3rd Edition 11.5.3 */
2269 HRESULT
mod_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2271 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2275 return binary_expr_eval(ctx
, expr
, mod_eval
, ei
, ret
);
2278 /* ECMA-262 3rd Edition 11.4.2 */
2279 HRESULT
delete_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2281 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2282 VARIANT_BOOL b
= VARIANT_FALSE
;
2288 hres
= expr_eval(ctx
, expr
->expression
, EXPR_STRREF
, ei
, &exprval
);
2292 switch(exprval
.type
) {
2293 case EXPRVAL_IDREF
: {
2294 IDispatchEx
*dispex
;
2296 hres
= IDispatch_QueryInterface(exprval
.u
.nameref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2297 if(SUCCEEDED(hres
)) {
2298 hres
= IDispatchEx_DeleteMemberByDispID(dispex
, exprval
.u
.idref
.id
);
2300 IDispatchEx_Release(dispex
);
2304 case EXPRVAL_NAMEREF
: {
2305 IDispatchEx
*dispex
;
2307 hres
= IDispatch_QueryInterface(exprval
.u
.nameref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2308 if(SUCCEEDED(hres
)) {
2309 hres
= IDispatchEx_DeleteMemberByName(dispex
, exprval
.u
.nameref
.name
,
2310 make_grfdex(ctx
->parser
->script
, fdexNameCaseSensitive
));
2312 IDispatchEx_Release(dispex
);
2317 FIXME("unsupported type %d\n", exprval
.type
);
2321 exprval_release(&exprval
);
2325 return return_bool(ret
, b
);
2328 /* ECMA-262 3rd Edition 11.4.2 */
2329 HRESULT
void_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2331 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2338 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2342 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &tmp
);
2343 exprval_release(&exprval
);
2349 ret
->type
= EXPRVAL_VARIANT
;
2350 V_VT(&ret
->u
.var
) = VT_EMPTY
;
2354 /* ECMA-262 3rd Edition 11.4.3 */
2355 static HRESULT
typeof_exprval(exec_ctx_t
*ctx
, exprval_t
*exprval
, jsexcept_t
*ei
, const WCHAR
**ret
)
2360 static const WCHAR booleanW
[] = {'b','o','o','l','e','a','n',0};
2361 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
2362 static const WCHAR numberW
[] = {'n','u','m','b','e','r',0};
2363 static const WCHAR objectW
[] = {'o','b','j','e','c','t',0};
2364 static const WCHAR stringW
[] = {'s','t','r','i','n','g',0};
2365 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
2367 if(exprval
->type
== EXPRVAL_INVALID
) {
2372 hres
= exprval_to_value(ctx
->parser
->script
, exprval
, ei
, &val
);
2376 switch(V_VT(&val
)) {
2396 if(V_DISPATCH(&val
) && (dispex
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(&val
)))) {
2397 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
2398 jsdisp_release(dispex
);
2405 FIXME("unhandled vt %d\n", V_VT(&val
));
2413 HRESULT
typeof_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2415 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2416 const WCHAR
*str
= NULL
;
2422 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2426 hres
= typeof_exprval(ctx
, &exprval
, ei
, &str
);
2427 exprval_release(&exprval
);
2431 ret
->type
= EXPRVAL_VARIANT
;
2432 V_VT(&ret
->u
.var
) = VT_BSTR
;
2433 V_BSTR(&ret
->u
.var
) = SysAllocString(str
);
2434 if(!V_BSTR(&ret
->u
.var
))
2435 return E_OUTOFMEMORY
;
2440 /* ECMA-262 3rd Edition 11.4.7 */
2441 HRESULT
minus_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2443 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2450 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2454 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2455 exprval_release(&exprval
);
2459 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2464 ret
->type
= EXPRVAL_VARIANT
;
2465 num_set_val(&ret
->u
.var
, -num_val(&num
));
2469 /* ECMA-262 3rd Edition 11.4.6 */
2470 HRESULT
plus_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2472 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2479 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2483 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2484 exprval_release(&exprval
);
2488 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2492 ret
->type
= EXPRVAL_VARIANT
;
2497 /* ECMA-262 3rd Edition 11.3.1 */
2498 HRESULT
post_increment_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2500 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2507 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2511 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2512 if(SUCCEEDED(hres
)) {
2513 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2517 if(SUCCEEDED(hres
)) {
2519 num_set_val(&inc
, num_val(&num
)+1.0);
2520 hres
= put_value(ctx
->parser
->script
, &exprval
, &inc
, ei
);
2523 exprval_release(&exprval
);
2527 ret
->type
= EXPRVAL_VARIANT
;
2532 /* ECMA-262 3rd Edition 11.3.2 */
2533 HRESULT
post_decrement_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2535 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2542 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2546 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2547 if(SUCCEEDED(hres
)) {
2548 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2552 if(SUCCEEDED(hres
)) {
2554 num_set_val(&dec
, num_val(&num
)-1.0);
2555 hres
= put_value(ctx
->parser
->script
, &exprval
, &dec
, ei
);
2558 exprval_release(&exprval
);
2562 ret
->type
= EXPRVAL_VARIANT
;
2567 /* ECMA-262 3rd Edition 11.4.4 */
2568 HRESULT
pre_increment_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2570 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2577 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2581 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2582 if(SUCCEEDED(hres
)) {
2583 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2587 if(SUCCEEDED(hres
)) {
2588 num_set_val(&val
, num_val(&num
)+1.0);
2589 hres
= put_value(ctx
->parser
->script
, &exprval
, &val
, ei
);
2592 exprval_release(&exprval
);
2596 ret
->type
= EXPRVAL_VARIANT
;
2601 /* ECMA-262 3rd Edition 11.4.5 */
2602 HRESULT
pre_decrement_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2604 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2611 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2615 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2616 if(SUCCEEDED(hres
)) {
2617 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2621 if(SUCCEEDED(hres
)) {
2622 num_set_val(&val
, num_val(&num
)-1.0);
2623 hres
= put_value(ctx
->parser
->script
, &exprval
, &val
, ei
);
2626 exprval_release(&exprval
);
2630 ret
->type
= EXPRVAL_VARIANT
;
2635 /* ECMA-262 3rd Edition 11.9.3 */
2636 static HRESULT
equal_values(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, BOOL
*ret
)
2638 if(V_VT(lval
) == V_VT(rval
) || (is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
))))
2639 return equal2_values(lval
, rval
, ret
);
2641 /* FIXME: NULL disps should be handled in more general way */
2642 if(V_VT(lval
) == VT_DISPATCH
&& !V_DISPATCH(lval
)) {
2645 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2648 if(V_VT(rval
) == VT_DISPATCH
&& !V_DISPATCH(rval
)) {
2651 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2654 if((V_VT(lval
) == VT_NULL
&& V_VT(rval
) == VT_EMPTY
) ||
2655 (V_VT(lval
) == VT_EMPTY
&& V_VT(rval
) == VT_NULL
)) {
2660 if(V_VT(lval
) == VT_BSTR
&& is_num_vt(V_VT(rval
))) {
2664 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &v
);
2668 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2671 if(V_VT(rval
) == VT_BSTR
&& is_num_vt(V_VT(lval
))) {
2675 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &v
);
2679 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2682 if(V_VT(rval
) == VT_BOOL
) {
2686 V_I4(&v
) = V_BOOL(rval
) ? 1 : 0;
2687 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2690 if(V_VT(lval
) == VT_BOOL
) {
2694 V_I4(&v
) = V_BOOL(lval
) ? 1 : 0;
2695 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2699 if(V_VT(rval
) == VT_DISPATCH
&& (V_VT(lval
) == VT_BSTR
|| is_num_vt(V_VT(lval
)))) {
2703 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &v
, NO_HINT
);
2707 hres
= equal_values(ctx
, lval
, &v
, ei
, ret
);
2714 if(V_VT(lval
) == VT_DISPATCH
&& (V_VT(rval
) == VT_BSTR
|| is_num_vt(V_VT(rval
)))) {
2718 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &v
, NO_HINT
);
2722 hres
= equal_values(ctx
, &v
, rval
, ei
, ret
);
2733 /* ECMA-262 3rd Edition 11.9.1 */
2734 HRESULT
equal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2736 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2743 hres
= get_binary_expr_values(ctx
, expr
, ei
, &rval
, &lval
);
2747 hres
= equal_values(ctx
, &rval
, &lval
, ei
, &b
);
2751 return return_bool(ret
, b
);
2754 /* ECMA-262 3rd Edition 11.9.4 */
2755 HRESULT
equal2_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2757 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2764 hres
= get_binary_expr_values(ctx
, expr
, ei
, &rval
, &lval
);
2768 hres
= equal2_values(&rval
, &lval
, &b
);
2772 return return_bool(ret
, b
);
2775 /* ECMA-262 3rd Edition 11.9.2 */
2776 HRESULT
not_equal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2778 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2785 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2789 hres
= equal_values(ctx
, &lval
, &rval
, ei
, &b
);
2793 return return_bool(ret
, !b
);
2796 /* ECMA-262 3rd Edition 11.9.5 */
2797 HRESULT
not_equal2_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2799 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2806 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2810 hres
= equal2_values(&lval
, &rval
, &b
);
2814 return return_bool(ret
, !b
);
2817 /* ECMA-262 3rd Edition 11.8.5 */
2818 static HRESULT
less_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, BOOL greater
, jsexcept_t
*ei
, BOOL
*ret
)
2820 VARIANT l
, r
, ln
, rn
;
2823 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &l
, NO_HINT
);
2827 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &r
, NO_HINT
);
2833 if(V_VT(&l
) == VT_BSTR
&& V_VT(&r
) == VT_BSTR
) {
2834 *ret
= (strcmpW(V_BSTR(&l
), V_BSTR(&r
)) < 0) ^ greater
;
2835 SysFreeString(V_BSTR(&l
));
2836 SysFreeString(V_BSTR(&r
));
2840 hres
= to_number(ctx
->parser
->script
, &l
, ei
, &ln
);
2843 hres
= to_number(ctx
->parser
->script
, &r
, ei
, &rn
);
2848 if(V_VT(&ln
) == VT_I4
&& V_VT(&rn
) == VT_I4
) {
2849 *ret
= (V_I4(&ln
) < V_I4(&rn
)) ^ greater
;
2851 DOUBLE ld
= num_val(&ln
);
2852 DOUBLE rd
= num_val(&rn
);
2854 *ret
= !isnan(ld
) && !isnan(rd
) && ((ld
< rd
) ^ greater
);
2860 /* ECMA-262 3rd Edition 11.8.1 */
2861 HRESULT
less_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2863 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2870 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2874 hres
= less_eval(ctx
, &lval
, &rval
, FALSE
, ei
, &b
);
2875 VariantClear(&lval
);
2876 VariantClear(&rval
);
2880 return return_bool(ret
, b
);
2883 /* ECMA-262 3rd Edition 11.8.3 */
2884 HRESULT
lesseq_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2886 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2893 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2897 hres
= less_eval(ctx
, &rval
, &lval
, TRUE
, ei
, &b
);
2898 VariantClear(&lval
);
2899 VariantClear(&rval
);
2903 return return_bool(ret
, b
);
2906 /* ECMA-262 3rd Edition 11.8.2 */
2907 HRESULT
greater_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2909 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2916 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2920 hres
= less_eval(ctx
, &rval
, &lval
, FALSE
, ei
, &b
);
2921 VariantClear(&lval
);
2922 VariantClear(&rval
);
2926 return return_bool(ret
, b
);
2929 /* ECMA-262 3rd Edition 11.8.4 */
2930 HRESULT
greatereq_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2932 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2939 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2943 hres
= less_eval(ctx
, &lval
, &rval
, TRUE
, ei
, &b
);
2944 VariantClear(&lval
);
2945 VariantClear(&rval
);
2949 return return_bool(ret
, b
);
2952 /* ECMA-262 3rd Edition 11.4.8 */
2953 HRESULT
binary_negation_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2955 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2963 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2967 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2968 exprval_release(&exprval
);
2972 hres
= to_int32(ctx
->parser
->script
, &val
, ei
, &i
);
2976 ret
->type
= EXPRVAL_VARIANT
;
2977 V_VT(&ret
->u
.var
) = VT_I4
;
2978 V_I4(&ret
->u
.var
) = ~i
;
2982 /* ECMA-262 3rd Edition 11.4.9 */
2983 HRESULT
logical_negation_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2985 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2992 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2996 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, ei
, &b
);
2997 exprval_release(&exprval
);
3001 return return_bool(ret
, !b
);
3004 /* ECMA-262 3rd Edition 11.7.1 */
3005 static HRESULT
lshift_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3011 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
3015 hres
= to_uint32(ctx
->parser
->script
, rval
, ei
, &ri
);
3020 V_I4(retv
) = li
<< (ri
&0x1f);
3024 /* ECMA-262 3rd Edition 11.7.1 */
3025 HRESULT
left_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3027 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3031 return binary_expr_eval(ctx
, expr
, lshift_eval
, ei
, ret
);
3034 /* ECMA-262 3rd Edition 11.7.2 */
3035 static HRESULT
rshift_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3041 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
3045 hres
= to_uint32(ctx
->parser
->script
, rval
, ei
, &ri
);
3050 V_I4(retv
) = li
>> (ri
&0x1f);
3054 /* ECMA-262 3rd Edition 11.7.2 */
3055 HRESULT
right_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3057 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3061 return binary_expr_eval(ctx
, expr
, rshift_eval
, ei
, ret
);
3064 /* ECMA-262 3rd Edition 11.7.3 */
3065 static HRESULT
rshift2_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3070 hres
= to_uint32(ctx
->parser
->script
, lval
, ei
, &li
);
3074 hres
= to_uint32(ctx
->parser
->script
, rval
, ei
, &ri
);
3079 V_I4(retv
) = li
>> (ri
&0x1f);
3083 /* ECMA-262 3rd Edition 11.7.3 */
3084 HRESULT
right2_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3086 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3090 return binary_expr_eval(ctx
, expr
, rshift2_eval
, ei
, ret
);
3093 /* ECMA-262 3rd Edition 11.13.1 */
3094 HRESULT
assign_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3096 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3097 exprval_t exprval
, exprvalr
;
3103 hres
= expr_eval(ctx
, expr
->expression1
, EXPR_NEWREF
, ei
, &exprval
);
3107 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprvalr
);
3108 if(SUCCEEDED(hres
)) {
3109 hres
= exprval_to_value(ctx
->parser
->script
, &exprvalr
, ei
, &rval
);
3110 exprval_release(&exprvalr
);
3114 hres
= put_value(ctx
->parser
->script
, &exprval
, &rval
, ei
);
3116 exprval_release(&exprval
);
3120 ret
->type
= EXPRVAL_VARIANT
;
3125 /* ECMA-262 3rd Edition 11.13.2 */
3126 HRESULT
assign_lshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3128 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3132 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, lshift_eval
, ei
, ret
);
3135 /* ECMA-262 3rd Edition 11.13.2 */
3136 HRESULT
assign_rshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3138 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3142 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, rshift_eval
, ei
, ret
);
3145 /* ECMA-262 3rd Edition 11.13.2 */
3146 HRESULT
assign_rrshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3148 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3152 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, rshift2_eval
, ei
, ret
);
3155 /* ECMA-262 3rd Edition 11.13.2 */
3156 HRESULT
assign_add_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3158 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3162 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, add_eval
, ei
, ret
);
3165 /* ECMA-262 3rd Edition 11.13.2 */
3166 HRESULT
assign_sub_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3168 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3172 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, sub_eval
, ei
, ret
);
3175 /* ECMA-262 3rd Edition 11.13.2 */
3176 HRESULT
assign_mul_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3178 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3182 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, mul_eval
, ei
, ret
);
3185 /* ECMA-262 3rd Edition 11.13.2 */
3186 HRESULT
assign_div_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3188 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3192 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, div_eval
, ei
, ret
);
3195 /* ECMA-262 3rd Edition 11.13.2 */
3196 HRESULT
assign_mod_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3198 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3202 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, mod_eval
, ei
, ret
);
3205 /* ECMA-262 3rd Edition 11.13.2 */
3206 HRESULT
assign_and_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3208 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3212 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, bitand_eval
, ei
, ret
);
3215 /* ECMA-262 3rd Edition 11.13.2 */
3216 HRESULT
assign_or_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3218 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3222 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, bitor_eval
, ei
, ret
);
3225 /* ECMA-262 3rd Edition 11.13.2 */
3226 HRESULT
assign_xor_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3228 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3232 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, xor_eval
, ei
, ret
);