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
));
1074 if(stat
->variable
) {
1075 hres
= identifier_eval(ctx
, identifier
, 0, NULL
, &exprval
);
1077 switch(stat
->expr
->type
) {
1079 hres
= array_expression_eval(ctx
, stat
->expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
1082 hres
= identifier_expression_eval(ctx
, stat
->expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
1085 hres
= member_expression_eval(ctx
, stat
->expr
, EXPR_NEWREF
, &rt
->ei
, &exprval
);
1088 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1091 if(SUCCEEDED(hres
)) {
1092 V_VT(&name
) = VT_BSTR
;
1093 V_BSTR(&name
) = str
;
1094 hres
= put_value(ctx
, &exprval
, &name
, &rt
->ei
);
1095 exprval_release(&exprval
);
1101 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
1105 VariantClear(&retv
);
1108 if(rt
->type
== RT_CONTINUE
)
1109 rt
->type
= RT_NORMAL
;
1110 else if(rt
->type
!= RT_NORMAL
)
1114 SysFreeString(identifier
);
1115 IDispatchEx_Release(in_obj
);
1117 VariantClear(&retv
);
1121 if(rt
->type
== RT_BREAK
)
1122 rt
->type
= RT_NORMAL
;
1128 /* ECMA-262 3rd Edition 12.7 */
1129 HRESULT
continue_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1131 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
1135 if(stat
->identifier
) {
1136 FIXME("indentifier not implemented\n");
1140 rt
->type
= RT_CONTINUE
;
1141 V_VT(ret
) = VT_EMPTY
;
1145 /* ECMA-262 3rd Edition 12.8 */
1146 HRESULT
break_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1148 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
1152 if(stat
->identifier
) {
1153 FIXME("indentifier not implemented\n");
1157 rt
->type
= RT_BREAK
;
1158 V_VT(ret
) = VT_EMPTY
;
1162 /* ECMA-262 3rd Edition 12.9 */
1163 HRESULT
return_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1165 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
1173 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1177 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, ret
);
1178 exprval_release(&exprval
);
1182 V_VT(ret
) = VT_EMPTY
;
1185 TRACE("= %s\n", debugstr_variant(ret
));
1186 rt
->type
= RT_RETURN
;
1190 /* ECMA-262 3rd Edition 12.10 */
1191 HRESULT
with_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1193 with_statement_t
*stat
= (with_statement_t
*)_stat
;
1202 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1206 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
1207 exprval_release(&exprval
);
1211 hres
= to_object(ctx
, &val
, &disp
);
1216 obj
= iface_to_jsdisp((IUnknown
*)disp
);
1217 IDispatch_Release(disp
);
1219 FIXME("disp id not jsdisp\n");
1223 hres
= scope_push(ctx
->exec_ctx
->scope_chain
, obj
, &ctx
->exec_ctx
->scope_chain
);
1224 jsdisp_release(obj
);
1228 hres
= stat_eval(ctx
, stat
->statement
, rt
, ret
);
1230 scope_pop(&ctx
->exec_ctx
->scope_chain
);
1234 /* ECMA-262 3rd Edition 12.12 */
1235 HRESULT
labelled_statement_eval(script_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
1241 /* ECMA-262 3rd Edition 12.13 */
1242 HRESULT
switch_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1244 switch_statement_t
*stat
= (switch_statement_t
*)_stat
;
1245 case_clausule_t
*iter
, *default_clausule
= NULL
;
1246 statement_t
*stat_iter
;
1254 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1258 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
1259 exprval_release(&exprval
);
1263 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1265 default_clausule
= iter
;
1269 hres
= expr_eval(ctx
, iter
->expr
, 0, &rt
->ei
, &exprval
);
1273 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &cval
);
1274 exprval_release(&exprval
);
1278 hres
= equal2_values(&val
, &cval
, &b
);
1279 VariantClear(&cval
);
1280 if(FAILED(hres
) || b
)
1289 iter
= default_clausule
;
1291 V_VT(&val
) = VT_EMPTY
;
1295 for(stat_iter
= iter
->stat
; stat_iter
; stat_iter
= stat_iter
->next
) {
1296 hres
= stat_eval(ctx
, stat_iter
, rt
, &tmp
);
1303 if(rt
->type
!= RT_NORMAL
)
1313 if(rt
->type
== RT_BREAK
)
1314 rt
->type
= RT_NORMAL
;
1320 /* ECMA-262 3rd Edition 12.13 */
1321 HRESULT
throw_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1323 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
1330 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1334 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
1335 exprval_release(&exprval
);
1340 return DISP_E_EXCEPTION
;
1343 static HRESULT
interp_throw(exec_ctx_t
*ctx
)
1345 const HRESULT arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1347 TRACE("%08x\n", arg
);
1349 return throw_reference_error(ctx
->parser
->script
, &ctx
->ei
, arg
, NULL
);
1352 static HRESULT
interp_throw_type(exec_ctx_t
*ctx
)
1354 const HRESULT hres
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1355 const WCHAR
*str
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.str
;
1357 TRACE("%08x %s\n", hres
, debugstr_w(str
));
1359 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, hres
, str
);
1362 /* ECMA-262 3rd Edition 12.14 */
1363 static HRESULT
catch_eval(script_ctx_t
*ctx
, catch_block_t
*block
, return_type_t
*rt
, VARIANT
*ret
)
1370 memset(&rt
->ei
, 0, sizeof(jsexcept_t
));
1372 hres
= create_dispex(ctx
, NULL
, NULL
, &var_disp
);
1373 if(SUCCEEDED(hres
)) {
1374 hres
= jsdisp_propput_name(var_disp
, block
->identifier
, &ex
, &rt
->ei
, NULL
/*FIXME*/);
1375 if(SUCCEEDED(hres
)) {
1376 hres
= scope_push(ctx
->exec_ctx
->scope_chain
, var_disp
, &ctx
->exec_ctx
->scope_chain
);
1377 if(SUCCEEDED(hres
)) {
1378 hres
= stat_eval(ctx
, block
->statement
, rt
, &val
);
1379 scope_pop(&ctx
->exec_ctx
->scope_chain
);
1383 jsdisp_release(var_disp
);
1394 /* ECMA-262 3rd Edition 12.14 */
1395 HRESULT
try_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1397 try_statement_t
*stat
= (try_statement_t
*)_stat
;
1403 hres
= stat_eval(ctx
, stat
->try_statement
, rt
, &val
);
1405 TRACE("EXCEPTION\n");
1406 if(!stat
->catch_block
)
1409 hres
= catch_eval(ctx
, stat
->catch_block
, rt
, &val
);
1414 if(stat
->finally_statement
) {
1416 hres
= stat_eval(ctx
, stat
->finally_statement
, rt
, &val
);
1425 /* ECMA-262 3rd Edition 13 */
1426 HRESULT
function_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1428 function_expression_t
*expr
= (function_expression_t
*)_expr
;
1434 if(expr
->identifier
) {
1435 hres
= jsdisp_propget_name(ctx
->exec_ctx
->var_disp
, expr
->identifier
, &var
, ei
, NULL
/*FIXME*/);
1441 hres
= create_source_function(ctx
->exec_ctx
->parser
, expr
->parameter_list
, expr
->source_elements
, ctx
->exec_ctx
->scope_chain
,
1442 expr
->src_str
, expr
->src_len
, &dispex
);
1446 var_set_jsdisp(&var
, dispex
);
1449 ret
->type
= EXPRVAL_VARIANT
;
1454 /* ECMA-262 3rd Edition 11.2.1 */
1455 HRESULT
array_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1457 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1459 VARIANT member
, val
;
1462 IDispatch
*obj
= NULL
;
1467 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1471 hres
= exprval_to_value(ctx
, &exprval
, ei
, &member
);
1472 exprval_release(&exprval
);
1476 hres
= expr_eval(ctx
, expr
->expression2
, EXPR_NEWREF
, ei
, &exprval
);
1477 if(SUCCEEDED(hres
)) {
1478 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1479 exprval_release(&exprval
);
1482 if(SUCCEEDED(hres
)) {
1483 hres
= to_object(ctx
, &member
, &obj
);
1487 VariantClear(&member
);
1488 if(SUCCEEDED(hres
)) {
1489 hres
= to_string(ctx
, &val
, ei
, &str
);
1491 if(SUCCEEDED(hres
)) {
1492 hres
= disp_get_id(ctx
, obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1496 if(SUCCEEDED(hres
)) {
1497 exprval_set_idref(ret
, obj
, id
);
1498 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1503 IDispatch_Release(obj
);
1509 /* ECMA-262 3rd Edition 11.2.1 */
1510 static HRESULT
interp_array(exec_ctx_t
*ctx
)
1520 namev
= stack_pop(ctx
);
1522 hres
= stack_pop_object(ctx
, &obj
);
1524 VariantClear(namev
);
1528 hres
= to_string(ctx
->parser
->script
, namev
, &ctx
->ei
, &name
);
1529 VariantClear(namev
);
1531 IDispatch_Release(obj
);
1535 hres
= disp_get_id(ctx
->parser
->script
, obj
, name
, 0, &id
);
1536 SysFreeString(name
);
1537 if(SUCCEEDED(hres
)) {
1538 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
1539 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1540 V_VT(&v
) = VT_EMPTY
;
1543 IDispatch_Release(obj
);
1547 return stack_push(ctx
, &v
);
1550 /* ECMA-262 3rd Edition 11.2.1 */
1551 HRESULT
member_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1553 member_expression_t
*expr
= (member_expression_t
*)_expr
;
1554 IDispatch
*obj
= NULL
;
1563 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1567 hres
= exprval_to_value(ctx
, &exprval
, ei
, &member
);
1568 exprval_release(&exprval
);
1572 hres
= to_object(ctx
, &member
, &obj
);
1573 VariantClear(&member
);
1577 str
= SysAllocString(expr
->identifier
);
1579 IDispatch_Release(obj
);
1580 return E_OUTOFMEMORY
;
1583 hres
= disp_get_id(ctx
, obj
, str
, flags
& EXPR_NEWREF
? fdexNameEnsure
: 0, &id
);
1585 if(SUCCEEDED(hres
)) {
1586 exprval_set_idref(ret
, obj
, id
);
1587 }else if(!(flags
& EXPR_NEWREF
) && hres
== DISP_E_UNKNOWNNAME
) {
1592 IDispatch_Release(obj
);
1596 /* ECMA-262 3rd Edition 11.2.1 */
1597 static HRESULT
interp_member(exec_ctx_t
*ctx
)
1599 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
1607 hres
= stack_pop_object(ctx
, &obj
);
1611 hres
= disp_get_id(ctx
->parser
->script
, obj
, arg
, 0, &id
);
1612 if(SUCCEEDED(hres
)) {
1613 V_VT(&v
) = VT_EMPTY
;
1614 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
1615 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1616 V_VT(&v
) = VT_EMPTY
;
1619 IDispatch_Release(obj
);
1623 return stack_push(ctx
, &v
);
1626 /* ECMA-262 3rd Edition 11.2.1 */
1627 static HRESULT
interp_memberid(exec_ctx_t
*ctx
)
1629 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1630 VARIANT
*objv
, *namev
;
1638 namev
= stack_pop(ctx
);
1639 objv
= stack_pop(ctx
);
1641 hres
= to_object(ctx
->parser
->script
, objv
, &obj
);
1643 if(SUCCEEDED(hres
)) {
1644 hres
= to_string(ctx
->parser
->script
, namev
, &ctx
->ei
, &name
);
1646 IDispatch_Release(obj
);
1648 VariantClear(namev
);
1652 hres
= disp_get_id(ctx
->parser
->script
, obj
, name
, arg
, &id
);
1653 SysFreeString(name
);
1655 IDispatch_Release(obj
);
1656 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
1658 id
= JS_E_INVALID_PROPERTY
;
1664 return stack_push_objid(ctx
, obj
, id
);
1667 /* ECMA-262 3rd Edition 11.2.1 */
1668 static HRESULT
interp_refval(exec_ctx_t
*ctx
)
1677 disp
= stack_topn_objid(ctx
, 0, &id
);
1679 return throw_reference_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_ILLEGAL_ASSIGN
, NULL
);
1681 hres
= disp_propget(ctx
->parser
->script
, disp
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
1685 return stack_push(ctx
, &v
);
1688 static void jsstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, DISPPARAMS
*dp
)
1693 dp
->cArgs
= arg_cnt
;
1694 dp
->rgdispidNamedArgs
= NULL
;
1697 assert(ctx
->top
>= arg_cnt
);
1699 for(i
=1; i
*2 <= arg_cnt
; i
++) {
1700 tmp
= ctx
->stack
[ctx
->top
-i
];
1701 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
1702 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
1705 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-arg_cnt
;
1708 /* ECMA-262 3rd Edition 11.2.2 */
1709 static HRESULT
interp_new(exec_ctx_t
*ctx
)
1711 const LONG arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1718 constr
= stack_topn(ctx
, arg
);
1720 /* NOTE: Should use to_object here */
1722 if(V_VT(constr
) == VT_NULL
)
1723 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
1724 else if(V_VT(constr
) != VT_DISPATCH
)
1725 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_INVALID_ACTION
, NULL
);
1726 else if(!V_DISPATCH(constr
))
1727 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_INVALID_PROPERTY
, NULL
);
1729 jsstack_to_dp(ctx
, arg
, &dp
);
1730 hres
= disp_call(ctx
->parser
->script
, V_DISPATCH(constr
), DISPID_VALUE
,
1731 DISPATCH_CONSTRUCT
, &dp
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
1735 stack_popn(ctx
, arg
+1);
1736 return stack_push(ctx
, &v
);
1739 /* ECMA-262 3rd Edition 11.2.3 */
1740 static HRESULT
interp_call(exec_ctx_t
*ctx
)
1742 const unsigned argn
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1743 const int do_ret
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.lng
;
1748 TRACE("%d %d\n", argn
, do_ret
);
1750 objv
= stack_topn(ctx
, argn
);
1751 if(V_VT(objv
) != VT_DISPATCH
)
1752 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_INVALID_PROPERTY
, NULL
);
1754 jsstack_to_dp(ctx
, argn
, &dp
);
1755 hres
= disp_call(ctx
->parser
->script
, V_DISPATCH(objv
), DISPID_VALUE
, DISPATCH_METHOD
, &dp
,
1756 do_ret
? &v
: NULL
, &ctx
->ei
, NULL
/*FIXME*/);
1760 stack_popn(ctx
, argn
+1);
1761 return do_ret
? stack_push(ctx
, &v
) : S_OK
;
1765 /* ECMA-262 3rd Edition 11.2.3 */
1766 static HRESULT
interp_call_member(exec_ctx_t
*ctx
)
1768 const unsigned argn
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1769 const int do_ret
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.lng
;
1776 TRACE("%d %d\n", argn
, do_ret
);
1778 obj
= stack_topn_objid(ctx
, argn
, &id
);
1780 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, id
, NULL
);
1782 jsstack_to_dp(ctx
, argn
, &dp
);
1783 hres
= disp_call(ctx
->parser
->script
, obj
, id
, DISPATCH_METHOD
, &dp
, do_ret
? &v
: NULL
, &ctx
->ei
, NULL
/*FIXME*/);
1787 stack_popn(ctx
, argn
+2);
1788 return do_ret
? stack_push(ctx
, &v
) : S_OK
;
1792 /* ECMA-262 3rd Edition 11.1.1 */
1793 static HRESULT
interp_this(exec_ctx_t
*ctx
)
1799 V_VT(&v
) = VT_DISPATCH
;
1800 V_DISPATCH(&v
) = ctx
->this_obj
;
1801 IDispatch_AddRef(ctx
->this_obj
);
1802 return stack_push(ctx
, &v
);
1805 /* ECMA-262 3rd Edition 10.1.4 */
1806 HRESULT
identifier_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1808 identifier_expression_t
*expr
= (identifier_expression_t
*)_expr
;
1814 identifier
= SysAllocString(expr
->identifier
);
1816 return E_OUTOFMEMORY
;
1818 hres
= identifier_eval(ctx
, identifier
, flags
, ei
, ret
);
1820 SysFreeString(identifier
);
1824 /* ECMA-262 3rd Edition 10.1.4 */
1825 static HRESULT
interp_ident(exec_ctx_t
*ctx
)
1827 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
1832 TRACE("%s\n", debugstr_w(arg
));
1834 hres
= identifier_eval(ctx
->parser
->script
, arg
, 0, &ctx
->ei
, &exprval
);
1838 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &ctx
->ei
, &v
);
1839 exprval_release(&exprval
);
1843 return stack_push(ctx
, &v
);
1846 /* ECMA-262 3rd Edition 10.1.4 */
1847 static HRESULT
interp_identid(exec_ctx_t
*ctx
)
1849 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
1850 const unsigned flags
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.uint
;
1854 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1856 hres
= identifier_eval(ctx
->parser
->script
, arg
, (flags
&fdexNameEnsure
) ? EXPR_NEWREF
: 0, &ctx
->ei
, &exprval
);
1860 if(exprval
.type
!= EXPRVAL_IDREF
) {
1861 WARN("invalid ref\n");
1862 exprval_release(&exprval
);
1863 return stack_push_objid(ctx
, NULL
, JS_E_OBJECT_EXPECTED
);
1866 return stack_push_objid(ctx
, exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
);
1869 /* ECMA-262 3rd Edition 7.8.1 */
1870 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1877 return stack_push(ctx
, &v
);
1880 /* ECMA-262 3rd Edition 7.8.2 */
1881 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1883 const LONG arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1885 TRACE("%s\n", arg
? "true" : "false");
1887 return stack_push_bool(ctx
, arg
);
1890 /* ECMA-262 3rd Edition 7.8.3 */
1891 static HRESULT
interp_int(exec_ctx_t
*ctx
)
1893 const LONG arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1900 return stack_push(ctx
, &v
);
1903 /* ECMA-262 3rd Edition 7.8.3 */
1904 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1906 const double arg
= *ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.dbl
;
1909 TRACE("%lf\n", arg
);
1913 return stack_push(ctx
, &v
);
1916 /* ECMA-262 3rd Edition 7.8.4 */
1917 static HRESULT
interp_str(exec_ctx_t
*ctx
)
1919 const WCHAR
*str
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.str
;
1922 TRACE("%s\n", debugstr_w(str
));
1925 V_BSTR(&v
) = SysAllocString(str
);
1927 return E_OUTOFMEMORY
;
1929 return stack_push(ctx
, &v
);
1932 /* ECMA-262 3rd Edition 7.8 */
1933 static HRESULT
interp_regexp(exec_ctx_t
*ctx
)
1935 const WCHAR
*source
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.str
;
1936 const LONG flags
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.lng
;
1941 TRACE("%s %x\n", debugstr_w(source
), flags
);
1943 hres
= create_regexp(ctx
->parser
->script
, source
, strlenW(source
), flags
, ®exp
);
1947 var_set_jsdisp(&v
, regexp
);
1948 return stack_push(ctx
, &v
);
1951 /* ECMA-262 3rd Edition 11.1.4 */
1952 static HRESULT
interp_carray(exec_ctx_t
*ctx
)
1954 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1962 hres
= create_array(ctx
->parser
->script
, arg
, &array
);
1969 hres
= jsdisp_propput_idx(array
, i
, v
, &ctx
->ei
, NULL
/*FIXME*/);
1972 jsdisp_release(array
);
1977 var_set_jsdisp(&r
, array
);
1978 return stack_push(ctx
, &r
);
1981 /* ECMA-262 3rd Edition 11.1.5 */
1982 HRESULT
property_value_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
1984 property_value_expression_t
*expr
= (property_value_expression_t
*)_expr
;
1994 hres
= create_object(ctx
, NULL
, &obj
);
1998 for(iter
= expr
->property_list
; iter
; iter
= iter
->next
) {
1999 hres
= literal_to_var(ctx
, iter
->name
, &tmp
);
2003 hres
= to_string(ctx
, &tmp
, ei
, &name
);
2008 hres
= expr_eval(ctx
, iter
->value
, 0, ei
, &exprval
);
2009 if(SUCCEEDED(hres
)) {
2010 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
2011 exprval_release(&exprval
);
2012 if(SUCCEEDED(hres
)) {
2013 hres
= jsdisp_propput_name(obj
, name
, &val
, ei
, NULL
/*FIXME*/);
2018 SysFreeString(name
);
2024 jsdisp_release(obj
);
2028 ret
->type
= EXPRVAL_VARIANT
;
2029 var_set_jsdisp(&ret
->u
.var
, obj
);
2033 /* ECMA-262 3rd Edition 11.11 */
2034 static HRESULT
interp_jmp_nz(exec_ctx_t
*ctx
)
2036 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
2042 hres
= to_boolean(stack_top(ctx
), &b
);
2055 /* ECMA-262 3rd Edition 11.11 */
2056 static HRESULT
interp_jmp_z(exec_ctx_t
*ctx
)
2058 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
2064 hres
= to_boolean(stack_top(ctx
), &b
);
2077 /* ECMA-262 3rd Edition 11.10 */
2078 static HRESULT
interp_or(exec_ctx_t
*ctx
)
2085 hres
= stack_pop_int(ctx
, &r
);
2089 hres
= stack_pop_int(ctx
, &l
);
2093 return stack_push_int(ctx
, l
|r
);
2096 /* ECMA-262 3rd Edition 11.10 */
2097 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
2104 hres
= stack_pop_int(ctx
, &r
);
2108 hres
= stack_pop_int(ctx
, &l
);
2112 return stack_push_int(ctx
, l
^r
);
2115 /* ECMA-262 3rd Edition 11.10 */
2116 static HRESULT
interp_and(exec_ctx_t
*ctx
)
2123 hres
= stack_pop_int(ctx
, &r
);
2127 hres
= stack_pop_int(ctx
, &l
);
2131 return stack_push_int(ctx
, l
&r
);
2134 /* ECMA-262 3rd Edition 11.8.6 */
2135 static HRESULT
interp_instanceof(exec_ctx_t
*ctx
)
2137 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
2142 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2145 if(V_VT(v
) != VT_DISPATCH
|| !V_DISPATCH(v
)) {
2147 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
2150 obj
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(v
));
2151 IDispatch_Release(V_DISPATCH(v
));
2153 FIXME("non-jsdisp objects not supported\n");
2157 if(is_class(obj
, JSCLASS_FUNCTION
)) {
2158 hres
= jsdisp_propget_name(obj
, prototypeW
, &prot
, &ctx
->ei
, NULL
/*FIXME*/);
2160 hres
= throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
2162 jsdisp_release(obj
);
2168 if(V_VT(&prot
) == VT_DISPATCH
) {
2169 if(V_VT(v
) == VT_DISPATCH
)
2170 tmp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(v
));
2171 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
2172 hres
= disp_cmp(V_DISPATCH(&prot
), to_disp(iter
), &ret
);
2178 jsdisp_release(tmp
);
2180 FIXME("prototype is not an object\n");
2184 VariantClear(&prot
);
2189 return stack_push_bool(ctx
, ret
);
2192 /* ECMA-262 3rd Edition 11.8.7 */
2193 static HRESULT
interp_in(exec_ctx_t
*ctx
)
2203 obj
= stack_pop(ctx
);
2206 if(V_VT(obj
) != VT_DISPATCH
|| !V_DISPATCH(obj
)) {
2209 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
2212 hres
= to_string(ctx
->parser
->script
, v
, &ctx
->ei
, &str
);
2215 IDispatch_Release(V_DISPATCH(obj
));
2219 hres
= disp_get_id(ctx
->parser
->script
, V_DISPATCH(obj
), str
, 0, &id
);
2220 IDispatch_Release(V_DISPATCH(obj
));
2224 else if(hres
== DISP_E_UNKNOWNNAME
)
2229 return stack_push_bool(ctx
, ret
);
2232 /* ECMA-262 3rd Edition 11.6.1 */
2233 static HRESULT
add_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2238 hres
= to_primitive(ctx
, lval
, ei
, &l
, NO_HINT
);
2242 hres
= to_primitive(ctx
, rval
, ei
, &r
, NO_HINT
);
2248 if(V_VT(&l
) == VT_BSTR
|| V_VT(&r
) == VT_BSTR
) {
2249 BSTR lstr
= NULL
, rstr
= NULL
;
2251 if(V_VT(&l
) == VT_BSTR
)
2254 hres
= to_string(ctx
, &l
, ei
, &lstr
);
2256 if(SUCCEEDED(hres
)) {
2257 if(V_VT(&r
) == VT_BSTR
)
2260 hres
= to_string(ctx
, &r
, ei
, &rstr
);
2263 if(SUCCEEDED(hres
)) {
2266 len1
= SysStringLen(lstr
);
2267 len2
= SysStringLen(rstr
);
2269 V_VT(retv
) = VT_BSTR
;
2270 V_BSTR(retv
) = SysAllocStringLen(NULL
, len1
+len2
);
2271 memcpy(V_BSTR(retv
), lstr
, len1
*sizeof(WCHAR
));
2272 memcpy(V_BSTR(retv
)+len1
, rstr
, (len2
+1)*sizeof(WCHAR
));
2275 if(V_VT(&l
) != VT_BSTR
)
2276 SysFreeString(lstr
);
2277 if(V_VT(&r
) != VT_BSTR
)
2278 SysFreeString(rstr
);
2282 hres
= to_number(ctx
, &l
, ei
, &nl
);
2283 if(SUCCEEDED(hres
)) {
2284 hres
= to_number(ctx
, &r
, ei
, &nr
);
2286 num_set_val(retv
, num_val(&nl
) + num_val(&nr
));
2295 /* ECMA-262 3rd Edition 11.6.1 */
2296 static HRESULT
interp_add(exec_ctx_t
*ctx
)
2298 VARIANT
*l
, *r
, ret
;
2304 TRACE("%s + %s\n", debugstr_variant(l
), debugstr_variant(r
));
2306 hres
= add_eval(ctx
->parser
->script
, l
, r
, &ctx
->ei
, &ret
);
2312 return stack_push(ctx
, &ret
);
2315 /* ECMA-262 3rd Edition 11.6.2 */
2316 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
2323 hres
= stack_pop_number(ctx
, &r
);
2327 hres
= stack_pop_number(ctx
, &l
);
2331 return stack_push_number(ctx
, num_val(&l
)-num_val(&r
));
2334 /* ECMA-262 3rd Edition 11.5.1 */
2335 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
2342 hres
= stack_pop_number(ctx
, &r
);
2346 hres
= stack_pop_number(ctx
, &l
);
2350 return stack_push_number(ctx
, num_val(&l
)*num_val(&r
));
2353 /* ECMA-262 3rd Edition 11.5.2 */
2354 static HRESULT
interp_div(exec_ctx_t
*ctx
)
2361 hres
= stack_pop_number(ctx
, &r
);
2365 hres
= stack_pop_number(ctx
, &l
);
2369 return stack_push_number(ctx
, num_val(&l
)/num_val(&r
));
2372 /* ECMA-262 3rd Edition 11.5.3 */
2373 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
2380 hres
= stack_pop_number(ctx
, &r
);
2384 hres
= stack_pop_number(ctx
, &l
);
2388 return stack_push_number(ctx
, fmod(num_val(&l
), num_val(&r
)));
2391 /* ECMA-262 3rd Edition 11.4.2 */
2392 static HRESULT
interp_delete(exec_ctx_t
*ctx
)
2394 VARIANT
*obj_var
, *name_var
;
2395 IDispatchEx
*dispex
;
2403 name_var
= stack_pop(ctx
);
2404 obj_var
= stack_pop(ctx
);
2406 hres
= to_object(ctx
->parser
->script
, obj_var
, &obj
);
2407 VariantClear(obj_var
);
2409 VariantClear(name_var
);
2413 hres
= to_string(ctx
->parser
->script
, name_var
, &ctx
->ei
, &name
);
2414 VariantClear(name_var
);
2416 IDispatch_Release(obj
);
2420 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
2421 if(SUCCEEDED(hres
)) {
2422 hres
= IDispatchEx_DeleteMemberByName(dispex
, name
, make_grfdex(ctx
->parser
->script
, fdexNameCaseSensitive
));
2424 IDispatchEx_Release(dispex
);
2430 IDispatch_Release(obj
);
2431 SysFreeString(name
);
2435 return stack_push_bool(ctx
, ret
);
2438 /* ECMA-262 3rd Edition 11.4.2 */
2439 static HRESULT
interp_delete_ident(exec_ctx_t
*ctx
)
2441 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
2442 IDispatchEx
*dispex
;
2447 TRACE("%s\n", debugstr_w(arg
));
2449 hres
= identifier_eval(ctx
->parser
->script
, arg
, 0, &ctx
->ei
, &exprval
);
2453 if(exprval
.type
!= EXPRVAL_IDREF
) {
2454 FIXME("Unsupported exprval\n");
2455 exprval_release(&exprval
);
2459 hres
= IDispatch_QueryInterface(exprval
.u
.idref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2460 IDispatch_Release(exprval
.u
.idref
.disp
);
2461 if(SUCCEEDED(hres
)) {
2462 hres
= IDispatchEx_DeleteMemberByDispID(dispex
, exprval
.u
.idref
.id
);
2463 IDispatchEx_Release(dispex
);
2470 return stack_push_bool(ctx
, ret
);
2473 /* ECMA-262 3rd Edition 11.4.2 */
2474 static HRESULT
interp_void(exec_ctx_t
*ctx
)
2482 V_VT(&v
) = VT_EMPTY
;
2483 return stack_push(ctx
, &v
);
2486 /* ECMA-262 3rd Edition 11.4.3 */
2487 static HRESULT
typeof_string(VARIANT
*v
, const WCHAR
**ret
)
2509 if(V_DISPATCH(v
) && (dispex
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(v
)))) {
2510 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
2511 jsdisp_release(dispex
);
2518 FIXME("unhandled vt %d\n", V_VT(v
));
2525 /* ECMA-262 3rd Edition 11.4.3 */
2526 static HRESULT
interp_typeofid(exec_ctx_t
*ctx
)
2534 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
2538 obj
= stack_pop_objid(ctx
, &id
);
2540 return stack_push_string(ctx
, undefinedW
);
2542 V_VT(&v
) = VT_EMPTY
;
2543 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
2544 IDispatch_Release(obj
);
2546 return stack_push_string(ctx
, unknownW
);
2548 hres
= typeof_string(&v
, &ret
);
2553 return stack_push_string(ctx
, ret
);
2556 /* ECMA-262 3rd Edition 11.4.3 */
2557 static HRESULT
interp_typeofident(exec_ctx_t
*ctx
)
2559 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
2565 TRACE("%s\n", debugstr_w(arg
));
2567 hres
= identifier_eval(ctx
->parser
->script
, arg
, 0, &ctx
->ei
, &exprval
);
2571 if(exprval
.type
== EXPRVAL_INVALID
) {
2572 hres
= stack_push_string(ctx
, undefinedW
);
2573 exprval_release(&exprval
);
2577 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &ctx
->ei
, &v
);
2578 exprval_release(&exprval
);
2582 hres
= typeof_string(&v
, &ret
);
2587 return stack_push_string(ctx
, ret
);
2590 /* ECMA-262 3rd Edition 11.4.3 */
2591 static HRESULT
interp_typeof(exec_ctx_t
*ctx
)
2600 hres
= typeof_string(v
, &ret
);
2605 return stack_push_string(ctx
, ret
);
2608 /* ECMA-262 3rd Edition 11.4.7 */
2609 static HRESULT
interp_minus(exec_ctx_t
*ctx
)
2616 hres
= stack_pop_number(ctx
, &n
);
2620 return stack_push_number(ctx
, -num_val(&n
));
2623 /* ECMA-262 3rd Edition 11.4.6 */
2624 static HRESULT
interp_tonum(exec_ctx_t
*ctx
)
2632 hres
= to_number(ctx
->parser
->script
, v
, &ctx
->ei
, &num
);
2637 return stack_push(ctx
, &num
);
2640 /* ECMA-262 3rd Edition 11.3.1 */
2641 static HRESULT
interp_postinc(exec_ctx_t
*ctx
)
2643 const int arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
2651 obj
= stack_pop_objid(ctx
, &id
);
2653 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
2655 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
2656 if(SUCCEEDED(hres
)) {
2659 hres
= to_number(ctx
->parser
->script
, &v
, &ctx
->ei
, &n
);
2660 if(SUCCEEDED(hres
)) {
2661 num_set_val(&inc
, num_val(&n
)+(double)arg
);
2662 hres
= disp_propput(ctx
->parser
->script
, obj
, id
, &inc
, &ctx
->ei
, NULL
/*FIXME*/);
2667 IDispatch_Release(obj
);
2671 return stack_push(ctx
, &v
);
2674 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2675 static HRESULT
interp_preinc(exec_ctx_t
*ctx
)
2677 const int arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
2685 obj
= stack_pop_objid(ctx
, &id
);
2687 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
2689 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
2690 if(SUCCEEDED(hres
)) {
2693 hres
= to_number(ctx
->parser
->script
, &v
, &ctx
->ei
, &n
);
2695 if(SUCCEEDED(hres
)) {
2696 num_set_val(&v
, num_val(&n
)+(double)arg
);
2697 hres
= disp_propput(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
2700 IDispatch_Release(obj
);
2704 return stack_push(ctx
, &v
);
2707 /* ECMA-262 3rd Edition 11.9.3 */
2708 static HRESULT
equal_values(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, BOOL
*ret
)
2710 if(V_VT(lval
) == V_VT(rval
) || (is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
))))
2711 return equal2_values(lval
, rval
, ret
);
2713 /* FIXME: NULL disps should be handled in more general way */
2714 if(V_VT(lval
) == VT_DISPATCH
&& !V_DISPATCH(lval
)) {
2717 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2720 if(V_VT(rval
) == VT_DISPATCH
&& !V_DISPATCH(rval
)) {
2723 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2726 if((V_VT(lval
) == VT_NULL
&& V_VT(rval
) == VT_EMPTY
) ||
2727 (V_VT(lval
) == VT_EMPTY
&& V_VT(rval
) == VT_NULL
)) {
2732 if(V_VT(lval
) == VT_BSTR
&& is_num_vt(V_VT(rval
))) {
2736 hres
= to_number(ctx
, lval
, ei
, &v
);
2740 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2743 if(V_VT(rval
) == VT_BSTR
&& is_num_vt(V_VT(lval
))) {
2747 hres
= to_number(ctx
, rval
, ei
, &v
);
2751 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2754 if(V_VT(rval
) == VT_BOOL
) {
2758 V_I4(&v
) = V_BOOL(rval
) ? 1 : 0;
2759 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2762 if(V_VT(lval
) == VT_BOOL
) {
2766 V_I4(&v
) = V_BOOL(lval
) ? 1 : 0;
2767 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2771 if(V_VT(rval
) == VT_DISPATCH
&& (V_VT(lval
) == VT_BSTR
|| is_num_vt(V_VT(lval
)))) {
2775 hres
= to_primitive(ctx
, rval
, ei
, &v
, NO_HINT
);
2779 hres
= equal_values(ctx
, lval
, &v
, ei
, ret
);
2786 if(V_VT(lval
) == VT_DISPATCH
&& (V_VT(rval
) == VT_BSTR
|| is_num_vt(V_VT(rval
)))) {
2790 hres
= to_primitive(ctx
, lval
, ei
, &v
, NO_HINT
);
2794 hres
= equal_values(ctx
, &v
, rval
, ei
, ret
);
2805 /* ECMA-262 3rd Edition 11.9.1 */
2806 static HRESULT
interp_eq(exec_ctx_t
*ctx
)
2815 TRACE("%s == %s\n", debugstr_variant(l
), debugstr_variant(r
));
2817 hres
= equal_values(ctx
->parser
->script
, l
, r
, &ctx
->ei
, &b
);
2823 return stack_push_bool(ctx
, b
);
2826 /* ECMA-262 3rd Edition 11.9.2 */
2827 static HRESULT
interp_neq(exec_ctx_t
*ctx
)
2836 TRACE("%s != %s\n", debugstr_variant(l
), debugstr_variant(r
));
2838 hres
= equal_values(ctx
->parser
->script
, l
, r
, &ctx
->ei
, &b
);
2844 return stack_push_bool(ctx
, !b
);
2847 /* ECMA-262 3rd Edition 11.9.4 */
2848 static HRESULT
interp_eq2(exec_ctx_t
*ctx
)
2859 hres
= equal2_values(r
, l
, &b
);
2865 return stack_push_bool(ctx
, b
);
2868 /* ECMA-262 3rd Edition 11.9.5 */
2869 static HRESULT
interp_neq2(exec_ctx_t
*ctx
)
2880 hres
= equal2_values(r
, l
, &b
);
2886 return stack_push_bool(ctx
, !b
);
2889 /* ECMA-262 3rd Edition 11.8.5 */
2890 static HRESULT
less_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, BOOL greater
, jsexcept_t
*ei
, BOOL
*ret
)
2892 VARIANT l
, r
, ln
, rn
;
2895 hres
= to_primitive(ctx
, lval
, ei
, &l
, NO_HINT
);
2899 hres
= to_primitive(ctx
, rval
, ei
, &r
, NO_HINT
);
2905 if(V_VT(&l
) == VT_BSTR
&& V_VT(&r
) == VT_BSTR
) {
2906 *ret
= (strcmpW(V_BSTR(&l
), V_BSTR(&r
)) < 0) ^ greater
;
2907 SysFreeString(V_BSTR(&l
));
2908 SysFreeString(V_BSTR(&r
));
2912 hres
= to_number(ctx
, &l
, ei
, &ln
);
2915 hres
= to_number(ctx
, &r
, ei
, &rn
);
2920 if(V_VT(&ln
) == VT_I4
&& V_VT(&rn
) == VT_I4
) {
2921 *ret
= (V_I4(&ln
) < V_I4(&rn
)) ^ greater
;
2923 DOUBLE ld
= num_val(&ln
);
2924 DOUBLE rd
= num_val(&rn
);
2926 *ret
= !isnan(ld
) && !isnan(rd
) && ((ld
< rd
) ^ greater
);
2932 /* ECMA-262 3rd Edition 11.8.1 */
2933 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
2942 TRACE("%s < %s\n", debugstr_variant(l
), debugstr_variant(r
));
2944 hres
= less_eval(ctx
->parser
->script
, l
, r
, FALSE
, &ctx
->ei
, &b
);
2950 return stack_push_bool(ctx
, b
);
2953 /* ECMA-262 3rd Edition 11.8.1 */
2954 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
2963 TRACE("%s <= %s\n", debugstr_variant(l
), debugstr_variant(r
));
2965 hres
= less_eval(ctx
->parser
->script
, r
, l
, TRUE
, &ctx
->ei
, &b
);
2971 return stack_push_bool(ctx
, b
);
2974 /* ECMA-262 3rd Edition 11.8.2 */
2975 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
2984 TRACE("%s > %s\n", debugstr_variant(l
), debugstr_variant(r
));
2986 hres
= less_eval(ctx
->parser
->script
, r
, l
, FALSE
, &ctx
->ei
, &b
);
2992 return stack_push_bool(ctx
, b
);
2995 /* ECMA-262 3rd Edition 11.8.4 */
2996 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
3005 TRACE("%s >= %s\n", debugstr_variant(l
), debugstr_variant(r
));
3007 hres
= less_eval(ctx
->parser
->script
, l
, r
, TRUE
, &ctx
->ei
, &b
);
3013 return stack_push_bool(ctx
, b
);
3016 /* ECMA-262 3rd Edition 11.4.8 */
3017 static HRESULT
interp_bneg(exec_ctx_t
*ctx
)
3026 hres
= to_int32(ctx
->parser
->script
, v
, &ctx
->ei
, &i
);
3033 return stack_push(ctx
, &r
);
3036 /* ECMA-262 3rd Edition 11.4.9 */
3037 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
3046 hres
= to_boolean(v
, &b
);
3051 return stack_push_bool(ctx
, !b
);
3054 /* ECMA-262 3rd Edition 11.7.1 */
3055 static HRESULT
interp_lshift(exec_ctx_t
*ctx
)
3061 hres
= stack_pop_uint(ctx
, &r
);
3065 hres
= stack_pop_int(ctx
, &l
);
3069 return stack_push_int(ctx
, l
<< (r
&0x1f));
3072 /* ECMA-262 3rd Edition 11.7.2 */
3073 static HRESULT
interp_rshift(exec_ctx_t
*ctx
)
3079 hres
= stack_pop_uint(ctx
, &r
);
3083 hres
= stack_pop_int(ctx
, &l
);
3087 return stack_push_int(ctx
, l
>> (r
&0x1f));
3090 /* ECMA-262 3rd Edition 11.7.3 */
3091 static HRESULT
interp_rshift2(exec_ctx_t
*ctx
)
3096 hres
= stack_pop_uint(ctx
, &r
);
3100 hres
= stack_pop_uint(ctx
, &l
);
3104 return stack_push_int(ctx
, l
>> (r
&0x1f));
3107 /* ECMA-262 3rd Edition 11.13.1 */
3108 static HRESULT
interp_assign(exec_ctx_t
*ctx
)
3118 disp
= stack_pop_objid(ctx
, &id
);
3121 return throw_reference_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_ILLEGAL_ASSIGN
, NULL
);
3123 hres
= disp_propput(ctx
->parser
->script
, disp
, id
, v
, &ctx
->ei
, NULL
/*FIXME*/);
3124 IDispatch_Release(disp
);
3130 return stack_push(ctx
, v
);
3133 static HRESULT
interp_undefined(exec_ctx_t
*ctx
)
3139 V_VT(&v
) = VT_EMPTY
;
3140 return stack_push(ctx
, &v
);
3143 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
3145 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
3153 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
3161 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
3169 static HRESULT
interp_tree(exec_ctx_t
*ctx
)
3171 instr_t
*instr
= ctx
->parser
->code
->instrs
+ctx
->ip
;
3178 hres
= expr_eval(ctx
->parser
->script
, instr
->arg1
.expr
, 0, &ctx
->ei
, &val
);
3182 hres
= exprval_to_value(ctx
->parser
->script
, &val
, &ctx
->ei
, &v
);
3183 exprval_release(&val
);
3187 return stack_push(ctx
, &v
);
3190 typedef HRESULT (*op_func_t
)(exec_ctx_t
*);
3192 static const op_func_t op_funcs
[] = {
3193 #define X(x,a,b,c) interp_##x,
3198 static const unsigned op_move
[] = {
3199 #define X(a,x,b,c) x,
3204 static HRESULT
interp_expression_eval(script_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3206 exec_ctx_t
*exec_ctx
= ctx
->exec_ctx
;
3207 unsigned prev_ip
, prev_top
;
3209 HRESULT hres
= S_OK
;
3213 prev_top
= exec_ctx
->top
;
3214 prev_ip
= exec_ctx
->ip
;
3215 exec_ctx
->ip
= expr
->instr_off
;
3217 while(exec_ctx
->ip
!= -1) {
3218 op
= exec_ctx
->parser
->code
->instrs
[exec_ctx
->ip
].op
;
3219 hres
= op_funcs
[op
](exec_ctx
);
3222 exec_ctx
->ip
+= op_move
[op
];
3225 exec_ctx
->ip
= prev_ip
;
3228 stack_popn(exec_ctx
, exec_ctx
->top
-prev_top
);
3230 memset(&exec_ctx
->ei
, 0, sizeof(exec_ctx
->ei
));
3234 assert(exec_ctx
->top
== prev_top
+1 || ((flags
&EXPR_NOVAL
) && exec_ctx
->top
== prev_top
));
3236 ret
->type
= EXPRVAL_VARIANT
;
3237 if(exec_ctx
->top
== prev_top
)
3238 V_VT(&ret
->u
.var
) = VT_EMPTY
;
3240 ret
->u
.var
= *stack_pop(exec_ctx
);
3244 HRESULT
compiled_expression_eval(script_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3250 hres
= compile_subscript(ctx
->exec_ctx
->parser
, expr
, !(flags
& EXPR_NOVAL
), &expr
->instr_off
);
3254 if(expr
->eval
== compiled_expression_eval
)
3255 expr
->eval
= interp_expression_eval
;
3257 return expr
->eval(ctx
, expr
, flags
, ei
, ret
);