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
35 static const WCHAR booleanW
[] = {'b','o','o','l','e','a','n',0};
36 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
37 static const WCHAR numberW
[] = {'n','u','m','b','e','r',0};
38 static const WCHAR objectW
[] = {'o','b','j','e','c','t',0};
39 static const WCHAR stringW
[] = {'s','t','r','i','n','g',0};
40 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
41 static const WCHAR unknownW
[] = {'u','n','k','n','o','w','n',0};
43 struct _return_type_t
{
53 static inline HRESULT
stat_eval(script_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
55 return stat
->eval(ctx
, stat
, rt
, ret
);
58 static inline HRESULT
expr_eval(script_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
60 return expr
->eval(ctx
, expr
, flags
, ei
, ret
);
63 static HRESULT
stack_push(exec_ctx_t
*ctx
, VARIANT
*v
)
65 if(!ctx
->stack_size
) {
66 ctx
->stack
= heap_alloc(16*sizeof(VARIANT
));
70 }else if(ctx
->stack_size
== ctx
->top
) {
73 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(VARIANT
));
79 ctx
->stack
= new_stack
;
83 ctx
->stack
[ctx
->top
++] = *v
;
87 static HRESULT
stack_push_bool(exec_ctx_t
*ctx
, BOOL b
)
92 V_BOOL(&v
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
93 return stack_push(ctx
, &v
);
96 static inline HRESULT
stack_push_number(exec_ctx_t
*ctx
, double number
)
100 num_set_val(&v
, number
);
101 return stack_push(ctx
, &v
);
104 static inline HRESULT
stack_push_int(exec_ctx_t
*ctx
, INT n
)
110 return stack_push(ctx
, &v
);
113 static inline HRESULT
stack_push_string(exec_ctx_t
*ctx
, const WCHAR
*str
)
118 V_BSTR(&v
) = SysAllocString(str
);
119 return V_BSTR(&v
) ? stack_push(ctx
, &v
) : E_OUTOFMEMORY
;
122 static HRESULT
stack_push_objid(exec_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
)
127 V_VT(&v
) = VT_DISPATCH
;
128 V_DISPATCH(&v
) = disp
;
129 hres
= stack_push(ctx
, &v
);
135 return stack_push(ctx
, &v
);
138 static inline VARIANT
*stack_top(exec_ctx_t
*ctx
)
141 return ctx
->stack
+ ctx
->top
-1;
144 static inline VARIANT
*stack_topn(exec_ctx_t
*ctx
, unsigned n
)
146 assert(ctx
->top
> n
);
147 return ctx
->stack
+ ctx
->top
-1-n
;
150 static inline VARIANT
*stack_pop(exec_ctx_t
*ctx
)
153 return ctx
->stack
+ --ctx
->top
;
156 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
159 VariantClear(stack_pop(ctx
));
162 static HRESULT
stack_pop_number(exec_ctx_t
*ctx
, VARIANT
*r
)
168 hres
= to_number(ctx
->parser
->script
, v
, &ctx
->ei
, r
);
173 static HRESULT
stack_pop_object(exec_ctx_t
*ctx
, IDispatch
**r
)
179 if(V_VT(v
) == VT_DISPATCH
) {
181 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_OBJECT_REQUIRED
, NULL
);
186 hres
= to_object(ctx
->parser
->script
, v
, r
);
191 static inline HRESULT
stack_pop_int(exec_ctx_t
*ctx
, INT
*r
)
193 return to_int32(ctx
->parser
->script
, stack_pop(ctx
), &ctx
->ei
, r
);
196 static inline HRESULT
stack_pop_uint(exec_ctx_t
*ctx
, DWORD
*r
)
198 return to_uint32(ctx
->parser
->script
, stack_pop(ctx
), &ctx
->ei
, r
);
201 static inline IDispatch
*stack_pop_objid(exec_ctx_t
*ctx
, DISPID
*id
)
203 assert(V_VT(stack_top(ctx
)) == VT_INT
&& V_VT(stack_topn(ctx
, 1)) == VT_DISPATCH
);
205 *id
= V_INT(stack_pop(ctx
));
206 return V_DISPATCH(stack_pop(ctx
));
209 static inline IDispatch
*stack_topn_objid(exec_ctx_t
*ctx
, unsigned n
, DISPID
*id
)
211 assert(V_VT(stack_topn(ctx
, n
)) == VT_INT
&& V_VT(stack_topn(ctx
, n
+1)) == VT_DISPATCH
);
213 *id
= V_INT(stack_topn(ctx
, n
));
214 return V_DISPATCH(stack_topn(ctx
, n
+1));
217 static void exprval_release(exprval_t
*val
)
220 case EXPRVAL_VARIANT
:
221 if(V_VT(&val
->u
.var
) != VT_EMPTY
)
222 VariantClear(&val
->u
.var
);
225 if(val
->u
.idref
.disp
)
226 IDispatch_Release(val
->u
.idref
.disp
);
228 case EXPRVAL_INVALID
:
229 SysFreeString(val
->u
.identifier
);
233 /* ECMA-262 3rd Edition 8.7.1 */
234 static HRESULT
exprval_value(script_ctx_t
*ctx
, exprval_t
*val
, jsexcept_t
*ei
, VARIANT
*ret
)
236 V_VT(ret
) = VT_EMPTY
;
239 case EXPRVAL_VARIANT
:
240 return VariantCopy(ret
, &val
->u
.var
);
242 if(!val
->u
.idref
.disp
) {
243 FIXME("throw ReferenceError\n");
247 return disp_propget(ctx
, val
->u
.idref
.disp
, val
->u
.idref
.id
, ret
, ei
, NULL
/*FIXME*/);
248 case EXPRVAL_INVALID
:
249 return throw_type_error(ctx
, ei
, JS_E_UNDEFINED_VARIABLE
, val
->u
.identifier
);
252 ERR("type %d\n", val
->type
);
256 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*val
, jsexcept_t
*ei
, VARIANT
*ret
)
258 if(val
->type
== EXPRVAL_VARIANT
) {
260 V_VT(&val
->u
.var
) = VT_EMPTY
;
264 return exprval_value(ctx
, val
, ei
, ret
);
267 static HRESULT
exprval_to_boolean(script_ctx_t
*ctx
, exprval_t
*exprval
, jsexcept_t
*ei
, VARIANT_BOOL
*b
)
269 if(exprval
->type
!= EXPRVAL_VARIANT
) {
273 hres
= exprval_to_value(ctx
, exprval
, ei
, &val
);
277 hres
= to_boolean(&val
, b
);
282 return to_boolean(&exprval
->u
.var
, b
);
285 static void exprval_init(exprval_t
*val
)
287 val
->type
= EXPRVAL_VARIANT
;
288 V_VT(&val
->u
.var
) = VT_EMPTY
;
291 static void exprval_set_idref(exprval_t
*val
, IDispatch
*disp
, DISPID id
)
293 val
->type
= EXPRVAL_IDREF
;
294 val
->u
.idref
.disp
= disp
;
295 val
->u
.idref
.id
= id
;
298 IDispatch_AddRef(disp
);
301 HRESULT
scope_push(scope_chain_t
*scope
, jsdisp_t
*obj
, scope_chain_t
**ret
)
303 scope_chain_t
*new_scope
;
305 new_scope
= heap_alloc(sizeof(scope_chain_t
));
307 return E_OUTOFMEMORY
;
312 new_scope
->obj
= obj
;
316 new_scope
->next
= scope
;
318 new_scope
->next
= NULL
;
325 static void scope_pop(scope_chain_t
**scope
)
334 void scope_release(scope_chain_t
*scope
)
340 scope_release(scope
->next
);
342 jsdisp_release(scope
->obj
);
346 HRESULT
create_exec_ctx(script_ctx_t
*script_ctx
, IDispatch
*this_obj
, jsdisp_t
*var_disp
,
347 scope_chain_t
*scope
, BOOL is_global
, exec_ctx_t
**ret
)
351 ctx
= heap_alloc_zero(sizeof(exec_ctx_t
));
353 return E_OUTOFMEMORY
;
356 ctx
->is_global
= is_global
;
359 ctx
->this_obj
= this_obj
;
360 else if(script_ctx
->host_global
)
361 ctx
->this_obj
= script_ctx
->host_global
;
363 ctx
->this_obj
= to_disp(script_ctx
->global
);
364 IDispatch_AddRef(ctx
->this_obj
);
366 jsdisp_addref(var_disp
);
367 ctx
->var_disp
= var_disp
;
371 ctx
->scope_chain
= scope
;
378 void exec_release(exec_ctx_t
*ctx
)
384 scope_release(ctx
->scope_chain
);
386 jsdisp_release(ctx
->var_disp
);
388 IDispatch_Release(ctx
->this_obj
);
389 heap_free(ctx
->stack
);
393 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, BSTR name
, DWORD flags
, DISPID
*id
)
398 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
400 TRACE("unsing IDispatch\n");
403 return IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &name
, 1, 0, id
);
407 hres
= IDispatchEx_GetDispID(dispex
, name
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
408 IDispatchEx_Release(dispex
);
412 /* ECMA-262 3rd Edition 8.7.2 */
413 static HRESULT
put_value(script_ctx_t
*ctx
, exprval_t
*ref
, VARIANT
*v
, jsexcept_t
*ei
)
415 if(ref
->type
!= EXPRVAL_IDREF
)
416 return throw_reference_error(ctx
, ei
, JS_E_ILLEGAL_ASSIGN
, NULL
);
418 return disp_propput(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, v
, ei
, NULL
/*FIXME*/);
421 static inline BOOL
is_null(const VARIANT
*v
)
423 return V_VT(v
) == VT_NULL
|| (V_VT(v
) == VT_DISPATCH
&& !V_DISPATCH(v
));
426 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
428 IObjectIdentity
*identity
;
429 IUnknown
*unk1
, *unk2
;
437 if(!disp1
|| !disp2
) {
442 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
446 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
448 IUnknown_Release(unk1
);
455 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
456 if(SUCCEEDED(hres
)) {
457 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
458 IObjectIdentity_Release(identity
);
465 IUnknown_Release(unk1
);
466 IUnknown_Release(unk2
);
470 /* ECMA-262 3rd Edition 11.9.6 */
471 static HRESULT
equal2_values(VARIANT
*lval
, VARIANT
*rval
, BOOL
*ret
)
475 if(V_VT(lval
) != V_VT(rval
)) {
476 if(is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
)))
477 *ret
= num_val(lval
) == num_val(rval
);
478 else if(is_null(lval
))
479 *ret
= is_null(rval
);
491 *ret
= V_I4(lval
) == V_I4(rval
);
494 *ret
= V_R8(lval
) == V_R8(rval
);
498 *ret
= SysStringLen(V_BSTR(rval
))?FALSE
:TRUE
;
499 else if(!V_BSTR(rval
))
500 *ret
= SysStringLen(V_BSTR(lval
))?FALSE
:TRUE
;
502 *ret
= !strcmpW(V_BSTR(lval
), V_BSTR(rval
));
505 return disp_cmp(V_DISPATCH(lval
), V_DISPATCH(rval
), ret
);
507 *ret
= !V_BOOL(lval
) == !V_BOOL(rval
);
510 FIXME("unimplemented vt %d\n", V_VT(lval
));
517 static HRESULT
literal_to_var(script_ctx_t
*ctx
, literal_t
*literal
, VARIANT
*v
)
519 switch(literal
->type
) {
525 V_I4(v
) = literal
->u
.lval
;
529 V_R8(v
) = literal
->u
.dval
;
532 BSTR str
= SysAllocString(literal
->u
.wstr
);
534 return E_OUTOFMEMORY
;
542 V_BOOL(v
) = literal
->u
.bval
;
548 hres
= create_regexp(ctx
, literal
->u
.regexp
.str
, literal
->u
.regexp
.str_len
,
549 literal
->u
.regexp
.flags
, ®exp
);
553 var_set_jsdisp(v
, regexp
);
560 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
566 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
567 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
568 hres
= disp_get_id(ctx
, item
->disp
, identifier
, 0, &id
);
569 if(SUCCEEDED(hres
)) {
571 exprval_set_idref(ret
, item
->disp
, id
);
580 HRESULT
exec_source(exec_ctx_t
*ctx
, parser_ctx_t
*parser
, source_elements_t
*source
, BOOL from_eval
,
581 jsexcept_t
*ei
, VARIANT
*retv
)
583 script_ctx_t
*script
= parser
->script
;
584 function_declaration_t
*func
;
585 parser_ctx_t
*prev_parser
;
589 exec_ctx_t
*prev_ctx
;
593 for(func
= source
->functions
; func
; func
= func
->next
) {
597 hres
= create_source_function(parser
, func
->expr
->parameter_list
, func
->expr
->source_elements
,
598 ctx
->scope_chain
, func
->expr
->src_str
, func
->expr
->src_len
, &func_obj
);
602 var_set_jsdisp(&var
, func_obj
);
603 hres
= jsdisp_propput_name(ctx
->var_disp
, func
->expr
->identifier
, &var
, ei
, NULL
);
604 jsdisp_release(func_obj
);
609 for(var
= source
->variables
; var
; var
= var
->next
) {
613 name
= SysAllocString(var
->identifier
);
615 return E_OUTOFMEMORY
;
617 if(!ctx
->is_global
|| !lookup_global_members(parser
->script
, name
, NULL
))
618 hres
= jsdisp_get_id(ctx
->var_disp
, var
->identifier
, fdexNameEnsure
, &id
);
624 prev_ctx
= script
->exec_ctx
;
625 script
->exec_ctx
= ctx
;
627 prev_parser
= ctx
->parser
;
628 ctx
->parser
= parser
;
630 V_VT(&val
) = VT_EMPTY
;
631 memset(&rt
, 0, sizeof(rt
));
634 for(stat
= source
->statement
; stat
; stat
= stat
->next
) {
635 hres
= stat_eval(script
, stat
, &rt
, &tmp
);
641 if(rt
.type
!= RT_NORMAL
)
645 script
->exec_ctx
= prev_ctx
;
646 ctx
->parser
= prev_parser
;
648 if(rt
.type
!= RT_NORMAL
&& rt
.type
!= RT_RETURN
) {
649 FIXME("wrong rt %d\n", rt
.type
);
659 if(!retv
|| (!from_eval
&& rt
.type
!= RT_RETURN
))
666 /* ECMA-262 3rd Edition 10.1.4 */
667 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
669 scope_chain_t
*scope
;
674 TRACE("%s\n", debugstr_w(identifier
));
676 for(scope
= ctx
->exec_ctx
->scope_chain
; scope
; scope
= scope
->next
) {
677 hres
= jsdisp_get_id(scope
->obj
, identifier
, 0, &id
);
678 if(SUCCEEDED(hres
)) {
679 exprval_set_idref(ret
, to_disp(scope
->obj
), id
);
684 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
685 if(SUCCEEDED(hres
)) {
686 exprval_set_idref(ret
, to_disp(ctx
->global
), id
);
690 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
691 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
698 hres
= IActiveScriptSite_GetItemInfo(ctx
->site
, identifier
,
699 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
701 WARN("GetItemInfo failed: %08x\n", hres
);
705 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
706 IUnknown_Release(unk
);
708 WARN("object does not implement IDispatch\n");
713 ret
->type
= EXPRVAL_VARIANT
;
714 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
715 V_DISPATCH(&ret
->u
.var
) = item
->disp
;
716 IDispatch_AddRef(item
->disp
);
721 if(lookup_global_members(ctx
, identifier
, ret
))
724 if(flags
& EXPR_NEWREF
) {
725 hres
= jsdisp_get_id(ctx
->global
, identifier
, fdexNameEnsure
, &id
);
729 exprval_set_idref(ret
, to_disp(ctx
->global
), id
);
733 ret
->type
= EXPRVAL_INVALID
;
734 ret
->u
.identifier
= SysAllocString(identifier
);
735 if(!ret
->u
.identifier
)
736 return E_OUTOFMEMORY
;
741 /* ECMA-262 3rd Edition 12.1 */
742 HRESULT
block_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
744 block_statement_t
*stat
= (block_statement_t
*)_stat
;
751 V_VT(&val
) = VT_EMPTY
;
752 for(iter
= stat
->stat_list
; iter
; iter
= iter
->next
) {
753 hres
= stat_eval(ctx
, iter
, rt
, &tmp
);
759 if(rt
->type
!= RT_NORMAL
)
772 /* ECMA-262 3rd Edition 12.2 */
773 static HRESULT
variable_list_eval(script_ctx_t
*ctx
, variable_declaration_t
*var_list
, jsexcept_t
*ei
)
775 variable_declaration_t
*iter
;
778 for(iter
= var_list
; iter
; iter
= iter
->next
) {
785 hres
= expr_eval(ctx
, iter
->expr
, 0, ei
, &exprval
);
789 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
790 exprval_release(&exprval
);
794 hres
= jsdisp_propput_name(ctx
->exec_ctx
->var_disp
, iter
->identifier
, &val
, ei
, NULL
/*FIXME*/);
803 /* ECMA-262 3rd Edition 12.2 */
804 HRESULT
var_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
806 var_statement_t
*stat
= (var_statement_t
*)_stat
;
811 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
815 V_VT(ret
) = VT_EMPTY
;
819 /* ECMA-262 3rd Edition 12.3 */
820 HRESULT
empty_statement_eval(script_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
824 V_VT(ret
) = VT_EMPTY
;
828 /* ECMA-262 3rd Edition 12.4 */
829 HRESULT
expression_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
831 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
838 hres
= expr_eval(ctx
, stat
->expr
, EXPR_NOVAL
, &rt
->ei
, &exprval
);
842 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
843 exprval_release(&exprval
);
848 TRACE("= %s\n", debugstr_variant(ret
));
852 /* ECMA-262 3rd Edition 12.5 */
853 HRESULT
if_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
855 if_statement_t
*stat
= (if_statement_t
*)_stat
;
862 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
866 hres
= exprval_to_boolean(ctx
, &exprval
, &rt
->ei
, &b
);
867 exprval_release(&exprval
);
872 hres
= stat_eval(ctx
, stat
->if_stat
, rt
, ret
);
873 else if(stat
->else_stat
)
874 hres
= stat_eval(ctx
, stat
->else_stat
, rt
, ret
);
876 V_VT(ret
) = VT_EMPTY
;
881 /* ECMA-262 3rd Edition 12.6.2 */
882 HRESULT
while_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
884 while_statement_t
*stat
= (while_statement_t
*)_stat
;
893 V_VT(&val
) = VT_EMPTY
;
894 test_expr
= !stat
->do_while
;
898 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
902 hres
= exprval_to_boolean(ctx
, &exprval
, &rt
->ei
, &b
);
903 exprval_release(&exprval
);
904 if(FAILED(hres
) || !b
)
910 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
917 if(rt
->type
== RT_CONTINUE
)
918 rt
->type
= RT_NORMAL
;
919 if(rt
->type
!= RT_NORMAL
)
928 if(rt
->type
== RT_BREAK
)
929 rt
->type
= RT_NORMAL
;
935 /* ECMA-262 3rd Edition 12.6.3 */
936 HRESULT
for_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
938 for_statement_t
*stat
= (for_statement_t
*)_stat
;
939 VARIANT val
, tmp
, retv
;
946 if(stat
->variable_list
) {
947 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
950 }else if(stat
->begin_expr
) {
951 hres
= expr_eval(ctx
, stat
->begin_expr
, 0, &rt
->ei
, &exprval
);
955 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
956 exprval_release(&exprval
);
963 V_VT(&retv
) = VT_EMPTY
;
967 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
971 hres
= exprval_to_boolean(ctx
, &exprval
, &rt
->ei
, &b
);
972 exprval_release(&exprval
);
973 if(FAILED(hres
) || !b
)
977 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
984 if(rt
->type
== RT_CONTINUE
)
985 rt
->type
= RT_NORMAL
;
986 else if(rt
->type
!= RT_NORMAL
)
990 hres
= expr_eval(ctx
, stat
->end_expr
, 0, &rt
->ei
, &exprval
);
994 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
995 exprval_release(&exprval
);
1004 VariantClear(&retv
);
1008 if(rt
->type
== RT_BREAK
)
1009 rt
->type
= RT_NORMAL
;
1015 /* ECMA-262 3rd Edition 12.6.4 */
1016 HRESULT
forin_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1018 forin_statement_t
*stat
= (forin_statement_t
*)_stat
;
1019 VARIANT val
, name
, retv
, tmp
;
1020 DISPID id
= DISPID_STARTENUM
;
1021 BSTR str
, identifier
= NULL
;
1022 IDispatchEx
*in_obj
;
1028 if(stat
->variable
) {
1029 hres
= variable_list_eval(ctx
, stat
->variable
, &rt
->ei
);
1034 hres
= expr_eval(ctx
, stat
->in_expr
, 0, &rt
->ei
, &exprval
);
1038 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
1039 exprval_release(&exprval
);
1043 if(V_VT(&val
) != VT_DISPATCH
) {
1044 TRACE("in vt %d\n", V_VT(&val
));
1046 V_VT(ret
) = VT_EMPTY
;
1050 hres
= IDispatch_QueryInterface(V_DISPATCH(&val
), &IID_IDispatchEx
, (void**)&in_obj
);
1051 IDispatch_Release(V_DISPATCH(&val
));
1053 TRACE("Object doesn't support IDispatchEx\n");
1054 V_VT(ret
) = VT_EMPTY
;
1058 V_VT(&retv
) = VT_EMPTY
;
1061 identifier
= SysAllocString(stat
->variable
->identifier
);
1064 hres
= IDispatchEx_GetNextDispID(in_obj
, fdexEnumDefault
, id
, &id
);
1065 if(FAILED(hres
) || hres
== S_FALSE
)
1068 hres
= IDispatchEx_GetMemberName(in_obj
, id
, &str
);
1072 TRACE("iter %s\n", debugstr_w(str
));
1075 hres
= identifier_eval(ctx
, identifier
, 0, NULL
, &exprval
);
1077 hres
= expr_eval(ctx
, stat
->expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
1078 if(SUCCEEDED(hres
)) {
1079 V_VT(&name
) = VT_BSTR
;
1080 V_BSTR(&name
) = str
;
1081 hres
= put_value(ctx
, &exprval
, &name
, &rt
->ei
);
1082 exprval_release(&exprval
);
1088 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
1092 VariantClear(&retv
);
1095 if(rt
->type
== RT_CONTINUE
)
1096 rt
->type
= RT_NORMAL
;
1097 else if(rt
->type
!= RT_NORMAL
)
1101 SysFreeString(identifier
);
1102 IDispatchEx_Release(in_obj
);
1104 VariantClear(&retv
);
1108 if(rt
->type
== RT_BREAK
)
1109 rt
->type
= RT_NORMAL
;
1115 /* ECMA-262 3rd Edition 12.7 */
1116 HRESULT
continue_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1118 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
1122 if(stat
->identifier
) {
1123 FIXME("indentifier not implemented\n");
1127 rt
->type
= RT_CONTINUE
;
1128 V_VT(ret
) = VT_EMPTY
;
1132 /* ECMA-262 3rd Edition 12.8 */
1133 HRESULT
break_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1135 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
1139 if(stat
->identifier
) {
1140 FIXME("indentifier not implemented\n");
1144 rt
->type
= RT_BREAK
;
1145 V_VT(ret
) = VT_EMPTY
;
1149 /* ECMA-262 3rd Edition 12.9 */
1150 HRESULT
return_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1152 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
1160 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1164 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, ret
);
1165 exprval_release(&exprval
);
1169 V_VT(ret
) = VT_EMPTY
;
1172 TRACE("= %s\n", debugstr_variant(ret
));
1173 rt
->type
= RT_RETURN
;
1177 /* ECMA-262 3rd Edition 12.10 */
1178 HRESULT
with_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1180 with_statement_t
*stat
= (with_statement_t
*)_stat
;
1189 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1193 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
1194 exprval_release(&exprval
);
1198 hres
= to_object(ctx
, &val
, &disp
);
1203 obj
= iface_to_jsdisp((IUnknown
*)disp
);
1204 IDispatch_Release(disp
);
1206 FIXME("disp id not jsdisp\n");
1210 hres
= scope_push(ctx
->exec_ctx
->scope_chain
, obj
, &ctx
->exec_ctx
->scope_chain
);
1211 jsdisp_release(obj
);
1215 hres
= stat_eval(ctx
, stat
->statement
, rt
, ret
);
1217 scope_pop(&ctx
->exec_ctx
->scope_chain
);
1221 /* ECMA-262 3rd Edition 12.12 */
1222 HRESULT
labelled_statement_eval(script_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
1228 /* ECMA-262 3rd Edition 12.13 */
1229 HRESULT
switch_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1231 switch_statement_t
*stat
= (switch_statement_t
*)_stat
;
1232 case_clausule_t
*iter
, *default_clausule
= NULL
;
1233 statement_t
*stat_iter
;
1241 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1245 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
1246 exprval_release(&exprval
);
1250 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1252 default_clausule
= iter
;
1256 hres
= expr_eval(ctx
, iter
->expr
, 0, &rt
->ei
, &exprval
);
1260 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &cval
);
1261 exprval_release(&exprval
);
1265 hres
= equal2_values(&val
, &cval
, &b
);
1266 VariantClear(&cval
);
1267 if(FAILED(hres
) || b
)
1276 iter
= default_clausule
;
1278 V_VT(&val
) = VT_EMPTY
;
1282 for(stat_iter
= iter
->stat
; stat_iter
; stat_iter
= stat_iter
->next
) {
1283 hres
= stat_eval(ctx
, stat_iter
, rt
, &tmp
);
1290 if(rt
->type
!= RT_NORMAL
)
1300 if(rt
->type
== RT_BREAK
)
1301 rt
->type
= RT_NORMAL
;
1307 /* ECMA-262 3rd Edition 12.13 */
1308 HRESULT
throw_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1310 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
1317 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1321 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
1322 exprval_release(&exprval
);
1327 return DISP_E_EXCEPTION
;
1330 static HRESULT
interp_throw(exec_ctx_t
*ctx
)
1332 const HRESULT arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1334 TRACE("%08x\n", arg
);
1336 return throw_reference_error(ctx
->parser
->script
, &ctx
->ei
, arg
, NULL
);
1339 static HRESULT
interp_throw_type(exec_ctx_t
*ctx
)
1341 const HRESULT hres
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1342 const WCHAR
*str
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.str
;
1344 TRACE("%08x %s\n", hres
, debugstr_w(str
));
1346 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, hres
, str
);
1349 /* ECMA-262 3rd Edition 12.14 */
1350 static HRESULT
catch_eval(script_ctx_t
*ctx
, catch_block_t
*block
, return_type_t
*rt
, VARIANT
*ret
)
1357 memset(&rt
->ei
, 0, sizeof(jsexcept_t
));
1359 hres
= create_dispex(ctx
, NULL
, NULL
, &var_disp
);
1360 if(SUCCEEDED(hres
)) {
1361 hres
= jsdisp_propput_name(var_disp
, block
->identifier
, &ex
, &rt
->ei
, NULL
/*FIXME*/);
1362 if(SUCCEEDED(hres
)) {
1363 hres
= scope_push(ctx
->exec_ctx
->scope_chain
, var_disp
, &ctx
->exec_ctx
->scope_chain
);
1364 if(SUCCEEDED(hres
)) {
1365 hres
= stat_eval(ctx
, block
->statement
, rt
, &val
);
1366 scope_pop(&ctx
->exec_ctx
->scope_chain
);
1370 jsdisp_release(var_disp
);
1381 /* ECMA-262 3rd Edition 12.14 */
1382 HRESULT
try_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1384 try_statement_t
*stat
= (try_statement_t
*)_stat
;
1390 hres
= stat_eval(ctx
, stat
->try_statement
, rt
, &val
);
1392 TRACE("EXCEPTION\n");
1393 if(!stat
->catch_block
)
1396 hres
= catch_eval(ctx
, stat
->catch_block
, rt
, &val
);
1401 if(stat
->finally_statement
) {
1403 hres
= stat_eval(ctx
, stat
->finally_statement
, rt
, &val
);
1412 /* ECMA-262 3rd Edition 13 */
1413 HRESULT
function_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1415 function_expression_t
*expr
= (function_expression_t
*)_expr
;
1421 if(expr
->identifier
) {
1422 hres
= jsdisp_propget_name(ctx
->exec_ctx
->var_disp
, expr
->identifier
, &var
, ei
, NULL
/*FIXME*/);
1428 hres
= create_source_function(ctx
->exec_ctx
->parser
, expr
->parameter_list
, expr
->source_elements
, ctx
->exec_ctx
->scope_chain
,
1429 expr
->src_str
, expr
->src_len
, &dispex
);
1433 var_set_jsdisp(&var
, dispex
);
1436 ret
->type
= EXPRVAL_VARIANT
;
1441 /* ECMA-262 3rd Edition 11.2.1 */
1442 HRESULT
array_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1444 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1446 VARIANT member
, val
;
1449 IDispatch
*obj
= NULL
;
1454 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1458 hres
= exprval_to_value(ctx
, &exprval
, ei
, &member
);
1459 exprval_release(&exprval
);
1463 hres
= expr_eval(ctx
, expr
->expression2
, EXPR_NEWREF
, ei
, &exprval
);
1464 if(SUCCEEDED(hres
)) {
1465 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1466 exprval_release(&exprval
);
1469 if(SUCCEEDED(hres
)) {
1470 hres
= to_object(ctx
, &member
, &obj
);
1474 VariantClear(&member
);
1475 if(SUCCEEDED(hres
)) {
1476 hres
= to_string(ctx
, &val
, ei
, &str
);
1478 if(SUCCEEDED(hres
)) {
1479 hres
= disp_get_id(ctx
, obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1483 if(SUCCEEDED(hres
)) {
1484 exprval_set_idref(ret
, obj
, id
);
1485 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1490 IDispatch_Release(obj
);
1496 /* ECMA-262 3rd Edition 11.2.1 */
1497 static HRESULT
interp_array(exec_ctx_t
*ctx
)
1507 namev
= stack_pop(ctx
);
1509 hres
= stack_pop_object(ctx
, &obj
);
1511 VariantClear(namev
);
1515 hres
= to_string(ctx
->parser
->script
, namev
, &ctx
->ei
, &name
);
1516 VariantClear(namev
);
1518 IDispatch_Release(obj
);
1522 hres
= disp_get_id(ctx
->parser
->script
, obj
, name
, 0, &id
);
1523 SysFreeString(name
);
1524 if(SUCCEEDED(hres
)) {
1525 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
1526 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1527 V_VT(&v
) = VT_EMPTY
;
1530 IDispatch_Release(obj
);
1534 return stack_push(ctx
, &v
);
1537 /* ECMA-262 3rd Edition 11.2.1 */
1538 HRESULT
member_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1540 member_expression_t
*expr
= (member_expression_t
*)_expr
;
1541 IDispatch
*obj
= NULL
;
1550 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1554 hres
= exprval_to_value(ctx
, &exprval
, ei
, &member
);
1555 exprval_release(&exprval
);
1559 hres
= to_object(ctx
, &member
, &obj
);
1560 VariantClear(&member
);
1564 str
= SysAllocString(expr
->identifier
);
1566 IDispatch_Release(obj
);
1567 return E_OUTOFMEMORY
;
1570 hres
= disp_get_id(ctx
, obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1572 if(SUCCEEDED(hres
)) {
1573 exprval_set_idref(ret
, obj
, id
);
1574 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1579 IDispatch_Release(obj
);
1583 /* ECMA-262 3rd Edition 11.2.1 */
1584 static HRESULT
interp_member(exec_ctx_t
*ctx
)
1586 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
1594 hres
= stack_pop_object(ctx
, &obj
);
1598 hres
= disp_get_id(ctx
->parser
->script
, obj
, arg
, 0, &id
);
1599 if(SUCCEEDED(hres
)) {
1600 V_VT(&v
) = VT_EMPTY
;
1601 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
1602 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1603 V_VT(&v
) = VT_EMPTY
;
1606 IDispatch_Release(obj
);
1610 return stack_push(ctx
, &v
);
1613 /* ECMA-262 3rd Edition 11.2.1 */
1614 static HRESULT
interp_memberid(exec_ctx_t
*ctx
)
1616 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1617 VARIANT
*objv
, *namev
;
1625 namev
= stack_pop(ctx
);
1626 objv
= stack_pop(ctx
);
1628 hres
= to_object(ctx
->parser
->script
, objv
, &obj
);
1630 if(SUCCEEDED(hres
)) {
1631 hres
= to_string(ctx
->parser
->script
, namev
, &ctx
->ei
, &name
);
1633 IDispatch_Release(obj
);
1635 VariantClear(namev
);
1639 hres
= disp_get_id(ctx
->parser
->script
, obj
, name
, arg
, &id
);
1640 SysFreeString(name
);
1642 IDispatch_Release(obj
);
1643 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
1645 id
= JS_E_INVALID_PROPERTY
;
1651 return stack_push_objid(ctx
, obj
, id
);
1654 /* ECMA-262 3rd Edition 11.2.1 */
1655 static HRESULT
interp_refval(exec_ctx_t
*ctx
)
1664 disp
= stack_topn_objid(ctx
, 0, &id
);
1666 return throw_reference_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_ILLEGAL_ASSIGN
, NULL
);
1668 hres
= disp_propget(ctx
->parser
->script
, disp
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
1672 return stack_push(ctx
, &v
);
1675 static void jsstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, DISPPARAMS
*dp
)
1680 dp
->cArgs
= arg_cnt
;
1681 dp
->rgdispidNamedArgs
= NULL
;
1684 assert(ctx
->top
>= arg_cnt
);
1686 for(i
=1; i
*2 <= arg_cnt
; i
++) {
1687 tmp
= ctx
->stack
[ctx
->top
-i
];
1688 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
1689 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
1692 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-arg_cnt
;
1695 /* ECMA-262 3rd Edition 11.2.2 */
1696 static HRESULT
interp_new(exec_ctx_t
*ctx
)
1698 const LONG arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1705 constr
= stack_topn(ctx
, arg
);
1707 /* NOTE: Should use to_object here */
1709 if(V_VT(constr
) == VT_NULL
)
1710 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
1711 else if(V_VT(constr
) != VT_DISPATCH
)
1712 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_INVALID_ACTION
, NULL
);
1713 else if(!V_DISPATCH(constr
))
1714 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_INVALID_PROPERTY
, NULL
);
1716 jsstack_to_dp(ctx
, arg
, &dp
);
1717 hres
= disp_call(ctx
->parser
->script
, V_DISPATCH(constr
), DISPID_VALUE
,
1718 DISPATCH_CONSTRUCT
, &dp
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
1722 stack_popn(ctx
, arg
+1);
1723 return stack_push(ctx
, &v
);
1726 /* ECMA-262 3rd Edition 11.2.3 */
1727 static HRESULT
interp_call(exec_ctx_t
*ctx
)
1729 const unsigned argn
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1730 const int do_ret
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.lng
;
1735 TRACE("%d %d\n", argn
, do_ret
);
1737 objv
= stack_topn(ctx
, argn
);
1738 if(V_VT(objv
) != VT_DISPATCH
)
1739 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_INVALID_PROPERTY
, NULL
);
1741 jsstack_to_dp(ctx
, argn
, &dp
);
1742 hres
= disp_call(ctx
->parser
->script
, V_DISPATCH(objv
), DISPID_VALUE
, DISPATCH_METHOD
, &dp
,
1743 do_ret
? &v
: NULL
, &ctx
->ei
, NULL
/*FIXME*/);
1747 stack_popn(ctx
, argn
+1);
1748 return do_ret
? stack_push(ctx
, &v
) : S_OK
;
1752 /* ECMA-262 3rd Edition 11.2.3 */
1753 static HRESULT
interp_call_member(exec_ctx_t
*ctx
)
1755 const unsigned argn
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1756 const int do_ret
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.lng
;
1763 TRACE("%d %d\n", argn
, do_ret
);
1765 obj
= stack_topn_objid(ctx
, argn
, &id
);
1767 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, id
, NULL
);
1769 jsstack_to_dp(ctx
, argn
, &dp
);
1770 hres
= disp_call(ctx
->parser
->script
, obj
, id
, DISPATCH_METHOD
, &dp
, do_ret
? &v
: NULL
, &ctx
->ei
, NULL
/*FIXME*/);
1774 stack_popn(ctx
, argn
+2);
1775 return do_ret
? stack_push(ctx
, &v
) : S_OK
;
1779 /* ECMA-262 3rd Edition 11.1.1 */
1780 static HRESULT
interp_this(exec_ctx_t
*ctx
)
1786 V_VT(&v
) = VT_DISPATCH
;
1787 V_DISPATCH(&v
) = ctx
->this_obj
;
1788 IDispatch_AddRef(ctx
->this_obj
);
1789 return stack_push(ctx
, &v
);
1792 /* ECMA-262 3rd Edition 10.1.4 */
1793 HRESULT
identifier_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1795 identifier_expression_t
*expr
= (identifier_expression_t
*)_expr
;
1801 identifier
= SysAllocString(expr
->identifier
);
1803 return E_OUTOFMEMORY
;
1805 hres
= identifier_eval(ctx
, identifier
, flags
, ei
, ret
);
1807 SysFreeString(identifier
);
1811 /* ECMA-262 3rd Edition 10.1.4 */
1812 static HRESULT
interp_ident(exec_ctx_t
*ctx
)
1814 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
1819 TRACE("%s\n", debugstr_w(arg
));
1821 hres
= identifier_eval(ctx
->parser
->script
, arg
, 0, &ctx
->ei
, &exprval
);
1825 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &ctx
->ei
, &v
);
1826 exprval_release(&exprval
);
1830 return stack_push(ctx
, &v
);
1833 /* ECMA-262 3rd Edition 10.1.4 */
1834 static HRESULT
interp_identid(exec_ctx_t
*ctx
)
1836 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
1837 const unsigned flags
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.uint
;
1841 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1843 hres
= identifier_eval(ctx
->parser
->script
, arg
, (flags
&fdexNameEnsure
) ? EXPR_NEWREF
: 0, &ctx
->ei
, &exprval
);
1847 if(exprval
.type
!= EXPRVAL_IDREF
) {
1848 WARN("invalid ref\n");
1849 exprval_release(&exprval
);
1850 return stack_push_objid(ctx
, NULL
, JS_E_OBJECT_EXPECTED
);
1853 return stack_push_objid(ctx
, exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
);
1856 /* ECMA-262 3rd Edition 7.8.1 */
1857 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1864 return stack_push(ctx
, &v
);
1867 /* ECMA-262 3rd Edition 7.8.2 */
1868 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1870 const LONG arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1872 TRACE("%s\n", arg
? "true" : "false");
1874 return stack_push_bool(ctx
, arg
);
1877 /* ECMA-262 3rd Edition 7.8.3 */
1878 static HRESULT
interp_int(exec_ctx_t
*ctx
)
1880 const LONG arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1887 return stack_push(ctx
, &v
);
1890 /* ECMA-262 3rd Edition 7.8.3 */
1891 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1893 const double arg
= *ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.dbl
;
1896 TRACE("%lf\n", arg
);
1900 return stack_push(ctx
, &v
);
1903 /* ECMA-262 3rd Edition 7.8.4 */
1904 static HRESULT
interp_str(exec_ctx_t
*ctx
)
1906 const WCHAR
*str
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.str
;
1909 TRACE("%s\n", debugstr_w(str
));
1912 V_BSTR(&v
) = SysAllocString(str
);
1914 return E_OUTOFMEMORY
;
1916 return stack_push(ctx
, &v
);
1919 /* ECMA-262 3rd Edition 7.8 */
1920 static HRESULT
interp_regexp(exec_ctx_t
*ctx
)
1922 const WCHAR
*source
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.str
;
1923 const LONG flags
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.lng
;
1928 TRACE("%s %x\n", debugstr_w(source
), flags
);
1930 hres
= create_regexp(ctx
->parser
->script
, source
, strlenW(source
), flags
, ®exp
);
1934 var_set_jsdisp(&v
, regexp
);
1935 return stack_push(ctx
, &v
);
1938 /* ECMA-262 3rd Edition 11.1.4 */
1939 static HRESULT
interp_carray(exec_ctx_t
*ctx
)
1941 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1949 hres
= create_array(ctx
->parser
->script
, arg
, &array
);
1956 hres
= jsdisp_propput_idx(array
, i
, v
, &ctx
->ei
, NULL
/*FIXME*/);
1959 jsdisp_release(array
);
1964 var_set_jsdisp(&r
, array
);
1965 return stack_push(ctx
, &r
);
1968 /* ECMA-262 3rd Edition 11.1.5 */
1969 HRESULT
property_value_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1971 property_value_expression_t
*expr
= (property_value_expression_t
*)_expr
;
1981 hres
= create_object(ctx
, NULL
, &obj
);
1985 for(iter
= expr
->property_list
; iter
; iter
= iter
->next
) {
1986 hres
= literal_to_var(ctx
, iter
->name
, &tmp
);
1990 hres
= to_string(ctx
, &tmp
, ei
, &name
);
1995 hres
= expr_eval(ctx
, iter
->value
, 0, ei
, &exprval
);
1996 if(SUCCEEDED(hres
)) {
1997 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1998 exprval_release(&exprval
);
1999 if(SUCCEEDED(hres
)) {
2000 hres
= jsdisp_propput_name(obj
, name
, &val
, ei
, NULL
/*FIXME*/);
2005 SysFreeString(name
);
2011 jsdisp_release(obj
);
2015 ret
->type
= EXPRVAL_VARIANT
;
2016 var_set_jsdisp(&ret
->u
.var
, obj
);
2020 /* ECMA-262 3rd Edition 11.11 */
2021 static HRESULT
interp_jmp_nz(exec_ctx_t
*ctx
)
2023 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
2029 hres
= to_boolean(stack_top(ctx
), &b
);
2042 /* ECMA-262 3rd Edition 11.11 */
2043 static HRESULT
interp_jmp_z(exec_ctx_t
*ctx
)
2045 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
2051 hres
= to_boolean(stack_top(ctx
), &b
);
2064 /* ECMA-262 3rd Edition 11.10 */
2065 static HRESULT
interp_or(exec_ctx_t
*ctx
)
2072 hres
= stack_pop_int(ctx
, &r
);
2076 hres
= stack_pop_int(ctx
, &l
);
2080 return stack_push_int(ctx
, l
|r
);
2083 /* ECMA-262 3rd Edition 11.10 */
2084 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
2091 hres
= stack_pop_int(ctx
, &r
);
2095 hres
= stack_pop_int(ctx
, &l
);
2099 return stack_push_int(ctx
, l
^r
);
2102 /* ECMA-262 3rd Edition 11.10 */
2103 static HRESULT
interp_and(exec_ctx_t
*ctx
)
2110 hres
= stack_pop_int(ctx
, &r
);
2114 hres
= stack_pop_int(ctx
, &l
);
2118 return stack_push_int(ctx
, l
&r
);
2121 /* ECMA-262 3rd Edition 11.8.6 */
2122 static HRESULT
interp_instanceof(exec_ctx_t
*ctx
)
2124 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
2129 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2132 if(V_VT(v
) != VT_DISPATCH
|| !V_DISPATCH(v
)) {
2134 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
2137 obj
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(v
));
2138 IDispatch_Release(V_DISPATCH(v
));
2140 FIXME("non-jsdisp objects not supported\n");
2144 if(is_class(obj
, JSCLASS_FUNCTION
)) {
2145 hres
= jsdisp_propget_name(obj
, prototypeW
, &prot
, &ctx
->ei
, NULL
/*FIXME*/);
2147 hres
= throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
2149 jsdisp_release(obj
);
2155 if(V_VT(&prot
) == VT_DISPATCH
) {
2156 if(V_VT(v
) == VT_DISPATCH
)
2157 tmp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(v
));
2158 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
2159 hres
= disp_cmp(V_DISPATCH(&prot
), to_disp(iter
), &ret
);
2165 jsdisp_release(tmp
);
2167 FIXME("prototype is not an object\n");
2171 VariantClear(&prot
);
2176 return stack_push_bool(ctx
, ret
);
2179 /* ECMA-262 3rd Edition 11.8.7 */
2180 static HRESULT
interp_in(exec_ctx_t
*ctx
)
2190 obj
= stack_pop(ctx
);
2193 if(V_VT(obj
) != VT_DISPATCH
|| !V_DISPATCH(obj
)) {
2196 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
2199 hres
= to_string(ctx
->parser
->script
, v
, &ctx
->ei
, &str
);
2202 IDispatch_Release(V_DISPATCH(obj
));
2206 hres
= disp_get_id(ctx
->parser
->script
, V_DISPATCH(obj
), str
, 0, &id
);
2207 IDispatch_Release(V_DISPATCH(obj
));
2211 else if(hres
== DISP_E_UNKNOWNNAME
)
2216 return stack_push_bool(ctx
, ret
);
2219 /* ECMA-262 3rd Edition 11.6.1 */
2220 static HRESULT
add_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2225 hres
= to_primitive(ctx
, lval
, ei
, &l
, NO_HINT
);
2229 hres
= to_primitive(ctx
, rval
, ei
, &r
, NO_HINT
);
2235 if(V_VT(&l
) == VT_BSTR
|| V_VT(&r
) == VT_BSTR
) {
2236 BSTR lstr
= NULL
, rstr
= NULL
;
2238 if(V_VT(&l
) == VT_BSTR
)
2241 hres
= to_string(ctx
, &l
, ei
, &lstr
);
2243 if(SUCCEEDED(hres
)) {
2244 if(V_VT(&r
) == VT_BSTR
)
2247 hres
= to_string(ctx
, &r
, ei
, &rstr
);
2250 if(SUCCEEDED(hres
)) {
2253 len1
= SysStringLen(lstr
);
2254 len2
= SysStringLen(rstr
);
2256 V_VT(retv
) = VT_BSTR
;
2257 V_BSTR(retv
) = SysAllocStringLen(NULL
, len1
+len2
);
2258 memcpy(V_BSTR(retv
), lstr
, len1
*sizeof(WCHAR
));
2259 memcpy(V_BSTR(retv
)+len1
, rstr
, (len2
+1)*sizeof(WCHAR
));
2262 if(V_VT(&l
) != VT_BSTR
)
2263 SysFreeString(lstr
);
2264 if(V_VT(&r
) != VT_BSTR
)
2265 SysFreeString(rstr
);
2269 hres
= to_number(ctx
, &l
, ei
, &nl
);
2270 if(SUCCEEDED(hres
)) {
2271 hres
= to_number(ctx
, &r
, ei
, &nr
);
2273 num_set_val(retv
, num_val(&nl
) + num_val(&nr
));
2282 /* ECMA-262 3rd Edition 11.6.1 */
2283 static HRESULT
interp_add(exec_ctx_t
*ctx
)
2285 VARIANT
*l
, *r
, ret
;
2291 TRACE("%s + %s\n", debugstr_variant(l
), debugstr_variant(r
));
2293 hres
= add_eval(ctx
->parser
->script
, l
, r
, &ctx
->ei
, &ret
);
2299 return stack_push(ctx
, &ret
);
2302 /* ECMA-262 3rd Edition 11.6.2 */
2303 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
2310 hres
= stack_pop_number(ctx
, &r
);
2314 hres
= stack_pop_number(ctx
, &l
);
2318 return stack_push_number(ctx
, num_val(&l
)-num_val(&r
));
2321 /* ECMA-262 3rd Edition 11.5.1 */
2322 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
2329 hres
= stack_pop_number(ctx
, &r
);
2333 hres
= stack_pop_number(ctx
, &l
);
2337 return stack_push_number(ctx
, num_val(&l
)*num_val(&r
));
2340 /* ECMA-262 3rd Edition 11.5.2 */
2341 static HRESULT
interp_div(exec_ctx_t
*ctx
)
2348 hres
= stack_pop_number(ctx
, &r
);
2352 hres
= stack_pop_number(ctx
, &l
);
2356 return stack_push_number(ctx
, num_val(&l
)/num_val(&r
));
2359 /* ECMA-262 3rd Edition 11.5.3 */
2360 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
2367 hres
= stack_pop_number(ctx
, &r
);
2371 hres
= stack_pop_number(ctx
, &l
);
2375 return stack_push_number(ctx
, fmod(num_val(&l
), num_val(&r
)));
2378 /* ECMA-262 3rd Edition 11.4.2 */
2379 static HRESULT
interp_delete(exec_ctx_t
*ctx
)
2381 VARIANT
*obj_var
, *name_var
;
2382 IDispatchEx
*dispex
;
2390 name_var
= stack_pop(ctx
);
2391 obj_var
= stack_pop(ctx
);
2393 hres
= to_object(ctx
->parser
->script
, obj_var
, &obj
);
2394 VariantClear(obj_var
);
2396 VariantClear(name_var
);
2400 hres
= to_string(ctx
->parser
->script
, name_var
, &ctx
->ei
, &name
);
2401 VariantClear(name_var
);
2403 IDispatch_Release(obj
);
2407 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
2408 if(SUCCEEDED(hres
)) {
2409 hres
= IDispatchEx_DeleteMemberByName(dispex
, name
, make_grfdex(ctx
->parser
->script
, fdexNameCaseSensitive
));
2411 IDispatchEx_Release(dispex
);
2417 IDispatch_Release(obj
);
2418 SysFreeString(name
);
2422 return stack_push_bool(ctx
, ret
);
2425 /* ECMA-262 3rd Edition 11.4.2 */
2426 static HRESULT
interp_delete_ident(exec_ctx_t
*ctx
)
2428 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
2429 IDispatchEx
*dispex
;
2434 TRACE("%s\n", debugstr_w(arg
));
2436 hres
= identifier_eval(ctx
->parser
->script
, arg
, 0, &ctx
->ei
, &exprval
);
2440 if(exprval
.type
!= EXPRVAL_IDREF
) {
2441 FIXME("Unsupported exprval\n");
2442 exprval_release(&exprval
);
2446 hres
= IDispatch_QueryInterface(exprval
.u
.idref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2447 IDispatch_Release(exprval
.u
.idref
.disp
);
2448 if(SUCCEEDED(hres
)) {
2449 hres
= IDispatchEx_DeleteMemberByDispID(dispex
, exprval
.u
.idref
.id
);
2450 IDispatchEx_Release(dispex
);
2457 return stack_push_bool(ctx
, ret
);
2460 /* ECMA-262 3rd Edition 11.4.2 */
2461 static HRESULT
interp_void(exec_ctx_t
*ctx
)
2469 V_VT(&v
) = VT_EMPTY
;
2470 return stack_push(ctx
, &v
);
2473 /* ECMA-262 3rd Edition 11.4.3 */
2474 static HRESULT
typeof_string(VARIANT
*v
, const WCHAR
**ret
)
2496 if(V_DISPATCH(v
) && (dispex
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(v
)))) {
2497 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
2498 jsdisp_release(dispex
);
2505 FIXME("unhandled vt %d\n", V_VT(v
));
2512 /* ECMA-262 3rd Edition 11.4.3 */
2513 static HRESULT
interp_typeofid(exec_ctx_t
*ctx
)
2521 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
2525 obj
= stack_pop_objid(ctx
, &id
);
2527 return stack_push_string(ctx
, undefinedW
);
2529 V_VT(&v
) = VT_EMPTY
;
2530 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
2531 IDispatch_Release(obj
);
2533 return stack_push_string(ctx
, unknownW
);
2535 hres
= typeof_string(&v
, &ret
);
2540 return stack_push_string(ctx
, ret
);
2543 /* ECMA-262 3rd Edition 11.4.3 */
2544 static HRESULT
interp_typeofident(exec_ctx_t
*ctx
)
2546 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
2552 TRACE("%s\n", debugstr_w(arg
));
2554 hres
= identifier_eval(ctx
->parser
->script
, arg
, 0, &ctx
->ei
, &exprval
);
2558 if(exprval
.type
== EXPRVAL_INVALID
) {
2559 hres
= stack_push_string(ctx
, undefinedW
);
2560 exprval_release(&exprval
);
2564 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &ctx
->ei
, &v
);
2565 exprval_release(&exprval
);
2569 hres
= typeof_string(&v
, &ret
);
2574 return stack_push_string(ctx
, ret
);
2577 /* ECMA-262 3rd Edition 11.4.3 */
2578 static HRESULT
interp_typeof(exec_ctx_t
*ctx
)
2587 hres
= typeof_string(v
, &ret
);
2592 return stack_push_string(ctx
, ret
);
2595 /* ECMA-262 3rd Edition 11.4.7 */
2596 static HRESULT
interp_minus(exec_ctx_t
*ctx
)
2603 hres
= stack_pop_number(ctx
, &n
);
2607 return stack_push_number(ctx
, -num_val(&n
));
2610 /* ECMA-262 3rd Edition 11.4.6 */
2611 static HRESULT
interp_tonum(exec_ctx_t
*ctx
)
2619 hres
= to_number(ctx
->parser
->script
, v
, &ctx
->ei
, &num
);
2624 return stack_push(ctx
, &num
);
2627 /* ECMA-262 3rd Edition 11.3.1 */
2628 static HRESULT
interp_postinc(exec_ctx_t
*ctx
)
2630 const int arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
2638 obj
= stack_pop_objid(ctx
, &id
);
2640 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
2642 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
2643 if(SUCCEEDED(hres
)) {
2646 hres
= to_number(ctx
->parser
->script
, &v
, &ctx
->ei
, &n
);
2647 if(SUCCEEDED(hres
)) {
2648 num_set_val(&inc
, num_val(&n
)+(double)arg
);
2649 hres
= disp_propput(ctx
->parser
->script
, obj
, id
, &inc
, &ctx
->ei
, NULL
/*FIXME*/);
2654 IDispatch_Release(obj
);
2658 return stack_push(ctx
, &v
);
2661 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2662 static HRESULT
interp_preinc(exec_ctx_t
*ctx
)
2664 const int arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
2672 obj
= stack_pop_objid(ctx
, &id
);
2674 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
2676 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
2677 if(SUCCEEDED(hres
)) {
2680 hres
= to_number(ctx
->parser
->script
, &v
, &ctx
->ei
, &n
);
2682 if(SUCCEEDED(hres
)) {
2683 num_set_val(&v
, num_val(&n
)+(double)arg
);
2684 hres
= disp_propput(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
2687 IDispatch_Release(obj
);
2691 return stack_push(ctx
, &v
);
2694 /* ECMA-262 3rd Edition 11.9.3 */
2695 static HRESULT
equal_values(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, BOOL
*ret
)
2697 if(V_VT(lval
) == V_VT(rval
) || (is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
))))
2698 return equal2_values(lval
, rval
, ret
);
2700 /* FIXME: NULL disps should be handled in more general way */
2701 if(V_VT(lval
) == VT_DISPATCH
&& !V_DISPATCH(lval
)) {
2704 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2707 if(V_VT(rval
) == VT_DISPATCH
&& !V_DISPATCH(rval
)) {
2710 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2713 if((V_VT(lval
) == VT_NULL
&& V_VT(rval
) == VT_EMPTY
) ||
2714 (V_VT(lval
) == VT_EMPTY
&& V_VT(rval
) == VT_NULL
)) {
2719 if(V_VT(lval
) == VT_BSTR
&& is_num_vt(V_VT(rval
))) {
2723 hres
= to_number(ctx
, lval
, ei
, &v
);
2727 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2730 if(V_VT(rval
) == VT_BSTR
&& is_num_vt(V_VT(lval
))) {
2734 hres
= to_number(ctx
, rval
, ei
, &v
);
2738 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2741 if(V_VT(rval
) == VT_BOOL
) {
2745 V_I4(&v
) = V_BOOL(rval
) ? 1 : 0;
2746 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2749 if(V_VT(lval
) == VT_BOOL
) {
2753 V_I4(&v
) = V_BOOL(lval
) ? 1 : 0;
2754 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2758 if(V_VT(rval
) == VT_DISPATCH
&& (V_VT(lval
) == VT_BSTR
|| is_num_vt(V_VT(lval
)))) {
2762 hres
= to_primitive(ctx
, rval
, ei
, &v
, NO_HINT
);
2766 hres
= equal_values(ctx
, lval
, &v
, ei
, ret
);
2773 if(V_VT(lval
) == VT_DISPATCH
&& (V_VT(rval
) == VT_BSTR
|| is_num_vt(V_VT(rval
)))) {
2777 hres
= to_primitive(ctx
, lval
, ei
, &v
, NO_HINT
);
2781 hres
= equal_values(ctx
, &v
, rval
, ei
, ret
);
2792 /* ECMA-262 3rd Edition 11.9.1 */
2793 static HRESULT
interp_eq(exec_ctx_t
*ctx
)
2802 TRACE("%s == %s\n", debugstr_variant(l
), debugstr_variant(r
));
2804 hres
= equal_values(ctx
->parser
->script
, l
, r
, &ctx
->ei
, &b
);
2810 return stack_push_bool(ctx
, b
);
2813 /* ECMA-262 3rd Edition 11.9.2 */
2814 static HRESULT
interp_neq(exec_ctx_t
*ctx
)
2823 TRACE("%s != %s\n", debugstr_variant(l
), debugstr_variant(r
));
2825 hres
= equal_values(ctx
->parser
->script
, l
, r
, &ctx
->ei
, &b
);
2831 return stack_push_bool(ctx
, !b
);
2834 /* ECMA-262 3rd Edition 11.9.4 */
2835 static HRESULT
interp_eq2(exec_ctx_t
*ctx
)
2846 hres
= equal2_values(r
, l
, &b
);
2852 return stack_push_bool(ctx
, b
);
2855 /* ECMA-262 3rd Edition 11.9.5 */
2856 static HRESULT
interp_neq2(exec_ctx_t
*ctx
)
2867 hres
= equal2_values(r
, l
, &b
);
2873 return stack_push_bool(ctx
, !b
);
2876 /* ECMA-262 3rd Edition 11.8.5 */
2877 static HRESULT
less_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, BOOL greater
, jsexcept_t
*ei
, BOOL
*ret
)
2879 VARIANT l
, r
, ln
, rn
;
2882 hres
= to_primitive(ctx
, lval
, ei
, &l
, NO_HINT
);
2886 hres
= to_primitive(ctx
, rval
, ei
, &r
, NO_HINT
);
2892 if(V_VT(&l
) == VT_BSTR
&& V_VT(&r
) == VT_BSTR
) {
2893 *ret
= (strcmpW(V_BSTR(&l
), V_BSTR(&r
)) < 0) ^ greater
;
2894 SysFreeString(V_BSTR(&l
));
2895 SysFreeString(V_BSTR(&r
));
2899 hres
= to_number(ctx
, &l
, ei
, &ln
);
2902 hres
= to_number(ctx
, &r
, ei
, &rn
);
2907 if(V_VT(&ln
) == VT_I4
&& V_VT(&rn
) == VT_I4
) {
2908 *ret
= (V_I4(&ln
) < V_I4(&rn
)) ^ greater
;
2910 DOUBLE ld
= num_val(&ln
);
2911 DOUBLE rd
= num_val(&rn
);
2913 *ret
= !isnan(ld
) && !isnan(rd
) && ((ld
< rd
) ^ greater
);
2919 /* ECMA-262 3rd Edition 11.8.1 */
2920 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
2929 TRACE("%s < %s\n", debugstr_variant(l
), debugstr_variant(r
));
2931 hres
= less_eval(ctx
->parser
->script
, l
, r
, FALSE
, &ctx
->ei
, &b
);
2937 return stack_push_bool(ctx
, b
);
2940 /* ECMA-262 3rd Edition 11.8.1 */
2941 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
2950 TRACE("%s <= %s\n", debugstr_variant(l
), debugstr_variant(r
));
2952 hres
= less_eval(ctx
->parser
->script
, r
, l
, TRUE
, &ctx
->ei
, &b
);
2958 return stack_push_bool(ctx
, b
);
2961 /* ECMA-262 3rd Edition 11.8.2 */
2962 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
2971 TRACE("%s > %s\n", debugstr_variant(l
), debugstr_variant(r
));
2973 hres
= less_eval(ctx
->parser
->script
, r
, l
, FALSE
, &ctx
->ei
, &b
);
2979 return stack_push_bool(ctx
, b
);
2982 /* ECMA-262 3rd Edition 11.8.4 */
2983 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
2992 TRACE("%s >= %s\n", debugstr_variant(l
), debugstr_variant(r
));
2994 hres
= less_eval(ctx
->parser
->script
, l
, r
, TRUE
, &ctx
->ei
, &b
);
3000 return stack_push_bool(ctx
, b
);
3003 /* ECMA-262 3rd Edition 11.4.8 */
3004 static HRESULT
interp_bneg(exec_ctx_t
*ctx
)
3013 hres
= to_int32(ctx
->parser
->script
, v
, &ctx
->ei
, &i
);
3020 return stack_push(ctx
, &r
);
3023 /* ECMA-262 3rd Edition 11.4.9 */
3024 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
3033 hres
= to_boolean(v
, &b
);
3038 return stack_push_bool(ctx
, !b
);
3041 /* ECMA-262 3rd Edition 11.7.1 */
3042 static HRESULT
interp_lshift(exec_ctx_t
*ctx
)
3048 hres
= stack_pop_uint(ctx
, &r
);
3052 hres
= stack_pop_int(ctx
, &l
);
3056 return stack_push_int(ctx
, l
<< (r
&0x1f));
3059 /* ECMA-262 3rd Edition 11.7.2 */
3060 static HRESULT
interp_rshift(exec_ctx_t
*ctx
)
3066 hres
= stack_pop_uint(ctx
, &r
);
3070 hres
= stack_pop_int(ctx
, &l
);
3074 return stack_push_int(ctx
, l
>> (r
&0x1f));
3077 /* ECMA-262 3rd Edition 11.7.3 */
3078 static HRESULT
interp_rshift2(exec_ctx_t
*ctx
)
3083 hres
= stack_pop_uint(ctx
, &r
);
3087 hres
= stack_pop_uint(ctx
, &l
);
3091 return stack_push_int(ctx
, l
>> (r
&0x1f));
3094 /* ECMA-262 3rd Edition 11.13.1 */
3095 static HRESULT
interp_assign(exec_ctx_t
*ctx
)
3105 disp
= stack_pop_objid(ctx
, &id
);
3108 return throw_reference_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_ILLEGAL_ASSIGN
, NULL
);
3110 hres
= disp_propput(ctx
->parser
->script
, disp
, id
, v
, &ctx
->ei
, NULL
/*FIXME*/);
3111 IDispatch_Release(disp
);
3117 return stack_push(ctx
, v
);
3120 static HRESULT
interp_undefined(exec_ctx_t
*ctx
)
3126 V_VT(&v
) = VT_EMPTY
;
3127 return stack_push(ctx
, &v
);
3130 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
3132 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
3140 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
3148 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
3156 static HRESULT
interp_tree(exec_ctx_t
*ctx
)
3158 instr_t
*instr
= ctx
->parser
->code
->instrs
+ctx
->ip
;
3165 hres
= expr_eval(ctx
->parser
->script
, instr
->arg1
.expr
, 0, &ctx
->ei
, &val
);
3169 hres
= exprval_to_value(ctx
->parser
->script
, &val
, &ctx
->ei
, &v
);
3170 exprval_release(&val
);
3174 return stack_push(ctx
, &v
);
3177 typedef HRESULT (*op_func_t
)(exec_ctx_t
*);
3179 static const op_func_t op_funcs
[] = {
3180 #define X(x,a,b,c) interp_##x,
3185 static const unsigned op_move
[] = {
3186 #define X(a,x,b,c) x,
3191 static HRESULT
interp_expression_eval(script_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3193 exec_ctx_t
*exec_ctx
= ctx
->exec_ctx
;
3194 unsigned prev_ip
, prev_top
;
3196 HRESULT hres
= S_OK
;
3200 prev_top
= exec_ctx
->top
;
3201 prev_ip
= exec_ctx
->ip
;
3202 exec_ctx
->ip
= expr
->instr_off
;
3204 while(exec_ctx
->ip
!= -1) {
3205 op
= exec_ctx
->parser
->code
->instrs
[exec_ctx
->ip
].op
;
3206 hres
= op_funcs
[op
](exec_ctx
);
3209 exec_ctx
->ip
+= op_move
[op
];
3212 exec_ctx
->ip
= prev_ip
;
3215 stack_popn(exec_ctx
, exec_ctx
->top
-prev_top
);
3217 memset(&exec_ctx
->ei
, 0, sizeof(exec_ctx
->ei
));
3221 assert(exec_ctx
->top
== prev_top
+1 || ((flags
&EXPR_NOVAL
) && exec_ctx
->top
== prev_top
));
3223 ret
->type
= EXPRVAL_VARIANT
;
3224 if(exec_ctx
->top
== prev_top
)
3225 V_VT(&ret
->u
.var
) = VT_EMPTY
;
3227 ret
->u
.var
= *stack_pop(exec_ctx
);
3231 HRESULT
compiled_expression_eval(script_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3237 hres
= compile_subscript(ctx
->exec_ctx
->parser
, expr
, !(flags
& EXPR_NOVAL
), &expr
->instr_off
);
3241 if(expr
->eval
== compiled_expression_eval
)
3242 expr
->eval
= interp_expression_eval
;
3244 return expr
->eval(ctx
, expr
, flags
, ei
, ret
);