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
21 static const WCHAR booleanW
[] = {'b','o','o','l','e','a','n',0};
22 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
23 static const WCHAR numberW
[] = {'n','u','m','b','e','r',0};
24 static const WCHAR objectW
[] = {'o','b','j','e','c','t',0};
25 static const WCHAR stringW
[] = {'s','t','r','i','n','g',0};
26 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
27 static const WCHAR unknownW
[] = {'u','n','k','n','o','w','n',0};
29 struct _except_frame_t
{
56 static HRESULT
stack_push(script_ctx_t
*ctx
, jsval_t v
)
58 if(!ctx
->stack_size
) {
59 ctx
->stack
= heap_alloc(16*sizeof(*ctx
->stack
));
63 }else if(ctx
->stack_size
== ctx
->stack_top
) {
66 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*new_stack
));
72 ctx
->stack
= new_stack
;
76 ctx
->stack
[ctx
->stack_top
++] = v
;
80 static inline HRESULT
stack_push_string(script_ctx_t
*ctx
, const WCHAR
*str
)
88 return stack_push(ctx
, jsval_string(v
));
91 static inline jsval_t
stack_top(script_ctx_t
*ctx
)
93 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
);
94 return ctx
->stack
[ctx
->stack_top
-1];
97 static inline jsval_t
*stack_top_ref(script_ctx_t
*ctx
, unsigned n
)
99 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
+n
);
100 return ctx
->stack
+ctx
->stack_top
-1-n
;
103 static inline jsval_t
stack_topn(script_ctx_t
*ctx
, unsigned n
)
105 return *stack_top_ref(ctx
, n
);
108 static inline jsval_t
*stack_args(script_ctx_t
*ctx
, unsigned n
)
112 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
+n
-1);
113 return ctx
->stack
+ ctx
->stack_top
-n
;
116 static inline jsval_t
stack_pop(script_ctx_t
*ctx
)
118 assert(ctx
->stack_top
> ctx
->call_ctx
->stack_base
);
119 return ctx
->stack
[--ctx
->stack_top
];
122 static void stack_popn(script_ctx_t
*ctx
, unsigned n
)
125 jsval_release(stack_pop(ctx
));
128 static HRESULT
stack_pop_number(script_ctx_t
*ctx
, double *r
)
134 hres
= to_number(ctx
, v
, r
);
139 static HRESULT
stack_pop_object(script_ctx_t
*ctx
, IDispatch
**r
)
145 if(is_object_instance(v
)) {
147 return throw_type_error(ctx
, JS_E_OBJECT_REQUIRED
, NULL
);
152 hres
= to_object(ctx
, v
, r
);
157 static inline HRESULT
stack_pop_int(script_ctx_t
*ctx
, INT
*r
)
159 return to_int32(ctx
, stack_pop(ctx
), r
);
162 static inline HRESULT
stack_pop_uint(script_ctx_t
*ctx
, DWORD
*r
)
164 return to_uint32(ctx
, stack_pop(ctx
), r
);
167 static inline unsigned local_off(call_frame_t
*frame
, int ref
)
170 ? frame
->arguments_off
- ref
-1
171 : frame
->variables_off
+ ref
;
174 static inline BSTR
local_name(call_frame_t
*frame
, int ref
)
176 return ref
< 0 ? frame
->function
->params
[-ref
-1] : frame
->function
->variables
[ref
].name
;
179 /* Steals input reference even on failure. */
180 static HRESULT
stack_push_exprval(script_ctx_t
*ctx
, exprval_t
*val
)
188 hres
= stack_push(ctx
, jsval_disp(val
->u
.idref
.disp
));
190 hres
= stack_push(ctx
, jsval_number(val
->u
.idref
.id
));
192 IDispatch_Release(val
->u
.idref
.disp
);
194 case EXPRVAL_STACK_REF
:
195 hres
= stack_push(ctx
, jsval_number(val
->u
.off
));
197 hres
= stack_push(ctx
, jsval_undefined());
199 case EXPRVAL_INVALID
:
200 hres
= stack_push(ctx
, jsval_undefined());
202 hres
= stack_push(ctx
, jsval_number(val
->u
.hres
));
210 static BOOL
stack_topn_exprval(script_ctx_t
*ctx
, unsigned n
, exprval_t
*r
)
212 jsval_t v
= stack_topn(ctx
, n
+1);
214 switch(jsval_type(v
)) {
216 call_frame_t
*frame
= ctx
->call_ctx
;
217 unsigned off
= get_number(v
);
219 if(!frame
->base_scope
->frame
&& off
>= frame
->arguments_off
) {
224 /* Got stack reference in deoptimized code. Need to convert it back to variable object reference. */
226 assert(off
< frame
->variables_off
+ frame
->function
->var_cnt
);
227 name
= off
>= frame
->variables_off
228 ? frame
->function
->variables
[off
- frame
->variables_off
].name
229 : frame
->function
->params
[off
- frame
->arguments_off
];
230 hres
= jsdisp_get_id(ctx
->call_ctx
->base_scope
->jsobj
, name
, 0, &id
);
232 r
->type
= EXPRVAL_INVALID
;
237 *stack_top_ref(ctx
, n
+1) = jsval_obj(jsdisp_addref(frame
->base_scope
->jsobj
));
238 *stack_top_ref(ctx
, n
) = jsval_number(id
);
239 r
->type
= EXPRVAL_IDREF
;
240 r
->u
.idref
.disp
= frame
->base_scope
->obj
;
245 r
->type
= EXPRVAL_STACK_REF
;
250 r
->type
= EXPRVAL_IDREF
;
251 r
->u
.idref
.disp
= get_object(v
);
252 assert(is_number(stack_topn(ctx
, n
)));
253 r
->u
.idref
.id
= get_number(stack_topn(ctx
, n
));
256 r
->type
= EXPRVAL_INVALID
;
257 assert(is_number(stack_topn(ctx
, n
)));
258 r
->u
.hres
= get_number(stack_topn(ctx
, n
));
266 static inline BOOL
stack_pop_exprval(script_ctx_t
*ctx
, exprval_t
*r
)
268 BOOL ret
= stack_topn_exprval(ctx
, 0, r
);
273 static HRESULT
exprval_propput(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t v
)
276 case EXPRVAL_STACK_REF
: {
277 jsval_t
*r
= ctx
->stack
+ ref
->u
.off
;
279 return jsval_copy(v
, r
);
282 return disp_propput(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, v
);
289 static HRESULT
exprval_propget(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t
*r
)
292 case EXPRVAL_STACK_REF
:
293 return jsval_copy(ctx
->stack
[ref
->u
.off
], r
);
295 return disp_propget(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, r
);
302 static HRESULT
exprval_call(script_ctx_t
*ctx
, exprval_t
*ref
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
305 case EXPRVAL_STACK_REF
: {
306 jsval_t v
= ctx
->stack
[ref
->u
.off
];
308 if(!is_object_instance(v
)) {
309 FIXME("invoke %s\n", debugstr_jsval(v
));
313 return disp_call_value(ctx
, get_object(v
), NULL
, flags
, argc
, argv
, r
);
316 return disp_call(ctx
, ref
->u
.idref
.disp
, ref
->u
.idref
.id
, flags
, argc
, argv
, r
);
323 /* ECMA-262 3rd Edition 8.7.1 */
324 /* Steals input reference. */
325 static HRESULT
exprval_to_value(script_ctx_t
*ctx
, exprval_t
*ref
, jsval_t
*r
)
329 if(ref
->type
== EXPRVAL_JSVAL
) {
334 hres
= exprval_propget(ctx
, ref
, r
);
336 if(ref
->type
== EXPRVAL_IDREF
)
337 IDispatch_Release(ref
->u
.idref
.disp
);
341 static void exprval_release(exprval_t
*val
)
345 jsval_release(val
->u
.val
);
348 if(val
->u
.idref
.disp
)
349 IDispatch_Release(val
->u
.idref
.disp
);
351 case EXPRVAL_STACK_REF
:
352 case EXPRVAL_INVALID
:
357 static inline void exprval_set_exception(exprval_t
*val
, HRESULT hres
)
359 val
->type
= EXPRVAL_INVALID
;
363 static inline void exprval_set_disp_ref(exprval_t
*ref
, IDispatch
*obj
, DISPID id
)
365 ref
->type
= EXPRVAL_IDREF
;
366 #ifdef __REACTOS__ /* FIXME: Inspect */
367 IDispatch_AddRef(obj
);
368 ref
->u
.idref
.disp
= obj
;
370 IDispatch_AddRef(ref
->u
.idref
.disp
= obj
);
372 ref
->u
.idref
.id
= id
;
375 static inline jsval_t
steal_ret(call_frame_t
*frame
)
377 jsval_t r
= frame
->ret
;
378 frame
->ret
= jsval_undefined();
382 static inline void clear_ret(call_frame_t
*frame
)
384 jsval_release(steal_ret(frame
));
387 HRESULT
scope_push(scope_chain_t
*scope
, jsdisp_t
*jsobj
, IDispatch
*obj
, scope_chain_t
**ret
)
389 scope_chain_t
*new_scope
;
391 new_scope
= heap_alloc(sizeof(scope_chain_t
));
393 return E_OUTOFMEMORY
;
397 IDispatch_AddRef(obj
);
398 new_scope
->jsobj
= jsobj
;
399 new_scope
->obj
= obj
;
400 new_scope
->frame
= NULL
;
401 new_scope
->next
= scope
? scope_addref(scope
) : NULL
;
407 static void scope_pop(scope_chain_t
**scope
)
416 void clear_ei(script_ctx_t
*ctx
)
418 memset(&ctx
->ei
.ei
, 0, sizeof(ctx
->ei
.ei
));
419 jsval_release(ctx
->ei
.val
);
420 ctx
->ei
.val
= jsval_undefined();
423 void scope_release(scope_chain_t
*scope
)
429 scope_release(scope
->next
);
431 IDispatch_Release(scope
->obj
);
435 static HRESULT
disp_get_id(script_ctx_t
*ctx
, IDispatch
*disp
, const WCHAR
*name
, BSTR name_bstr
, DWORD flags
, DISPID
*id
)
442 jsdisp
= iface_to_jsdisp(disp
);
444 hres
= jsdisp_get_id(jsdisp
, name
, flags
, id
);
445 jsdisp_release(jsdisp
);
452 bstr
= SysAllocString(name
);
454 return E_OUTOFMEMORY
;
458 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
459 if(SUCCEEDED(hres
)) {
460 hres
= IDispatchEx_GetDispID(dispex
, bstr
, make_grfdex(ctx
, flags
|fdexNameCaseSensitive
), id
);
461 IDispatchEx_Release(dispex
);
463 TRACE("using IDispatch\n");
464 hres
= IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &bstr
, 1, 0, id
);
467 if(name_bstr
!= bstr
)
472 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, BOOL
*ret
)
474 IObjectIdentity
*identity
;
475 IUnknown
*unk1
, *unk2
;
483 if(!disp1
|| !disp2
) {
488 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
492 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
494 IUnknown_Release(unk1
);
501 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
502 if(SUCCEEDED(hres
)) {
503 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
504 IObjectIdentity_Release(identity
);
511 IUnknown_Release(unk1
);
512 IUnknown_Release(unk2
);
516 /* ECMA-262 3rd Edition 11.9.6 */
517 static HRESULT
equal2_values(jsval_t lval
, jsval_t rval
, BOOL
*ret
)
519 jsval_type_t type
= jsval_type(lval
);
523 if(type
!= jsval_type(rval
)) {
524 if(is_null_instance(lval
))
525 *ret
= is_null_instance(rval
);
537 return disp_cmp(get_object(lval
), get_object(rval
), ret
);
539 *ret
= jsstr_eq(get_string(lval
), get_string(rval
));
542 *ret
= get_number(lval
) == get_number(rval
);
545 *ret
= !get_bool(lval
) == !get_bool(rval
);
548 FIXME("VARIANT not implemented\n");
556 * Transfers local variables from stack to variable object.
557 * It's slow, so we want to avoid it as much as possible.
559 static HRESULT
detach_variable_object(script_ctx_t
*ctx
, call_frame_t
*frame
, BOOL from_release
)
564 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
567 TRACE("detaching %p\n", frame
);
569 assert(frame
== frame
->base_scope
->frame
);
570 assert(frame
->variable_obj
== frame
->base_scope
->jsobj
);
572 if(!from_release
&& !frame
->arguments_obj
) {
573 hres
= setup_arguments_object(ctx
, frame
);
578 frame
->base_scope
->frame
= NULL
;
580 for(i
= 0; i
< frame
->function
->locals_cnt
; i
++) {
581 hres
= jsdisp_propput_name(frame
->variable_obj
, frame
->function
->locals
[i
].name
,
582 ctx
->stack
[local_off(frame
, frame
->function
->locals
[i
].ref
)]);
590 static BOOL
lookup_global_members(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
596 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
597 if(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
) {
598 hres
= disp_get_id(ctx
, item
->disp
, identifier
, identifier
, 0, &id
);
599 if(SUCCEEDED(hres
)) {
601 exprval_set_disp_ref(ret
, item
->disp
, id
);
610 static int local_ref_cmp(const void *key
, const void *ref
)
612 return strcmpW((const WCHAR
*)key
, ((const local_ref_t
*)ref
)->name
);
615 local_ref_t
*lookup_local(const function_code_t
*function
, const WCHAR
*identifier
)
617 return bsearch(identifier
, function
->locals
, function
->locals_cnt
, sizeof(*function
->locals
), local_ref_cmp
);
620 /* ECMA-262 3rd Edition 10.1.4 */
621 static HRESULT
identifier_eval(script_ctx_t
*ctx
, BSTR identifier
, exprval_t
*ret
)
623 scope_chain_t
*scope
;
628 TRACE("%s\n", debugstr_w(identifier
));
631 for(scope
= ctx
->call_ctx
->scope
; scope
; scope
= scope
->next
) {
633 function_code_t
*func
= scope
->frame
->function
;
634 local_ref_t
*ref
= lookup_local(func
, identifier
);
635 static const WCHAR argumentsW
[] = {'a','r','g','u','m','e','n','t','s',0};
638 ret
->type
= EXPRVAL_STACK_REF
;
639 ret
->u
.off
= local_off(scope
->frame
, ref
->ref
);
640 TRACE("returning ref %d for %d\n", ret
->u
.off
, ref
->ref
);
644 if(!strcmpW(identifier
, argumentsW
)) {
645 hres
= detach_variable_object(ctx
, scope
->frame
, FALSE
);
651 hres
= jsdisp_get_id(scope
->jsobj
, identifier
, fdexNameImplicit
, &id
);
653 hres
= disp_get_id(ctx
, scope
->obj
, identifier
, identifier
, fdexNameImplicit
, &id
);
654 if(SUCCEEDED(hres
)) {
655 exprval_set_disp_ref(ret
, scope
->obj
, id
);
661 hres
= jsdisp_get_id(ctx
->global
, identifier
, 0, &id
);
662 if(SUCCEEDED(hres
)) {
663 exprval_set_disp_ref(ret
, to_disp(ctx
->global
), id
);
667 for(item
= ctx
->named_items
; item
; item
= item
->next
) {
668 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpW(item
->name
, identifier
)) {
675 hres
= IActiveScriptSite_GetItemInfo(ctx
->site
, identifier
,
676 SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
678 WARN("GetItemInfo failed: %08x\n", hres
);
682 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
683 IUnknown_Release(unk
);
685 WARN("object does not implement IDispatch\n");
690 IDispatch_AddRef(item
->disp
);
691 ret
->type
= EXPRVAL_JSVAL
;
692 ret
->u
.val
= jsval_disp(item
->disp
);
697 if(lookup_global_members(ctx
, identifier
, ret
))
700 exprval_set_exception(ret
, JS_E_UNDEFINED_VARIABLE
);
704 static inline BSTR
get_op_bstr(script_ctx_t
*ctx
, int i
)
706 call_frame_t
*frame
= ctx
->call_ctx
;
707 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].bstr
;
710 static inline unsigned get_op_uint(script_ctx_t
*ctx
, int i
)
712 call_frame_t
*frame
= ctx
->call_ctx
;
713 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].uint
;
716 static inline unsigned get_op_int(script_ctx_t
*ctx
, int i
)
718 call_frame_t
*frame
= ctx
->call_ctx
;
719 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].lng
;
722 static inline jsstr_t
*get_op_str(script_ctx_t
*ctx
, int i
)
724 call_frame_t
*frame
= ctx
->call_ctx
;
725 return frame
->bytecode
->instrs
[frame
->ip
].u
.arg
[i
].str
;
728 static inline double get_op_double(script_ctx_t
*ctx
)
730 call_frame_t
*frame
= ctx
->call_ctx
;
731 return frame
->bytecode
->instrs
[frame
->ip
].u
.dbl
;
734 static inline void jmp_next(script_ctx_t
*ctx
)
739 static inline void jmp_abs(script_ctx_t
*ctx
, unsigned dst
)
741 ctx
->call_ctx
->ip
= dst
;
744 /* ECMA-262 3rd Edition 12.6.4 */
745 static HRESULT
interp_forin(script_ctx_t
*ctx
)
747 const HRESULT arg
= get_op_uint(ctx
, 0);
748 IDispatch
*obj
= NULL
;
757 assert(is_number(stack_top(ctx
)));
758 id
= get_number(stack_top(ctx
));
760 if(!stack_topn_exprval(ctx
, 1, &prop_ref
)) {
761 FIXME("invalid ref: %08x\n", prop_ref
.u
.hres
);
765 if(is_object_instance(stack_topn(ctx
, 3)))
766 obj
= get_object(stack_topn(ctx
, 3));
769 hres
= IDispatch_QueryInterface(obj
, &IID_IDispatchEx
, (void**)&dispex
);
770 if(SUCCEEDED(hres
)) {
771 hres
= IDispatchEx_GetNextDispID(dispex
, fdexEnumDefault
, id
, &id
);
773 hres
= IDispatchEx_GetMemberName(dispex
, id
, &name
);
774 IDispatchEx_Release(dispex
);
778 TRACE("No IDispatchEx\n");
785 str
= jsstr_alloc_len(name
, SysStringLen(name
));
788 return E_OUTOFMEMORY
;
791 stack_push(ctx
, jsval_number(id
)); /* safe, just after pop() */
793 hres
= exprval_propput(ctx
, &prop_ref
, jsval_string(str
));
806 /* ECMA-262 3rd Edition 12.10 */
807 static HRESULT
interp_push_scope(script_ctx_t
*ctx
)
816 hres
= to_object(ctx
, v
, &disp
);
821 hres
= scope_push(ctx
->call_ctx
->scope
, to_jsdisp(disp
), disp
, &ctx
->call_ctx
->scope
);
822 IDispatch_Release(disp
);
826 /* ECMA-262 3rd Edition 12.10 */
827 static HRESULT
interp_pop_scope(script_ctx_t
*ctx
)
831 scope_pop(&ctx
->call_ctx
->scope
);
835 /* ECMA-262 3rd Edition 12.13 */
836 static HRESULT
interp_case(script_ctx_t
*ctx
)
838 const unsigned arg
= get_op_uint(ctx
, 0);
846 hres
= equal2_values(stack_top(ctx
), v
, &b
);
860 /* ECMA-262 3rd Edition 12.13 */
861 static HRESULT
interp_throw(script_ctx_t
*ctx
)
865 jsval_release(ctx
->ei
.val
);
866 ctx
->ei
.val
= stack_pop(ctx
);
867 return DISP_E_EXCEPTION
;
870 static HRESULT
interp_throw_ref(script_ctx_t
*ctx
)
872 const HRESULT arg
= get_op_uint(ctx
, 0);
874 TRACE("%08x\n", arg
);
876 return throw_reference_error(ctx
, arg
, NULL
);
879 static HRESULT
interp_throw_type(script_ctx_t
*ctx
)
881 const HRESULT hres
= get_op_uint(ctx
, 0);
882 jsstr_t
*str
= get_op_str(ctx
, 1);
885 TRACE("%08x %s\n", hres
, debugstr_jsstr(str
));
887 ptr
= jsstr_flatten(str
);
888 return ptr
? throw_type_error(ctx
, hres
, ptr
) : E_OUTOFMEMORY
;
891 /* ECMA-262 3rd Edition 12.14 */
892 static HRESULT
interp_push_except(script_ctx_t
*ctx
)
894 const unsigned arg1
= get_op_uint(ctx
, 0);
895 const BSTR arg2
= get_op_bstr(ctx
, 1);
896 call_frame_t
*frame
= ctx
->call_ctx
;
897 except_frame_t
*except
;
902 stack_top
= ctx
->stack_top
;
907 hres
= stack_push(ctx
, jsval_bool(TRUE
));
910 hres
= stack_push(ctx
, jsval_bool(TRUE
));
915 except
= heap_alloc(sizeof(*except
));
917 return E_OUTOFMEMORY
;
919 except
->stack_top
= stack_top
;
920 except
->scope
= frame
->scope
;
921 except
->catch_off
= arg1
;
922 except
->ident
= arg2
;
923 except
->next
= frame
->except_frame
;
924 frame
->except_frame
= except
;
928 /* ECMA-262 3rd Edition 12.14 */
929 static HRESULT
interp_pop_except(script_ctx_t
*ctx
)
931 call_frame_t
*frame
= ctx
->call_ctx
;
932 except_frame_t
*except
;
936 except
= frame
->except_frame
;
937 assert(except
!= NULL
);
939 frame
->except_frame
= except
->next
;
944 /* ECMA-262 3rd Edition 12.14 */
945 static HRESULT
interp_end_finally(script_ctx_t
*ctx
)
955 TRACE("passing exception\n");
957 ctx
->ei
.val
= stack_pop(ctx
);
958 return DISP_E_EXCEPTION
;
965 /* ECMA-262 3rd Edition 13 */
966 static HRESULT
interp_func(script_ctx_t
*ctx
)
968 unsigned func_idx
= get_op_uint(ctx
, 0);
969 call_frame_t
*frame
= ctx
->call_ctx
;
973 TRACE("%d\n", func_idx
);
975 hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
+func_idx
,
976 frame
->scope
, &dispex
);
980 return stack_push(ctx
, jsval_obj(dispex
));
983 /* ECMA-262 3rd Edition 11.2.1 */
984 static HRESULT
interp_array(script_ctx_t
*ctx
)
995 namev
= stack_pop(ctx
);
997 hres
= stack_pop_object(ctx
, &obj
);
999 jsval_release(namev
);
1003 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
1004 jsval_release(namev
);
1006 IDispatch_Release(obj
);
1010 hres
= disp_get_id(ctx
, obj
, name
, NULL
, 0, &id
);
1011 jsstr_release(name_str
);
1012 if(SUCCEEDED(hres
)) {
1013 hres
= disp_propget(ctx
, obj
, id
, &v
);
1014 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1015 v
= jsval_undefined();
1018 IDispatch_Release(obj
);
1022 return stack_push(ctx
, v
);
1025 /* ECMA-262 3rd Edition 11.2.1 */
1026 static HRESULT
interp_member(script_ctx_t
*ctx
)
1028 const BSTR arg
= get_op_bstr(ctx
, 0);
1036 hres
= stack_pop_object(ctx
, &obj
);
1040 hres
= disp_get_id(ctx
, obj
, arg
, arg
, 0, &id
);
1041 if(SUCCEEDED(hres
)) {
1042 hres
= disp_propget(ctx
, obj
, id
, &v
);
1043 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1044 v
= jsval_undefined();
1047 IDispatch_Release(obj
);
1051 return stack_push(ctx
, v
);
1054 /* ECMA-262 3rd Edition 11.2.1 */
1055 static HRESULT
interp_memberid(script_ctx_t
*ctx
)
1057 const unsigned arg
= get_op_uint(ctx
, 0);
1058 jsval_t objv
, namev
;
1068 namev
= stack_pop(ctx
);
1069 objv
= stack_pop(ctx
);
1071 hres
= to_object(ctx
, objv
, &obj
);
1072 jsval_release(objv
);
1073 if(SUCCEEDED(hres
)) {
1074 hres
= to_flat_string(ctx
, namev
, &name_str
, &name
);
1076 IDispatch_Release(obj
);
1078 jsval_release(namev
);
1082 hres
= disp_get_id(ctx
, obj
, name
, NULL
, arg
, &id
);
1083 jsstr_release(name_str
);
1084 if(SUCCEEDED(hres
)) {
1085 ref
.type
= EXPRVAL_IDREF
;
1086 ref
.u
.idref
.disp
= obj
;
1087 ref
.u
.idref
.id
= id
;
1089 IDispatch_Release(obj
);
1090 if(hres
== DISP_E_UNKNOWNNAME
&& !(arg
& fdexNameEnsure
)) {
1091 exprval_set_exception(&ref
, JS_E_INVALID_PROPERTY
);
1094 ERR("failed %08x\n", hres
);
1099 return stack_push_exprval(ctx
, &ref
);
1102 /* ECMA-262 3rd Edition 11.2.1 */
1103 static HRESULT
interp_refval(script_ctx_t
*ctx
)
1111 if(!stack_topn_exprval(ctx
, 0, &ref
))
1112 return throw_reference_error(ctx
, JS_E_ILLEGAL_ASSIGN
, NULL
);
1114 hres
= exprval_propget(ctx
, &ref
, &v
);
1118 return stack_push(ctx
, v
);
1121 /* ECMA-262 3rd Edition 11.2.2 */
1122 static HRESULT
interp_new(script_ctx_t
*ctx
)
1124 const unsigned argc
= get_op_uint(ctx
, 0);
1125 call_frame_t
*frame
= ctx
->call_ctx
;
1128 TRACE("%d\n", argc
);
1130 constr
= stack_topn(ctx
, argc
);
1132 /* NOTE: Should use to_object here */
1135 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
1136 else if(!is_object_instance(constr
))
1137 return throw_type_error(ctx
, JS_E_INVALID_ACTION
, NULL
);
1138 else if(!get_object(constr
))
1139 return throw_type_error(ctx
, JS_E_INVALID_PROPERTY
, NULL
);
1142 return disp_call_value(ctx
, get_object(constr
), NULL
, DISPATCH_CONSTRUCT
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1143 argc
, stack_args(ctx
, argc
), &frame
->ret
);
1146 /* ECMA-262 3rd Edition 11.2.3 */
1147 static HRESULT
interp_call(script_ctx_t
*ctx
)
1149 const unsigned argn
= get_op_uint(ctx
, 0);
1150 const int do_ret
= get_op_int(ctx
, 1);
1151 call_frame_t
*frame
= ctx
->call_ctx
;
1154 TRACE("%d %d\n", argn
, do_ret
);
1156 obj
= stack_topn(ctx
, argn
);
1157 if(!is_object_instance(obj
))
1158 return throw_type_error(ctx
, JS_E_INVALID_PROPERTY
, NULL
);
1161 return disp_call_value(ctx
, get_object(obj
), NULL
, DISPATCH_METHOD
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1162 argn
, stack_args(ctx
, argn
), do_ret
? &frame
->ret
: NULL
);
1165 /* ECMA-262 3rd Edition 11.2.3 */
1166 static HRESULT
interp_call_member(script_ctx_t
*ctx
)
1168 const unsigned argn
= get_op_uint(ctx
, 0);
1169 const int do_ret
= get_op_int(ctx
, 1);
1170 call_frame_t
*frame
= ctx
->call_ctx
;
1173 TRACE("%d %d\n", argn
, do_ret
);
1175 if(!stack_topn_exprval(ctx
, argn
, &ref
))
1176 return throw_type_error(ctx
, ref
.u
.hres
, NULL
);
1179 return exprval_call(ctx
, &ref
, DISPATCH_METHOD
| DISPATCH_JSCRIPT_CALLEREXECSSOURCE
,
1180 argn
, stack_args(ctx
, argn
), do_ret
? &frame
->ret
: NULL
);
1183 /* ECMA-262 3rd Edition 11.1.1 */
1184 static HRESULT
interp_this(script_ctx_t
*ctx
)
1186 call_frame_t
*frame
= ctx
->call_ctx
;
1190 IDispatch_AddRef(frame
->this_obj
);
1191 return stack_push(ctx
, jsval_disp(frame
->this_obj
));
1194 static HRESULT
interp_identifier_ref(script_ctx_t
*ctx
, BSTR identifier
, unsigned flags
)
1199 hres
= identifier_eval(ctx
, identifier
, &exprval
);
1203 if(exprval
.type
== EXPRVAL_INVALID
&& (flags
& fdexNameEnsure
)) {
1206 hres
= jsdisp_get_id(ctx
->global
, identifier
, fdexNameEnsure
, &id
);
1210 exprval_set_disp_ref(&exprval
, to_disp(ctx
->global
), id
);
1213 if(exprval
.type
== EXPRVAL_JSVAL
|| exprval
.type
== EXPRVAL_INVALID
) {
1214 WARN("invalid ref\n");
1215 exprval_release(&exprval
);
1216 exprval_set_exception(&exprval
, JS_E_OBJECT_EXPECTED
);
1219 return stack_push_exprval(ctx
, &exprval
);
1222 static HRESULT
identifier_value(script_ctx_t
*ctx
, BSTR identifier
)
1228 hres
= identifier_eval(ctx
, identifier
, &exprval
);
1232 if(exprval
.type
== EXPRVAL_INVALID
)
1233 return throw_type_error(ctx
, exprval
.u
.hres
, identifier
);
1235 hres
= exprval_to_value(ctx
, &exprval
, &v
);
1239 return stack_push(ctx
, v
);
1242 static HRESULT
interp_local_ref(script_ctx_t
*ctx
)
1244 const int arg
= get_op_int(ctx
, 0);
1245 const unsigned flags
= get_op_uint(ctx
, 1);
1246 call_frame_t
*frame
= ctx
->call_ctx
;
1251 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
1252 return interp_identifier_ref(ctx
, local_name(frame
, arg
), flags
);
1254 ref
.type
= EXPRVAL_STACK_REF
;
1255 ref
.u
.off
= local_off(frame
, arg
);
1256 return stack_push_exprval(ctx
, &ref
);
1259 static HRESULT
interp_local(script_ctx_t
*ctx
)
1261 const int arg
= get_op_int(ctx
, 0);
1262 call_frame_t
*frame
= ctx
->call_ctx
;
1268 if(!frame
->base_scope
|| !frame
->base_scope
->frame
)
1269 return identifier_value(ctx
, local_name(frame
, arg
));
1271 hres
= jsval_copy(ctx
->stack
[local_off(frame
, arg
)], ©
);
1275 return stack_push(ctx
, copy
);
1278 /* ECMA-262 3rd Edition 10.1.4 */
1279 static HRESULT
interp_ident(script_ctx_t
*ctx
)
1281 const BSTR arg
= get_op_bstr(ctx
, 0);
1283 TRACE("%s\n", debugstr_w(arg
));
1285 return identifier_value(ctx
, arg
);
1288 /* ECMA-262 3rd Edition 10.1.4 */
1289 static HRESULT
interp_identid(script_ctx_t
*ctx
)
1291 const BSTR arg
= get_op_bstr(ctx
, 0);
1292 const unsigned flags
= get_op_uint(ctx
, 1);
1294 TRACE("%s %x\n", debugstr_w(arg
), flags
);
1296 return interp_identifier_ref(ctx
, arg
, flags
);
1299 /* ECMA-262 3rd Edition 7.8.1 */
1300 static HRESULT
interp_null(script_ctx_t
*ctx
)
1304 return stack_push(ctx
, jsval_null());
1307 /* ECMA-262 3rd Edition 7.8.2 */
1308 static HRESULT
interp_bool(script_ctx_t
*ctx
)
1310 const int arg
= get_op_int(ctx
, 0);
1312 TRACE("%s\n", arg
? "true" : "false");
1314 return stack_push(ctx
, jsval_bool(arg
));
1317 /* ECMA-262 3rd Edition 7.8.3 */
1318 static HRESULT
interp_int(script_ctx_t
*ctx
)
1320 const int arg
= get_op_int(ctx
, 0);
1324 return stack_push(ctx
, jsval_number(arg
));
1327 /* ECMA-262 3rd Edition 7.8.3 */
1328 static HRESULT
interp_double(script_ctx_t
*ctx
)
1330 const double arg
= get_op_double(ctx
);
1332 TRACE("%lf\n", arg
);
1334 return stack_push(ctx
, jsval_number(arg
));
1337 /* ECMA-262 3rd Edition 7.8.4 */
1338 static HRESULT
interp_str(script_ctx_t
*ctx
)
1340 jsstr_t
*str
= get_op_str(ctx
, 0);
1342 TRACE("%s\n", debugstr_jsstr(str
));
1344 return stack_push(ctx
, jsval_string(jsstr_addref(str
)));
1347 /* ECMA-262 3rd Edition 7.8 */
1348 static HRESULT
interp_regexp(script_ctx_t
*ctx
)
1350 jsstr_t
*source
= get_op_str(ctx
, 0);
1351 const unsigned flags
= get_op_uint(ctx
, 1);
1355 TRACE("%s %x\n", debugstr_jsstr(source
), flags
);
1357 hres
= create_regexp(ctx
, source
, flags
, ®exp
);
1361 return stack_push(ctx
, jsval_obj(regexp
));
1364 /* ECMA-262 3rd Edition 11.1.4 */
1365 static HRESULT
interp_carray(script_ctx_t
*ctx
)
1367 const unsigned arg
= get_op_uint(ctx
, 0);
1375 hres
= create_array(ctx
, arg
, &array
);
1381 val
= stack_pop(ctx
);
1382 hres
= jsdisp_propput_idx(array
, i
, val
);
1385 jsdisp_release(array
);
1390 return stack_push(ctx
, jsval_obj(array
));
1393 /* ECMA-262 3rd Edition 11.1.5 */
1394 static HRESULT
interp_new_obj(script_ctx_t
*ctx
)
1401 hres
= create_object(ctx
, NULL
, &obj
);
1405 return stack_push(ctx
, jsval_obj(obj
));
1408 /* ECMA-262 3rd Edition 11.1.5 */
1409 static HRESULT
interp_obj_prop(script_ctx_t
*ctx
)
1411 const BSTR name
= get_op_bstr(ctx
, 0);
1416 TRACE("%s\n", debugstr_w(name
));
1418 val
= stack_pop(ctx
);
1420 assert(is_object_instance(stack_top(ctx
)));
1421 obj
= as_jsdisp(get_object(stack_top(ctx
)));
1423 hres
= jsdisp_propput_name(obj
, name
, val
);
1428 /* ECMA-262 3rd Edition 11.11 */
1429 static HRESULT
interp_cnd_nz(script_ctx_t
*ctx
)
1431 const unsigned arg
= get_op_uint(ctx
, 0);
1437 hres
= to_boolean(stack_top(ctx
), &b
);
1450 /* ECMA-262 3rd Edition 11.11 */
1451 static HRESULT
interp_cnd_z(script_ctx_t
*ctx
)
1453 const unsigned arg
= get_op_uint(ctx
, 0);
1459 hres
= to_boolean(stack_top(ctx
), &b
);
1472 /* ECMA-262 3rd Edition 11.10 */
1473 static HRESULT
interp_or(script_ctx_t
*ctx
)
1480 hres
= stack_pop_int(ctx
, &r
);
1484 hres
= stack_pop_int(ctx
, &l
);
1488 return stack_push(ctx
, jsval_number(l
|r
));
1491 /* ECMA-262 3rd Edition 11.10 */
1492 static HRESULT
interp_xor(script_ctx_t
*ctx
)
1499 hres
= stack_pop_int(ctx
, &r
);
1503 hres
= stack_pop_int(ctx
, &l
);
1507 return stack_push(ctx
, jsval_number(l
^r
));
1510 /* ECMA-262 3rd Edition 11.10 */
1511 static HRESULT
interp_and(script_ctx_t
*ctx
)
1518 hres
= stack_pop_int(ctx
, &r
);
1522 hres
= stack_pop_int(ctx
, &l
);
1526 return stack_push(ctx
, jsval_number(l
&r
));
1529 /* ECMA-262 3rd Edition 11.8.6 */
1530 static HRESULT
interp_instanceof(script_ctx_t
*ctx
)
1532 jsdisp_t
*obj
, *iter
, *tmp
= NULL
;
1537 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1540 if(!is_object_instance(v
) || !get_object(v
)) {
1542 return throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
1545 obj
= iface_to_jsdisp(get_object(v
));
1546 IDispatch_Release(get_object(v
));
1548 FIXME("non-jsdisp objects not supported\n");
1552 if(is_class(obj
, JSCLASS_FUNCTION
)) {
1553 hres
= jsdisp_propget_name(obj
, prototypeW
, &prot
);
1555 hres
= throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
1557 jsdisp_release(obj
);
1563 if(is_object_instance(prot
)) {
1564 if(is_object_instance(v
))
1565 tmp
= iface_to_jsdisp(get_object(v
));
1566 for(iter
= tmp
; !ret
&& iter
; iter
= iter
->prototype
) {
1567 hres
= disp_cmp(get_object(prot
), to_disp(iter
), &ret
);
1573 jsdisp_release(tmp
);
1575 FIXME("prototype is not an object\n");
1579 jsval_release(prot
);
1584 return stack_push(ctx
, jsval_bool(ret
));
1587 /* ECMA-262 3rd Edition 11.8.7 */
1588 static HRESULT
interp_in(script_ctx_t
*ctx
)
1599 obj
= stack_pop(ctx
);
1600 if(!is_object_instance(obj
) || !get_object(obj
)) {
1602 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
1606 hres
= to_flat_string(ctx
, v
, &jsstr
, &str
);
1609 IDispatch_Release(get_object(obj
));
1613 hres
= disp_get_id(ctx
, get_object(obj
), str
, NULL
, 0, &id
);
1614 IDispatch_Release(get_object(obj
));
1615 jsstr_release(jsstr
);
1618 else if(hres
== DISP_E_UNKNOWNNAME
)
1623 return stack_push(ctx
, jsval_bool(ret
));
1626 /* ECMA-262 3rd Edition 11.6.1 */
1627 static HRESULT
add_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, jsval_t
*ret
)
1632 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
1636 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
1642 if(is_string(l
) || is_string(r
)) {
1643 jsstr_t
*lstr
, *rstr
= NULL
;
1645 hres
= to_string(ctx
, l
, &lstr
);
1647 hres
= to_string(ctx
, r
, &rstr
);
1649 if(SUCCEEDED(hres
)) {
1652 ret_str
= jsstr_concat(lstr
, rstr
);
1654 *ret
= jsval_string(ret_str
);
1656 hres
= E_OUTOFMEMORY
;
1659 jsstr_release(lstr
);
1661 jsstr_release(rstr
);
1665 hres
= to_number(ctx
, l
, &nl
);
1666 if(SUCCEEDED(hres
)) {
1667 hres
= to_number(ctx
, r
, &nr
);
1669 *ret
= jsval_number(nl
+nr
);
1678 /* ECMA-262 3rd Edition 11.6.1 */
1679 static HRESULT
interp_add(script_ctx_t
*ctx
)
1687 TRACE("%s + %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
1689 hres
= add_eval(ctx
, l
, r
, &ret
);
1695 return stack_push(ctx
, ret
);
1698 /* ECMA-262 3rd Edition 11.6.2 */
1699 static HRESULT
interp_sub(script_ctx_t
*ctx
)
1706 hres
= stack_pop_number(ctx
, &r
);
1710 hres
= stack_pop_number(ctx
, &l
);
1714 return stack_push(ctx
, jsval_number(l
-r
));
1717 /* ECMA-262 3rd Edition 11.5.1 */
1718 static HRESULT
interp_mul(script_ctx_t
*ctx
)
1725 hres
= stack_pop_number(ctx
, &r
);
1729 hres
= stack_pop_number(ctx
, &l
);
1733 return stack_push(ctx
, jsval_number(l
*r
));
1736 /* ECMA-262 3rd Edition 11.5.2 */
1737 static HRESULT
interp_div(script_ctx_t
*ctx
)
1744 hres
= stack_pop_number(ctx
, &r
);
1748 hres
= stack_pop_number(ctx
, &l
);
1752 return stack_push(ctx
, jsval_number(l
/r
));
1755 /* ECMA-262 3rd Edition 11.5.3 */
1756 static HRESULT
interp_mod(script_ctx_t
*ctx
)
1763 hres
= stack_pop_number(ctx
, &r
);
1767 hres
= stack_pop_number(ctx
, &l
);
1771 return stack_push(ctx
, jsval_number(fmod(l
, r
)));
1774 /* ECMA-262 3rd Edition 11.4.2 */
1775 static HRESULT
interp_delete(script_ctx_t
*ctx
)
1777 jsval_t objv
, namev
;
1785 namev
= stack_pop(ctx
);
1786 objv
= stack_pop(ctx
);
1788 hres
= to_object(ctx
, objv
, &obj
);
1789 jsval_release(objv
);
1791 jsval_release(namev
);
1795 hres
= to_string(ctx
, namev
, &name
);
1796 jsval_release(namev
);
1798 IDispatch_Release(obj
);
1802 hres
= disp_delete_name(ctx
, obj
, name
, &ret
);
1803 IDispatch_Release(obj
);
1804 jsstr_release(name
);
1808 return stack_push(ctx
, jsval_bool(ret
));
1811 /* ECMA-262 3rd Edition 11.4.2 */
1812 static HRESULT
interp_delete_ident(script_ctx_t
*ctx
)
1814 const BSTR arg
= get_op_bstr(ctx
, 0);
1819 TRACE("%s\n", debugstr_w(arg
));
1821 hres
= identifier_eval(ctx
, arg
, &exprval
);
1825 switch(exprval
.type
) {
1826 case EXPRVAL_STACK_REF
:
1830 hres
= disp_delete(exprval
.u
.idref
.disp
, exprval
.u
.idref
.id
, &ret
);
1831 IDispatch_Release(exprval
.u
.idref
.disp
);
1835 case EXPRVAL_INVALID
:
1839 FIXME("Unsupported exprval\n");
1840 exprval_release(&exprval
);
1845 return stack_push(ctx
, jsval_bool(ret
));
1848 /* ECMA-262 3rd Edition 11.4.2 */
1849 static HRESULT
interp_void(script_ctx_t
*ctx
)
1854 return stack_push(ctx
, jsval_undefined());
1857 /* ECMA-262 3rd Edition 11.4.3 */
1858 static HRESULT
typeof_string(jsval_t v
, const WCHAR
**ret
)
1860 switch(jsval_type(v
)) {
1870 if(get_object(v
) && (dispex
= iface_to_jsdisp(get_object(v
)))) {
1871 *ret
= is_class(dispex
, JSCLASS_FUNCTION
) ? functionW
: objectW
;
1872 jsdisp_release(dispex
);
1888 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v
)));
1895 /* ECMA-262 3rd Edition 11.4.3 */
1896 static HRESULT
interp_typeofid(script_ctx_t
*ctx
)
1905 if(!stack_pop_exprval(ctx
, &ref
))
1906 return stack_push(ctx
, jsval_string(jsstr_undefined()));
1908 hres
= exprval_propget(ctx
, &ref
, &v
);
1909 exprval_release(&ref
);
1911 return stack_push_string(ctx
, unknownW
);
1913 hres
= typeof_string(v
, &ret
);
1918 return stack_push_string(ctx
, ret
);
1921 /* ECMA-262 3rd Edition 11.4.3 */
1922 static HRESULT
interp_typeofident(script_ctx_t
*ctx
)
1924 const BSTR arg
= get_op_bstr(ctx
, 0);
1930 TRACE("%s\n", debugstr_w(arg
));
1932 hres
= identifier_eval(ctx
, arg
, &exprval
);
1936 if(exprval
.type
== EXPRVAL_INVALID
)
1937 return stack_push(ctx
, jsval_string(jsstr_undefined()));
1939 hres
= exprval_to_value(ctx
, &exprval
, &v
);
1943 hres
= typeof_string(v
, &ret
);
1948 return stack_push_string(ctx
, ret
);
1951 /* ECMA-262 3rd Edition 11.4.3 */
1952 static HRESULT
interp_typeof(script_ctx_t
*ctx
)
1961 hres
= typeof_string(v
, &ret
);
1966 return stack_push_string(ctx
, ret
);
1969 /* ECMA-262 3rd Edition 11.4.7 */
1970 static HRESULT
interp_minus(script_ctx_t
*ctx
)
1977 hres
= stack_pop_number(ctx
, &n
);
1981 return stack_push(ctx
, jsval_number(-n
));
1984 /* ECMA-262 3rd Edition 11.4.6 */
1985 static HRESULT
interp_tonum(script_ctx_t
*ctx
)
1994 hres
= to_number(ctx
, v
, &n
);
1999 return stack_push(ctx
, jsval_number(n
));
2002 /* ECMA-262 3rd Edition 11.3.1 */
2003 static HRESULT
interp_postinc(script_ctx_t
*ctx
)
2005 const int arg
= get_op_int(ctx
, 0);
2012 if(!stack_pop_exprval(ctx
, &ref
))
2013 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
2015 hres
= exprval_propget(ctx
, &ref
, &v
);
2016 if(SUCCEEDED(hres
)) {
2019 hres
= to_number(ctx
, v
, &n
);
2021 hres
= exprval_propput(ctx
, &ref
, jsval_number(n
+(double)arg
));
2025 exprval_release(&ref
);
2029 return stack_push(ctx
, v
);
2032 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2033 static HRESULT
interp_preinc(script_ctx_t
*ctx
)
2035 const int arg
= get_op_int(ctx
, 0);
2043 if(!stack_pop_exprval(ctx
, &ref
))
2044 return throw_type_error(ctx
, JS_E_OBJECT_EXPECTED
, NULL
);
2046 hres
= exprval_propget(ctx
, &ref
, &v
);
2047 if(SUCCEEDED(hres
)) {
2050 hres
= to_number(ctx
, v
, &n
);
2052 if(SUCCEEDED(hres
)) {
2053 ret
= n
+(double)arg
;
2054 hres
= exprval_propput(ctx
, &ref
, jsval_number(ret
));
2057 exprval_release(&ref
);
2061 return stack_push(ctx
, jsval_number(ret
));
2064 /* ECMA-262 3rd Edition 11.9.3 */
2065 static HRESULT
equal_values(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL
*ret
)
2067 if(jsval_type(lval
) == jsval_type(rval
) || (is_number(lval
) && is_number(rval
)))
2068 return equal2_values(lval
, rval
, ret
);
2070 /* FIXME: NULL disps should be handled in more general way */
2071 if(is_object_instance(lval
) && !get_object(lval
))
2072 return equal_values(ctx
, jsval_null(), rval
, ret
);
2073 if(is_object_instance(rval
) && !get_object(rval
))
2074 return equal_values(ctx
, lval
, jsval_null(), ret
);
2076 if((is_null(lval
) && is_undefined(rval
)) || (is_undefined(lval
) && is_null(rval
))) {
2081 if(is_string(lval
) && is_number(rval
)) {
2085 hres
= to_number(ctx
, lval
, &n
);
2089 /* FIXME: optimize */
2090 return equal_values(ctx
, jsval_number(n
), rval
, ret
);
2093 if(is_string(rval
) && is_number(lval
)) {
2097 hres
= to_number(ctx
, rval
, &n
);
2101 /* FIXME: optimize */
2102 return equal_values(ctx
, lval
, jsval_number(n
), ret
);
2106 return equal_values(ctx
, lval
, jsval_number(get_bool(rval
) ? 1 : 0), ret
);
2109 return equal_values(ctx
, jsval_number(get_bool(lval
) ? 1 : 0), rval
, ret
);
2112 if(is_object_instance(rval
) && (is_string(lval
) || is_number(lval
))) {
2116 hres
= to_primitive(ctx
, rval
, &prim
, NO_HINT
);
2120 hres
= equal_values(ctx
, lval
, prim
, ret
);
2121 jsval_release(prim
);
2126 if(is_object_instance(lval
) && (is_string(rval
) || is_number(rval
))) {
2130 hres
= to_primitive(ctx
, lval
, &prim
, NO_HINT
);
2134 hres
= equal_values(ctx
, prim
, rval
, ret
);
2135 jsval_release(prim
);
2144 /* ECMA-262 3rd Edition 11.9.1 */
2145 static HRESULT
interp_eq(script_ctx_t
*ctx
)
2154 TRACE("%s == %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2156 hres
= equal_values(ctx
, l
, r
, &b
);
2162 return stack_push(ctx
, jsval_bool(b
));
2165 /* ECMA-262 3rd Edition 11.9.2 */
2166 static HRESULT
interp_neq(script_ctx_t
*ctx
)
2175 TRACE("%s != %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2177 hres
= equal_values(ctx
, l
, r
, &b
);
2183 return stack_push(ctx
, jsval_bool(!b
));
2186 /* ECMA-262 3rd Edition 11.9.4 */
2187 static HRESULT
interp_eq2(script_ctx_t
*ctx
)
2196 TRACE("%s === %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2198 hres
= equal2_values(r
, l
, &b
);
2204 return stack_push(ctx
, jsval_bool(b
));
2207 /* ECMA-262 3rd Edition 11.9.5 */
2208 static HRESULT
interp_neq2(script_ctx_t
*ctx
)
2219 hres
= equal2_values(r
, l
, &b
);
2225 return stack_push(ctx
, jsval_bool(!b
));
2228 /* ECMA-262 3rd Edition 11.8.5 */
2229 static HRESULT
less_eval(script_ctx_t
*ctx
, jsval_t lval
, jsval_t rval
, BOOL greater
, BOOL
*ret
)
2235 hres
= to_primitive(ctx
, lval
, &l
, NO_HINT
);
2239 hres
= to_primitive(ctx
, rval
, &r
, NO_HINT
);
2245 if(is_string(l
) && is_string(r
)) {
2246 *ret
= (jsstr_cmp(get_string(l
), get_string(r
)) < 0) ^ greater
;
2247 jsstr_release(get_string(l
));
2248 jsstr_release(get_string(r
));
2252 hres
= to_number(ctx
, l
, &ln
);
2255 hres
= to_number(ctx
, r
, &rn
);
2260 *ret
= !isnan(ln
) && !isnan(rn
) && ((ln
< rn
) ^ greater
);
2264 /* ECMA-262 3rd Edition 11.8.1 */
2265 static HRESULT
interp_lt(script_ctx_t
*ctx
)
2274 TRACE("%s < %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2276 hres
= less_eval(ctx
, l
, r
, FALSE
, &b
);
2282 return stack_push(ctx
, jsval_bool(b
));
2285 /* ECMA-262 3rd Edition 11.8.1 */
2286 static HRESULT
interp_lteq(script_ctx_t
*ctx
)
2295 TRACE("%s <= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2297 hres
= less_eval(ctx
, r
, l
, TRUE
, &b
);
2303 return stack_push(ctx
, jsval_bool(b
));
2306 /* ECMA-262 3rd Edition 11.8.2 */
2307 static HRESULT
interp_gt(script_ctx_t
*ctx
)
2316 TRACE("%s > %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2318 hres
= less_eval(ctx
, r
, l
, FALSE
, &b
);
2324 return stack_push(ctx
, jsval_bool(b
));
2327 /* ECMA-262 3rd Edition 11.8.4 */
2328 static HRESULT
interp_gteq(script_ctx_t
*ctx
)
2337 TRACE("%s >= %s\n", debugstr_jsval(l
), debugstr_jsval(r
));
2339 hres
= less_eval(ctx
, l
, r
, TRUE
, &b
);
2345 return stack_push(ctx
, jsval_bool(b
));
2348 /* ECMA-262 3rd Edition 11.4.8 */
2349 static HRESULT
interp_bneg(script_ctx_t
*ctx
)
2358 hres
= to_int32(ctx
, v
, &i
);
2363 return stack_push(ctx
, jsval_number(~i
));
2366 /* ECMA-262 3rd Edition 11.4.9 */
2367 static HRESULT
interp_neg(script_ctx_t
*ctx
)
2376 hres
= to_boolean(v
, &b
);
2381 return stack_push(ctx
, jsval_bool(!b
));
2384 /* ECMA-262 3rd Edition 11.7.1 */
2385 static HRESULT
interp_lshift(script_ctx_t
*ctx
)
2391 hres
= stack_pop_uint(ctx
, &r
);
2395 hres
= stack_pop_int(ctx
, &l
);
2399 return stack_push(ctx
, jsval_number(l
<< (r
&0x1f)));
2402 /* ECMA-262 3rd Edition 11.7.2 */
2403 static HRESULT
interp_rshift(script_ctx_t
*ctx
)
2409 hres
= stack_pop_uint(ctx
, &r
);
2413 hres
= stack_pop_int(ctx
, &l
);
2417 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2420 /* ECMA-262 3rd Edition 11.7.3 */
2421 static HRESULT
interp_rshift2(script_ctx_t
*ctx
)
2426 hres
= stack_pop_uint(ctx
, &r
);
2430 hres
= stack_pop_uint(ctx
, &l
);
2434 return stack_push(ctx
, jsval_number(l
>> (r
&0x1f)));
2437 /* ECMA-262 3rd Edition 11.13.1 */
2438 static HRESULT
interp_assign(script_ctx_t
*ctx
)
2448 if(!stack_pop_exprval(ctx
, &ref
)) {
2450 return throw_reference_error(ctx
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2453 hres
= exprval_propput(ctx
, &ref
, v
);
2454 exprval_release(&ref
);
2460 return stack_push(ctx
, v
);
2463 /* JScript extension */
2464 static HRESULT
interp_assign_call(script_ctx_t
*ctx
)
2466 const unsigned argc
= get_op_uint(ctx
, 0);
2471 TRACE("%u\n", argc
);
2473 if(!stack_topn_exprval(ctx
, argc
+1, &ref
))
2474 return throw_reference_error(ctx
, JS_E_ILLEGAL_ASSIGN
, NULL
);
2476 hres
= exprval_call(ctx
, &ref
, DISPATCH_PROPERTYPUT
, argc
+1, stack_args(ctx
, argc
+1), NULL
);
2481 stack_popn(ctx
, argc
+2);
2482 return stack_push(ctx
, v
);
2485 static HRESULT
interp_undefined(script_ctx_t
*ctx
)
2489 return stack_push(ctx
, jsval_undefined());
2492 static HRESULT
interp_jmp(script_ctx_t
*ctx
)
2494 const unsigned arg
= get_op_uint(ctx
, 0);
2502 static HRESULT
interp_jmp_z(script_ctx_t
*ctx
)
2504 const unsigned arg
= get_op_uint(ctx
, 0);
2512 hres
= to_boolean(v
, &b
);
2524 static HRESULT
interp_pop(script_ctx_t
*ctx
)
2526 const unsigned arg
= get_op_uint(ctx
, 0);
2530 stack_popn(ctx
, arg
);
2534 static HRESULT
interp_ret(script_ctx_t
*ctx
)
2536 const unsigned clear_ret
= get_op_uint(ctx
, 0);
2537 call_frame_t
*frame
= ctx
->call_ctx
;
2542 jsval_release(steal_ret(frame
));
2544 if((frame
->flags
& EXEC_CONSTRUCTOR
) && !is_object_instance(frame
->ret
)) {
2545 jsval_release(frame
->ret
);
2546 IDispatch_AddRef(frame
->this_obj
);
2547 frame
->ret
= jsval_disp(frame
->this_obj
);
2554 static HRESULT
interp_setret(script_ctx_t
*ctx
)
2556 call_frame_t
*frame
= ctx
->call_ctx
;
2560 jsval_release(frame
->ret
);
2561 frame
->ret
= stack_pop(ctx
);
2565 static HRESULT
interp_push_ret(script_ctx_t
*ctx
)
2567 call_frame_t
*frame
= ctx
->call_ctx
;
2572 hres
= stack_push(ctx
, frame
->ret
);
2574 frame
->ret
= jsval_undefined();
2578 typedef HRESULT (*op_func_t
)(script_ctx_t
*);
2580 static const op_func_t op_funcs
[] = {
2581 #define X(x,a,b,c) interp_##x,
2586 static const unsigned op_move
[] = {
2587 #define X(a,x,b,c) x,
2592 static void pop_call_frame(script_ctx_t
*ctx
)
2594 call_frame_t
*frame
= ctx
->call_ctx
;
2596 frame
->stack_base
-= frame
->pop_locals
+ frame
->pop_variables
;
2598 assert(frame
->scope
== frame
->base_scope
);
2600 /* If current scope will be kept alive, we need to transfer local variables to its variable object. */
2601 if(frame
->scope
&& frame
->scope
->ref
> 1) {
2602 HRESULT hres
= detach_variable_object(ctx
, frame
, TRUE
);
2604 ERR("Failed to detach variable object: %08x\n", hres
);
2607 if(frame
->arguments_obj
)
2608 detach_arguments_object(frame
->arguments_obj
);
2610 scope_release(frame
->scope
);
2612 if(frame
->pop_variables
)
2613 stack_popn(ctx
, frame
->pop_variables
);
2614 stack_popn(ctx
, frame
->pop_locals
);
2616 ctx
->call_ctx
= frame
->prev_frame
;
2618 if(frame
->function_instance
)
2619 jsdisp_release(frame
->function_instance
);
2620 if(frame
->variable_obj
)
2621 jsdisp_release(frame
->variable_obj
);
2623 IDispatch_Release(frame
->this_obj
);
2624 jsval_release(frame
->ret
);
2625 release_bytecode(frame
->bytecode
);
2629 static HRESULT
unwind_exception(script_ctx_t
*ctx
, HRESULT exception_hres
)
2631 except_frame_t
*except_frame
;
2632 call_frame_t
*frame
;
2637 for(frame
= ctx
->call_ctx
; !frame
->except_frame
; frame
= ctx
->call_ctx
) {
2640 while(frame
->scope
!= frame
->base_scope
)
2641 scope_pop(&frame
->scope
);
2643 stack_popn(ctx
, ctx
->stack_top
-frame
->stack_base
);
2645 flags
= frame
->flags
;
2646 pop_call_frame(ctx
);
2647 if(!(flags
& EXEC_RETURN_TO_INTERP
))
2648 return exception_hres
;
2651 except_frame
= frame
->except_frame
;
2652 frame
->except_frame
= except_frame
->next
;
2654 assert(except_frame
->stack_top
<= ctx
->stack_top
);
2655 stack_popn(ctx
, ctx
->stack_top
- except_frame
->stack_top
);
2657 while(except_frame
->scope
!= frame
->scope
)
2658 scope_pop(&frame
->scope
);
2660 frame
->ip
= except_frame
->catch_off
;
2662 except_val
= ctx
->ei
.val
;
2663 ctx
->ei
.val
= jsval_undefined();
2666 ident
= except_frame
->ident
;
2667 heap_free(except_frame
);
2670 jsdisp_t
*scope_obj
;
2672 hres
= create_dispex(ctx
, NULL
, NULL
, &scope_obj
);
2673 if(SUCCEEDED(hres
)) {
2674 hres
= jsdisp_propput_name(scope_obj
, ident
, except_val
);
2676 jsdisp_release(scope_obj
);
2678 jsval_release(except_val
);
2682 hres
= scope_push(frame
->scope
, scope_obj
, to_disp(scope_obj
), &frame
->scope
);
2683 jsdisp_release(scope_obj
);
2685 hres
= stack_push(ctx
, except_val
);
2689 hres
= stack_push(ctx
, jsval_bool(FALSE
));
2695 static HRESULT
enter_bytecode(script_ctx_t
*ctx
, jsval_t
*r
)
2697 call_frame_t
*frame
;
2699 HRESULT hres
= S_OK
;
2704 frame
= ctx
->call_ctx
;
2705 op
= frame
->bytecode
->instrs
[frame
->ip
].op
;
2706 hres
= op_funcs
[op
](ctx
);
2708 TRACE("EXCEPTION %08x\n", hres
);
2710 hres
= unwind_exception(ctx
, hres
);
2713 }else if(frame
->ip
== -1) {
2714 const DWORD return_to_interp
= frame
->flags
& EXEC_RETURN_TO_INTERP
;
2716 assert(ctx
->stack_top
== frame
->stack_base
);
2717 assert(frame
->scope
== frame
->base_scope
);
2719 if(return_to_interp
) {
2720 clear_ret(frame
->prev_frame
);
2721 frame
->prev_frame
->ret
= steal_ret(frame
);
2723 *r
= steal_ret(frame
);
2725 pop_call_frame(ctx
);
2726 if(!return_to_interp
)
2729 frame
->ip
+= op_move
[op
];
2736 static HRESULT
bind_event_target(script_ctx_t
*ctx
, function_code_t
*func
, jsdisp_t
*func_obj
)
2738 IBindEventHandler
*target
;
2744 hres
= identifier_eval(ctx
, func
->event_target
, &exprval
);
2748 hres
= exprval_to_value(ctx
, &exprval
, &v
);
2752 if(!is_object_instance(v
)) {
2753 FIXME("Can't bind to %s\n", debugstr_jsval(v
));
2757 disp
= get_object(v
);
2758 hres
= IDispatch_QueryInterface(disp
, &IID_IBindEventHandler
, (void**)&target
);
2759 if(SUCCEEDED(hres
)) {
2760 hres
= IBindEventHandler_BindHandler(target
, func
->name
, (IDispatch
*)&func_obj
->IDispatchEx_iface
);
2761 IBindEventHandler_Release(target
);
2763 WARN("BindEvent failed: %08x\n", hres
);
2765 FIXME("No IBindEventHandler, not yet supported binding\n");
2768 IDispatch_Release(disp
);
2772 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
)
2774 const unsigned orig_stack
= ctx
->stack_top
;
2775 scope_chain_t
*scope
;
2780 /* If arguments are already on the stack, we may use them. */
2781 if(argv
+ argc
== ctx
->stack
+ ctx
->stack_top
) {
2782 frame
->arguments_off
= argv
- ctx
->stack
;
2785 frame
->arguments_off
= ctx
->stack_top
;
2786 for(i
= 0; i
< argc
; i
++) {
2787 hres
= jsval_copy(argv
[i
], &v
);
2789 hres
= stack_push(ctx
, v
);
2797 /* If fewer than declared arguments were passed, fill remaining with undefined value. */
2798 for(; i
< frame
->function
->param_cnt
; i
++) {
2799 hres
= stack_push(ctx
, jsval_undefined());
2801 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
2806 frame
->pop_locals
= ctx
->stack_top
- orig_stack
;
2808 frame
->variables_off
= ctx
->stack_top
;
2810 for(i
= 0; i
< frame
->function
->var_cnt
; i
++) {
2811 hres
= stack_push(ctx
, jsval_undefined());
2813 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
2818 frame
->pop_variables
= i
;
2820 hres
= scope_push(scope_chain
, variable_object
, to_disp(variable_object
), &scope
);
2822 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
2826 for(i
= 0; i
< frame
->function
->func_cnt
; i
++) {
2827 if(frame
->function
->funcs
[i
].name
&& !frame
->function
->funcs
[i
].event_target
) {
2831 hres
= create_source_function(ctx
, frame
->bytecode
, frame
->function
->funcs
+i
, scope
, &func_obj
);
2833 stack_popn(ctx
, ctx
->stack_top
- orig_stack
);
2834 scope_release(scope
);
2838 off
= local_off(frame
, frame
->function
->funcs
[i
].local_ref
);
2839 jsval_release(ctx
->stack
[off
]);
2840 ctx
->stack
[off
] = jsval_obj(func_obj
);
2844 scope
->frame
= frame
;
2845 frame
->base_scope
= frame
->scope
= scope
;
2849 HRESULT
exec_source(script_ctx_t
*ctx
, DWORD flags
, bytecode_t
*bytecode
, function_code_t
*function
, scope_chain_t
*scope
,
2850 IDispatch
*this_obj
, jsdisp_t
*function_instance
, jsdisp_t
*variable_obj
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
2852 call_frame_t
*frame
;
2856 for(i
= 0; i
< function
->func_cnt
; i
++) {
2859 if(!function
->funcs
[i
].event_target
)
2862 hres
= create_source_function(ctx
, bytecode
, function
->funcs
+i
, scope
, &func_obj
);
2866 hres
= bind_event_target(ctx
, function
->funcs
+i
, func_obj
);
2867 jsdisp_release(func_obj
);
2872 if(flags
& (EXEC_GLOBAL
| EXEC_EVAL
)) {
2873 for(i
=0; i
< function
->var_cnt
; i
++) {
2874 TRACE("[%d] %s %d\n", i
, debugstr_w(function
->variables
[i
].name
), function
->variables
[i
].func_id
);
2875 if(function
->variables
[i
].func_id
!= -1) {
2878 hres
= create_source_function(ctx
, bytecode
, function
->funcs
+function
->variables
[i
].func_id
, scope
, &func_obj
);
2882 hres
= jsdisp_propput_name(variable_obj
, function
->variables
[i
].name
, jsval_obj(func_obj
));
2883 jsdisp_release(func_obj
);
2884 }else if(!(flags
& EXEC_GLOBAL
) || !lookup_global_members(ctx
, function
->variables
[i
].name
, NULL
)) {
2887 hres
= jsdisp_get_id(variable_obj
, function
->variables
[i
].name
, fdexNameEnsure
, &id
);
2894 /* ECMA-262 3rd Edition 11.2.3.7 */
2898 jsthis
= iface_to_jsdisp(this_obj
);
2900 if(jsthis
->builtin_info
->class == JSCLASS_GLOBAL
|| jsthis
->builtin_info
->class == JSCLASS_NONE
)
2902 jsdisp_release(jsthis
);
2906 if(ctx
->call_ctx
&& (flags
& EXEC_EVAL
)) {
2907 hres
= detach_variable_object(ctx
, ctx
->call_ctx
, FALSE
);
2912 frame
= heap_alloc_zero(sizeof(*frame
));
2914 return E_OUTOFMEMORY
;
2916 frame
->function
= function
;
2917 frame
->ret
= jsval_undefined();
2919 frame
->bytecode
= bytecode_addref(bytecode
);
2921 if(!(flags
& (EXEC_GLOBAL
|EXEC_EVAL
))) {
2922 hres
= setup_scope(ctx
, frame
, scope
, variable_obj
, argc
, argv
);
2924 release_bytecode(frame
->bytecode
);
2929 frame
->base_scope
= frame
->scope
= scope_addref(scope
);
2932 frame
->ip
= function
->instr_off
;
2933 frame
->stack_base
= ctx
->stack_top
;
2935 frame
->this_obj
= this_obj
;
2936 else if(ctx
->host_global
)
2937 frame
->this_obj
= ctx
->host_global
;
2939 frame
->this_obj
= to_disp(ctx
->global
);
2940 IDispatch_AddRef(frame
->this_obj
);
2942 if(function_instance
)
2943 frame
->function_instance
= jsdisp_addref(function_instance
);
2945 frame
->flags
= flags
;
2946 frame
->variable_obj
= jsdisp_addref(variable_obj
);
2948 frame
->prev_frame
= ctx
->call_ctx
;
2949 ctx
->call_ctx
= frame
;
2951 if(flags
& EXEC_RETURN_TO_INTERP
) {
2953 * We're called directly from interpreter, so we may just setup call frame and return.
2954 * Already running interpreter will take care of execution.
2957 *r
= jsval_undefined();
2961 return enter_bytecode(ctx
, r
);