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"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
32 #define EXPR_NOVAL 0x0001
33 #define EXPR_NEWREF 0x0002
34 #define EXPR_STRREF 0x0004
36 struct _return_type_t
{
46 static inline HRESULT
stat_eval(script_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
48 return stat
->eval(ctx
, stat
, rt
, ret
);
51 static inline HRESULT
expr_eval(script_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
53 return expr
->eval(ctx
, expr
, flags
, ei
, ret
);
56 static HRESULT
stack_push(exec_ctx_t
*ctx
, VARIANT
*v
)
58 if(!ctx
->stack_size
) {
59 ctx
->stack
= heap_alloc(16*sizeof(VARIANT
));
63 }else if(ctx
->stack_size
== ctx
->top
) {
66 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(VARIANT
));
72 ctx
->stack
= new_stack
;
76 ctx
->stack
[ctx
->top
++] = *v
;
80 static HRESULT
stack_push_bool(exec_ctx_t
*ctx
, BOOL b
)
85 V_BOOL(&v
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
86 return stack_push(ctx
, &v
);
89 static inline VARIANT
*stack_pop(exec_ctx_t
*ctx
)
92 return ctx
->stack
+ --ctx
->top
;
95 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
98 VariantClear(stack_pop(ctx
));
101 static void exprval_release(exprval_t
*val
)
104 case EXPRVAL_VARIANT
:
105 if(V_VT(&val
->u
.var
) != VT_EMPTY
)
106 VariantClear(&val
->u
.var
);
109 if(val
->u
.idref
.disp
)
110 IDispatch_Release(val
->u
.idref
.disp
);
112 case EXPRVAL_NAMEREF
:
113 if(val
->u
.nameref
.disp
)
114 IDispatch_Release(val
->u
.nameref
.disp
);
115 SysFreeString(val
->u
.nameref
.name
);
117 case EXPRVAL_INVALID
:
118 SysFreeString(val
->u
.identifier
);
122 /* ECMA-262 3rd Edition 8.7.1 */
123 static HRESULT
exprval_value(script_ctx_t
*ctx
, exprval_t
*val
, jsexcept_t
*ei
, VARIANT
*ret
)
125 V_VT(ret
) = VT_EMPTY
;
128 case EXPRVAL_VARIANT
:
129 return VariantCopy(ret
, &val
->u
.var
);
131 if(!val
->u
.idref
.disp
) {
132 FIXME("throw ReferenceError\n");
136 return disp_propget(ctx
, val
->u
.idref
.disp
, val
->u
.idref
.id
, ret
, ei
, NULL
/*FIXME*/);
137 case EXPRVAL_NAMEREF
:
139 case EXPRVAL_INVALID
:
140 return throw_type_error(ctx
, ei
, JS_E_UNDEFINED_VARIABLE
, val
->u
.identifier
);
143 ERR("type %d\n", val
->type
);
147 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*val
, jsexcept_t
*ei
, VARIANT
*ret
)
149 if(val
->type
== EXPRVAL_VARIANT
) {
151 V_VT(&val
->u
.var
) = VT_EMPTY
;
155 return exprval_value(ctx
, val
, ei
, ret
);
158 static HRESULT
exprval_to_boolean(script_ctx_t
*ctx
, exprval_t
*exprval
, jsexcept_t
*ei
, VARIANT_BOOL
*b
)
160 if(exprval
->type
!= EXPRVAL_VARIANT
) {
164 hres
= exprval_to_value(ctx
, exprval
, ei
, &val
);
168 hres
= to_boolean(&val
, b
);
173 return to_boolean(&exprval
->u
.var
, b
);
176 static void exprval_init(exprval_t
*val
)
178 val
->type
= EXPRVAL_VARIANT
;
179 V_VT(&val
->u
.var
) = VT_EMPTY
;
182 static void exprval_set_idref(exprval_t
*val
, IDispatch
*disp
, DISPID id
)
184 val
->type
= EXPRVAL_IDREF
;
185 val
->u
.idref
.disp
= disp
;
186 val
->u
.idref
.id
= id
;
189 IDispatch_AddRef(disp
);
192 HRESULT
scope_push(scope_chain_t
*scope
, jsdisp_t
*obj
, scope_chain_t
**ret
)
194 scope_chain_t
*new_scope
;
196 new_scope
= heap_alloc(sizeof(scope_chain_t
));
198 return E_OUTOFMEMORY
;
203 new_scope
->obj
= obj
;
207 new_scope
->next
= scope
;
209 new_scope
->next
= NULL
;
216 static void scope_pop(scope_chain_t
**scope
)
225 void scope_release(scope_chain_t
*scope
)
231 scope_release(scope
->next
);
233 jsdisp_release(scope
->obj
);
237 HRESULT
create_exec_ctx(script_ctx_t
*script_ctx
, IDispatch
*this_obj
, jsdisp_t
*var_disp
,
238 scope_chain_t
*scope
, BOOL is_global
, exec_ctx_t
**ret
)
242 ctx
= heap_alloc_zero(sizeof(exec_ctx_t
));
244 return E_OUTOFMEMORY
;
247 ctx
->is_global
= is_global
;
250 ctx
->this_obj
= this_obj
;
251 else if(script_ctx
->host_global
)
252 ctx
->this_obj
= script_ctx
->host_global
;
254 ctx
->this_obj
= to_disp(script_ctx
->global
);
255 IDispatch_AddRef(ctx
->this_obj
);
257 jsdisp_addref(var_disp
);
258 ctx
->var_disp
= var_disp
;
262 ctx
->scope_chain
= scope
;
269 void exec_release(exec_ctx_t
*ctx
)
275 scope_release(ctx
->scope_chain
);
277 jsdisp_release(ctx
->var_disp
);
279 IDispatch_Release(ctx
->this_obj
);
280 heap_free(ctx
->stack
);
284 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, BSTR name
, DWORD flags
, DISPID
*id
)
289 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
291 TRACE("unsing IDispatch\n");
294 return IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &name
, 1, 0, id
);
298 hres
= IDispatchEx_GetDispID(dispex
, name
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
299 IDispatchEx_Release(dispex
);
303 /* ECMA-262 3rd Edition 8.7.2 */
304 static HRESULT
put_value(script_ctx_t
*ctx
, exprval_t
*ref
, VARIANT
*v
, jsexcept_t
*ei
)
306 if(ref
->type
!= EXPRVAL_IDREF
)
307 return throw_reference_error(ctx
, ei
, JS_E_ILLEGAL_ASSIGN
, NULL
);
309 return disp_propput(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, v
, ei
, NULL
/*FIXME*/);
312 static inline BOOL
is_null(const VARIANT
*v
)
314 return V_VT(v
) == VT_NULL
|| (V_VT(v
) == VT_DISPATCH
&& !V_DISPATCH(v
));
317 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
319 IObjectIdentity
*identity
;
320 IUnknown
*unk1
, *unk2
;
328 if(!disp1
|| !disp2
) {
333 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
337 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
339 IUnknown_Release(unk1
);
346 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
347 if(SUCCEEDED(hres
)) {
348 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
349 IObjectIdentity_Release(identity
);
356 IUnknown_Release(unk1
);
357 IUnknown_Release(unk2
);
361 /* ECMA-262 3rd Edition 11.9.6 */
362 static HRESULT
equal2_values(VARIANT
*lval
, VARIANT
*rval
, BOOL
*ret
)
366 if(V_VT(lval
) != V_VT(rval
)) {
367 if(is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
)))
368 *ret
= num_val(lval
) == num_val(rval
);
369 else if(is_null(lval
))
370 *ret
= is_null(rval
);
382 *ret
= V_I4(lval
) == V_I4(rval
);
385 *ret
= V_R8(lval
) == V_R8(rval
);
389 *ret
= SysStringLen(V_BSTR(rval
))?FALSE
:TRUE
;
390 else if(!V_BSTR(rval
))
391 *ret
= SysStringLen(V_BSTR(lval
))?FALSE
:TRUE
;
393 *ret
= !strcmpW(V_BSTR(lval
), V_BSTR(rval
));
396 return disp_cmp(V_DISPATCH(lval
), V_DISPATCH(rval
), ret
);
398 *ret
= !V_BOOL(lval
) == !V_BOOL(rval
);
401 FIXME("unimplemented vt %d\n", V_VT(lval
));
408 static HRESULT
literal_to_var(script_ctx_t
*ctx
, literal_t
*literal
, VARIANT
*v
)
410 switch(literal
->type
) {
416 V_I4(v
) = literal
->u
.lval
;
420 V_R8(v
) = literal
->u
.dval
;
423 BSTR str
= SysAllocString(literal
->u
.wstr
);
425 return E_OUTOFMEMORY
;
433 V_BOOL(v
) = literal
->u
.bval
;
439 hres
= create_regexp(ctx
, literal
->u
.regexp
.str
, literal
->u
.regexp
.str_len
,
440 literal
->u
.regexp
.flags
, ®exp
);
444 var_set_jsdisp(v
, regexp
);
451 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
457 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
458 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
459 hres
= disp_get_id(ctx
, item
->disp
, identifier
, 0, &id
);
460 if(SUCCEEDED(hres
)) {
462 exprval_set_idref(ret
, item
->disp
, id
);
471 HRESULT
exec_source(exec_ctx_t
*ctx
, parser_ctx_t
*parser
, source_elements_t
*source
, BOOL from_eval
,
472 jsexcept_t
*ei
, VARIANT
*retv
)
474 script_ctx_t
*script
= parser
->script
;
475 function_declaration_t
*func
;
476 parser_ctx_t
*prev_parser
;
480 exec_ctx_t
*prev_ctx
;
484 for(func
= source
->functions
; func
; func
= func
->next
) {
488 hres
= create_source_function(parser
, func
->expr
->parameter_list
, func
->expr
->source_elements
,
489 ctx
->scope_chain
, func
->expr
->src_str
, func
->expr
->src_len
, &func_obj
);
493 var_set_jsdisp(&var
, func_obj
);
494 hres
= jsdisp_propput_name(ctx
->var_disp
, func
->expr
->identifier
, &var
, ei
, NULL
);
495 jsdisp_release(func_obj
);
500 for(var
= source
->variables
; var
; var
= var
->next
) {
504 name
= SysAllocString(var
->identifier
);
506 return E_OUTOFMEMORY
;
508 if(!ctx
->is_global
|| !lookup_global_members(parser
->script
, name
, NULL
))
509 hres
= jsdisp_get_id(ctx
->var_disp
, var
->identifier
, fdexNameEnsure
, &id
);
515 prev_ctx
= script
->exec_ctx
;
516 script
->exec_ctx
= ctx
;
518 prev_parser
= ctx
->parser
;
519 ctx
->parser
= parser
;
521 V_VT(&val
) = VT_EMPTY
;
522 memset(&rt
, 0, sizeof(rt
));
525 for(stat
= source
->statement
; stat
; stat
= stat
->next
) {
526 hres
= stat_eval(script
, stat
, &rt
, &tmp
);
532 if(rt
.type
!= RT_NORMAL
)
536 script
->exec_ctx
= prev_ctx
;
537 ctx
->parser
= prev_parser
;
539 if(rt
.type
!= RT_NORMAL
&& rt
.type
!= RT_RETURN
) {
540 FIXME("wrong rt %d\n", rt
.type
);
550 if(!retv
|| (!from_eval
&& rt
.type
!= RT_RETURN
))
557 /* ECMA-262 3rd Edition 10.1.4 */
558 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
560 scope_chain_t
*scope
;
565 TRACE("%s\n", debugstr_w(identifier
));
567 for(scope
= ctx
->exec_ctx
->scope_chain
; scope
; scope
= scope
->next
) {
568 hres
= jsdisp_get_id(scope
->obj
, identifier
, 0, &id
);
569 if(SUCCEEDED(hres
)) {
570 exprval_set_idref(ret
, to_disp(scope
->obj
), id
);
575 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
576 if(SUCCEEDED(hres
)) {
577 exprval_set_idref(ret
, to_disp(ctx
->global
), id
);
581 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
582 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
589 hres
= IActiveScriptSite_GetItemInfo(ctx
->site
, identifier
,
590 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
592 WARN("GetItemInfo failed: %08x\n", hres
);
596 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
597 IUnknown_Release(unk
);
599 WARN("object does not implement IDispatch\n");
604 ret
->type
= EXPRVAL_VARIANT
;
605 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
606 V_DISPATCH(&ret
->u
.var
) = item
->disp
;
607 IDispatch_AddRef(item
->disp
);
612 if(lookup_global_members(ctx
, identifier
, ret
))
615 if(flags
& EXPR_NEWREF
) {
616 hres
= jsdisp_get_id(ctx
->global
, identifier
, fdexNameEnsure
, &id
);
620 exprval_set_idref(ret
, to_disp(ctx
->global
), id
);
624 ret
->type
= EXPRVAL_INVALID
;
625 ret
->u
.identifier
= SysAllocString(identifier
);
626 if(!ret
->u
.identifier
)
627 return E_OUTOFMEMORY
;
632 /* ECMA-262 3rd Edition 12.1 */
633 HRESULT
block_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
635 block_statement_t
*stat
= (block_statement_t
*)_stat
;
642 V_VT(&val
) = VT_EMPTY
;
643 for(iter
= stat
->stat_list
; iter
; iter
= iter
->next
) {
644 hres
= stat_eval(ctx
, iter
, rt
, &tmp
);
650 if(rt
->type
!= RT_NORMAL
)
663 /* ECMA-262 3rd Edition 12.2 */
664 static HRESULT
variable_list_eval(script_ctx_t
*ctx
, variable_declaration_t
*var_list
, jsexcept_t
*ei
)
666 variable_declaration_t
*iter
;
669 for(iter
= var_list
; iter
; iter
= iter
->next
) {
676 hres
= expr_eval(ctx
, iter
->expr
, 0, ei
, &exprval
);
680 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
681 exprval_release(&exprval
);
685 hres
= jsdisp_propput_name(ctx
->exec_ctx
->var_disp
, iter
->identifier
, &val
, ei
, NULL
/*FIXME*/);
694 /* ECMA-262 3rd Edition 12.2 */
695 HRESULT
var_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
697 var_statement_t
*stat
= (var_statement_t
*)_stat
;
702 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
706 V_VT(ret
) = VT_EMPTY
;
710 /* ECMA-262 3rd Edition 12.3 */
711 HRESULT
empty_statement_eval(script_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
715 V_VT(ret
) = VT_EMPTY
;
719 /* ECMA-262 3rd Edition 12.4 */
720 HRESULT
expression_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
722 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
729 hres
= expr_eval(ctx
, stat
->expr
, EXPR_NOVAL
, &rt
->ei
, &exprval
);
733 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
734 exprval_release(&exprval
);
739 TRACE("= %s\n", debugstr_variant(ret
));
743 /* ECMA-262 3rd Edition 12.5 */
744 HRESULT
if_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
746 if_statement_t
*stat
= (if_statement_t
*)_stat
;
753 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
757 hres
= exprval_to_boolean(ctx
, &exprval
, &rt
->ei
, &b
);
758 exprval_release(&exprval
);
763 hres
= stat_eval(ctx
, stat
->if_stat
, rt
, ret
);
764 else if(stat
->else_stat
)
765 hres
= stat_eval(ctx
, stat
->else_stat
, rt
, ret
);
767 V_VT(ret
) = VT_EMPTY
;
772 /* ECMA-262 3rd Edition 12.6.2 */
773 HRESULT
while_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
775 while_statement_t
*stat
= (while_statement_t
*)_stat
;
784 V_VT(&val
) = VT_EMPTY
;
785 test_expr
= !stat
->do_while
;
789 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
793 hres
= exprval_to_boolean(ctx
, &exprval
, &rt
->ei
, &b
);
794 exprval_release(&exprval
);
795 if(FAILED(hres
) || !b
)
801 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
808 if(rt
->type
== RT_CONTINUE
)
809 rt
->type
= RT_NORMAL
;
810 if(rt
->type
!= RT_NORMAL
)
819 if(rt
->type
== RT_BREAK
)
820 rt
->type
= RT_NORMAL
;
826 /* ECMA-262 3rd Edition 12.6.3 */
827 HRESULT
for_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
829 for_statement_t
*stat
= (for_statement_t
*)_stat
;
830 VARIANT val
, tmp
, retv
;
837 if(stat
->variable_list
) {
838 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
841 }else if(stat
->begin_expr
) {
842 hres
= expr_eval(ctx
, stat
->begin_expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
846 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
847 exprval_release(&exprval
);
854 V_VT(&retv
) = VT_EMPTY
;
858 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
862 hres
= exprval_to_boolean(ctx
, &exprval
, &rt
->ei
, &b
);
863 exprval_release(&exprval
);
864 if(FAILED(hres
) || !b
)
868 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
875 if(rt
->type
== RT_CONTINUE
)
876 rt
->type
= RT_NORMAL
;
877 else if(rt
->type
!= RT_NORMAL
)
881 hres
= expr_eval(ctx
, stat
->end_expr
, 0, &rt
->ei
, &exprval
);
885 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
886 exprval_release(&exprval
);
899 if(rt
->type
== RT_BREAK
)
900 rt
->type
= RT_NORMAL
;
906 /* ECMA-262 3rd Edition 12.6.4 */
907 HRESULT
forin_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
909 forin_statement_t
*stat
= (forin_statement_t
*)_stat
;
910 VARIANT val
, name
, retv
, tmp
;
911 DISPID id
= DISPID_STARTENUM
;
912 BSTR str
, identifier
= NULL
;
920 hres
= variable_list_eval(ctx
, stat
->variable
, &rt
->ei
);
925 hres
= expr_eval(ctx
, stat
->in_expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
929 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
930 exprval_release(&exprval
);
934 if(V_VT(&val
) != VT_DISPATCH
) {
935 TRACE("in vt %d\n", V_VT(&val
));
937 V_VT(ret
) = VT_EMPTY
;
941 hres
= IDispatch_QueryInterface(V_DISPATCH(&val
), &IID_IDispatchEx
, (void**)&in_obj
);
942 IDispatch_Release(V_DISPATCH(&val
));
944 TRACE("Object doesn't support IDispatchEx\n");
945 V_VT(ret
) = VT_EMPTY
;
949 V_VT(&retv
) = VT_EMPTY
;
952 identifier
= SysAllocString(stat
->variable
->identifier
);
955 hres
= IDispatchEx_GetNextDispID(in_obj
, fdexEnumDefault
, id
, &id
);
956 if(FAILED(hres
) || hres
== S_FALSE
)
959 hres
= IDispatchEx_GetMemberName(in_obj
, id
, &str
);
963 TRACE("iter %s\n", debugstr_w(str
));
966 hres
= identifier_eval(ctx
, identifier
, 0, NULL
, &exprval
);
968 hres
= expr_eval(ctx
, stat
->expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
969 if(SUCCEEDED(hres
)) {
970 V_VT(&name
) = VT_BSTR
;
972 hres
= put_value(ctx
, &exprval
, &name
, &rt
->ei
);
973 exprval_release(&exprval
);
979 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
986 if(rt
->type
== RT_CONTINUE
)
987 rt
->type
= RT_NORMAL
;
988 else if(rt
->type
!= RT_NORMAL
)
992 SysFreeString(identifier
);
993 IDispatchEx_Release(in_obj
);
999 if(rt
->type
== RT_BREAK
)
1000 rt
->type
= RT_NORMAL
;
1006 /* ECMA-262 3rd Edition 12.7 */
1007 HRESULT
continue_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1009 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
1013 if(stat
->identifier
) {
1014 FIXME("indentifier not implemented\n");
1018 rt
->type
= RT_CONTINUE
;
1019 V_VT(ret
) = VT_EMPTY
;
1023 /* ECMA-262 3rd Edition 12.8 */
1024 HRESULT
break_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1026 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
1030 if(stat
->identifier
) {
1031 FIXME("indentifier not implemented\n");
1035 rt
->type
= RT_BREAK
;
1036 V_VT(ret
) = VT_EMPTY
;
1040 /* ECMA-262 3rd Edition 12.9 */
1041 HRESULT
return_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1043 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
1051 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1055 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, ret
);
1056 exprval_release(&exprval
);
1060 V_VT(ret
) = VT_EMPTY
;
1063 TRACE("= %s\n", debugstr_variant(ret
));
1064 rt
->type
= RT_RETURN
;
1068 /* ECMA-262 3rd Edition 12.10 */
1069 HRESULT
with_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1071 with_statement_t
*stat
= (with_statement_t
*)_stat
;
1080 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1084 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
1085 exprval_release(&exprval
);
1089 hres
= to_object(ctx
, &val
, &disp
);
1094 obj
= iface_to_jsdisp((IUnknown
*)disp
);
1095 IDispatch_Release(disp
);
1097 FIXME("disp id not jsdisp\n");
1101 hres
= scope_push(ctx
->exec_ctx
->scope_chain
, obj
, &ctx
->exec_ctx
->scope_chain
);
1102 jsdisp_release(obj
);
1106 hres
= stat_eval(ctx
, stat
->statement
, rt
, ret
);
1108 scope_pop(&ctx
->exec_ctx
->scope_chain
);
1112 /* ECMA-262 3rd Edition 12.12 */
1113 HRESULT
labelled_statement_eval(script_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
1119 /* ECMA-262 3rd Edition 12.13 */
1120 HRESULT
switch_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1122 switch_statement_t
*stat
= (switch_statement_t
*)_stat
;
1123 case_clausule_t
*iter
, *default_clausule
= NULL
;
1124 statement_t
*stat_iter
;
1132 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1136 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
1137 exprval_release(&exprval
);
1141 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1143 default_clausule
= iter
;
1147 hres
= expr_eval(ctx
, iter
->expr
, 0, &rt
->ei
, &exprval
);
1151 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &cval
);
1152 exprval_release(&exprval
);
1156 hres
= equal2_values(&val
, &cval
, &b
);
1157 VariantClear(&cval
);
1158 if(FAILED(hres
) || b
)
1167 iter
= default_clausule
;
1169 V_VT(&val
) = VT_EMPTY
;
1173 for(stat_iter
= iter
->stat
; stat_iter
; stat_iter
= stat_iter
->next
) {
1174 hres
= stat_eval(ctx
, stat_iter
, rt
, &tmp
);
1181 if(rt
->type
!= RT_NORMAL
)
1191 if(rt
->type
== RT_BREAK
)
1192 rt
->type
= RT_NORMAL
;
1198 /* ECMA-262 3rd Edition 12.13 */
1199 HRESULT
throw_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1201 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
1208 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1212 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
1213 exprval_release(&exprval
);
1218 return DISP_E_EXCEPTION
;
1221 /* ECMA-262 3rd Edition 12.14 */
1222 static HRESULT
catch_eval(script_ctx_t
*ctx
, catch_block_t
*block
, return_type_t
*rt
, VARIANT
*ret
)
1229 memset(&rt
->ei
, 0, sizeof(jsexcept_t
));
1231 hres
= create_dispex(ctx
, NULL
, NULL
, &var_disp
);
1232 if(SUCCEEDED(hres
)) {
1233 hres
= jsdisp_propput_name(var_disp
, block
->identifier
, &ex
, &rt
->ei
, NULL
/*FIXME*/);
1234 if(SUCCEEDED(hres
)) {
1235 hres
= scope_push(ctx
->exec_ctx
->scope_chain
, var_disp
, &ctx
->exec_ctx
->scope_chain
);
1236 if(SUCCEEDED(hres
)) {
1237 hres
= stat_eval(ctx
, block
->statement
, rt
, &val
);
1238 scope_pop(&ctx
->exec_ctx
->scope_chain
);
1242 jsdisp_release(var_disp
);
1253 /* ECMA-262 3rd Edition 12.14 */
1254 HRESULT
try_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1256 try_statement_t
*stat
= (try_statement_t
*)_stat
;
1262 hres
= stat_eval(ctx
, stat
->try_statement
, rt
, &val
);
1264 TRACE("EXCEPTION\n");
1265 if(!stat
->catch_block
)
1268 hres
= catch_eval(ctx
, stat
->catch_block
, rt
, &val
);
1273 if(stat
->finally_statement
) {
1275 hres
= stat_eval(ctx
, stat
->finally_statement
, rt
, &val
);
1284 static HRESULT
return_bool(exprval_t
*ret
, DWORD b
)
1286 ret
->type
= EXPRVAL_VARIANT
;
1287 V_VT(&ret
->u
.var
) = VT_BOOL
;
1288 V_BOOL(&ret
->u
.var
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
1293 static HRESULT
get_binary_expr_values(script_ctx_t
*ctx
, binary_expression_t
*expr
, jsexcept_t
*ei
, VARIANT
*lval
, VARIANT
*rval
)
1298 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1302 hres
= exprval_to_value(ctx
, &exprval
, ei
, lval
);
1303 exprval_release(&exprval
);
1307 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1308 if(SUCCEEDED(hres
)) {
1309 hres
= exprval_to_value(ctx
, &exprval
, ei
, rval
);
1310 exprval_release(&exprval
);
1321 typedef HRESULT (*oper_t
)(script_ctx_t
*,VARIANT
*,VARIANT
*,jsexcept_t
*,VARIANT
*);
1323 static HRESULT
binary_expr_eval(script_ctx_t
*ctx
, binary_expression_t
*expr
, oper_t oper
, jsexcept_t
*ei
,
1326 VARIANT lval
, rval
, retv
;
1329 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
1333 hres
= oper(ctx
, &lval
, &rval
, ei
, &retv
);
1334 VariantClear(&lval
);
1335 VariantClear(&rval
);
1339 ret
->type
= EXPRVAL_VARIANT
;
1344 /* ECMA-262 3rd Edition 11.13.2 */
1345 static HRESULT
assign_oper_eval(script_ctx_t
*ctx
, expression_t
*lexpr
, expression_t
*rexpr
, oper_t oper
,
1346 jsexcept_t
*ei
, exprval_t
*ret
)
1348 VARIANT retv
, lval
, rval
;
1349 exprval_t exprval
, exprvalr
;
1352 hres
= expr_eval(ctx
, lexpr
, EXPR_NEWREF
, ei
, &exprval
);
1356 hres
= exprval_value(ctx
, &exprval
, ei
, &lval
);
1357 if(SUCCEEDED(hres
)) {
1358 hres
= expr_eval(ctx
, rexpr
, 0, ei
, &exprvalr
);
1359 if(SUCCEEDED(hres
)) {
1360 hres
= exprval_value(ctx
, &exprvalr
, ei
, &rval
);
1361 exprval_release(&exprvalr
);
1363 if(SUCCEEDED(hres
)) {
1364 hres
= oper(ctx
, &lval
, &rval
, ei
, &retv
);
1365 VariantClear(&rval
);
1367 VariantClear(&lval
);
1370 if(SUCCEEDED(hres
)) {
1371 hres
= put_value(ctx
, &exprval
, &retv
, ei
);
1373 VariantClear(&retv
);
1375 exprval_release(&exprval
);
1380 ret
->type
= EXPRVAL_VARIANT
;
1385 /* ECMA-262 3rd Edition 13 */
1386 HRESULT
function_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1388 function_expression_t
*expr
= (function_expression_t
*)_expr
;
1394 if(expr
->identifier
) {
1395 hres
= jsdisp_propget_name(ctx
->exec_ctx
->var_disp
, expr
->identifier
, &var
, ei
, NULL
/*FIXME*/);
1401 hres
= create_source_function(ctx
->exec_ctx
->parser
, expr
->parameter_list
, expr
->source_elements
, ctx
->exec_ctx
->scope_chain
,
1402 expr
->src_str
, expr
->src_len
, &dispex
);
1406 var_set_jsdisp(&var
, dispex
);
1409 ret
->type
= EXPRVAL_VARIANT
;
1414 /* ECMA-262 3rd Edition 11.12 */
1415 HRESULT
conditional_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1417 conditional_expression_t
*expr
= (conditional_expression_t
*)_expr
;
1424 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1428 hres
= exprval_to_boolean(ctx
, &exprval
, ei
, &b
);
1429 exprval_release(&exprval
);
1433 return expr_eval(ctx
, b
? expr
->true_expression
: expr
->false_expression
, flags
, ei
, ret
);
1436 /* ECMA-262 3rd Edition 11.2.1 */
1437 HRESULT
array_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1439 array_expression_t
*expr
= (array_expression_t
*)_expr
;
1441 VARIANT member
, val
;
1444 IDispatch
*obj
= NULL
;
1449 hres
= expr_eval(ctx
, expr
->member_expr
, 0, ei
, &exprval
);
1453 hres
= exprval_to_value(ctx
, &exprval
, ei
, &member
);
1454 exprval_release(&exprval
);
1458 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
1459 if(SUCCEEDED(hres
)) {
1460 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1461 exprval_release(&exprval
);
1464 if(SUCCEEDED(hres
)) {
1465 hres
= to_object(ctx
, &member
, &obj
);
1469 VariantClear(&member
);
1470 if(SUCCEEDED(hres
)) {
1471 hres
= to_string(ctx
, &val
, ei
, &str
);
1473 if(SUCCEEDED(hres
)) {
1474 if(flags
& EXPR_STRREF
) {
1475 ret
->type
= EXPRVAL_NAMEREF
;
1476 ret
->u
.nameref
.disp
= obj
;
1477 ret
->u
.nameref
.name
= str
;
1481 hres
= disp_get_id(ctx
, obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1485 if(SUCCEEDED(hres
)) {
1486 exprval_set_idref(ret
, obj
, id
);
1487 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1492 IDispatch_Release(obj
);
1498 /* ECMA-262 3rd Edition 11.2.1 */
1499 HRESULT
member_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1501 member_expression_t
*expr
= (member_expression_t
*)_expr
;
1502 IDispatch
*obj
= NULL
;
1511 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1515 hres
= exprval_to_value(ctx
, &exprval
, ei
, &member
);
1516 exprval_release(&exprval
);
1520 hres
= to_object(ctx
, &member
, &obj
);
1521 VariantClear(&member
);
1525 str
= SysAllocString(expr
->identifier
);
1526 if(flags
& EXPR_STRREF
) {
1527 ret
->type
= EXPRVAL_NAMEREF
;
1528 ret
->u
.nameref
.disp
= obj
;
1529 ret
->u
.nameref
.name
= str
;
1533 hres
= disp_get_id(ctx
, obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1535 if(SUCCEEDED(hres
)) {
1536 exprval_set_idref(ret
, obj
, id
);
1537 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1542 IDispatch_Release(obj
);
1546 static void free_dp(DISPPARAMS
*dp
)
1550 for(i
=0; i
< dp
->cArgs
; i
++)
1551 VariantClear(dp
->rgvarg
+i
);
1552 heap_free(dp
->rgvarg
);
1555 static HRESULT
args_to_param(script_ctx_t
*ctx
, argument_t
*args
, jsexcept_t
*ei
, DISPPARAMS
*dp
)
1561 HRESULT hres
= S_OK
;
1563 memset(dp
, 0, sizeof(*dp
));
1567 for(iter
= args
; iter
; iter
= iter
->next
)
1570 vargs
= heap_alloc_zero(cnt
* sizeof(*vargs
));
1572 return E_OUTOFMEMORY
;
1574 for(i
= cnt
, iter
= args
; iter
; iter
= iter
->next
) {
1575 hres
= expr_eval(ctx
, iter
->expr
, 0, ei
, &exprval
);
1579 hres
= exprval_to_value(ctx
, &exprval
, ei
, vargs
+ (--i
));
1580 exprval_release(&exprval
);
1595 /* ECMA-262 3rd Edition 11.2.2 */
1596 HRESULT
new_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
;
1600 VARIANT constr
, var
;
1606 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1610 hres
= args_to_param(ctx
, expr
->argument_list
, ei
, &dp
);
1612 hres
= exprval_to_value(ctx
, &exprval
, ei
, &constr
);
1613 exprval_release(&exprval
);
1617 /* NOTE: Should use to_object here */
1619 if(V_VT(&constr
) == VT_NULL
) {
1620 VariantClear(&constr
);
1621 return throw_type_error(ctx
, ei
, JS_E_OBJECT_EXPECTED
, NULL
);
1622 } else if(V_VT(&constr
) != VT_DISPATCH
) {
1623 VariantClear(&constr
);
1624 return throw_type_error(ctx
, ei
, JS_E_INVALID_ACTION
, NULL
);
1625 } else if(!V_DISPATCH(&constr
)) {
1626 VariantClear(&constr
);
1627 return throw_type_error(ctx
, ei
, JS_E_INVALID_PROPERTY
, NULL
);
1630 hres
= disp_call(ctx
, V_DISPATCH(&constr
), DISPID_VALUE
,
1631 DISPATCH_CONSTRUCT
, &dp
, &var
, ei
, NULL
/*FIXME*/);
1632 IDispatch_Release(V_DISPATCH(&constr
));
1637 ret
->type
= EXPRVAL_VARIANT
;
1642 /* ECMA-262 3rd Edition 11.2.3 */
1643 HRESULT
call_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1645 call_expression_t
*expr
= (call_expression_t
*)_expr
;
1653 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1657 hres
= args_to_param(ctx
, expr
->argument_list
, ei
, &dp
);
1658 if(SUCCEEDED(hres
)) {
1659 switch(exprval
.type
) {
1660 case EXPRVAL_VARIANT
:
1661 if(V_VT(&exprval
.u
.var
) == VT_DISPATCH
)
1662 hres
= disp_call(ctx
, V_DISPATCH(&exprval
.u
.var
), DISPID_VALUE
,
1663 DISPATCH_METHOD
, &dp
, flags
& EXPR_NOVAL
? NULL
: &var
, ei
, NULL
/*FIXME*/);
1665 hres
= throw_type_error(ctx
, ei
, JS_E_INVALID_PROPERTY
, NULL
);
1668 hres
= disp_call(ctx
, exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
,
1669 DISPATCH_METHOD
, &dp
, flags
& EXPR_NOVAL
? NULL
: &var
, ei
, NULL
/*FIXME*/);
1671 case EXPRVAL_INVALID
:
1672 hres
= throw_type_error(ctx
, ei
, JS_E_OBJECT_EXPECTED
, NULL
);
1675 FIXME("unimplemented type %d\n", exprval
.type
);
1682 exprval_release(&exprval
);
1686 ret
->type
= EXPRVAL_VARIANT
;
1687 if(flags
& EXPR_NOVAL
) {
1688 V_VT(&ret
->u
.var
) = VT_EMPTY
;
1690 TRACE("= %s\n", debugstr_variant(&var
));
1696 /* ECMA-262 3rd Edition 11.1.1 */
1697 HRESULT
this_expression_eval(script_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1701 ret
->type
= EXPRVAL_VARIANT
;
1702 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
1703 V_DISPATCH(&ret
->u
.var
) = ctx
->exec_ctx
->this_obj
;
1704 IDispatch_AddRef(ctx
->exec_ctx
->this_obj
);
1708 /* ECMA-262 3rd Edition 10.1.4 */
1709 HRESULT
identifier_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1711 identifier_expression_t
*expr
= (identifier_expression_t
*)_expr
;
1717 identifier
= SysAllocString(expr
->identifier
);
1719 return E_OUTOFMEMORY
;
1721 hres
= identifier_eval(ctx
, identifier
, flags
, ei
, ret
);
1723 SysFreeString(identifier
);
1727 /* ECMA-262 3rd Edition 7.8.2 */
1728 HRESULT
interp_bool(exec_ctx_t
*ctx
)
1730 const LONG arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1732 TRACE("%s\n", arg
? "true" : "false");
1734 return stack_push_bool(ctx
, arg
);
1737 /* ECMA-262 3rd Edition 7.8.3 */
1738 HRESULT
interp_int(exec_ctx_t
*ctx
)
1740 const LONG arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1747 return stack_push(ctx
, &v
);
1750 /* ECMA-262 3rd Edition 7.8 */
1751 HRESULT
literal_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1753 literal_expression_t
*expr
= (literal_expression_t
*)_expr
;
1759 hres
= literal_to_var(ctx
, expr
->literal
, &var
);
1763 ret
->type
= EXPRVAL_VARIANT
;
1768 /* ECMA-262 3rd Edition 11.1.4 */
1769 HRESULT
array_literal_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1771 array_literal_expression_t
*expr
= (array_literal_expression_t
*)_expr
;
1772 DWORD length
= 0, i
= 0;
1773 array_element_t
*elem
;
1781 for(elem
= expr
->element_list
; elem
; elem
= elem
->next
)
1782 length
+= elem
->elision
+1;
1783 length
+= expr
->length
;
1785 hres
= create_array(ctx
, length
, &array
);
1789 for(elem
= expr
->element_list
; elem
; elem
= elem
->next
) {
1792 hres
= expr_eval(ctx
, elem
->expr
, 0, ei
, &exprval
);
1796 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1797 exprval_release(&exprval
);
1801 hres
= jsdisp_propput_idx(array
, i
, &val
, ei
, NULL
/*FIXME*/);
1810 jsdisp_release(array
);
1814 ret
->type
= EXPRVAL_VARIANT
;
1815 var_set_jsdisp(&ret
->u
.var
, array
);
1819 /* ECMA-262 3rd Edition 11.1.5 */
1820 HRESULT
property_value_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1822 property_value_expression_t
*expr
= (property_value_expression_t
*)_expr
;
1832 hres
= create_object(ctx
, NULL
, &obj
);
1836 for(iter
= expr
->property_list
; iter
; iter
= iter
->next
) {
1837 hres
= literal_to_var(ctx
, iter
->name
, &tmp
);
1841 hres
= to_string(ctx
, &tmp
, ei
, &name
);
1846 hres
= expr_eval(ctx
, iter
->value
, 0, ei
, &exprval
);
1847 if(SUCCEEDED(hres
)) {
1848 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1849 exprval_release(&exprval
);
1850 if(SUCCEEDED(hres
)) {
1851 hres
= jsdisp_propput_name(obj
, name
, &val
, ei
, NULL
/*FIXME*/);
1856 SysFreeString(name
);
1862 jsdisp_release(obj
);
1866 ret
->type
= EXPRVAL_VARIANT
;
1867 var_set_jsdisp(&ret
->u
.var
, obj
);
1871 /* ECMA-262 3rd Edition 11.14 */
1872 HRESULT
comma_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1874 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1880 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
1884 VariantClear(&lval
);
1886 ret
->type
= EXPRVAL_VARIANT
;
1891 /* ECMA-262 3rd Edition 11.11 */
1892 HRESULT
logical_or_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1894 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1902 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1906 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1907 exprval_release(&exprval
);
1911 hres
= to_boolean(&val
, &b
);
1912 if(SUCCEEDED(hres
) && b
) {
1913 ret
->type
= EXPRVAL_VARIANT
;
1922 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1926 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1927 exprval_release(&exprval
);
1931 ret
->type
= EXPRVAL_VARIANT
;
1936 /* ECMA-262 3rd Edition 11.11 */
1937 HRESULT
logical_and_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1939 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1947 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1951 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1952 exprval_release(&exprval
);
1956 hres
= to_boolean(&val
, &b
);
1957 if(SUCCEEDED(hres
) && !b
) {
1958 ret
->type
= EXPRVAL_VARIANT
;
1967 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1971 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1972 exprval_release(&exprval
);
1976 ret
->type
= EXPRVAL_VARIANT
;
1981 /* ECMA-262 3rd Edition 11.10 */
1982 static HRESULT
bitor_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
1987 hres
= to_int32(ctx
, lval
, ei
, &li
);
1991 hres
= to_int32(ctx
, rval
, ei
, &ri
);
2000 /* ECMA-262 3rd Edition 11.10 */
2001 HRESULT
binary_or_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2003 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2007 return binary_expr_eval(ctx
, expr
, bitor_eval
, ei
, ret
);
2010 /* ECMA-262 3rd Edition 11.10 */
2011 static HRESULT
xor_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2016 hres
= to_int32(ctx
, lval
, ei
, &li
);
2020 hres
= to_int32(ctx
, rval
, ei
, &ri
);
2029 /* ECMA-262 3rd Edition 11.10 */
2030 HRESULT
binary_xor_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2032 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2036 return binary_expr_eval(ctx
, expr
, xor_eval
, ei
, ret
);
2039 /* ECMA-262 3rd Edition 11.10 */
2040 static HRESULT
bitand_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2045 hres
= to_int32(ctx
, lval
, ei
, &li
);
2049 hres
= to_int32(ctx
, rval
, ei
, &ri
);
2058 /* ECMA-262 3rd Edition 11.10 */
2059 HRESULT
binary_and_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2061 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2065 return binary_expr_eval(ctx
, expr
, bitand_eval
, ei
, ret
);
2068 /* ECMA-262 3rd Edition 11.8.6 */
2069 static HRESULT
instanceof_eval(script_ctx_t
*ctx
, VARIANT
*inst
, VARIANT
*objv
, jsexcept_t
*ei
, VARIANT
*retv
)
2071 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
2072 VARIANT_BOOL ret
= VARIANT_FALSE
;
2077 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2079 if(V_VT(objv
) != VT_DISPATCH
|| !V_DISPATCH(objv
))
2080 return throw_type_error(ctx
, ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
2082 obj
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(objv
));
2084 FIXME("non-jsdisp objects not supported\n");
2088 if(is_class(obj
, JSCLASS_FUNCTION
)) {
2089 hres
= jsdisp_propget_name(obj
, prototypeW
, &var
, ei
, NULL
/*FIXME*/);
2091 hres
= throw_type_error(ctx
, ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
2093 jsdisp_release(obj
);
2097 if(V_VT(&var
) == VT_DISPATCH
) {
2098 if(V_VT(inst
) == VT_DISPATCH
)
2099 tmp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(inst
));
2100 for(iter
= tmp
; iter
; iter
= iter
->prototype
) {
2101 hres
= disp_cmp(V_DISPATCH(&var
), to_disp(iter
), &b
);
2111 jsdisp_release(tmp
);
2113 FIXME("prototype is not an object\n");
2121 V_VT(retv
) = VT_BOOL
;
2126 /* ECMA-262 3rd Edition 11.8.6 */
2127 HRESULT
instanceof_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2129 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2133 return binary_expr_eval(ctx
, expr
, instanceof_eval
, ei
, ret
);
2136 /* ECMA-262 3rd Edition 11.8.7 */
2137 HRESULT
interp_in(exec_ctx_t
*ctx
)
2147 obj
= stack_pop(ctx
);
2150 if(V_VT(obj
) != VT_DISPATCH
|| !V_DISPATCH(obj
)) {
2153 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
2156 hres
= to_string(ctx
->parser
->script
, v
, &ctx
->ei
, &str
);
2159 IDispatch_Release(V_DISPATCH(obj
));
2163 hres
= disp_get_id(ctx
->parser
->script
, V_DISPATCH(obj
), str
, 0, &id
);
2164 IDispatch_Release(V_DISPATCH(obj
));
2168 else if(hres
== DISP_E_UNKNOWNNAME
)
2173 return stack_push_bool(ctx
, ret
);
2176 /* ECMA-262 3rd Edition 11.6.1 */
2177 static HRESULT
add_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2182 hres
= to_primitive(ctx
, lval
, ei
, &l
, NO_HINT
);
2186 hres
= to_primitive(ctx
, rval
, ei
, &r
, NO_HINT
);
2192 if(V_VT(&l
) == VT_BSTR
|| V_VT(&r
) == VT_BSTR
) {
2193 BSTR lstr
= NULL
, rstr
= NULL
;
2195 if(V_VT(&l
) == VT_BSTR
)
2198 hres
= to_string(ctx
, &l
, ei
, &lstr
);
2200 if(SUCCEEDED(hres
)) {
2201 if(V_VT(&r
) == VT_BSTR
)
2204 hres
= to_string(ctx
, &r
, ei
, &rstr
);
2207 if(SUCCEEDED(hres
)) {
2210 len1
= SysStringLen(lstr
);
2211 len2
= SysStringLen(rstr
);
2213 V_VT(retv
) = VT_BSTR
;
2214 V_BSTR(retv
) = SysAllocStringLen(NULL
, len1
+len2
);
2215 memcpy(V_BSTR(retv
), lstr
, len1
*sizeof(WCHAR
));
2216 memcpy(V_BSTR(retv
)+len1
, rstr
, (len2
+1)*sizeof(WCHAR
));
2219 if(V_VT(&l
) != VT_BSTR
)
2220 SysFreeString(lstr
);
2221 if(V_VT(&r
) != VT_BSTR
)
2222 SysFreeString(rstr
);
2226 hres
= to_number(ctx
, &l
, ei
, &nl
);
2227 if(SUCCEEDED(hres
)) {
2228 hres
= to_number(ctx
, &r
, ei
, &nr
);
2230 num_set_val(retv
, num_val(&nl
) + num_val(&nr
));
2239 /* ECMA-262 3rd Edition 11.6.1 */
2240 HRESULT
interp_add(exec_ctx_t
*ctx
)
2242 VARIANT
*l
, *r
, ret
;
2248 TRACE("%s + %s\n", debugstr_variant(l
), debugstr_variant(r
));
2250 hres
= add_eval(ctx
->parser
->script
, l
, r
, &ctx
->ei
, &ret
);
2254 return stack_push(ctx
, &ret
);
2257 /* ECMA-262 3rd Edition 11.6.2 */
2258 static HRESULT
sub_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2263 hres
= to_number(ctx
, lval
, ei
, &lnum
);
2267 hres
= to_number(ctx
, rval
, ei
, &rnum
);
2271 num_set_val(retv
, num_val(&lnum
) - num_val(&rnum
));
2275 /* ECMA-262 3rd Edition 11.6.2 */
2276 HRESULT
sub_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2278 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2282 return binary_expr_eval(ctx
, expr
, sub_eval
, ei
, ret
);
2285 /* ECMA-262 3rd Edition 11.5.1 */
2286 static HRESULT
mul_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2291 hres
= to_number(ctx
, lval
, ei
, &lnum
);
2295 hres
= to_number(ctx
, rval
, ei
, &rnum
);
2299 num_set_val(retv
, num_val(&lnum
) * num_val(&rnum
));
2303 /* ECMA-262 3rd Edition 11.5.1 */
2304 HRESULT
mul_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2306 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2310 return binary_expr_eval(ctx
, expr
, mul_eval
, ei
, ret
);
2313 /* ECMA-262 3rd Edition 11.5.2 */
2314 static HRESULT
div_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2319 hres
= to_number(ctx
, lval
, ei
, &lnum
);
2323 hres
= to_number(ctx
, rval
, ei
, &rnum
);
2327 num_set_val(retv
, num_val(&lnum
) / num_val(&rnum
));
2331 /* ECMA-262 3rd Edition 11.5.2 */
2332 HRESULT
div_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2334 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2338 return binary_expr_eval(ctx
, expr
, div_eval
, ei
, ret
);
2341 /* ECMA-262 3rd Edition 11.5.3 */
2342 static HRESULT
mod_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2347 hres
= to_number(ctx
, lval
, ei
, &lnum
);
2351 hres
= to_number(ctx
, rval
, ei
, &rnum
);
2355 num_set_val(retv
, fmod(num_val(&lnum
), num_val(&rnum
)));
2359 /* ECMA-262 3rd Edition 11.5.3 */
2360 HRESULT
mod_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2362 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2366 return binary_expr_eval(ctx
, expr
, mod_eval
, ei
, ret
);
2369 /* ECMA-262 3rd Edition 11.4.2 */
2370 HRESULT
delete_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2372 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2373 VARIANT_BOOL b
= VARIANT_FALSE
;
2379 hres
= expr_eval(ctx
, expr
->expression
, EXPR_STRREF
, ei
, &exprval
);
2383 switch(exprval
.type
) {
2384 case EXPRVAL_IDREF
: {
2385 IDispatchEx
*dispex
;
2387 hres
= IDispatch_QueryInterface(exprval
.u
.nameref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2388 if(SUCCEEDED(hres
)) {
2389 hres
= IDispatchEx_DeleteMemberByDispID(dispex
, exprval
.u
.idref
.id
);
2391 IDispatchEx_Release(dispex
);
2395 case EXPRVAL_NAMEREF
: {
2396 IDispatchEx
*dispex
;
2398 hres
= IDispatch_QueryInterface(exprval
.u
.nameref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2399 if(SUCCEEDED(hres
)) {
2400 hres
= IDispatchEx_DeleteMemberByName(dispex
, exprval
.u
.nameref
.name
,
2401 make_grfdex(ctx
, fdexNameCaseSensitive
));
2403 IDispatchEx_Release(dispex
);
2408 FIXME("unsupported type %d\n", exprval
.type
);
2412 exprval_release(&exprval
);
2416 return return_bool(ret
, b
);
2419 /* ECMA-262 3rd Edition 11.4.2 */
2420 HRESULT
void_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2422 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2429 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2433 hres
= exprval_to_value(ctx
, &exprval
, ei
, &tmp
);
2434 exprval_release(&exprval
);
2440 ret
->type
= EXPRVAL_VARIANT
;
2441 V_VT(&ret
->u
.var
) = VT_EMPTY
;
2445 /* ECMA-262 3rd Edition 11.4.3 */
2446 static HRESULT
typeof_exprval(script_ctx_t
*ctx
, exprval_t
*exprval
, jsexcept_t
*ei
, const WCHAR
**ret
)
2451 static const WCHAR booleanW
[] = {'b','o','o','l','e','a','n',0};
2452 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
2453 static const WCHAR numberW
[] = {'n','u','m','b','e','r',0};
2454 static const WCHAR objectW
[] = {'o','b','j','e','c','t',0};
2455 static const WCHAR stringW
[] = {'s','t','r','i','n','g',0};
2456 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
2457 static const WCHAR unknownW
[] = {'u','n','k','n','o','w','n',0};
2459 if(exprval
->type
== EXPRVAL_INVALID
) {
2464 hres
= exprval_to_value(ctx
, exprval
, ei
, &val
);
2466 if(exprval
->type
== EXPRVAL_IDREF
) {
2473 switch(V_VT(&val
)) {
2493 if(V_DISPATCH(&val
) && (dispex
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(&val
)))) {
2494 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
2495 jsdisp_release(dispex
);
2502 FIXME("unhandled vt %d\n", V_VT(&val
));
2510 HRESULT
typeof_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2512 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2513 const WCHAR
*str
= NULL
;
2519 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2523 hres
= typeof_exprval(ctx
, &exprval
, ei
, &str
);
2524 exprval_release(&exprval
);
2528 ret
->type
= EXPRVAL_VARIANT
;
2529 V_VT(&ret
->u
.var
) = VT_BSTR
;
2530 V_BSTR(&ret
->u
.var
) = SysAllocString(str
);
2531 if(!V_BSTR(&ret
->u
.var
))
2532 return E_OUTOFMEMORY
;
2537 /* ECMA-262 3rd Edition 11.4.7 */
2538 HRESULT
minus_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2540 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2547 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
2551 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
2552 exprval_release(&exprval
);
2556 hres
= to_number(ctx
, &val
, ei
, &num
);
2561 ret
->type
= EXPRVAL_VARIANT
;
2562 num_set_val(&ret
->u
.var
, -num_val(&num
));
2566 /* ECMA-262 3rd Edition 11.4.6 */
2567 HRESULT
interp_tonum(exec_ctx_t
*ctx
)
2575 hres
= to_number(ctx
->parser
->script
, v
, &ctx
->ei
, &num
);
2580 return stack_push(ctx
, &num
);
2583 /* ECMA-262 3rd Edition 11.3.1 */
2584 HRESULT
post_increment_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2586 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2593 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2597 hres
= exprval_value(ctx
, &exprval
, ei
, &val
);
2598 if(SUCCEEDED(hres
)) {
2599 hres
= to_number(ctx
, &val
, ei
, &num
);
2603 if(SUCCEEDED(hres
)) {
2605 num_set_val(&inc
, num_val(&num
)+1.0);
2606 hres
= put_value(ctx
, &exprval
, &inc
, ei
);
2609 exprval_release(&exprval
);
2613 ret
->type
= EXPRVAL_VARIANT
;
2618 /* ECMA-262 3rd Edition 11.3.2 */
2619 HRESULT
post_decrement_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2621 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2628 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2632 hres
= exprval_value(ctx
, &exprval
, ei
, &val
);
2633 if(SUCCEEDED(hres
)) {
2634 hres
= to_number(ctx
, &val
, ei
, &num
);
2638 if(SUCCEEDED(hres
)) {
2640 num_set_val(&dec
, num_val(&num
)-1.0);
2641 hres
= put_value(ctx
, &exprval
, &dec
, ei
);
2644 exprval_release(&exprval
);
2648 ret
->type
= EXPRVAL_VARIANT
;
2653 /* ECMA-262 3rd Edition 11.4.4 */
2654 HRESULT
pre_increment_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2656 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2663 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2667 hres
= exprval_value(ctx
, &exprval
, ei
, &val
);
2668 if(SUCCEEDED(hres
)) {
2669 hres
= to_number(ctx
, &val
, ei
, &num
);
2673 if(SUCCEEDED(hres
)) {
2674 num_set_val(&val
, num_val(&num
)+1.0);
2675 hres
= put_value(ctx
, &exprval
, &val
, ei
);
2678 exprval_release(&exprval
);
2682 ret
->type
= EXPRVAL_VARIANT
;
2687 /* ECMA-262 3rd Edition 11.4.5 */
2688 HRESULT
pre_decrement_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2690 unary_expression_t
*expr
= (unary_expression_t
*)_expr
;
2697 hres
= expr_eval(ctx
, expr
->expression
, EXPR_NEWREF
, ei
, &exprval
);
2701 hres
= exprval_value(ctx
, &exprval
, ei
, &val
);
2702 if(SUCCEEDED(hres
)) {
2703 hres
= to_number(ctx
, &val
, ei
, &num
);
2707 if(SUCCEEDED(hres
)) {
2708 num_set_val(&val
, num_val(&num
)-1.0);
2709 hres
= put_value(ctx
, &exprval
, &val
, ei
);
2712 exprval_release(&exprval
);
2716 ret
->type
= EXPRVAL_VARIANT
;
2721 /* ECMA-262 3rd Edition 11.9.3 */
2722 static HRESULT
equal_values(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, BOOL
*ret
)
2724 if(V_VT(lval
) == V_VT(rval
) || (is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
))))
2725 return equal2_values(lval
, rval
, ret
);
2727 /* FIXME: NULL disps should be handled in more general way */
2728 if(V_VT(lval
) == VT_DISPATCH
&& !V_DISPATCH(lval
)) {
2731 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2734 if(V_VT(rval
) == VT_DISPATCH
&& !V_DISPATCH(rval
)) {
2737 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2740 if((V_VT(lval
) == VT_NULL
&& V_VT(rval
) == VT_EMPTY
) ||
2741 (V_VT(lval
) == VT_EMPTY
&& V_VT(rval
) == VT_NULL
)) {
2746 if(V_VT(lval
) == VT_BSTR
&& is_num_vt(V_VT(rval
))) {
2750 hres
= to_number(ctx
, lval
, ei
, &v
);
2754 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2757 if(V_VT(rval
) == VT_BSTR
&& is_num_vt(V_VT(lval
))) {
2761 hres
= to_number(ctx
, rval
, ei
, &v
);
2765 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2768 if(V_VT(rval
) == VT_BOOL
) {
2772 V_I4(&v
) = V_BOOL(rval
) ? 1 : 0;
2773 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2776 if(V_VT(lval
) == VT_BOOL
) {
2780 V_I4(&v
) = V_BOOL(lval
) ? 1 : 0;
2781 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2785 if(V_VT(rval
) == VT_DISPATCH
&& (V_VT(lval
) == VT_BSTR
|| is_num_vt(V_VT(lval
)))) {
2789 hres
= to_primitive(ctx
, rval
, ei
, &v
, NO_HINT
);
2793 hres
= equal_values(ctx
, lval
, &v
, ei
, ret
);
2800 if(V_VT(lval
) == VT_DISPATCH
&& (V_VT(rval
) == VT_BSTR
|| is_num_vt(V_VT(rval
)))) {
2804 hres
= to_primitive(ctx
, lval
, ei
, &v
, NO_HINT
);
2808 hres
= equal_values(ctx
, &v
, rval
, ei
, ret
);
2819 /* ECMA-262 3rd Edition 11.9.1 */
2820 HRESULT
equal_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2822 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2829 hres
= get_binary_expr_values(ctx
, expr
, ei
, &rval
, &lval
);
2833 hres
= equal_values(ctx
, &rval
, &lval
, ei
, &b
);
2834 VariantClear(&lval
);
2835 VariantClear(&rval
);
2839 return return_bool(ret
, b
);
2842 /* ECMA-262 3rd Edition 11.9.4 */
2843 static HRESULT
interp_eq2(exec_ctx_t
*ctx
)
2854 hres
= equal2_values(r
, l
, &b
);
2860 return stack_push_bool(ctx
, b
);
2863 /* ECMA-262 3rd Edition 11.9.2 */
2864 HRESULT
not_equal_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2866 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2873 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2877 hres
= equal_values(ctx
, &lval
, &rval
, ei
, &b
);
2878 VariantClear(&lval
);
2879 VariantClear(&rval
);
2883 return return_bool(ret
, !b
);
2886 /* ECMA-262 3rd Edition 11.9.5 */
2887 static HRESULT
interp_neq2(exec_ctx_t
*ctx
)
2898 hres
= equal2_values(r
, l
, &b
);
2904 return stack_push_bool(ctx
, !b
);
2907 /* ECMA-262 3rd Edition 11.8.5 */
2908 static HRESULT
less_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, BOOL greater
, jsexcept_t
*ei
, BOOL
*ret
)
2910 VARIANT l
, r
, ln
, rn
;
2913 hres
= to_primitive(ctx
, lval
, ei
, &l
, NO_HINT
);
2917 hres
= to_primitive(ctx
, rval
, ei
, &r
, NO_HINT
);
2923 if(V_VT(&l
) == VT_BSTR
&& V_VT(&r
) == VT_BSTR
) {
2924 *ret
= (strcmpW(V_BSTR(&l
), V_BSTR(&r
)) < 0) ^ greater
;
2925 SysFreeString(V_BSTR(&l
));
2926 SysFreeString(V_BSTR(&r
));
2930 hres
= to_number(ctx
, &l
, ei
, &ln
);
2933 hres
= to_number(ctx
, &r
, ei
, &rn
);
2938 if(V_VT(&ln
) == VT_I4
&& V_VT(&rn
) == VT_I4
) {
2939 *ret
= (V_I4(&ln
) < V_I4(&rn
)) ^ greater
;
2941 DOUBLE ld
= num_val(&ln
);
2942 DOUBLE rd
= num_val(&rn
);
2944 *ret
= !isnan(ld
) && !isnan(rd
) && ((ld
< rd
) ^ greater
);
2950 /* ECMA-262 3rd Edition 11.8.1 */
2951 HRESULT
less_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2953 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2960 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2964 hres
= less_eval(ctx
, &lval
, &rval
, FALSE
, ei
, &b
);
2965 VariantClear(&lval
);
2966 VariantClear(&rval
);
2970 return return_bool(ret
, b
);
2973 /* ECMA-262 3rd Edition 11.8.3 */
2974 HRESULT
lesseq_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2976 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
2983 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
2987 hres
= less_eval(ctx
, &rval
, &lval
, TRUE
, ei
, &b
);
2988 VariantClear(&lval
);
2989 VariantClear(&rval
);
2993 return return_bool(ret
, b
);
2996 /* ECMA-262 3rd Edition 11.8.2 */
2997 HRESULT
greater_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
2999 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3006 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
3010 hres
= less_eval(ctx
, &rval
, &lval
, FALSE
, ei
, &b
);
3011 VariantClear(&lval
);
3012 VariantClear(&rval
);
3016 return return_bool(ret
, b
);
3019 /* ECMA-262 3rd Edition 11.8.4 */
3020 HRESULT
greatereq_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3022 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3029 hres
= get_binary_expr_values(ctx
, expr
, ei
, &lval
, &rval
);
3033 hres
= less_eval(ctx
, &lval
, &rval
, TRUE
, ei
, &b
);
3034 VariantClear(&lval
);
3035 VariantClear(&rval
);
3039 return return_bool(ret
, b
);
3042 /* ECMA-262 3rd Edition 11.4.8 */
3043 HRESULT
interp_bneg(exec_ctx_t
*ctx
)
3052 hres
= to_int32(ctx
->parser
->script
, v
, &ctx
->ei
, &i
);
3059 return stack_push(ctx
, &r
);
3062 /* ECMA-262 3rd Edition 11.4.9 */
3063 HRESULT
interp_neg(exec_ctx_t
*ctx
)
3072 hres
= to_boolean(v
, &b
);
3077 return stack_push_bool(ctx
, !b
);
3080 /* ECMA-262 3rd Edition 11.7.1 */
3081 static HRESULT
lshift_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3087 hres
= to_int32(ctx
, lval
, ei
, &li
);
3091 hres
= to_uint32(ctx
, rval
, ei
, &ri
);
3096 V_I4(retv
) = li
<< (ri
&0x1f);
3100 /* ECMA-262 3rd Edition 11.7.1 */
3101 HRESULT
left_shift_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3103 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3107 return binary_expr_eval(ctx
, expr
, lshift_eval
, ei
, ret
);
3110 /* ECMA-262 3rd Edition 11.7.2 */
3111 static HRESULT
rshift_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3117 hres
= to_int32(ctx
, lval
, ei
, &li
);
3121 hres
= to_uint32(ctx
, rval
, ei
, &ri
);
3126 V_I4(retv
) = li
>> (ri
&0x1f);
3130 /* ECMA-262 3rd Edition 11.7.2 */
3131 HRESULT
right_shift_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3133 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3137 return binary_expr_eval(ctx
, expr
, rshift_eval
, ei
, ret
);
3140 /* ECMA-262 3rd Edition 11.7.3 */
3141 static HRESULT
rshift2_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
3146 hres
= to_uint32(ctx
, lval
, ei
, &li
);
3150 hres
= to_uint32(ctx
, rval
, ei
, &ri
);
3155 V_I4(retv
) = li
>> (ri
&0x1f);
3159 /* ECMA-262 3rd Edition 11.7.3 */
3160 HRESULT
right2_shift_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3162 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3166 return binary_expr_eval(ctx
, expr
, rshift2_eval
, ei
, ret
);
3169 /* ECMA-262 3rd Edition 11.13.1 */
3170 HRESULT
assign_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3172 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3173 exprval_t exprval
, exprvalr
;
3179 hres
= expr_eval(ctx
, expr
->expression1
, EXPR_NEWREF
, ei
, &exprval
);
3183 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprvalr
);
3184 if(SUCCEEDED(hres
)) {
3185 hres
= exprval_to_value(ctx
, &exprvalr
, ei
, &rval
);
3186 exprval_release(&exprvalr
);
3189 if(SUCCEEDED(hres
)) {
3190 hres
= put_value(ctx
, &exprval
, &rval
, ei
);
3192 VariantClear(&rval
);
3195 exprval_release(&exprval
);
3199 ret
->type
= EXPRVAL_VARIANT
;
3204 /* ECMA-262 3rd Edition 11.13.2 */
3205 HRESULT
assign_lshift_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3207 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3211 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, lshift_eval
, ei
, ret
);
3214 /* ECMA-262 3rd Edition 11.13.2 */
3215 HRESULT
assign_rshift_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3217 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3221 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, rshift_eval
, ei
, ret
);
3224 /* ECMA-262 3rd Edition 11.13.2 */
3225 HRESULT
assign_rrshift_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3227 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3231 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, rshift2_eval
, ei
, ret
);
3234 /* ECMA-262 3rd Edition 11.13.2 */
3235 HRESULT
assign_add_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3237 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3241 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, add_eval
, ei
, ret
);
3244 /* ECMA-262 3rd Edition 11.13.2 */
3245 HRESULT
assign_sub_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3247 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3251 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, sub_eval
, ei
, ret
);
3254 /* ECMA-262 3rd Edition 11.13.2 */
3255 HRESULT
assign_mul_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3257 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3261 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, mul_eval
, ei
, ret
);
3264 /* ECMA-262 3rd Edition 11.13.2 */
3265 HRESULT
assign_div_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3267 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3271 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, div_eval
, ei
, ret
);
3274 /* ECMA-262 3rd Edition 11.13.2 */
3275 HRESULT
assign_mod_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3277 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3281 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, mod_eval
, ei
, ret
);
3284 /* ECMA-262 3rd Edition 11.13.2 */
3285 HRESULT
assign_and_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3287 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3291 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, bitand_eval
, ei
, ret
);
3294 /* ECMA-262 3rd Edition 11.13.2 */
3295 HRESULT
assign_or_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3297 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3301 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, bitor_eval
, ei
, ret
);
3304 /* ECMA-262 3rd Edition 11.13.2 */
3305 HRESULT
assign_xor_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3307 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
3311 return assign_oper_eval(ctx
, expr
->expression1
, expr
->expression2
, xor_eval
, ei
, ret
);
3314 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
3322 static HRESULT
interp_tree(exec_ctx_t
*ctx
)
3324 instr_t
*instr
= ctx
->parser
->code
->instrs
+ctx
->ip
;
3331 hres
= expr_eval(ctx
->parser
->script
, instr
->arg1
.expr
, 0, &ctx
->ei
, &val
);
3335 hres
= exprval_to_value(ctx
->parser
->script
, &val
, &ctx
->ei
, &v
);
3339 return stack_push(ctx
, &v
);
3342 typedef HRESULT (*op_func_t
)(exec_ctx_t
*);
3344 static const op_func_t op_funcs
[] = {
3345 #define X(x,a,b) interp_##x,
3350 static const unsigned op_move
[] = {
3356 HRESULT
interp_expression_eval(script_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3358 exec_ctx_t
*exec_ctx
= ctx
->exec_ctx
;
3359 unsigned prev_ip
, prev_top
;
3361 HRESULT hres
= S_OK
;
3365 prev_top
= exec_ctx
->top
;
3366 prev_ip
= exec_ctx
->ip
;
3367 exec_ctx
->ip
= expr
->instr_off
;
3369 while(exec_ctx
->ip
!= -1) {
3370 op
= exec_ctx
->parser
->code
->instrs
[exec_ctx
->ip
].op
;
3371 hres
= op_funcs
[op
](exec_ctx
);
3374 exec_ctx
->ip
+= op_move
[op
];
3377 exec_ctx
->ip
= prev_ip
;
3380 stack_popn(exec_ctx
, exec_ctx
->top
-prev_top
);
3382 memset(&exec_ctx
->ei
, 0, sizeof(exec_ctx
->ei
));
3386 assert(exec_ctx
->top
== prev_top
+1);
3388 ret
->type
= EXPRVAL_VARIANT
;
3389 ret
->u
.var
= *stack_pop(exec_ctx
);
3393 HRESULT
compiled_expression_eval(script_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3399 hres
= compile_subscript(ctx
->exec_ctx
->parser
, expr
, &expr
->instr_off
);
3403 return (expr
->eval
= interp_expression_eval
)(ctx
, expr
, flags
, ei
, ret
);