winegstreamer: Register stub ColorConvertDMO transform.
[wine.git] / dlls / jscript / engine.c
blob7dce4fe5d79bb179d02d932c20c213d97f840c11
1 /*
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
20 #include <math.h>
21 #include <assert.h>
23 #include "jscript.h"
24 #include "engine.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
30 struct _except_frame_t {
31 unsigned stack_top;
32 scope_chain_t *scope;
33 unsigned catch_off;
34 unsigned finally_off;
36 except_frame_t *next;
39 typedef struct {
40 enum {
41 EXPRVAL_JSVAL,
42 EXPRVAL_IDREF,
43 EXPRVAL_STACK_REF,
44 EXPRVAL_INVALID
45 } type;
46 union {
47 jsval_t val;
48 struct {
49 IDispatch *disp;
50 DISPID id;
51 } idref;
52 unsigned off;
53 HRESULT hres;
54 } u;
55 } exprval_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;
65 return S_OK;
68 static inline HRESULT stack_push_string(script_ctx_t *ctx, const WCHAR *str)
70 jsstr_t *v;
72 v = jsstr_alloc(str);
73 if(!v)
74 return E_OUTOFMEMORY;
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)
98 if(!n)
99 return NULL;
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)
112 while(n--)
113 jsval_release(stack_pop(ctx));
116 static HRESULT stack_pop_number(script_ctx_t *ctx, double *r)
118 jsval_t v;
119 HRESULT hres;
121 v = stack_pop(ctx);
122 hres = to_number(ctx, v, r);
123 jsval_release(v);
124 return hres;
127 static HRESULT stack_pop_object(script_ctx_t *ctx, IDispatch **r)
129 jsval_t v;
130 HRESULT hres;
132 v = stack_pop(ctx);
133 if(is_object_instance(v)) {
134 *r = get_object(v);
135 return S_OK;
138 hres = to_object(ctx, v, r);
139 jsval_release(v);
140 return hres;
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)
155 return ref < 0
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)
168 HRESULT hres;
170 switch(val->type) {
171 case EXPRVAL_JSVAL:
172 hres = stack_push(ctx, jsval_null());
173 if(SUCCEEDED(hres))
174 hres = stack_push(ctx, val->u.val);
175 return hres;
176 case EXPRVAL_IDREF:
177 hres = stack_push(ctx, jsval_disp(val->u.idref.disp));
178 if(SUCCEEDED(hres))
179 hres = stack_push(ctx, jsval_number(val->u.idref.id));
180 else
181 IDispatch_Release(val->u.idref.disp);
182 return hres;
183 case EXPRVAL_STACK_REF:
184 hres = stack_push(ctx, jsval_number(val->u.off));
185 if(SUCCEEDED(hres))
186 hres = stack_push(ctx, jsval_undefined());
187 return hres;
188 case EXPRVAL_INVALID:
189 hres = stack_push(ctx, jsval_undefined());
190 if(SUCCEEDED(hres))
191 hres = stack_push(ctx, jsval_number(val->u.hres));
192 return hres;
195 assert(0);
196 return E_FAIL;
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)) {
204 case JSV_NUMBER: {
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) {
210 DISPID id;
211 BSTR name;
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;
222 else
224 name = frame->function->params[off - frame->arguments_off];
225 scope = frame->base_scope;
228 while (1)
230 if (scope->jsobj && SUCCEEDED(hres = jsdisp_get_id(scope->jsobj, name, 0, &id)))
231 break;
232 if (scope == frame->base_scope)
234 r->type = EXPRVAL_INVALID;
235 r->u.hres = hres;
236 return FALSE;
238 scope = scope->next;
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;
245 r->u.idref.id = id;
246 return TRUE;
249 r->type = EXPRVAL_STACK_REF;
250 r->u.off = off;
251 return TRUE;
253 case JSV_OBJECT:
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));
258 return TRUE;
259 case JSV_UNDEFINED:
260 r->type = EXPRVAL_INVALID;
261 assert(is_number(stack_topn(ctx, n)));
262 r->u.hres = get_number(stack_topn(ctx, n));
263 return FALSE;
264 case JSV_NULL:
265 r->type = EXPRVAL_JSVAL;
266 r->u.val = stack_topn(ctx, n);
267 return TRUE;
268 default:
269 assert(0);
270 return FALSE;
274 static inline BOOL stack_pop_exprval(script_ctx_t *ctx, exprval_t *r)
276 BOOL ret = stack_topn_exprval(ctx, 0, r);
277 ctx->stack_top -= 2;
278 return ret;
281 static HRESULT exprval_propput(script_ctx_t *ctx, exprval_t *ref, jsval_t v)
283 switch(ref->type) {
284 case EXPRVAL_STACK_REF: {
285 jsval_t *r = ctx->stack + ref->u.off;
286 jsval_release(*r);
287 return jsval_copy(v, r);
289 case EXPRVAL_IDREF:
290 return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v);
291 case EXPRVAL_JSVAL:
292 WARN("ignoring an attempt to set value reference\n");
293 return S_OK;
294 default:
295 assert(0);
296 return E_FAIL;
300 static HRESULT exprval_propget(script_ctx_t *ctx, exprval_t *ref, jsval_t *r)
302 switch(ref->type) {
303 case EXPRVAL_STACK_REF:
304 return jsval_copy(ctx->stack[ref->u.off], r);
305 case EXPRVAL_IDREF:
306 return disp_propget(ctx, ref->u.idref.disp, ref->u.idref.id, r);
307 case EXPRVAL_JSVAL:
308 return jsval_copy(ref->u.val, r);
309 default:
310 assert(0);
311 return E_FAIL;
315 static HRESULT exprval_call(script_ctx_t *ctx, exprval_t *ref, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
317 jsdisp_t *jsdisp;
318 HRESULT hres;
319 jsval_t v;
321 switch(ref->type) {
322 case EXPRVAL_STACK_REF: {
323 v = ctx->stack[ref->u.off];
325 if(!is_object_instance(v)) {
326 FIXME("invoke %s\n", debugstr_jsval(v));
327 return E_FAIL;
330 return disp_call_value(ctx, get_object(v), NULL, flags, argc, argv, r);
332 case EXPRVAL_IDREF:
333 /* ECMA-262 3rd Edition 11.2.3.7 / ECMA-262 5.1 Edition 11.2.3.6 *
334 * Don't treat scope object props as PropertyReferences. */
335 if((jsdisp = to_jsdisp(ref->u.idref.disp)) && jsdisp->builtin_info->class == JSCLASS_NONE) {
336 hres = disp_propget(ctx, ref->u.idref.disp, ref->u.idref.id, &v);
337 if(FAILED(hres))
338 return hres;
339 if(!is_object_instance(v)) {
340 FIXME("invoke %s\n", debugstr_jsval(v));
341 hres = E_FAIL;
342 }else {
343 hres = disp_call_value(ctx, get_object(v), NULL, flags, argc, argv, r);
345 jsval_release(v);
346 return hres;
348 return disp_call(ctx, ref->u.idref.disp, ref->u.idref.id, flags, argc, argv, r);
349 case EXPRVAL_JSVAL: {
350 IDispatch *obj;
352 hres = to_object(ctx, ref->u.val, &obj);
353 if(SUCCEEDED(hres)) {
354 hres = disp_call_value(ctx, obj, NULL, flags, argc, argv, r);
355 IDispatch_Release(obj);
357 return hres;
359 default:
360 assert(0);
361 return E_FAIL;
365 /* ECMA-262 3rd Edition 8.7.1 */
366 /* Steals input reference. */
367 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *ref, jsval_t *r)
369 HRESULT hres;
371 if(ref->type == EXPRVAL_JSVAL) {
372 *r = ref->u.val;
373 return S_OK;
376 hres = exprval_propget(ctx, ref, r);
378 if(ref->type == EXPRVAL_IDREF)
379 IDispatch_Release(ref->u.idref.disp);
380 return hres;
383 static void exprval_release(exprval_t *val)
385 switch(val->type) {
386 case EXPRVAL_JSVAL:
387 jsval_release(val->u.val);
388 return;
389 case EXPRVAL_IDREF:
390 if(val->u.idref.disp)
391 IDispatch_Release(val->u.idref.disp);
392 return;
393 case EXPRVAL_STACK_REF:
394 case EXPRVAL_INVALID:
395 return;
399 static inline void exprval_set_exception(exprval_t *val, HRESULT hres)
401 val->type = EXPRVAL_INVALID;
402 val->u.hres = hres;
405 static inline void exprval_set_disp_ref(exprval_t *ref, IDispatch *obj, DISPID id)
407 ref->type = EXPRVAL_IDREF;
408 IDispatch_AddRef(ref->u.idref.disp = obj);
409 ref->u.idref.id = id;
412 static inline jsval_t steal_ret(call_frame_t *frame)
414 jsval_t r = frame->ret;
415 frame->ret = jsval_undefined();
416 return r;
419 static inline void clear_acc(script_ctx_t *ctx)
421 jsval_release(ctx->acc);
422 ctx->acc = jsval_undefined();
425 static HRESULT scope_push(scope_chain_t *scope, jsdisp_t *jsobj, IDispatch *obj, scope_chain_t **ret)
427 scope_chain_t *new_scope;
429 new_scope = heap_alloc(sizeof(scope_chain_t));
430 if(!new_scope)
431 return E_OUTOFMEMORY;
433 new_scope->ref = 1;
435 if (obj)
436 IDispatch_AddRef(obj);
437 new_scope->jsobj = jsobj;
438 new_scope->obj = obj;
439 new_scope->frame = NULL;
440 new_scope->next = scope ? scope_addref(scope) : NULL;
441 new_scope->scope_index = 0;
443 *ret = new_scope;
444 return S_OK;
447 static void scope_pop(scope_chain_t **scope)
449 scope_chain_t *tmp;
451 tmp = *scope;
452 *scope = tmp->next;
453 scope_release(tmp);
456 void scope_release(scope_chain_t *scope)
458 if(--scope->ref)
459 return;
461 if(scope->next)
462 scope_release(scope->next);
464 if (scope->obj)
465 IDispatch_Release(scope->obj);
466 heap_free(scope);
469 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, const WCHAR *name, BSTR name_bstr, DWORD flags, DISPID *id)
471 IDispatchEx *dispex;
472 jsdisp_t *jsdisp;
473 BSTR bstr;
474 HRESULT hres;
476 jsdisp = iface_to_jsdisp(disp);
477 if(jsdisp) {
478 hres = jsdisp_get_id(jsdisp, name, flags, id);
479 jsdisp_release(jsdisp);
480 return hres;
483 if(name_bstr) {
484 bstr = name_bstr;
485 }else {
486 bstr = SysAllocString(name);
487 if(!bstr)
488 return E_OUTOFMEMORY;
491 *id = 0;
492 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
493 if(SUCCEEDED(hres)) {
494 hres = IDispatchEx_GetDispID(dispex, bstr, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
495 IDispatchEx_Release(dispex);
496 }else {
497 TRACE("using IDispatch\n");
498 hres = IDispatch_GetIDsOfNames(disp, &IID_NULL, &bstr, 1, 0, id);
501 if(name_bstr != bstr)
502 SysFreeString(bstr);
503 return hres;
506 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
508 IObjectIdentity *identity;
509 IUnknown *unk1, *unk2;
510 HRESULT hres;
512 if(disp1 == disp2) {
513 *ret = TRUE;
514 return S_OK;
517 if(!disp1 || !disp2) {
518 *ret = FALSE;
519 return S_OK;
522 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
523 if(FAILED(hres))
524 return hres;
526 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
527 if(FAILED(hres)) {
528 IUnknown_Release(unk1);
529 return hres;
532 if(unk1 == unk2) {
533 *ret = TRUE;
534 }else {
535 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
536 if(SUCCEEDED(hres)) {
537 hres = IObjectIdentity_IsEqualObject(identity, unk2);
538 IObjectIdentity_Release(identity);
539 *ret = hres == S_OK;
540 }else {
541 *ret = FALSE;
545 IUnknown_Release(unk1);
546 IUnknown_Release(unk2);
547 return S_OK;
550 /* ECMA-262 3rd Edition 11.9.6 */
551 HRESULT jsval_strict_equal(jsval_t lval, jsval_t rval, BOOL *ret)
553 jsval_type_t type = jsval_type(lval);
555 TRACE("\n");
557 if(type != jsval_type(rval)) {
558 *ret = FALSE;
559 return S_OK;
562 switch(type) {
563 case JSV_UNDEFINED:
564 case JSV_NULL:
565 *ret = TRUE;
566 break;
567 case JSV_OBJECT:
568 return disp_cmp(get_object(lval), get_object(rval), ret);
569 case JSV_STRING:
570 *ret = jsstr_eq(get_string(lval), get_string(rval));
571 break;
572 case JSV_NUMBER:
573 *ret = get_number(lval) == get_number(rval);
574 break;
575 case JSV_BOOL:
576 *ret = !get_bool(lval) == !get_bool(rval);
577 break;
578 case JSV_VARIANT:
579 WARN("VARIANT type, returning false\n");
580 *ret = FALSE;
581 return S_OK;
584 return S_OK;
587 static HRESULT detach_scope(script_ctx_t *ctx, call_frame_t *frame, scope_chain_t *scope)
589 function_code_t *func = frame->function;
590 unsigned int i, index;
591 HRESULT hres;
593 if (!scope->frame)
594 return S_OK;
596 assert(scope->frame == frame);
597 scope->frame = NULL;
599 if (!scope->jsobj)
601 assert(!scope->obj);
603 if (FAILED(hres = create_object(ctx, NULL, &scope->jsobj)))
604 return hres;
605 scope->obj = to_disp(scope->jsobj);
608 if (scope == frame->base_scope && func->name && func->local_ref == INVALID_LOCAL_REF &&
609 ctx->version >= SCRIPTLANGUAGEVERSION_ES5)
610 jsdisp_propput_name(scope->jsobj, func->name, jsval_obj(jsdisp_addref(frame->function_instance)));
612 index = scope->scope_index;
613 for(i = 0; i < frame->function->local_scopes[index].locals_cnt; i++)
615 WCHAR *name = frame->function->local_scopes[index].locals[i].name;
616 int ref = frame->function->local_scopes[index].locals[i].ref;
618 if (FAILED(hres = jsdisp_propput_name(scope->jsobj, name, ctx->stack[local_off(frame, ref)])))
619 return hres;
620 if (frame->function->variables[ref].func_id != -1 && scope != frame->base_scope
621 && FAILED(hres = jsdisp_propput_name(frame->variable_obj, name, ctx->stack[local_off(frame, ref)])))
622 return hres;
624 return S_OK;
627 static HRESULT detach_scope_chain(script_ctx_t *ctx, call_frame_t *frame, scope_chain_t *scope)
629 HRESULT hres;
631 if (scope != frame->base_scope && FAILED(hres = detach_scope_chain(ctx, frame, scope->next)))
632 return hres;
633 return detach_scope(ctx, frame, scope);
637 * Transfers local variables from stack to variable object.
638 * It's slow, so we want to avoid it as much as possible.
640 static HRESULT detach_variable_object(script_ctx_t *ctx, call_frame_t *frame, BOOL from_release)
642 HRESULT hres;
644 if(!frame->base_scope || !frame->base_scope->frame)
645 return S_OK;
647 TRACE("detaching %p\n", frame);
649 assert(frame == frame->base_scope->frame);
650 assert(frame->variable_obj == frame->base_scope->jsobj);
652 if(!from_release && !frame->arguments_obj) {
653 hres = setup_arguments_object(ctx, frame);
654 if(FAILED(hres))
655 return hres;
658 TRACE("detaching scope chain %p, frame %p.\n", ctx->call_ctx->scope, frame);
659 return detach_scope_chain(ctx, frame, ctx->call_ctx->scope);
662 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
664 named_item_t *item;
665 DISPID id;
666 HRESULT hres;
668 LIST_FOR_EACH_ENTRY(item, &ctx->named_items, named_item_t, entry) {
669 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
670 hres = disp_get_id(ctx, item->disp, identifier, identifier, 0, &id);
671 if(SUCCEEDED(hres)) {
672 if(ret)
673 exprval_set_disp_ref(ret, item->disp, id);
674 return TRUE;
679 return FALSE;
682 IDispatch *lookup_global_host(script_ctx_t *ctx)
684 IDispatch *disp = NULL;
685 named_item_t *item;
687 LIST_FOR_EACH_ENTRY(item, &ctx->named_items, named_item_t, entry) {
688 if(!(item->flags & SCRIPTITEM_GLOBALMEMBERS)) continue;
689 disp = item->disp;
690 break;
692 if(!disp) disp = to_disp(ctx->global);
694 return disp;
697 static int __cdecl local_ref_cmp(const void *key, const void *ref)
699 return wcscmp((const WCHAR*)key, ((const local_ref_t*)ref)->name);
702 local_ref_t *lookup_local(const function_code_t *function, const WCHAR *identifier, unsigned int scope)
704 return bsearch(identifier, function->local_scopes[scope].locals, function->local_scopes[scope].locals_cnt,
705 sizeof(*function->local_scopes[scope].locals), local_ref_cmp);
708 /* ECMA-262 3rd Edition 10.1.4 */
709 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
711 scope_chain_t *scope;
712 named_item_t *item;
713 DISPID id = 0;
714 HRESULT hres;
716 TRACE("%s\n", debugstr_w(identifier));
718 if(ctx->call_ctx) {
719 for(scope = ctx->call_ctx->scope; scope; scope = scope->next) {
720 if(scope->frame) {
721 function_code_t *func = scope->frame->function;
722 local_ref_t *ref = lookup_local(func, identifier, scope->scope_index);
724 if(ref) {
725 ret->type = EXPRVAL_STACK_REF;
726 ret->u.off = local_off(scope->frame, ref->ref);
727 TRACE("returning ref %d for %d\n", ret->u.off, ref->ref);
728 return S_OK;
731 if(!wcscmp(identifier, L"arguments")) {
732 hres = detach_variable_object(ctx, scope->frame, FALSE);
733 if(FAILED(hres))
734 return hres;
737 /* ECMA-262 5.1 Edition 13 */
738 if(func->name && ctx->version >= SCRIPTLANGUAGEVERSION_ES5 &&
739 func->local_ref == INVALID_LOCAL_REF && !wcscmp(identifier, func->name)) {
740 TRACE("returning a function from scope chain\n");
741 ret->type = EXPRVAL_JSVAL;
742 ret->u.val = jsval_obj(jsdisp_addref(scope->frame->function_instance));
743 return S_OK;
747 if (!scope->jsobj && !scope->obj)
748 continue;
750 if(scope->jsobj)
751 hres = jsdisp_get_id(scope->jsobj, identifier, fdexNameImplicit, &id);
752 else
753 hres = disp_get_id(ctx, scope->obj, identifier, identifier, fdexNameImplicit, &id);
754 if(SUCCEEDED(hres)) {
755 exprval_set_disp_ref(ret, scope->obj, id);
756 return S_OK;
760 item = ctx->call_ctx->bytecode->named_item;
761 if(item) {
762 hres = jsdisp_get_id(item->script_obj, identifier, 0, &id);
763 if(SUCCEEDED(hres)) {
764 exprval_set_disp_ref(ret, to_disp(item->script_obj), id);
765 return S_OK;
767 if(!(item->flags & SCRIPTITEM_CODEONLY)) {
768 hres = disp_get_id(ctx, item->disp, identifier, identifier, 0, &id);
769 if(SUCCEEDED(hres)) {
770 exprval_set_disp_ref(ret, item->disp, id);
771 return S_OK;
777 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
778 if(SUCCEEDED(hres)) {
779 exprval_set_disp_ref(ret, to_disp(ctx->global), id);
780 return S_OK;
783 item = lookup_named_item(ctx, identifier, SCRIPTITEM_ISVISIBLE);
784 if(item) {
785 IDispatch_AddRef(item->disp);
786 ret->type = EXPRVAL_JSVAL;
787 ret->u.val = jsval_disp(item->disp);
788 return S_OK;
791 if(lookup_global_members(ctx, identifier, ret))
792 return S_OK;
794 exprval_set_exception(ret, JS_E_UNDEFINED_VARIABLE);
795 return S_OK;
798 static inline BSTR get_op_bstr(script_ctx_t *ctx, int i)
800 call_frame_t *frame = ctx->call_ctx;
801 return frame->bytecode->instrs[frame->ip].u.arg[i].bstr;
804 static inline unsigned get_op_uint(script_ctx_t *ctx, int i)
806 call_frame_t *frame = ctx->call_ctx;
807 return frame->bytecode->instrs[frame->ip].u.arg[i].uint;
810 static inline unsigned get_op_int(script_ctx_t *ctx, int i)
812 call_frame_t *frame = ctx->call_ctx;
813 return frame->bytecode->instrs[frame->ip].u.arg[i].lng;
816 static inline jsstr_t *get_op_str(script_ctx_t *ctx, int i)
818 call_frame_t *frame = ctx->call_ctx;
819 return frame->bytecode->instrs[frame->ip].u.arg[i].str;
822 static inline double get_op_double(script_ctx_t *ctx)
824 call_frame_t *frame = ctx->call_ctx;
825 return frame->bytecode->instrs[frame->ip].u.dbl;
828 static inline void jmp_next(script_ctx_t *ctx)
830 ctx->call_ctx->ip++;
833 static inline void jmp_abs(script_ctx_t *ctx, unsigned dst)
835 ctx->call_ctx->ip = dst;
838 /* ECMA-262 3rd Edition 12.6.4 */
839 static HRESULT interp_forin(script_ctx_t *ctx)
841 const HRESULT arg = get_op_uint(ctx, 0);
842 IDispatch *obj = NULL;
843 IDispatchEx *dispex;
844 exprval_t prop_ref;
845 DISPID id;
846 BSTR name = NULL;
847 HRESULT hres;
849 TRACE("\n");
851 assert(is_number(stack_top(ctx)));
852 id = get_number(stack_top(ctx));
854 if(!stack_topn_exprval(ctx, 1, &prop_ref)) {
855 FIXME("invalid ref: %08lx\n", prop_ref.u.hres);
856 return E_FAIL;
859 if(is_object_instance(stack_topn(ctx, 3)))
860 obj = get_object(stack_topn(ctx, 3));
862 if(obj) {
863 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
864 if(SUCCEEDED(hres)) {
865 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
866 if(hres == S_OK)
867 hres = IDispatchEx_GetMemberName(dispex, id, &name);
868 IDispatchEx_Release(dispex);
869 if(FAILED(hres))
870 return hres;
871 }else {
872 TRACE("No IDispatchEx\n");
876 if(name) {
877 jsstr_t *str;
879 str = jsstr_alloc_len(name, SysStringLen(name));
880 SysFreeString(name);
881 if(!str)
882 return E_OUTOFMEMORY;
884 stack_pop(ctx);
885 stack_push(ctx, jsval_number(id)); /* safe, just after pop() */
887 hres = exprval_propput(ctx, &prop_ref, jsval_string(str));
888 jsstr_release(str);
889 if(FAILED(hres))
890 return hres;
892 jmp_next(ctx);
893 }else {
894 stack_popn(ctx, 4);
895 jmp_abs(ctx, arg);
897 return S_OK;
900 static HRESULT scope_init_locals(script_ctx_t *ctx)
902 call_frame_t *frame = ctx->call_ctx;
903 unsigned int i, off, index;
904 scope_chain_t *scope;
905 BOOL detached_vars;
906 HRESULT hres;
908 scope = frame->scope;
909 index = scope->scope_index;
910 detached_vars = !(frame->base_scope && frame->base_scope->frame);
912 if (!detached_vars)
914 assert(frame->base_scope->frame == frame);
915 frame->scope->frame = ctx->call_ctx;
917 else if (!scope->jsobj)
919 assert(!scope->obj);
920 if (FAILED(hres = create_object(ctx, NULL, &scope->jsobj)))
921 return hres;
922 scope->obj = to_disp(scope->jsobj);
925 for(i = 0; i < frame->function->local_scopes[index].locals_cnt; i++)
927 WCHAR *name = frame->function->local_scopes[index].locals[i].name;
928 int ref = frame->function->local_scopes[index].locals[i].ref;
929 jsdisp_t *func_obj;
930 jsval_t val;
932 if (frame->function->variables[ref].func_id != -1)
934 TRACE("function %s %d\n", debugstr_w(name), i);
936 if (FAILED(hres = create_source_function(ctx, frame->bytecode, frame->function->funcs
937 + frame->function->variables[ref].func_id, ctx->call_ctx->scope, &func_obj)))
938 return hres;
939 val = jsval_obj(func_obj);
940 if (detached_vars && FAILED(hres = jsdisp_propput_name(frame->variable_obj, name, jsval_obj(func_obj))))
941 return hres;
943 else
945 val = jsval_undefined();
948 if (detached_vars)
950 if (FAILED(hres = jsdisp_propput_name(scope->jsobj, name, val)))
951 return hres;
953 else
955 off = local_off(frame, ref);
956 jsval_release(ctx->stack[off]);
957 ctx->stack[off] = val;
960 return S_OK;
963 /* ECMA-262 3rd Edition 12.10 */
964 static HRESULT interp_push_with_scope(script_ctx_t *ctx)
966 IDispatch *disp;
967 jsval_t v;
968 HRESULT hres;
970 TRACE("\n");
972 v = stack_pop(ctx);
973 hres = to_object(ctx, v, &disp);
974 jsval_release(v);
975 if(FAILED(hres))
976 return hres;
978 hres = scope_push(ctx->call_ctx->scope, to_jsdisp(disp), disp, &ctx->call_ctx->scope);
979 IDispatch_Release(disp);
980 return hres;
983 /* ECMA-262 10th Edition 13.3.1 */
984 static HRESULT interp_push_block_scope(script_ctx_t *ctx)
986 unsigned int scope_index = get_op_uint(ctx, 0);
987 call_frame_t *frame = ctx->call_ctx;
988 HRESULT hres;
990 TRACE("scope_index %u.\n", scope_index);
992 hres = scope_push(ctx->call_ctx->scope, NULL, NULL, &frame->scope);
994 if (FAILED(hres) || !scope_index)
995 return hres;
997 frame->scope->scope_index = scope_index;
999 return scope_init_locals(ctx);
1002 /* ECMA-262 3rd Edition 12.10 */
1003 static HRESULT interp_pop_scope(script_ctx_t *ctx)
1005 TRACE("\n");
1007 if(ctx->call_ctx->scope->ref > 1) {
1008 HRESULT hres = detach_variable_object(ctx, ctx->call_ctx, FALSE);
1009 if(FAILED(hres))
1010 ERR("Failed to detach variable object: %08lx\n", hres);
1013 scope_pop(&ctx->call_ctx->scope);
1014 return S_OK;
1017 /* ECMA-262 3rd Edition 12.13 */
1018 static HRESULT interp_case(script_ctx_t *ctx)
1020 const unsigned arg = get_op_uint(ctx, 0);
1021 jsval_t v;
1022 BOOL b;
1023 HRESULT hres;
1025 TRACE("\n");
1027 v = stack_pop(ctx);
1028 hres = jsval_strict_equal(stack_top(ctx), v, &b);
1029 jsval_release(v);
1030 if(FAILED(hres))
1031 return hres;
1033 if(b) {
1034 stack_popn(ctx, 1);
1035 jmp_abs(ctx, arg);
1036 }else {
1037 jmp_next(ctx);
1039 return S_OK;
1042 static void set_error_value(script_ctx_t *ctx, jsval_t value)
1044 jsexcept_t *ei = ctx->ei;
1045 jsdisp_t *obj;
1047 reset_ei(ei);
1048 ei->error = JS_E_EXCEPTION_THROWN;
1049 ei->valid_value = TRUE;
1050 ei->value = value;
1052 if(is_object_instance(value) && (obj = to_jsdisp(get_object(value)))) {
1053 UINT32 number;
1054 jsstr_t *str;
1055 jsval_t v;
1056 HRESULT hres;
1058 /* FIXME: We should check if object is an error instance */
1060 hres = jsdisp_propget_name(obj, L"number", &v);
1061 if(SUCCEEDED(hres)) {
1062 hres = to_uint32(ctx, v, &number);
1063 if(SUCCEEDED(hres))
1064 ei->error = FAILED(number) ? number : E_FAIL;
1065 jsval_release(v);
1068 hres = jsdisp_propget_name(obj, L"description", &v);
1069 if(SUCCEEDED(hres)) {
1070 hres = to_string(ctx, v, &str);
1071 if(SUCCEEDED(hres))
1072 ei->message = str;
1073 jsval_release(v);
1078 /* ECMA-262 3rd Edition 12.13 */
1079 static HRESULT interp_throw(script_ctx_t *ctx)
1081 TRACE("\n");
1083 set_error_value(ctx, stack_pop(ctx));
1084 return DISP_E_EXCEPTION;
1087 static HRESULT interp_throw_ref(script_ctx_t *ctx)
1089 const HRESULT arg = get_op_uint(ctx, 0);
1091 TRACE("%08lx\n", arg);
1093 return arg;
1096 static HRESULT interp_throw_type(script_ctx_t *ctx)
1098 const HRESULT hres = get_op_uint(ctx, 0);
1099 jsstr_t *str = get_op_str(ctx, 1);
1100 const WCHAR *ptr;
1102 TRACE("%08lx %s\n", hres, debugstr_jsstr(str));
1104 ptr = jsstr_flatten(str);
1105 return ptr ? throw_error(ctx, hres, ptr) : E_OUTOFMEMORY;
1108 /* ECMA-262 3rd Edition 12.14 */
1109 static HRESULT interp_push_except(script_ctx_t *ctx)
1111 const unsigned catch_off = get_op_uint(ctx, 0);
1112 const unsigned finally_off = get_op_uint(ctx, 1);
1113 call_frame_t *frame = ctx->call_ctx;
1114 except_frame_t *except;
1116 TRACE("\n");
1118 except = heap_alloc(sizeof(*except));
1119 if(!except)
1120 return E_OUTOFMEMORY;
1122 except->stack_top = ctx->stack_top;
1123 except->scope = frame->scope;
1124 except->catch_off = catch_off;
1125 except->finally_off = finally_off;
1126 except->next = frame->except_frame;
1127 frame->except_frame = except;
1128 return S_OK;
1131 /* ECMA-262 3rd Edition 12.14 */
1132 static HRESULT interp_pop_except(script_ctx_t *ctx)
1134 const unsigned ret_off = get_op_uint(ctx, 0);
1135 call_frame_t *frame = ctx->call_ctx;
1136 except_frame_t *except;
1137 unsigned finally_off;
1139 TRACE("%u\n", ret_off);
1141 except = frame->except_frame;
1142 assert(except != NULL);
1144 finally_off = except->finally_off;
1145 frame->except_frame = except->next;
1146 heap_free(except);
1148 if(finally_off) {
1149 HRESULT hres;
1151 hres = stack_push(ctx, jsval_number(ret_off));
1152 if(FAILED(hres))
1153 return hres;
1154 hres = stack_push(ctx, jsval_bool(TRUE));
1155 if(FAILED(hres))
1156 return hres;
1157 frame->ip = finally_off;
1158 }else {
1159 frame->ip = ret_off;
1162 return S_OK;
1165 /* ECMA-262 3rd Edition 12.14 */
1166 static HRESULT interp_end_finally(script_ctx_t *ctx)
1168 call_frame_t *frame = ctx->call_ctx;
1169 jsval_t v;
1171 TRACE("\n");
1173 v = stack_pop(ctx);
1174 assert(is_bool(v));
1176 if(!get_bool(v)) {
1177 TRACE("passing exception\n");
1179 set_error_value(ctx, stack_pop(ctx));
1180 return DISP_E_EXCEPTION;
1183 v = stack_pop(ctx);
1184 assert(is_number(v));
1185 frame->ip = get_number(v);
1186 return S_OK;
1189 static HRESULT interp_enter_catch(script_ctx_t *ctx)
1191 const BSTR ident = get_op_bstr(ctx, 0);
1192 jsdisp_t *scope_obj;
1193 jsval_t v;
1194 HRESULT hres;
1196 hres = create_dispex(ctx, NULL, NULL, &scope_obj);
1197 if(FAILED(hres))
1198 return hres;
1200 v = stack_pop(ctx);
1201 hres = jsdisp_propput_name(scope_obj, ident, v);
1202 jsval_release(v);
1203 if(SUCCEEDED(hres))
1204 hres = scope_push(ctx->call_ctx->scope, scope_obj, to_disp(scope_obj), &ctx->call_ctx->scope);
1205 jsdisp_release(scope_obj);
1206 return hres;
1209 /* ECMA-262 3rd Edition 13 */
1210 static HRESULT interp_func(script_ctx_t *ctx)
1212 unsigned func_idx = get_op_uint(ctx, 0);
1213 call_frame_t *frame = ctx->call_ctx;
1214 jsdisp_t *dispex;
1215 HRESULT hres;
1217 TRACE("%d\n", func_idx);
1219 hres = create_source_function(ctx, frame->bytecode, frame->function->funcs+func_idx,
1220 frame->scope, &dispex);
1221 if(FAILED(hres))
1222 return hres;
1224 return stack_push(ctx, jsval_obj(dispex));
1227 /* ECMA-262 3rd Edition 11.2.1 */
1228 static HRESULT interp_array(script_ctx_t *ctx)
1230 jsstr_t *name_str;
1231 const WCHAR *name;
1232 jsval_t v, namev;
1233 IDispatch *obj;
1234 DISPID id;
1235 HRESULT hres;
1237 TRACE("\n");
1239 namev = stack_pop(ctx);
1241 hres = stack_pop_object(ctx, &obj);
1242 if(FAILED(hres)) {
1243 jsval_release(namev);
1244 return hres;
1247 hres = to_flat_string(ctx, namev, &name_str, &name);
1248 jsval_release(namev);
1249 if(FAILED(hres)) {
1250 IDispatch_Release(obj);
1251 return hres;
1254 hres = disp_get_id(ctx, obj, name, NULL, 0, &id);
1255 jsstr_release(name_str);
1256 if(SUCCEEDED(hres)) {
1257 hres = disp_propget(ctx, obj, id, &v);
1258 }else if(hres == DISP_E_UNKNOWNNAME) {
1259 v = jsval_undefined();
1260 hres = S_OK;
1262 IDispatch_Release(obj);
1263 if(FAILED(hres))
1264 return hres;
1266 return stack_push(ctx, v);
1269 /* ECMA-262 3rd Edition 11.2.1 */
1270 static HRESULT interp_member(script_ctx_t *ctx)
1272 const BSTR arg = get_op_bstr(ctx, 0);
1273 IDispatch *obj;
1274 jsval_t v;
1275 DISPID id;
1276 HRESULT hres;
1278 TRACE("\n");
1280 hres = stack_pop_object(ctx, &obj);
1281 if(FAILED(hres))
1282 return hres;
1284 hres = disp_get_id(ctx, obj, arg, arg, 0, &id);
1285 if(SUCCEEDED(hres)) {
1286 hres = disp_propget(ctx, obj, id, &v);
1287 }else if(hres == DISP_E_UNKNOWNNAME) {
1288 v = jsval_undefined();
1289 hres = S_OK;
1291 IDispatch_Release(obj);
1292 if(FAILED(hres))
1293 return hres;
1295 return stack_push(ctx, v);
1298 /* ECMA-262 3rd Edition 11.2.1 */
1299 static HRESULT interp_memberid(script_ctx_t *ctx)
1301 const unsigned arg = get_op_uint(ctx, 0);
1302 jsval_t objv, namev;
1303 const WCHAR *name;
1304 jsstr_t *name_str;
1305 IDispatch *obj;
1306 exprval_t ref;
1307 DISPID id;
1308 HRESULT hres;
1310 TRACE("%x\n", arg);
1312 namev = stack_pop(ctx);
1313 objv = stack_pop(ctx);
1315 hres = to_object(ctx, objv, &obj);
1316 jsval_release(objv);
1317 if(SUCCEEDED(hres)) {
1318 hres = to_flat_string(ctx, namev, &name_str, &name);
1319 if(FAILED(hres))
1320 IDispatch_Release(obj);
1322 jsval_release(namev);
1323 if(FAILED(hres))
1324 return hres;
1326 hres = disp_get_id(ctx, obj, name, NULL, arg, &id);
1327 jsstr_release(name_str);
1328 if(SUCCEEDED(hres)) {
1329 ref.type = EXPRVAL_IDREF;
1330 ref.u.idref.disp = obj;
1331 ref.u.idref.id = id;
1332 }else {
1333 IDispatch_Release(obj);
1334 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
1335 exprval_set_exception(&ref, JS_E_INVALID_PROPERTY);
1336 hres = S_OK;
1337 }else {
1338 ERR("failed %08lx\n", hres);
1339 return hres;
1343 return stack_push_exprval(ctx, &ref);
1346 /* ECMA-262 3rd Edition 11.2.1 */
1347 static HRESULT interp_refval(script_ctx_t *ctx)
1349 exprval_t ref;
1350 jsval_t v;
1351 HRESULT hres;
1353 TRACE("\n");
1355 if(!stack_topn_exprval(ctx, 0, &ref))
1356 return JS_E_ILLEGAL_ASSIGN;
1358 hres = exprval_propget(ctx, &ref, &v);
1359 if(FAILED(hres))
1360 return hres;
1362 return stack_push(ctx, v);
1365 /* ECMA-262 3rd Edition 11.2.2 */
1366 static HRESULT interp_new(script_ctx_t *ctx)
1368 const unsigned argc = get_op_uint(ctx, 0);
1369 jsval_t constr;
1371 TRACE("%d\n", argc);
1373 constr = stack_topn(ctx, argc);
1375 /* NOTE: Should use to_object here */
1377 if(is_null(constr))
1378 return is_null_disp(constr) ? JS_E_INVALID_PROPERTY : JS_E_OBJECT_EXPECTED;
1379 if(!is_object_instance(constr))
1380 return JS_E_INVALID_ACTION;
1382 clear_acc(ctx);
1383 return disp_call_value(ctx, get_object(constr), NULL, DISPATCH_CONSTRUCT | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1384 argc, stack_args(ctx, argc), &ctx->acc);
1387 /* ECMA-262 3rd Edition 11.2.3 */
1388 static HRESULT interp_call(script_ctx_t *ctx)
1390 const unsigned argn = get_op_uint(ctx, 0);
1391 const int do_ret = get_op_int(ctx, 1);
1392 jsval_t obj;
1394 TRACE("%d %d\n", argn, do_ret);
1396 obj = stack_topn(ctx, argn);
1397 if(!is_object_instance(obj))
1398 return JS_E_INVALID_PROPERTY;
1400 clear_acc(ctx);
1401 return disp_call_value(ctx, get_object(obj), NULL, DISPATCH_METHOD | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1402 argn, stack_args(ctx, argn), do_ret ? &ctx->acc : NULL);
1405 /* ECMA-262 3rd Edition 11.2.3 */
1406 static HRESULT interp_call_member(script_ctx_t *ctx)
1408 const unsigned argn = get_op_uint(ctx, 0);
1409 const int do_ret = get_op_int(ctx, 1);
1410 exprval_t ref;
1412 TRACE("%d %d\n", argn, do_ret);
1414 if(!stack_topn_exprval(ctx, argn, &ref))
1415 return ref.u.hres;
1417 clear_acc(ctx);
1418 return exprval_call(ctx, &ref, DISPATCH_METHOD | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1419 argn, stack_args(ctx, argn), do_ret ? &ctx->acc : NULL);
1422 /* ECMA-262 3rd Edition 11.1.1 */
1423 static HRESULT interp_this(script_ctx_t *ctx)
1425 IDispatch *this_obj = ctx->call_ctx->this_obj;
1427 TRACE("\n");
1429 if(!this_obj) {
1430 named_item_t *item = ctx->call_ctx->bytecode->named_item;
1432 if(item)
1433 this_obj = (item->flags & SCRIPTITEM_CODEONLY) ? to_disp(item->script_obj) : item->disp;
1434 else
1435 this_obj = lookup_global_host(ctx);
1438 IDispatch_AddRef(this_obj);
1439 return stack_push(ctx, jsval_disp(this_obj));
1442 static HRESULT interp_identifier_ref(script_ctx_t *ctx, BSTR identifier, unsigned flags)
1444 exprval_t exprval;
1445 HRESULT hres;
1447 hres = identifier_eval(ctx, identifier, &exprval);
1448 if(FAILED(hres))
1449 return hres;
1451 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1452 jsdisp_t *script_obj = ctx->global;
1453 DISPID id;
1455 if(ctx->call_ctx->bytecode->named_item)
1456 script_obj = ctx->call_ctx->bytecode->named_item->script_obj;
1458 hres = jsdisp_get_id(script_obj, identifier, fdexNameEnsure, &id);
1459 if(FAILED(hres))
1460 return hres;
1462 exprval_set_disp_ref(&exprval, to_disp(script_obj), id);
1465 if(exprval.type == EXPRVAL_INVALID ||
1466 (exprval.type == EXPRVAL_JSVAL && ctx->version < SCRIPTLANGUAGEVERSION_ES5)) {
1467 WARN("invalid ref\n");
1468 exprval_release(&exprval);
1469 exprval_set_exception(&exprval, JS_E_OBJECT_EXPECTED);
1472 return stack_push_exprval(ctx, &exprval);
1475 static HRESULT identifier_value(script_ctx_t *ctx, BSTR identifier)
1477 exprval_t exprval;
1478 jsval_t v;
1479 HRESULT hres;
1481 hres = identifier_eval(ctx, identifier, &exprval);
1482 if(FAILED(hres))
1483 return hres;
1485 if(exprval.type == EXPRVAL_INVALID)
1486 return throw_error(ctx, exprval.u.hres, identifier);
1488 hres = exprval_to_value(ctx, &exprval, &v);
1489 if(FAILED(hres))
1490 return hres;
1492 return stack_push(ctx, v);
1495 static HRESULT interp_local_ref(script_ctx_t *ctx)
1497 const int arg = get_op_int(ctx, 0);
1498 const unsigned flags = get_op_uint(ctx, 1);
1499 call_frame_t *frame = ctx->call_ctx;
1500 exprval_t ref;
1502 TRACE("%s\n", debugstr_w(local_name(frame, arg)));
1504 if(!frame->base_scope || !frame->base_scope->frame)
1505 return interp_identifier_ref(ctx, local_name(frame, arg), flags);
1507 ref.type = EXPRVAL_STACK_REF;
1508 ref.u.off = local_off(frame, arg);
1509 return stack_push_exprval(ctx, &ref);
1512 static HRESULT interp_local(script_ctx_t *ctx)
1514 const int arg = get_op_int(ctx, 0);
1515 call_frame_t *frame = ctx->call_ctx;
1516 jsval_t copy;
1517 HRESULT hres;
1519 if(!frame->base_scope || !frame->base_scope->frame) {
1520 TRACE("%s\n", debugstr_w(local_name(frame, arg)));
1521 return identifier_value(ctx, local_name(frame, arg));
1524 hres = jsval_copy(ctx->stack[local_off(frame, arg)], &copy);
1525 if(FAILED(hres))
1526 return hres;
1528 TRACE("%s: %s\n", debugstr_w(local_name(frame, arg)), debugstr_jsval(copy));
1529 return stack_push(ctx, copy);
1532 /* ECMA-262 3rd Edition 10.1.4 */
1533 static HRESULT interp_ident(script_ctx_t *ctx)
1535 const BSTR arg = get_op_bstr(ctx, 0);
1537 TRACE("%s\n", debugstr_w(arg));
1539 return identifier_value(ctx, arg);
1542 /* ECMA-262 3rd Edition 10.1.4 */
1543 static HRESULT interp_identid(script_ctx_t *ctx)
1545 const BSTR arg = get_op_bstr(ctx, 0);
1546 const unsigned flags = get_op_uint(ctx, 1);
1548 TRACE("%s %x\n", debugstr_w(arg), flags);
1550 return interp_identifier_ref(ctx, arg, flags);
1553 /* ECMA-262 3rd Edition 7.8.1 */
1554 static HRESULT interp_null(script_ctx_t *ctx)
1556 TRACE("\n");
1558 return stack_push(ctx, jsval_null());
1561 /* ECMA-262 3rd Edition 7.8.2 */
1562 static HRESULT interp_bool(script_ctx_t *ctx)
1564 const int arg = get_op_int(ctx, 0);
1566 TRACE("%s\n", arg ? "true" : "false");
1568 return stack_push(ctx, jsval_bool(arg));
1571 /* ECMA-262 3rd Edition 7.8.3 */
1572 static HRESULT interp_int(script_ctx_t *ctx)
1574 const int arg = get_op_int(ctx, 0);
1576 TRACE("%d\n", arg);
1578 return stack_push(ctx, jsval_number(arg));
1581 /* ECMA-262 3rd Edition 7.8.3 */
1582 static HRESULT interp_double(script_ctx_t *ctx)
1584 const double arg = get_op_double(ctx);
1586 TRACE("%lf\n", arg);
1588 return stack_push(ctx, jsval_number(arg));
1591 /* ECMA-262 3rd Edition 7.8.4 */
1592 static HRESULT interp_str(script_ctx_t *ctx)
1594 jsstr_t *str = get_op_str(ctx, 0);
1596 TRACE("%s\n", debugstr_jsstr(str));
1598 return stack_push(ctx, jsval_string(jsstr_addref(str)));
1601 /* ECMA-262 3rd Edition 7.8 */
1602 static HRESULT interp_regexp(script_ctx_t *ctx)
1604 jsstr_t *source = get_op_str(ctx, 0);
1605 const unsigned flags = get_op_uint(ctx, 1);
1606 jsdisp_t *regexp;
1607 HRESULT hres;
1609 TRACE("%s %x\n", debugstr_jsstr(source), flags);
1611 hres = create_regexp(ctx, source, flags, &regexp);
1612 if(FAILED(hres))
1613 return hres;
1615 return stack_push(ctx, jsval_obj(regexp));
1618 /* ECMA-262 3rd Edition 11.1.4 */
1619 static HRESULT interp_carray(script_ctx_t *ctx)
1621 const unsigned arg = get_op_uint(ctx, 0);
1622 jsdisp_t *array;
1623 HRESULT hres;
1625 TRACE("%u\n", arg);
1627 hres = create_array(ctx, arg, &array);
1628 if(FAILED(hres))
1629 return hres;
1631 return stack_push(ctx, jsval_obj(array));
1634 static HRESULT interp_carray_set(script_ctx_t *ctx)
1636 const unsigned index = get_op_uint(ctx, 0);
1637 jsval_t value, array;
1638 HRESULT hres;
1640 value = stack_pop(ctx);
1642 TRACE("[%u] = %s\n", index, debugstr_jsval(value));
1644 array = stack_top(ctx);
1645 assert(is_object_instance(array));
1647 hres = jsdisp_propput_idx(iface_to_jsdisp(get_object(array)), index, value);
1648 jsval_release(value);
1649 return hres;
1652 /* ECMA-262 3rd Edition 11.1.5 */
1653 static HRESULT interp_new_obj(script_ctx_t *ctx)
1655 jsdisp_t *obj;
1656 HRESULT hres;
1658 TRACE("\n");
1660 hres = create_object(ctx, NULL, &obj);
1661 if(FAILED(hres))
1662 return hres;
1664 return stack_push(ctx, jsval_obj(obj));
1667 /* ECMA-262 3rd Edition 11.1.5 */
1668 static HRESULT interp_obj_prop(script_ctx_t *ctx)
1670 jsstr_t *name_arg = get_op_str(ctx, 0);
1671 unsigned type = get_op_uint(ctx, 1);
1672 const WCHAR *name;
1673 jsdisp_t *obj;
1674 jsval_t val;
1675 HRESULT hres;
1677 TRACE("%s\n", debugstr_jsstr(name_arg));
1679 val = stack_pop(ctx);
1681 /* FIXME: we should pass it as jsstr_t */
1682 name = jsstr_flatten(name_arg);
1684 assert(is_object_instance(stack_top(ctx)));
1685 obj = as_jsdisp(get_object(stack_top(ctx)));
1687 if(type == PROPERTY_DEFINITION_VALUE) {
1688 hres = jsdisp_propput_name(obj, name, val);
1689 }else {
1690 property_desc_t desc = {PROPF_ENUMERABLE | PROPF_CONFIGURABLE};
1691 jsdisp_t *func;
1693 assert(is_object_instance(val));
1694 func = iface_to_jsdisp(get_object(val));
1696 desc.mask = desc.flags;
1697 if(type == PROPERTY_DEFINITION_GETTER) {
1698 desc.explicit_getter = TRUE;
1699 desc.getter = func;
1700 }else {
1701 desc.explicit_setter = TRUE;
1702 desc.setter = func;
1705 hres = jsdisp_define_property(obj, name, &desc);
1706 jsdisp_release(func);
1709 jsval_release(val);
1710 return hres;
1713 /* ECMA-262 3rd Edition 11.11 */
1714 static HRESULT interp_cnd_nz(script_ctx_t *ctx)
1716 const unsigned arg = get_op_uint(ctx, 0);
1717 BOOL b;
1718 HRESULT hres;
1720 TRACE("\n");
1722 hres = to_boolean(stack_top(ctx), &b);
1723 if(FAILED(hres))
1724 return hres;
1726 if(b) {
1727 jmp_abs(ctx, arg);
1728 }else {
1729 stack_popn(ctx, 1);
1730 jmp_next(ctx);
1732 return S_OK;
1735 /* ECMA-262 3rd Edition 11.11 */
1736 static HRESULT interp_cnd_z(script_ctx_t *ctx)
1738 const unsigned arg = get_op_uint(ctx, 0);
1739 BOOL b;
1740 HRESULT hres;
1742 TRACE("\n");
1744 hres = to_boolean(stack_top(ctx), &b);
1745 if(FAILED(hres))
1746 return hres;
1748 if(b) {
1749 stack_popn(ctx, 1);
1750 jmp_next(ctx);
1751 }else {
1752 jmp_abs(ctx, arg);
1754 return S_OK;
1757 /* ECMA-262 3rd Edition 11.10 */
1758 static HRESULT interp_or(script_ctx_t *ctx)
1760 INT l, r;
1761 HRESULT hres;
1763 TRACE("\n");
1765 hres = stack_pop_int(ctx, &r);
1766 if(FAILED(hres))
1767 return hres;
1769 hres = stack_pop_int(ctx, &l);
1770 if(FAILED(hres))
1771 return hres;
1773 return stack_push(ctx, jsval_number(l|r));
1776 /* ECMA-262 3rd Edition 11.10 */
1777 static HRESULT interp_xor(script_ctx_t *ctx)
1779 INT l, r;
1780 HRESULT hres;
1782 TRACE("\n");
1784 hres = stack_pop_int(ctx, &r);
1785 if(FAILED(hres))
1786 return hres;
1788 hres = stack_pop_int(ctx, &l);
1789 if(FAILED(hres))
1790 return hres;
1792 return stack_push(ctx, jsval_number(l^r));
1795 /* ECMA-262 3rd Edition 11.10 */
1796 static HRESULT interp_and(script_ctx_t *ctx)
1798 INT l, r;
1799 HRESULT hres;
1801 TRACE("\n");
1803 hres = stack_pop_int(ctx, &r);
1804 if(FAILED(hres))
1805 return hres;
1807 hres = stack_pop_int(ctx, &l);
1808 if(FAILED(hres))
1809 return hres;
1811 return stack_push(ctx, jsval_number(l&r));
1814 /* ECMA-262 3rd Edition 11.8.6 */
1815 static HRESULT interp_instanceof(script_ctx_t *ctx)
1817 jsdisp_t *obj, *iter, *tmp = NULL;
1818 jsval_t prot, v;
1819 BOOL ret = FALSE;
1820 HRESULT hres;
1822 v = stack_pop(ctx);
1823 if(!is_object_instance(v)) {
1824 jsval_release(v);
1825 return JS_E_FUNCTION_EXPECTED;
1828 obj = iface_to_jsdisp(get_object(v));
1829 IDispatch_Release(get_object(v));
1830 if(!obj) {
1831 FIXME("non-jsdisp objects not supported\n");
1832 return E_FAIL;
1835 if(is_class(obj, JSCLASS_FUNCTION)) {
1836 hres = jsdisp_propget_name(obj, L"prototype", &prot);
1837 }else {
1838 hres = JS_E_FUNCTION_EXPECTED;
1840 jsdisp_release(obj);
1841 if(FAILED(hres))
1842 return hres;
1844 v = stack_pop(ctx);
1846 if(is_null_disp(v))
1847 hres = JS_E_OBJECT_EXPECTED;
1848 else if(is_object_instance(prot)) {
1849 if(is_object_instance(v))
1850 tmp = iface_to_jsdisp(get_object(v));
1851 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1852 hres = disp_cmp(get_object(prot), to_disp(iter), &ret);
1853 if(FAILED(hres))
1854 break;
1857 if(tmp)
1858 jsdisp_release(tmp);
1859 }else {
1860 FIXME("prototype is not an object\n");
1861 hres = E_FAIL;
1864 jsval_release(prot);
1865 jsval_release(v);
1866 if(FAILED(hres))
1867 return hres;
1869 return stack_push(ctx, jsval_bool(ret));
1872 /* ECMA-262 3rd Edition 11.8.7 */
1873 static HRESULT interp_in(script_ctx_t *ctx)
1875 const WCHAR *str;
1876 jsstr_t *jsstr;
1877 jsval_t obj, v;
1878 DISPID id = 0;
1879 BOOL ret;
1880 HRESULT hres;
1882 TRACE("\n");
1884 obj = stack_pop(ctx);
1885 if(!is_object_instance(obj)) {
1886 jsval_release(obj);
1887 return JS_E_OBJECT_EXPECTED;
1890 v = stack_pop(ctx);
1891 hres = to_flat_string(ctx, v, &jsstr, &str);
1892 jsval_release(v);
1893 if(FAILED(hres)) {
1894 IDispatch_Release(get_object(obj));
1895 return hres;
1898 hres = disp_get_id(ctx, get_object(obj), str, NULL, 0, &id);
1899 IDispatch_Release(get_object(obj));
1900 jsstr_release(jsstr);
1901 if(SUCCEEDED(hres))
1902 ret = TRUE;
1903 else if(hres == DISP_E_UNKNOWNNAME)
1904 ret = FALSE;
1905 else
1906 return hres;
1908 return stack_push(ctx, jsval_bool(ret));
1911 /* ECMA-262 3rd Edition 11.6.1 */
1912 static HRESULT interp_add(script_ctx_t *ctx)
1914 jsval_t l, r, lval, rval, ret;
1915 HRESULT hres;
1917 rval = stack_pop(ctx);
1918 lval = stack_pop(ctx);
1920 TRACE("%s + %s\n", debugstr_jsval(lval), debugstr_jsval(rval));
1922 hres = to_primitive(ctx, lval, &l, NO_HINT);
1923 if(SUCCEEDED(hres)) {
1924 hres = to_primitive(ctx, rval, &r, NO_HINT);
1925 if(FAILED(hres))
1926 jsval_release(l);
1928 jsval_release(lval);
1929 jsval_release(rval);
1930 if(FAILED(hres))
1931 return hres;
1933 if(is_string(l) || is_string(r)) {
1934 jsstr_t *lstr, *rstr = NULL;
1936 hres = to_string(ctx, l, &lstr);
1937 if(SUCCEEDED(hres))
1938 hres = to_string(ctx, r, &rstr);
1940 if(SUCCEEDED(hres)) {
1941 jsstr_t *ret_str;
1943 ret_str = jsstr_concat(lstr, rstr);
1944 if(ret_str)
1945 ret = jsval_string(ret_str);
1946 else
1947 hres = E_OUTOFMEMORY;
1950 jsstr_release(lstr);
1951 if(rstr)
1952 jsstr_release(rstr);
1953 }else {
1954 double nl, nr;
1956 hres = to_number(ctx, l, &nl);
1957 if(SUCCEEDED(hres)) {
1958 hres = to_number(ctx, r, &nr);
1959 if(SUCCEEDED(hres))
1960 ret = jsval_number(nl+nr);
1964 jsval_release(r);
1965 jsval_release(l);
1966 if(FAILED(hres))
1967 return hres;
1969 return stack_push(ctx, ret);
1972 /* ECMA-262 3rd Edition 11.6.2 */
1973 static HRESULT interp_sub(script_ctx_t *ctx)
1975 double l, r;
1976 HRESULT hres;
1978 TRACE("\n");
1980 hres = stack_pop_number(ctx, &r);
1981 if(FAILED(hres))
1982 return hres;
1984 hres = stack_pop_number(ctx, &l);
1985 if(FAILED(hres))
1986 return hres;
1988 return stack_push(ctx, jsval_number(l-r));
1991 /* ECMA-262 3rd Edition 11.5.1 */
1992 static HRESULT interp_mul(script_ctx_t *ctx)
1994 double l, r;
1995 HRESULT hres;
1997 TRACE("\n");
1999 hres = stack_pop_number(ctx, &r);
2000 if(FAILED(hres))
2001 return hres;
2003 hres = stack_pop_number(ctx, &l);
2004 if(FAILED(hres))
2005 return hres;
2007 return stack_push(ctx, jsval_number(l*r));
2010 /* ECMA-262 3rd Edition 11.5.2 */
2011 static HRESULT interp_div(script_ctx_t *ctx)
2013 double l, r;
2014 HRESULT hres;
2016 TRACE("\n");
2018 hres = stack_pop_number(ctx, &r);
2019 if(FAILED(hres))
2020 return hres;
2022 hres = stack_pop_number(ctx, &l);
2023 if(FAILED(hres))
2024 return hres;
2026 return stack_push(ctx, jsval_number(l/r));
2029 /* ECMA-262 3rd Edition 11.5.3 */
2030 static HRESULT interp_mod(script_ctx_t *ctx)
2032 double l, r;
2033 HRESULT hres;
2035 TRACE("\n");
2037 hres = stack_pop_number(ctx, &r);
2038 if(FAILED(hres))
2039 return hres;
2041 hres = stack_pop_number(ctx, &l);
2042 if(FAILED(hres))
2043 return hres;
2045 return stack_push(ctx, jsval_number(fmod(l, r)));
2048 /* ECMA-262 3rd Edition 11.4.2 */
2049 static HRESULT interp_delete(script_ctx_t *ctx)
2051 jsval_t objv, namev;
2052 IDispatch *obj;
2053 jsstr_t *name;
2054 BOOL ret;
2055 HRESULT hres;
2057 TRACE("\n");
2059 namev = stack_pop(ctx);
2060 objv = stack_pop(ctx);
2062 hres = to_object(ctx, objv, &obj);
2063 jsval_release(objv);
2064 if(FAILED(hres)) {
2065 jsval_release(namev);
2066 return hres;
2069 hres = to_string(ctx, namev, &name);
2070 jsval_release(namev);
2071 if(FAILED(hres)) {
2072 IDispatch_Release(obj);
2073 return hres;
2076 hres = disp_delete_name(ctx, obj, name, &ret);
2077 IDispatch_Release(obj);
2078 jsstr_release(name);
2079 if(FAILED(hres))
2080 return hres;
2082 return stack_push(ctx, jsval_bool(ret));
2085 /* ECMA-262 3rd Edition 11.4.2 */
2086 static HRESULT interp_delete_ident(script_ctx_t *ctx)
2088 const BSTR arg = get_op_bstr(ctx, 0);
2089 exprval_t exprval;
2090 BOOL ret;
2091 HRESULT hres;
2093 TRACE("%s\n", debugstr_w(arg));
2095 hres = identifier_eval(ctx, arg, &exprval);
2096 if(FAILED(hres))
2097 return hres;
2099 switch(exprval.type) {
2100 case EXPRVAL_STACK_REF:
2101 ret = FALSE;
2102 break;
2103 case EXPRVAL_IDREF:
2104 hres = disp_delete(exprval.u.idref.disp, exprval.u.idref.id, &ret);
2105 IDispatch_Release(exprval.u.idref.disp);
2106 if(FAILED(hres))
2107 return hres;
2108 break;
2109 case EXPRVAL_INVALID:
2110 ret = TRUE;
2111 break;
2112 default:
2113 FIXME("Unsupported exprval\n");
2114 exprval_release(&exprval);
2115 return E_NOTIMPL;
2119 return stack_push(ctx, jsval_bool(ret));
2122 /* ECMA-262 3rd Edition 11.4.2 */
2123 static HRESULT interp_void(script_ctx_t *ctx)
2125 TRACE("\n");
2127 stack_popn(ctx, 1);
2128 return stack_push(ctx, jsval_undefined());
2131 /* ECMA-262 3rd Edition 11.4.3 */
2132 static HRESULT typeof_string(jsval_t v, const WCHAR **ret)
2134 switch(jsval_type(v)) {
2135 case JSV_UNDEFINED:
2136 *ret = L"undefined";
2137 break;
2138 case JSV_NULL:
2139 *ret = L"object";
2140 break;
2141 case JSV_OBJECT: {
2142 jsdisp_t *dispex;
2144 if((dispex = iface_to_jsdisp(get_object(v)))) {
2145 *ret = is_class(dispex, JSCLASS_FUNCTION) ? L"function" : L"object";
2146 jsdisp_release(dispex);
2147 }else {
2148 *ret = L"object";
2150 break;
2152 case JSV_STRING:
2153 *ret = L"string";
2154 break;
2155 case JSV_NUMBER:
2156 *ret = L"number";
2157 break;
2158 case JSV_BOOL:
2159 *ret = L"boolean";
2160 break;
2161 case JSV_VARIANT:
2162 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v)));
2163 return E_NOTIMPL;
2166 return S_OK;
2169 /* ECMA-262 3rd Edition 11.4.3 */
2170 static HRESULT interp_typeofid(script_ctx_t *ctx)
2172 const WCHAR *ret;
2173 exprval_t ref;
2174 jsval_t v;
2175 HRESULT hres;
2177 TRACE("\n");
2179 if(!stack_pop_exprval(ctx, &ref))
2180 return stack_push(ctx, jsval_string(jsstr_undefined()));
2182 hres = exprval_propget(ctx, &ref, &v);
2183 exprval_release(&ref);
2184 if(FAILED(hres))
2185 return stack_push_string(ctx, L"unknown");
2187 hres = typeof_string(v, &ret);
2188 jsval_release(v);
2189 if(FAILED(hres))
2190 return hres;
2192 return stack_push_string(ctx, ret);
2195 /* ECMA-262 3rd Edition 11.4.3 */
2196 static HRESULT interp_typeofident(script_ctx_t *ctx)
2198 const BSTR arg = get_op_bstr(ctx, 0);
2199 exprval_t exprval;
2200 const WCHAR *ret;
2201 jsval_t v;
2202 HRESULT hres;
2204 TRACE("%s\n", debugstr_w(arg));
2206 hres = identifier_eval(ctx, arg, &exprval);
2207 if(FAILED(hres))
2208 return hres;
2210 if(exprval.type == EXPRVAL_INVALID)
2211 return stack_push(ctx, jsval_string(jsstr_undefined()));
2213 hres = exprval_to_value(ctx, &exprval, &v);
2214 if(FAILED(hres))
2215 return hres;
2217 hres = typeof_string(v, &ret);
2218 jsval_release(v);
2219 if(FAILED(hres))
2220 return hres;
2222 return stack_push_string(ctx, ret);
2225 /* ECMA-262 3rd Edition 11.4.3 */
2226 static HRESULT interp_typeof(script_ctx_t *ctx)
2228 const WCHAR *ret;
2229 jsval_t v;
2230 HRESULT hres;
2232 TRACE("\n");
2234 v = stack_pop(ctx);
2235 hres = typeof_string(v, &ret);
2236 jsval_release(v);
2237 if(FAILED(hres))
2238 return hres;
2240 return stack_push_string(ctx, ret);
2243 /* ECMA-262 3rd Edition 11.4.7 */
2244 static HRESULT interp_minus(script_ctx_t *ctx)
2246 double n;
2247 HRESULT hres;
2249 TRACE("\n");
2251 hres = stack_pop_number(ctx, &n);
2252 if(FAILED(hres))
2253 return hres;
2255 return stack_push(ctx, jsval_number(-n));
2258 /* ECMA-262 3rd Edition 11.4.6 */
2259 static HRESULT interp_tonum(script_ctx_t *ctx)
2261 jsval_t v;
2262 double n;
2263 HRESULT hres;
2265 TRACE("\n");
2267 v = stack_pop(ctx);
2268 hres = to_number(ctx, v, &n);
2269 jsval_release(v);
2270 if(FAILED(hres))
2271 return hres;
2273 return stack_push(ctx, jsval_number(n));
2276 /* ECMA-262 3rd Edition 11.3.1 */
2277 static HRESULT interp_postinc(script_ctx_t *ctx)
2279 const int arg = get_op_int(ctx, 0);
2280 exprval_t ref;
2281 jsval_t v;
2282 HRESULT hres;
2284 TRACE("%d\n", arg);
2286 if(!stack_pop_exprval(ctx, &ref))
2287 return JS_E_OBJECT_EXPECTED;
2289 hres = exprval_propget(ctx, &ref, &v);
2290 if(SUCCEEDED(hres)) {
2291 double n;
2293 hres = to_number(ctx, v, &n);
2294 if(SUCCEEDED(hres))
2295 hres = exprval_propput(ctx, &ref, jsval_number(n+(double)arg));
2296 if(FAILED(hres))
2297 jsval_release(v);
2299 exprval_release(&ref);
2300 if(FAILED(hres))
2301 return hres;
2303 return stack_push(ctx, v);
2306 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2307 static HRESULT interp_preinc(script_ctx_t *ctx)
2309 const int arg = get_op_int(ctx, 0);
2310 exprval_t ref;
2311 double ret;
2312 jsval_t v;
2313 HRESULT hres;
2315 TRACE("%d\n", arg);
2317 if(!stack_pop_exprval(ctx, &ref))
2318 return JS_E_OBJECT_EXPECTED;
2320 hres = exprval_propget(ctx, &ref, &v);
2321 if(SUCCEEDED(hres)) {
2322 double n;
2324 hres = to_number(ctx, v, &n);
2325 jsval_release(v);
2326 if(SUCCEEDED(hres)) {
2327 ret = n+(double)arg;
2328 hres = exprval_propput(ctx, &ref, jsval_number(ret));
2331 exprval_release(&ref);
2332 if(FAILED(hres))
2333 return hres;
2335 return stack_push(ctx, jsval_number(ret));
2338 /* ECMA-262 3rd Edition 11.9.3 */
2339 static HRESULT equal_values(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL *ret)
2341 if(jsval_type(lval) == jsval_type(rval) || (is_number(lval) && is_number(rval)))
2342 return jsval_strict_equal(lval, rval, ret);
2344 if((is_null(lval) && is_undefined(rval)) || (is_undefined(lval) && is_null(rval))) {
2345 *ret = TRUE;
2346 return S_OK;
2349 if(is_string(lval) && is_number(rval)) {
2350 double n;
2351 HRESULT hres;
2353 hres = to_number(ctx, lval, &n);
2354 if(FAILED(hres))
2355 return hres;
2357 /* FIXME: optimize */
2358 return equal_values(ctx, jsval_number(n), rval, ret);
2361 if(is_string(rval) && is_number(lval)) {
2362 double n;
2363 HRESULT hres;
2365 hres = to_number(ctx, rval, &n);
2366 if(FAILED(hres))
2367 return hres;
2369 /* FIXME: optimize */
2370 return equal_values(ctx, lval, jsval_number(n), ret);
2373 if(is_bool(rval))
2374 return equal_values(ctx, lval, jsval_number(get_bool(rval) ? 1 : 0), ret);
2376 if(is_bool(lval))
2377 return equal_values(ctx, jsval_number(get_bool(lval) ? 1 : 0), rval, ret);
2380 if(is_object_instance(rval) && (is_string(lval) || is_number(lval))) {
2381 jsval_t prim;
2382 HRESULT hres;
2384 hres = to_primitive(ctx, rval, &prim, NO_HINT);
2385 if(FAILED(hres))
2386 return hres;
2388 hres = equal_values(ctx, lval, prim, ret);
2389 jsval_release(prim);
2390 return hres;
2394 if(is_object_instance(lval) && (is_string(rval) || is_number(rval))) {
2395 jsval_t prim;
2396 HRESULT hres;
2398 hres = to_primitive(ctx, lval, &prim, NO_HINT);
2399 if(FAILED(hres))
2400 return hres;
2402 hres = equal_values(ctx, prim, rval, ret);
2403 jsval_release(prim);
2404 return hres;
2408 *ret = FALSE;
2409 return S_OK;
2412 /* ECMA-262 3rd Edition 11.9.1 */
2413 static HRESULT interp_eq(script_ctx_t *ctx)
2415 jsval_t l, r;
2416 BOOL b;
2417 HRESULT hres;
2419 r = stack_pop(ctx);
2420 l = stack_pop(ctx);
2422 TRACE("%s == %s\n", debugstr_jsval(l), debugstr_jsval(r));
2424 hres = equal_values(ctx, l, r, &b);
2425 jsval_release(l);
2426 jsval_release(r);
2427 if(FAILED(hres))
2428 return hres;
2430 return stack_push(ctx, jsval_bool(b));
2433 /* ECMA-262 3rd Edition 11.9.2 */
2434 static HRESULT interp_neq(script_ctx_t *ctx)
2436 jsval_t l, r;
2437 BOOL b;
2438 HRESULT hres;
2440 r = stack_pop(ctx);
2441 l = stack_pop(ctx);
2443 TRACE("%s != %s\n", debugstr_jsval(l), debugstr_jsval(r));
2445 hres = equal_values(ctx, l, r, &b);
2446 jsval_release(l);
2447 jsval_release(r);
2448 if(FAILED(hres))
2449 return hres;
2451 return stack_push(ctx, jsval_bool(!b));
2454 /* ECMA-262 3rd Edition 11.9.4 */
2455 static HRESULT interp_eq2(script_ctx_t *ctx)
2457 jsval_t l, r;
2458 BOOL b;
2459 HRESULT hres;
2461 r = stack_pop(ctx);
2462 l = stack_pop(ctx);
2464 TRACE("%s === %s\n", debugstr_jsval(l), debugstr_jsval(r));
2466 hres = jsval_strict_equal(r, l, &b);
2467 jsval_release(l);
2468 jsval_release(r);
2469 if(FAILED(hres))
2470 return hres;
2472 return stack_push(ctx, jsval_bool(b));
2475 /* ECMA-262 3rd Edition 11.9.5 */
2476 static HRESULT interp_neq2(script_ctx_t *ctx)
2478 jsval_t l, r;
2479 BOOL b;
2480 HRESULT hres;
2482 TRACE("\n");
2484 r = stack_pop(ctx);
2485 l = stack_pop(ctx);
2487 hres = jsval_strict_equal(r, l, &b);
2488 jsval_release(l);
2489 jsval_release(r);
2490 if(FAILED(hres))
2491 return hres;
2493 return stack_push(ctx, jsval_bool(!b));
2496 /* ECMA-262 3rd Edition 11.8.5 */
2497 static HRESULT less_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL greater, BOOL *ret)
2499 double ln, rn;
2500 jsval_t l, r;
2501 HRESULT hres;
2503 hres = to_primitive(ctx, lval, &l, NO_HINT);
2504 if(FAILED(hres))
2505 return hres;
2507 hres = to_primitive(ctx, rval, &r, NO_HINT);
2508 if(FAILED(hres)) {
2509 jsval_release(l);
2510 return hres;
2513 if(is_string(l) && is_string(r)) {
2514 *ret = (jsstr_cmp(get_string(l), get_string(r)) < 0) ^ greater;
2515 jsstr_release(get_string(l));
2516 jsstr_release(get_string(r));
2517 return S_OK;
2520 hres = to_number(ctx, l, &ln);
2521 jsval_release(l);
2522 if(SUCCEEDED(hres))
2523 hres = to_number(ctx, r, &rn);
2524 jsval_release(r);
2525 if(FAILED(hres))
2526 return hres;
2528 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2529 return S_OK;
2532 /* ECMA-262 3rd Edition 11.8.1 */
2533 static HRESULT interp_lt(script_ctx_t *ctx)
2535 jsval_t l, r;
2536 BOOL b;
2537 HRESULT hres;
2539 r = stack_pop(ctx);
2540 l = stack_pop(ctx);
2542 TRACE("%s < %s\n", debugstr_jsval(l), debugstr_jsval(r));
2544 hres = less_eval(ctx, l, r, FALSE, &b);
2545 jsval_release(l);
2546 jsval_release(r);
2547 if(FAILED(hres))
2548 return hres;
2550 return stack_push(ctx, jsval_bool(b));
2553 /* ECMA-262 3rd Edition 11.8.1 */
2554 static HRESULT interp_lteq(script_ctx_t *ctx)
2556 jsval_t l, r;
2557 BOOL b;
2558 HRESULT hres;
2560 r = stack_pop(ctx);
2561 l = stack_pop(ctx);
2563 TRACE("%s <= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2565 hres = less_eval(ctx, r, l, TRUE, &b);
2566 jsval_release(l);
2567 jsval_release(r);
2568 if(FAILED(hres))
2569 return hres;
2571 return stack_push(ctx, jsval_bool(b));
2574 /* ECMA-262 3rd Edition 11.8.2 */
2575 static HRESULT interp_gt(script_ctx_t *ctx)
2577 jsval_t l, r;
2578 BOOL b;
2579 HRESULT hres;
2581 r = stack_pop(ctx);
2582 l = stack_pop(ctx);
2584 TRACE("%s > %s\n", debugstr_jsval(l), debugstr_jsval(r));
2586 hres = less_eval(ctx, r, l, FALSE, &b);
2587 jsval_release(l);
2588 jsval_release(r);
2589 if(FAILED(hres))
2590 return hres;
2592 return stack_push(ctx, jsval_bool(b));
2595 /* ECMA-262 3rd Edition 11.8.4 */
2596 static HRESULT interp_gteq(script_ctx_t *ctx)
2598 jsval_t l, r;
2599 BOOL b;
2600 HRESULT hres;
2602 r = stack_pop(ctx);
2603 l = stack_pop(ctx);
2605 TRACE("%s >= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2607 hres = less_eval(ctx, l, r, TRUE, &b);
2608 jsval_release(l);
2609 jsval_release(r);
2610 if(FAILED(hres))
2611 return hres;
2613 return stack_push(ctx, jsval_bool(b));
2616 /* ECMA-262 3rd Edition 11.4.8 */
2617 static HRESULT interp_bneg(script_ctx_t *ctx)
2619 jsval_t v;
2620 INT i;
2621 HRESULT hres;
2623 TRACE("\n");
2625 v = stack_pop(ctx);
2626 hres = to_int32(ctx, v, &i);
2627 jsval_release(v);
2628 if(FAILED(hres))
2629 return hres;
2631 return stack_push(ctx, jsval_number(~i));
2634 /* ECMA-262 3rd Edition 11.4.9 */
2635 static HRESULT interp_neg(script_ctx_t *ctx)
2637 jsval_t v;
2638 BOOL b;
2639 HRESULT hres;
2641 TRACE("\n");
2643 v = stack_pop(ctx);
2644 hres = to_boolean(v, &b);
2645 jsval_release(v);
2646 if(FAILED(hres))
2647 return hres;
2649 return stack_push(ctx, jsval_bool(!b));
2652 /* ECMA-262 3rd Edition 11.7.1 */
2653 static HRESULT interp_lshift(script_ctx_t *ctx)
2655 UINT32 r;
2656 INT l;
2657 HRESULT hres;
2659 hres = stack_pop_uint(ctx, &r);
2660 if(FAILED(hres))
2661 return hres;
2663 hres = stack_pop_int(ctx, &l);
2664 if(FAILED(hres))
2665 return hres;
2667 return stack_push(ctx, jsval_number(l << (r&0x1f)));
2670 /* ECMA-262 3rd Edition 11.7.2 */
2671 static HRESULT interp_rshift(script_ctx_t *ctx)
2673 UINT32 r;
2674 INT l;
2675 HRESULT hres;
2677 hres = stack_pop_uint(ctx, &r);
2678 if(FAILED(hres))
2679 return hres;
2681 hres = stack_pop_int(ctx, &l);
2682 if(FAILED(hres))
2683 return hres;
2685 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2688 /* ECMA-262 3rd Edition 11.7.3 */
2689 static HRESULT interp_rshift2(script_ctx_t *ctx)
2691 UINT32 r, l;
2692 HRESULT hres;
2694 hres = stack_pop_uint(ctx, &r);
2695 if(FAILED(hres))
2696 return hres;
2698 hres = stack_pop_uint(ctx, &l);
2699 if(FAILED(hres))
2700 return hres;
2702 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2705 /* ECMA-262 3rd Edition 9.8 */
2706 static HRESULT interp_to_string(script_ctx_t *ctx)
2708 jsstr_t *str;
2709 jsval_t v;
2710 HRESULT hres;
2712 v = stack_pop(ctx);
2713 TRACE("%s\n", debugstr_jsval(v));
2714 hres = to_string(ctx, v, &str);
2715 jsval_release(v);
2716 if(FAILED(hres)) {
2717 WARN("failed %08lx\n", hres);
2718 return hres;
2721 return stack_push(ctx, jsval_string(str));
2724 /* ECMA-262 3rd Edition 11.13.1 */
2725 static HRESULT interp_assign(script_ctx_t *ctx)
2727 exprval_t ref;
2728 jsval_t v;
2729 HRESULT hres;
2731 TRACE("\n");
2733 v = stack_pop(ctx);
2735 if(!stack_pop_exprval(ctx, &ref)) {
2736 jsval_release(v);
2737 return JS_E_ILLEGAL_ASSIGN;
2740 hres = exprval_propput(ctx, &ref, v);
2741 exprval_release(&ref);
2742 if(FAILED(hres)) {
2743 jsval_release(v);
2744 return hres;
2747 return stack_push(ctx, v);
2750 /* ECMA-262 3rd Edition 11.13.1 */
2751 static HRESULT interp_set_member(script_ctx_t *ctx)
2753 jsval_t objv, namev, value;
2754 const WCHAR *name;
2755 IDispatch *obj;
2756 HRESULT hres;
2758 value = stack_pop(ctx);
2759 namev = stack_pop(ctx);
2760 assert(is_string(namev));
2761 objv = stack_pop(ctx);
2763 TRACE("%s.%s = %s\n", debugstr_jsval(objv), debugstr_jsval(namev), debugstr_jsval(value));
2765 hres = to_object(ctx, objv, &obj);
2766 jsval_release(objv);
2767 if(SUCCEEDED(hres) && !(name = jsstr_flatten(get_string(namev)))) {
2768 IDispatch_Release(obj);
2769 hres = E_OUTOFMEMORY;
2771 if(SUCCEEDED(hres)) {
2772 hres = disp_propput_name(ctx, obj, name, value);
2773 IDispatch_Release(obj);
2774 jsstr_release(get_string(namev));
2776 if(FAILED(hres)) {
2777 WARN("failed %08lx\n", hres);
2778 jsval_release(value);
2779 return hres;
2782 return stack_push(ctx, value);
2785 /* JScript extension */
2786 static HRESULT interp_assign_call(script_ctx_t *ctx)
2788 const unsigned argc = get_op_uint(ctx, 0);
2789 exprval_t ref;
2790 jsval_t v;
2791 HRESULT hres;
2793 TRACE("%u\n", argc);
2795 if(!stack_topn_exprval(ctx, argc+1, &ref))
2796 return JS_E_ILLEGAL_ASSIGN;
2798 hres = exprval_call(ctx, &ref, DISPATCH_PROPERTYPUT, argc+1, stack_args(ctx, argc+1), NULL);
2799 if(FAILED(hres))
2800 return hres;
2802 v = stack_pop(ctx);
2803 stack_popn(ctx, argc+2);
2804 return stack_push(ctx, v);
2807 static HRESULT interp_undefined(script_ctx_t *ctx)
2809 TRACE("\n");
2811 return stack_push(ctx, jsval_undefined());
2814 static HRESULT interp_jmp(script_ctx_t *ctx)
2816 const unsigned arg = get_op_uint(ctx, 0);
2818 TRACE("%u\n", arg);
2820 jmp_abs(ctx, arg);
2821 return S_OK;
2824 static HRESULT interp_jmp_z(script_ctx_t *ctx)
2826 const unsigned arg = get_op_uint(ctx, 0);
2827 BOOL b;
2828 jsval_t v;
2829 HRESULT hres;
2831 TRACE("\n");
2833 v = stack_pop(ctx);
2834 hres = to_boolean(v, &b);
2835 jsval_release(v);
2836 if(FAILED(hres))
2837 return hres;
2839 if(b)
2840 jmp_next(ctx);
2841 else
2842 jmp_abs(ctx, arg);
2843 return S_OK;
2846 static HRESULT interp_pop(script_ctx_t *ctx)
2848 const unsigned arg = get_op_uint(ctx, 0);
2850 TRACE("%u\n", arg);
2852 stack_popn(ctx, arg);
2853 return S_OK;
2856 static HRESULT interp_ret(script_ctx_t *ctx)
2858 const unsigned clear_ret = get_op_uint(ctx, 0);
2859 call_frame_t *frame = ctx->call_ctx;
2861 TRACE("\n");
2863 if(clear_ret)
2864 jsval_release(steal_ret(frame));
2866 if((frame->flags & EXEC_CONSTRUCTOR) && !is_object_instance(frame->ret)) {
2867 jsval_release(frame->ret);
2868 IDispatch_AddRef(frame->this_obj);
2869 frame->ret = jsval_disp(frame->this_obj);
2872 jmp_abs(ctx, -1);
2873 return S_OK;
2876 static HRESULT interp_setret(script_ctx_t *ctx)
2878 call_frame_t *frame = ctx->call_ctx;
2880 TRACE("\n");
2882 jsval_release(frame->ret);
2883 frame->ret = stack_pop(ctx);
2884 return S_OK;
2887 static HRESULT interp_push_acc(script_ctx_t *ctx)
2889 HRESULT hres;
2891 TRACE("\n");
2893 hres = stack_push(ctx, ctx->acc);
2894 if(SUCCEEDED(hres))
2895 ctx->acc = jsval_undefined();
2896 return hres;
2899 typedef HRESULT (*op_func_t)(script_ctx_t*);
2901 static const op_func_t op_funcs[] = {
2902 #define X(x,a,b,c) interp_##x,
2903 OP_LIST
2904 #undef X
2907 static const unsigned op_move[] = {
2908 #define X(a,x,b,c) x,
2909 OP_LIST
2910 #undef X
2913 static void pop_call_frame(script_ctx_t *ctx)
2915 call_frame_t *frame = ctx->call_ctx;
2917 frame->stack_base -= frame->pop_locals + frame->pop_variables;
2919 assert(frame->scope == frame->base_scope);
2921 /* If current scope will be kept alive, we need to transfer local variables to its variable object. */
2922 if(frame->scope && frame->scope->ref > 1) {
2923 HRESULT hres = detach_variable_object(ctx, frame, TRUE);
2924 if(FAILED(hres))
2925 ERR("Failed to detach variable object: %08lx\n", hres);
2928 if(frame->arguments_obj)
2929 detach_arguments_object(frame->arguments_obj);
2930 if(frame->scope)
2931 scope_release(frame->scope);
2933 if(frame->pop_variables)
2934 stack_popn(ctx, frame->pop_variables);
2935 stack_popn(ctx, frame->pop_locals);
2937 ctx->call_ctx = frame->prev_frame;
2939 if(frame->function_instance)
2940 jsdisp_release(frame->function_instance);
2941 if(frame->variable_obj)
2942 jsdisp_release(frame->variable_obj);
2943 if(frame->this_obj)
2944 IDispatch_Release(frame->this_obj);
2945 jsval_release(frame->ret);
2946 release_bytecode(frame->bytecode);
2947 heap_free(frame);
2950 static void print_backtrace(script_ctx_t *ctx)
2952 unsigned depth = 0, i, line, char_pos;
2953 call_frame_t *frame;
2955 for(frame = ctx->call_ctx; frame; frame = frame->prev_frame) {
2956 WARN("%u\t", depth);
2957 depth++;
2959 if(frame->this_obj)
2960 WARN("%p->", frame->this_obj);
2961 WARN("%s(", frame->function->name ? debugstr_w(frame->function->name) : "[unnamed]");
2962 if(frame->base_scope && frame->base_scope->frame) {
2963 for(i=0; i < frame->argc; i++) {
2964 if(i < frame->function->param_cnt)
2965 WARN("%s%s=%s", i ? ", " : "", debugstr_w(frame->function->params[i]),
2966 debugstr_jsval(ctx->stack[local_off(frame, -i-1)]));
2967 else
2968 WARN("%s%s", i ? ", " : "", debugstr_jsval(ctx->stack[local_off(frame, -i-1)]));
2970 }else {
2971 WARN("[detached frame]");
2973 line = get_location_line(frame->bytecode, frame->bytecode->instrs[frame->ip].loc, &char_pos);
2974 WARN(") context %s line %u char %u\n", wine_dbgstr_longlong(frame->bytecode->source_context), line, char_pos);
2976 if(!(frame->flags & EXEC_RETURN_TO_INTERP)) {
2977 WARN("%u\t[native code]\n", depth);
2978 depth++;
2983 static HRESULT unwind_exception(script_ctx_t *ctx, HRESULT exception_hres)
2985 except_frame_t *except_frame;
2986 jsexcept_t *ei = ctx->ei;
2987 call_frame_t *frame;
2988 jsval_t except_val;
2989 unsigned catch_off;
2990 HRESULT hres;
2992 if(WARN_ON(jscript)) {
2993 jsdisp_t *error_obj;
2994 jsval_t msg;
2996 WARN("Exception %08lx %s", exception_hres, debugstr_jsval(ei->valid_value ? ei->value : jsval_undefined()));
2997 if(ei->valid_value && jsval_type(ei->value) == JSV_OBJECT) {
2998 error_obj = to_jsdisp(get_object(ei->value));
2999 if(error_obj) {
3000 hres = jsdisp_propget_name(error_obj, L"message", &msg);
3001 if(SUCCEEDED(hres)) {
3002 WARN(" (message %s)", debugstr_jsval(msg));
3003 jsval_release(msg);
3007 WARN(" in:\n");
3009 print_backtrace(ctx);
3012 frame = ctx->call_ctx;
3013 if(exception_hres != DISP_E_EXCEPTION)
3014 throw_error(ctx, exception_hres, NULL);
3015 set_error_location(ei, frame->bytecode, frame->bytecode->instrs[frame->ip].loc, IDS_RUNTIME_ERROR, NULL);
3017 while(!frame->except_frame) {
3018 DWORD flags;
3020 while(frame->scope != frame->base_scope)
3021 scope_pop(&frame->scope);
3023 stack_popn(ctx, ctx->stack_top-frame->stack_base);
3025 flags = frame->flags;
3026 pop_call_frame(ctx);
3027 if(!(flags & EXEC_RETURN_TO_INTERP))
3028 return DISP_E_EXCEPTION;
3029 frame = ctx->call_ctx;
3032 except_frame = frame->except_frame;
3033 catch_off = except_frame->catch_off;
3035 assert(except_frame->stack_top <= ctx->stack_top);
3036 stack_popn(ctx, ctx->stack_top - except_frame->stack_top);
3038 while(except_frame->scope != frame->scope)
3039 scope_pop(&frame->scope);
3041 frame->ip = catch_off ? catch_off : except_frame->finally_off;
3042 assert(!catch_off || frame->bytecode->instrs[frame->ip].op == OP_enter_catch);
3044 if(ei->valid_value) {
3045 except_val = ctx->ei->value;
3046 ei->valid_value = FALSE;
3047 }else {
3048 jsdisp_t *err;
3049 if(!(err = create_builtin_error(ctx)))
3050 return E_OUTOFMEMORY;
3051 except_val = jsval_obj(err);
3054 /* keep current except_frame if we're entering catch block with finally block associated */
3055 if(catch_off && except_frame->finally_off) {
3056 except_frame->catch_off = 0;
3057 }else {
3058 frame->except_frame = except_frame->next;
3059 heap_free(except_frame);
3062 hres = stack_push(ctx, except_val);
3063 if(FAILED(hres))
3064 return hres;
3066 if(!catch_off)
3067 hres = stack_push(ctx, jsval_bool(FALSE));
3068 return hres;
3071 static HRESULT enter_bytecode(script_ctx_t *ctx, jsval_t *r)
3073 call_frame_t *frame;
3074 jsop_t op;
3075 HRESULT hres = S_OK;
3077 TRACE("\n");
3079 while(1) {
3080 frame = ctx->call_ctx;
3081 op = frame->bytecode->instrs[frame->ip].op;
3082 hres = op_funcs[op](ctx);
3083 if(FAILED(hres)) {
3084 hres = unwind_exception(ctx, hres);
3085 if(FAILED(hres))
3086 return hres;
3087 }else if(frame->ip == -1) {
3088 const DWORD return_to_interp = frame->flags & EXEC_RETURN_TO_INTERP;
3090 assert(ctx->stack_top == frame->stack_base);
3091 assert(frame->scope == frame->base_scope);
3093 if(return_to_interp) {
3094 jsval_release(ctx->acc);
3095 ctx->acc = steal_ret(frame);
3096 }else if(r) {
3097 *r = steal_ret(frame);
3099 pop_call_frame(ctx);
3100 if(!return_to_interp)
3101 break;
3102 }else {
3103 frame->ip += op_move[op];
3107 return S_OK;
3110 static HRESULT bind_event_target(script_ctx_t *ctx, function_code_t *func, jsdisp_t *func_obj)
3112 IBindEventHandler *target;
3113 exprval_t exprval;
3114 IDispatch *disp;
3115 jsval_t v;
3116 HRESULT hres;
3118 hres = identifier_eval(ctx, func->event_target, &exprval);
3119 if(FAILED(hres))
3120 return hres;
3122 hres = exprval_to_value(ctx, &exprval, &v);
3123 if(FAILED(hres))
3124 return hres;
3126 if(!is_object_instance(v)) {
3127 FIXME("Can't bind to %s\n", debugstr_jsval(v));
3128 jsval_release(v);
3131 disp = get_object(v);
3132 hres = IDispatch_QueryInterface(disp, &IID_IBindEventHandler, (void**)&target);
3133 if(SUCCEEDED(hres)) {
3134 hres = IBindEventHandler_BindHandler(target, func->name, (IDispatch*)&func_obj->IDispatchEx_iface);
3135 IBindEventHandler_Release(target);
3136 if(FAILED(hres))
3137 WARN("BindEvent failed: %08lx\n", hres);
3138 }else {
3139 FIXME("No IBindEventHandler, not yet supported binding\n");
3142 IDispatch_Release(disp);
3143 return hres;
3146 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)
3148 const unsigned orig_stack = ctx->stack_top;
3149 scope_chain_t *scope;
3150 unsigned i;
3151 jsval_t v;
3152 HRESULT hres;
3154 /* If arguments are already on the stack, we may use them. */
3155 if(argv + argc == ctx->stack + ctx->stack_top) {
3156 frame->arguments_off = argv - ctx->stack;
3157 i = argc;
3158 }else {
3159 frame->arguments_off = ctx->stack_top;
3160 for(i = 0; i < argc; i++) {
3161 hres = jsval_copy(argv[i], &v);
3162 if(SUCCEEDED(hres))
3163 hres = stack_push(ctx, v);
3164 if(FAILED(hres)) {
3165 stack_popn(ctx, i);
3166 return hres;
3171 /* If fewer than declared arguments were passed, fill remaining with undefined value. */
3172 for(; i < frame->function->param_cnt; i++) {
3173 hres = stack_push(ctx, jsval_undefined());
3174 if(FAILED(hres)) {
3175 stack_popn(ctx, ctx->stack_top - orig_stack);
3176 return hres;
3180 frame->pop_locals = ctx->stack_top - orig_stack;
3182 frame->variables_off = ctx->stack_top;
3184 for(i = 0; i < frame->function->var_cnt; i++) {
3185 hres = stack_push(ctx, jsval_undefined());
3186 if(FAILED(hres)) {
3187 stack_popn(ctx, ctx->stack_top - orig_stack);
3188 return hres;
3192 frame->pop_variables = i;
3194 hres = scope_push(scope_chain, variable_object, to_disp(variable_object), &scope);
3195 if(FAILED(hres)) {
3196 stack_popn(ctx, ctx->stack_top - orig_stack);
3197 return hres;
3200 for(i = 0; i < frame->function->func_cnt; i++) {
3201 if(frame->function->funcs[i].local_ref != INVALID_LOCAL_REF
3202 && !frame->function->funcs[i].scope_index)
3204 jsdisp_t *func_obj;
3205 unsigned off;
3207 hres = create_source_function(ctx, frame->bytecode, frame->function->funcs+i, scope, &func_obj);
3208 if(FAILED(hres)) {
3209 stack_popn(ctx, ctx->stack_top - orig_stack);
3210 scope_release(scope);
3211 return hres;
3214 off = local_off(frame, frame->function->funcs[i].local_ref);
3215 jsval_release(ctx->stack[off]);
3216 ctx->stack[off] = jsval_obj(func_obj);
3220 scope->frame = frame;
3221 frame->base_scope = frame->scope = scope;
3222 return S_OK;
3225 HRESULT exec_source(script_ctx_t *ctx, DWORD flags, bytecode_t *bytecode, function_code_t *function, scope_chain_t *scope,
3226 IDispatch *this_obj, jsdisp_t *function_instance, unsigned argc, jsval_t *argv, jsval_t *r)
3228 jsdisp_t *variable_obj;
3229 call_frame_t *frame;
3230 unsigned i;
3231 HRESULT hres;
3233 if(!ctx->stack) {
3234 ctx->stack = heap_alloc(stack_size * sizeof(*ctx->stack));
3235 if(!ctx->stack)
3236 return E_OUTOFMEMORY;
3239 if(bytecode->named_item) {
3240 if(!bytecode->named_item->script_obj) {
3241 hres = create_named_item_script_obj(ctx, bytecode->named_item);
3242 if(FAILED(hres)) return hres;
3246 if(!ctx->ei->enter_notified) {
3247 ctx->ei->enter_notified = TRUE;
3248 IActiveScriptSite_OnEnterScript(ctx->site);
3251 for(i = 0; i < function->func_cnt; i++) {
3252 jsdisp_t *func_obj;
3254 if(!function->funcs[i].event_target)
3255 continue;
3257 if (function->funcs[i].scope_index)
3259 /* TODO: Add tests and handle in interp_push_scope(). */
3260 FIXME("Event target with scope index are not properly handled.\n");
3263 hres = create_source_function(ctx, bytecode, function->funcs+i, scope, &func_obj);
3264 if(FAILED(hres))
3265 return hres;
3267 hres = bind_event_target(ctx, function->funcs+i, func_obj);
3268 jsdisp_release(func_obj);
3269 if(FAILED(hres))
3270 return hres;
3273 if((flags & EXEC_EVAL) && ctx->call_ctx) {
3274 variable_obj = jsdisp_addref(ctx->call_ctx->variable_obj);
3275 }else if(!(flags & (EXEC_GLOBAL | EXEC_EVAL))) {
3276 hres = create_dispex(ctx, NULL, NULL, &variable_obj);
3277 if(FAILED(hres)) return hres;
3278 }else if(bytecode->named_item) {
3279 variable_obj = jsdisp_addref(bytecode->named_item->script_obj);
3280 }else {
3281 variable_obj = jsdisp_addref(ctx->global);
3284 if(flags & (EXEC_GLOBAL | EXEC_EVAL)) {
3285 named_item_t *item = bytecode->named_item;
3286 DISPID id;
3288 for(i=0; i < function->var_cnt; i++) {
3289 TRACE("[%d] %s %d\n", i, debugstr_w(function->variables[i].name), function->variables[i].func_id);
3290 if(function->variables[i].func_id != -1) {
3291 jsdisp_t *func_obj;
3293 if (function->funcs[function->variables[i].func_id].scope_index && flags & EXEC_EVAL)
3295 /* TODO: Add tests and handle in interp_push_scope(). */
3296 FIXME("Functions with scope index inside eval() are not properly handled.\n");
3299 hres = create_source_function(ctx, bytecode, function->funcs+function->variables[i].func_id, scope, &func_obj);
3300 if(FAILED(hres))
3301 goto fail;
3303 hres = jsdisp_propput_name(variable_obj, function->variables[i].name, jsval_obj(func_obj));
3304 jsdisp_release(func_obj);
3305 continue;
3308 if(item && !(item->flags & SCRIPTITEM_CODEONLY)
3309 && SUCCEEDED(disp_get_id(ctx, item->disp, function->variables[i].name, function->variables[i].name, 0, &id)))
3310 continue;
3312 if(!item && (flags & EXEC_GLOBAL) && lookup_global_members(ctx, function->variables[i].name, NULL))
3313 continue;
3315 hres = jsdisp_get_id(variable_obj, function->variables[i].name, fdexNameEnsure, &id);
3316 if(FAILED(hres))
3317 goto fail;
3321 if(this_obj) {
3322 jsdisp_t *jsthis = to_jsdisp(this_obj);
3324 if(jsthis && jsthis->builtin_info->class == JSCLASS_GLOBAL)
3325 this_obj = NULL;
3328 if(ctx->call_ctx && (flags & EXEC_EVAL)) {
3329 hres = detach_variable_object(ctx, ctx->call_ctx, FALSE);
3330 if(FAILED(hres))
3331 goto fail;
3334 frame = heap_alloc_zero(sizeof(*frame));
3335 if(!frame) {
3336 hres = E_OUTOFMEMORY;
3337 goto fail;
3340 frame->function = function;
3341 frame->ret = jsval_undefined();
3342 frame->argc = argc;
3343 frame->bytecode = bytecode_addref(bytecode);
3345 if(!(flags & (EXEC_GLOBAL|EXEC_EVAL))) {
3346 hres = setup_scope(ctx, frame, scope, variable_obj, argc, argv);
3347 if(FAILED(hres)) {
3348 release_bytecode(frame->bytecode);
3349 heap_free(frame);
3350 goto fail;
3352 }else if(scope) {
3353 frame->base_scope = frame->scope = scope_addref(scope);
3356 frame->ip = function->instr_off;
3357 frame->stack_base = ctx->stack_top;
3358 if(this_obj) {
3359 frame->this_obj = this_obj;
3360 IDispatch_AddRef(frame->this_obj);
3363 if(function_instance)
3364 frame->function_instance = jsdisp_addref(function_instance);
3366 frame->flags = flags;
3367 frame->variable_obj = variable_obj;
3369 frame->prev_frame = ctx->call_ctx;
3370 ctx->call_ctx = frame;
3372 if(flags & EXEC_RETURN_TO_INTERP) {
3374 * We're called directly from interpreter, so we may just setup call frame and return.
3375 * Already running interpreter will take care of execution.
3377 if(r)
3378 *r = jsval_undefined();
3379 return S_OK;
3382 return enter_bytecode(ctx, r);
3384 fail:
3385 jsdisp_release(variable_obj);
3386 return hres;