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
34 static const WCHAR booleanW
[] = {'b','o','o','l','e','a','n',0};
35 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
36 static const WCHAR numberW
[] = {'n','u','m','b','e','r',0};
37 static const WCHAR objectW
[] = {'o','b','j','e','c','t',0};
38 static const WCHAR stringW
[] = {'s','t','r','i','n','g',0};
39 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
40 static const WCHAR unknownW
[] = {'u','n','k','n','o','w','n',0};
42 struct _return_type_t
{
52 static inline HRESULT
stat_eval(script_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
54 return stat
->eval(ctx
, stat
, rt
, ret
);
57 static HRESULT
expr_eval(script_ctx_t
*,expression_t
*,DWORD
,jsexcept_t
*,exprval_t
*);
59 static HRESULT
stack_push(exec_ctx_t
*ctx
, VARIANT
*v
)
61 if(!ctx
->stack_size
) {
62 ctx
->stack
= heap_alloc(16*sizeof(VARIANT
));
66 }else if(ctx
->stack_size
== ctx
->top
) {
69 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(VARIANT
));
75 ctx
->stack
= new_stack
;
79 ctx
->stack
[ctx
->top
++] = *v
;
83 static HRESULT
stack_push_bool(exec_ctx_t
*ctx
, BOOL b
)
88 V_BOOL(&v
) = b
? VARIANT_TRUE
: VARIANT_FALSE
;
89 return stack_push(ctx
, &v
);
92 static inline HRESULT
stack_push_number(exec_ctx_t
*ctx
, double number
)
96 num_set_val(&v
, number
);
97 return stack_push(ctx
, &v
);
100 static inline HRESULT
stack_push_int(exec_ctx_t
*ctx
, INT n
)
106 return stack_push(ctx
, &v
);
109 static inline HRESULT
stack_push_string(exec_ctx_t
*ctx
, const WCHAR
*str
)
114 V_BSTR(&v
) = SysAllocString(str
);
115 return V_BSTR(&v
) ? stack_push(ctx
, &v
) : E_OUTOFMEMORY
;
118 static HRESULT
stack_push_objid(exec_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
)
123 V_VT(&v
) = VT_DISPATCH
;
124 V_DISPATCH(&v
) = disp
;
125 hres
= stack_push(ctx
, &v
);
131 return stack_push(ctx
, &v
);
134 static inline VARIANT
*stack_top(exec_ctx_t
*ctx
)
137 return ctx
->stack
+ ctx
->top
-1;
140 static inline VARIANT
*stack_topn(exec_ctx_t
*ctx
, unsigned n
)
142 assert(ctx
->top
> n
);
143 return ctx
->stack
+ ctx
->top
-1-n
;
146 static inline VARIANT
*stack_pop(exec_ctx_t
*ctx
)
149 return ctx
->stack
+ --ctx
->top
;
152 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
155 VariantClear(stack_pop(ctx
));
158 static HRESULT
stack_pop_number(exec_ctx_t
*ctx
, VARIANT
*r
)
164 hres
= to_number(ctx
->parser
->script
, v
, &ctx
->ei
, r
);
169 static HRESULT
stack_pop_object(exec_ctx_t
*ctx
, IDispatch
**r
)
175 if(V_VT(v
) == VT_DISPATCH
) {
177 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_OBJECT_REQUIRED
, NULL
);
182 hres
= to_object(ctx
->parser
->script
, v
, r
);
187 static inline HRESULT
stack_pop_int(exec_ctx_t
*ctx
, INT
*r
)
189 return to_int32(ctx
->parser
->script
, stack_pop(ctx
), &ctx
->ei
, r
);
192 static inline HRESULT
stack_pop_uint(exec_ctx_t
*ctx
, DWORD
*r
)
194 return to_uint32(ctx
->parser
->script
, stack_pop(ctx
), &ctx
->ei
, r
);
197 static inline IDispatch
*stack_pop_objid(exec_ctx_t
*ctx
, DISPID
*id
)
199 assert(V_VT(stack_top(ctx
)) == VT_INT
&& V_VT(stack_topn(ctx
, 1)) == VT_DISPATCH
);
201 *id
= V_INT(stack_pop(ctx
));
202 return V_DISPATCH(stack_pop(ctx
));
205 static inline IDispatch
*stack_topn_objid(exec_ctx_t
*ctx
, unsigned n
, DISPID
*id
)
207 assert(V_VT(stack_topn(ctx
, n
)) == VT_INT
&& V_VT(stack_topn(ctx
, n
+1)) == VT_DISPATCH
);
209 *id
= V_INT(stack_topn(ctx
, n
));
210 return V_DISPATCH(stack_topn(ctx
, n
+1));
213 static void exprval_release(exprval_t
*val
)
216 case EXPRVAL_VARIANT
:
217 if(V_VT(&val
->u
.var
) != VT_EMPTY
)
218 VariantClear(&val
->u
.var
);
221 if(val
->u
.idref
.disp
)
222 IDispatch_Release(val
->u
.idref
.disp
);
224 case EXPRVAL_INVALID
:
225 SysFreeString(val
->u
.identifier
);
229 /* ECMA-262 3rd Edition 8.7.1 */
230 static HRESULT
exprval_value(script_ctx_t
*ctx
, exprval_t
*val
, jsexcept_t
*ei
, VARIANT
*ret
)
232 V_VT(ret
) = VT_EMPTY
;
235 case EXPRVAL_VARIANT
:
236 return VariantCopy(ret
, &val
->u
.var
);
238 if(!val
->u
.idref
.disp
) {
239 FIXME("throw ReferenceError\n");
243 return disp_propget(ctx
, val
->u
.idref
.disp
, val
->u
.idref
.id
, ret
, ei
, NULL
/*FIXME*/);
244 case EXPRVAL_INVALID
:
245 return throw_type_error(ctx
, ei
, JS_E_UNDEFINED_VARIABLE
, val
->u
.identifier
);
248 ERR("type %d\n", val
->type
);
252 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*val
, jsexcept_t
*ei
, VARIANT
*ret
)
254 if(val
->type
== EXPRVAL_VARIANT
) {
256 V_VT(&val
->u
.var
) = VT_EMPTY
;
260 return exprval_value(ctx
, val
, ei
, ret
);
263 static HRESULT
exprval_to_boolean(script_ctx_t
*ctx
, exprval_t
*exprval
, jsexcept_t
*ei
, VARIANT_BOOL
*b
)
265 if(exprval
->type
!= EXPRVAL_VARIANT
) {
269 hres
= exprval_to_value(ctx
, exprval
, ei
, &val
);
273 hres
= to_boolean(&val
, b
);
278 return to_boolean(&exprval
->u
.var
, b
);
281 static void exprval_set_idref(exprval_t
*val
, IDispatch
*disp
, DISPID id
)
283 val
->type
= EXPRVAL_IDREF
;
284 val
->u
.idref
.disp
= disp
;
285 val
->u
.idref
.id
= id
;
288 IDispatch_AddRef(disp
);
291 HRESULT
scope_push(scope_chain_t
*scope
, jsdisp_t
*obj
, scope_chain_t
**ret
)
293 scope_chain_t
*new_scope
;
295 new_scope
= heap_alloc(sizeof(scope_chain_t
));
297 return E_OUTOFMEMORY
;
302 new_scope
->obj
= obj
;
306 new_scope
->next
= scope
;
308 new_scope
->next
= NULL
;
315 static void scope_pop(scope_chain_t
**scope
)
324 void scope_release(scope_chain_t
*scope
)
330 scope_release(scope
->next
);
332 jsdisp_release(scope
->obj
);
336 HRESULT
create_exec_ctx(script_ctx_t
*script_ctx
, IDispatch
*this_obj
, jsdisp_t
*var_disp
,
337 scope_chain_t
*scope
, BOOL is_global
, exec_ctx_t
**ret
)
341 ctx
= heap_alloc_zero(sizeof(exec_ctx_t
));
343 return E_OUTOFMEMORY
;
346 ctx
->is_global
= is_global
;
349 ctx
->this_obj
= this_obj
;
350 else if(script_ctx
->host_global
)
351 ctx
->this_obj
= script_ctx
->host_global
;
353 ctx
->this_obj
= to_disp(script_ctx
->global
);
354 IDispatch_AddRef(ctx
->this_obj
);
356 jsdisp_addref(var_disp
);
357 ctx
->var_disp
= var_disp
;
361 ctx
->scope_chain
= scope
;
368 void exec_release(exec_ctx_t
*ctx
)
374 scope_release(ctx
->scope_chain
);
376 jsdisp_release(ctx
->var_disp
);
378 IDispatch_Release(ctx
->this_obj
);
379 heap_free(ctx
->stack
);
383 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, BSTR name
, DWORD flags
, DISPID
*id
)
388 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
390 TRACE("unsing IDispatch\n");
393 return IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &name
, 1, 0, id
);
397 hres
= IDispatchEx_GetDispID(dispex
, name
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
398 IDispatchEx_Release(dispex
);
402 /* ECMA-262 3rd Edition 8.7.2 */
403 static HRESULT
put_value(script_ctx_t
*ctx
, exprval_t
*ref
, VARIANT
*v
, jsexcept_t
*ei
)
405 if(ref
->type
!= EXPRVAL_IDREF
)
406 return throw_reference_error(ctx
, ei
, JS_E_ILLEGAL_ASSIGN
, NULL
);
408 return disp_propput(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, v
, ei
, NULL
/*FIXME*/);
411 static inline BOOL
is_null(const VARIANT
*v
)
413 return V_VT(v
) == VT_NULL
|| (V_VT(v
) == VT_DISPATCH
&& !V_DISPATCH(v
));
416 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
418 IObjectIdentity
*identity
;
419 IUnknown
*unk1
, *unk2
;
427 if(!disp1
|| !disp2
) {
432 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
436 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
438 IUnknown_Release(unk1
);
445 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
446 if(SUCCEEDED(hres
)) {
447 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
448 IObjectIdentity_Release(identity
);
455 IUnknown_Release(unk1
);
456 IUnknown_Release(unk2
);
460 /* ECMA-262 3rd Edition 11.9.6 */
461 static HRESULT
equal2_values(VARIANT
*lval
, VARIANT
*rval
, BOOL
*ret
)
465 if(V_VT(lval
) != V_VT(rval
)) {
466 if(is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
)))
467 *ret
= num_val(lval
) == num_val(rval
);
468 else if(is_null(lval
))
469 *ret
= is_null(rval
);
481 *ret
= V_I4(lval
) == V_I4(rval
);
484 *ret
= V_R8(lval
) == V_R8(rval
);
488 *ret
= SysStringLen(V_BSTR(rval
))?FALSE
:TRUE
;
489 else if(!V_BSTR(rval
))
490 *ret
= SysStringLen(V_BSTR(lval
))?FALSE
:TRUE
;
492 *ret
= !strcmpW(V_BSTR(lval
), V_BSTR(rval
));
495 return disp_cmp(V_DISPATCH(lval
), V_DISPATCH(rval
), ret
);
497 *ret
= !V_BOOL(lval
) == !V_BOOL(rval
);
500 FIXME("unimplemented vt %d\n", V_VT(lval
));
507 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
513 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
514 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
515 hres
= disp_get_id(ctx
, item
->disp
, identifier
, 0, &id
);
516 if(SUCCEEDED(hres
)) {
518 exprval_set_idref(ret
, item
->disp
, id
);
527 HRESULT
exec_source(exec_ctx_t
*ctx
, parser_ctx_t
*parser
, source_elements_t
*source
, BOOL from_eval
,
528 jsexcept_t
*ei
, VARIANT
*retv
)
530 script_ctx_t
*script
= parser
->script
;
531 function_declaration_t
*func
;
532 parser_ctx_t
*prev_parser
;
536 exec_ctx_t
*prev_ctx
;
540 for(func
= source
->functions
; func
; func
= func
->next
) {
544 hres
= create_source_function(parser
, func
->expr
->parameter_list
, func
->expr
->source_elements
,
545 ctx
->scope_chain
, func
->expr
->src_str
, func
->expr
->src_len
, &func_obj
);
549 var_set_jsdisp(&var
, func_obj
);
550 hres
= jsdisp_propput_name(ctx
->var_disp
, func
->expr
->identifier
, &var
, ei
, NULL
);
551 jsdisp_release(func_obj
);
556 for(var
= source
->variables
; var
; var
= var
->next
) {
560 name
= SysAllocString(var
->identifier
);
562 return E_OUTOFMEMORY
;
564 if(!ctx
->is_global
|| !lookup_global_members(parser
->script
, name
, NULL
))
565 hres
= jsdisp_get_id(ctx
->var_disp
, var
->identifier
, fdexNameEnsure
, &id
);
571 prev_ctx
= script
->exec_ctx
;
572 script
->exec_ctx
= ctx
;
574 prev_parser
= ctx
->parser
;
575 ctx
->parser
= parser
;
577 V_VT(&val
) = VT_EMPTY
;
578 memset(&rt
, 0, sizeof(rt
));
581 for(stat
= source
->statement
; stat
; stat
= stat
->next
) {
582 hres
= stat_eval(script
, stat
, &rt
, &tmp
);
588 if(rt
.type
!= RT_NORMAL
)
592 script
->exec_ctx
= prev_ctx
;
593 ctx
->parser
= prev_parser
;
595 if(rt
.type
!= RT_NORMAL
&& rt
.type
!= RT_RETURN
) {
596 FIXME("wrong rt %d\n", rt
.type
);
606 if(!retv
|| (!from_eval
&& rt
.type
!= RT_RETURN
))
613 /* ECMA-262 3rd Edition 10.1.4 */
614 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
616 scope_chain_t
*scope
;
621 TRACE("%s\n", debugstr_w(identifier
));
623 for(scope
= ctx
->exec_ctx
->scope_chain
; scope
; scope
= scope
->next
) {
624 hres
= jsdisp_get_id(scope
->obj
, identifier
, 0, &id
);
625 if(SUCCEEDED(hres
)) {
626 exprval_set_idref(ret
, to_disp(scope
->obj
), id
);
631 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
632 if(SUCCEEDED(hres
)) {
633 exprval_set_idref(ret
, to_disp(ctx
->global
), id
);
637 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
638 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
645 hres
= IActiveScriptSite_GetItemInfo(ctx
->site
, identifier
,
646 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
648 WARN("GetItemInfo failed: %08x\n", hres
);
652 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
653 IUnknown_Release(unk
);
655 WARN("object does not implement IDispatch\n");
660 ret
->type
= EXPRVAL_VARIANT
;
661 V_VT(&ret
->u
.var
) = VT_DISPATCH
;
662 V_DISPATCH(&ret
->u
.var
) = item
->disp
;
663 IDispatch_AddRef(item
->disp
);
668 if(lookup_global_members(ctx
, identifier
, ret
))
671 if(flags
& fdexNameEnsure
) {
672 hres
= jsdisp_get_id(ctx
->global
, identifier
, fdexNameEnsure
, &id
);
676 exprval_set_idref(ret
, to_disp(ctx
->global
), id
);
680 ret
->type
= EXPRVAL_INVALID
;
681 ret
->u
.identifier
= SysAllocString(identifier
);
682 if(!ret
->u
.identifier
)
683 return E_OUTOFMEMORY
;
688 /* ECMA-262 3rd Edition 12.1 */
689 HRESULT
block_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
691 block_statement_t
*stat
= (block_statement_t
*)_stat
;
698 V_VT(&val
) = VT_EMPTY
;
699 for(iter
= stat
->stat_list
; iter
; iter
= iter
->next
) {
700 hres
= stat_eval(ctx
, iter
, rt
, &tmp
);
706 if(rt
->type
!= RT_NORMAL
)
719 /* ECMA-262 3rd Edition 12.2 */
720 static HRESULT
variable_list_eval(script_ctx_t
*ctx
, variable_declaration_t
*var_list
, jsexcept_t
*ei
)
722 variable_declaration_t
*iter
;
725 for(iter
= var_list
; iter
; iter
= iter
->next
) {
732 hres
= expr_eval(ctx
, iter
->expr
, 0, ei
, &exprval
);
736 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
737 exprval_release(&exprval
);
741 hres
= jsdisp_propput_name(ctx
->exec_ctx
->var_disp
, iter
->identifier
, &val
, ei
, NULL
/*FIXME*/);
750 /* ECMA-262 3rd Edition 12.2 */
751 HRESULT
var_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
753 var_statement_t
*stat
= (var_statement_t
*)_stat
;
758 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
762 V_VT(ret
) = VT_EMPTY
;
766 /* ECMA-262 3rd Edition 12.3 */
767 HRESULT
empty_statement_eval(script_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
771 V_VT(ret
) = VT_EMPTY
;
775 /* ECMA-262 3rd Edition 12.4 */
776 HRESULT
expression_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
778 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
785 hres
= expr_eval(ctx
, stat
->expr
, EXPR_NOVAL
, &rt
->ei
, &exprval
);
789 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
790 exprval_release(&exprval
);
795 TRACE("= %s\n", debugstr_variant(ret
));
799 /* ECMA-262 3rd Edition 12.5 */
800 HRESULT
if_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
802 if_statement_t
*stat
= (if_statement_t
*)_stat
;
809 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
813 hres
= exprval_to_boolean(ctx
, &exprval
, &rt
->ei
, &b
);
814 exprval_release(&exprval
);
819 hres
= stat_eval(ctx
, stat
->if_stat
, rt
, ret
);
820 else if(stat
->else_stat
)
821 hres
= stat_eval(ctx
, stat
->else_stat
, rt
, ret
);
823 V_VT(ret
) = VT_EMPTY
;
828 /* ECMA-262 3rd Edition 12.6.2 */
829 HRESULT
while_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
831 while_statement_t
*stat
= (while_statement_t
*)_stat
;
840 V_VT(&val
) = VT_EMPTY
;
841 test_expr
= !stat
->do_while
;
845 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
849 hres
= exprval_to_boolean(ctx
, &exprval
, &rt
->ei
, &b
);
850 exprval_release(&exprval
);
851 if(FAILED(hres
) || !b
)
857 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
864 if(rt
->type
== RT_CONTINUE
)
865 rt
->type
= RT_NORMAL
;
866 if(rt
->type
!= RT_NORMAL
)
875 if(rt
->type
== RT_BREAK
)
876 rt
->type
= RT_NORMAL
;
882 /* ECMA-262 3rd Edition 12.6.3 */
883 HRESULT
for_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
885 for_statement_t
*stat
= (for_statement_t
*)_stat
;
886 VARIANT val
, tmp
, retv
;
893 if(stat
->variable_list
) {
894 hres
= variable_list_eval(ctx
, stat
->variable_list
, &rt
->ei
);
897 }else if(stat
->begin_expr
) {
898 hres
= expr_eval(ctx
, stat
->begin_expr
, 0, &rt
->ei
, &exprval
);
902 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
903 exprval_release(&exprval
);
910 V_VT(&retv
) = VT_EMPTY
;
914 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
918 hres
= exprval_to_boolean(ctx
, &exprval
, &rt
->ei
, &b
);
919 exprval_release(&exprval
);
920 if(FAILED(hres
) || !b
)
924 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
931 if(rt
->type
== RT_CONTINUE
)
932 rt
->type
= RT_NORMAL
;
933 else if(rt
->type
!= RT_NORMAL
)
937 hres
= expr_eval(ctx
, stat
->end_expr
, 0, &rt
->ei
, &exprval
);
941 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
942 exprval_release(&exprval
);
955 if(rt
->type
== RT_BREAK
)
956 rt
->type
= RT_NORMAL
;
962 static HRESULT
array_expression_eval(script_ctx_t
*,expression_t
*,jsexcept_t
*,exprval_t
*);
963 static HRESULT
member_expression_eval(script_ctx_t
*,expression_t
*,jsexcept_t
*,exprval_t
*);
964 static HRESULT
identifier_expression_eval(script_ctx_t
*,expression_t
*,jsexcept_t
*,exprval_t
*);
966 /* ECMA-262 3rd Edition 12.6.4 */
967 HRESULT
forin_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
969 forin_statement_t
*stat
= (forin_statement_t
*)_stat
;
970 VARIANT val
, name
, retv
, tmp
;
971 DISPID id
= DISPID_STARTENUM
;
972 BSTR str
, identifier
= NULL
;
980 hres
= variable_list_eval(ctx
, stat
->variable
, &rt
->ei
);
985 hres
= expr_eval(ctx
, stat
->in_expr
, 0, &rt
->ei
, &exprval
);
989 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
990 exprval_release(&exprval
);
994 if(V_VT(&val
) != VT_DISPATCH
) {
995 TRACE("in vt %d\n", V_VT(&val
));
997 V_VT(ret
) = VT_EMPTY
;
1001 hres
= IDispatch_QueryInterface(V_DISPATCH(&val
), &IID_IDispatchEx
, (void**)&in_obj
);
1002 IDispatch_Release(V_DISPATCH(&val
));
1004 TRACE("Object doesn't support IDispatchEx\n");
1005 V_VT(ret
) = VT_EMPTY
;
1009 V_VT(&retv
) = VT_EMPTY
;
1012 identifier
= SysAllocString(stat
->variable
->identifier
);
1015 hres
= IDispatchEx_GetNextDispID(in_obj
, fdexEnumDefault
, id
, &id
);
1016 if(FAILED(hres
) || hres
== S_FALSE
)
1019 hres
= IDispatchEx_GetMemberName(in_obj
, id
, &str
);
1023 TRACE("iter %s\n", debugstr_w(str
));
1025 if(stat
->variable
) {
1026 hres
= identifier_eval(ctx
, identifier
, 0, NULL
, &exprval
);
1028 switch(stat
->expr
->type
) {
1030 hres
= array_expression_eval(ctx
, stat
->expr
, &rt
->ei
, &exprval
);
1033 hres
= identifier_expression_eval(ctx
, stat
->expr
, &rt
->ei
, &exprval
);
1036 hres
= member_expression_eval(ctx
, stat
->expr
, &rt
->ei
, &exprval
);
1039 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1042 if(SUCCEEDED(hres
)) {
1043 V_VT(&name
) = VT_BSTR
;
1044 V_BSTR(&name
) = str
;
1045 hres
= put_value(ctx
, &exprval
, &name
, &rt
->ei
);
1046 exprval_release(&exprval
);
1052 hres
= stat_eval(ctx
, stat
->statement
, rt
, &tmp
);
1056 VariantClear(&retv
);
1059 if(rt
->type
== RT_CONTINUE
)
1060 rt
->type
= RT_NORMAL
;
1061 else if(rt
->type
!= RT_NORMAL
)
1065 SysFreeString(identifier
);
1066 IDispatchEx_Release(in_obj
);
1068 VariantClear(&retv
);
1072 if(rt
->type
== RT_BREAK
)
1073 rt
->type
= RT_NORMAL
;
1079 /* ECMA-262 3rd Edition 12.7 */
1080 HRESULT
continue_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1082 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
1086 if(stat
->identifier
) {
1087 FIXME("indentifier not implemented\n");
1091 rt
->type
= RT_CONTINUE
;
1092 V_VT(ret
) = VT_EMPTY
;
1096 /* ECMA-262 3rd Edition 12.8 */
1097 HRESULT
break_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1099 branch_statement_t
*stat
= (branch_statement_t
*)_stat
;
1103 if(stat
->identifier
) {
1104 FIXME("indentifier not implemented\n");
1108 rt
->type
= RT_BREAK
;
1109 V_VT(ret
) = VT_EMPTY
;
1113 /* ECMA-262 3rd Edition 12.9 */
1114 HRESULT
return_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1116 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
1124 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1128 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, ret
);
1129 exprval_release(&exprval
);
1133 V_VT(ret
) = VT_EMPTY
;
1136 TRACE("= %s\n", debugstr_variant(ret
));
1137 rt
->type
= RT_RETURN
;
1141 /* ECMA-262 3rd Edition 12.10 */
1142 HRESULT
with_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1144 with_statement_t
*stat
= (with_statement_t
*)_stat
;
1153 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1157 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
1158 exprval_release(&exprval
);
1162 hres
= to_object(ctx
, &val
, &disp
);
1167 obj
= iface_to_jsdisp((IUnknown
*)disp
);
1168 IDispatch_Release(disp
);
1170 FIXME("disp id not jsdisp\n");
1174 hres
= scope_push(ctx
->exec_ctx
->scope_chain
, obj
, &ctx
->exec_ctx
->scope_chain
);
1175 jsdisp_release(obj
);
1179 hres
= stat_eval(ctx
, stat
->statement
, rt
, ret
);
1181 scope_pop(&ctx
->exec_ctx
->scope_chain
);
1185 /* ECMA-262 3rd Edition 12.12 */
1186 HRESULT
labelled_statement_eval(script_ctx_t
*ctx
, statement_t
*stat
, return_type_t
*rt
, VARIANT
*ret
)
1192 /* ECMA-262 3rd Edition 12.13 */
1193 HRESULT
switch_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1195 switch_statement_t
*stat
= (switch_statement_t
*)_stat
;
1196 case_clausule_t
*iter
, *default_clausule
= NULL
;
1197 statement_t
*stat_iter
;
1205 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1209 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
1210 exprval_release(&exprval
);
1214 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1216 default_clausule
= iter
;
1220 hres
= expr_eval(ctx
, iter
->expr
, 0, &rt
->ei
, &exprval
);
1224 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &cval
);
1225 exprval_release(&exprval
);
1229 hres
= equal2_values(&val
, &cval
, &b
);
1230 VariantClear(&cval
);
1231 if(FAILED(hres
) || b
)
1240 iter
= default_clausule
;
1242 V_VT(&val
) = VT_EMPTY
;
1246 for(stat_iter
= iter
->stat
; stat_iter
; stat_iter
= stat_iter
->next
) {
1247 hres
= stat_eval(ctx
, stat_iter
, rt
, &tmp
);
1254 if(rt
->type
!= RT_NORMAL
)
1264 if(rt
->type
== RT_BREAK
)
1265 rt
->type
= RT_NORMAL
;
1271 /* ECMA-262 3rd Edition 12.13 */
1272 HRESULT
throw_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1274 expression_statement_t
*stat
= (expression_statement_t
*)_stat
;
1281 hres
= expr_eval(ctx
, stat
->expr
, 0, &rt
->ei
, &exprval
);
1285 hres
= exprval_to_value(ctx
, &exprval
, &rt
->ei
, &val
);
1286 exprval_release(&exprval
);
1291 return DISP_E_EXCEPTION
;
1294 static HRESULT
interp_throw(exec_ctx_t
*ctx
)
1296 const HRESULT arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1298 TRACE("%08x\n", arg
);
1300 return throw_reference_error(ctx
->parser
->script
, &ctx
->ei
, arg
, NULL
);
1303 static HRESULT
interp_throw_type(exec_ctx_t
*ctx
)
1305 const HRESULT hres
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1306 const WCHAR
*str
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.str
;
1308 TRACE("%08x %s\n", hres
, debugstr_w(str
));
1310 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, hres
, str
);
1313 /* ECMA-262 3rd Edition 12.14 */
1314 static HRESULT
catch_eval(script_ctx_t
*ctx
, catch_block_t
*block
, return_type_t
*rt
, VARIANT
*ret
)
1321 memset(&rt
->ei
, 0, sizeof(jsexcept_t
));
1323 hres
= create_dispex(ctx
, NULL
, NULL
, &var_disp
);
1324 if(SUCCEEDED(hres
)) {
1325 hres
= jsdisp_propput_name(var_disp
, block
->identifier
, &ex
, &rt
->ei
, NULL
/*FIXME*/);
1326 if(SUCCEEDED(hres
)) {
1327 hres
= scope_push(ctx
->exec_ctx
->scope_chain
, var_disp
, &ctx
->exec_ctx
->scope_chain
);
1328 if(SUCCEEDED(hres
)) {
1329 hres
= stat_eval(ctx
, block
->statement
, rt
, &val
);
1330 scope_pop(&ctx
->exec_ctx
->scope_chain
);
1334 jsdisp_release(var_disp
);
1345 /* ECMA-262 3rd Edition 12.14 */
1346 HRESULT
try_statement_eval(script_ctx_t
*ctx
, statement_t
*_stat
, return_type_t
*rt
, VARIANT
*ret
)
1348 try_statement_t
*stat
= (try_statement_t
*)_stat
;
1354 hres
= stat_eval(ctx
, stat
->try_statement
, rt
, &val
);
1356 TRACE("EXCEPTION\n");
1357 if(!stat
->catch_block
)
1360 hres
= catch_eval(ctx
, stat
->catch_block
, rt
, &val
);
1365 if(stat
->finally_statement
) {
1367 hres
= stat_eval(ctx
, stat
->finally_statement
, rt
, &val
);
1376 /* ECMA-262 3rd Edition 13 */
1377 static HRESULT
interp_func(exec_ctx_t
*ctx
)
1379 function_expression_t
*expr
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.func
;
1386 hres
= create_source_function(ctx
->parser
, expr
->parameter_list
, expr
->source_elements
, ctx
->scope_chain
,
1387 expr
->src_str
, expr
->src_len
, &dispex
);
1391 var_set_jsdisp(&v
, dispex
);
1392 return stack_push(ctx
, &v
);
1395 /* ECMA-262 3rd Edition 11.2.1 */
1396 static HRESULT
array_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, jsexcept_t
*ei
, exprval_t
*ret
)
1398 binary_expression_t
*expr
= (binary_expression_t
*)_expr
;
1400 VARIANT member
, val
;
1403 IDispatch
*obj
= NULL
;
1408 hres
= expr_eval(ctx
, expr
->expression1
, 0, ei
, &exprval
);
1412 hres
= exprval_to_value(ctx
, &exprval
, ei
, &member
);
1413 exprval_release(&exprval
);
1417 hres
= expr_eval(ctx
, expr
->expression2
, 0, ei
, &exprval
);
1418 if(SUCCEEDED(hres
)) {
1419 hres
= exprval_to_value(ctx
, &exprval
, ei
, &val
);
1420 exprval_release(&exprval
);
1423 if(SUCCEEDED(hres
)) {
1424 hres
= to_object(ctx
, &member
, &obj
);
1428 VariantClear(&member
);
1429 if(SUCCEEDED(hres
)) {
1430 hres
= to_string(ctx
, &val
, ei
, &str
);
1432 if(SUCCEEDED(hres
)) {
1433 hres
= disp_get_id(ctx
, obj
, str
, fdexNameEnsure
, &id
);
1438 exprval_set_idref(ret
, obj
, id
);
1440 IDispatch_Release(obj
);
1446 /* ECMA-262 3rd Edition 11.2.1 */
1447 static HRESULT
interp_array(exec_ctx_t
*ctx
)
1457 namev
= stack_pop(ctx
);
1459 hres
= stack_pop_object(ctx
, &obj
);
1461 VariantClear(namev
);
1465 hres
= to_string(ctx
->parser
->script
, namev
, &ctx
->ei
, &name
);
1466 VariantClear(namev
);
1468 IDispatch_Release(obj
);
1472 hres
= disp_get_id(ctx
->parser
->script
, obj
, name
, 0, &id
);
1473 SysFreeString(name
);
1474 if(SUCCEEDED(hres
)) {
1475 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
1476 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1477 V_VT(&v
) = VT_EMPTY
;
1480 IDispatch_Release(obj
);
1484 return stack_push(ctx
, &v
);
1487 /* ECMA-262 3rd Edition 11.2.1 */
1488 static HRESULT
member_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, jsexcept_t
*ei
, exprval_t
*ret
)
1490 member_expression_t
*expr
= (member_expression_t
*)_expr
;
1491 IDispatch
*obj
= NULL
;
1500 hres
= expr_eval(ctx
, expr
->expression
, 0, ei
, &exprval
);
1504 hres
= exprval_to_value(ctx
, &exprval
, ei
, &member
);
1505 exprval_release(&exprval
);
1509 hres
= to_object(ctx
, &member
, &obj
);
1510 VariantClear(&member
);
1514 str
= SysAllocString(expr
->identifier
);
1516 IDispatch_Release(obj
);
1517 return E_OUTOFMEMORY
;
1520 hres
= disp_get_id(ctx
, obj
, str
, fdexNameEnsure
, &id
);
1523 exprval_set_idref(ret
, obj
, id
);
1525 IDispatch_Release(obj
);
1529 /* ECMA-262 3rd Edition 11.2.1 */
1530 static HRESULT
interp_member(exec_ctx_t
*ctx
)
1532 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
1540 hres
= stack_pop_object(ctx
, &obj
);
1544 hres
= disp_get_id(ctx
->parser
->script
, obj
, arg
, 0, &id
);
1545 if(SUCCEEDED(hres
)) {
1546 V_VT(&v
) = VT_EMPTY
;
1547 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
1548 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1549 V_VT(&v
) = VT_EMPTY
;
1552 IDispatch_Release(obj
);
1556 return stack_push(ctx
, &v
);
1559 /* ECMA-262 3rd Edition 11.2.1 */
1560 static HRESULT
interp_memberid(exec_ctx_t
*ctx
)
1562 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1563 VARIANT
*objv
, *namev
;
1571 namev
= stack_pop(ctx
);
1572 objv
= stack_pop(ctx
);
1574 hres
= to_object(ctx
->parser
->script
, objv
, &obj
);
1576 if(SUCCEEDED(hres
)) {
1577 hres
= to_string(ctx
->parser
->script
, namev
, &ctx
->ei
, &name
);
1579 IDispatch_Release(obj
);
1581 VariantClear(namev
);
1585 hres
= disp_get_id(ctx
->parser
->script
, obj
, name
, arg
, &id
);
1586 SysFreeString(name
);
1588 IDispatch_Release(obj
);
1589 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
1591 id
= JS_E_INVALID_PROPERTY
;
1597 return stack_push_objid(ctx
, obj
, id
);
1600 /* ECMA-262 3rd Edition 11.2.1 */
1601 static HRESULT
interp_refval(exec_ctx_t
*ctx
)
1610 disp
= stack_topn_objid(ctx
, 0, &id
);
1612 return throw_reference_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_ILLEGAL_ASSIGN
, NULL
);
1614 hres
= disp_propget(ctx
->parser
->script
, disp
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
1618 return stack_push(ctx
, &v
);
1621 static void jsstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, DISPPARAMS
*dp
)
1626 dp
->cArgs
= arg_cnt
;
1627 dp
->rgdispidNamedArgs
= NULL
;
1630 assert(ctx
->top
>= arg_cnt
);
1632 for(i
=1; i
*2 <= arg_cnt
; i
++) {
1633 tmp
= ctx
->stack
[ctx
->top
-i
];
1634 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
1635 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
1638 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-arg_cnt
;
1641 /* ECMA-262 3rd Edition 11.2.2 */
1642 static HRESULT
interp_new(exec_ctx_t
*ctx
)
1644 const LONG arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1651 constr
= stack_topn(ctx
, arg
);
1653 /* NOTE: Should use to_object here */
1655 if(V_VT(constr
) == VT_NULL
)
1656 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
1657 else if(V_VT(constr
) != VT_DISPATCH
)
1658 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_INVALID_ACTION
, NULL
);
1659 else if(!V_DISPATCH(constr
))
1660 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_INVALID_PROPERTY
, NULL
);
1662 jsstack_to_dp(ctx
, arg
, &dp
);
1663 hres
= disp_call(ctx
->parser
->script
, V_DISPATCH(constr
), DISPID_VALUE
,
1664 DISPATCH_CONSTRUCT
, &dp
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
1668 stack_popn(ctx
, arg
+1);
1669 return stack_push(ctx
, &v
);
1672 /* ECMA-262 3rd Edition 11.2.3 */
1673 static HRESULT
interp_call(exec_ctx_t
*ctx
)
1675 const unsigned argn
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1676 const int do_ret
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.lng
;
1681 TRACE("%d %d\n", argn
, do_ret
);
1683 objv
= stack_topn(ctx
, argn
);
1684 if(V_VT(objv
) != VT_DISPATCH
)
1685 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_INVALID_PROPERTY
, NULL
);
1687 jsstack_to_dp(ctx
, argn
, &dp
);
1688 hres
= disp_call(ctx
->parser
->script
, V_DISPATCH(objv
), DISPID_VALUE
, DISPATCH_METHOD
, &dp
,
1689 do_ret
? &v
: NULL
, &ctx
->ei
, NULL
/*FIXME*/);
1693 stack_popn(ctx
, argn
+1);
1694 return do_ret
? stack_push(ctx
, &v
) : S_OK
;
1698 /* ECMA-262 3rd Edition 11.2.3 */
1699 static HRESULT
interp_call_member(exec_ctx_t
*ctx
)
1701 const unsigned argn
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1702 const int do_ret
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.lng
;
1709 TRACE("%d %d\n", argn
, do_ret
);
1711 obj
= stack_topn_objid(ctx
, argn
, &id
);
1713 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, id
, NULL
);
1715 jsstack_to_dp(ctx
, argn
, &dp
);
1716 hres
= disp_call(ctx
->parser
->script
, obj
, id
, DISPATCH_METHOD
, &dp
, do_ret
? &v
: NULL
, &ctx
->ei
, NULL
/*FIXME*/);
1720 stack_popn(ctx
, argn
+2);
1721 return do_ret
? stack_push(ctx
, &v
) : S_OK
;
1725 /* ECMA-262 3rd Edition 11.1.1 */
1726 static HRESULT
interp_this(exec_ctx_t
*ctx
)
1732 V_VT(&v
) = VT_DISPATCH
;
1733 V_DISPATCH(&v
) = ctx
->this_obj
;
1734 IDispatch_AddRef(ctx
->this_obj
);
1735 return stack_push(ctx
, &v
);
1738 /* ECMA-262 3rd Edition 10.1.4 */
1739 static HRESULT
identifier_expression_eval(script_ctx_t
*ctx
, expression_t
*_expr
, jsexcept_t
*ei
, exprval_t
*ret
)
1741 identifier_expression_t
*expr
= (identifier_expression_t
*)_expr
;
1747 identifier
= SysAllocString(expr
->identifier
);
1749 return E_OUTOFMEMORY
;
1751 hres
= identifier_eval(ctx
, identifier
, fdexNameEnsure
, ei
, ret
);
1753 SysFreeString(identifier
);
1757 /* ECMA-262 3rd Edition 10.1.4 */
1758 static HRESULT
interp_ident(exec_ctx_t
*ctx
)
1760 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
1765 TRACE("%s\n", debugstr_w(arg
));
1767 hres
= identifier_eval(ctx
->parser
->script
, arg
, 0, &ctx
->ei
, &exprval
);
1771 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &ctx
->ei
, &v
);
1772 exprval_release(&exprval
);
1776 return stack_push(ctx
, &v
);
1779 /* ECMA-262 3rd Edition 10.1.4 */
1780 static HRESULT
interp_identid(exec_ctx_t
*ctx
)
1782 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
1783 const unsigned flags
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.uint
;
1787 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1789 hres
= identifier_eval(ctx
->parser
->script
, arg
, flags
, &ctx
->ei
, &exprval
);
1793 if(exprval
.type
!= EXPRVAL_IDREF
) {
1794 WARN("invalid ref\n");
1795 exprval_release(&exprval
);
1796 return stack_push_objid(ctx
, NULL
, JS_E_OBJECT_EXPECTED
);
1799 return stack_push_objid(ctx
, exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
);
1802 /* ECMA-262 3rd Edition 7.8.1 */
1803 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1810 return stack_push(ctx
, &v
);
1813 /* ECMA-262 3rd Edition 7.8.2 */
1814 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1816 const LONG arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1818 TRACE("%s\n", arg
? "true" : "false");
1820 return stack_push_bool(ctx
, arg
);
1823 /* ECMA-262 3rd Edition 7.8.3 */
1824 static HRESULT
interp_int(exec_ctx_t
*ctx
)
1826 const LONG arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
1833 return stack_push(ctx
, &v
);
1836 /* ECMA-262 3rd Edition 7.8.3 */
1837 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1839 const double arg
= *ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.dbl
;
1842 TRACE("%lf\n", arg
);
1846 return stack_push(ctx
, &v
);
1849 /* ECMA-262 3rd Edition 7.8.4 */
1850 static HRESULT
interp_str(exec_ctx_t
*ctx
)
1852 const WCHAR
*str
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.str
;
1855 TRACE("%s\n", debugstr_w(str
));
1858 V_BSTR(&v
) = SysAllocString(str
);
1860 return E_OUTOFMEMORY
;
1862 return stack_push(ctx
, &v
);
1865 /* ECMA-262 3rd Edition 7.8 */
1866 static HRESULT
interp_regexp(exec_ctx_t
*ctx
)
1868 const WCHAR
*source
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.str
;
1869 const LONG flags
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg2
.lng
;
1874 TRACE("%s %x\n", debugstr_w(source
), flags
);
1876 hres
= create_regexp(ctx
->parser
->script
, source
, strlenW(source
), flags
, ®exp
);
1880 var_set_jsdisp(&v
, regexp
);
1881 return stack_push(ctx
, &v
);
1884 /* ECMA-262 3rd Edition 11.1.4 */
1885 static HRESULT
interp_carray(exec_ctx_t
*ctx
)
1887 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1895 hres
= create_array(ctx
->parser
->script
, arg
, &array
);
1902 hres
= jsdisp_propput_idx(array
, i
, v
, &ctx
->ei
, NULL
/*FIXME*/);
1905 jsdisp_release(array
);
1910 var_set_jsdisp(&r
, array
);
1911 return stack_push(ctx
, &r
);
1914 /* ECMA-262 3rd Edition 11.1.5 */
1915 HRESULT
interp_new_obj(exec_ctx_t
*ctx
)
1923 hres
= create_object(ctx
->parser
->script
, NULL
, &obj
);
1927 var_set_jsdisp(&v
, obj
);
1928 return stack_push(ctx
, &v
);
1931 /* ECMA-262 3rd Edition 11.1.5 */
1932 HRESULT
interp_obj_prop(exec_ctx_t
*ctx
)
1934 const BSTR name
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
1939 TRACE("%s\n", debugstr_w(name
));
1943 assert(V_VT(stack_top(ctx
)) == VT_DISPATCH
);
1944 obj
= as_jsdisp(V_DISPATCH(stack_top(ctx
)));
1946 hres
= jsdisp_propput_name(obj
, name
, v
, &ctx
->ei
, NULL
/*FIXME*/);
1951 /* ECMA-262 3rd Edition 11.11 */
1952 static HRESULT
interp_jmp_nz(exec_ctx_t
*ctx
)
1954 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1960 hres
= to_boolean(stack_top(ctx
), &b
);
1973 /* ECMA-262 3rd Edition 11.11 */
1974 static HRESULT
interp_jmp_z(exec_ctx_t
*ctx
)
1976 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
1982 hres
= to_boolean(stack_top(ctx
), &b
);
1995 /* ECMA-262 3rd Edition 11.10 */
1996 static HRESULT
interp_or(exec_ctx_t
*ctx
)
2003 hres
= stack_pop_int(ctx
, &r
);
2007 hres
= stack_pop_int(ctx
, &l
);
2011 return stack_push_int(ctx
, l
|r
);
2014 /* ECMA-262 3rd Edition 11.10 */
2015 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
2022 hres
= stack_pop_int(ctx
, &r
);
2026 hres
= stack_pop_int(ctx
, &l
);
2030 return stack_push_int(ctx
, l
^r
);
2033 /* ECMA-262 3rd Edition 11.10 */
2034 static HRESULT
interp_and(exec_ctx_t
*ctx
)
2041 hres
= stack_pop_int(ctx
, &r
);
2045 hres
= stack_pop_int(ctx
, &l
);
2049 return stack_push_int(ctx
, l
&r
);
2052 /* ECMA-262 3rd Edition 11.8.6 */
2053 static HRESULT
interp_instanceof(exec_ctx_t
*ctx
)
2055 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
2060 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2063 if(V_VT(v
) != VT_DISPATCH
|| !V_DISPATCH(v
)) {
2065 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
2068 obj
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(v
));
2069 IDispatch_Release(V_DISPATCH(v
));
2071 FIXME("non-jsdisp objects not supported\n");
2075 if(is_class(obj
, JSCLASS_FUNCTION
)) {
2076 hres
= jsdisp_propget_name(obj
, prototypeW
, &prot
, &ctx
->ei
, NULL
/*FIXME*/);
2078 hres
= throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
2080 jsdisp_release(obj
);
2086 if(V_VT(&prot
) == VT_DISPATCH
) {
2087 if(V_VT(v
) == VT_DISPATCH
)
2088 tmp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(v
));
2089 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
2090 hres
= disp_cmp(V_DISPATCH(&prot
), to_disp(iter
), &ret
);
2096 jsdisp_release(tmp
);
2098 FIXME("prototype is not an object\n");
2102 VariantClear(&prot
);
2107 return stack_push_bool(ctx
, ret
);
2110 /* ECMA-262 3rd Edition 11.8.7 */
2111 static HRESULT
interp_in(exec_ctx_t
*ctx
)
2121 obj
= stack_pop(ctx
);
2124 if(V_VT(obj
) != VT_DISPATCH
|| !V_DISPATCH(obj
)) {
2127 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
2130 hres
= to_string(ctx
->parser
->script
, v
, &ctx
->ei
, &str
);
2133 IDispatch_Release(V_DISPATCH(obj
));
2137 hres
= disp_get_id(ctx
->parser
->script
, V_DISPATCH(obj
), str
, 0, &id
);
2138 IDispatch_Release(V_DISPATCH(obj
));
2142 else if(hres
== DISP_E_UNKNOWNNAME
)
2147 return stack_push_bool(ctx
, ret
);
2150 /* ECMA-262 3rd Edition 11.6.1 */
2151 static HRESULT
add_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, VARIANT
*retv
)
2156 hres
= to_primitive(ctx
, lval
, ei
, &l
, NO_HINT
);
2160 hres
= to_primitive(ctx
, rval
, ei
, &r
, NO_HINT
);
2166 if(V_VT(&l
) == VT_BSTR
|| V_VT(&r
) == VT_BSTR
) {
2167 BSTR lstr
= NULL
, rstr
= NULL
;
2169 if(V_VT(&l
) == VT_BSTR
)
2172 hres
= to_string(ctx
, &l
, ei
, &lstr
);
2174 if(SUCCEEDED(hres
)) {
2175 if(V_VT(&r
) == VT_BSTR
)
2178 hres
= to_string(ctx
, &r
, ei
, &rstr
);
2181 if(SUCCEEDED(hres
)) {
2184 len1
= SysStringLen(lstr
);
2185 len2
= SysStringLen(rstr
);
2187 V_VT(retv
) = VT_BSTR
;
2188 V_BSTR(retv
) = SysAllocStringLen(NULL
, len1
+len2
);
2189 memcpy(V_BSTR(retv
), lstr
, len1
*sizeof(WCHAR
));
2190 memcpy(V_BSTR(retv
)+len1
, rstr
, (len2
+1)*sizeof(WCHAR
));
2193 if(V_VT(&l
) != VT_BSTR
)
2194 SysFreeString(lstr
);
2195 if(V_VT(&r
) != VT_BSTR
)
2196 SysFreeString(rstr
);
2200 hres
= to_number(ctx
, &l
, ei
, &nl
);
2201 if(SUCCEEDED(hres
)) {
2202 hres
= to_number(ctx
, &r
, ei
, &nr
);
2204 num_set_val(retv
, num_val(&nl
) + num_val(&nr
));
2213 /* ECMA-262 3rd Edition 11.6.1 */
2214 static HRESULT
interp_add(exec_ctx_t
*ctx
)
2216 VARIANT
*l
, *r
, ret
;
2222 TRACE("%s + %s\n", debugstr_variant(l
), debugstr_variant(r
));
2224 hres
= add_eval(ctx
->parser
->script
, l
, r
, &ctx
->ei
, &ret
);
2230 return stack_push(ctx
, &ret
);
2233 /* ECMA-262 3rd Edition 11.6.2 */
2234 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
2241 hres
= stack_pop_number(ctx
, &r
);
2245 hres
= stack_pop_number(ctx
, &l
);
2249 return stack_push_number(ctx
, num_val(&l
)-num_val(&r
));
2252 /* ECMA-262 3rd Edition 11.5.1 */
2253 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
2260 hres
= stack_pop_number(ctx
, &r
);
2264 hres
= stack_pop_number(ctx
, &l
);
2268 return stack_push_number(ctx
, num_val(&l
)*num_val(&r
));
2271 /* ECMA-262 3rd Edition 11.5.2 */
2272 static HRESULT
interp_div(exec_ctx_t
*ctx
)
2279 hres
= stack_pop_number(ctx
, &r
);
2283 hres
= stack_pop_number(ctx
, &l
);
2287 return stack_push_number(ctx
, num_val(&l
)/num_val(&r
));
2290 /* ECMA-262 3rd Edition 11.5.3 */
2291 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
2298 hres
= stack_pop_number(ctx
, &r
);
2302 hres
= stack_pop_number(ctx
, &l
);
2306 return stack_push_number(ctx
, fmod(num_val(&l
), num_val(&r
)));
2309 /* ECMA-262 3rd Edition 11.4.2 */
2310 static HRESULT
interp_delete(exec_ctx_t
*ctx
)
2312 VARIANT
*obj_var
, *name_var
;
2313 IDispatchEx
*dispex
;
2321 name_var
= stack_pop(ctx
);
2322 obj_var
= stack_pop(ctx
);
2324 hres
= to_object(ctx
->parser
->script
, obj_var
, &obj
);
2325 VariantClear(obj_var
);
2327 VariantClear(name_var
);
2331 hres
= to_string(ctx
->parser
->script
, name_var
, &ctx
->ei
, &name
);
2332 VariantClear(name_var
);
2334 IDispatch_Release(obj
);
2338 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
2339 if(SUCCEEDED(hres
)) {
2340 hres
= IDispatchEx_DeleteMemberByName(dispex
, name
, make_grfdex(ctx
->parser
->script
, fdexNameCaseSensitive
));
2342 IDispatchEx_Release(dispex
);
2348 IDispatch_Release(obj
);
2349 SysFreeString(name
);
2353 return stack_push_bool(ctx
, ret
);
2356 /* ECMA-262 3rd Edition 11.4.2 */
2357 static HRESULT
interp_delete_ident(exec_ctx_t
*ctx
)
2359 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
2360 IDispatchEx
*dispex
;
2365 TRACE("%s\n", debugstr_w(arg
));
2367 hres
= identifier_eval(ctx
->parser
->script
, arg
, 0, &ctx
->ei
, &exprval
);
2371 if(exprval
.type
!= EXPRVAL_IDREF
) {
2372 FIXME("Unsupported exprval\n");
2373 exprval_release(&exprval
);
2377 hres
= IDispatch_QueryInterface(exprval
.u
.idref
.disp
, &IID_IDispatchEx
, (void**)&dispex
);
2378 IDispatch_Release(exprval
.u
.idref
.disp
);
2379 if(SUCCEEDED(hres
)) {
2380 hres
= IDispatchEx_DeleteMemberByDispID(dispex
, exprval
.u
.idref
.id
);
2381 IDispatchEx_Release(dispex
);
2388 return stack_push_bool(ctx
, ret
);
2391 /* ECMA-262 3rd Edition 11.4.2 */
2392 static HRESULT
interp_void(exec_ctx_t
*ctx
)
2400 V_VT(&v
) = VT_EMPTY
;
2401 return stack_push(ctx
, &v
);
2404 /* ECMA-262 3rd Edition 11.4.3 */
2405 static HRESULT
typeof_string(VARIANT
*v
, const WCHAR
**ret
)
2427 if(V_DISPATCH(v
) && (dispex
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(v
)))) {
2428 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
2429 jsdisp_release(dispex
);
2436 FIXME("unhandled vt %d\n", V_VT(v
));
2443 /* ECMA-262 3rd Edition 11.4.3 */
2444 static HRESULT
interp_typeofid(exec_ctx_t
*ctx
)
2452 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
2456 obj
= stack_pop_objid(ctx
, &id
);
2458 return stack_push_string(ctx
, undefinedW
);
2460 V_VT(&v
) = VT_EMPTY
;
2461 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
2462 IDispatch_Release(obj
);
2464 return stack_push_string(ctx
, unknownW
);
2466 hres
= typeof_string(&v
, &ret
);
2471 return stack_push_string(ctx
, ret
);
2474 /* ECMA-262 3rd Edition 11.4.3 */
2475 static HRESULT
interp_typeofident(exec_ctx_t
*ctx
)
2477 const BSTR arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.bstr
;
2483 TRACE("%s\n", debugstr_w(arg
));
2485 hres
= identifier_eval(ctx
->parser
->script
, arg
, 0, &ctx
->ei
, &exprval
);
2489 if(exprval
.type
== EXPRVAL_INVALID
) {
2490 hres
= stack_push_string(ctx
, undefinedW
);
2491 exprval_release(&exprval
);
2495 hres
= exprval_to_value(ctx
->parser
->script
, &exprval
, &ctx
->ei
, &v
);
2496 exprval_release(&exprval
);
2500 hres
= typeof_string(&v
, &ret
);
2505 return stack_push_string(ctx
, ret
);
2508 /* ECMA-262 3rd Edition 11.4.3 */
2509 static HRESULT
interp_typeof(exec_ctx_t
*ctx
)
2518 hres
= typeof_string(v
, &ret
);
2523 return stack_push_string(ctx
, ret
);
2526 /* ECMA-262 3rd Edition 11.4.7 */
2527 static HRESULT
interp_minus(exec_ctx_t
*ctx
)
2534 hres
= stack_pop_number(ctx
, &n
);
2538 return stack_push_number(ctx
, -num_val(&n
));
2541 /* ECMA-262 3rd Edition 11.4.6 */
2542 static HRESULT
interp_tonum(exec_ctx_t
*ctx
)
2550 hres
= to_number(ctx
->parser
->script
, v
, &ctx
->ei
, &num
);
2555 return stack_push(ctx
, &num
);
2558 /* ECMA-262 3rd Edition 11.3.1 */
2559 static HRESULT
interp_postinc(exec_ctx_t
*ctx
)
2561 const int arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
2569 obj
= stack_pop_objid(ctx
, &id
);
2571 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
2573 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
2574 if(SUCCEEDED(hres
)) {
2577 hres
= to_number(ctx
->parser
->script
, &v
, &ctx
->ei
, &n
);
2578 if(SUCCEEDED(hres
)) {
2579 num_set_val(&inc
, num_val(&n
)+(double)arg
);
2580 hres
= disp_propput(ctx
->parser
->script
, obj
, id
, &inc
, &ctx
->ei
, NULL
/*FIXME*/);
2585 IDispatch_Release(obj
);
2589 return stack_push(ctx
, &v
);
2592 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2593 static HRESULT
interp_preinc(exec_ctx_t
*ctx
)
2595 const int arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.lng
;
2603 obj
= stack_pop_objid(ctx
, &id
);
2605 return throw_type_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_OBJECT_EXPECTED
, NULL
);
2607 hres
= disp_propget(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
2608 if(SUCCEEDED(hres
)) {
2611 hres
= to_number(ctx
->parser
->script
, &v
, &ctx
->ei
, &n
);
2613 if(SUCCEEDED(hres
)) {
2614 num_set_val(&v
, num_val(&n
)+(double)arg
);
2615 hres
= disp_propput(ctx
->parser
->script
, obj
, id
, &v
, &ctx
->ei
, NULL
/*FIXME*/);
2618 IDispatch_Release(obj
);
2622 return stack_push(ctx
, &v
);
2625 /* ECMA-262 3rd Edition 11.9.3 */
2626 static HRESULT
equal_values(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, jsexcept_t
*ei
, BOOL
*ret
)
2628 if(V_VT(lval
) == V_VT(rval
) || (is_num_vt(V_VT(lval
)) && is_num_vt(V_VT(rval
))))
2629 return equal2_values(lval
, rval
, ret
);
2631 /* FIXME: NULL disps should be handled in more general way */
2632 if(V_VT(lval
) == VT_DISPATCH
&& !V_DISPATCH(lval
)) {
2635 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2638 if(V_VT(rval
) == VT_DISPATCH
&& !V_DISPATCH(rval
)) {
2641 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2644 if((V_VT(lval
) == VT_NULL
&& V_VT(rval
) == VT_EMPTY
) ||
2645 (V_VT(lval
) == VT_EMPTY
&& V_VT(rval
) == VT_NULL
)) {
2650 if(V_VT(lval
) == VT_BSTR
&& is_num_vt(V_VT(rval
))) {
2654 hres
= to_number(ctx
, lval
, ei
, &v
);
2658 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2661 if(V_VT(rval
) == VT_BSTR
&& is_num_vt(V_VT(lval
))) {
2665 hres
= to_number(ctx
, rval
, ei
, &v
);
2669 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2672 if(V_VT(rval
) == VT_BOOL
) {
2676 V_I4(&v
) = V_BOOL(rval
) ? 1 : 0;
2677 return equal_values(ctx
, lval
, &v
, ei
, ret
);
2680 if(V_VT(lval
) == VT_BOOL
) {
2684 V_I4(&v
) = V_BOOL(lval
) ? 1 : 0;
2685 return equal_values(ctx
, &v
, rval
, ei
, ret
);
2689 if(V_VT(rval
) == VT_DISPATCH
&& (V_VT(lval
) == VT_BSTR
|| is_num_vt(V_VT(lval
)))) {
2693 hres
= to_primitive(ctx
, rval
, ei
, &v
, NO_HINT
);
2697 hres
= equal_values(ctx
, lval
, &v
, ei
, ret
);
2704 if(V_VT(lval
) == VT_DISPATCH
&& (V_VT(rval
) == VT_BSTR
|| is_num_vt(V_VT(rval
)))) {
2708 hres
= to_primitive(ctx
, lval
, ei
, &v
, NO_HINT
);
2712 hres
= equal_values(ctx
, &v
, rval
, ei
, ret
);
2723 /* ECMA-262 3rd Edition 11.9.1 */
2724 static HRESULT
interp_eq(exec_ctx_t
*ctx
)
2733 TRACE("%s == %s\n", debugstr_variant(l
), debugstr_variant(r
));
2735 hres
= equal_values(ctx
->parser
->script
, l
, r
, &ctx
->ei
, &b
);
2741 return stack_push_bool(ctx
, b
);
2744 /* ECMA-262 3rd Edition 11.9.2 */
2745 static HRESULT
interp_neq(exec_ctx_t
*ctx
)
2754 TRACE("%s != %s\n", debugstr_variant(l
), debugstr_variant(r
));
2756 hres
= equal_values(ctx
->parser
->script
, l
, r
, &ctx
->ei
, &b
);
2762 return stack_push_bool(ctx
, !b
);
2765 /* ECMA-262 3rd Edition 11.9.4 */
2766 static HRESULT
interp_eq2(exec_ctx_t
*ctx
)
2777 hres
= equal2_values(r
, l
, &b
);
2783 return stack_push_bool(ctx
, b
);
2786 /* ECMA-262 3rd Edition 11.9.5 */
2787 static HRESULT
interp_neq2(exec_ctx_t
*ctx
)
2798 hres
= equal2_values(r
, l
, &b
);
2804 return stack_push_bool(ctx
, !b
);
2807 /* ECMA-262 3rd Edition 11.8.5 */
2808 static HRESULT
less_eval(script_ctx_t
*ctx
, VARIANT
*lval
, VARIANT
*rval
, BOOL greater
, jsexcept_t
*ei
, BOOL
*ret
)
2810 VARIANT l
, r
, ln
, rn
;
2813 hres
= to_primitive(ctx
, lval
, ei
, &l
, NO_HINT
);
2817 hres
= to_primitive(ctx
, rval
, ei
, &r
, NO_HINT
);
2823 if(V_VT(&l
) == VT_BSTR
&& V_VT(&r
) == VT_BSTR
) {
2824 *ret
= (strcmpW(V_BSTR(&l
), V_BSTR(&r
)) < 0) ^ greater
;
2825 SysFreeString(V_BSTR(&l
));
2826 SysFreeString(V_BSTR(&r
));
2830 hres
= to_number(ctx
, &l
, ei
, &ln
);
2833 hres
= to_number(ctx
, &r
, ei
, &rn
);
2838 if(V_VT(&ln
) == VT_I4
&& V_VT(&rn
) == VT_I4
) {
2839 *ret
= (V_I4(&ln
) < V_I4(&rn
)) ^ greater
;
2841 DOUBLE ld
= num_val(&ln
);
2842 DOUBLE rd
= num_val(&rn
);
2844 *ret
= !isnan(ld
) && !isnan(rd
) && ((ld
< rd
) ^ greater
);
2850 /* ECMA-262 3rd Edition 11.8.1 */
2851 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
2860 TRACE("%s < %s\n", debugstr_variant(l
), debugstr_variant(r
));
2862 hres
= less_eval(ctx
->parser
->script
, l
, r
, FALSE
, &ctx
->ei
, &b
);
2868 return stack_push_bool(ctx
, b
);
2871 /* ECMA-262 3rd Edition 11.8.1 */
2872 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
2881 TRACE("%s <= %s\n", debugstr_variant(l
), debugstr_variant(r
));
2883 hres
= less_eval(ctx
->parser
->script
, r
, l
, TRUE
, &ctx
->ei
, &b
);
2889 return stack_push_bool(ctx
, b
);
2892 /* ECMA-262 3rd Edition 11.8.2 */
2893 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
2902 TRACE("%s > %s\n", debugstr_variant(l
), debugstr_variant(r
));
2904 hres
= less_eval(ctx
->parser
->script
, r
, l
, FALSE
, &ctx
->ei
, &b
);
2910 return stack_push_bool(ctx
, b
);
2913 /* ECMA-262 3rd Edition 11.8.4 */
2914 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
2923 TRACE("%s >= %s\n", debugstr_variant(l
), debugstr_variant(r
));
2925 hres
= less_eval(ctx
->parser
->script
, l
, r
, TRUE
, &ctx
->ei
, &b
);
2931 return stack_push_bool(ctx
, b
);
2934 /* ECMA-262 3rd Edition 11.4.8 */
2935 static HRESULT
interp_bneg(exec_ctx_t
*ctx
)
2944 hres
= to_int32(ctx
->parser
->script
, v
, &ctx
->ei
, &i
);
2951 return stack_push(ctx
, &r
);
2954 /* ECMA-262 3rd Edition 11.4.9 */
2955 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
2964 hres
= to_boolean(v
, &b
);
2969 return stack_push_bool(ctx
, !b
);
2972 /* ECMA-262 3rd Edition 11.7.1 */
2973 static HRESULT
interp_lshift(exec_ctx_t
*ctx
)
2979 hres
= stack_pop_uint(ctx
, &r
);
2983 hres
= stack_pop_int(ctx
, &l
);
2987 return stack_push_int(ctx
, l
<< (r
&0x1f));
2990 /* ECMA-262 3rd Edition 11.7.2 */
2991 static HRESULT
interp_rshift(exec_ctx_t
*ctx
)
2997 hres
= stack_pop_uint(ctx
, &r
);
3001 hres
= stack_pop_int(ctx
, &l
);
3005 return stack_push_int(ctx
, l
>> (r
&0x1f));
3008 /* ECMA-262 3rd Edition 11.7.3 */
3009 static HRESULT
interp_rshift2(exec_ctx_t
*ctx
)
3014 hres
= stack_pop_uint(ctx
, &r
);
3018 hres
= stack_pop_uint(ctx
, &l
);
3022 return stack_push_int(ctx
, l
>> (r
&0x1f));
3025 /* ECMA-262 3rd Edition 11.13.1 */
3026 static HRESULT
interp_assign(exec_ctx_t
*ctx
)
3036 disp
= stack_pop_objid(ctx
, &id
);
3039 return throw_reference_error(ctx
->parser
->script
, &ctx
->ei
, JS_E_ILLEGAL_ASSIGN
, NULL
);
3041 hres
= disp_propput(ctx
->parser
->script
, disp
, id
, v
, &ctx
->ei
, NULL
/*FIXME*/);
3042 IDispatch_Release(disp
);
3048 return stack_push(ctx
, v
);
3051 static HRESULT
interp_undefined(exec_ctx_t
*ctx
)
3057 V_VT(&v
) = VT_EMPTY
;
3058 return stack_push(ctx
, &v
);
3061 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
3063 const unsigned arg
= ctx
->parser
->code
->instrs
[ctx
->ip
].arg1
.uint
;
3071 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
3079 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
3087 static HRESULT
interp_tree(exec_ctx_t
*ctx
)
3089 instr_t
*instr
= ctx
->parser
->code
->instrs
+ctx
->ip
;
3096 hres
= expr_eval(ctx
->parser
->script
, instr
->arg1
.expr
, 0, &ctx
->ei
, &val
);
3100 hres
= exprval_to_value(ctx
->parser
->script
, &val
, &ctx
->ei
, &v
);
3101 exprval_release(&val
);
3105 return stack_push(ctx
, &v
);
3108 typedef HRESULT (*op_func_t
)(exec_ctx_t
*);
3110 static const op_func_t op_funcs
[] = {
3111 #define X(x,a,b,c) interp_##x,
3116 static const unsigned op_move
[] = {
3117 #define X(a,x,b,c) x,
3122 static HRESULT
expr_eval(script_ctx_t
*ctx
, expression_t
*expr
, DWORD flags
, jsexcept_t
*ei
, exprval_t
*ret
)
3124 exec_ctx_t
*exec_ctx
= ctx
->exec_ctx
;
3125 unsigned prev_ip
, prev_top
;
3127 HRESULT hres
= S_OK
;
3131 if(expr
->instr_off
== -1) {
3132 hres
= compile_subscript(ctx
->exec_ctx
->parser
, expr
, !(flags
& EXPR_NOVAL
), &expr
->instr_off
);
3137 prev_top
= exec_ctx
->top
;
3138 prev_ip
= exec_ctx
->ip
;
3139 exec_ctx
->ip
= expr
->instr_off
;
3141 while(exec_ctx
->ip
!= -1) {
3142 op
= exec_ctx
->parser
->code
->instrs
[exec_ctx
->ip
].op
;
3143 hres
= op_funcs
[op
](exec_ctx
);
3146 exec_ctx
->ip
+= op_move
[op
];
3149 exec_ctx
->ip
= prev_ip
;
3152 stack_popn(exec_ctx
, exec_ctx
->top
-prev_top
);
3154 memset(&exec_ctx
->ei
, 0, sizeof(exec_ctx
->ei
));
3158 assert(exec_ctx
->top
== prev_top
+1 || ((flags
&EXPR_NOVAL
) && exec_ctx
->top
== prev_top
));
3160 ret
->type
= EXPRVAL_VARIANT
;
3161 if(exec_ctx
->top
== prev_top
)
3162 V_VT(&ret
->u
.var
) = VT_EMPTY
;
3164 ret
->u
.var
= *stack_pop(exec_ctx
);