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 struct _return_type_t
{
45 static inline HRESULT
stat_eval(script_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
47 return stat
->eval(ctx
, stat
, rt
, ret
);
50 static inline HRESULT
expr_eval(script_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
52 return expr
->eval(ctx
, expr
, flags
, ei
, ret
);
55 static void exprval_release(exprval_t
*val
)
59 if(V_VT(&val
->u
.var
) != VT_EMPTY
)
60 VariantClear(&val
->u
.var
);
64 IDispatch_Release(val
->u
.idref
.disp
);
67 if(val
->u
.nameref
.disp
)
68 IDispatch_Release(val
->u
.nameref
.disp
);
69 SysFreeString(val
->u
.nameref
.name
);
72 SysFreeString(val
->u
.identifier
);
76 /* ECMA-262 3rd Edition 8.7.1 */
77 static HRESULT
exprval_value(script_ctx_t
*ctx
, exprval_t
*val
, jsexcept_t
*ei
, VARIANT
*ret
)
83 return VariantCopy(ret
, &val
->u
.var
);
85 if(!val
->u
.idref
.disp
) {
86 FIXME("throw ReferenceError\n");
90 return disp_propget(ctx
, val
->u
.idref
.disp
, val
->u
.idref
.id
, ret
, ei
, NULL
/*FIXME*/);
94 return throw_type_error(ctx
, ei
, JS_E_UNDEFINED_VARIABLE
, val
->u
.identifier
);
97 ERR("type %d\n", val
->type
);
101 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*val
, jsexcept_t
*ei
, VARIANT
*ret
)
103 if(val
->type
== EXPRVAL_VARIANT
) {
105 V_VT(&val
->u
.var
) = VT_EMPTY
;
109 return exprval_value(ctx
, val
, ei
, ret
);
112 static HRESULT
exprval_to_boolean(script_ctx_t
*ctx
, exprval_t
*exprval
, jsexcept_t
*ei
, VARIANT_BOOL
*b
)
114 if(exprval
->type
!= EXPRVAL_VARIANT
) {
118 hres
= exprval_to_value(ctx
, exprval
, ei
, &val
);
122 hres
= to_boolean(&val
, b
);
127 return to_boolean(&exprval
->u
.var
, b
);
130 static void exprval_init(exprval_t
*val
)
132 val
->type
= EXPRVAL_VARIANT
;
133 V_VT(&val
->u
.var
) = VT_EMPTY
;
136 static void exprval_set_idref(exprval_t
*val
, IDispatch
*disp
, DISPID id
)
138 val
->type
= EXPRVAL_IDREF
;
139 val
->u
.idref
.disp
= disp
;
140 val
->u
.idref
.id
= id
;
143 IDispatch_AddRef(disp
);
146 HRESULT
scope_push(scope_chain_t
*scope
, jsdisp_t
*obj
, scope_chain_t
**ret
)
148 scope_chain_t
*new_scope
;
150 new_scope
= heap_alloc(sizeof(scope_chain_t
));
152 return E_OUTOFMEMORY
;
157 new_scope
->obj
= obj
;
161 new_scope
->next
= scope
;
163 new_scope
->next
= NULL
;
170 static void scope_pop(scope_chain_t
**scope
)
179 void scope_release(scope_chain_t
*scope
)
185 scope_release(scope
->next
);
187 jsdisp_release(scope
->obj
);
191 HRESULT
create_exec_ctx(script_ctx_t
*script_ctx
, IDispatch
*this_obj
, jsdisp_t
*var_disp
,
192 scope_chain_t
*scope
, BOOL is_global
, exec_ctx_t
**ret
)
196 ctx
= heap_alloc_zero(sizeof(exec_ctx_t
));
198 return E_OUTOFMEMORY
;
201 ctx
->is_global
= is_global
;
204 ctx
->this_obj
= this_obj
;
205 else if(script_ctx
->host_global
)
206 ctx
->this_obj
= script_ctx
->host_global
;
208 ctx
->this_obj
= to_disp(script_ctx
->global
);
209 IDispatch_AddRef(ctx
->this_obj
);
211 jsdisp_addref(var_disp
);
212 ctx
->var_disp
= var_disp
;
216 ctx
->scope_chain
= scope
;
223 void exec_release(exec_ctx_t
*ctx
)
229 scope_release(ctx
->scope_chain
);
231 jsdisp_release(ctx
->var_disp
);
233 IDispatch_Release(ctx
->this_obj
);
237 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, BSTR name
, DWORD flags
, DISPID
*id
)
242 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
244 TRACE("unsing IDispatch\n");
247 return IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &name
, 1, 0, id
);
251 hres
= IDispatchEx_GetDispID(dispex
, name
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
252 IDispatchEx_Release(dispex
);
256 /* ECMA-262 3rd Edition 8.7.2 */
257 static HRESULT
put_value(script_ctx_t
*ctx
, exprval_t
*ref
, VARIANT
*v
, jsexcept_t
*ei
)
259 if(ref
->type
!= EXPRVAL_IDREF
)
260 return throw_reference_error(ctx
, ei
, JS_E_ILLEGAL_ASSIGN
, NULL
);
262 return disp_propput(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, v
, ei
, NULL
/*FIXME*/);
265 static inline BOOL
is_null(const VARIANT
*v
)
267 return V_VT(v
) == VT_NULL
|| (V_VT(v
) == VT_DISPATCH
&& !V_DISPATCH(v
));
270 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
272 IObjectIdentity
*identity
;
273 IUnknown
*unk1
, *unk2
;
281 if(!disp1
|| !disp2
) {
286 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
290 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
292 IUnknown_Release(unk1
);
299 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
300 if(SUCCEEDED(hres
)) {
301 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
302 IObjectIdentity_Release(identity
);
309 IUnknown_Release(unk1
);
310 IUnknown_Release(unk2
);
314 /* ECMA-262 3rd Edition 11.9.6 */
315 static HRESULT
equal2_values(VARIANT
*lval
, VARIANT
*rval
, BOOL
*ret
)
319 if(V_VT(lval
) != V_VT(rval
)) {
320 if(is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
)))
321 *ret
= num_val(lval
) == num_val(rval
);
322 else if(is_null(lval
))
323 *ret
= is_null(rval
);
335 *ret
= V_I4(lval
) == V_I4(rval
);
338 *ret
= V_R8(lval
) == V_R8(rval
);
342 *ret
= SysStringLen(V_BSTR(rval
))?FALSE
:TRUE
;
343 else if(!V_BSTR(rval
))
344 *ret
= SysStringLen(V_BSTR(lval
))?FALSE
:TRUE
;
346 *ret
= !strcmpW(V_BSTR(lval
), V_BSTR(rval
));
349 return disp_cmp(V_DISPATCH(lval
), V_DISPATCH(rval
), ret
);
351 *ret
= !V_BOOL(lval
) == !V_BOOL(rval
);
354 FIXME("unimplemented vt %d\n", V_VT(lval
));
361 static HRESULT
literal_to_var(script_ctx_t
*ctx
, literal_t
*literal
, VARIANT
*v
)
363 switch(literal
->type
) {
369 V_I4(v
) = literal
->u
.lval
;
373 V_R8(v
) = literal
->u
.dval
;
376 BSTR str
= SysAllocString(literal
->u
.wstr
);
378 return E_OUTOFMEMORY
;
386 V_BOOL(v
) = literal
->u
.bval
;
392 hres
= create_regexp(ctx
, literal
->u
.regexp
.str
, literal
->u
.regexp
.str_len
,
393 literal
->u
.regexp
.flags
, ®exp
);
397 var_set_jsdisp(v
, regexp
);
404 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
410 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
411 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
412 hres
= disp_get_id(ctx
, item
->disp
, identifier
, 0, &id
);
413 if(SUCCEEDED(hres
)) {
415 exprval_set_idref(ret
, item
->disp
, id
);
424 HRESULT
exec_source(exec_ctx_t
*ctx
, parser_ctx_t
*parser
, source_elements_t
*source
, BOOL from_eval
,
425 jsexcept_t
*ei
, VARIANT
*retv
)
427 script_ctx_t
*script
= parser
->script
;
428 function_declaration_t
*func
;
429 parser_ctx_t
*prev_parser
;
433 exec_ctx_t
*prev_ctx
;
437 for(func
= source
->functions
; func
; func
= func
->next
) {
441 hres
= create_source_function(parser
, func
->expr
->parameter_list
, func
->expr
->source_elements
,
442 ctx
->scope_chain
, func
->expr
->src_str
, func
->expr
->src_len
, &func_obj
);
446 var_set_jsdisp(&var
, func_obj
);
447 hres
= jsdisp_propput_name(ctx
->var_disp
, func
->expr
->identifier
, &var
, ei
, NULL
);
448 jsdisp_release(func_obj
);
453 for(var
= source
->variables
; var
; var
= var
->next
) {
457 name
= SysAllocString(var
->identifier
);
459 return E_OUTOFMEMORY
;
461 if(!ctx
->is_global
|| !lookup_global_members(parser
->script
, name
, NULL
))
462 hres
= jsdisp_get_id(ctx
->var_disp
, var
->identifier
, fdexNameEnsure
, &id
);
468 prev_ctx
= script
->exec_ctx
;
469 script
->exec_ctx
= ctx
;
471 prev_parser
= ctx
->parser
;
472 ctx
->parser
= parser
;
474 V_VT(&val
) = VT_EMPTY
;
475 memset(&rt
, 0, sizeof(rt
));
478 for(stat
= source
->statement
; stat
; stat
= stat
->next
) {
479 hres
= stat_eval(script
, stat
, &rt
, &tmp
);
485 if(rt
.type
!= RT_NORMAL
)
489 script
->exec_ctx
= prev_ctx
;
490 ctx
->parser
= prev_parser
;
492 if(rt
.type
!= RT_NORMAL
&& rt
.type
!= RT_RETURN
) {
493 FIXME("wrong rt %d\n", rt
.type
);
503 if(!retv
|| (!from_eval
&& rt
.type
!= RT_RETURN
))
510 /* ECMA-262 3rd Edition 10.1.4 */
511 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
513 scope_chain_t
*scope
;
518 TRACE("%s\n", debugstr_w(identifier
));
520 for(scope
= ctx
->exec_ctx
->scope_chain
; scope
; scope
= scope
->next
) {
521 hres
= jsdisp_get_id(scope
->obj
, identifier
, 0, &id
);
522 if(SUCCEEDED(hres
)) {
523 exprval_set_idref(ret
, to_disp(scope
->obj
), id
);
528 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
529 if(SUCCEEDED(hres
)) {
530 exprval_set_idref(ret
, to_disp(ctx
->global
), id
);
534 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
535 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
542 hres
= IActiveScriptSite_GetItemInfo(ctx
->site
, identifier
,
543 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
545 WARN("GetItemInfo failed: %08x\n", hres
);
549 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
550 IUnknown_Release(unk
);
552 WARN("object does not implement IDispatch\n");
557 ret
->type
= EXPRVAL_VARIANT
;
558 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
559 V_DISPATCH(&ret
->u
.var
) = item
->disp
;
560 IDispatch_AddRef(item
->disp
);
565 if(lookup_global_members(ctx
, identifier
, ret
))
568 if(flags
& EXPR_NEWREF
) {
569 hres
= jsdisp_get_id(ctx
->global
, identifier
, fdexNameEnsure
, &id
);
573 exprval_set_idref(ret
, to_disp(ctx
->global
), id
);
577 ret
->type
= EXPRVAL_INVALID
;
578 ret
->u
.identifier
= SysAllocString(identifier
);
579 if(!ret
->u
.identifier
)
580 return E_OUTOFMEMORY
;
585 /* ECMA-262 3rd Edition 12.1 */
586 HRESULT
block_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
588 block_statement_t
*stat
= (block_statement_t
*)_stat
;
595 V_VT(&val
) = VT_EMPTY
;
596 for(iter
= stat
->stat_list
; iter
; iter
= iter
->next
) {
597 hres
= stat_eval(ctx
, iter
, rt
, &tmp
);
603 if(rt
->type
!= RT_NORMAL
)
616 /* ECMA-262 3rd Edition 12.2 */
617 static HRESULT
variable_list_eval(script_ctx_t
*ctx
, variable_declaration_t
*var_list
, jsexcept_t
*ei
)
619 variable_declaration_t
*iter
;
622 for(iter
= var_list
; iter
; iter
= iter
->next
) {
629 hres
= expr_eval(ctx
, iter
->expr
, 0, ei
, &exprval
);
633 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
634 exprval_release(&exprval
);
638 hres
= jsdisp_propput_name(ctx
->exec_ctx
->var_disp
, iter
->identifier
, &val
, ei
, NULL
/*FIXME*/);
647 /* ECMA-262 3rd Edition 12.2 */
648 HRESULT
var_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
650 var_statement_t
*stat
= (var_statement_t
*)_stat
;
655 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
659 V_VT(ret
) = VT_EMPTY
;
663 /* ECMA-262 3rd Edition 12.3 */
664 HRESULT
empty_statement_eval(script_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
668 V_VT(ret
) = VT_EMPTY
;
672 /* ECMA-262 3rd Edition 12.4 */
673 HRESULT
expression_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
675 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
682 hres
= expr_eval(ctx
, stat
->expr
, EXPR_NOVAL
, &rt
->ei
, &exprval
);
686 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
687 exprval_release(&exprval
);
692 TRACE("= %s\n", debugstr_variant(ret
));
696 /* ECMA-262 3rd Edition 12.5 */
697 HRESULT
if_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
699 if_statement_t
*stat
= (if_statement_t
*)_stat
;
706 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
710 hres
= exprval_to_boolean(ctx
, &exprval
, &rt
->ei
, &b
);
711 exprval_release(&exprval
);
716 hres
= stat_eval(ctx
, stat
->if_stat
, rt
, ret
);
717 else if(stat
->else_stat
)
718 hres
= stat_eval(ctx
, stat
->else_stat
, rt
, ret
);
720 V_VT(ret
) = VT_EMPTY
;
725 /* ECMA-262 3rd Edition 12.6.2 */
726 HRESULT
while_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
728 while_statement_t
*stat
= (while_statement_t
*)_stat
;
737 V_VT(&val
) = VT_EMPTY
;
738 test_expr
= !stat
->do_while
;
742 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
746 hres
= exprval_to_boolean(ctx
, &exprval
, &rt
->ei
, &b
);
747 exprval_release(&exprval
);
748 if(FAILED(hres
) || !b
)
754 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
761 if(rt
->type
== RT_CONTINUE
)
762 rt
->type
= RT_NORMAL
;
763 if(rt
->type
!= RT_NORMAL
)
772 if(rt
->type
== RT_BREAK
)
773 rt
->type
= RT_NORMAL
;
779 /* ECMA-262 3rd Edition 12.6.3 */
780 HRESULT
for_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
782 for_statement_t
*stat
= (for_statement_t
*)_stat
;
783 VARIANT val
, tmp
, retv
;
790 if(stat
->variable_list
) {
791 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
794 }else if(stat
->begin_expr
) {
795 hres
= expr_eval(ctx
, stat
->begin_expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
799 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
800 exprval_release(&exprval
);
807 V_VT(&retv
) = VT_EMPTY
;
811 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
815 hres
= exprval_to_boolean(ctx
, &exprval
, &rt
->ei
, &b
);
816 exprval_release(&exprval
);
817 if(FAILED(hres
) || !b
)
821 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
828 if(rt
->type
== RT_CONTINUE
)
829 rt
->type
= RT_NORMAL
;
830 else if(rt
->type
!= RT_NORMAL
)
834 hres
= expr_eval(ctx
, stat
->end_expr
, 0, &rt
->ei
, &exprval
);
838 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
839 exprval_release(&exprval
);
852 if(rt
->type
== RT_BREAK
)
853 rt
->type
= RT_NORMAL
;
859 /* ECMA-262 3rd Edition 12.6.4 */
860 HRESULT
forin_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
862 forin_statement_t
*stat
= (forin_statement_t
*)_stat
;
863 VARIANT val
, name
, retv
, tmp
;
864 DISPID id
= DISPID_STARTENUM
;
865 BSTR str
, identifier
= NULL
;
873 hres
= variable_list_eval(ctx
, stat
->variable
, &rt
->ei
);
878 hres
= expr_eval(ctx
, stat
->in_expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
882 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
883 exprval_release(&exprval
);
887 if(V_VT(&val
) != VT_DISPATCH
) {
888 TRACE("in vt %d\n", V_VT(&val
));
890 V_VT(ret
) = VT_EMPTY
;
894 hres
= IDispatch_QueryInterface(V_DISPATCH(&val
), &IID_IDispatchEx
, (void**)&in_obj
);
895 IDispatch_Release(V_DISPATCH(&val
));
897 TRACE("Object doesn't support IDispatchEx\n");
898 V_VT(ret
) = VT_EMPTY
;
902 V_VT(&retv
) = VT_EMPTY
;
905 identifier
= SysAllocString(stat
->variable
->identifier
);
908 hres
= IDispatchEx_GetNextDispID(in_obj
, fdexEnumDefault
, id
, &id
);
909 if(FAILED(hres
) || hres
== S_FALSE
)
912 hres
= IDispatchEx_GetMemberName(in_obj
, id
, &str
);
916 TRACE("iter %s\n", debugstr_w(str
));
919 hres
= identifier_eval(ctx
, identifier
, 0, NULL
, &exprval
);
921 hres
= expr_eval(ctx
, stat
->expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
922 if(SUCCEEDED(hres
)) {
923 V_VT(&name
) = VT_BSTR
;
925 hres
= put_value(ctx
, &exprval
, &name
, &rt
->ei
);
926 exprval_release(&exprval
);
932 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
939 if(rt
->type
== RT_CONTINUE
)
940 rt
->type
= RT_NORMAL
;
941 else if(rt
->type
!= RT_NORMAL
)
945 SysFreeString(identifier
);
946 IDispatchEx_Release(in_obj
);
952 if(rt
->type
== RT_BREAK
)
953 rt
->type
= RT_NORMAL
;
959 /* ECMA-262 3rd Edition 12.7 */
960 HRESULT
continue_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
962 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
966 if(stat
->identifier
) {
967 FIXME("indentifier not implemented\n");
971 rt
->type
= RT_CONTINUE
;
972 V_VT(ret
) = VT_EMPTY
;
976 /* ECMA-262 3rd Edition 12.8 */
977 HRESULT
break_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
979 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
983 if(stat
->identifier
) {
984 FIXME("indentifier not implemented\n");
989 V_VT(ret
) = VT_EMPTY
;
993 /* ECMA-262 3rd Edition 12.9 */
994 HRESULT
return_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
996 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
1004 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1008 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, ret
);
1009 exprval_release(&exprval
);
1013 V_VT(ret
) = VT_EMPTY
;
1016 TRACE("= %s\n", debugstr_variant(ret
));
1017 rt
->type
= RT_RETURN
;
1021 /* ECMA-262 3rd Edition 12.10 */
1022 HRESULT
with_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1024 with_statement_t
*stat
= (with_statement_t
*)_stat
;
1033 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1037 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
1038 exprval_release(&exprval
);
1042 hres
= to_object(ctx
, &val
, &disp
);
1047 obj
= iface_to_jsdisp((IUnknown
*)disp
);
1048 IDispatch_Release(disp
);
1050 FIXME("disp id not jsdisp\n");
1054 hres
= scope_push(ctx
->exec_ctx
->scope_chain
, obj
, &ctx
->exec_ctx
->scope_chain
);
1055 jsdisp_release(obj
);
1059 hres
= stat_eval(ctx
, stat
->statement
, rt
, ret
);
1061 scope_pop(&ctx
->exec_ctx
->scope_chain
);
1065 /* ECMA-262 3rd Edition 12.12 */
1066 HRESULT
labelled_statement_eval(script_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
1072 /* ECMA-262 3rd Edition 12.13 */
1073 HRESULT
switch_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1075 switch_statement_t
*stat
= (switch_statement_t
*)_stat
;
1076 case_clausule_t
*iter
, *default_clausule
= NULL
;
1077 statement_t
*stat_iter
;
1085 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1089 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
1090 exprval_release(&exprval
);
1094 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1096 default_clausule
= iter
;
1100 hres
= expr_eval(ctx
, iter
->expr
, 0, &rt
->ei
, &exprval
);
1104 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &cval
);
1105 exprval_release(&exprval
);
1109 hres
= equal2_values(&val
, &cval
, &b
);
1110 VariantClear(&cval
);
1111 if(FAILED(hres
) || b
)
1120 iter
= default_clausule
;
1122 V_VT(&val
) = VT_EMPTY
;
1126 for(stat_iter
= iter
->stat
; stat_iter
; stat_iter
= stat_iter
->next
) {
1127 hres
= stat_eval(ctx
, stat_iter
, rt
, &tmp
);
1134 if(rt
->type
!= RT_NORMAL
)
1144 if(rt
->type
== RT_BREAK
)
1145 rt
->type
= RT_NORMAL
;
1151 /* ECMA-262 3rd Edition 12.13 */
1152 HRESULT
throw_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1154 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
1161 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1165 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
1166 exprval_release(&exprval
);
1171 return DISP_E_EXCEPTION
;
1174 /* ECMA-262 3rd Edition 12.14 */
1175 static HRESULT
catch_eval(script_ctx_t
*ctx
, catch_block_t
*block
, return_type_t
*rt
, VARIANT
*ret
)
1182 memset(&rt
->ei
, 0, sizeof(jsexcept_t
));
1184 hres
= create_dispex(ctx
, NULL
, NULL
, &var_disp
);
1185 if(SUCCEEDED(hres
)) {
1186 hres
= jsdisp_propput_name(var_disp
, block
->identifier
, &ex
, &rt
->ei
, NULL
/*FIXME*/);
1187 if(SUCCEEDED(hres
)) {
1188 hres
= scope_push(ctx
->exec_ctx
->scope_chain
, var_disp
, &ctx
->exec_ctx
->scope_chain
);
1189 if(SUCCEEDED(hres
)) {
1190 hres
= stat_eval(ctx
, block
->statement
, rt
, &val
);
1191 scope_pop(&ctx
->exec_ctx
->scope_chain
);
1195 jsdisp_release(var_disp
);
1206 /* ECMA-262 3rd Edition 12.14 */
1207 HRESULT
try_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1209 try_statement_t
*stat
= (try_statement_t
*)_stat
;
1215 hres
= stat_eval(ctx
, stat
->try_statement
, rt
, &val
);
1217 TRACE("EXCEPTION\n");
1218 if(!stat
->catch_block
)
1221 hres
= catch_eval(ctx
, stat
->catch_block
, rt
, &val
);
1226 if(stat
->finally_statement
) {
1228 hres
= stat_eval(ctx
, stat
->finally_statement
, rt
, &val
);
1237 static HRESULT
return_bool(exprval_t
*ret
, DWORD b
)
1239 ret
->type
= EXPRVAL_VARIANT
;
1240 V_VT(&ret
->u
.var
) = VT_BOOL
;
1241 V_BOOL(&ret
->u
.var
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
1246 static HRESULT
get_binary_expr_values(script_ctx_t
*ctx
, binary_expression_t
*expr
, jsexcept_t
*ei
, VARIANT
*lval
, VARIANT
*rval
)
1251 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1255 hres
= exprval_to_value(ctx
, &exprval
, ei
, lval
);
1256 exprval_release(&exprval
);
1260 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1261 if(SUCCEEDED(hres
)) {
1262 hres
= exprval_to_value(ctx
, &exprval
, ei
, rval
);
1263 exprval_release(&exprval
);
1274 typedef HRESULT (*oper_t
)(script_ctx_t
*,VARIANT
*,VARIANT
*,jsexcept_t
*,VARIANT
*);
1276 static HRESULT
binary_expr_eval(script_ctx_t
*ctx
, binary_expression_t
*expr
, oper_t oper
, jsexcept_t
*ei
,
1279 VARIANT lval
, rval
, retv
;
1282 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
1286 hres
= oper(ctx
, &lval
, &rval
, ei
, &retv
);
1287 VariantClear(&lval
);
1288 VariantClear(&rval
);
1292 ret
->type
= EXPRVAL_VARIANT
;
1297 /* ECMA-262 3rd Edition 11.13.2 */
1298 static HRESULT
assign_oper_eval(script_ctx_t
*ctx
, expression_t
*lexpr
, expression_t
*rexpr
, oper_t oper
,
1299 jsexcept_t
*ei
, exprval_t
*ret
)
1301 VARIANT retv
, lval
, rval
;
1302 exprval_t exprval
, exprvalr
;
1305 hres
= expr_eval(ctx
, lexpr
, EXPR_NEWREF
, ei
, &exprval
);
1309 hres
= exprval_value(ctx
, &exprval
, ei
, &lval
);
1310 if(SUCCEEDED(hres
)) {
1311 hres
= expr_eval(ctx
, rexpr
, 0, ei
, &exprvalr
);
1312 if(SUCCEEDED(hres
)) {
1313 hres
= exprval_value(ctx
, &exprvalr
, ei
, &rval
);
1314 exprval_release(&exprvalr
);
1316 if(SUCCEEDED(hres
)) {
1317 hres
= oper(ctx
, &lval
, &rval
, ei
, &retv
);
1318 VariantClear(&rval
);
1320 VariantClear(&lval
);
1323 if(SUCCEEDED(hres
)) {
1324 hres
= put_value(ctx
, &exprval
, &retv
, ei
);
1326 VariantClear(&retv
);
1328 exprval_release(&exprval
);
1333 ret
->type
= EXPRVAL_VARIANT
;
1338 /* ECMA-262 3rd Edition 13 */
1339 HRESULT
function_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1341 function_expression_t
*expr
= (function_expression_t
*)_expr
;
1347 if(expr
->identifier
) {
1348 hres
= jsdisp_propget_name(ctx
->exec_ctx
->var_disp
, expr
->identifier
, &var
, ei
, NULL
/*FIXME*/);
1354 hres
= create_source_function(ctx
->exec_ctx
->parser
, expr
->parameter_list
, expr
->source_elements
, ctx
->exec_ctx
->scope_chain
,
1355 expr
->src_str
, expr
->src_len
, &dispex
);
1359 var_set_jsdisp(&var
, dispex
);
1362 ret
->type
= EXPRVAL_VARIANT
;
1367 /* ECMA-262 3rd Edition 11.12 */
1368 HRESULT
conditional_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1370 conditional_expression_t
*expr
= (conditional_expression_t
*)_expr
;
1377 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1381 hres
= exprval_to_boolean(ctx
, &exprval
, ei
, &b
);
1382 exprval_release(&exprval
);
1386 return expr_eval(ctx
, b
? expr
->true_expression
: expr
->false_expression
, flags
, ei
, ret
);
1389 /* ECMA-262 3rd Edition 11.2.1 */
1390 HRESULT
array_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1392 array_expression_t
*expr
= (array_expression_t
*)_expr
;
1394 VARIANT member
, val
;
1397 IDispatch
*obj
= NULL
;
1402 hres
= expr_eval(ctx
, expr
->member_expr
, 0, ei
, &exprval
);
1406 hres
= exprval_to_value(ctx
, &exprval
, ei
, &member
);
1407 exprval_release(&exprval
);
1411 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
1412 if(SUCCEEDED(hres
)) {
1413 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1414 exprval_release(&exprval
);
1417 if(SUCCEEDED(hres
)) {
1418 hres
= to_object(ctx
, &member
, &obj
);
1422 VariantClear(&member
);
1423 if(SUCCEEDED(hres
)) {
1424 hres
= to_string(ctx
, &val
, ei
, &str
);
1426 if(SUCCEEDED(hres
)) {
1427 if(flags
& EXPR_STRREF
) {
1428 ret
->type
= EXPRVAL_NAMEREF
;
1429 ret
->u
.nameref
.disp
= obj
;
1430 ret
->u
.nameref
.name
= str
;
1434 hres
= disp_get_id(ctx
, obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1438 if(SUCCEEDED(hres
)) {
1439 exprval_set_idref(ret
, obj
, id
);
1440 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1445 IDispatch_Release(obj
);
1451 /* ECMA-262 3rd Edition 11.2.1 */
1452 HRESULT
member_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1454 member_expression_t
*expr
= (member_expression_t
*)_expr
;
1455 IDispatch
*obj
= NULL
;
1464 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1468 hres
= exprval_to_value(ctx
, &exprval
, ei
, &member
);
1469 exprval_release(&exprval
);
1473 hres
= to_object(ctx
, &member
, &obj
);
1474 VariantClear(&member
);
1478 str
= SysAllocString(expr
->identifier
);
1479 if(flags
& EXPR_STRREF
) {
1480 ret
->type
= EXPRVAL_NAMEREF
;
1481 ret
->u
.nameref
.disp
= obj
;
1482 ret
->u
.nameref
.name
= str
;
1486 hres
= disp_get_id(ctx
, obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1488 if(SUCCEEDED(hres
)) {
1489 exprval_set_idref(ret
, obj
, id
);
1490 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1495 IDispatch_Release(obj
);
1499 static void free_dp(DISPPARAMS
*dp
)
1503 for(i
=0; i
< dp
->cArgs
; i
++)
1504 VariantClear(dp
->rgvarg
+i
);
1505 heap_free(dp
->rgvarg
);
1508 static HRESULT
args_to_param(script_ctx_t
*ctx
, argument_t
*args
, jsexcept_t
*ei
, DISPPARAMS
*dp
)
1514 HRESULT hres
= S_OK
;
1516 memset(dp
, 0, sizeof(*dp
));
1520 for(iter
= args
; iter
; iter
= iter
->next
)
1523 vargs
= heap_alloc_zero(cnt
* sizeof(*vargs
));
1525 return E_OUTOFMEMORY
;
1527 for(i
= cnt
, iter
= args
; iter
; iter
= iter
->next
) {
1528 hres
= expr_eval(ctx
, iter
->expr
, 0, ei
, &exprval
);
1532 hres
= exprval_to_value(ctx
, &exprval
, ei
, vargs
+ (--i
));
1533 exprval_release(&exprval
);
1548 /* ECMA-262 3rd Edition 11.2.2 */
1549 HRESULT
new_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1551 call_expression_t
*expr
= (call_expression_t
*)_expr
;
1553 VARIANT constr
, var
;
1559 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1563 hres
= args_to_param(ctx
, expr
->argument_list
, ei
, &dp
);
1565 hres
= exprval_to_value(ctx
, &exprval
, ei
, &constr
);
1566 exprval_release(&exprval
);
1570 /* NOTE: Should use to_object here */
1572 if(V_VT(&constr
) == VT_NULL
) {
1573 VariantClear(&constr
);
1574 return throw_type_error(ctx
, ei
, JS_E_OBJECT_EXPECTED
, NULL
);
1575 } else if(V_VT(&constr
) != VT_DISPATCH
) {
1576 VariantClear(&constr
);
1577 return throw_type_error(ctx
, ei
, JS_E_INVALID_ACTION
, NULL
);
1578 } else if(!V_DISPATCH(&constr
)) {
1579 VariantClear(&constr
);
1580 return throw_type_error(ctx
, ei
, JS_E_INVALID_PROPERTY
, NULL
);
1583 hres
= disp_call(ctx
, V_DISPATCH(&constr
), DISPID_VALUE
,
1584 DISPATCH_CONSTRUCT
, &dp
, &var
, ei
, NULL
/*FIXME*/);
1585 IDispatch_Release(V_DISPATCH(&constr
));
1590 ret
->type
= EXPRVAL_VARIANT
;
1595 /* ECMA-262 3rd Edition 11.2.3 */
1596 HRESULT
call_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1598 call_expression_t
*expr
= (call_expression_t
*)_expr
;
1606 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1610 hres
= args_to_param(ctx
, expr
->argument_list
, ei
, &dp
);
1611 if(SUCCEEDED(hres
)) {
1612 switch(exprval
.type
) {
1613 case EXPRVAL_VARIANT
:
1614 if(V_VT(&exprval
.u
.var
) == VT_DISPATCH
)
1615 hres
= disp_call(ctx
, V_DISPATCH(&exprval
.u
.var
), DISPID_VALUE
,
1616 DISPATCH_METHOD
, &dp
, flags
& EXPR_NOVAL
? NULL
: &var
, ei
, NULL
/*FIXME*/);
1618 hres
= throw_type_error(ctx
, ei
, JS_E_INVALID_PROPERTY
, NULL
);
1621 hres
= disp_call(ctx
, exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
,
1622 DISPATCH_METHOD
, &dp
, flags
& EXPR_NOVAL
? NULL
: &var
, ei
, NULL
/*FIXME*/);
1624 case EXPRVAL_INVALID
:
1625 hres
= throw_type_error(ctx
, ei
, JS_E_OBJECT_EXPECTED
, NULL
);
1628 FIXME("unimplemented type %d\n", exprval
.type
);
1635 exprval_release(&exprval
);
1639 ret
->type
= EXPRVAL_VARIANT
;
1640 if(flags
& EXPR_NOVAL
) {
1641 V_VT(&ret
->u
.var
) = VT_EMPTY
;
1643 TRACE("= %s\n", debugstr_variant(&var
));
1649 /* ECMA-262 3rd Edition 11.1.1 */
1650 HRESULT
this_expression_eval(script_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1654 ret
->type
= EXPRVAL_VARIANT
;
1655 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
1656 V_DISPATCH(&ret
->u
.var
) = ctx
->exec_ctx
->this_obj
;
1657 IDispatch_AddRef(ctx
->exec_ctx
->this_obj
);
1661 /* ECMA-262 3rd Edition 10.1.4 */
1662 HRESULT
identifier_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1664 identifier_expression_t
*expr
= (identifier_expression_t
*)_expr
;
1670 identifier
= SysAllocString(expr
->identifier
);
1672 return E_OUTOFMEMORY
;
1674 hres
= identifier_eval(ctx
, identifier
, flags
, ei
, ret
);
1676 SysFreeString(identifier
);
1680 /* ECMA-262 3rd Edition 7.8 */
1681 HRESULT
literal_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1683 literal_expression_t
*expr
= (literal_expression_t
*)_expr
;
1689 hres
= literal_to_var(ctx
, expr
->literal
, &var
);
1693 ret
->type
= EXPRVAL_VARIANT
;
1698 /* ECMA-262 3rd Edition 11.1.4 */
1699 HRESULT
array_literal_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1701 array_literal_expression_t
*expr
= (array_literal_expression_t
*)_expr
;
1702 DWORD length
= 0, i
= 0;
1703 array_element_t
*elem
;
1711 for(elem
= expr
->element_list
; elem
; elem
= elem
->next
)
1712 length
+= elem
->elision
+1;
1713 length
+= expr
->length
;
1715 hres
= create_array(ctx
, length
, &array
);
1719 for(elem
= expr
->element_list
; elem
; elem
= elem
->next
) {
1722 hres
= expr_eval(ctx
, elem
->expr
, 0, ei
, &exprval
);
1726 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1727 exprval_release(&exprval
);
1731 hres
= jsdisp_propput_idx(array
, i
, &val
, ei
, NULL
/*FIXME*/);
1740 jsdisp_release(array
);
1744 ret
->type
= EXPRVAL_VARIANT
;
1745 var_set_jsdisp(&ret
->u
.var
, array
);
1749 /* ECMA-262 3rd Edition 11.1.5 */
1750 HRESULT
property_value_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1752 property_value_expression_t
*expr
= (property_value_expression_t
*)_expr
;
1762 hres
= create_object(ctx
, NULL
, &obj
);
1766 for(iter
= expr
->property_list
; iter
; iter
= iter
->next
) {
1767 hres
= literal_to_var(ctx
, iter
->name
, &tmp
);
1771 hres
= to_string(ctx
, &tmp
, ei
, &name
);
1776 hres
= expr_eval(ctx
, iter
->value
, 0, ei
, &exprval
);
1777 if(SUCCEEDED(hres
)) {
1778 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1779 exprval_release(&exprval
);
1780 if(SUCCEEDED(hres
)) {
1781 hres
= jsdisp_propput_name(obj
, name
, &val
, ei
, NULL
/*FIXME*/);
1786 SysFreeString(name
);
1792 jsdisp_release(obj
);
1796 ret
->type
= EXPRVAL_VARIANT
;
1797 var_set_jsdisp(&ret
->u
.var
, obj
);
1801 /* ECMA-262 3rd Edition 11.14 */
1802 HRESULT
comma_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1804 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1810 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
1814 VariantClear(&lval
);
1816 ret
->type
= EXPRVAL_VARIANT
;
1821 /* ECMA-262 3rd Edition 11.11 */
1822 HRESULT
logical_or_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1824 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1832 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1836 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1837 exprval_release(&exprval
);
1841 hres
= to_boolean(&val
, &b
);
1842 if(SUCCEEDED(hres
) && b
) {
1843 ret
->type
= EXPRVAL_VARIANT
;
1852 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1856 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1857 exprval_release(&exprval
);
1861 ret
->type
= EXPRVAL_VARIANT
;
1866 /* ECMA-262 3rd Edition 11.11 */
1867 HRESULT
logical_and_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1869 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1877 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1881 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1882 exprval_release(&exprval
);
1886 hres
= to_boolean(&val
, &b
);
1887 if(SUCCEEDED(hres
) && !b
) {
1888 ret
->type
= EXPRVAL_VARIANT
;
1897 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1901 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1902 exprval_release(&exprval
);
1906 ret
->type
= EXPRVAL_VARIANT
;
1911 /* ECMA-262 3rd Edition 11.10 */
1912 static HRESULT
bitor_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1917 hres
= to_int32(ctx
, lval
, ei
, &li
);
1921 hres
= to_int32(ctx
, rval
, ei
, &ri
);
1930 /* ECMA-262 3rd Edition 11.10 */
1931 HRESULT
binary_or_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1933 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1937 return binary_expr_eval(ctx
, expr
, bitor_eval
, ei
, ret
);
1940 /* ECMA-262 3rd Edition 11.10 */
1941 static HRESULT
xor_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1946 hres
= to_int32(ctx
, lval
, ei
, &li
);
1950 hres
= to_int32(ctx
, rval
, ei
, &ri
);
1959 /* ECMA-262 3rd Edition 11.10 */
1960 HRESULT
binary_xor_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1962 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1966 return binary_expr_eval(ctx
, expr
, xor_eval
, ei
, ret
);
1969 /* ECMA-262 3rd Edition 11.10 */
1970 static HRESULT
bitand_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1975 hres
= to_int32(ctx
, lval
, ei
, &li
);
1979 hres
= to_int32(ctx
, rval
, ei
, &ri
);
1988 /* ECMA-262 3rd Edition 11.10 */
1989 HRESULT
binary_and_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1991 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1995 return binary_expr_eval(ctx
, expr
, bitand_eval
, ei
, ret
);
1998 /* ECMA-262 3rd Edition 11.8.6 */
1999 static HRESULT
instanceof_eval(script_ctx_t
*ctx
, VARIANT
*inst
, VARIANT
*objv
, jsexcept_t
*ei
, VARIANT
*retv
)
2001 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
2002 VARIANT_BOOL ret
= VARIANT_FALSE
;
2007 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2009 if(V_VT(objv
) != VT_DISPATCH
|| !V_DISPATCH(objv
))
2010 return throw_type_error(ctx
, ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
2012 obj
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(objv
));
2014 FIXME("non-jsdisp objects not supported\n");
2018 if(is_class(obj
, JSCLASS_FUNCTION
)) {
2019 hres
= jsdisp_propget_name(obj
, prototypeW
, &var
, ei
, NULL
/*FIXME*/);
2021 hres
= throw_type_error(ctx
, ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
2023 jsdisp_release(obj
);
2027 if(V_VT(&var
) == VT_DISPATCH
) {
2028 if(V_VT(inst
) == VT_DISPATCH
)
2029 tmp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(inst
));
2030 for(iter
= tmp
; iter
; iter
= iter
->prototype
) {
2031 hres
= disp_cmp(V_DISPATCH(&var
), to_disp(iter
), &b
);
2041 jsdisp_release(tmp
);
2043 FIXME("prototype is not an object\n");
2051 V_VT(retv
) = VT_BOOL
;
2056 /* ECMA-262 3rd Edition 11.8.6 */
2057 HRESULT
instanceof_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2059 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2063 return binary_expr_eval(ctx
, expr
, instanceof_eval
, ei
, ret
);
2066 /* ECMA-262 3rd Edition 11.8.7 */
2067 static HRESULT
in_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*obj
, jsexcept_t
*ei
, VARIANT
*retv
)
2074 if(V_VT(obj
) != VT_DISPATCH
|| !V_DISPATCH(obj
))
2075 return throw_type_error(ctx
, ei
, JS_E_OBJECT_EXPECTED
, NULL
);
2077 hres
= to_string(ctx
, lval
, ei
, &str
);
2081 hres
= disp_get_id(ctx
, V_DISPATCH(obj
), str
, 0, &id
);
2085 else if(hres
== DISP_E_UNKNOWNNAME
)
2086 ret
= VARIANT_FALSE
;
2090 V_VT(retv
) = VT_BOOL
;
2095 /* ECMA-262 3rd Edition 11.8.7 */
2096 HRESULT
in_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2098 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2102 return binary_expr_eval(ctx
, expr
, in_eval
, ei
, ret
);
2105 /* ECMA-262 3rd Edition 11.6.1 */
2106 static HRESULT
add_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2111 hres
= to_primitive(ctx
, lval
, ei
, &l
, NO_HINT
);
2115 hres
= to_primitive(ctx
, rval
, ei
, &r
, NO_HINT
);
2121 if(V_VT(&l
) == VT_BSTR
|| V_VT(&r
) == VT_BSTR
) {
2122 BSTR lstr
= NULL
, rstr
= NULL
;
2124 if(V_VT(&l
) == VT_BSTR
)
2127 hres
= to_string(ctx
, &l
, ei
, &lstr
);
2129 if(SUCCEEDED(hres
)) {
2130 if(V_VT(&r
) == VT_BSTR
)
2133 hres
= to_string(ctx
, &r
, ei
, &rstr
);
2136 if(SUCCEEDED(hres
)) {
2139 len1
= SysStringLen(lstr
);
2140 len2
= SysStringLen(rstr
);
2142 V_VT(retv
) = VT_BSTR
;
2143 V_BSTR(retv
) = SysAllocStringLen(NULL
, len1
+len2
);
2144 memcpy(V_BSTR(retv
), lstr
, len1
*sizeof(WCHAR
));
2145 memcpy(V_BSTR(retv
)+len1
, rstr
, (len2
+1)*sizeof(WCHAR
));
2148 if(V_VT(&l
) != VT_BSTR
)
2149 SysFreeString(lstr
);
2150 if(V_VT(&r
) != VT_BSTR
)
2151 SysFreeString(rstr
);
2155 hres
= to_number(ctx
, &l
, ei
, &nl
);
2156 if(SUCCEEDED(hres
)) {
2157 hres
= to_number(ctx
, &r
, ei
, &nr
);
2159 num_set_val(retv
, num_val(&nl
) + num_val(&nr
));
2168 /* ECMA-262 3rd Edition 11.6.1 */
2169 HRESULT
add_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2171 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2175 return binary_expr_eval(ctx
, expr
, add_eval
, ei
, ret
);
2178 /* ECMA-262 3rd Edition 11.6.2 */
2179 static HRESULT
sub_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2184 hres
= to_number(ctx
, lval
, ei
, &lnum
);
2188 hres
= to_number(ctx
, rval
, ei
, &rnum
);
2192 num_set_val(retv
, num_val(&lnum
) - num_val(&rnum
));
2196 /* ECMA-262 3rd Edition 11.6.2 */
2197 HRESULT
sub_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2199 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2203 return binary_expr_eval(ctx
, expr
, sub_eval
, ei
, ret
);
2206 /* ECMA-262 3rd Edition 11.5.1 */
2207 static HRESULT
mul_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2212 hres
= to_number(ctx
, lval
, ei
, &lnum
);
2216 hres
= to_number(ctx
, rval
, ei
, &rnum
);
2220 num_set_val(retv
, num_val(&lnum
) * num_val(&rnum
));
2224 /* ECMA-262 3rd Edition 11.5.1 */
2225 HRESULT
mul_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2227 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2231 return binary_expr_eval(ctx
, expr
, mul_eval
, ei
, ret
);
2234 /* ECMA-262 3rd Edition 11.5.2 */
2235 static HRESULT
div_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2240 hres
= to_number(ctx
, lval
, ei
, &lnum
);
2244 hres
= to_number(ctx
, rval
, ei
, &rnum
);
2248 num_set_val(retv
, num_val(&lnum
) / num_val(&rnum
));
2252 /* ECMA-262 3rd Edition 11.5.2 */
2253 HRESULT
div_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2255 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2259 return binary_expr_eval(ctx
, expr
, div_eval
, ei
, ret
);
2262 /* ECMA-262 3rd Edition 11.5.3 */
2263 static HRESULT
mod_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2268 hres
= to_number(ctx
, lval
, ei
, &lnum
);
2272 hres
= to_number(ctx
, rval
, ei
, &rnum
);
2276 num_set_val(retv
, fmod(num_val(&lnum
), num_val(&rnum
)));
2280 /* ECMA-262 3rd Edition 11.5.3 */
2281 HRESULT
mod_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2283 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2287 return binary_expr_eval(ctx
, expr
, mod_eval
, ei
, ret
);
2290 /* ECMA-262 3rd Edition 11.4.2 */
2291 HRESULT
delete_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2293 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2294 VARIANT_BOOL b
= VARIANT_FALSE
;
2300 hres
= expr_eval(ctx
, expr
->expression
, EXPR_STRREF
, ei
, &exprval
);
2304 switch(exprval
.type
) {
2305 case EXPRVAL_IDREF
: {
2306 IDispatchEx
*dispex
;
2308 hres
= IDispatch_QueryInterface(exprval
.u
.nameref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2309 if(SUCCEEDED(hres
)) {
2310 hres
= IDispatchEx_DeleteMemberByDispID(dispex
, exprval
.u
.idref
.id
);
2312 IDispatchEx_Release(dispex
);
2316 case EXPRVAL_NAMEREF
: {
2317 IDispatchEx
*dispex
;
2319 hres
= IDispatch_QueryInterface(exprval
.u
.nameref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2320 if(SUCCEEDED(hres
)) {
2321 hres
= IDispatchEx_DeleteMemberByName(dispex
, exprval
.u
.nameref
.name
,
2322 make_grfdex(ctx
, fdexNameCaseSensitive
));
2324 IDispatchEx_Release(dispex
);
2329 FIXME("unsupported type %d\n", exprval
.type
);
2333 exprval_release(&exprval
);
2337 return return_bool(ret
, b
);
2340 /* ECMA-262 3rd Edition 11.4.2 */
2341 HRESULT
void_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2343 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2350 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2354 hres
= exprval_to_value(ctx
, &exprval
, ei
, &tmp
);
2355 exprval_release(&exprval
);
2361 ret
->type
= EXPRVAL_VARIANT
;
2362 V_VT(&ret
->u
.var
) = VT_EMPTY
;
2366 /* ECMA-262 3rd Edition 11.4.3 */
2367 static HRESULT
typeof_exprval(script_ctx_t
*ctx
, exprval_t
*exprval
, jsexcept_t
*ei
, const WCHAR
**ret
)
2372 static const WCHAR booleanW
[] = {'b','o','o','l','e','a','n',0};
2373 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
2374 static const WCHAR numberW
[] = {'n','u','m','b','e','r',0};
2375 static const WCHAR objectW
[] = {'o','b','j','e','c','t',0};
2376 static const WCHAR stringW
[] = {'s','t','r','i','n','g',0};
2377 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
2378 static const WCHAR unknownW
[] = {'u','n','k','n','o','w','n',0};
2380 if(exprval
->type
== EXPRVAL_INVALID
) {
2385 hres
= exprval_to_value(ctx
, exprval
, ei
, &val
);
2387 if(exprval
->type
== EXPRVAL_IDREF
) {
2394 switch(V_VT(&val
)) {
2414 if(V_DISPATCH(&val
) && (dispex
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(&val
)))) {
2415 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
2416 jsdisp_release(dispex
);
2423 FIXME("unhandled vt %d\n", V_VT(&val
));
2431 HRESULT
typeof_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2433 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2434 const WCHAR
*str
= NULL
;
2440 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2444 hres
= typeof_exprval(ctx
, &exprval
, ei
, &str
);
2445 exprval_release(&exprval
);
2449 ret
->type
= EXPRVAL_VARIANT
;
2450 V_VT(&ret
->u
.var
) = VT_BSTR
;
2451 V_BSTR(&ret
->u
.var
) = SysAllocString(str
);
2452 if(!V_BSTR(&ret
->u
.var
))
2453 return E_OUTOFMEMORY
;
2458 /* ECMA-262 3rd Edition 11.4.7 */
2459 HRESULT
minus_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2461 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2468 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2472 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
2473 exprval_release(&exprval
);
2477 hres
= to_number(ctx
, &val
, ei
, &num
);
2482 ret
->type
= EXPRVAL_VARIANT
;
2483 num_set_val(&ret
->u
.var
, -num_val(&num
));
2487 /* ECMA-262 3rd Edition 11.4.6 */
2488 HRESULT
plus_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2490 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2497 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2501 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
2502 exprval_release(&exprval
);
2506 hres
= to_number(ctx
, &val
, ei
, &num
);
2511 ret
->type
= EXPRVAL_VARIANT
;
2516 /* ECMA-262 3rd Edition 11.3.1 */
2517 HRESULT
post_increment_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2519 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2526 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2530 hres
= exprval_value(ctx
, &exprval
, ei
, &val
);
2531 if(SUCCEEDED(hres
)) {
2532 hres
= to_number(ctx
, &val
, ei
, &num
);
2536 if(SUCCEEDED(hres
)) {
2538 num_set_val(&inc
, num_val(&num
)+1.0);
2539 hres
= put_value(ctx
, &exprval
, &inc
, ei
);
2542 exprval_release(&exprval
);
2546 ret
->type
= EXPRVAL_VARIANT
;
2551 /* ECMA-262 3rd Edition 11.3.2 */
2552 HRESULT
post_decrement_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2554 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2561 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2565 hres
= exprval_value(ctx
, &exprval
, ei
, &val
);
2566 if(SUCCEEDED(hres
)) {
2567 hres
= to_number(ctx
, &val
, ei
, &num
);
2571 if(SUCCEEDED(hres
)) {
2573 num_set_val(&dec
, num_val(&num
)-1.0);
2574 hres
= put_value(ctx
, &exprval
, &dec
, ei
);
2577 exprval_release(&exprval
);
2581 ret
->type
= EXPRVAL_VARIANT
;
2586 /* ECMA-262 3rd Edition 11.4.4 */
2587 HRESULT
pre_increment_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2589 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2596 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2600 hres
= exprval_value(ctx
, &exprval
, ei
, &val
);
2601 if(SUCCEEDED(hres
)) {
2602 hres
= to_number(ctx
, &val
, ei
, &num
);
2606 if(SUCCEEDED(hres
)) {
2607 num_set_val(&val
, num_val(&num
)+1.0);
2608 hres
= put_value(ctx
, &exprval
, &val
, ei
);
2611 exprval_release(&exprval
);
2615 ret
->type
= EXPRVAL_VARIANT
;
2620 /* ECMA-262 3rd Edition 11.4.5 */
2621 HRESULT
pre_decrement_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2623 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2630 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2634 hres
= exprval_value(ctx
, &exprval
, ei
, &val
);
2635 if(SUCCEEDED(hres
)) {
2636 hres
= to_number(ctx
, &val
, ei
, &num
);
2640 if(SUCCEEDED(hres
)) {
2641 num_set_val(&val
, num_val(&num
)-1.0);
2642 hres
= put_value(ctx
, &exprval
, &val
, ei
);
2645 exprval_release(&exprval
);
2649 ret
->type
= EXPRVAL_VARIANT
;
2654 /* ECMA-262 3rd Edition 11.9.3 */
2655 static HRESULT
equal_values(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, BOOL
*ret
)
2657 if(V_VT(lval
) == V_VT(rval
) || (is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
))))
2658 return equal2_values(lval
, rval
, ret
);
2660 /* FIXME: NULL disps should be handled in more general way */
2661 if(V_VT(lval
) == VT_DISPATCH
&& !V_DISPATCH(lval
)) {
2664 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2667 if(V_VT(rval
) == VT_DISPATCH
&& !V_DISPATCH(rval
)) {
2670 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2673 if((V_VT(lval
) == VT_NULL
&& V_VT(rval
) == VT_EMPTY
) ||
2674 (V_VT(lval
) == VT_EMPTY
&& V_VT(rval
) == VT_NULL
)) {
2679 if(V_VT(lval
) == VT_BSTR
&& is_num_vt(V_VT(rval
))) {
2683 hres
= to_number(ctx
, lval
, ei
, &v
);
2687 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2690 if(V_VT(rval
) == VT_BSTR
&& is_num_vt(V_VT(lval
))) {
2694 hres
= to_number(ctx
, rval
, ei
, &v
);
2698 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2701 if(V_VT(rval
) == VT_BOOL
) {
2705 V_I4(&v
) = V_BOOL(rval
) ? 1 : 0;
2706 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2709 if(V_VT(lval
) == VT_BOOL
) {
2713 V_I4(&v
) = V_BOOL(lval
) ? 1 : 0;
2714 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2718 if(V_VT(rval
) == VT_DISPATCH
&& (V_VT(lval
) == VT_BSTR
|| is_num_vt(V_VT(lval
)))) {
2722 hres
= to_primitive(ctx
, rval
, ei
, &v
, NO_HINT
);
2726 hres
= equal_values(ctx
, lval
, &v
, ei
, ret
);
2733 if(V_VT(lval
) == VT_DISPATCH
&& (V_VT(rval
) == VT_BSTR
|| is_num_vt(V_VT(rval
)))) {
2737 hres
= to_primitive(ctx
, lval
, ei
, &v
, NO_HINT
);
2741 hres
= equal_values(ctx
, &v
, rval
, ei
, ret
);
2752 /* ECMA-262 3rd Edition 11.9.1 */
2753 HRESULT
equal_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2755 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2762 hres
= get_binary_expr_values(ctx
, expr
, ei
, &rval
, &lval
);
2766 hres
= equal_values(ctx
, &rval
, &lval
, ei
, &b
);
2767 VariantClear(&lval
);
2768 VariantClear(&rval
);
2772 return return_bool(ret
, b
);
2775 /* ECMA-262 3rd Edition 11.9.4 */
2776 HRESULT
equal2_expression_eval(script_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
, &rval
, &lval
);
2789 hres
= equal2_values(&rval
, &lval
, &b
);
2790 VariantClear(&lval
);
2791 VariantClear(&rval
);
2795 return return_bool(ret
, b
);
2798 /* ECMA-262 3rd Edition 11.9.2 */
2799 HRESULT
not_equal_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2801 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2808 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2812 hres
= equal_values(ctx
, &lval
, &rval
, ei
, &b
);
2813 VariantClear(&lval
);
2814 VariantClear(&rval
);
2818 return return_bool(ret
, !b
);
2821 /* ECMA-262 3rd Edition 11.9.5 */
2822 HRESULT
not_equal2_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2824 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2831 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2835 hres
= equal2_values(&lval
, &rval
, &b
);
2836 VariantClear(&lval
);
2837 VariantClear(&rval
);
2841 return return_bool(ret
, !b
);
2844 /* ECMA-262 3rd Edition 11.8.5 */
2845 static HRESULT
less_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, BOOL greater
, jsexcept_t
*ei
, BOOL
*ret
)
2847 VARIANT l
, r
, ln
, rn
;
2850 hres
= to_primitive(ctx
, lval
, ei
, &l
, NO_HINT
);
2854 hres
= to_primitive(ctx
, rval
, ei
, &r
, NO_HINT
);
2860 if(V_VT(&l
) == VT_BSTR
&& V_VT(&r
) == VT_BSTR
) {
2861 *ret
= (strcmpW(V_BSTR(&l
), V_BSTR(&r
)) < 0) ^ greater
;
2862 SysFreeString(V_BSTR(&l
));
2863 SysFreeString(V_BSTR(&r
));
2867 hres
= to_number(ctx
, &l
, ei
, &ln
);
2870 hres
= to_number(ctx
, &r
, ei
, &rn
);
2875 if(V_VT(&ln
) == VT_I4
&& V_VT(&rn
) == VT_I4
) {
2876 *ret
= (V_I4(&ln
) < V_I4(&rn
)) ^ greater
;
2878 DOUBLE ld
= num_val(&ln
);
2879 DOUBLE rd
= num_val(&rn
);
2881 *ret
= !isnan(ld
) && !isnan(rd
) && ((ld
< rd
) ^ greater
);
2887 /* ECMA-262 3rd Edition 11.8.1 */
2888 HRESULT
less_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2890 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2897 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2901 hres
= less_eval(ctx
, &lval
, &rval
, FALSE
, ei
, &b
);
2902 VariantClear(&lval
);
2903 VariantClear(&rval
);
2907 return return_bool(ret
, b
);
2910 /* ECMA-262 3rd Edition 11.8.3 */
2911 HRESULT
lesseq_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2913 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2920 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2924 hres
= less_eval(ctx
, &rval
, &lval
, TRUE
, ei
, &b
);
2925 VariantClear(&lval
);
2926 VariantClear(&rval
);
2930 return return_bool(ret
, b
);
2933 /* ECMA-262 3rd Edition 11.8.2 */
2934 HRESULT
greater_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2936 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2943 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2947 hres
= less_eval(ctx
, &rval
, &lval
, FALSE
, ei
, &b
);
2948 VariantClear(&lval
);
2949 VariantClear(&rval
);
2953 return return_bool(ret
, b
);
2956 /* ECMA-262 3rd Edition 11.8.4 */
2957 HRESULT
greatereq_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2959 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2966 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2970 hres
= less_eval(ctx
, &lval
, &rval
, TRUE
, ei
, &b
);
2971 VariantClear(&lval
);
2972 VariantClear(&rval
);
2976 return return_bool(ret
, b
);
2979 /* ECMA-262 3rd Edition 11.4.8 */
2980 HRESULT
binary_negation_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2982 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2990 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2994 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
2995 exprval_release(&exprval
);
2999 hres
= to_int32(ctx
, &val
, ei
, &i
);
3003 ret
->type
= EXPRVAL_VARIANT
;
3004 V_VT(&ret
->u
.var
) = VT_I4
;
3005 V_I4(&ret
->u
.var
) = ~i
;
3009 /* ECMA-262 3rd Edition 11.4.9 */
3010 HRESULT
logical_negation_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3012 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
3019 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
3023 hres
= exprval_to_boolean(ctx
, &exprval
, ei
, &b
);
3024 exprval_release(&exprval
);
3028 return return_bool(ret
, !b
);
3031 /* ECMA-262 3rd Edition 11.7.1 */
3032 static HRESULT
lshift_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3038 hres
= to_int32(ctx
, lval
, ei
, &li
);
3042 hres
= to_uint32(ctx
, rval
, ei
, &ri
);
3047 V_I4(retv
) = li
<< (ri
&0x1f);
3051 /* ECMA-262 3rd Edition 11.7.1 */
3052 HRESULT
left_shift_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3054 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3058 return binary_expr_eval(ctx
, expr
, lshift_eval
, ei
, ret
);
3061 /* ECMA-262 3rd Edition 11.7.2 */
3062 static HRESULT
rshift_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3068 hres
= to_int32(ctx
, lval
, ei
, &li
);
3072 hres
= to_uint32(ctx
, rval
, ei
, &ri
);
3077 V_I4(retv
) = li
>> (ri
&0x1f);
3081 /* ECMA-262 3rd Edition 11.7.2 */
3082 HRESULT
right_shift_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3084 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3088 return binary_expr_eval(ctx
, expr
, rshift_eval
, ei
, ret
);
3091 /* ECMA-262 3rd Edition 11.7.3 */
3092 static HRESULT
rshift2_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3097 hres
= to_uint32(ctx
, lval
, ei
, &li
);
3101 hres
= to_uint32(ctx
, rval
, ei
, &ri
);
3106 V_I4(retv
) = li
>> (ri
&0x1f);
3110 /* ECMA-262 3rd Edition 11.7.3 */
3111 HRESULT
right2_shift_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3113 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3117 return binary_expr_eval(ctx
, expr
, rshift2_eval
, ei
, ret
);
3120 /* ECMA-262 3rd Edition 11.13.1 */
3121 HRESULT
assign_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3123 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3124 exprval_t exprval
, exprvalr
;
3130 hres
= expr_eval(ctx
, expr
->expression1
, EXPR_NEWREF
, ei
, &exprval
);
3134 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprvalr
);
3135 if(SUCCEEDED(hres
)) {
3136 hres
= exprval_to_value(ctx
, &exprvalr
, ei
, &rval
);
3137 exprval_release(&exprvalr
);
3140 if(SUCCEEDED(hres
)) {
3141 hres
= put_value(ctx
, &exprval
, &rval
, ei
);
3143 VariantClear(&rval
);
3146 exprval_release(&exprval
);
3150 ret
->type
= EXPRVAL_VARIANT
;
3155 /* ECMA-262 3rd Edition 11.13.2 */
3156 HRESULT
assign_lshift_expression_eval(script_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
, lshift_eval
, ei
, ret
);
3165 /* ECMA-262 3rd Edition 11.13.2 */
3166 HRESULT
assign_rshift_expression_eval(script_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
, rshift_eval
, ei
, ret
);
3175 /* ECMA-262 3rd Edition 11.13.2 */
3176 HRESULT
assign_rrshift_expression_eval(script_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
, rshift2_eval
, ei
, ret
);
3185 /* ECMA-262 3rd Edition 11.13.2 */
3186 HRESULT
assign_add_expression_eval(script_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
, add_eval
, ei
, ret
);
3195 /* ECMA-262 3rd Edition 11.13.2 */
3196 HRESULT
assign_sub_expression_eval(script_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
, sub_eval
, ei
, ret
);
3205 /* ECMA-262 3rd Edition 11.13.2 */
3206 HRESULT
assign_mul_expression_eval(script_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
, mul_eval
, ei
, ret
);
3215 /* ECMA-262 3rd Edition 11.13.2 */
3216 HRESULT
assign_div_expression_eval(script_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
, div_eval
, ei
, ret
);
3225 /* ECMA-262 3rd Edition 11.13.2 */
3226 HRESULT
assign_mod_expression_eval(script_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
, mod_eval
, ei
, ret
);
3235 /* ECMA-262 3rd Edition 11.13.2 */
3236 HRESULT
assign_and_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3238 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3242 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, bitand_eval
, ei
, ret
);
3245 /* ECMA-262 3rd Edition 11.13.2 */
3246 HRESULT
assign_or_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3248 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3252 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, bitor_eval
, ei
, ret
);
3255 /* ECMA-262 3rd Edition 11.13.2 */
3256 HRESULT
assign_xor_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3258 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3262 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, xor_eval
, ei
, ret
);