2 * Copyright 2008,2011 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
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
30 struct _except_frame_t
{
57 static const size_t stack_size
= 0x40000;
59 static HRESULT
stack_push(script_ctx_t
*ctx
, jsval_t v
)
61 if(ctx
->stack_top
== stack_size
)
62 return JS_E_STACK_OVERFLOW
;
64 ctx
->stack
[ctx
->stack_top
++] = v
;
68 static inline HRESULT
stack_push_string(script_ctx_t
*ctx
, const WCHAR
*str
)
76 return stack_push(ctx
, jsval_string(v
));
79 static inline jsval_t
stack_top(script_ctx_t
*ctx
)
81 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
);
82 return ctx
->stack
[ctx
->stack_top
-1];
85 static inline jsval_t
*stack_top_ref(script_ctx_t
*ctx
, unsigned n
)
87 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
+n
);
88 return ctx
->stack
+ctx
->stack_top
-1-n
;
91 static inline jsval_t
stack_topn(script_ctx_t
*ctx
, unsigned n
)
93 return *stack_top_ref(ctx
, n
);
96 static inline jsval_t
*stack_args(script_ctx_t
*ctx
, unsigned n
)
100 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
+n
-1);
101 return ctx
->stack
+ ctx
->stack_top
-n
;
104 static inline jsval_t
stack_pop(script_ctx_t
*ctx
)
106 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
);
107 return ctx
->stack
[--ctx
->stack_top
];
110 static void stack_popn(script_ctx_t
*ctx
, unsigned n
)
113 jsval_release(stack_pop(ctx
));
116 static HRESULT
stack_pop_number(script_ctx_t
*ctx
, double *r
)
122 hres
= to_number(ctx
, v
, r
);
127 static HRESULT
stack_pop_object(script_ctx_t
*ctx
, IDispatch
**r
)
133 if(is_object_instance(v
)) {
138 hres
= to_object(ctx
, v
, r
);
143 static inline HRESULT
stack_pop_int(script_ctx_t
*ctx
, INT
*r
)
145 return to_int32(ctx
, stack_pop(ctx
), r
);
148 static inline HRESULT
stack_pop_uint(script_ctx_t
*ctx
, UINT32
*r
)
150 return to_uint32(ctx
, stack_pop(ctx
), r
);
153 static inline unsigned local_off(call_frame_t
*frame
, int ref
)
156 ? frame
->arguments_off
- ref
-1
157 : frame
->variables_off
+ ref
;
160 static inline BSTR
local_name(call_frame_t
*frame
, int ref
)
162 return ref
< 0 ? frame
->function
->params
[-ref
-1] : frame
->function
->variables
[ref
].name
;
165 /* Steals input reference even on failure. */
166 static HRESULT
stack_push_exprval(script_ctx_t
*ctx
, exprval_t
*val
)
172 hres
= stack_push(ctx
, jsval_null());
174 hres
= stack_push(ctx
, val
->u
.val
);
177 hres
= stack_push(ctx
, jsval_disp(val
->u
.idref
.disp
));
179 hres
= stack_push(ctx
, jsval_number(val
->u
.idref
.id
));
181 IDispatch_Release(val
->u
.idref
.disp
);
183 case EXPRVAL_STACK_REF
:
184 hres
= stack_push(ctx
, jsval_number(val
->u
.off
));
186 hres
= stack_push(ctx
, jsval_undefined());
188 case EXPRVAL_INVALID
:
189 hres
= stack_push(ctx
, jsval_undefined());
191 hres
= stack_push(ctx
, jsval_number(val
->u
.hres
));
199 static BOOL
stack_topn_exprval(script_ctx_t
*ctx
, unsigned n
, exprval_t
*r
)
201 jsval_t v
= stack_topn(ctx
, n
+1);
203 switch(jsval_type(v
)) {
205 call_frame_t
*frame
= ctx
->call_ctx
;
206 scope_chain_t
*scope
;
207 unsigned off
= get_number(v
);
209 if(!frame
->base_scope
->frame
&& off
>= frame
->arguments_off
) {
212 HRESULT hres
= E_FAIL
;
214 /* Got stack reference in deoptimized code. Need to convert it back to variable object reference. */
216 assert(off
< frame
->variables_off
+ frame
->function
->var_cnt
);
217 if (off
>= frame
->variables_off
)
219 name
= frame
->function
->variables
[off
- frame
->variables_off
].name
;
220 scope
= frame
->scope
;
224 name
= frame
->function
->params
[off
- frame
->arguments_off
];
225 scope
= frame
->base_scope
;
230 if (scope
->jsobj
&& SUCCEEDED(hres
= jsdisp_get_id(scope
->jsobj
, name
, 0, &id
)))
232 if (scope
== frame
->base_scope
)
234 r
->type
= EXPRVAL_INVALID
;
241 *stack_top_ref(ctx
, n
+1) = jsval_obj(jsdisp_addref(scope
->jsobj
));
242 *stack_top_ref(ctx
, n
) = jsval_number(id
);
243 r
->type
= EXPRVAL_IDREF
;
244 r
->u
.idref
.disp
= scope
->obj
;
249 r
->type
= EXPRVAL_STACK_REF
;
254 r
->type
= EXPRVAL_IDREF
;
255 r
->u
.idref
.disp
= get_object(v
);
256 assert(is_number(stack_topn(ctx
, n
)));
257 r
->u
.idref
.id
= get_number(stack_topn(ctx
, n
));
260 r
->type
= EXPRVAL_INVALID
;
261 assert(is_number(stack_topn(ctx
, n
)));
262 r
->u
.hres
= get_number(stack_topn(ctx
, n
));
265 r
->type
= EXPRVAL_JSVAL
;
266 r
->u
.val
= stack_topn(ctx
, n
);
274 static inline BOOL
stack_pop_exprval(script_ctx_t
*ctx
, exprval_t
*r
)
276 BOOL ret
= stack_topn_exprval(ctx
, 0, r
);
281 static HRESULT
exprval_propput(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t v
)
284 case EXPRVAL_STACK_REF
: {
285 jsval_t
*r
= ctx
->stack
+ ref
->u
.off
;
287 return jsval_copy(v
, r
);
290 return disp_propput(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, v
);
292 WARN("ignoring an attempt to set value reference\n");
300 static HRESULT
exprval_propget(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t
*r
)
303 case EXPRVAL_STACK_REF
:
304 return jsval_copy(ctx
->stack
[ref
->u
.off
], r
);
306 return disp_propget(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, r
);
308 return jsval_copy(ref
->u
.val
, r
);
315 static HRESULT
exprval_call(script_ctx_t
*ctx
, exprval_t
*ref
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
318 case EXPRVAL_STACK_REF
: {
319 jsval_t v
= ctx
->stack
[ref
->u
.off
];
321 if(!is_object_instance(v
)) {
322 FIXME("invoke %s\n", debugstr_jsval(v
));
326 return disp_call_value(ctx
, get_object(v
), NULL
, flags
, argc
, argv
, r
);
329 return disp_call(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, flags
, argc
, argv
, r
);
330 case EXPRVAL_JSVAL
: {
334 hres
= to_object(ctx
, ref
->u
.val
, &obj
);
335 if(SUCCEEDED(hres
)) {
336 hres
= disp_call_value(ctx
, obj
, NULL
, flags
, argc
, argv
, r
);
337 IDispatch_Release(obj
);
347 /* ECMA-262 3rd Edition 8.7.1 */
348 /* Steals input reference. */
349 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t
*r
)
353 if(ref
->type
== EXPRVAL_JSVAL
) {
358 hres
= exprval_propget(ctx
, ref
, r
);
360 if(ref
->type
== EXPRVAL_IDREF
)
361 IDispatch_Release(ref
->u
.idref
.disp
);
365 static void exprval_release(exprval_t
*val
)
369 jsval_release(val
->u
.val
);
372 if(val
->u
.idref
.disp
)
373 IDispatch_Release(val
->u
.idref
.disp
);
375 case EXPRVAL_STACK_REF
:
376 case EXPRVAL_INVALID
:
381 static inline void exprval_set_exception(exprval_t
*val
, HRESULT hres
)
383 val
->type
= EXPRVAL_INVALID
;
387 static inline void exprval_set_disp_ref(exprval_t
*ref
, IDispatch
*obj
, DISPID id
)
389 ref
->type
= EXPRVAL_IDREF
;
390 IDispatch_AddRef(ref
->u
.idref
.disp
= obj
);
391 ref
->u
.idref
.id
= id
;
394 static inline jsval_t
steal_ret(call_frame_t
*frame
)
396 jsval_t r
= frame
->ret
;
397 frame
->ret
= jsval_undefined();
401 static inline void clear_acc(script_ctx_t
*ctx
)
403 jsval_release(ctx
->acc
);
404 ctx
->acc
= jsval_undefined();
407 static HRESULT
scope_push(scope_chain_t
*scope
, jsdisp_t
*jsobj
, IDispatch
*obj
, scope_chain_t
**ret
)
409 scope_chain_t
*new_scope
;
411 new_scope
= heap_alloc(sizeof(scope_chain_t
));
413 return E_OUTOFMEMORY
;
418 IDispatch_AddRef(obj
);
419 new_scope
->jsobj
= jsobj
;
420 new_scope
->obj
= obj
;
421 new_scope
->frame
= NULL
;
422 new_scope
->next
= scope
? scope_addref(scope
) : NULL
;
423 new_scope
->scope_index
= 0;
429 static void scope_pop(scope_chain_t
**scope
)
438 void scope_release(scope_chain_t
*scope
)
444 scope_release(scope
->next
);
447 IDispatch_Release(scope
->obj
);
451 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, const WCHAR
*name
, BSTR name_bstr
, DWORD flags
, DISPID
*id
)
458 jsdisp
= iface_to_jsdisp(disp
);
460 hres
= jsdisp_get_id(jsdisp
, name
, flags
, id
);
461 jsdisp_release(jsdisp
);
468 bstr
= SysAllocString(name
);
470 return E_OUTOFMEMORY
;
474 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
475 if(SUCCEEDED(hres
)) {
476 hres
= IDispatchEx_GetDispID(dispex
, bstr
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
477 IDispatchEx_Release(dispex
);
479 TRACE("using IDispatch\n");
480 hres
= IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &bstr
, 1, 0, id
);
483 if(name_bstr
!= bstr
)
488 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
490 IObjectIdentity
*identity
;
491 IUnknown
*unk1
, *unk2
;
499 if(!disp1
|| !disp2
) {
504 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
508 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
510 IUnknown_Release(unk1
);
517 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
518 if(SUCCEEDED(hres
)) {
519 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
520 IObjectIdentity_Release(identity
);
527 IUnknown_Release(unk1
);
528 IUnknown_Release(unk2
);
532 /* ECMA-262 3rd Edition 11.9.6 */
533 HRESULT
jsval_strict_equal(jsval_t lval
, jsval_t rval
, BOOL
*ret
)
535 jsval_type_t type
= jsval_type(lval
);
539 if(type
!= jsval_type(rval
)) {
550 return disp_cmp(get_object(lval
), get_object(rval
), ret
);
552 *ret
= jsstr_eq(get_string(lval
), get_string(rval
));
555 *ret
= get_number(lval
) == get_number(rval
);
558 *ret
= !get_bool(lval
) == !get_bool(rval
);
561 WARN("VARIANT type, returning false\n");
569 static HRESULT
detach_scope(script_ctx_t
*ctx
, call_frame_t
*frame
, scope_chain_t
*scope
)
571 function_code_t
*func
= frame
->function
;
572 unsigned int i
, index
;
578 assert(scope
->frame
== frame
);
585 if (FAILED(hres
= create_object(ctx
, NULL
, &scope
->jsobj
)))
587 scope
->obj
= to_disp(scope
->jsobj
);
590 if (scope
== frame
->base_scope
&& func
->name
&& func
->local_ref
== INVALID_LOCAL_REF
&&
591 ctx
->version
>= SCRIPTLANGUAGEVERSION_ES5
)
592 jsdisp_propput_name(scope
->jsobj
, func
->name
, jsval_obj(jsdisp_addref(frame
->function_instance
)));
594 index
= scope
->scope_index
;
595 for(i
= 0; i
< frame
->function
->local_scopes
[index
].locals_cnt
; i
++)
597 WCHAR
*name
= frame
->function
->local_scopes
[index
].locals
[i
].name
;
598 int ref
= frame
->function
->local_scopes
[index
].locals
[i
].ref
;
600 if (FAILED(hres
= jsdisp_propput_name(scope
->jsobj
, name
, ctx
->stack
[local_off(frame
, ref
)])))
602 if (frame
->function
->variables
[ref
].func_id
!= -1 && scope
!= frame
->base_scope
603 && FAILED(hres
= jsdisp_propput_name(frame
->variable_obj
, name
, ctx
->stack
[local_off(frame
, ref
)])))
609 static HRESULT
detach_scope_chain(script_ctx_t
*ctx
, call_frame_t
*frame
, scope_chain_t
*scope
)
613 if (scope
!= frame
->base_scope
&& FAILED(hres
= detach_scope_chain(ctx
, frame
, scope
->next
)))
615 return detach_scope(ctx
, frame
, scope
);
619 * Transfers local variables from stack to variable object.
620 * It's slow, so we want to avoid it as much as possible.
622 static HRESULT
detach_variable_object(script_ctx_t
*ctx
, call_frame_t
*frame
, BOOL from_release
)
626 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
629 TRACE("detaching %p\n", frame
);
631 assert(frame
== frame
->base_scope
->frame
);
632 assert(frame
->variable_obj
== frame
->base_scope
->jsobj
);
634 if(!from_release
&& !frame
->arguments_obj
) {
635 hres
= setup_arguments_object(ctx
, frame
);
640 TRACE("detaching scope chain %p, frame %p.\n", ctx
->call_ctx
->scope
, frame
);
641 return detach_scope_chain(ctx
, frame
, ctx
->call_ctx
->scope
);
644 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
650 LIST_FOR_EACH_ENTRY(item
, &ctx
->named_items
, named_item_t
, entry
) {
651 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
652 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
653 if(SUCCEEDED(hres
)) {
655 exprval_set_disp_ref(ret
, item
->disp
, id
);
664 IDispatch
*lookup_global_host(script_ctx_t
*ctx
)
666 IDispatch
*disp
= NULL
;
669 LIST_FOR_EACH_ENTRY(item
, &ctx
->named_items
, named_item_t
, entry
) {
670 if(!(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) continue;
674 if(!disp
) disp
= to_disp(ctx
->global
);
679 static int __cdecl
local_ref_cmp(const void *key
, const void *ref
)
681 return wcscmp((const WCHAR
*)key
, ((const local_ref_t
*)ref
)->name
);
684 local_ref_t
*lookup_local(const function_code_t
*function
, const WCHAR
*identifier
, unsigned int scope
)
686 return bsearch(identifier
, function
->local_scopes
[scope
].locals
, function
->local_scopes
[scope
].locals_cnt
,
687 sizeof(*function
->local_scopes
[scope
].locals
), local_ref_cmp
);
690 /* ECMA-262 3rd Edition 10.1.4 */
691 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
693 scope_chain_t
*scope
;
698 TRACE("%s\n", debugstr_w(identifier
));
701 for(scope
= ctx
->call_ctx
->scope
; scope
; scope
= scope
->next
) {
703 function_code_t
*func
= scope
->frame
->function
;
704 local_ref_t
*ref
= lookup_local(func
, identifier
, scope
->scope_index
);
707 ret
->type
= EXPRVAL_STACK_REF
;
708 ret
->u
.off
= local_off(scope
->frame
, ref
->ref
);
709 TRACE("returning ref %d for %d\n", ret
->u
.off
, ref
->ref
);
713 if(!wcscmp(identifier
, L
"arguments")) {
714 hres
= detach_variable_object(ctx
, scope
->frame
, FALSE
);
719 /* ECMA-262 5.1 Edition 13 */
720 if(func
->name
&& ctx
->version
>= SCRIPTLANGUAGEVERSION_ES5
&&
721 func
->local_ref
== INVALID_LOCAL_REF
&& !wcscmp(identifier
, func
->name
)) {
722 TRACE("returning a function from scope chain\n");
723 ret
->type
= EXPRVAL_JSVAL
;
724 ret
->u
.val
= jsval_obj(jsdisp_addref(scope
->frame
->function_instance
));
729 if (!scope
->jsobj
&& !scope
->obj
)
733 hres
= jsdisp_get_id(scope
->jsobj
, identifier
, fdexNameImplicit
, &id
);
735 hres
= disp_get_id(ctx
, scope
->obj
, identifier
, identifier
, fdexNameImplicit
, &id
);
736 if(SUCCEEDED(hres
)) {
737 exprval_set_disp_ref(ret
, scope
->obj
, id
);
742 item
= ctx
->call_ctx
->bytecode
->named_item
;
744 hres
= jsdisp_get_id(item
->script_obj
, identifier
, 0, &id
);
745 if(SUCCEEDED(hres
)) {
746 exprval_set_disp_ref(ret
, to_disp(item
->script_obj
), id
);
749 if(!(item
->flags
& SCRIPTITEM_CODEONLY
)) {
750 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
751 if(SUCCEEDED(hres
)) {
752 exprval_set_disp_ref(ret
, item
->disp
, id
);
759 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
760 if(SUCCEEDED(hres
)) {
761 exprval_set_disp_ref(ret
, to_disp(ctx
->global
), id
);
765 item
= lookup_named_item(ctx
, identifier
, SCRIPTITEM_ISVISIBLE
);
767 IDispatch_AddRef(item
->disp
);
768 ret
->type
= EXPRVAL_JSVAL
;
769 ret
->u
.val
= jsval_disp(item
->disp
);
773 if(lookup_global_members(ctx
, identifier
, ret
))
776 exprval_set_exception(ret
, JS_E_UNDEFINED_VARIABLE
);
780 static inline BSTR
get_op_bstr(script_ctx_t
*ctx
, int i
)
782 call_frame_t
*frame
= ctx
->call_ctx
;
783 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].bstr
;
786 static inline unsigned get_op_uint(script_ctx_t
*ctx
, int i
)
788 call_frame_t
*frame
= ctx
->call_ctx
;
789 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].uint
;
792 static inline unsigned get_op_int(script_ctx_t
*ctx
, int i
)
794 call_frame_t
*frame
= ctx
->call_ctx
;
795 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].lng
;
798 static inline jsstr_t
*get_op_str(script_ctx_t
*ctx
, int i
)
800 call_frame_t
*frame
= ctx
->call_ctx
;
801 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].str
;
804 static inline double get_op_double(script_ctx_t
*ctx
)
806 call_frame_t
*frame
= ctx
->call_ctx
;
807 return frame
->bytecode
->instrs
[frame
->ip
].u
.dbl
;
810 static inline void jmp_next(script_ctx_t
*ctx
)
815 static inline void jmp_abs(script_ctx_t
*ctx
, unsigned dst
)
817 ctx
->call_ctx
->ip
= dst
;
820 /* ECMA-262 3rd Edition 12.6.4 */
821 static HRESULT
interp_forin(script_ctx_t
*ctx
)
823 const HRESULT arg
= get_op_uint(ctx
, 0);
824 IDispatch
*obj
= NULL
;
833 assert(is_number(stack_top(ctx
)));
834 id
= get_number(stack_top(ctx
));
836 if(!stack_topn_exprval(ctx
, 1, &prop_ref
)) {
837 FIXME("invalid ref: %08lx\n", prop_ref
.u
.hres
);
841 if(is_object_instance(stack_topn(ctx
, 3)))
842 obj
= get_object(stack_topn(ctx
, 3));
845 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
846 if(SUCCEEDED(hres
)) {
847 hres
= IDispatchEx_GetNextDispID(dispex
, fdexEnumDefault
, id
, &id
);
849 hres
= IDispatchEx_GetMemberName(dispex
, id
, &name
);
850 IDispatchEx_Release(dispex
);
854 TRACE("No IDispatchEx\n");
861 str
= jsstr_alloc_len(name
, SysStringLen(name
));
864 return E_OUTOFMEMORY
;
867 stack_push(ctx
, jsval_number(id
)); /* safe, just after pop() */
869 hres
= exprval_propput(ctx
, &prop_ref
, jsval_string(str
));
882 static HRESULT
scope_init_locals(script_ctx_t
*ctx
)
884 call_frame_t
*frame
= ctx
->call_ctx
;
885 unsigned int i
, off
, index
;
886 scope_chain_t
*scope
;
890 scope
= frame
->scope
;
891 index
= scope
->scope_index
;
892 detached_vars
= !(frame
->base_scope
&& frame
->base_scope
->frame
);
896 assert(frame
->base_scope
->frame
== frame
);
897 frame
->scope
->frame
= ctx
->call_ctx
;
899 else if (!scope
->jsobj
)
902 if (FAILED(hres
= create_object(ctx
, NULL
, &scope
->jsobj
)))
904 scope
->obj
= to_disp(scope
->jsobj
);
907 for(i
= 0; i
< frame
->function
->local_scopes
[index
].locals_cnt
; i
++)
909 WCHAR
*name
= frame
->function
->local_scopes
[index
].locals
[i
].name
;
910 int ref
= frame
->function
->local_scopes
[index
].locals
[i
].ref
;
914 if (frame
->function
->variables
[ref
].func_id
!= -1)
916 TRACE("function %s %d\n", debugstr_w(name
), i
);
918 if (FAILED(hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
919 + frame
->function
->variables
[ref
].func_id
, ctx
->call_ctx
->scope
, &func_obj
)))
921 val
= jsval_obj(func_obj
);
922 if (detached_vars
&& FAILED(hres
= jsdisp_propput_name(frame
->variable_obj
, name
, jsval_obj(func_obj
))))
927 val
= jsval_undefined();
932 if (FAILED(hres
= jsdisp_propput_name(scope
->jsobj
, name
, val
)))
937 off
= local_off(frame
, ref
);
938 jsval_release(ctx
->stack
[off
]);
939 ctx
->stack
[off
] = val
;
945 /* ECMA-262 3rd Edition 12.10 */
946 static HRESULT
interp_push_with_scope(script_ctx_t
*ctx
)
955 hres
= to_object(ctx
, v
, &disp
);
960 hres
= scope_push(ctx
->call_ctx
->scope
, to_jsdisp(disp
), disp
, &ctx
->call_ctx
->scope
);
961 IDispatch_Release(disp
);
965 /* ECMA-262 10th Edition 13.3.1 */
966 static HRESULT
interp_push_block_scope(script_ctx_t
*ctx
)
968 unsigned int scope_index
= get_op_uint(ctx
, 0);
969 call_frame_t
*frame
= ctx
->call_ctx
;
972 TRACE("scope_index %u.\n", scope_index
);
974 hres
= scope_push(ctx
->call_ctx
->scope
, NULL
, NULL
, &frame
->scope
);
976 if (FAILED(hres
) || !scope_index
)
979 frame
->scope
->scope_index
= scope_index
;
981 return scope_init_locals(ctx
);
984 /* ECMA-262 3rd Edition 12.10 */
985 static HRESULT
interp_pop_scope(script_ctx_t
*ctx
)
989 if(ctx
->call_ctx
->scope
->ref
> 1) {
990 HRESULT hres
= detach_variable_object(ctx
, ctx
->call_ctx
, FALSE
);
992 ERR("Failed to detach variable object: %08lx\n", hres
);
995 scope_pop(&ctx
->call_ctx
->scope
);
999 /* ECMA-262 3rd Edition 12.13 */
1000 static HRESULT
interp_case(script_ctx_t
*ctx
)
1002 const unsigned arg
= get_op_uint(ctx
, 0);
1010 hres
= jsval_strict_equal(stack_top(ctx
), v
, &b
);
1024 static void set_error_value(script_ctx_t
*ctx
, jsval_t value
)
1026 jsexcept_t
*ei
= ctx
->ei
;
1030 ei
->error
= JS_E_EXCEPTION_THROWN
;
1031 ei
->valid_value
= TRUE
;
1034 if(is_object_instance(value
) && (obj
= to_jsdisp(get_object(value
)))) {
1040 /* FIXME: We should check if object is an error instance */
1042 hres
= jsdisp_propget_name(obj
, L
"number", &v
);
1043 if(SUCCEEDED(hres
)) {
1044 hres
= to_uint32(ctx
, v
, &number
);
1046 ei
->error
= FAILED(number
) ? number
: E_FAIL
;
1050 hres
= jsdisp_propget_name(obj
, L
"description", &v
);
1051 if(SUCCEEDED(hres
)) {
1052 hres
= to_string(ctx
, v
, &str
);
1060 /* ECMA-262 3rd Edition 12.13 */
1061 static HRESULT
interp_throw(script_ctx_t
*ctx
)
1065 set_error_value(ctx
, stack_pop(ctx
));
1066 return DISP_E_EXCEPTION
;
1069 static HRESULT
interp_throw_ref(script_ctx_t
*ctx
)
1071 const HRESULT arg
= get_op_uint(ctx
, 0);
1073 TRACE("%08lx\n", arg
);
1078 static HRESULT
interp_throw_type(script_ctx_t
*ctx
)
1080 const HRESULT hres
= get_op_uint(ctx
, 0);
1081 jsstr_t
*str
= get_op_str(ctx
, 1);
1084 TRACE("%08lx %s\n", hres
, debugstr_jsstr(str
));
1086 ptr
= jsstr_flatten(str
);
1087 return ptr
? throw_error(ctx
, hres
, ptr
) : E_OUTOFMEMORY
;
1090 /* ECMA-262 3rd Edition 12.14 */
1091 static HRESULT
interp_push_except(script_ctx_t
*ctx
)
1093 const unsigned catch_off
= get_op_uint(ctx
, 0);
1094 const unsigned finally_off
= get_op_uint(ctx
, 1);
1095 call_frame_t
*frame
= ctx
->call_ctx
;
1096 except_frame_t
*except
;
1100 except
= heap_alloc(sizeof(*except
));
1102 return E_OUTOFMEMORY
;
1104 except
->stack_top
= ctx
->stack_top
;
1105 except
->scope
= frame
->scope
;
1106 except
->catch_off
= catch_off
;
1107 except
->finally_off
= finally_off
;
1108 except
->next
= frame
->except_frame
;
1109 frame
->except_frame
= except
;
1113 /* ECMA-262 3rd Edition 12.14 */
1114 static HRESULT
interp_pop_except(script_ctx_t
*ctx
)
1116 const unsigned ret_off
= get_op_uint(ctx
, 0);
1117 call_frame_t
*frame
= ctx
->call_ctx
;
1118 except_frame_t
*except
;
1119 unsigned finally_off
;
1121 TRACE("%u\n", ret_off
);
1123 except
= frame
->except_frame
;
1124 assert(except
!= NULL
);
1126 finally_off
= except
->finally_off
;
1127 frame
->except_frame
= except
->next
;
1133 hres
= stack_push(ctx
, jsval_number(ret_off
));
1136 hres
= stack_push(ctx
, jsval_bool(TRUE
));
1139 frame
->ip
= finally_off
;
1141 frame
->ip
= ret_off
;
1147 /* ECMA-262 3rd Edition 12.14 */
1148 static HRESULT
interp_end_finally(script_ctx_t
*ctx
)
1150 call_frame_t
*frame
= ctx
->call_ctx
;
1159 TRACE("passing exception\n");
1161 set_error_value(ctx
, stack_pop(ctx
));
1162 return DISP_E_EXCEPTION
;
1166 assert(is_number(v
));
1167 frame
->ip
= get_number(v
);
1171 static HRESULT
interp_enter_catch(script_ctx_t
*ctx
)
1173 const BSTR ident
= get_op_bstr(ctx
, 0);
1174 jsdisp_t
*scope_obj
;
1178 hres
= create_dispex(ctx
, NULL
, NULL
, &scope_obj
);
1183 hres
= jsdisp_propput_name(scope_obj
, ident
, v
);
1186 hres
= scope_push(ctx
->call_ctx
->scope
, scope_obj
, to_disp(scope_obj
), &ctx
->call_ctx
->scope
);
1187 jsdisp_release(scope_obj
);
1191 /* ECMA-262 3rd Edition 13 */
1192 static HRESULT
interp_func(script_ctx_t
*ctx
)
1194 unsigned func_idx
= get_op_uint(ctx
, 0);
1195 call_frame_t
*frame
= ctx
->call_ctx
;
1199 TRACE("%d\n", func_idx
);
1201 hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
+func_idx
,
1202 frame
->scope
, &dispex
);
1206 return stack_push(ctx
, jsval_obj(dispex
));
1209 /* ECMA-262 3rd Edition 11.2.1 */
1210 static HRESULT
interp_array(script_ctx_t
*ctx
)
1221 namev
= stack_pop(ctx
);
1223 hres
= stack_pop_object(ctx
, &obj
);
1225 jsval_release(namev
);
1229 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
1230 jsval_release(namev
);
1232 IDispatch_Release(obj
);
1236 hres
= disp_get_id(ctx
, obj
, name
, NULL
, 0, &id
);
1237 jsstr_release(name_str
);
1238 if(SUCCEEDED(hres
)) {
1239 hres
= disp_propget(ctx
, obj
, id
, &v
);
1240 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1241 v
= jsval_undefined();
1244 IDispatch_Release(obj
);
1248 return stack_push(ctx
, v
);
1251 /* ECMA-262 3rd Edition 11.2.1 */
1252 static HRESULT
interp_member(script_ctx_t
*ctx
)
1254 const BSTR arg
= get_op_bstr(ctx
, 0);
1262 hres
= stack_pop_object(ctx
, &obj
);
1266 hres
= disp_get_id(ctx
, obj
, arg
, arg
, 0, &id
);
1267 if(SUCCEEDED(hres
)) {
1268 hres
= disp_propget(ctx
, obj
, id
, &v
);
1269 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1270 v
= jsval_undefined();
1273 IDispatch_Release(obj
);
1277 return stack_push(ctx
, v
);
1280 /* ECMA-262 3rd Edition 11.2.1 */
1281 static HRESULT
interp_memberid(script_ctx_t
*ctx
)
1283 const unsigned arg
= get_op_uint(ctx
, 0);
1284 jsval_t objv
, namev
;
1294 namev
= stack_pop(ctx
);
1295 objv
= stack_pop(ctx
);
1297 hres
= to_object(ctx
, objv
, &obj
);
1298 jsval_release(objv
);
1299 if(SUCCEEDED(hres
)) {
1300 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
1302 IDispatch_Release(obj
);
1304 jsval_release(namev
);
1308 hres
= disp_get_id(ctx
, obj
, name
, NULL
, arg
, &id
);
1309 jsstr_release(name_str
);
1310 if(SUCCEEDED(hres
)) {
1311 ref
.type
= EXPRVAL_IDREF
;
1312 ref
.u
.idref
.disp
= obj
;
1313 ref
.u
.idref
.id
= id
;
1315 IDispatch_Release(obj
);
1316 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
1317 exprval_set_exception(&ref
, JS_E_INVALID_PROPERTY
);
1320 ERR("failed %08lx\n", hres
);
1325 return stack_push_exprval(ctx
, &ref
);
1328 /* ECMA-262 3rd Edition 11.2.1 */
1329 static HRESULT
interp_refval(script_ctx_t
*ctx
)
1337 if(!stack_topn_exprval(ctx
, 0, &ref
))
1338 return JS_E_ILLEGAL_ASSIGN
;
1340 hres
= exprval_propget(ctx
, &ref
, &v
);
1344 return stack_push(ctx
, v
);
1347 /* ECMA-262 3rd Edition 11.2.2 */
1348 static HRESULT
interp_new(script_ctx_t
*ctx
)
1350 const unsigned argc
= get_op_uint(ctx
, 0);
1353 TRACE("%d\n", argc
);
1355 constr
= stack_topn(ctx
, argc
);
1357 /* NOTE: Should use to_object here */
1360 return is_null_disp(constr
) ? JS_E_INVALID_PROPERTY
: JS_E_OBJECT_EXPECTED
;
1361 if(!is_object_instance(constr
))
1362 return JS_E_INVALID_ACTION
;
1365 return disp_call_value(ctx
, get_object(constr
), NULL
, DISPATCH_CONSTRUCT
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1366 argc
, stack_args(ctx
, argc
), &ctx
->acc
);
1369 /* ECMA-262 3rd Edition 11.2.3 */
1370 static HRESULT
interp_call(script_ctx_t
*ctx
)
1372 const unsigned argn
= get_op_uint(ctx
, 0);
1373 const int do_ret
= get_op_int(ctx
, 1);
1376 TRACE("%d %d\n", argn
, do_ret
);
1378 obj
= stack_topn(ctx
, argn
);
1379 if(!is_object_instance(obj
))
1380 return JS_E_INVALID_PROPERTY
;
1383 return disp_call_value(ctx
, get_object(obj
), NULL
, DISPATCH_METHOD
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1384 argn
, stack_args(ctx
, argn
), do_ret
? &ctx
->acc
: NULL
);
1387 /* ECMA-262 3rd Edition 11.2.3 */
1388 static HRESULT
interp_call_member(script_ctx_t
*ctx
)
1390 const unsigned argn
= get_op_uint(ctx
, 0);
1391 const int do_ret
= get_op_int(ctx
, 1);
1394 TRACE("%d %d\n", argn
, do_ret
);
1396 if(!stack_topn_exprval(ctx
, argn
, &ref
))
1400 return exprval_call(ctx
, &ref
, DISPATCH_METHOD
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1401 argn
, stack_args(ctx
, argn
), do_ret
? &ctx
->acc
: NULL
);
1404 /* ECMA-262 3rd Edition 11.1.1 */
1405 static HRESULT
interp_this(script_ctx_t
*ctx
)
1407 IDispatch
*this_obj
= ctx
->call_ctx
->this_obj
;
1412 named_item_t
*item
= ctx
->call_ctx
->bytecode
->named_item
;
1415 this_obj
= (item
->flags
& SCRIPTITEM_CODEONLY
) ? to_disp(item
->script_obj
) : item
->disp
;
1417 this_obj
= lookup_global_host(ctx
);
1420 IDispatch_AddRef(this_obj
);
1421 return stack_push(ctx
, jsval_disp(this_obj
));
1424 static HRESULT
interp_identifier_ref(script_ctx_t
*ctx
, BSTR identifier
, unsigned flags
)
1429 hres
= identifier_eval(ctx
, identifier
, &exprval
);
1433 if(exprval
.type
== EXPRVAL_INVALID
&& (flags
& fdexNameEnsure
)) {
1434 jsdisp_t
*script_obj
= ctx
->global
;
1437 if(ctx
->call_ctx
->bytecode
->named_item
)
1438 script_obj
= ctx
->call_ctx
->bytecode
->named_item
->script_obj
;
1440 hres
= jsdisp_get_id(script_obj
, identifier
, fdexNameEnsure
, &id
);
1444 exprval_set_disp_ref(&exprval
, to_disp(script_obj
), id
);
1447 if(exprval
.type
== EXPRVAL_INVALID
||
1448 (exprval
.type
== EXPRVAL_JSVAL
&& ctx
->version
< SCRIPTLANGUAGEVERSION_ES5
)) {
1449 WARN("invalid ref\n");
1450 exprval_release(&exprval
);
1451 exprval_set_exception(&exprval
, JS_E_OBJECT_EXPECTED
);
1454 return stack_push_exprval(ctx
, &exprval
);
1457 static HRESULT
identifier_value(script_ctx_t
*ctx
, BSTR identifier
)
1463 hres
= identifier_eval(ctx
, identifier
, &exprval
);
1467 if(exprval
.type
== EXPRVAL_INVALID
)
1468 return throw_error(ctx
, exprval
.u
.hres
, identifier
);
1470 hres
= exprval_to_value(ctx
, &exprval
, &v
);
1474 return stack_push(ctx
, v
);
1477 static HRESULT
interp_local_ref(script_ctx_t
*ctx
)
1479 const int arg
= get_op_int(ctx
, 0);
1480 const unsigned flags
= get_op_uint(ctx
, 1);
1481 call_frame_t
*frame
= ctx
->call_ctx
;
1484 TRACE("%s\n", debugstr_w(local_name(frame
, arg
)));
1486 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
1487 return interp_identifier_ref(ctx
, local_name(frame
, arg
), flags
);
1489 ref
.type
= EXPRVAL_STACK_REF
;
1490 ref
.u
.off
= local_off(frame
, arg
);
1491 return stack_push_exprval(ctx
, &ref
);
1494 static HRESULT
interp_local(script_ctx_t
*ctx
)
1496 const int arg
= get_op_int(ctx
, 0);
1497 call_frame_t
*frame
= ctx
->call_ctx
;
1501 if(!frame
->base_scope
|| !frame
->base_scope
->frame
) {
1502 TRACE("%s\n", debugstr_w(local_name(frame
, arg
)));
1503 return identifier_value(ctx
, local_name(frame
, arg
));
1506 hres
= jsval_copy(ctx
->stack
[local_off(frame
, arg
)], ©
);
1510 TRACE("%s: %s\n", debugstr_w(local_name(frame
, arg
)), debugstr_jsval(copy
));
1511 return stack_push(ctx
, copy
);
1514 /* ECMA-262 3rd Edition 10.1.4 */
1515 static HRESULT
interp_ident(script_ctx_t
*ctx
)
1517 const BSTR arg
= get_op_bstr(ctx
, 0);
1519 TRACE("%s\n", debugstr_w(arg
));
1521 return identifier_value(ctx
, arg
);
1524 /* ECMA-262 3rd Edition 10.1.4 */
1525 static HRESULT
interp_identid(script_ctx_t
*ctx
)
1527 const BSTR arg
= get_op_bstr(ctx
, 0);
1528 const unsigned flags
= get_op_uint(ctx
, 1);
1530 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1532 return interp_identifier_ref(ctx
, arg
, flags
);
1535 /* ECMA-262 3rd Edition 7.8.1 */
1536 static HRESULT
interp_null(script_ctx_t
*ctx
)
1540 return stack_push(ctx
, jsval_null());
1543 /* ECMA-262 3rd Edition 7.8.2 */
1544 static HRESULT
interp_bool(script_ctx_t
*ctx
)
1546 const int arg
= get_op_int(ctx
, 0);
1548 TRACE("%s\n", arg
? "true" : "false");
1550 return stack_push(ctx
, jsval_bool(arg
));
1553 /* ECMA-262 3rd Edition 7.8.3 */
1554 static HRESULT
interp_int(script_ctx_t
*ctx
)
1556 const int arg
= get_op_int(ctx
, 0);
1560 return stack_push(ctx
, jsval_number(arg
));
1563 /* ECMA-262 3rd Edition 7.8.3 */
1564 static HRESULT
interp_double(script_ctx_t
*ctx
)
1566 const double arg
= get_op_double(ctx
);
1568 TRACE("%lf\n", arg
);
1570 return stack_push(ctx
, jsval_number(arg
));
1573 /* ECMA-262 3rd Edition 7.8.4 */
1574 static HRESULT
interp_str(script_ctx_t
*ctx
)
1576 jsstr_t
*str
= get_op_str(ctx
, 0);
1578 TRACE("%s\n", debugstr_jsstr(str
));
1580 return stack_push(ctx
, jsval_string(jsstr_addref(str
)));
1583 /* ECMA-262 3rd Edition 7.8 */
1584 static HRESULT
interp_regexp(script_ctx_t
*ctx
)
1586 jsstr_t
*source
= get_op_str(ctx
, 0);
1587 const unsigned flags
= get_op_uint(ctx
, 1);
1591 TRACE("%s %x\n", debugstr_jsstr(source
), flags
);
1593 hres
= create_regexp(ctx
, source
, flags
, ®exp
);
1597 return stack_push(ctx
, jsval_obj(regexp
));
1600 /* ECMA-262 3rd Edition 11.1.4 */
1601 static HRESULT
interp_carray(script_ctx_t
*ctx
)
1603 const unsigned arg
= get_op_uint(ctx
, 0);
1609 hres
= create_array(ctx
, arg
, &array
);
1613 return stack_push(ctx
, jsval_obj(array
));
1616 static HRESULT
interp_carray_set(script_ctx_t
*ctx
)
1618 const unsigned index
= get_op_uint(ctx
, 0);
1619 jsval_t value
, array
;
1622 value
= stack_pop(ctx
);
1624 TRACE("[%u] = %s\n", index
, debugstr_jsval(value
));
1626 array
= stack_top(ctx
);
1627 assert(is_object_instance(array
));
1629 hres
= jsdisp_propput_idx(iface_to_jsdisp(get_object(array
)), index
, value
);
1630 jsval_release(value
);
1634 /* ECMA-262 3rd Edition 11.1.5 */
1635 static HRESULT
interp_new_obj(script_ctx_t
*ctx
)
1642 hres
= create_object(ctx
, NULL
, &obj
);
1646 return stack_push(ctx
, jsval_obj(obj
));
1649 /* ECMA-262 3rd Edition 11.1.5 */
1650 static HRESULT
interp_obj_prop(script_ctx_t
*ctx
)
1652 jsstr_t
*name_arg
= get_op_str(ctx
, 0);
1653 unsigned type
= get_op_uint(ctx
, 1);
1659 TRACE("%s\n", debugstr_jsstr(name_arg
));
1661 val
= stack_pop(ctx
);
1663 /* FIXME: we should pass it as jsstr_t */
1664 name
= jsstr_flatten(name_arg
);
1666 assert(is_object_instance(stack_top(ctx
)));
1667 obj
= as_jsdisp(get_object(stack_top(ctx
)));
1669 if(type
== PROPERTY_DEFINITION_VALUE
) {
1670 hres
= jsdisp_propput_name(obj
, name
, val
);
1672 property_desc_t desc
= {PROPF_ENUMERABLE
| PROPF_CONFIGURABLE
};
1675 assert(is_object_instance(val
));
1676 func
= iface_to_jsdisp(get_object(val
));
1678 desc
.mask
= desc
.flags
;
1679 if(type
== PROPERTY_DEFINITION_GETTER
) {
1680 desc
.explicit_getter
= TRUE
;
1683 desc
.explicit_setter
= TRUE
;
1687 hres
= jsdisp_define_property(obj
, name
, &desc
);
1688 jsdisp_release(func
);
1695 /* ECMA-262 3rd Edition 11.11 */
1696 static HRESULT
interp_cnd_nz(script_ctx_t
*ctx
)
1698 const unsigned arg
= get_op_uint(ctx
, 0);
1704 hres
= to_boolean(stack_top(ctx
), &b
);
1717 /* ECMA-262 3rd Edition 11.11 */
1718 static HRESULT
interp_cnd_z(script_ctx_t
*ctx
)
1720 const unsigned arg
= get_op_uint(ctx
, 0);
1726 hres
= to_boolean(stack_top(ctx
), &b
);
1739 /* ECMA-262 3rd Edition 11.10 */
1740 static HRESULT
interp_or(script_ctx_t
*ctx
)
1747 hres
= stack_pop_int(ctx
, &r
);
1751 hres
= stack_pop_int(ctx
, &l
);
1755 return stack_push(ctx
, jsval_number(l
|r
));
1758 /* ECMA-262 3rd Edition 11.10 */
1759 static HRESULT
interp_xor(script_ctx_t
*ctx
)
1766 hres
= stack_pop_int(ctx
, &r
);
1770 hres
= stack_pop_int(ctx
, &l
);
1774 return stack_push(ctx
, jsval_number(l
^r
));
1777 /* ECMA-262 3rd Edition 11.10 */
1778 static HRESULT
interp_and(script_ctx_t
*ctx
)
1785 hres
= stack_pop_int(ctx
, &r
);
1789 hres
= stack_pop_int(ctx
, &l
);
1793 return stack_push(ctx
, jsval_number(l
&r
));
1796 /* ECMA-262 3rd Edition 11.8.6 */
1797 static HRESULT
interp_instanceof(script_ctx_t
*ctx
)
1799 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
1805 if(!is_object_instance(v
)) {
1807 return JS_E_FUNCTION_EXPECTED
;
1810 obj
= iface_to_jsdisp(get_object(v
));
1811 IDispatch_Release(get_object(v
));
1813 FIXME("non-jsdisp objects not supported\n");
1817 if(is_class(obj
, JSCLASS_FUNCTION
)) {
1818 hres
= jsdisp_propget_name(obj
, L
"prototype", &prot
);
1820 hres
= JS_E_FUNCTION_EXPECTED
;
1822 jsdisp_release(obj
);
1829 hres
= JS_E_OBJECT_EXPECTED
;
1830 else if(is_object_instance(prot
)) {
1831 if(is_object_instance(v
))
1832 tmp
= iface_to_jsdisp(get_object(v
));
1833 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
1834 hres
= disp_cmp(get_object(prot
), to_disp(iter
), &ret
);
1840 jsdisp_release(tmp
);
1842 FIXME("prototype is not an object\n");
1846 jsval_release(prot
);
1851 return stack_push(ctx
, jsval_bool(ret
));
1854 /* ECMA-262 3rd Edition 11.8.7 */
1855 static HRESULT
interp_in(script_ctx_t
*ctx
)
1866 obj
= stack_pop(ctx
);
1867 if(!is_object_instance(obj
)) {
1869 return JS_E_OBJECT_EXPECTED
;
1873 hres
= to_flat_string(ctx
, v
, &jsstr
, &str
);
1876 IDispatch_Release(get_object(obj
));
1880 hres
= disp_get_id(ctx
, get_object(obj
), str
, NULL
, 0, &id
);
1881 IDispatch_Release(get_object(obj
));
1882 jsstr_release(jsstr
);
1885 else if(hres
== DISP_E_UNKNOWNNAME
)
1890 return stack_push(ctx
, jsval_bool(ret
));
1893 /* ECMA-262 3rd Edition 11.6.1 */
1894 static HRESULT
interp_add(script_ctx_t
*ctx
)
1896 jsval_t l
, r
, lval
, rval
, ret
;
1899 rval
= stack_pop(ctx
);
1900 lval
= stack_pop(ctx
);
1902 TRACE("%s + %s\n", debugstr_jsval(lval
), debugstr_jsval(rval
));
1904 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
1905 if(SUCCEEDED(hres
)) {
1906 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
1910 jsval_release(lval
);
1911 jsval_release(rval
);
1915 if(is_string(l
) || is_string(r
)) {
1916 jsstr_t
*lstr
, *rstr
= NULL
;
1918 hres
= to_string(ctx
, l
, &lstr
);
1920 hres
= to_string(ctx
, r
, &rstr
);
1922 if(SUCCEEDED(hres
)) {
1925 ret_str
= jsstr_concat(lstr
, rstr
);
1927 ret
= jsval_string(ret_str
);
1929 hres
= E_OUTOFMEMORY
;
1932 jsstr_release(lstr
);
1934 jsstr_release(rstr
);
1938 hres
= to_number(ctx
, l
, &nl
);
1939 if(SUCCEEDED(hres
)) {
1940 hres
= to_number(ctx
, r
, &nr
);
1942 ret
= jsval_number(nl
+nr
);
1951 return stack_push(ctx
, ret
);
1954 /* ECMA-262 3rd Edition 11.6.2 */
1955 static HRESULT
interp_sub(script_ctx_t
*ctx
)
1962 hres
= stack_pop_number(ctx
, &r
);
1966 hres
= stack_pop_number(ctx
, &l
);
1970 return stack_push(ctx
, jsval_number(l
-r
));
1973 /* ECMA-262 3rd Edition 11.5.1 */
1974 static HRESULT
interp_mul(script_ctx_t
*ctx
)
1981 hres
= stack_pop_number(ctx
, &r
);
1985 hres
= stack_pop_number(ctx
, &l
);
1989 return stack_push(ctx
, jsval_number(l
*r
));
1992 /* ECMA-262 3rd Edition 11.5.2 */
1993 static HRESULT
interp_div(script_ctx_t
*ctx
)
2000 hres
= stack_pop_number(ctx
, &r
);
2004 hres
= stack_pop_number(ctx
, &l
);
2008 return stack_push(ctx
, jsval_number(l
/r
));
2011 /* ECMA-262 3rd Edition 11.5.3 */
2012 static HRESULT
interp_mod(script_ctx_t
*ctx
)
2019 hres
= stack_pop_number(ctx
, &r
);
2023 hres
= stack_pop_number(ctx
, &l
);
2027 return stack_push(ctx
, jsval_number(fmod(l
, r
)));
2030 /* ECMA-262 3rd Edition 11.4.2 */
2031 static HRESULT
interp_delete(script_ctx_t
*ctx
)
2033 jsval_t objv
, namev
;
2041 namev
= stack_pop(ctx
);
2042 objv
= stack_pop(ctx
);
2044 hres
= to_object(ctx
, objv
, &obj
);
2045 jsval_release(objv
);
2047 jsval_release(namev
);
2051 hres
= to_string(ctx
, namev
, &name
);
2052 jsval_release(namev
);
2054 IDispatch_Release(obj
);
2058 hres
= disp_delete_name(ctx
, obj
, name
, &ret
);
2059 IDispatch_Release(obj
);
2060 jsstr_release(name
);
2064 return stack_push(ctx
, jsval_bool(ret
));
2067 /* ECMA-262 3rd Edition 11.4.2 */
2068 static HRESULT
interp_delete_ident(script_ctx_t
*ctx
)
2070 const BSTR arg
= get_op_bstr(ctx
, 0);
2075 TRACE("%s\n", debugstr_w(arg
));
2077 hres
= identifier_eval(ctx
, arg
, &exprval
);
2081 switch(exprval
.type
) {
2082 case EXPRVAL_STACK_REF
:
2086 hres
= disp_delete(exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
, &ret
);
2087 IDispatch_Release(exprval
.u
.idref
.disp
);
2091 case EXPRVAL_INVALID
:
2095 FIXME("Unsupported exprval\n");
2096 exprval_release(&exprval
);
2101 return stack_push(ctx
, jsval_bool(ret
));
2104 /* ECMA-262 3rd Edition 11.4.2 */
2105 static HRESULT
interp_void(script_ctx_t
*ctx
)
2110 return stack_push(ctx
, jsval_undefined());
2113 /* ECMA-262 3rd Edition 11.4.3 */
2114 static HRESULT
typeof_string(jsval_t v
, const WCHAR
**ret
)
2116 switch(jsval_type(v
)) {
2118 *ret
= L
"undefined";
2126 if((dispex
= iface_to_jsdisp(get_object(v
)))) {
2127 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? L
"function" : L
"object";
2128 jsdisp_release(dispex
);
2144 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v
)));
2151 /* ECMA-262 3rd Edition 11.4.3 */
2152 static HRESULT
interp_typeofid(script_ctx_t
*ctx
)
2161 if(!stack_pop_exprval(ctx
, &ref
))
2162 return stack_push(ctx
, jsval_string(jsstr_undefined()));
2164 hres
= exprval_propget(ctx
, &ref
, &v
);
2165 exprval_release(&ref
);
2167 return stack_push_string(ctx
, L
"unknown");
2169 hres
= typeof_string(v
, &ret
);
2174 return stack_push_string(ctx
, ret
);
2177 /* ECMA-262 3rd Edition 11.4.3 */
2178 static HRESULT
interp_typeofident(script_ctx_t
*ctx
)
2180 const BSTR arg
= get_op_bstr(ctx
, 0);
2186 TRACE("%s\n", debugstr_w(arg
));
2188 hres
= identifier_eval(ctx
, arg
, &exprval
);
2192 if(exprval
.type
== EXPRVAL_INVALID
)
2193 return stack_push(ctx
, jsval_string(jsstr_undefined()));
2195 hres
= exprval_to_value(ctx
, &exprval
, &v
);
2199 hres
= typeof_string(v
, &ret
);
2204 return stack_push_string(ctx
, ret
);
2207 /* ECMA-262 3rd Edition 11.4.3 */
2208 static HRESULT
interp_typeof(script_ctx_t
*ctx
)
2217 hres
= typeof_string(v
, &ret
);
2222 return stack_push_string(ctx
, ret
);
2225 /* ECMA-262 3rd Edition 11.4.7 */
2226 static HRESULT
interp_minus(script_ctx_t
*ctx
)
2233 hres
= stack_pop_number(ctx
, &n
);
2237 return stack_push(ctx
, jsval_number(-n
));
2240 /* ECMA-262 3rd Edition 11.4.6 */
2241 static HRESULT
interp_tonum(script_ctx_t
*ctx
)
2250 hres
= to_number(ctx
, v
, &n
);
2255 return stack_push(ctx
, jsval_number(n
));
2258 /* ECMA-262 3rd Edition 11.3.1 */
2259 static HRESULT
interp_postinc(script_ctx_t
*ctx
)
2261 const int arg
= get_op_int(ctx
, 0);
2268 if(!stack_pop_exprval(ctx
, &ref
))
2269 return JS_E_OBJECT_EXPECTED
;
2271 hres
= exprval_propget(ctx
, &ref
, &v
);
2272 if(SUCCEEDED(hres
)) {
2275 hres
= to_number(ctx
, v
, &n
);
2277 hres
= exprval_propput(ctx
, &ref
, jsval_number(n
+(double)arg
));
2281 exprval_release(&ref
);
2285 return stack_push(ctx
, v
);
2288 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2289 static HRESULT
interp_preinc(script_ctx_t
*ctx
)
2291 const int arg
= get_op_int(ctx
, 0);
2299 if(!stack_pop_exprval(ctx
, &ref
))
2300 return JS_E_OBJECT_EXPECTED
;
2302 hres
= exprval_propget(ctx
, &ref
, &v
);
2303 if(SUCCEEDED(hres
)) {
2306 hres
= to_number(ctx
, v
, &n
);
2308 if(SUCCEEDED(hres
)) {
2309 ret
= n
+(double)arg
;
2310 hres
= exprval_propput(ctx
, &ref
, jsval_number(ret
));
2313 exprval_release(&ref
);
2317 return stack_push(ctx
, jsval_number(ret
));
2320 /* ECMA-262 3rd Edition 11.9.3 */
2321 static HRESULT
equal_values(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL
*ret
)
2323 if(jsval_type(lval
) == jsval_type(rval
) || (is_number(lval
) && is_number(rval
)))
2324 return jsval_strict_equal(lval
, rval
, ret
);
2326 if((is_null(lval
) && is_undefined(rval
)) || (is_undefined(lval
) && is_null(rval
))) {
2331 if(is_string(lval
) && is_number(rval
)) {
2335 hres
= to_number(ctx
, lval
, &n
);
2339 /* FIXME: optimize */
2340 return equal_values(ctx
, jsval_number(n
), rval
, ret
);
2343 if(is_string(rval
) && is_number(lval
)) {
2347 hres
= to_number(ctx
, rval
, &n
);
2351 /* FIXME: optimize */
2352 return equal_values(ctx
, lval
, jsval_number(n
), ret
);
2356 return equal_values(ctx
, lval
, jsval_number(get_bool(rval
) ? 1 : 0), ret
);
2359 return equal_values(ctx
, jsval_number(get_bool(lval
) ? 1 : 0), rval
, ret
);
2362 if(is_object_instance(rval
) && (is_string(lval
) || is_number(lval
))) {
2366 hres
= to_primitive(ctx
, rval
, &prim
, NO_HINT
);
2370 hres
= equal_values(ctx
, lval
, prim
, ret
);
2371 jsval_release(prim
);
2376 if(is_object_instance(lval
) && (is_string(rval
) || is_number(rval
))) {
2380 hres
= to_primitive(ctx
, lval
, &prim
, NO_HINT
);
2384 hres
= equal_values(ctx
, prim
, rval
, ret
);
2385 jsval_release(prim
);
2394 /* ECMA-262 3rd Edition 11.9.1 */
2395 static HRESULT
interp_eq(script_ctx_t
*ctx
)
2404 TRACE("%s == %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2406 hres
= equal_values(ctx
, l
, r
, &b
);
2412 return stack_push(ctx
, jsval_bool(b
));
2415 /* ECMA-262 3rd Edition 11.9.2 */
2416 static HRESULT
interp_neq(script_ctx_t
*ctx
)
2425 TRACE("%s != %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2427 hres
= equal_values(ctx
, l
, r
, &b
);
2433 return stack_push(ctx
, jsval_bool(!b
));
2436 /* ECMA-262 3rd Edition 11.9.4 */
2437 static HRESULT
interp_eq2(script_ctx_t
*ctx
)
2446 TRACE("%s === %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2448 hres
= jsval_strict_equal(r
, l
, &b
);
2454 return stack_push(ctx
, jsval_bool(b
));
2457 /* ECMA-262 3rd Edition 11.9.5 */
2458 static HRESULT
interp_neq2(script_ctx_t
*ctx
)
2469 hres
= jsval_strict_equal(r
, l
, &b
);
2475 return stack_push(ctx
, jsval_bool(!b
));
2478 /* ECMA-262 3rd Edition 11.8.5 */
2479 static HRESULT
less_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL greater
, BOOL
*ret
)
2485 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
2489 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
2495 if(is_string(l
) && is_string(r
)) {
2496 *ret
= (jsstr_cmp(get_string(l
), get_string(r
)) < 0) ^ greater
;
2497 jsstr_release(get_string(l
));
2498 jsstr_release(get_string(r
));
2502 hres
= to_number(ctx
, l
, &ln
);
2505 hres
= to_number(ctx
, r
, &rn
);
2510 *ret
= !isnan(ln
) && !isnan(rn
) && ((ln
< rn
) ^ greater
);
2514 /* ECMA-262 3rd Edition 11.8.1 */
2515 static HRESULT
interp_lt(script_ctx_t
*ctx
)
2524 TRACE("%s < %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2526 hres
= less_eval(ctx
, l
, r
, FALSE
, &b
);
2532 return stack_push(ctx
, jsval_bool(b
));
2535 /* ECMA-262 3rd Edition 11.8.1 */
2536 static HRESULT
interp_lteq(script_ctx_t
*ctx
)
2545 TRACE("%s <= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2547 hres
= less_eval(ctx
, r
, l
, TRUE
, &b
);
2553 return stack_push(ctx
, jsval_bool(b
));
2556 /* ECMA-262 3rd Edition 11.8.2 */
2557 static HRESULT
interp_gt(script_ctx_t
*ctx
)
2566 TRACE("%s > %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2568 hres
= less_eval(ctx
, r
, l
, FALSE
, &b
);
2574 return stack_push(ctx
, jsval_bool(b
));
2577 /* ECMA-262 3rd Edition 11.8.4 */
2578 static HRESULT
interp_gteq(script_ctx_t
*ctx
)
2587 TRACE("%s >= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2589 hres
= less_eval(ctx
, l
, r
, TRUE
, &b
);
2595 return stack_push(ctx
, jsval_bool(b
));
2598 /* ECMA-262 3rd Edition 11.4.8 */
2599 static HRESULT
interp_bneg(script_ctx_t
*ctx
)
2608 hres
= to_int32(ctx
, v
, &i
);
2613 return stack_push(ctx
, jsval_number(~i
));
2616 /* ECMA-262 3rd Edition 11.4.9 */
2617 static HRESULT
interp_neg(script_ctx_t
*ctx
)
2626 hres
= to_boolean(v
, &b
);
2631 return stack_push(ctx
, jsval_bool(!b
));
2634 /* ECMA-262 3rd Edition 11.7.1 */
2635 static HRESULT
interp_lshift(script_ctx_t
*ctx
)
2641 hres
= stack_pop_uint(ctx
, &r
);
2645 hres
= stack_pop_int(ctx
, &l
);
2649 return stack_push(ctx
, jsval_number(l
<< (r
&0x1f)));
2652 /* ECMA-262 3rd Edition 11.7.2 */
2653 static HRESULT
interp_rshift(script_ctx_t
*ctx
)
2659 hres
= stack_pop_uint(ctx
, &r
);
2663 hres
= stack_pop_int(ctx
, &l
);
2667 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2670 /* ECMA-262 3rd Edition 11.7.3 */
2671 static HRESULT
interp_rshift2(script_ctx_t
*ctx
)
2676 hres
= stack_pop_uint(ctx
, &r
);
2680 hres
= stack_pop_uint(ctx
, &l
);
2684 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2687 /* ECMA-262 3rd Edition 9.8 */
2688 static HRESULT
interp_to_string(script_ctx_t
*ctx
)
2695 TRACE("%s\n", debugstr_jsval(v
));
2696 hres
= to_string(ctx
, v
, &str
);
2699 WARN("failed %08lx\n", hres
);
2703 return stack_push(ctx
, jsval_string(str
));
2706 /* ECMA-262 3rd Edition 11.13.1 */
2707 static HRESULT
interp_assign(script_ctx_t
*ctx
)
2717 if(!stack_pop_exprval(ctx
, &ref
)) {
2719 return JS_E_ILLEGAL_ASSIGN
;
2722 hres
= exprval_propput(ctx
, &ref
, v
);
2723 exprval_release(&ref
);
2729 return stack_push(ctx
, v
);
2732 /* ECMA-262 3rd Edition 11.13.1 */
2733 static HRESULT
interp_set_member(script_ctx_t
*ctx
)
2735 jsval_t objv
, namev
, value
;
2740 value
= stack_pop(ctx
);
2741 namev
= stack_pop(ctx
);
2742 assert(is_string(namev
));
2743 objv
= stack_pop(ctx
);
2745 TRACE("%s.%s = %s\n", debugstr_jsval(objv
), debugstr_jsval(namev
), debugstr_jsval(value
));
2747 hres
= to_object(ctx
, objv
, &obj
);
2748 jsval_release(objv
);
2749 if(SUCCEEDED(hres
) && !(name
= jsstr_flatten(get_string(namev
)))) {
2750 IDispatch_Release(obj
);
2751 hres
= E_OUTOFMEMORY
;
2753 if(SUCCEEDED(hres
)) {
2754 hres
= disp_propput_name(ctx
, obj
, name
, value
);
2755 IDispatch_Release(obj
);
2756 jsstr_release(get_string(namev
));
2759 WARN("failed %08lx\n", hres
);
2760 jsval_release(value
);
2764 return stack_push(ctx
, value
);
2767 /* JScript extension */
2768 static HRESULT
interp_assign_call(script_ctx_t
*ctx
)
2770 const unsigned argc
= get_op_uint(ctx
, 0);
2775 TRACE("%u\n", argc
);
2777 if(!stack_topn_exprval(ctx
, argc
+1, &ref
))
2778 return JS_E_ILLEGAL_ASSIGN
;
2780 hres
= exprval_call(ctx
, &ref
, DISPATCH_PROPERTYPUT
, argc
+1, stack_args(ctx
, argc
+1), NULL
);
2785 stack_popn(ctx
, argc
+2);
2786 return stack_push(ctx
, v
);
2789 static HRESULT
interp_undefined(script_ctx_t
*ctx
)
2793 return stack_push(ctx
, jsval_undefined());
2796 static HRESULT
interp_jmp(script_ctx_t
*ctx
)
2798 const unsigned arg
= get_op_uint(ctx
, 0);
2806 static HRESULT
interp_jmp_z(script_ctx_t
*ctx
)
2808 const unsigned arg
= get_op_uint(ctx
, 0);
2816 hres
= to_boolean(v
, &b
);
2828 static HRESULT
interp_pop(script_ctx_t
*ctx
)
2830 const unsigned arg
= get_op_uint(ctx
, 0);
2834 stack_popn(ctx
, arg
);
2838 static HRESULT
interp_ret(script_ctx_t
*ctx
)
2840 const unsigned clear_ret
= get_op_uint(ctx
, 0);
2841 call_frame_t
*frame
= ctx
->call_ctx
;
2846 jsval_release(steal_ret(frame
));
2848 if((frame
->flags
& EXEC_CONSTRUCTOR
) && !is_object_instance(frame
->ret
)) {
2849 jsval_release(frame
->ret
);
2850 IDispatch_AddRef(frame
->this_obj
);
2851 frame
->ret
= jsval_disp(frame
->this_obj
);
2858 static HRESULT
interp_setret(script_ctx_t
*ctx
)
2860 call_frame_t
*frame
= ctx
->call_ctx
;
2864 jsval_release(frame
->ret
);
2865 frame
->ret
= stack_pop(ctx
);
2869 static HRESULT
interp_push_acc(script_ctx_t
*ctx
)
2875 hres
= stack_push(ctx
, ctx
->acc
);
2877 ctx
->acc
= jsval_undefined();
2881 typedef HRESULT (*op_func_t
)(script_ctx_t
*);
2883 static const op_func_t op_funcs
[] = {
2884 #define X(x,a,b,c) interp_##x,
2889 static const unsigned op_move
[] = {
2890 #define X(a,x,b,c) x,
2895 static void pop_call_frame(script_ctx_t
*ctx
)
2897 call_frame_t
*frame
= ctx
->call_ctx
;
2899 frame
->stack_base
-= frame
->pop_locals
+ frame
->pop_variables
;
2901 assert(frame
->scope
== frame
->base_scope
);
2903 /* If current scope will be kept alive, we need to transfer local variables to its variable object. */
2904 if(frame
->scope
&& frame
->scope
->ref
> 1) {
2905 HRESULT hres
= detach_variable_object(ctx
, frame
, TRUE
);
2907 ERR("Failed to detach variable object: %08lx\n", hres
);
2910 if(frame
->arguments_obj
)
2911 detach_arguments_object(frame
->arguments_obj
);
2913 scope_release(frame
->scope
);
2915 if(frame
->pop_variables
)
2916 stack_popn(ctx
, frame
->pop_variables
);
2917 stack_popn(ctx
, frame
->pop_locals
);
2919 ctx
->call_ctx
= frame
->prev_frame
;
2921 if(frame
->function_instance
)
2922 jsdisp_release(frame
->function_instance
);
2923 if(frame
->variable_obj
)
2924 jsdisp_release(frame
->variable_obj
);
2926 IDispatch_Release(frame
->this_obj
);
2927 jsval_release(frame
->ret
);
2928 release_bytecode(frame
->bytecode
);
2932 static void print_backtrace(script_ctx_t
*ctx
)
2934 unsigned depth
= 0, i
, line
, char_pos
;
2935 call_frame_t
*frame
;
2937 for(frame
= ctx
->call_ctx
; frame
; frame
= frame
->prev_frame
) {
2938 WARN("%u\t", depth
);
2942 WARN("%p->", frame
->this_obj
);
2943 WARN("%s(", frame
->function
->name
? debugstr_w(frame
->function
->name
) : "[unnamed]");
2944 if(frame
->base_scope
&& frame
->base_scope
->frame
) {
2945 for(i
=0; i
< frame
->argc
; i
++) {
2946 if(i
< frame
->function
->param_cnt
)
2947 WARN("%s%s=%s", i
? ", " : "", debugstr_w(frame
->function
->params
[i
]),
2948 debugstr_jsval(ctx
->stack
[local_off(frame
, -i
-1)]));
2950 WARN("%s%s", i
? ", " : "", debugstr_jsval(ctx
->stack
[local_off(frame
, -i
-1)]));
2953 WARN("[detached frame]");
2955 line
= get_location_line(frame
->bytecode
, frame
->bytecode
->instrs
[frame
->ip
].loc
, &char_pos
);
2956 WARN(") context %s line %u char %u\n", wine_dbgstr_longlong(frame
->bytecode
->source_context
), line
, char_pos
);
2958 if(!(frame
->flags
& EXEC_RETURN_TO_INTERP
)) {
2959 WARN("%u\t[native code]\n", depth
);
2965 static HRESULT
unwind_exception(script_ctx_t
*ctx
, HRESULT exception_hres
)
2967 except_frame_t
*except_frame
;
2968 jsexcept_t
*ei
= ctx
->ei
;
2969 call_frame_t
*frame
;
2974 if(WARN_ON(jscript
)) {
2975 jsdisp_t
*error_obj
;
2978 WARN("Exception %08lx %s", exception_hres
, debugstr_jsval(ei
->valid_value
? ei
->value
: jsval_undefined()));
2979 if(ei
->valid_value
&& jsval_type(ei
->value
) == JSV_OBJECT
) {
2980 error_obj
= to_jsdisp(get_object(ei
->value
));
2982 hres
= jsdisp_propget_name(error_obj
, L
"message", &msg
);
2983 if(SUCCEEDED(hres
)) {
2984 WARN(" (message %s)", debugstr_jsval(msg
));
2991 print_backtrace(ctx
);
2994 frame
= ctx
->call_ctx
;
2995 if(exception_hres
!= DISP_E_EXCEPTION
)
2996 throw_error(ctx
, exception_hres
, NULL
);
2997 set_error_location(ei
, frame
->bytecode
, frame
->bytecode
->instrs
[frame
->ip
].loc
, IDS_RUNTIME_ERROR
, NULL
);
2999 while(!frame
->except_frame
) {
3002 while(frame
->scope
!= frame
->base_scope
)
3003 scope_pop(&frame
->scope
);
3005 stack_popn(ctx
, ctx
->stack_top
-frame
->stack_base
);
3007 flags
= frame
->flags
;
3008 pop_call_frame(ctx
);
3009 if(!(flags
& EXEC_RETURN_TO_INTERP
))
3010 return DISP_E_EXCEPTION
;
3011 frame
= ctx
->call_ctx
;
3014 except_frame
= frame
->except_frame
;
3015 catch_off
= except_frame
->catch_off
;
3017 assert(except_frame
->stack_top
<= ctx
->stack_top
);
3018 stack_popn(ctx
, ctx
->stack_top
- except_frame
->stack_top
);
3020 while(except_frame
->scope
!= frame
->scope
)
3021 scope_pop(&frame
->scope
);
3023 frame
->ip
= catch_off
? catch_off
: except_frame
->finally_off
;
3024 assert(!catch_off
|| frame
->bytecode
->instrs
[frame
->ip
].op
== OP_enter_catch
);
3026 if(ei
->valid_value
) {
3027 except_val
= ctx
->ei
->value
;
3028 ei
->valid_value
= FALSE
;
3031 if(!(err
= create_builtin_error(ctx
)))
3032 return E_OUTOFMEMORY
;
3033 except_val
= jsval_obj(err
);
3036 /* keep current except_frame if we're entering catch block with finally block associated */
3037 if(catch_off
&& except_frame
->finally_off
) {
3038 except_frame
->catch_off
= 0;
3040 frame
->except_frame
= except_frame
->next
;
3041 heap_free(except_frame
);
3044 hres
= stack_push(ctx
, except_val
);
3049 hres
= stack_push(ctx
, jsval_bool(FALSE
));
3053 static HRESULT
enter_bytecode(script_ctx_t
*ctx
, jsval_t
*r
)
3055 call_frame_t
*frame
;
3057 HRESULT hres
= S_OK
;
3062 frame
= ctx
->call_ctx
;
3063 op
= frame
->bytecode
->instrs
[frame
->ip
].op
;
3064 hres
= op_funcs
[op
](ctx
);
3066 hres
= unwind_exception(ctx
, hres
);
3069 }else if(frame
->ip
== -1) {
3070 const DWORD return_to_interp
= frame
->flags
& EXEC_RETURN_TO_INTERP
;
3072 assert(ctx
->stack_top
== frame
->stack_base
);
3073 assert(frame
->scope
== frame
->base_scope
);
3075 if(return_to_interp
) {
3076 jsval_release(ctx
->acc
);
3077 ctx
->acc
= steal_ret(frame
);
3079 *r
= steal_ret(frame
);
3081 pop_call_frame(ctx
);
3082 if(!return_to_interp
)
3085 frame
->ip
+= op_move
[op
];
3092 static HRESULT
bind_event_target(script_ctx_t
*ctx
, function_code_t
*func
, jsdisp_t
*func_obj
)
3094 IBindEventHandler
*target
;
3100 hres
= identifier_eval(ctx
, func
->event_target
, &exprval
);
3104 hres
= exprval_to_value(ctx
, &exprval
, &v
);
3108 if(!is_object_instance(v
)) {
3109 FIXME("Can't bind to %s\n", debugstr_jsval(v
));
3113 disp
= get_object(v
);
3114 hres
= IDispatch_QueryInterface(disp
, &IID_IBindEventHandler
, (void**)&target
);
3115 if(SUCCEEDED(hres
)) {
3116 hres
= IBindEventHandler_BindHandler(target
, func
->name
, (IDispatch
*)&func_obj
->IDispatchEx_iface
);
3117 IBindEventHandler_Release(target
);
3119 WARN("BindEvent failed: %08lx\n", hres
);
3121 FIXME("No IBindEventHandler, not yet supported binding\n");
3124 IDispatch_Release(disp
);
3128 static HRESULT
setup_scope(script_ctx_t
*ctx
, call_frame_t
*frame
, scope_chain_t
*scope_chain
, jsdisp_t
*variable_object
, unsigned argc
, jsval_t
*argv
)
3130 const unsigned orig_stack
= ctx
->stack_top
;
3131 scope_chain_t
*scope
;
3136 /* If arguments are already on the stack, we may use them. */
3137 if(argv
+ argc
== ctx
->stack
+ ctx
->stack_top
) {
3138 frame
->arguments_off
= argv
- ctx
->stack
;
3141 frame
->arguments_off
= ctx
->stack_top
;
3142 for(i
= 0; i
< argc
; i
++) {
3143 hres
= jsval_copy(argv
[i
], &v
);
3145 hres
= stack_push(ctx
, v
);
3153 /* If fewer than declared arguments were passed, fill remaining with undefined value. */
3154 for(; i
< frame
->function
->param_cnt
; i
++) {
3155 hres
= stack_push(ctx
, jsval_undefined());
3157 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
3162 frame
->pop_locals
= ctx
->stack_top
- orig_stack
;
3164 frame
->variables_off
= ctx
->stack_top
;
3166 for(i
= 0; i
< frame
->function
->var_cnt
; i
++) {
3167 hres
= stack_push(ctx
, jsval_undefined());
3169 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
3174 frame
->pop_variables
= i
;
3176 hres
= scope_push(scope_chain
, variable_object
, to_disp(variable_object
), &scope
);
3178 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
3182 for(i
= 0; i
< frame
->function
->func_cnt
; i
++) {
3183 if(frame
->function
->funcs
[i
].local_ref
!= INVALID_LOCAL_REF
3184 && !frame
->function
->funcs
[i
].scope_index
)
3189 hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
+i
, scope
, &func_obj
);
3191 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
3192 scope_release(scope
);
3196 off
= local_off(frame
, frame
->function
->funcs
[i
].local_ref
);
3197 jsval_release(ctx
->stack
[off
]);
3198 ctx
->stack
[off
] = jsval_obj(func_obj
);
3202 scope
->frame
= frame
;
3203 frame
->base_scope
= frame
->scope
= scope
;
3207 HRESULT
exec_source(script_ctx_t
*ctx
, DWORD flags
, bytecode_t
*bytecode
, function_code_t
*function
, scope_chain_t
*scope
,
3208 IDispatch
*this_obj
, jsdisp_t
*function_instance
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
3210 jsdisp_t
*variable_obj
;
3211 call_frame_t
*frame
;
3216 ctx
->stack
= heap_alloc(stack_size
* sizeof(*ctx
->stack
));
3218 return E_OUTOFMEMORY
;
3221 if(bytecode
->named_item
) {
3222 if(!bytecode
->named_item
->script_obj
) {
3223 hres
= create_named_item_script_obj(ctx
, bytecode
->named_item
);
3224 if(FAILED(hres
)) return hres
;
3228 if(!ctx
->ei
->enter_notified
) {
3229 ctx
->ei
->enter_notified
= TRUE
;
3230 IActiveScriptSite_OnEnterScript(ctx
->site
);
3233 for(i
= 0; i
< function
->func_cnt
; i
++) {
3236 if(!function
->funcs
[i
].event_target
)
3239 if (function
->funcs
[i
].scope_index
)
3241 /* TODO: Add tests and handle in interp_push_scope(). */
3242 FIXME("Event target with scope index are not properly handled.\n");
3245 hres
= create_source_function(ctx
, bytecode
, function
->funcs
+i
, scope
, &func_obj
);
3249 hres
= bind_event_target(ctx
, function
->funcs
+i
, func_obj
);
3250 jsdisp_release(func_obj
);
3255 if((flags
& EXEC_EVAL
) && ctx
->call_ctx
) {
3256 variable_obj
= jsdisp_addref(ctx
->call_ctx
->variable_obj
);
3257 }else if(!(flags
& (EXEC_GLOBAL
| EXEC_EVAL
))) {
3258 hres
= create_dispex(ctx
, NULL
, NULL
, &variable_obj
);
3259 if(FAILED(hres
)) return hres
;
3260 }else if(bytecode
->named_item
) {
3261 variable_obj
= jsdisp_addref(bytecode
->named_item
->script_obj
);
3263 variable_obj
= jsdisp_addref(ctx
->global
);
3266 if(flags
& (EXEC_GLOBAL
| EXEC_EVAL
)) {
3267 named_item_t
*item
= bytecode
->named_item
;
3270 for(i
=0; i
< function
->var_cnt
; i
++) {
3271 TRACE("[%d] %s %d\n", i
, debugstr_w(function
->variables
[i
].name
), function
->variables
[i
].func_id
);
3272 if(function
->variables
[i
].func_id
!= -1) {
3275 if (function
->funcs
[function
->variables
[i
].func_id
].scope_index
&& flags
& EXEC_EVAL
)
3277 /* TODO: Add tests and handle in interp_push_scope(). */
3278 FIXME("Functions with scope index inside eval() are not properly handled.\n");
3281 hres
= create_source_function(ctx
, bytecode
, function
->funcs
+function
->variables
[i
].func_id
, scope
, &func_obj
);
3285 hres
= jsdisp_propput_name(variable_obj
, function
->variables
[i
].name
, jsval_obj(func_obj
));
3286 jsdisp_release(func_obj
);
3290 if(item
&& !(item
->flags
& SCRIPTITEM_CODEONLY
)
3291 && SUCCEEDED(disp_get_id(ctx
, item
->disp
, function
->variables
[i
].name
, function
->variables
[i
].name
, 0, &id
)))
3294 if(!item
&& (flags
& EXEC_GLOBAL
) && lookup_global_members(ctx
, function
->variables
[i
].name
, NULL
))
3297 hres
= jsdisp_get_id(variable_obj
, function
->variables
[i
].name
, fdexNameEnsure
, &id
);
3303 /* ECMA-262 3rd Edition 11.2.3.7 */
3307 jsthis
= iface_to_jsdisp(this_obj
);
3309 if(jsthis
->builtin_info
->class == JSCLASS_GLOBAL
|| jsthis
->builtin_info
->class == JSCLASS_NONE
)
3311 jsdisp_release(jsthis
);
3315 if(ctx
->call_ctx
&& (flags
& EXEC_EVAL
)) {
3316 hres
= detach_variable_object(ctx
, ctx
->call_ctx
, FALSE
);
3321 frame
= heap_alloc_zero(sizeof(*frame
));
3323 hres
= E_OUTOFMEMORY
;
3327 frame
->function
= function
;
3328 frame
->ret
= jsval_undefined();
3330 frame
->bytecode
= bytecode_addref(bytecode
);
3332 if(!(flags
& (EXEC_GLOBAL
|EXEC_EVAL
))) {
3333 hres
= setup_scope(ctx
, frame
, scope
, variable_obj
, argc
, argv
);
3335 release_bytecode(frame
->bytecode
);
3340 frame
->base_scope
= frame
->scope
= scope_addref(scope
);
3343 frame
->ip
= function
->instr_off
;
3344 frame
->stack_base
= ctx
->stack_top
;
3346 frame
->this_obj
= this_obj
;
3347 IDispatch_AddRef(frame
->this_obj
);
3350 if(function_instance
)
3351 frame
->function_instance
= jsdisp_addref(function_instance
);
3353 frame
->flags
= flags
;
3354 frame
->variable_obj
= variable_obj
;
3356 frame
->prev_frame
= ctx
->call_ctx
;
3357 ctx
->call_ctx
= frame
;
3359 if(flags
& EXEC_RETURN_TO_INTERP
) {
3361 * We're called directly from interpreter, so we may just setup call frame and return.
3362 * Already running interpreter will take care of execution.
3365 *r
= jsval_undefined();
3369 return enter_bytecode(ctx
, r
);
3372 jsdisp_release(variable_obj
);