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(val
->u
.idref
.disp
, val
->u
.idref
.id
, ctx
->lcid
, 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 IDispatchEx_Release(_IDispatchEx_(scope
->obj
));
181 HRESULT
create_exec_ctx(IDispatch
*this_obj
, DispatchEx
*var_disp
, scope_chain_t
*scope
, exec_ctx_t
**ret
)
185 ctx
= heap_alloc_zero(sizeof(exec_ctx_t
));
187 return E_OUTOFMEMORY
;
191 IDispatch_AddRef(this_obj
);
192 ctx
->this_obj
= this_obj
;
194 IDispatchEx_AddRef(_IDispatchEx_(var_disp
));
195 ctx
->var_disp
= var_disp
;
199 ctx
->scope_chain
= scope
;
206 void exec_release(exec_ctx_t
*ctx
)
212 scope_release(ctx
->scope_chain
);
214 IDispatchEx_Release(_IDispatchEx_(ctx
->var_disp
));
216 IDispatch_Release(ctx
->this_obj
);
220 static HRESULT
disp_get_id(IDispatch
*disp
, BSTR name
, DWORD flags
, DISPID
*id
)
225 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
227 TRACE("unsing IDispatch\n");
230 return IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &name
, 1, 0, id
);
234 hres
= IDispatchEx_GetDispID(dispex
, name
, flags
|fdexNameCaseSensitive
, id
);
235 IDispatchEx_Release(dispex
);
239 /* ECMA-262 3rd Edition 8.7.2 */
240 static HRESULT
put_value(script_ctx_t
*ctx
, exprval_t
*ref
, VARIANT
*v
, jsexcept_t
*ei
)
242 if(ref
->type
!= EXPRVAL_IDREF
)
243 return throw_reference_error(ctx
, ei
, IDS_ILLEGAL_ASSIGN
, NULL
);
245 return disp_propput(ref
->u
.idref
.disp
, ref
->u
.idref
.id
, ctx
->lcid
, v
, ei
, NULL
/*FIXME*/);
248 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
250 IObjectIdentity
*identity
;
251 IUnknown
*unk1
, *unk2
;
259 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
263 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
265 IUnknown_Release(unk1
);
272 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
273 if(SUCCEEDED(hres
)) {
274 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
275 IObjectIdentity_Release(identity
);
282 IUnknown_Release(unk1
);
283 IUnknown_Release(unk2
);
287 /* ECMA-262 3rd Edition 11.9.6 */
288 static HRESULT
equal2_values(VARIANT
*lval
, VARIANT
*rval
, BOOL
*ret
)
292 if(V_VT(lval
) != V_VT(rval
)) {
293 if(is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
))) {
294 *ret
= num_val(lval
) == num_val(rval
);
308 *ret
= V_I4(lval
) == V_I4(rval
);
311 *ret
= V_R8(lval
) == V_R8(rval
);
315 *ret
= SysStringLen(V_BSTR(rval
))?FALSE
:TRUE
;
316 else if(!V_BSTR(rval
))
317 *ret
= SysStringLen(V_BSTR(lval
))?FALSE
:TRUE
;
319 *ret
= !strcmpW(V_BSTR(lval
), V_BSTR(rval
));
322 return disp_cmp(V_DISPATCH(lval
), V_DISPATCH(rval
), ret
);
324 *ret
= !V_BOOL(lval
) == !V_BOOL(rval
);
327 FIXME("unimplemented vt %d\n", V_VT(lval
));
334 static HRESULT
literal_to_var(literal_t
*literal
, VARIANT
*v
)
336 V_VT(v
) = literal
->vt
;
343 V_I4(v
) = literal
->u
.lval
;
346 V_R8(v
) = literal
->u
.dval
;
349 V_BSTR(v
) = SysAllocString(literal
->u
.wstr
);
352 V_BOOL(v
) = literal
->u
.bval
;
355 IDispatch_AddRef(literal
->u
.disp
);
356 V_DISPATCH(v
) = literal
->u
.disp
;
359 ERR("wrong type %d\n", V_VT(v
));
366 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
372 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
373 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
374 hres
= disp_get_id(item
->disp
, identifier
, 0, &id
);
375 if(SUCCEEDED(hres
)) {
377 exprval_set_idref(ret
, item
->disp
, id
);
386 HRESULT
exec_source(exec_ctx_t
*ctx
, parser_ctx_t
*parser
, source_elements_t
*source
, jsexcept_t
*ei
, VARIANT
*retv
)
388 script_ctx_t
*script
= parser
->script
;
389 function_declaration_t
*func
;
390 parser_ctx_t
*prev_parser
;
394 exec_ctx_t
*prev_ctx
;
398 for(func
= source
->functions
; func
; func
= func
->next
) {
399 DispatchEx
*func_obj
;
402 hres
= create_source_function(parser
, func
->expr
->parameter_list
, func
->expr
->source_elements
,
403 ctx
->scope_chain
, func
->expr
->src_str
, func
->expr
->src_len
, &func_obj
);
407 V_VT(&var
) = VT_DISPATCH
;
408 V_DISPATCH(&var
) = (IDispatch
*)_IDispatchEx_(func_obj
);
409 hres
= jsdisp_propput_name(ctx
->var_disp
, func
->expr
->identifier
, script
->lcid
, &var
, ei
, NULL
);
410 jsdisp_release(func_obj
);
415 for(var
= source
->variables
; var
; var
= var
->next
) {
419 name
= SysAllocString(var
->identifier
);
421 return E_OUTOFMEMORY
;
423 if(!lookup_global_members(parser
->script
, name
, NULL
))
424 hres
= jsdisp_get_id(ctx
->var_disp
, var
->identifier
, fdexNameEnsure
, &id
);
430 prev_ctx
= script
->exec_ctx
;
431 script
->exec_ctx
= ctx
;
433 prev_parser
= ctx
->parser
;
434 ctx
->parser
= parser
;
436 V_VT(&val
) = VT_EMPTY
;
437 memset(&rt
, 0, sizeof(rt
));
440 for(stat
= source
->statement
; stat
; stat
= stat
->next
) {
441 hres
= stat_eval(ctx
, stat
, &rt
, &tmp
);
447 if(rt
.type
!= RT_NORMAL
)
451 script
->exec_ctx
= prev_ctx
;
452 ctx
->parser
= prev_parser
;
454 if(rt
.type
!= RT_NORMAL
&& rt
.type
!= RT_RETURN
) {
455 FIXME("wrong rt %d\n", rt
.type
);
472 /* ECMA-262 3rd Edition 10.1.4 */
473 static HRESULT
identifier_eval(exec_ctx_t
*ctx
, BSTR identifier
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
475 scope_chain_t
*scope
;
480 TRACE("%s\n", debugstr_w(identifier
));
482 for(scope
= ctx
->scope_chain
; scope
; scope
= scope
->next
) {
483 hres
= jsdisp_get_id(scope
->obj
, identifier
, 0, &id
);
489 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(scope
->obj
), id
);
493 hres
= jsdisp_get_id(ctx
->parser
->script
->global
, identifier
, 0, &id
);
494 if(SUCCEEDED(hres
)) {
495 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(ctx
->parser
->script
->global
), id
);
499 for(item
= ctx
->parser
->script
->named_items
; item
; item
= item
->next
) {
500 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
504 if(!ctx
->parser
->script
->site
)
507 hres
= IActiveScriptSite_GetItemInfo(ctx
->parser
->script
->site
, identifier
,
508 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
510 WARN("GetItemInfo failed: %08x\n", hres
);
514 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
515 IUnknown_Release(unk
);
517 WARN("object does not implement IDispatch\n");
522 ret
->type
= EXPRVAL_VARIANT
;
523 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
524 V_DISPATCH(&ret
->u
.var
) = item
->disp
;
525 IDispatch_AddRef(item
->disp
);
530 hres
= jsdisp_get_id(ctx
->parser
->script
->script_disp
, identifier
, 0, &id
);
531 if(SUCCEEDED(hres
)) {
532 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(ctx
->parser
->script
->script_disp
), id
);
536 if(lookup_global_members(ctx
->parser
->script
, identifier
, ret
))
539 if(flags
& EXPR_NEWREF
) {
540 hres
= jsdisp_get_id(ctx
->parser
->script
->script_disp
, identifier
, fdexNameEnsure
, &id
);
544 exprval_set_idref(ret
, (IDispatch
*)_IDispatchEx_(ctx
->parser
->script
->script_disp
), id
);
548 ret
->type
= EXPRVAL_INVALID
;
549 ret
->u
.identifier
= SysAllocString(identifier
);
550 if(!ret
->u
.identifier
)
551 return E_OUTOFMEMORY
;
556 /* ECMA-262 3rd Edition 12.1 */
557 HRESULT
block_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
559 block_statement_t
*stat
= (block_statement_t
*)_stat
;
566 V_VT(&val
) = VT_EMPTY
;
567 for(iter
= stat
->stat_list
; iter
; iter
= iter
->next
) {
568 hres
= stat_eval(ctx
, iter
, rt
, &tmp
);
574 if(rt
->type
!= RT_NORMAL
)
587 /* ECMA-262 3rd Edition 12.2 */
588 static HRESULT
variable_list_eval(exec_ctx_t
*ctx
, variable_declaration_t
*var_list
, jsexcept_t
*ei
)
590 variable_declaration_t
*iter
;
593 for(iter
= var_list
; iter
; iter
= iter
->next
) {
600 hres
= expr_eval(ctx
, iter
->expr
, 0, ei
, &exprval
);
604 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
605 exprval_release(&exprval
);
609 hres
= jsdisp_propput_name(ctx
->var_disp
, iter
->identifier
, ctx
->parser
->script
->lcid
, &val
, ei
, NULL
/*FIXME*/);
618 /* ECMA-262 3rd Edition 12.2 */
619 HRESULT
var_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
621 var_statement_t
*stat
= (var_statement_t
*)_stat
;
626 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
630 V_VT(ret
) = VT_EMPTY
;
634 /* ECMA-262 3rd Edition 12.3 */
635 HRESULT
empty_statement_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
639 V_VT(ret
) = VT_EMPTY
;
643 /* ECMA-262 3rd Edition 12.4 */
644 HRESULT
expression_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
646 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
653 hres
= expr_eval(ctx
, stat
->expr
, EXPR_NOVAL
, &rt
->ei
, &exprval
);
657 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
658 exprval_release(&exprval
);
663 TRACE("= %s\n", debugstr_variant(ret
));
667 /* ECMA-262 3rd Edition 12.5 */
668 HRESULT
if_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
670 if_statement_t
*stat
= (if_statement_t
*)_stat
;
677 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
681 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, &rt
->ei
, &b
);
682 exprval_release(&exprval
);
687 hres
= stat_eval(ctx
, stat
->if_stat
, rt
, ret
);
688 else if(stat
->else_stat
)
689 hres
= stat_eval(ctx
, stat
->else_stat
, rt
, ret
);
691 V_VT(ret
) = VT_EMPTY
;
696 /* ECMA-262 3rd Edition 12.6.2 */
697 HRESULT
while_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
699 while_statement_t
*stat
= (while_statement_t
*)_stat
;
708 V_VT(&val
) = VT_EMPTY
;
709 test_expr
= !stat
->do_while
;
713 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
717 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, &rt
->ei
, &b
);
718 exprval_release(&exprval
);
719 if(FAILED(hres
) || !b
)
725 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
732 if(rt
->type
== RT_CONTINUE
)
733 rt
->type
= RT_NORMAL
;
734 if(rt
->type
!= RT_NORMAL
)
743 if(rt
->type
== RT_BREAK
)
744 rt
->type
= RT_NORMAL
;
750 /* ECMA-262 3rd Edition 12.6.3 */
751 HRESULT
for_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
753 for_statement_t
*stat
= (for_statement_t
*)_stat
;
754 VARIANT val
, tmp
, retv
;
761 if(stat
->variable_list
) {
762 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
765 }else if(stat
->begin_expr
) {
766 hres
= expr_eval(ctx
, stat
->begin_expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
770 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
771 exprval_release(&exprval
);
778 V_VT(&retv
) = VT_EMPTY
;
782 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
786 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, &rt
->ei
, &b
);
787 exprval_release(&exprval
);
788 if(FAILED(hres
) || !b
)
792 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
799 if(rt
->type
== RT_CONTINUE
)
800 rt
->type
= RT_NORMAL
;
801 else if(rt
->type
!= RT_NORMAL
)
805 hres
= expr_eval(ctx
, stat
->end_expr
, 0, &rt
->ei
, &exprval
);
809 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
810 exprval_release(&exprval
);
823 if(rt
->type
== RT_BREAK
)
824 rt
->type
= RT_NORMAL
;
830 /* ECMA-262 3rd Edition 12.6.4 */
831 HRESULT
forin_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
833 forin_statement_t
*stat
= (forin_statement_t
*)_stat
;
834 VARIANT val
, name
, retv
, tmp
;
835 DISPID id
= DISPID_STARTENUM
;
836 BSTR str
, identifier
= NULL
;
844 hres
= variable_list_eval(ctx
, stat
->variable
, &rt
->ei
);
849 hres
= expr_eval(ctx
, stat
->in_expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
853 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
854 exprval_release(&exprval
);
858 if(V_VT(&val
) != VT_DISPATCH
) {
859 TRACE("in vt %d\n", V_VT(&val
));
861 V_VT(ret
) = VT_EMPTY
;
865 hres
= IDispatch_QueryInterface(V_DISPATCH(&val
), &IID_IDispatchEx
, (void**)&in_obj
);
866 IDispatch_Release(V_DISPATCH(&val
));
868 FIXME("Object doesn't support IDispatchEx\n");
872 V_VT(&retv
) = VT_EMPTY
;
875 identifier
= SysAllocString(stat
->variable
->identifier
);
878 hres
= IDispatchEx_GetNextDispID(in_obj
, fdexEnumDefault
, id
, &id
);
879 if(FAILED(hres
) || hres
== S_FALSE
)
882 hres
= IDispatchEx_GetMemberName(in_obj
, id
, &str
);
886 TRACE("iter %s\n", debugstr_w(str
));
889 hres
= identifier_eval(ctx
, identifier
, 0, NULL
, &exprval
);
891 hres
= expr_eval(ctx
, stat
->expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
892 if(SUCCEEDED(hres
)) {
893 V_VT(&name
) = VT_BSTR
;
895 hres
= put_value(ctx
->parser
->script
, &exprval
, &name
, &rt
->ei
);
896 exprval_release(&exprval
);
902 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
909 if(rt
->type
== RT_CONTINUE
)
910 rt
->type
= RT_NORMAL
;
911 else if(rt
->type
!= RT_NORMAL
)
915 SysFreeString(identifier
);
916 IDispatchEx_Release(in_obj
);
922 if(rt
->type
== RT_BREAK
)
923 rt
->type
= RT_NORMAL
;
929 /* ECMA-262 3rd Edition 12.7 */
930 HRESULT
continue_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
932 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
936 if(stat
->identifier
) {
937 FIXME("indentifier not implemented\n");
941 rt
->type
= RT_CONTINUE
;
942 V_VT(ret
) = VT_EMPTY
;
946 /* ECMA-262 3rd Edition 12.8 */
947 HRESULT
break_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
949 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
953 if(stat
->identifier
) {
954 FIXME("indentifier not implemented\n");
959 V_VT(ret
) = VT_EMPTY
;
963 /* ECMA-262 3rd Edition 12.9 */
964 HRESULT
return_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
966 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
974 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
978 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, ret
);
979 exprval_release(&exprval
);
983 V_VT(ret
) = VT_EMPTY
;
986 TRACE("= %s\n", debugstr_variant(ret
));
987 rt
->type
= RT_RETURN
;
991 /* ECMA-262 3rd Edition 12.10 */
992 HRESULT
with_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
994 with_statement_t
*stat
= (with_statement_t
*)_stat
;
1003 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1007 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
1008 exprval_release(&exprval
);
1012 hres
= to_object(ctx
->parser
->script
, &val
, &disp
);
1017 obj
= iface_to_jsdisp((IUnknown
*)disp
);
1018 IDispatch_Release(disp
);
1020 FIXME("disp id not jsdisp\n");
1024 hres
= scope_push(ctx
->scope_chain
, obj
, &ctx
->scope_chain
);
1025 jsdisp_release(obj
);
1029 hres
= stat_eval(ctx
, stat
->statement
, rt
, ret
);
1031 scope_pop(&ctx
->scope_chain
);
1035 /* ECMA-262 3rd Edition 12.12 */
1036 HRESULT
labelled_statement_eval(exec_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
1042 /* ECMA-262 3rd Edition 12.13 */
1043 HRESULT
switch_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1045 switch_statement_t
*stat
= (switch_statement_t
*)_stat
;
1046 case_clausule_t
*iter
, *default_clausule
= NULL
;
1047 statement_t
*stat_iter
;
1055 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1059 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
1060 exprval_release(&exprval
);
1064 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1066 default_clausule
= iter
;
1070 hres
= expr_eval(ctx
, iter
->expr
, 0, &rt
->ei
, &exprval
);
1074 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &cval
);
1075 exprval_release(&exprval
);
1079 hres
= equal2_values(&val
, &cval
, &b
);
1080 VariantClear(&cval
);
1081 if(FAILED(hres
) || b
)
1090 iter
= default_clausule
;
1092 V_VT(&val
) = VT_EMPTY
;
1096 for(stat_iter
= iter
->stat
; stat_iter
; stat_iter
= stat_iter
->next
) {
1097 hres
= stat_eval(ctx
, stat_iter
, rt
, &tmp
);
1104 if(rt
->type
!= RT_NORMAL
)
1114 if(rt
->type
== RT_BREAK
)
1115 rt
->type
= RT_NORMAL
;
1121 /* ECMA-262 3rd Edition 12.13 */
1122 HRESULT
throw_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1124 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
1131 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1135 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &rt
->ei
, &val
);
1136 exprval_release(&exprval
);
1141 return DISP_E_EXCEPTION
;
1144 /* ECMA-262 3rd Edition 12.14 */
1145 static HRESULT
catch_eval(exec_ctx_t
*ctx
, catch_block_t
*block
, return_type_t
*rt
, VARIANT
*ret
)
1147 DispatchEx
*var_disp
;
1152 memset(&rt
->ei
, 0, sizeof(jsexcept_t
));
1154 hres
= create_dispex(ctx
->parser
->script
, NULL
, NULL
, &var_disp
);
1155 if(SUCCEEDED(hres
)) {
1156 hres
= jsdisp_propput_name(var_disp
, block
->identifier
, ctx
->parser
->script
->lcid
,
1157 &ex
, &rt
->ei
, NULL
/*FIXME*/);
1158 if(SUCCEEDED(hres
)) {
1159 hres
= scope_push(ctx
->scope_chain
, var_disp
, &ctx
->scope_chain
);
1160 if(SUCCEEDED(hres
)) {
1161 hres
= stat_eval(ctx
, block
->statement
, rt
, &val
);
1162 scope_pop(&ctx
->scope_chain
);
1166 jsdisp_release(var_disp
);
1177 /* ECMA-262 3rd Edition 12.14 */
1178 HRESULT
try_statement_eval(exec_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1180 try_statement_t
*stat
= (try_statement_t
*)_stat
;
1186 hres
= stat_eval(ctx
, stat
->try_statement
, rt
, &val
);
1188 TRACE("EXCEPTION\n");
1189 if(!stat
->catch_block
)
1192 hres
= catch_eval(ctx
, stat
->catch_block
, rt
, &val
);
1197 if(stat
->finally_statement
) {
1199 hres
= stat_eval(ctx
, stat
->finally_statement
, rt
, &val
);
1208 static HRESULT
return_bool(exprval_t
*ret
, DWORD b
)
1210 ret
->type
= EXPRVAL_VARIANT
;
1211 V_VT(&ret
->u
.var
) = VT_BOOL
;
1212 V_BOOL(&ret
->u
.var
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
1217 static HRESULT
get_binary_expr_values(exec_ctx_t
*ctx
, binary_expression_t
*expr
, jsexcept_t
*ei
, VARIANT
*lval
, VARIANT
*rval
)
1222 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1226 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, lval
);
1227 exprval_release(&exprval
);
1231 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1232 if(SUCCEEDED(hres
)) {
1233 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, rval
);
1234 exprval_release(&exprval
);
1245 typedef HRESULT (*oper_t
)(exec_ctx_t
*,VARIANT
*,VARIANT
*,jsexcept_t
*,VARIANT
*);
1247 static HRESULT
binary_expr_eval(exec_ctx_t
*ctx
, binary_expression_t
*expr
, oper_t oper
, jsexcept_t
*ei
,
1250 VARIANT lval
, rval
, retv
;
1253 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
1257 hres
= oper(ctx
, &lval
, &rval
, ei
, &retv
);
1258 VariantClear(&lval
);
1259 VariantClear(&rval
);
1263 ret
->type
= EXPRVAL_VARIANT
;
1268 /* ECMA-262 3rd Edition 11.13.2 */
1269 static HRESULT
assign_oper_eval(exec_ctx_t
*ctx
, expression_t
*lexpr
, expression_t
*rexpr
, oper_t oper
,
1270 jsexcept_t
*ei
, exprval_t
*ret
)
1272 VARIANT retv
, lval
, rval
;
1273 exprval_t exprval
, exprvalr
;
1276 hres
= expr_eval(ctx
, lexpr
, EXPR_NEWREF
, ei
, &exprval
);
1280 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &lval
);
1281 if(SUCCEEDED(hres
)) {
1282 hres
= expr_eval(ctx
, rexpr
, 0, ei
, &exprvalr
);
1283 if(SUCCEEDED(hres
)) {
1284 hres
= exprval_value(ctx
->parser
->script
, &exprvalr
, ei
, &rval
);
1285 exprval_release(&exprvalr
);
1287 if(SUCCEEDED(hres
)) {
1288 hres
= oper(ctx
, &lval
, &rval
, ei
, &retv
);
1289 VariantClear(&rval
);
1291 VariantClear(&lval
);
1294 if(SUCCEEDED(hres
)) {
1295 hres
= put_value(ctx
->parser
->script
, &exprval
, &retv
, ei
);
1297 VariantClear(&retv
);
1299 exprval_release(&exprval
);
1304 ret
->type
= EXPRVAL_VARIANT
;
1309 /* ECMA-262 3rd Edition 13 */
1310 HRESULT
function_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1312 function_expression_t
*expr
= (function_expression_t
*)_expr
;
1318 if(expr
->identifier
) {
1319 hres
= jsdisp_propget_name(ctx
->var_disp
, expr
->identifier
, ctx
->parser
->script
->lcid
, &var
, ei
, NULL
/*FIXME*/);
1325 hres
= create_source_function(ctx
->parser
, expr
->parameter_list
, expr
->source_elements
, ctx
->scope_chain
,
1326 expr
->src_str
, expr
->src_len
, &dispex
);
1330 V_VT(&var
) = VT_DISPATCH
;
1331 V_DISPATCH(&var
) = (IDispatch
*)_IDispatchEx_(dispex
);
1334 ret
->type
= EXPRVAL_VARIANT
;
1339 /* ECMA-262 3rd Edition 11.12 */
1340 HRESULT
conditional_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1342 conditional_expression_t
*expr
= (conditional_expression_t
*)_expr
;
1349 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1353 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, ei
, &b
);
1354 exprval_release(&exprval
);
1358 return expr_eval(ctx
, b
? expr
->true_expression
: expr
->false_expression
, flags
, ei
, ret
);
1361 /* ECMA-262 3rd Edition 11.2.1 */
1362 HRESULT
array_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1364 array_expression_t
*expr
= (array_expression_t
*)_expr
;
1366 VARIANT member
, val
;
1369 IDispatch
*obj
= NULL
;
1374 hres
= expr_eval(ctx
, expr
->member_expr
, EXPR_NEWREF
, ei
, &exprval
);
1378 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &member
);
1379 exprval_release(&exprval
);
1383 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
1384 if(SUCCEEDED(hres
)) {
1385 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1386 exprval_release(&exprval
);
1390 hres
= to_object(ctx
->parser
->script
, &member
, &obj
);
1391 VariantClear(&member
);
1392 if(SUCCEEDED(hres
)) {
1393 hres
= to_string(ctx
->parser
->script
, &val
, ei
, &str
);
1394 if(SUCCEEDED(hres
)) {
1395 if(flags
& EXPR_STRREF
) {
1396 ret
->type
= EXPRVAL_NAMEREF
;
1397 ret
->u
.nameref
.disp
= obj
;
1398 ret
->u
.nameref
.name
= str
;
1402 hres
= disp_get_id(obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1405 if(SUCCEEDED(hres
)) {
1406 exprval_set_idref(ret
, obj
, id
);
1407 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1412 IDispatch_Release(obj
);
1418 /* ECMA-262 3rd Edition 11.2.1 */
1419 HRESULT
member_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1421 member_expression_t
*expr
= (member_expression_t
*)_expr
;
1422 IDispatch
*obj
= NULL
;
1431 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1435 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &member
);
1436 exprval_release(&exprval
);
1440 hres
= to_object(ctx
->parser
->script
, &member
, &obj
);
1441 VariantClear(&member
);
1445 str
= SysAllocString(expr
->identifier
);
1446 if(flags
& EXPR_STRREF
) {
1447 ret
->type
= EXPRVAL_NAMEREF
;
1448 ret
->u
.nameref
.disp
= obj
;
1449 ret
->u
.nameref
.name
= str
;
1453 hres
= disp_get_id(obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1455 if(SUCCEEDED(hres
)) {
1456 exprval_set_idref(ret
, obj
, id
);
1457 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1462 IDispatch_Release(obj
);
1466 static void free_dp(DISPPARAMS
*dp
)
1470 for(i
=0; i
< dp
->cArgs
; i
++)
1471 VariantClear(dp
->rgvarg
+i
);
1472 heap_free(dp
->rgvarg
);
1475 static HRESULT
args_to_param(exec_ctx_t
*ctx
, argument_t
*args
, jsexcept_t
*ei
, DISPPARAMS
*dp
)
1481 HRESULT hres
= S_OK
;
1483 memset(dp
, 0, sizeof(*dp
));
1487 for(iter
= args
; iter
; iter
= iter
->next
)
1490 vargs
= heap_alloc_zero(cnt
* sizeof(*vargs
));
1492 return E_OUTOFMEMORY
;
1494 for(i
= cnt
, iter
= args
; iter
; iter
= iter
->next
) {
1495 hres
= expr_eval(ctx
, iter
->expr
, 0, ei
, &exprval
);
1499 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, vargs
+ (--i
));
1500 exprval_release(&exprval
);
1515 /* ECMA-262 3rd Edition 11.2.2 */
1516 HRESULT
new_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1518 call_expression_t
*expr
= (call_expression_t
*)_expr
;
1520 VARIANT constr
, var
;
1526 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1530 hres
= args_to_param(ctx
, expr
->argument_list
, ei
, &dp
);
1532 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &constr
);
1533 exprval_release(&exprval
);
1537 if(V_VT(&constr
) != VT_DISPATCH
) {
1538 FIXME("throw TypeError\n");
1539 VariantClear(&constr
);
1543 hres
= disp_call(V_DISPATCH(&constr
), DISPID_VALUE
, ctx
->parser
->script
->lcid
,
1544 DISPATCH_CONSTRUCT
, &dp
, &var
, ei
, NULL
/*FIXME*/);
1545 IDispatch_Release(V_DISPATCH(&constr
));
1549 ret
->type
= EXPRVAL_VARIANT
;
1554 /* ECMA-262 3rd Edition 11.2.3 */
1555 HRESULT
call_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1557 call_expression_t
*expr
= (call_expression_t
*)_expr
;
1565 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1569 hres
= args_to_param(ctx
, expr
->argument_list
, ei
, &dp
);
1570 if(SUCCEEDED(hres
)) {
1571 switch(exprval
.type
) {
1572 case EXPRVAL_VARIANT
:
1573 if(V_VT(&exprval
.u
.var
) != VT_DISPATCH
)
1574 return throw_type_error(ctx
->var_disp
->ctx
, ei
, IDS_NO_PROPERTY
, NULL
);
1576 hres
= disp_call(V_DISPATCH(&exprval
.u
.var
), DISPID_VALUE
, ctx
->parser
->script
->lcid
,
1577 DISPATCH_METHOD
, &dp
, flags
& EXPR_NOVAL
? NULL
: &var
, ei
, NULL
/*FIXME*/);
1580 hres
= disp_call(exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
, ctx
->parser
->script
->lcid
,
1581 DISPATCH_METHOD
, &dp
, flags
& EXPR_NOVAL
? NULL
: &var
, ei
, NULL
/*FIXME*/);
1584 FIXME("unimplemented type %d\n", exprval
.type
);
1591 exprval_release(&exprval
);
1595 ret
->type
= EXPRVAL_VARIANT
;
1596 if(flags
& EXPR_NOVAL
) {
1597 V_VT(&ret
->u
.var
) = VT_EMPTY
;
1599 TRACE("= %s\n", debugstr_variant(&var
));
1605 /* ECMA-262 3rd Edition 11.1.1 */
1606 HRESULT
this_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1610 ret
->type
= EXPRVAL_VARIANT
;
1611 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
1612 V_DISPATCH(&ret
->u
.var
) = ctx
->this_obj
;
1613 IDispatch_AddRef(ctx
->this_obj
);
1617 /* ECMA-262 3rd Edition 10.1.4 */
1618 HRESULT
identifier_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1620 identifier_expression_t
*expr
= (identifier_expression_t
*)_expr
;
1626 identifier
= SysAllocString(expr
->identifier
);
1628 return E_OUTOFMEMORY
;
1630 hres
= identifier_eval(ctx
, identifier
, flags
, ei
, ret
);
1632 SysFreeString(identifier
);
1636 /* ECMA-262 3rd Edition 7.8 */
1637 HRESULT
literal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1639 literal_expression_t
*expr
= (literal_expression_t
*)_expr
;
1645 hres
= literal_to_var(expr
->literal
, &var
);
1649 ret
->type
= EXPRVAL_VARIANT
;
1654 /* ECMA-262 3rd Edition 11.1.4 */
1655 HRESULT
array_literal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1657 array_literal_expression_t
*expr
= (array_literal_expression_t
*)_expr
;
1658 DWORD length
= 0, i
= 0;
1659 array_element_t
*elem
;
1667 for(elem
= expr
->element_list
; elem
; elem
= elem
->next
)
1668 length
+= elem
->elision
+1;
1669 length
+= expr
->length
;
1671 hres
= create_array(ctx
->parser
->script
, length
, &array
);
1675 for(elem
= expr
->element_list
; elem
; elem
= elem
->next
) {
1678 hres
= expr_eval(ctx
, elem
->expr
, 0, ei
, &exprval
);
1682 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1683 exprval_release(&exprval
);
1687 hres
= jsdisp_propput_idx(array
, i
, ctx
->parser
->script
->lcid
, &val
, ei
, NULL
/*FIXME*/);
1696 jsdisp_release(array
);
1700 ret
->type
= EXPRVAL_VARIANT
;
1701 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
1702 V_DISPATCH(&ret
->u
.var
) = (IDispatch
*)_IDispatchEx_(array
);
1706 /* ECMA-262 3rd Edition 11.1.5 */
1707 HRESULT
property_value_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1709 property_value_expression_t
*expr
= (property_value_expression_t
*)_expr
;
1719 hres
= create_object(ctx
->parser
->script
, NULL
, &obj
);
1723 for(iter
= expr
->property_list
; iter
; iter
= iter
->next
) {
1724 hres
= literal_to_var(iter
->name
, &tmp
);
1728 hres
= to_string(ctx
->parser
->script
, &tmp
, ei
, &name
);
1733 hres
= expr_eval(ctx
, iter
->value
, 0, ei
, &exprval
);
1734 if(SUCCEEDED(hres
)) {
1735 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1736 exprval_release(&exprval
);
1737 if(SUCCEEDED(hres
)) {
1738 hres
= jsdisp_propput_name(obj
, name
, ctx
->parser
->script
->lcid
, &val
, ei
, NULL
/*FIXME*/);
1743 SysFreeString(name
);
1749 jsdisp_release(obj
);
1753 ret
->type
= EXPRVAL_VARIANT
;
1754 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
1755 V_DISPATCH(&ret
->u
.var
) = (IDispatch
*)_IDispatchEx_(obj
);
1759 /* ECMA-262 3rd Edition 11.14 */
1760 HRESULT
comma_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1762 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1768 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
1772 VariantClear(&lval
);
1774 ret
->type
= EXPRVAL_VARIANT
;
1779 /* ECMA-262 3rd Edition 11.11 */
1780 HRESULT
logical_or_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1782 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1790 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1794 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1795 exprval_release(&exprval
);
1799 hres
= to_boolean(&val
, &b
);
1800 if(SUCCEEDED(hres
) && b
) {
1801 ret
->type
= EXPRVAL_VARIANT
;
1810 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1814 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1815 exprval_release(&exprval
);
1819 ret
->type
= EXPRVAL_VARIANT
;
1824 /* ECMA-262 3rd Edition 11.11 */
1825 HRESULT
logical_and_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1827 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1835 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1839 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1840 exprval_release(&exprval
);
1844 hres
= to_boolean(&val
, &b
);
1845 if(SUCCEEDED(hres
) && !b
) {
1846 ret
->type
= EXPRVAL_VARIANT
;
1855 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1859 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
1860 exprval_release(&exprval
);
1864 ret
->type
= EXPRVAL_VARIANT
;
1869 /* ECMA-262 3rd Edition 11.10 */
1870 static HRESULT
bitor_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1875 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
1879 hres
= to_int32(ctx
->parser
->script
, rval
, ei
, &ri
);
1888 /* ECMA-262 3rd Edition 11.10 */
1889 HRESULT
binary_or_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1891 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1895 return binary_expr_eval(ctx
, expr
, bitor_eval
, ei
, ret
);
1898 /* ECMA-262 3rd Edition 11.10 */
1899 static HRESULT
xor_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1904 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
1908 hres
= to_int32(ctx
->parser
->script
, rval
, ei
, &ri
);
1917 /* ECMA-262 3rd Edition 11.10 */
1918 HRESULT
binary_xor_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1920 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1924 return binary_expr_eval(ctx
, expr
, xor_eval
, ei
, ret
);
1927 /* ECMA-262 3rd Edition 11.10 */
1928 static HRESULT
bitand_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1933 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
1937 hres
= to_int32(ctx
->parser
->script
, rval
, ei
, &ri
);
1946 /* ECMA-262 3rd Edition 11.10 */
1947 HRESULT
binary_and_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1949 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1953 return binary_expr_eval(ctx
, expr
, bitand_eval
, ei
, ret
);
1956 /* ECMA-262 3rd Edition 11.8.6 */
1957 static HRESULT
instanceof_eval(exec_ctx_t
*ctx
, VARIANT
*inst
, VARIANT
*objv
, jsexcept_t
*ei
, VARIANT
*retv
)
1959 DispatchEx
*obj
, *iter
, *tmp
= NULL
;
1960 VARIANT_BOOL ret
= VARIANT_FALSE
;
1965 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1967 if(V_VT(objv
) != VT_DISPATCH
) {
1968 FIXME("throw TypeError\n");
1972 obj
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(objv
));
1974 FIXME("throw TypeError\n");
1978 if(is_class(obj
, JSCLASS_FUNCTION
)) {
1979 hres
= jsdisp_propget_name(obj
, prototypeW
, ctx
->parser
->script
->lcid
, &var
, ei
, NULL
/*FIXME*/);
1981 FIXME("throw TypeError\n");
1984 jsdisp_release(obj
);
1988 if(V_VT(&var
) == VT_DISPATCH
) {
1989 if(V_VT(inst
) == VT_DISPATCH
)
1990 tmp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(inst
));
1991 for(iter
= tmp
; iter
; iter
= iter
->prototype
) {
1992 hres
= disp_cmp(V_DISPATCH(&var
), (IDispatch
*)_IDispatchEx_(iter
), &b
);
2002 jsdisp_release(tmp
);
2004 FIXME("prototype is not an object\n");
2012 V_VT(retv
) = VT_BOOL
;
2017 /* ECMA-262 3rd Edition 11.8.6 */
2018 HRESULT
instanceof_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2020 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2024 return binary_expr_eval(ctx
, expr
, instanceof_eval
, ei
, ret
);
2027 /* ECMA-262 3rd Edition 11.8.7 */
2028 HRESULT
in_expression_eval(exec_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2034 /* ECMA-262 3rd Edition 11.6.1 */
2035 static HRESULT
add_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2040 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &l
, NO_HINT
);
2044 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &r
, NO_HINT
);
2050 if(V_VT(&l
) == VT_BSTR
|| V_VT(&r
) == VT_BSTR
) {
2051 BSTR lstr
= NULL
, rstr
= NULL
;
2053 if(V_VT(&l
) == VT_BSTR
)
2056 hres
= to_string(ctx
->parser
->script
, &l
, ei
, &lstr
);
2058 if(SUCCEEDED(hres
)) {
2059 if(V_VT(&r
) == VT_BSTR
)
2062 hres
= to_string(ctx
->parser
->script
, &r
, ei
, &rstr
);
2065 if(SUCCEEDED(hres
)) {
2068 len1
= SysStringLen(lstr
);
2069 len2
= SysStringLen(rstr
);
2071 V_VT(retv
) = VT_BSTR
;
2072 V_BSTR(retv
) = SysAllocStringLen(NULL
, len1
+len2
);
2073 memcpy(V_BSTR(retv
), lstr
, len1
*sizeof(WCHAR
));
2074 memcpy(V_BSTR(retv
)+len1
, rstr
, (len2
+1)*sizeof(WCHAR
));
2077 if(V_VT(&l
) != VT_BSTR
)
2078 SysFreeString(lstr
);
2079 if(V_VT(&r
) != VT_BSTR
)
2080 SysFreeString(rstr
);
2084 hres
= to_number(ctx
->parser
->script
, &l
, ei
, &nl
);
2085 if(SUCCEEDED(hres
)) {
2086 hres
= to_number(ctx
->parser
->script
, &r
, ei
, &nr
);
2088 num_set_val(retv
, num_val(&nl
) + num_val(&nr
));
2097 /* ECMA-262 3rd Edition 11.6.1 */
2098 HRESULT
add_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2100 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2104 return binary_expr_eval(ctx
, expr
, add_eval
, ei
, ret
);
2107 /* ECMA-262 3rd Edition 11.6.2 */
2108 static HRESULT
sub_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2113 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2117 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2121 num_set_val(retv
, num_val(&lnum
) - num_val(&rnum
));
2125 /* ECMA-262 3rd Edition 11.6.2 */
2126 HRESULT
sub_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2128 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2132 return binary_expr_eval(ctx
, expr
, sub_eval
, ei
, ret
);
2135 /* ECMA-262 3rd Edition 11.5.1 */
2136 static HRESULT
mul_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2141 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2145 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2149 num_set_val(retv
, num_val(&lnum
) * num_val(&rnum
));
2153 /* ECMA-262 3rd Edition 11.5.1 */
2154 HRESULT
mul_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2156 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2160 return binary_expr_eval(ctx
, expr
, mul_eval
, ei
, ret
);
2163 /* ECMA-262 3rd Edition 11.5.2 */
2164 static HRESULT
div_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2169 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2173 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2177 num_set_val(retv
, num_val(&lnum
) / num_val(&rnum
));
2181 /* ECMA-262 3rd Edition 11.5.2 */
2182 HRESULT
div_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2184 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2188 return binary_expr_eval(ctx
, expr
, div_eval
, ei
, ret
);
2191 /* ECMA-262 3rd Edition 11.5.3 */
2192 static HRESULT
mod_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2197 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &lnum
);
2201 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &rnum
);
2205 num_set_val(retv
, fmod(num_val(&lnum
), num_val(&rnum
)));
2209 /* ECMA-262 3rd Edition 11.5.3 */
2210 HRESULT
mod_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2212 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2216 return binary_expr_eval(ctx
, expr
, mod_eval
, ei
, ret
);
2219 /* ECMA-262 3rd Edition 11.4.2 */
2220 HRESULT
delete_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2222 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2223 VARIANT_BOOL b
= VARIANT_FALSE
;
2229 hres
= expr_eval(ctx
, expr
->expression
, EXPR_STRREF
, ei
, &exprval
);
2233 switch(exprval
.type
) {
2234 case EXPRVAL_IDREF
: {
2235 IDispatchEx
*dispex
;
2237 hres
= IDispatch_QueryInterface(exprval
.u
.nameref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2238 if(SUCCEEDED(hres
)) {
2239 hres
= IDispatchEx_DeleteMemberByDispID(dispex
, exprval
.u
.idref
.id
);
2241 IDispatchEx_Release(dispex
);
2245 case EXPRVAL_NAMEREF
: {
2246 IDispatchEx
*dispex
;
2248 hres
= IDispatch_QueryInterface(exprval
.u
.nameref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2249 if(SUCCEEDED(hres
)) {
2250 hres
= IDispatchEx_DeleteMemberByName(dispex
, exprval
.u
.nameref
.name
, fdexNameCaseSensitive
);
2252 IDispatchEx_Release(dispex
);
2257 FIXME("unsupported type %d\n", exprval
.type
);
2261 exprval_release(&exprval
);
2265 return return_bool(ret
, b
);
2268 /* ECMA-262 3rd Edition 11.4.2 */
2269 HRESULT
void_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2271 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2278 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2282 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &tmp
);
2283 exprval_release(&exprval
);
2289 ret
->type
= EXPRVAL_VARIANT
;
2290 V_VT(&ret
->u
.var
) = VT_EMPTY
;
2294 /* ECMA-262 3rd Edition 11.4.3 */
2295 static HRESULT
typeof_exprval(exec_ctx_t
*ctx
, exprval_t
*exprval
, jsexcept_t
*ei
, const WCHAR
**ret
)
2300 static const WCHAR booleanW
[] = {'b','o','o','l','e','a','n',0};
2301 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
2302 static const WCHAR numberW
[] = {'n','u','m','b','e','r',0};
2303 static const WCHAR objectW
[] = {'o','b','j','e','c','t',0};
2304 static const WCHAR stringW
[] = {'s','t','r','i','n','g',0};
2305 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
2307 if(exprval
->type
== EXPRVAL_INVALID
) {
2312 hres
= exprval_to_value(ctx
->parser
->script
, exprval
, ei
, &val
);
2316 switch(V_VT(&val
)) {
2336 dispex
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(&val
));
2338 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
2339 jsdisp_release(dispex
);
2346 FIXME("unhandled vt %d\n", V_VT(&val
));
2354 HRESULT
typeof_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2356 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2357 const WCHAR
*str
= NULL
;
2363 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2367 hres
= typeof_exprval(ctx
, &exprval
, ei
, &str
);
2368 exprval_release(&exprval
);
2372 ret
->type
= EXPRVAL_VARIANT
;
2373 V_VT(&ret
->u
.var
) = VT_BSTR
;
2374 V_BSTR(&ret
->u
.var
) = SysAllocString(str
);
2375 if(!V_BSTR(&ret
->u
.var
))
2376 return E_OUTOFMEMORY
;
2381 /* ECMA-262 3rd Edition 11.4.7 */
2382 HRESULT
minus_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2384 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2391 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2395 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2396 exprval_release(&exprval
);
2400 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2405 ret
->type
= EXPRVAL_VARIANT
;
2406 num_set_val(&ret
->u
.var
, -num_val(&num
));
2410 /* ECMA-262 3rd Edition 11.4.6 */
2411 HRESULT
plus_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2413 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2420 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2424 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2425 exprval_release(&exprval
);
2429 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2433 ret
->type
= EXPRVAL_VARIANT
;
2438 /* ECMA-262 3rd Edition 11.3.1 */
2439 HRESULT
post_increment_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2441 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2448 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2452 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2453 if(SUCCEEDED(hres
)) {
2454 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2458 if(SUCCEEDED(hres
)) {
2460 num_set_val(&inc
, num_val(&num
)+1.0);
2461 hres
= put_value(ctx
->parser
->script
, &exprval
, &inc
, ei
);
2464 exprval_release(&exprval
);
2468 ret
->type
= EXPRVAL_VARIANT
;
2473 /* ECMA-262 3rd Edition 11.3.2 */
2474 HRESULT
post_decrement_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2476 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2483 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2487 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2488 if(SUCCEEDED(hres
)) {
2489 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2493 if(SUCCEEDED(hres
)) {
2495 num_set_val(&dec
, num_val(&num
)-1.0);
2496 hres
= put_value(ctx
->parser
->script
, &exprval
, &dec
, ei
);
2499 exprval_release(&exprval
);
2503 ret
->type
= EXPRVAL_VARIANT
;
2508 /* ECMA-262 3rd Edition 11.4.4 */
2509 HRESULT
pre_increment_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2511 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2518 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2522 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2523 if(SUCCEEDED(hres
)) {
2524 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2528 if(SUCCEEDED(hres
)) {
2529 num_set_val(&val
, num_val(&num
)+1.0);
2530 hres
= put_value(ctx
->parser
->script
, &exprval
, &val
, ei
);
2533 exprval_release(&exprval
);
2537 ret
->type
= EXPRVAL_VARIANT
;
2542 /* ECMA-262 3rd Edition 11.4.5 */
2543 HRESULT
pre_decrement_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2545 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2552 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2556 hres
= exprval_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2557 if(SUCCEEDED(hres
)) {
2558 hres
= to_number(ctx
->parser
->script
, &val
, ei
, &num
);
2562 if(SUCCEEDED(hres
)) {
2563 num_set_val(&val
, num_val(&num
)-1.0);
2564 hres
= put_value(ctx
->parser
->script
, &exprval
, &val
, ei
);
2567 exprval_release(&exprval
);
2571 ret
->type
= EXPRVAL_VARIANT
;
2576 /* ECMA-262 3rd Edition 11.9.3 */
2577 static HRESULT
equal_values(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, BOOL
*ret
)
2579 if(V_VT(lval
) == V_VT(rval
) || (is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
))))
2580 return equal2_values(lval
, rval
, ret
);
2582 /* FIXME: NULL disps should be handled in more general way */
2583 if(V_VT(lval
) == VT_DISPATCH
&& !V_DISPATCH(lval
)) {
2586 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2589 if(V_VT(rval
) == VT_DISPATCH
&& !V_DISPATCH(rval
)) {
2592 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2595 if((V_VT(lval
) == VT_NULL
&& V_VT(rval
) == VT_EMPTY
) ||
2596 (V_VT(lval
) == VT_EMPTY
&& V_VT(rval
) == VT_NULL
)) {
2601 if(V_VT(lval
) == VT_BSTR
&& is_num_vt(V_VT(rval
))) {
2605 hres
= to_number(ctx
->parser
->script
, lval
, ei
, &v
);
2609 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2612 if(V_VT(rval
) == VT_BSTR
&& is_num_vt(V_VT(lval
))) {
2616 hres
= to_number(ctx
->parser
->script
, rval
, ei
, &v
);
2620 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2623 if(V_VT(rval
) == VT_BOOL
) {
2627 V_I4(&v
) = V_BOOL(rval
) ? 1 : 0;
2628 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2631 if(V_VT(lval
) == VT_BOOL
) {
2635 V_I4(&v
) = V_BOOL(lval
) ? 1 : 0;
2636 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2640 if(V_VT(rval
) == VT_DISPATCH
&& (V_VT(lval
) == VT_BSTR
|| is_num_vt(V_VT(lval
)))) {
2644 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &v
, NO_HINT
);
2648 hres
= equal_values(ctx
, lval
, &v
, ei
, ret
);
2655 if(V_VT(lval
) == VT_DISPATCH
&& (V_VT(rval
) == VT_BSTR
|| is_num_vt(V_VT(rval
)))) {
2659 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &v
, NO_HINT
);
2663 hres
= equal_values(ctx
, &v
, rval
, ei
, ret
);
2674 /* ECMA-262 3rd Edition 11.9.1 */
2675 HRESULT
equal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2677 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2684 hres
= get_binary_expr_values(ctx
, expr
, ei
, &rval
, &lval
);
2688 hres
= equal_values(ctx
, &rval
, &lval
, ei
, &b
);
2692 return return_bool(ret
, b
);
2695 /* ECMA-262 3rd Edition 11.9.4 */
2696 HRESULT
equal2_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2698 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2705 hres
= get_binary_expr_values(ctx
, expr
, ei
, &rval
, &lval
);
2709 hres
= equal2_values(&rval
, &lval
, &b
);
2713 return return_bool(ret
, b
);
2716 /* ECMA-262 3rd Edition 11.9.2 */
2717 HRESULT
not_equal_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2719 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2726 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2730 hres
= equal_values(ctx
, &lval
, &rval
, ei
, &b
);
2734 return return_bool(ret
, !b
);
2737 /* ECMA-262 3rd Edition 11.9.5 */
2738 HRESULT
not_equal2_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2740 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2747 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2751 hres
= equal2_values(&lval
, &rval
, &b
);
2755 return return_bool(ret
, !b
);
2758 /* ECMA-262 3rd Edition 11.8.5 */
2759 static HRESULT
less_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, BOOL greater
, jsexcept_t
*ei
, BOOL
*ret
)
2761 VARIANT l
, r
, ln
, rn
;
2764 hres
= to_primitive(ctx
->parser
->script
, lval
, ei
, &l
, NO_HINT
);
2768 hres
= to_primitive(ctx
->parser
->script
, rval
, ei
, &r
, NO_HINT
);
2774 if(V_VT(&l
) == VT_BSTR
&& V_VT(&r
) == VT_BSTR
) {
2775 *ret
= (strcmpW(V_BSTR(&l
), V_BSTR(&r
)) < 0) ^ greater
;
2776 SysFreeString(V_BSTR(&l
));
2777 SysFreeString(V_BSTR(&r
));
2781 hres
= to_number(ctx
->parser
->script
, &l
, ei
, &ln
);
2784 hres
= to_number(ctx
->parser
->script
, &r
, ei
, &rn
);
2789 if(V_VT(&ln
) == VT_I4
&& V_VT(&rn
) == VT_I4
) {
2790 *ret
= (V_I4(&ln
) < V_I4(&rn
)) ^ greater
;
2792 DOUBLE ld
= num_val(&ln
);
2793 DOUBLE rd
= num_val(&rn
);
2795 *ret
= !isnan(ld
) && !isnan(rd
) && ((ld
< rd
) ^ greater
);
2801 /* ECMA-262 3rd Edition 11.8.1 */
2802 HRESULT
less_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2804 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2811 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2815 hres
= less_eval(ctx
, &lval
, &rval
, FALSE
, ei
, &b
);
2816 VariantClear(&lval
);
2817 VariantClear(&rval
);
2821 return return_bool(ret
, b
);
2824 /* ECMA-262 3rd Edition 11.8.3 */
2825 HRESULT
lesseq_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2827 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2834 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2838 hres
= less_eval(ctx
, &rval
, &lval
, TRUE
, ei
, &b
);
2839 VariantClear(&lval
);
2840 VariantClear(&rval
);
2844 return return_bool(ret
, b
);
2847 /* ECMA-262 3rd Edition 11.8.2 */
2848 HRESULT
greater_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2850 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2857 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2861 hres
= less_eval(ctx
, &rval
, &lval
, FALSE
, ei
, &b
);
2862 VariantClear(&lval
);
2863 VariantClear(&rval
);
2867 return return_bool(ret
, b
);
2870 /* ECMA-262 3rd Edition 11.8.4 */
2871 HRESULT
greatereq_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2873 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2880 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2884 hres
= less_eval(ctx
, &lval
, &rval
, TRUE
, ei
, &b
);
2885 VariantClear(&lval
);
2886 VariantClear(&rval
);
2890 return return_bool(ret
, b
);
2893 /* ECMA-262 3rd Edition 11.4.8 */
2894 HRESULT
binary_negation_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2896 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2904 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2908 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, ei
, &val
);
2909 exprval_release(&exprval
);
2913 hres
= to_int32(ctx
->parser
->script
, &val
, ei
, &i
);
2917 ret
->type
= EXPRVAL_VARIANT
;
2918 V_VT(&ret
->u
.var
) = VT_I4
;
2919 V_I4(&ret
->u
.var
) = ~i
;
2923 /* ECMA-262 3rd Edition 11.4.9 */
2924 HRESULT
logical_negation_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2926 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2933 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2937 hres
= exprval_to_boolean(ctx
->parser
->script
, &exprval
, ei
, &b
);
2938 exprval_release(&exprval
);
2942 return return_bool(ret
, !b
);
2945 /* ECMA-262 3rd Edition 11.7.1 */
2946 static HRESULT
lshift_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2952 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
2956 hres
= to_uint32(ctx
->parser
->script
, rval
, ei
, &ri
);
2961 V_I4(retv
) = li
<< (ri
&0x1f);
2965 /* ECMA-262 3rd Edition 11.7.1 */
2966 HRESULT
left_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2968 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2972 return binary_expr_eval(ctx
, expr
, lshift_eval
, ei
, ret
);
2975 /* ECMA-262 3rd Edition 11.7.2 */
2976 static HRESULT
rshift_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2982 hres
= to_int32(ctx
->parser
->script
, lval
, ei
, &li
);
2986 hres
= to_uint32(ctx
->parser
->script
, rval
, ei
, &ri
);
2991 V_I4(retv
) = li
>> (ri
&0x1f);
2995 /* ECMA-262 3rd Edition 11.7.2 */
2996 HRESULT
right_shift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2998 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3002 return binary_expr_eval(ctx
, expr
, rshift_eval
, ei
, ret
);
3005 /* ECMA-262 3rd Edition 11.7.3 */
3006 static HRESULT
rshift2_eval(exec_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3011 hres
= to_uint32(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.3 */
3025 HRESULT
right2_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
, rshift2_eval
, ei
, ret
);
3034 /* ECMA-262 3rd Edition 11.13.1 */
3035 HRESULT
assign_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3037 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3038 exprval_t exprval
, exprvalr
;
3044 hres
= expr_eval(ctx
, expr
->expression1
, EXPR_NEWREF
, ei
, &exprval
);
3048 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprvalr
);
3049 if(SUCCEEDED(hres
)) {
3050 hres
= exprval_to_value(ctx
->parser
->script
, &exprvalr
, ei
, &rval
);
3051 exprval_release(&exprvalr
);
3055 hres
= put_value(ctx
->parser
->script
, &exprval
, &rval
, ei
);
3057 exprval_release(&exprval
);
3061 ret
->type
= EXPRVAL_VARIANT
;
3066 /* ECMA-262 3rd Edition 11.13.2 */
3067 HRESULT
assign_lshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3069 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3073 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, lshift_eval
, ei
, ret
);
3076 /* ECMA-262 3rd Edition 11.13.2 */
3077 HRESULT
assign_rshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3079 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3083 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, rshift_eval
, ei
, ret
);
3086 /* ECMA-262 3rd Edition 11.13.2 */
3087 HRESULT
assign_rrshift_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3089 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3093 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, rshift2_eval
, ei
, ret
);
3096 /* ECMA-262 3rd Edition 11.13.2 */
3097 HRESULT
assign_add_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3099 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3103 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, add_eval
, ei
, ret
);
3106 /* ECMA-262 3rd Edition 11.13.2 */
3107 HRESULT
assign_sub_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3109 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3113 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, sub_eval
, ei
, ret
);
3116 /* ECMA-262 3rd Edition 11.13.2 */
3117 HRESULT
assign_mul_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3119 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3123 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, mul_eval
, ei
, ret
);
3126 /* ECMA-262 3rd Edition 11.13.2 */
3127 HRESULT
assign_div_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3129 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3133 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, div_eval
, ei
, ret
);
3136 /* ECMA-262 3rd Edition 11.13.2 */
3137 HRESULT
assign_mod_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3139 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3143 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, mod_eval
, ei
, ret
);
3146 /* ECMA-262 3rd Edition 11.13.2 */
3147 HRESULT
assign_and_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3149 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3153 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, bitand_eval
, ei
, ret
);
3156 /* ECMA-262 3rd Edition 11.13.2 */
3157 HRESULT
assign_or_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3159 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3163 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, bitor_eval
, ei
, ret
);
3166 /* ECMA-262 3rd Edition 11.13.2 */
3167 HRESULT
assign_xor_expression_eval(exec_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3169 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3173 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, xor_eval
, ei
, ret
);