[JSCRIPT] Sync with Wine 3.0. CORE-14225
[reactos.git] / dll / win32 / jscript / engine.c
1 /*
2 * Copyright 2008,2011 Jacek Caban for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "jscript.h"
20
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};
28
29 struct _except_frame_t {
30 unsigned stack_top;
31 scope_chain_t *scope;
32 unsigned catch_off;
33 unsigned finally_off;
34
35 except_frame_t *next;
36 };
37
38 typedef struct {
39 enum {
40 EXPRVAL_JSVAL,
41 EXPRVAL_IDREF,
42 EXPRVAL_STACK_REF,
43 EXPRVAL_INVALID
44 } type;
45 union {
46 jsval_t val;
47 struct {
48 IDispatch *disp;
49 DISPID id;
50 } idref;
51 unsigned off;
52 HRESULT hres;
53 } u;
54 } exprval_t;
55
56 static HRESULT stack_push(script_ctx_t *ctx, jsval_t v)
57 {
58 if(!ctx->stack_size) {
59 ctx->stack = heap_alloc(16*sizeof(*ctx->stack));
60 if(!ctx->stack)
61 return E_OUTOFMEMORY;
62 ctx->stack_size = 16;
63 }else if(ctx->stack_size == ctx->stack_top) {
64 jsval_t *new_stack;
65
66 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*new_stack));
67 if(!new_stack) {
68 jsval_release(v);
69 return E_OUTOFMEMORY;
70 }
71
72 ctx->stack = new_stack;
73 ctx->stack_size *= 2;
74 }
75
76 ctx->stack[ctx->stack_top++] = v;
77 return S_OK;
78 }
79
80 static inline HRESULT stack_push_string(script_ctx_t *ctx, const WCHAR *str)
81 {
82 jsstr_t *v;
83
84 v = jsstr_alloc(str);
85 if(!v)
86 return E_OUTOFMEMORY;
87
88 return stack_push(ctx, jsval_string(v));
89 }
90
91 static inline jsval_t stack_top(script_ctx_t *ctx)
92 {
93 assert(ctx->stack_top > ctx->call_ctx->stack_base);
94 return ctx->stack[ctx->stack_top-1];
95 }
96
97 static inline jsval_t *stack_top_ref(script_ctx_t *ctx, unsigned n)
98 {
99 assert(ctx->stack_top > ctx->call_ctx->stack_base+n);
100 return ctx->stack+ctx->stack_top-1-n;
101 }
102
103 static inline jsval_t stack_topn(script_ctx_t *ctx, unsigned n)
104 {
105 return *stack_top_ref(ctx, n);
106 }
107
108 static inline jsval_t *stack_args(script_ctx_t *ctx, unsigned n)
109 {
110 if(!n)
111 return NULL;
112 assert(ctx->stack_top > ctx->call_ctx->stack_base+n-1);
113 return ctx->stack + ctx->stack_top-n;
114 }
115
116 static inline jsval_t stack_pop(script_ctx_t *ctx)
117 {
118 assert(ctx->stack_top > ctx->call_ctx->stack_base);
119 return ctx->stack[--ctx->stack_top];
120 }
121
122 static void stack_popn(script_ctx_t *ctx, unsigned n)
123 {
124 while(n--)
125 jsval_release(stack_pop(ctx));
126 }
127
128 static HRESULT stack_pop_number(script_ctx_t *ctx, double *r)
129 {
130 jsval_t v;
131 HRESULT hres;
132
133 v = stack_pop(ctx);
134 hres = to_number(ctx, v, r);
135 jsval_release(v);
136 return hres;
137 }
138
139 static HRESULT stack_pop_object(script_ctx_t *ctx, IDispatch **r)
140 {
141 jsval_t v;
142 HRESULT hres;
143
144 v = stack_pop(ctx);
145 if(is_object_instance(v)) {
146 if(!get_object(v))
147 return throw_type_error(ctx, JS_E_OBJECT_REQUIRED, NULL);
148 *r = get_object(v);
149 return S_OK;
150 }
151
152 hres = to_object(ctx, v, r);
153 jsval_release(v);
154 return hres;
155 }
156
157 static inline HRESULT stack_pop_int(script_ctx_t *ctx, INT *r)
158 {
159 return to_int32(ctx, stack_pop(ctx), r);
160 }
161
162 static inline HRESULT stack_pop_uint(script_ctx_t *ctx, DWORD *r)
163 {
164 return to_uint32(ctx, stack_pop(ctx), r);
165 }
166
167 static inline unsigned local_off(call_frame_t *frame, int ref)
168 {
169 return ref < 0
170 ? frame->arguments_off - ref-1
171 : frame->variables_off + ref;
172 }
173
174 static inline BSTR local_name(call_frame_t *frame, int ref)
175 {
176 return ref < 0 ? frame->function->params[-ref-1] : frame->function->variables[ref].name;
177 }
178
179 /* Steals input reference even on failure. */
180 static HRESULT stack_push_exprval(script_ctx_t *ctx, exprval_t *val)
181 {
182 HRESULT hres;
183
184 switch(val->type) {
185 case EXPRVAL_JSVAL:
186 assert(0);
187 case EXPRVAL_IDREF:
188 hres = stack_push(ctx, jsval_disp(val->u.idref.disp));
189 if(SUCCEEDED(hres))
190 hres = stack_push(ctx, jsval_number(val->u.idref.id));
191 else
192 IDispatch_Release(val->u.idref.disp);
193 return hres;
194 case EXPRVAL_STACK_REF:
195 hres = stack_push(ctx, jsval_number(val->u.off));
196 if(SUCCEEDED(hres))
197 hres = stack_push(ctx, jsval_undefined());
198 return hres;
199 case EXPRVAL_INVALID:
200 hres = stack_push(ctx, jsval_undefined());
201 if(SUCCEEDED(hres))
202 hres = stack_push(ctx, jsval_number(val->u.hres));
203 return hres;
204 }
205
206 assert(0);
207 return E_FAIL;
208 }
209
210 static BOOL stack_topn_exprval(script_ctx_t *ctx, unsigned n, exprval_t *r)
211 {
212 jsval_t v = stack_topn(ctx, n+1);
213
214 switch(jsval_type(v)) {
215 case JSV_NUMBER: {
216 call_frame_t *frame = ctx->call_ctx;
217 unsigned off = get_number(v);
218
219 if(!frame->base_scope->frame && off >= frame->arguments_off) {
220 DISPID id;
221 BSTR name;
222 HRESULT hres;
223
224 /* Got stack reference in deoptimized code. Need to convert it back to variable object reference. */
225
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);
231 if(FAILED(hres)) {
232 r->type = EXPRVAL_INVALID;
233 r->u.hres = hres;
234 return FALSE;
235 }
236
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;
241 r->u.idref.id = id;
242 return TRUE;
243 }
244
245 r->type = EXPRVAL_STACK_REF;
246 r->u.off = off;
247 return TRUE;
248 }
249 case JSV_OBJECT:
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));
254 return TRUE;
255 case JSV_UNDEFINED:
256 r->type = EXPRVAL_INVALID;
257 assert(is_number(stack_topn(ctx, n)));
258 r->u.hres = get_number(stack_topn(ctx, n));
259 return FALSE;
260 default:
261 assert(0);
262 return FALSE;
263 }
264 }
265
266 static inline BOOL stack_pop_exprval(script_ctx_t *ctx, exprval_t *r)
267 {
268 BOOL ret = stack_topn_exprval(ctx, 0, r);
269 ctx->stack_top -= 2;
270 return ret;
271 }
272
273 static HRESULT exprval_propput(script_ctx_t *ctx, exprval_t *ref, jsval_t v)
274 {
275 switch(ref->type) {
276 case EXPRVAL_STACK_REF: {
277 jsval_t *r = ctx->stack + ref->u.off;
278 jsval_release(*r);
279 return jsval_copy(v, r);
280 }
281 case EXPRVAL_IDREF:
282 return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v);
283 default:
284 assert(0);
285 return E_FAIL;
286 }
287 }
288
289 static HRESULT exprval_propget(script_ctx_t *ctx, exprval_t *ref, jsval_t *r)
290 {
291 switch(ref->type) {
292 case EXPRVAL_STACK_REF:
293 return jsval_copy(ctx->stack[ref->u.off], r);
294 case EXPRVAL_IDREF:
295 return disp_propget(ctx, ref->u.idref.disp, ref->u.idref.id, r);
296 default:
297 assert(0);
298 return E_FAIL;
299 }
300 }
301
302 static HRESULT exprval_call(script_ctx_t *ctx, exprval_t *ref, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
303 {
304 switch(ref->type) {
305 case EXPRVAL_STACK_REF: {
306 jsval_t v = ctx->stack[ref->u.off];
307
308 if(!is_object_instance(v)) {
309 FIXME("invoke %s\n", debugstr_jsval(v));
310 return E_FAIL;
311 }
312
313 return disp_call_value(ctx, get_object(v), NULL, flags, argc, argv, r);
314 }
315 case EXPRVAL_IDREF:
316 return disp_call(ctx, ref->u.idref.disp, ref->u.idref.id, flags, argc, argv, r);
317 default:
318 assert(0);
319 return E_FAIL;
320 }
321 }
322
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)
326 {
327 HRESULT hres;
328
329 if(ref->type == EXPRVAL_JSVAL) {
330 *r = ref->u.val;
331 return S_OK;
332 }
333
334 hres = exprval_propget(ctx, ref, r);
335
336 if(ref->type == EXPRVAL_IDREF)
337 IDispatch_Release(ref->u.idref.disp);
338 return hres;
339 }
340
341 static void exprval_release(exprval_t *val)
342 {
343 switch(val->type) {
344 case EXPRVAL_JSVAL:
345 jsval_release(val->u.val);
346 return;
347 case EXPRVAL_IDREF:
348 if(val->u.idref.disp)
349 IDispatch_Release(val->u.idref.disp);
350 return;
351 case EXPRVAL_STACK_REF:
352 case EXPRVAL_INVALID:
353 return;
354 }
355 }
356
357 static inline void exprval_set_exception(exprval_t *val, HRESULT hres)
358 {
359 val->type = EXPRVAL_INVALID;
360 val->u.hres = hres;
361 }
362
363 static inline void exprval_set_disp_ref(exprval_t *ref, IDispatch *obj, DISPID id)
364 {
365 ref->type = EXPRVAL_IDREF;
366 #ifdef __REACTOS__ /* FIXME: Inspect */
367 IDispatch_AddRef(obj);
368 ref->u.idref.disp = obj;
369 #else
370 IDispatch_AddRef(ref->u.idref.disp = obj);
371 #endif
372 ref->u.idref.id = id;
373 }
374
375 static inline jsval_t steal_ret(call_frame_t *frame)
376 {
377 jsval_t r = frame->ret;
378 frame->ret = jsval_undefined();
379 return r;
380 }
381
382 static inline void clear_ret(call_frame_t *frame)
383 {
384 jsval_release(steal_ret(frame));
385 }
386
387 static HRESULT scope_push(scope_chain_t *scope, jsdisp_t *jsobj, IDispatch *obj, scope_chain_t **ret)
388 {
389 scope_chain_t *new_scope;
390
391 new_scope = heap_alloc(sizeof(scope_chain_t));
392 if(!new_scope)
393 return E_OUTOFMEMORY;
394
395 new_scope->ref = 1;
396
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;
402
403 *ret = new_scope;
404 return S_OK;
405 }
406
407 static void scope_pop(scope_chain_t **scope)
408 {
409 scope_chain_t *tmp;
410
411 tmp = *scope;
412 *scope = tmp->next;
413 scope_release(tmp);
414 }
415
416 void clear_ei(script_ctx_t *ctx)
417 {
418 memset(&ctx->ei.ei, 0, sizeof(ctx->ei.ei));
419 jsval_release(ctx->ei.val);
420 ctx->ei.val = jsval_undefined();
421 }
422
423 void scope_release(scope_chain_t *scope)
424 {
425 if(--scope->ref)
426 return;
427
428 if(scope->next)
429 scope_release(scope->next);
430
431 IDispatch_Release(scope->obj);
432 heap_free(scope);
433 }
434
435 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, const WCHAR *name, BSTR name_bstr, DWORD flags, DISPID *id)
436 {
437 IDispatchEx *dispex;
438 jsdisp_t *jsdisp;
439 BSTR bstr;
440 HRESULT hres;
441
442 jsdisp = iface_to_jsdisp(disp);
443 if(jsdisp) {
444 hres = jsdisp_get_id(jsdisp, name, flags, id);
445 jsdisp_release(jsdisp);
446 return hres;
447 }
448
449 if(name_bstr) {
450 bstr = name_bstr;
451 }else {
452 bstr = SysAllocString(name);
453 if(!bstr)
454 return E_OUTOFMEMORY;
455 }
456
457 *id = 0;
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);
462 }else {
463 TRACE("using IDispatch\n");
464 hres = IDispatch_GetIDsOfNames(disp, &IID_NULL, &bstr, 1, 0, id);
465 }
466
467 if(name_bstr != bstr)
468 SysFreeString(bstr);
469 return hres;
470 }
471
472 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
473 {
474 IObjectIdentity *identity;
475 IUnknown *unk1, *unk2;
476 HRESULT hres;
477
478 if(disp1 == disp2) {
479 *ret = TRUE;
480 return S_OK;
481 }
482
483 if(!disp1 || !disp2) {
484 *ret = FALSE;
485 return S_OK;
486 }
487
488 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
489 if(FAILED(hres))
490 return hres;
491
492 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
493 if(FAILED(hres)) {
494 IUnknown_Release(unk1);
495 return hres;
496 }
497
498 if(unk1 == unk2) {
499 *ret = TRUE;
500 }else {
501 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
502 if(SUCCEEDED(hres)) {
503 hres = IObjectIdentity_IsEqualObject(identity, unk2);
504 IObjectIdentity_Release(identity);
505 *ret = hres == S_OK;
506 }else {
507 *ret = FALSE;
508 }
509 }
510
511 IUnknown_Release(unk1);
512 IUnknown_Release(unk2);
513 return S_OK;
514 }
515
516 /* ECMA-262 3rd Edition 11.9.6 */
517 static HRESULT equal2_values(jsval_t lval, jsval_t rval, BOOL *ret)
518 {
519 jsval_type_t type = jsval_type(lval);
520
521 TRACE("\n");
522
523 if(type != jsval_type(rval)) {
524 if(is_null_instance(lval))
525 *ret = is_null_instance(rval);
526 else
527 *ret = FALSE;
528 return S_OK;
529 }
530
531 switch(type) {
532 case JSV_UNDEFINED:
533 case JSV_NULL:
534 *ret = TRUE;
535 break;
536 case JSV_OBJECT:
537 return disp_cmp(get_object(lval), get_object(rval), ret);
538 case JSV_STRING:
539 *ret = jsstr_eq(get_string(lval), get_string(rval));
540 break;
541 case JSV_NUMBER:
542 *ret = get_number(lval) == get_number(rval);
543 break;
544 case JSV_BOOL:
545 *ret = !get_bool(lval) == !get_bool(rval);
546 break;
547 case JSV_VARIANT:
548 FIXME("VARIANT not implemented\n");
549 return E_NOTIMPL;
550 }
551
552 return S_OK;
553 }
554
555 /*
556 * Transfers local variables from stack to variable object.
557 * It's slow, so we want to avoid it as much as possible.
558 */
559 static HRESULT detach_variable_object(script_ctx_t *ctx, call_frame_t *frame, BOOL from_release)
560 {
561 unsigned i;
562 HRESULT hres;
563
564 if(!frame->base_scope || !frame->base_scope->frame)
565 return S_OK;
566
567 TRACE("detaching %p\n", frame);
568
569 assert(frame == frame->base_scope->frame);
570 assert(frame->variable_obj == frame->base_scope->jsobj);
571
572 if(!from_release && !frame->arguments_obj) {
573 hres = setup_arguments_object(ctx, frame);
574 if(FAILED(hres))
575 return hres;
576 }
577
578 frame->base_scope->frame = NULL;
579
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)]);
583 if(FAILED(hres))
584 return hres;
585 }
586
587 return S_OK;
588 }
589
590 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
591 {
592 named_item_t *item;
593 DISPID id;
594 HRESULT hres;
595
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)) {
600 if(ret)
601 exprval_set_disp_ref(ret, item->disp, id);
602 return TRUE;
603 }
604 }
605 }
606
607 return FALSE;
608 }
609
610 static int local_ref_cmp(const void *key, const void *ref)
611 {
612 return strcmpW((const WCHAR*)key, ((const local_ref_t*)ref)->name);
613 }
614
615 local_ref_t *lookup_local(const function_code_t *function, const WCHAR *identifier)
616 {
617 return bsearch(identifier, function->locals, function->locals_cnt, sizeof(*function->locals), local_ref_cmp);
618 }
619
620 /* ECMA-262 3rd Edition 10.1.4 */
621 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
622 {
623 scope_chain_t *scope;
624 named_item_t *item;
625 DISPID id = 0;
626 HRESULT hres;
627
628 TRACE("%s\n", debugstr_w(identifier));
629
630 if(ctx->call_ctx) {
631 for(scope = ctx->call_ctx->scope; scope; scope = scope->next) {
632 if(scope->frame) {
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};
636
637 if(ref) {
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);
641 return S_OK;
642 }
643
644 if(!strcmpW(identifier, argumentsW)) {
645 hres = detach_variable_object(ctx, scope->frame, FALSE);
646 if(FAILED(hres))
647 return hres;
648 }
649 }
650 if(scope->jsobj)
651 hres = jsdisp_get_id(scope->jsobj, identifier, fdexNameImplicit, &id);
652 else
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);
656 return S_OK;
657 }
658 }
659 }
660
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);
664 return S_OK;
665 }
666
667 for(item = ctx->named_items; item; item = item->next) {
668 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
669 if(!item->disp) {
670 IUnknown *unk;
671
672 if(!ctx->site)
673 break;
674
675 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
676 SCRIPTINFO_IUNKNOWN, &unk, NULL);
677 if(FAILED(hres)) {
678 WARN("GetItemInfo failed: %08x\n", hres);
679 break;
680 }
681
682 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
683 IUnknown_Release(unk);
684 if(FAILED(hres)) {
685 WARN("object does not implement IDispatch\n");
686 break;
687 }
688 }
689
690 IDispatch_AddRef(item->disp);
691 ret->type = EXPRVAL_JSVAL;
692 ret->u.val = jsval_disp(item->disp);
693 return S_OK;
694 }
695 }
696
697 if(lookup_global_members(ctx, identifier, ret))
698 return S_OK;
699
700 exprval_set_exception(ret, JS_E_UNDEFINED_VARIABLE);
701 return S_OK;
702 }
703
704 static inline BSTR get_op_bstr(script_ctx_t *ctx, int i)
705 {
706 call_frame_t *frame = ctx->call_ctx;
707 return frame->bytecode->instrs[frame->ip].u.arg[i].bstr;
708 }
709
710 static inline unsigned get_op_uint(script_ctx_t *ctx, int i)
711 {
712 call_frame_t *frame = ctx->call_ctx;
713 return frame->bytecode->instrs[frame->ip].u.arg[i].uint;
714 }
715
716 static inline unsigned get_op_int(script_ctx_t *ctx, int i)
717 {
718 call_frame_t *frame = ctx->call_ctx;
719 return frame->bytecode->instrs[frame->ip].u.arg[i].lng;
720 }
721
722 static inline jsstr_t *get_op_str(script_ctx_t *ctx, int i)
723 {
724 call_frame_t *frame = ctx->call_ctx;
725 return frame->bytecode->instrs[frame->ip].u.arg[i].str;
726 }
727
728 static inline double get_op_double(script_ctx_t *ctx)
729 {
730 call_frame_t *frame = ctx->call_ctx;
731 return frame->bytecode->instrs[frame->ip].u.dbl;
732 }
733
734 static inline void jmp_next(script_ctx_t *ctx)
735 {
736 ctx->call_ctx->ip++;
737 }
738
739 static inline void jmp_abs(script_ctx_t *ctx, unsigned dst)
740 {
741 ctx->call_ctx->ip = dst;
742 }
743
744 /* ECMA-262 3rd Edition 12.6.4 */
745 static HRESULT interp_forin(script_ctx_t *ctx)
746 {
747 const HRESULT arg = get_op_uint(ctx, 0);
748 IDispatch *obj = NULL;
749 IDispatchEx *dispex;
750 exprval_t prop_ref;
751 DISPID id;
752 BSTR name = NULL;
753 HRESULT hres;
754
755 TRACE("\n");
756
757 assert(is_number(stack_top(ctx)));
758 id = get_number(stack_top(ctx));
759
760 if(!stack_topn_exprval(ctx, 1, &prop_ref)) {
761 FIXME("invalid ref: %08x\n", prop_ref.u.hres);
762 return E_FAIL;
763 }
764
765 if(is_object_instance(stack_topn(ctx, 3)))
766 obj = get_object(stack_topn(ctx, 3));
767
768 if(obj) {
769 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
770 if(SUCCEEDED(hres)) {
771 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
772 if(hres == S_OK)
773 hres = IDispatchEx_GetMemberName(dispex, id, &name);
774 IDispatchEx_Release(dispex);
775 if(FAILED(hres))
776 return hres;
777 }else {
778 TRACE("No IDispatchEx\n");
779 }
780 }
781
782 if(name) {
783 jsstr_t *str;
784
785 str = jsstr_alloc_len(name, SysStringLen(name));
786 SysFreeString(name);
787 if(!str)
788 return E_OUTOFMEMORY;
789
790 stack_pop(ctx);
791 stack_push(ctx, jsval_number(id)); /* safe, just after pop() */
792
793 hres = exprval_propput(ctx, &prop_ref, jsval_string(str));
794 jsstr_release(str);
795 if(FAILED(hres))
796 return hres;
797
798 jmp_next(ctx);
799 }else {
800 stack_popn(ctx, 4);
801 jmp_abs(ctx, arg);
802 }
803 return S_OK;
804 }
805
806 /* ECMA-262 3rd Edition 12.10 */
807 static HRESULT interp_push_scope(script_ctx_t *ctx)
808 {
809 IDispatch *disp;
810 jsval_t v;
811 HRESULT hres;
812
813 TRACE("\n");
814
815 v = stack_pop(ctx);
816 hres = to_object(ctx, v, &disp);
817 jsval_release(v);
818 if(FAILED(hres))
819 return hres;
820
821 hres = scope_push(ctx->call_ctx->scope, to_jsdisp(disp), disp, &ctx->call_ctx->scope);
822 IDispatch_Release(disp);
823 return hres;
824 }
825
826 /* ECMA-262 3rd Edition 12.10 */
827 static HRESULT interp_pop_scope(script_ctx_t *ctx)
828 {
829 TRACE("\n");
830
831 scope_pop(&ctx->call_ctx->scope);
832 return S_OK;
833 }
834
835 /* ECMA-262 3rd Edition 12.13 */
836 static HRESULT interp_case(script_ctx_t *ctx)
837 {
838 const unsigned arg = get_op_uint(ctx, 0);
839 jsval_t v;
840 BOOL b;
841 HRESULT hres;
842
843 TRACE("\n");
844
845 v = stack_pop(ctx);
846 hres = equal2_values(stack_top(ctx), v, &b);
847 jsval_release(v);
848 if(FAILED(hres))
849 return hres;
850
851 if(b) {
852 stack_popn(ctx, 1);
853 jmp_abs(ctx, arg);
854 }else {
855 jmp_next(ctx);
856 }
857 return S_OK;
858 }
859
860 /* ECMA-262 3rd Edition 12.13 */
861 static HRESULT interp_throw(script_ctx_t *ctx)
862 {
863 TRACE("\n");
864
865 jsval_release(ctx->ei.val);
866 ctx->ei.val = stack_pop(ctx);
867 return DISP_E_EXCEPTION;
868 }
869
870 static HRESULT interp_throw_ref(script_ctx_t *ctx)
871 {
872 const HRESULT arg = get_op_uint(ctx, 0);
873
874 TRACE("%08x\n", arg);
875
876 return throw_reference_error(ctx, arg, NULL);
877 }
878
879 static HRESULT interp_throw_type(script_ctx_t *ctx)
880 {
881 const HRESULT hres = get_op_uint(ctx, 0);
882 jsstr_t *str = get_op_str(ctx, 1);
883 const WCHAR *ptr;
884
885 TRACE("%08x %s\n", hres, debugstr_jsstr(str));
886
887 ptr = jsstr_flatten(str);
888 return ptr ? throw_type_error(ctx, hres, ptr) : E_OUTOFMEMORY;
889 }
890
891 /* ECMA-262 3rd Edition 12.14 */
892 static HRESULT interp_push_except(script_ctx_t *ctx)
893 {
894 const unsigned catch_off = get_op_uint(ctx, 0);
895 const unsigned finally_off = get_op_uint(ctx, 1);
896 call_frame_t *frame = ctx->call_ctx;
897 except_frame_t *except;
898
899 TRACE("\n");
900
901 except = heap_alloc(sizeof(*except));
902 if(!except)
903 return E_OUTOFMEMORY;
904
905 except->stack_top = ctx->stack_top;
906 except->scope = frame->scope;
907 except->catch_off = catch_off;
908 except->finally_off = finally_off;
909 except->next = frame->except_frame;
910 frame->except_frame = except;
911 return S_OK;
912 }
913
914 /* ECMA-262 3rd Edition 12.14 */
915 static HRESULT interp_pop_except(script_ctx_t *ctx)
916 {
917 const unsigned ret_off = get_op_uint(ctx, 0);
918 call_frame_t *frame = ctx->call_ctx;
919 except_frame_t *except;
920 unsigned finally_off;
921
922 TRACE("%u\n", ret_off);
923
924 except = frame->except_frame;
925 assert(except != NULL);
926
927 finally_off = except->finally_off;
928 frame->except_frame = except->next;
929 heap_free(except);
930
931 if(finally_off) {
932 HRESULT hres;
933
934 hres = stack_push(ctx, jsval_number(ret_off));
935 if(FAILED(hres))
936 return hres;
937 hres = stack_push(ctx, jsval_bool(TRUE));
938 if(FAILED(hres))
939 return hres;
940 frame->ip = finally_off;
941 }else {
942 frame->ip = ret_off;
943 }
944
945 return S_OK;
946 }
947
948 /* ECMA-262 3rd Edition 12.14 */
949 static HRESULT interp_end_finally(script_ctx_t *ctx)
950 {
951 call_frame_t *frame = ctx->call_ctx;
952 jsval_t v;
953
954 TRACE("\n");
955
956 v = stack_pop(ctx);
957 assert(is_bool(v));
958
959 if(!get_bool(v)) {
960 TRACE("passing exception\n");
961
962 ctx->ei.val = stack_pop(ctx);
963 return DISP_E_EXCEPTION;
964 }
965
966 v = stack_pop(ctx);
967 assert(is_number(v));
968 frame->ip = get_number(v);
969 return S_OK;
970 }
971
972 static HRESULT interp_enter_catch(script_ctx_t *ctx)
973 {
974 const BSTR ident = get_op_bstr(ctx, 0);
975 jsdisp_t *scope_obj;
976 jsval_t v;
977 HRESULT hres;
978
979 hres = create_dispex(ctx, NULL, NULL, &scope_obj);
980 if(FAILED(hres))
981 return hres;
982
983 v = stack_pop(ctx);
984 hres = jsdisp_propput_name(scope_obj, ident, v);
985 jsval_release(v);
986 if(SUCCEEDED(hres))
987 hres = scope_push(ctx->call_ctx->scope, scope_obj, to_disp(scope_obj), &ctx->call_ctx->scope);
988 jsdisp_release(scope_obj);
989 return hres;
990 }
991
992 /* ECMA-262 3rd Edition 13 */
993 static HRESULT interp_func(script_ctx_t *ctx)
994 {
995 unsigned func_idx = get_op_uint(ctx, 0);
996 call_frame_t *frame = ctx->call_ctx;
997 jsdisp_t *dispex;
998 HRESULT hres;
999
1000 TRACE("%d\n", func_idx);
1001
1002 hres = create_source_function(ctx, frame->bytecode, frame->function->funcs+func_idx,
1003 frame->scope, &dispex);
1004 if(FAILED(hres))
1005 return hres;
1006
1007 return stack_push(ctx, jsval_obj(dispex));
1008 }
1009
1010 /* ECMA-262 3rd Edition 11.2.1 */
1011 static HRESULT interp_array(script_ctx_t *ctx)
1012 {
1013 jsstr_t *name_str;
1014 const WCHAR *name;
1015 jsval_t v, namev;
1016 IDispatch *obj;
1017 DISPID id;
1018 HRESULT hres;
1019
1020 TRACE("\n");
1021
1022 namev = stack_pop(ctx);
1023
1024 hres = stack_pop_object(ctx, &obj);
1025 if(FAILED(hres)) {
1026 jsval_release(namev);
1027 return hres;
1028 }
1029
1030 hres = to_flat_string(ctx, namev, &name_str, &name);
1031 jsval_release(namev);
1032 if(FAILED(hres)) {
1033 IDispatch_Release(obj);
1034 return hres;
1035 }
1036
1037 hres = disp_get_id(ctx, obj, name, NULL, 0, &id);
1038 jsstr_release(name_str);
1039 if(SUCCEEDED(hres)) {
1040 hres = disp_propget(ctx, obj, id, &v);
1041 }else if(hres == DISP_E_UNKNOWNNAME) {
1042 v = jsval_undefined();
1043 hres = S_OK;
1044 }
1045 IDispatch_Release(obj);
1046 if(FAILED(hres))
1047 return hres;
1048
1049 return stack_push(ctx, v);
1050 }
1051
1052 /* ECMA-262 3rd Edition 11.2.1 */
1053 static HRESULT interp_member(script_ctx_t *ctx)
1054 {
1055 const BSTR arg = get_op_bstr(ctx, 0);
1056 IDispatch *obj;
1057 jsval_t v;
1058 DISPID id;
1059 HRESULT hres;
1060
1061 TRACE("\n");
1062
1063 hres = stack_pop_object(ctx, &obj);
1064 if(FAILED(hres))
1065 return hres;
1066
1067 hres = disp_get_id(ctx, obj, arg, arg, 0, &id);
1068 if(SUCCEEDED(hres)) {
1069 hres = disp_propget(ctx, obj, id, &v);
1070 }else if(hres == DISP_E_UNKNOWNNAME) {
1071 v = jsval_undefined();
1072 hres = S_OK;
1073 }
1074 IDispatch_Release(obj);
1075 if(FAILED(hres))
1076 return hres;
1077
1078 return stack_push(ctx, v);
1079 }
1080
1081 /* ECMA-262 3rd Edition 11.2.1 */
1082 static HRESULT interp_memberid(script_ctx_t *ctx)
1083 {
1084 const unsigned arg = get_op_uint(ctx, 0);
1085 jsval_t objv, namev;
1086 const WCHAR *name;
1087 jsstr_t *name_str;
1088 IDispatch *obj;
1089 exprval_t ref;
1090 DISPID id;
1091 HRESULT hres;
1092
1093 TRACE("%x\n", arg);
1094
1095 namev = stack_pop(ctx);
1096 objv = stack_pop(ctx);
1097
1098 hres = to_object(ctx, objv, &obj);
1099 jsval_release(objv);
1100 if(SUCCEEDED(hres)) {
1101 hres = to_flat_string(ctx, namev, &name_str, &name);
1102 if(FAILED(hres))
1103 IDispatch_Release(obj);
1104 }
1105 jsval_release(namev);
1106 if(FAILED(hres))
1107 return hres;
1108
1109 hres = disp_get_id(ctx, obj, name, NULL, arg, &id);
1110 jsstr_release(name_str);
1111 if(SUCCEEDED(hres)) {
1112 ref.type = EXPRVAL_IDREF;
1113 ref.u.idref.disp = obj;
1114 ref.u.idref.id = id;
1115 }else {
1116 IDispatch_Release(obj);
1117 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
1118 exprval_set_exception(&ref, JS_E_INVALID_PROPERTY);
1119 hres = S_OK;
1120 }else {
1121 ERR("failed %08x\n", hres);
1122 return hres;
1123 }
1124 }
1125
1126 return stack_push_exprval(ctx, &ref);
1127 }
1128
1129 /* ECMA-262 3rd Edition 11.2.1 */
1130 static HRESULT interp_refval(script_ctx_t *ctx)
1131 {
1132 exprval_t ref;
1133 jsval_t v;
1134 HRESULT hres;
1135
1136 TRACE("\n");
1137
1138 if(!stack_topn_exprval(ctx, 0, &ref))
1139 return throw_reference_error(ctx, JS_E_ILLEGAL_ASSIGN, NULL);
1140
1141 hres = exprval_propget(ctx, &ref, &v);
1142 if(FAILED(hres))
1143 return hres;
1144
1145 return stack_push(ctx, v);
1146 }
1147
1148 /* ECMA-262 3rd Edition 11.2.2 */
1149 static HRESULT interp_new(script_ctx_t *ctx)
1150 {
1151 const unsigned argc = get_op_uint(ctx, 0);
1152 call_frame_t *frame = ctx->call_ctx;
1153 jsval_t constr;
1154
1155 TRACE("%d\n", argc);
1156
1157 constr = stack_topn(ctx, argc);
1158
1159 /* NOTE: Should use to_object here */
1160
1161 if(is_null(constr))
1162 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
1163 else if(!is_object_instance(constr))
1164 return throw_type_error(ctx, JS_E_INVALID_ACTION, NULL);
1165 else if(!get_object(constr))
1166 return throw_type_error(ctx, JS_E_INVALID_PROPERTY, NULL);
1167
1168 clear_ret(frame);
1169 return disp_call_value(ctx, get_object(constr), NULL, DISPATCH_CONSTRUCT | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1170 argc, stack_args(ctx, argc), &frame->ret);
1171 }
1172
1173 /* ECMA-262 3rd Edition 11.2.3 */
1174 static HRESULT interp_call(script_ctx_t *ctx)
1175 {
1176 const unsigned argn = get_op_uint(ctx, 0);
1177 const int do_ret = get_op_int(ctx, 1);
1178 call_frame_t *frame = ctx->call_ctx;
1179 jsval_t obj;
1180
1181 TRACE("%d %d\n", argn, do_ret);
1182
1183 obj = stack_topn(ctx, argn);
1184 if(!is_object_instance(obj))
1185 return throw_type_error(ctx, JS_E_INVALID_PROPERTY, NULL);
1186
1187 clear_ret(frame);
1188 return disp_call_value(ctx, get_object(obj), NULL, DISPATCH_METHOD | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1189 argn, stack_args(ctx, argn), do_ret ? &frame->ret : NULL);
1190 }
1191
1192 /* ECMA-262 3rd Edition 11.2.3 */
1193 static HRESULT interp_call_member(script_ctx_t *ctx)
1194 {
1195 const unsigned argn = get_op_uint(ctx, 0);
1196 const int do_ret = get_op_int(ctx, 1);
1197 call_frame_t *frame = ctx->call_ctx;
1198 exprval_t ref;
1199
1200 TRACE("%d %d\n", argn, do_ret);
1201
1202 if(!stack_topn_exprval(ctx, argn, &ref))
1203 return throw_type_error(ctx, ref.u.hres, NULL);
1204
1205 clear_ret(frame);
1206 return exprval_call(ctx, &ref, DISPATCH_METHOD | DISPATCH_JSCRIPT_CALLEREXECSSOURCE,
1207 argn, stack_args(ctx, argn), do_ret ? &frame->ret : NULL);
1208 }
1209
1210 /* ECMA-262 3rd Edition 11.1.1 */
1211 static HRESULT interp_this(script_ctx_t *ctx)
1212 {
1213 call_frame_t *frame = ctx->call_ctx;
1214
1215 TRACE("\n");
1216
1217 IDispatch_AddRef(frame->this_obj);
1218 return stack_push(ctx, jsval_disp(frame->this_obj));
1219 }
1220
1221 static HRESULT interp_identifier_ref(script_ctx_t *ctx, BSTR identifier, unsigned flags)
1222 {
1223 exprval_t exprval;
1224 HRESULT hres;
1225
1226 hres = identifier_eval(ctx, identifier, &exprval);
1227 if(FAILED(hres))
1228 return hres;
1229
1230 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1231 DISPID id;
1232
1233 hres = jsdisp_get_id(ctx->global, identifier, fdexNameEnsure, &id);
1234 if(FAILED(hres))
1235 return hres;
1236
1237 exprval_set_disp_ref(&exprval, to_disp(ctx->global), id);
1238 }
1239
1240 if(exprval.type == EXPRVAL_JSVAL || exprval.type == EXPRVAL_INVALID) {
1241 WARN("invalid ref\n");
1242 exprval_release(&exprval);
1243 exprval_set_exception(&exprval, JS_E_OBJECT_EXPECTED);
1244 }
1245
1246 return stack_push_exprval(ctx, &exprval);
1247 }
1248
1249 static HRESULT identifier_value(script_ctx_t *ctx, BSTR identifier)
1250 {
1251 exprval_t exprval;
1252 jsval_t v;
1253 HRESULT hres;
1254
1255 hres = identifier_eval(ctx, identifier, &exprval);
1256 if(FAILED(hres))
1257 return hres;
1258
1259 if(exprval.type == EXPRVAL_INVALID)
1260 return throw_type_error(ctx, exprval.u.hres, identifier);
1261
1262 hres = exprval_to_value(ctx, &exprval, &v);
1263 if(FAILED(hres))
1264 return hres;
1265
1266 return stack_push(ctx, v);
1267 }
1268
1269 static HRESULT interp_local_ref(script_ctx_t *ctx)
1270 {
1271 const int arg = get_op_int(ctx, 0);
1272 const unsigned flags = get_op_uint(ctx, 1);
1273 call_frame_t *frame = ctx->call_ctx;
1274 exprval_t ref;
1275
1276 TRACE("%d\n", arg);
1277
1278 if(!frame->base_scope || !frame->base_scope->frame)
1279 return interp_identifier_ref(ctx, local_name(frame, arg), flags);
1280
1281 ref.type = EXPRVAL_STACK_REF;
1282 ref.u.off = local_off(frame, arg);
1283 return stack_push_exprval(ctx, &ref);
1284 }
1285
1286 static HRESULT interp_local(script_ctx_t *ctx)
1287 {
1288 const int arg = get_op_int(ctx, 0);
1289 call_frame_t *frame = ctx->call_ctx;
1290 jsval_t copy;
1291 HRESULT hres;
1292
1293 TRACE("%d\n", arg);
1294
1295 if(!frame->base_scope || !frame->base_scope->frame)
1296 return identifier_value(ctx, local_name(frame, arg));
1297
1298 hres = jsval_copy(ctx->stack[local_off(frame, arg)], &copy);
1299 if(FAILED(hres))
1300 return hres;
1301
1302 return stack_push(ctx, copy);
1303 }
1304
1305 /* ECMA-262 3rd Edition 10.1.4 */
1306 static HRESULT interp_ident(script_ctx_t *ctx)
1307 {
1308 const BSTR arg = get_op_bstr(ctx, 0);
1309
1310 TRACE("%s\n", debugstr_w(arg));
1311
1312 return identifier_value(ctx, arg);
1313 }
1314
1315 /* ECMA-262 3rd Edition 10.1.4 */
1316 static HRESULT interp_identid(script_ctx_t *ctx)
1317 {
1318 const BSTR arg = get_op_bstr(ctx, 0);
1319 const unsigned flags = get_op_uint(ctx, 1);
1320
1321 TRACE("%s %x\n", debugstr_w(arg), flags);
1322
1323 return interp_identifier_ref(ctx, arg, flags);
1324 }
1325
1326 /* ECMA-262 3rd Edition 7.8.1 */
1327 static HRESULT interp_null(script_ctx_t *ctx)
1328 {
1329 TRACE("\n");
1330
1331 return stack_push(ctx, jsval_null());
1332 }
1333
1334 /* ECMA-262 3rd Edition 7.8.2 */
1335 static HRESULT interp_bool(script_ctx_t *ctx)
1336 {
1337 const int arg = get_op_int(ctx, 0);
1338
1339 TRACE("%s\n", arg ? "true" : "false");
1340
1341 return stack_push(ctx, jsval_bool(arg));
1342 }
1343
1344 /* ECMA-262 3rd Edition 7.8.3 */
1345 static HRESULT interp_int(script_ctx_t *ctx)
1346 {
1347 const int arg = get_op_int(ctx, 0);
1348
1349 TRACE("%d\n", arg);
1350
1351 return stack_push(ctx, jsval_number(arg));
1352 }
1353
1354 /* ECMA-262 3rd Edition 7.8.3 */
1355 static HRESULT interp_double(script_ctx_t *ctx)
1356 {
1357 const double arg = get_op_double(ctx);
1358
1359 TRACE("%lf\n", arg);
1360
1361 return stack_push(ctx, jsval_number(arg));
1362 }
1363
1364 /* ECMA-262 3rd Edition 7.8.4 */
1365 static HRESULT interp_str(script_ctx_t *ctx)
1366 {
1367 jsstr_t *str = get_op_str(ctx, 0);
1368
1369 TRACE("%s\n", debugstr_jsstr(str));
1370
1371 return stack_push(ctx, jsval_string(jsstr_addref(str)));
1372 }
1373
1374 /* ECMA-262 3rd Edition 7.8 */
1375 static HRESULT interp_regexp(script_ctx_t *ctx)
1376 {
1377 jsstr_t *source = get_op_str(ctx, 0);
1378 const unsigned flags = get_op_uint(ctx, 1);
1379 jsdisp_t *regexp;
1380 HRESULT hres;
1381
1382 TRACE("%s %x\n", debugstr_jsstr(source), flags);
1383
1384 hres = create_regexp(ctx, source, flags, &regexp);
1385 if(FAILED(hres))
1386 return hres;
1387
1388 return stack_push(ctx, jsval_obj(regexp));
1389 }
1390
1391 /* ECMA-262 3rd Edition 11.1.4 */
1392 static HRESULT interp_carray(script_ctx_t *ctx)
1393 {
1394 const unsigned arg = get_op_uint(ctx, 0);
1395 jsdisp_t *array;
1396 jsval_t val;
1397 unsigned i;
1398 HRESULT hres;
1399
1400 TRACE("%u\n", arg);
1401
1402 hres = create_array(ctx, arg, &array);
1403 if(FAILED(hres))
1404 return hres;
1405
1406 i = arg;
1407 while(i--) {
1408 val = stack_pop(ctx);
1409 hres = jsdisp_propput_idx(array, i, val);
1410 jsval_release(val);
1411 if(FAILED(hres)) {
1412 jsdisp_release(array);
1413 return hres;
1414 }
1415 }
1416
1417 return stack_push(ctx, jsval_obj(array));
1418 }
1419
1420 /* ECMA-262 3rd Edition 11.1.5 */
1421 static HRESULT interp_new_obj(script_ctx_t *ctx)
1422 {
1423 jsdisp_t *obj;
1424 HRESULT hres;
1425
1426 TRACE("\n");
1427
1428 hres = create_object(ctx, NULL, &obj);
1429 if(FAILED(hres))
1430 return hres;
1431
1432 return stack_push(ctx, jsval_obj(obj));
1433 }
1434
1435 /* ECMA-262 3rd Edition 11.1.5 */
1436 static HRESULT interp_obj_prop(script_ctx_t *ctx)
1437 {
1438 const BSTR name = get_op_bstr(ctx, 0);
1439 jsdisp_t *obj;
1440 jsval_t val;
1441 HRESULT hres;
1442
1443 TRACE("%s\n", debugstr_w(name));
1444
1445 val = stack_pop(ctx);
1446
1447 assert(is_object_instance(stack_top(ctx)));
1448 obj = as_jsdisp(get_object(stack_top(ctx)));
1449
1450 hres = jsdisp_propput_name(obj, name, val);
1451 jsval_release(val);
1452 return hres;
1453 }
1454
1455 /* ECMA-262 3rd Edition 11.11 */
1456 static HRESULT interp_cnd_nz(script_ctx_t *ctx)
1457 {
1458 const unsigned arg = get_op_uint(ctx, 0);
1459 BOOL b;
1460 HRESULT hres;
1461
1462 TRACE("\n");
1463
1464 hres = to_boolean(stack_top(ctx), &b);
1465 if(FAILED(hres))
1466 return hres;
1467
1468 if(b) {
1469 jmp_abs(ctx, arg);
1470 }else {
1471 stack_popn(ctx, 1);
1472 jmp_next(ctx);
1473 }
1474 return S_OK;
1475 }
1476
1477 /* ECMA-262 3rd Edition 11.11 */
1478 static HRESULT interp_cnd_z(script_ctx_t *ctx)
1479 {
1480 const unsigned arg = get_op_uint(ctx, 0);
1481 BOOL b;
1482 HRESULT hres;
1483
1484 TRACE("\n");
1485
1486 hres = to_boolean(stack_top(ctx), &b);
1487 if(FAILED(hres))
1488 return hres;
1489
1490 if(b) {
1491 stack_popn(ctx, 1);
1492 jmp_next(ctx);
1493 }else {
1494 jmp_abs(ctx, arg);
1495 }
1496 return S_OK;
1497 }
1498
1499 /* ECMA-262 3rd Edition 11.10 */
1500 static HRESULT interp_or(script_ctx_t *ctx)
1501 {
1502 INT l, r;
1503 HRESULT hres;
1504
1505 TRACE("\n");
1506
1507 hres = stack_pop_int(ctx, &r);
1508 if(FAILED(hres))
1509 return hres;
1510
1511 hres = stack_pop_int(ctx, &l);
1512 if(FAILED(hres))
1513 return hres;
1514
1515 return stack_push(ctx, jsval_number(l|r));
1516 }
1517
1518 /* ECMA-262 3rd Edition 11.10 */
1519 static HRESULT interp_xor(script_ctx_t *ctx)
1520 {
1521 INT l, r;
1522 HRESULT hres;
1523
1524 TRACE("\n");
1525
1526 hres = stack_pop_int(ctx, &r);
1527 if(FAILED(hres))
1528 return hres;
1529
1530 hres = stack_pop_int(ctx, &l);
1531 if(FAILED(hres))
1532 return hres;
1533
1534 return stack_push(ctx, jsval_number(l^r));
1535 }
1536
1537 /* ECMA-262 3rd Edition 11.10 */
1538 static HRESULT interp_and(script_ctx_t *ctx)
1539 {
1540 INT l, r;
1541 HRESULT hres;
1542
1543 TRACE("\n");
1544
1545 hres = stack_pop_int(ctx, &r);
1546 if(FAILED(hres))
1547 return hres;
1548
1549 hres = stack_pop_int(ctx, &l);
1550 if(FAILED(hres))
1551 return hres;
1552
1553 return stack_push(ctx, jsval_number(l&r));
1554 }
1555
1556 /* ECMA-262 3rd Edition 11.8.6 */
1557 static HRESULT interp_instanceof(script_ctx_t *ctx)
1558 {
1559 jsdisp_t *obj, *iter, *tmp = NULL;
1560 jsval_t prot, v;
1561 BOOL ret = FALSE;
1562 HRESULT hres;
1563
1564 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1565
1566 v = stack_pop(ctx);
1567 if(!is_object_instance(v) || !get_object(v)) {
1568 jsval_release(v);
1569 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
1570 }
1571
1572 obj = iface_to_jsdisp(get_object(v));
1573 IDispatch_Release(get_object(v));
1574 if(!obj) {
1575 FIXME("non-jsdisp objects not supported\n");
1576 return E_FAIL;
1577 }
1578
1579 if(is_class(obj, JSCLASS_FUNCTION)) {
1580 hres = jsdisp_propget_name(obj, prototypeW, &prot);
1581 }else {
1582 hres = throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
1583 }
1584 jsdisp_release(obj);
1585 if(FAILED(hres))
1586 return hres;
1587
1588 v = stack_pop(ctx);
1589
1590 if(is_object_instance(prot)) {
1591 if(is_object_instance(v))
1592 tmp = iface_to_jsdisp(get_object(v));
1593 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1594 hres = disp_cmp(get_object(prot), to_disp(iter), &ret);
1595 if(FAILED(hres))
1596 break;
1597 }
1598
1599 if(tmp)
1600 jsdisp_release(tmp);
1601 }else {
1602 FIXME("prototype is not an object\n");
1603 hres = E_FAIL;
1604 }
1605
1606 jsval_release(prot);
1607 jsval_release(v);
1608 if(FAILED(hres))
1609 return hres;
1610
1611 return stack_push(ctx, jsval_bool(ret));
1612 }
1613
1614 /* ECMA-262 3rd Edition 11.8.7 */
1615 static HRESULT interp_in(script_ctx_t *ctx)
1616 {
1617 const WCHAR *str;
1618 jsstr_t *jsstr;
1619 jsval_t obj, v;
1620 DISPID id = 0;
1621 BOOL ret;
1622 HRESULT hres;
1623
1624 TRACE("\n");
1625
1626 obj = stack_pop(ctx);
1627 if(!is_object_instance(obj) || !get_object(obj)) {
1628 jsval_release(obj);
1629 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
1630 }
1631
1632 v = stack_pop(ctx);
1633 hres = to_flat_string(ctx, v, &jsstr, &str);
1634 jsval_release(v);
1635 if(FAILED(hres)) {
1636 IDispatch_Release(get_object(obj));
1637 return hres;
1638 }
1639
1640 hres = disp_get_id(ctx, get_object(obj), str, NULL, 0, &id);
1641 IDispatch_Release(get_object(obj));
1642 jsstr_release(jsstr);
1643 if(SUCCEEDED(hres))
1644 ret = TRUE;
1645 else if(hres == DISP_E_UNKNOWNNAME)
1646 ret = FALSE;
1647 else
1648 return hres;
1649
1650 return stack_push(ctx, jsval_bool(ret));
1651 }
1652
1653 /* ECMA-262 3rd Edition 11.6.1 */
1654 static HRESULT add_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, jsval_t *ret)
1655 {
1656 jsval_t r, l;
1657 HRESULT hres;
1658
1659 hres = to_primitive(ctx, lval, &l, NO_HINT);
1660 if(FAILED(hres))
1661 return hres;
1662
1663 hres = to_primitive(ctx, rval, &r, NO_HINT);
1664 if(FAILED(hres)) {
1665 jsval_release(l);
1666 return hres;
1667 }
1668
1669 if(is_string(l) || is_string(r)) {
1670 jsstr_t *lstr, *rstr = NULL;
1671
1672 hres = to_string(ctx, l, &lstr);
1673 if(SUCCEEDED(hres))
1674 hres = to_string(ctx, r, &rstr);
1675
1676 if(SUCCEEDED(hres)) {
1677 jsstr_t *ret_str;
1678
1679 ret_str = jsstr_concat(lstr, rstr);
1680 if(ret_str)
1681 *ret = jsval_string(ret_str);
1682 else
1683 hres = E_OUTOFMEMORY;
1684 }
1685
1686 jsstr_release(lstr);
1687 if(rstr)
1688 jsstr_release(rstr);
1689 }else {
1690 double nl, nr;
1691
1692 hres = to_number(ctx, l, &nl);
1693 if(SUCCEEDED(hres)) {
1694 hres = to_number(ctx, r, &nr);
1695 if(SUCCEEDED(hres))
1696 *ret = jsval_number(nl+nr);
1697 }
1698 }
1699
1700 jsval_release(r);
1701 jsval_release(l);
1702 return hres;
1703 }
1704
1705 /* ECMA-262 3rd Edition 11.6.1 */
1706 static HRESULT interp_add(script_ctx_t *ctx)
1707 {
1708 jsval_t l, r, ret;
1709 HRESULT hres;
1710
1711 r = stack_pop(ctx);
1712 l = stack_pop(ctx);
1713
1714 TRACE("%s + %s\n", debugstr_jsval(l), debugstr_jsval(r));
1715
1716 hres = add_eval(ctx, l, r, &ret);
1717 jsval_release(l);
1718 jsval_release(r);
1719 if(FAILED(hres))
1720 return hres;
1721
1722 return stack_push(ctx, ret);
1723 }
1724
1725 /* ECMA-262 3rd Edition 11.6.2 */
1726 static HRESULT interp_sub(script_ctx_t *ctx)
1727 {
1728 double l, r;
1729 HRESULT hres;
1730
1731 TRACE("\n");
1732
1733 hres = stack_pop_number(ctx, &r);
1734 if(FAILED(hres))
1735 return hres;
1736
1737 hres = stack_pop_number(ctx, &l);
1738 if(FAILED(hres))
1739 return hres;
1740
1741 return stack_push(ctx, jsval_number(l-r));
1742 }
1743
1744 /* ECMA-262 3rd Edition 11.5.1 */
1745 static HRESULT interp_mul(script_ctx_t *ctx)
1746 {
1747 double l, r;
1748 HRESULT hres;
1749
1750 TRACE("\n");
1751
1752 hres = stack_pop_number(ctx, &r);
1753 if(FAILED(hres))
1754 return hres;
1755
1756 hres = stack_pop_number(ctx, &l);
1757 if(FAILED(hres))
1758 return hres;
1759
1760 return stack_push(ctx, jsval_number(l*r));
1761 }
1762
1763 /* ECMA-262 3rd Edition 11.5.2 */
1764 static HRESULT interp_div(script_ctx_t *ctx)
1765 {
1766 double l, r;
1767 HRESULT hres;
1768
1769 TRACE("\n");
1770
1771 hres = stack_pop_number(ctx, &r);
1772 if(FAILED(hres))
1773 return hres;
1774
1775 hres = stack_pop_number(ctx, &l);
1776 if(FAILED(hres))
1777 return hres;
1778
1779 return stack_push(ctx, jsval_number(l/r));
1780 }
1781
1782 /* ECMA-262 3rd Edition 11.5.3 */
1783 static HRESULT interp_mod(script_ctx_t *ctx)
1784 {
1785 double l, r;
1786 HRESULT hres;
1787
1788 TRACE("\n");
1789
1790 hres = stack_pop_number(ctx, &r);
1791 if(FAILED(hres))
1792 return hres;
1793
1794 hres = stack_pop_number(ctx, &l);
1795 if(FAILED(hres))
1796 return hres;
1797
1798 return stack_push(ctx, jsval_number(fmod(l, r)));
1799 }
1800
1801 /* ECMA-262 3rd Edition 11.4.2 */
1802 static HRESULT interp_delete(script_ctx_t *ctx)
1803 {
1804 jsval_t objv, namev;
1805 IDispatch *obj;
1806 jsstr_t *name;
1807 BOOL ret;
1808 HRESULT hres;
1809
1810 TRACE("\n");
1811
1812 namev = stack_pop(ctx);
1813 objv = stack_pop(ctx);
1814
1815 hres = to_object(ctx, objv, &obj);
1816 jsval_release(objv);
1817 if(FAILED(hres)) {
1818 jsval_release(namev);
1819 return hres;
1820 }
1821
1822 hres = to_string(ctx, namev, &name);
1823 jsval_release(namev);
1824 if(FAILED(hres)) {
1825 IDispatch_Release(obj);
1826 return hres;
1827 }
1828
1829 hres = disp_delete_name(ctx, obj, name, &ret);
1830 IDispatch_Release(obj);
1831 jsstr_release(name);
1832 if(FAILED(hres))
1833 return hres;
1834
1835 return stack_push(ctx, jsval_bool(ret));
1836 }
1837
1838 /* ECMA-262 3rd Edition 11.4.2 */
1839 static HRESULT interp_delete_ident(script_ctx_t *ctx)
1840 {
1841 const BSTR arg = get_op_bstr(ctx, 0);
1842 exprval_t exprval;
1843 BOOL ret;
1844 HRESULT hres;
1845
1846 TRACE("%s\n", debugstr_w(arg));
1847
1848 hres = identifier_eval(ctx, arg, &exprval);
1849 if(FAILED(hres))
1850 return hres;
1851
1852 switch(exprval.type) {
1853 case EXPRVAL_STACK_REF:
1854 ret = FALSE;
1855 break;
1856 case EXPRVAL_IDREF:
1857 hres = disp_delete(exprval.u.idref.disp, exprval.u.idref.id, &ret);
1858 IDispatch_Release(exprval.u.idref.disp);
1859 if(FAILED(hres))
1860 return hres;
1861 break;
1862 case EXPRVAL_INVALID:
1863 ret = TRUE;
1864 break;
1865 default:
1866 FIXME("Unsupported exprval\n");
1867 exprval_release(&exprval);
1868 return E_NOTIMPL;
1869 }
1870
1871
1872 return stack_push(ctx, jsval_bool(ret));
1873 }
1874
1875 /* ECMA-262 3rd Edition 11.4.2 */
1876 static HRESULT interp_void(script_ctx_t *ctx)
1877 {
1878 TRACE("\n");
1879
1880 stack_popn(ctx, 1);
1881 return stack_push(ctx, jsval_undefined());
1882 }
1883
1884 /* ECMA-262 3rd Edition 11.4.3 */
1885 static HRESULT typeof_string(jsval_t v, const WCHAR **ret)
1886 {
1887 switch(jsval_type(v)) {
1888 case JSV_UNDEFINED:
1889 *ret = undefinedW;
1890 break;
1891 case JSV_NULL:
1892 *ret = objectW;
1893 break;
1894 case JSV_OBJECT: {
1895 jsdisp_t *dispex;
1896
1897 if(get_object(v) && (dispex = iface_to_jsdisp(get_object(v)))) {
1898 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1899 jsdisp_release(dispex);
1900 }else {
1901 *ret = objectW;
1902 }
1903 break;
1904 }
1905 case JSV_STRING:
1906 *ret = stringW;
1907 break;
1908 case JSV_NUMBER:
1909 *ret = numberW;
1910 break;
1911 case JSV_BOOL:
1912 *ret = booleanW;
1913 break;
1914 case JSV_VARIANT:
1915 FIXME("unhandled variant %s\n", debugstr_variant(get_variant(v)));
1916 return E_NOTIMPL;
1917 }
1918
1919 return S_OK;
1920 }
1921
1922 /* ECMA-262 3rd Edition 11.4.3 */
1923 static HRESULT interp_typeofid(script_ctx_t *ctx)
1924 {
1925 const WCHAR *ret;
1926 exprval_t ref;
1927 jsval_t v;
1928 HRESULT hres;
1929
1930 TRACE("\n");
1931
1932 if(!stack_pop_exprval(ctx, &ref))
1933 return stack_push(ctx, jsval_string(jsstr_undefined()));
1934
1935 hres = exprval_propget(ctx, &ref, &v);
1936 exprval_release(&ref);
1937 if(FAILED(hres))
1938 return stack_push_string(ctx, unknownW);
1939
1940 hres = typeof_string(v, &ret);
1941 jsval_release(v);
1942 if(FAILED(hres))
1943 return hres;
1944
1945 return stack_push_string(ctx, ret);
1946 }
1947
1948 /* ECMA-262 3rd Edition 11.4.3 */
1949 static HRESULT interp_typeofident(script_ctx_t *ctx)
1950 {
1951 const BSTR arg = get_op_bstr(ctx, 0);
1952 exprval_t exprval;
1953 const WCHAR *ret;
1954 jsval_t v;
1955 HRESULT hres;
1956
1957 TRACE("%s\n", debugstr_w(arg));
1958
1959 hres = identifier_eval(ctx, arg, &exprval);
1960 if(FAILED(hres))
1961 return hres;
1962
1963 if(exprval.type == EXPRVAL_INVALID)
1964 return stack_push(ctx, jsval_string(jsstr_undefined()));
1965
1966 hres = exprval_to_value(ctx, &exprval, &v);
1967 if(FAILED(hres))
1968 return hres;
1969
1970 hres = typeof_string(v, &ret);
1971 jsval_release(v);
1972 if(FAILED(hres))
1973 return hres;
1974
1975 return stack_push_string(ctx, ret);
1976 }
1977
1978 /* ECMA-262 3rd Edition 11.4.3 */
1979 static HRESULT interp_typeof(script_ctx_t *ctx)
1980 {
1981 const WCHAR *ret;
1982 jsval_t v;
1983 HRESULT hres;
1984
1985 TRACE("\n");
1986
1987 v = stack_pop(ctx);
1988 hres = typeof_string(v, &ret);
1989 jsval_release(v);
1990 if(FAILED(hres))
1991 return hres;
1992
1993 return stack_push_string(ctx, ret);
1994 }
1995
1996 /* ECMA-262 3rd Edition 11.4.7 */
1997 static HRESULT interp_minus(script_ctx_t *ctx)
1998 {
1999 double n;
2000 HRESULT hres;
2001
2002 TRACE("\n");
2003
2004 hres = stack_pop_number(ctx, &n);
2005 if(FAILED(hres))
2006 return hres;
2007
2008 return stack_push(ctx, jsval_number(-n));
2009 }
2010
2011 /* ECMA-262 3rd Edition 11.4.6 */
2012 static HRESULT interp_tonum(script_ctx_t *ctx)
2013 {
2014 jsval_t v;
2015 double n;
2016 HRESULT hres;
2017
2018 TRACE("\n");
2019
2020 v = stack_pop(ctx);
2021 hres = to_number(ctx, v, &n);
2022 jsval_release(v);
2023 if(FAILED(hres))
2024 return hres;
2025
2026 return stack_push(ctx, jsval_number(n));
2027 }
2028
2029 /* ECMA-262 3rd Edition 11.3.1 */
2030 static HRESULT interp_postinc(script_ctx_t *ctx)
2031 {
2032 const int arg = get_op_int(ctx, 0);
2033 exprval_t ref;
2034 jsval_t v;
2035 HRESULT hres;
2036
2037 TRACE("%d\n", arg);
2038
2039 if(!stack_pop_exprval(ctx, &ref))
2040 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
2041
2042 hres = exprval_propget(ctx, &ref, &v);
2043 if(SUCCEEDED(hres)) {
2044 double n;
2045
2046 hres = to_number(ctx, v, &n);
2047 if(SUCCEEDED(hres))
2048 hres = exprval_propput(ctx, &ref, jsval_number(n+(double)arg));
2049 if(FAILED(hres))
2050 jsval_release(v);
2051 }
2052 exprval_release(&ref);
2053 if(FAILED(hres))
2054 return hres;
2055
2056 return stack_push(ctx, v);
2057 }
2058
2059 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2060 static HRESULT interp_preinc(script_ctx_t *ctx)
2061 {
2062 const int arg = get_op_int(ctx, 0);
2063 exprval_t ref;
2064 double ret;
2065 jsval_t v;
2066 HRESULT hres;
2067
2068 TRACE("%d\n", arg);
2069
2070 if(!stack_pop_exprval(ctx, &ref))
2071 return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL);
2072
2073 hres = exprval_propget(ctx, &ref, &v);
2074 if(SUCCEEDED(hres)) {
2075 double n;
2076
2077 hres = to_number(ctx, v, &n);
2078 jsval_release(v);
2079 if(SUCCEEDED(hres)) {
2080 ret = n+(double)arg;
2081 hres = exprval_propput(ctx, &ref, jsval_number(ret));
2082 }
2083 }
2084 exprval_release(&ref);
2085 if(FAILED(hres))
2086 return hres;
2087
2088 return stack_push(ctx, jsval_number(ret));
2089 }
2090
2091 /* ECMA-262 3rd Edition 11.9.3 */
2092 static HRESULT equal_values(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL *ret)
2093 {
2094 if(jsval_type(lval) == jsval_type(rval) || (is_number(lval) && is_number(rval)))
2095 return equal2_values(lval, rval, ret);
2096
2097 /* FIXME: NULL disps should be handled in more general way */
2098 if(is_object_instance(lval) && !get_object(lval))
2099 return equal_values(ctx, jsval_null(), rval, ret);
2100 if(is_object_instance(rval) && !get_object(rval))
2101 return equal_values(ctx, lval, jsval_null(), ret);
2102
2103 if((is_null(lval) && is_undefined(rval)) || (is_undefined(lval) && is_null(rval))) {
2104 *ret = TRUE;
2105 return S_OK;
2106 }
2107
2108 if(is_string(lval) && is_number(rval)) {
2109 double n;
2110 HRESULT hres;
2111
2112 hres = to_number(ctx, lval, &n);
2113 if(FAILED(hres))
2114 return hres;
2115
2116 /* FIXME: optimize */
2117 return equal_values(ctx, jsval_number(n), rval, ret);
2118 }
2119
2120 if(is_string(rval) && is_number(lval)) {
2121 double n;
2122 HRESULT hres;
2123
2124 hres = to_number(ctx, rval, &n);
2125 if(FAILED(hres))
2126 return hres;
2127
2128 /* FIXME: optimize */
2129 return equal_values(ctx, lval, jsval_number(n), ret);
2130 }
2131
2132 if(is_bool(rval))
2133 return equal_values(ctx, lval, jsval_number(get_bool(rval) ? 1 : 0), ret);
2134
2135 if(is_bool(lval))
2136 return equal_values(ctx, jsval_number(get_bool(lval) ? 1 : 0), rval, ret);
2137
2138
2139 if(is_object_instance(rval) && (is_string(lval) || is_number(lval))) {
2140 jsval_t prim;
2141 HRESULT hres;
2142
2143 hres = to_primitive(ctx, rval, &prim, NO_HINT);
2144 if(FAILED(hres))
2145 return hres;
2146
2147 hres = equal_values(ctx, lval, prim, ret);
2148 jsval_release(prim);
2149 return hres;
2150 }
2151
2152
2153 if(is_object_instance(lval) && (is_string(rval) || is_number(rval))) {
2154 jsval_t prim;
2155 HRESULT hres;
2156
2157 hres = to_primitive(ctx, lval, &prim, NO_HINT);
2158 if(FAILED(hres))
2159 return hres;
2160
2161 hres = equal_values(ctx, prim, rval, ret);
2162 jsval_release(prim);
2163 return hres;
2164 }
2165
2166
2167 *ret = FALSE;
2168 return S_OK;
2169 }
2170
2171 /* ECMA-262 3rd Edition 11.9.1 */
2172 static HRESULT interp_eq(script_ctx_t *ctx)
2173 {
2174 jsval_t l, r;
2175 BOOL b;
2176 HRESULT hres;
2177
2178 r = stack_pop(ctx);
2179 l = stack_pop(ctx);
2180
2181 TRACE("%s == %s\n", debugstr_jsval(l), debugstr_jsval(r));
2182
2183 hres = equal_values(ctx, l, r, &b);
2184 jsval_release(l);
2185 jsval_release(r);
2186 if(FAILED(hres))
2187 return hres;
2188
2189 return stack_push(ctx, jsval_bool(b));
2190 }
2191
2192 /* ECMA-262 3rd Edition 11.9.2 */
2193 static HRESULT interp_neq(script_ctx_t *ctx)
2194 {
2195 jsval_t l, r;
2196 BOOL b;
2197 HRESULT hres;
2198
2199 r = stack_pop(ctx);
2200 l = stack_pop(ctx);
2201
2202 TRACE("%s != %s\n", debugstr_jsval(l), debugstr_jsval(r));
2203
2204 hres = equal_values(ctx, l, r, &b);
2205 jsval_release(l);
2206 jsval_release(r);
2207 if(FAILED(hres))
2208 return hres;
2209
2210 return stack_push(ctx, jsval_bool(!b));
2211 }
2212
2213 /* ECMA-262 3rd Edition 11.9.4 */
2214 static HRESULT interp_eq2(script_ctx_t *ctx)
2215 {
2216 jsval_t l, r;
2217 BOOL b;
2218 HRESULT hres;
2219
2220 r = stack_pop(ctx);
2221 l = stack_pop(ctx);
2222
2223 TRACE("%s === %s\n", debugstr_jsval(l), debugstr_jsval(r));
2224
2225 hres = equal2_values(r, l, &b);
2226 jsval_release(l);
2227 jsval_release(r);
2228 if(FAILED(hres))
2229 return hres;
2230
2231 return stack_push(ctx, jsval_bool(b));
2232 }
2233
2234 /* ECMA-262 3rd Edition 11.9.5 */
2235 static HRESULT interp_neq2(script_ctx_t *ctx)
2236 {
2237 jsval_t l, r;
2238 BOOL b;
2239 HRESULT hres;
2240
2241 TRACE("\n");
2242
2243 r = stack_pop(ctx);
2244 l = stack_pop(ctx);
2245
2246 hres = equal2_values(r, l, &b);
2247 jsval_release(l);
2248 jsval_release(r);
2249 if(FAILED(hres))
2250 return hres;
2251
2252 return stack_push(ctx, jsval_bool(!b));
2253 }
2254
2255 /* ECMA-262 3rd Edition 11.8.5 */
2256 static HRESULT less_eval(script_ctx_t *ctx, jsval_t lval, jsval_t rval, BOOL greater, BOOL *ret)
2257 {
2258 double ln, rn;
2259 jsval_t l, r;
2260 HRESULT hres;
2261
2262 hres = to_primitive(ctx, lval, &l, NO_HINT);
2263 if(FAILED(hres))
2264 return hres;
2265
2266 hres = to_primitive(ctx, rval, &r, NO_HINT);
2267 if(FAILED(hres)) {
2268 jsval_release(l);
2269 return hres;
2270 }
2271
2272 if(is_string(l) && is_string(r)) {
2273 *ret = (jsstr_cmp(get_string(l), get_string(r)) < 0) ^ greater;
2274 jsstr_release(get_string(l));
2275 jsstr_release(get_string(r));
2276 return S_OK;
2277 }
2278
2279 hres = to_number(ctx, l, &ln);
2280 jsval_release(l);
2281 if(SUCCEEDED(hres))
2282 hres = to_number(ctx, r, &rn);
2283 jsval_release(r);
2284 if(FAILED(hres))
2285 return hres;
2286
2287 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2288 return S_OK;
2289 }
2290
2291 /* ECMA-262 3rd Edition 11.8.1 */
2292 static HRESULT interp_lt(script_ctx_t *ctx)
2293 {
2294 jsval_t l, r;
2295 BOOL b;
2296 HRESULT hres;
2297
2298 r = stack_pop(ctx);
2299 l = stack_pop(ctx);
2300
2301 TRACE("%s < %s\n", debugstr_jsval(l), debugstr_jsval(r));
2302
2303 hres = less_eval(ctx, l, r, FALSE, &b);
2304 jsval_release(l);
2305 jsval_release(r);
2306 if(FAILED(hres))
2307 return hres;
2308
2309 return stack_push(ctx, jsval_bool(b));
2310 }
2311
2312 /* ECMA-262 3rd Edition 11.8.1 */
2313 static HRESULT interp_lteq(script_ctx_t *ctx)
2314 {
2315 jsval_t l, r;
2316 BOOL b;
2317 HRESULT hres;
2318
2319 r = stack_pop(ctx);
2320 l = stack_pop(ctx);
2321
2322 TRACE("%s <= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2323
2324 hres = less_eval(ctx, r, l, TRUE, &b);
2325 jsval_release(l);
2326 jsval_release(r);
2327 if(FAILED(hres))
2328 return hres;
2329
2330 return stack_push(ctx, jsval_bool(b));
2331 }
2332
2333 /* ECMA-262 3rd Edition 11.8.2 */
2334 static HRESULT interp_gt(script_ctx_t *ctx)
2335 {
2336 jsval_t l, r;
2337 BOOL b;
2338 HRESULT hres;
2339
2340 r = stack_pop(ctx);
2341 l = stack_pop(ctx);
2342
2343 TRACE("%s > %s\n", debugstr_jsval(l), debugstr_jsval(r));
2344
2345 hres = less_eval(ctx, r, l, FALSE, &b);
2346 jsval_release(l);
2347 jsval_release(r);
2348 if(FAILED(hres))
2349 return hres;
2350
2351 return stack_push(ctx, jsval_bool(b));
2352 }
2353
2354 /* ECMA-262 3rd Edition 11.8.4 */
2355 static HRESULT interp_gteq(script_ctx_t *ctx)
2356 {
2357 jsval_t l, r;
2358 BOOL b;
2359 HRESULT hres;
2360
2361 r = stack_pop(ctx);
2362 l = stack_pop(ctx);
2363
2364 TRACE("%s >= %s\n", debugstr_jsval(l), debugstr_jsval(r));
2365
2366 hres = less_eval(ctx, l, r, TRUE, &b);
2367 jsval_release(l);
2368 jsval_release(r);
2369 if(FAILED(hres))
2370 return hres;
2371
2372 return stack_push(ctx, jsval_bool(b));
2373 }
2374
2375 /* ECMA-262 3rd Edition 11.4.8 */
2376 static HRESULT interp_bneg(script_ctx_t *ctx)
2377 {
2378 jsval_t v;
2379 INT i;
2380 HRESULT hres;
2381
2382 TRACE("\n");
2383
2384 v = stack_pop(ctx);
2385 hres = to_int32(ctx, v, &i);
2386 jsval_release(v);
2387 if(FAILED(hres))
2388 return hres;
2389
2390 return stack_push(ctx, jsval_number(~i));
2391 }
2392
2393 /* ECMA-262 3rd Edition 11.4.9 */
2394 static HRESULT interp_neg(script_ctx_t *ctx)
2395 {
2396 jsval_t v;
2397 BOOL b;
2398 HRESULT hres;
2399
2400 TRACE("\n");
2401
2402 v = stack_pop(ctx);
2403 hres = to_boolean(v, &b);
2404 jsval_release(v);
2405 if(FAILED(hres))
2406 return hres;
2407
2408 return stack_push(ctx, jsval_bool(!b));
2409 }
2410
2411 /* ECMA-262 3rd Edition 11.7.1 */
2412 static HRESULT interp_lshift(script_ctx_t *ctx)
2413 {
2414 DWORD r;
2415 INT l;
2416 HRESULT hres;
2417
2418 hres = stack_pop_uint(ctx, &r);
2419 if(FAILED(hres))
2420 return hres;
2421
2422 hres = stack_pop_int(ctx, &l);
2423 if(FAILED(hres))
2424 return hres;
2425
2426 return stack_push(ctx, jsval_number(l << (r&0x1f)));
2427 }
2428
2429 /* ECMA-262 3rd Edition 11.7.2 */
2430 static HRESULT interp_rshift(script_ctx_t *ctx)
2431 {
2432 DWORD r;
2433 INT l;
2434 HRESULT hres;
2435
2436 hres = stack_pop_uint(ctx, &r);
2437 if(FAILED(hres))
2438 return hres;
2439
2440 hres = stack_pop_int(ctx, &l);
2441 if(FAILED(hres))
2442 return hres;
2443
2444 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2445 }
2446
2447 /* ECMA-262 3rd Edition 11.7.3 */
2448 static HRESULT interp_rshift2(script_ctx_t *ctx)
2449 {
2450 DWORD r, l;
2451 HRESULT hres;
2452
2453 hres = stack_pop_uint(ctx, &r);
2454 if(FAILED(hres))
2455 return hres;
2456
2457 hres = stack_pop_uint(ctx, &l);
2458 if(FAILED(hres))
2459 return hres;
2460
2461 return stack_push(ctx, jsval_number(l >> (r&0x1f)));
2462 }
2463
2464 /* ECMA-262 3rd Edition 11.13.1 */
2465 static HRESULT interp_assign(script_ctx_t *ctx)
2466 {
2467 exprval_t ref;
2468 jsval_t v;
2469 HRESULT hres;
2470
2471 TRACE("\n");
2472
2473 v = stack_pop(ctx);
2474
2475 if(!stack_pop_exprval(ctx, &ref)) {
2476 jsval_release(v);
2477 return throw_reference_error(ctx, JS_E_ILLEGAL_ASSIGN, NULL);
2478 }
2479
2480 hres = exprval_propput(ctx, &ref, v);
2481 exprval_release(&ref);
2482 if(FAILED(hres)) {
2483 jsval_release(v);
2484 return hres;
2485 }
2486
2487 return stack_push(ctx, v);
2488 }
2489
2490 /* JScript extension */
2491 static HRESULT interp_assign_call(script_ctx_t *ctx)
2492 {
2493 const unsigned argc = get_op_uint(ctx, 0);
2494 exprval_t ref;
2495 jsval_t v;
2496 HRESULT hres;
2497
2498 TRACE("%u\n", argc);
2499
2500 if(!stack_topn_exprval(ctx, argc+1, &ref))
2501 return throw_reference_error(ctx, JS_E_ILLEGAL_ASSIGN, NULL);
2502
2503 hres = exprval_call(ctx, &ref, DISPATCH_PROPERTYPUT, argc+1, stack_args(ctx, argc+1), NULL);
2504 if(FAILED(hres))
2505 return hres;
2506
2507 v = stack_pop(ctx);
2508 stack_popn(ctx, argc+2);
2509 return stack_push(ctx, v);
2510 }
2511
2512 static HRESULT interp_undefined(script_ctx_t *ctx)
2513 {
2514 TRACE("\n");
2515
2516 return stack_push(ctx, jsval_undefined());
2517 }
2518
2519 static HRESULT interp_jmp(script_ctx_t *ctx)
2520 {
2521 const unsigned arg = get_op_uint(ctx, 0);
2522
2523 TRACE("%u\n", arg);
2524
2525 jmp_abs(ctx, arg);
2526 return S_OK;
2527 }
2528
2529 static HRESULT interp_jmp_z(script_ctx_t *ctx)
2530 {
2531 const unsigned arg = get_op_uint(ctx, 0);
2532 BOOL b;
2533 jsval_t v;
2534 HRESULT hres;
2535
2536 TRACE("\n");
2537
2538 v = stack_pop(ctx);
2539 hres = to_boolean(v, &b);
2540 jsval_release(v);
2541 if(FAILED(hres))
2542 return hres;
2543
2544 if(b)
2545 jmp_next(ctx);
2546 else
2547 jmp_abs(ctx, arg);
2548 return S_OK;
2549 }
2550
2551 static HRESULT interp_pop(script_ctx_t *ctx)
2552 {
2553 const unsigned arg = get_op_uint(ctx, 0);
2554
2555 TRACE("%u\n", arg);
2556
2557 stack_popn(ctx, arg);
2558 return S_OK;
2559 }
2560
2561 static HRESULT interp_ret(script_ctx_t *ctx)
2562 {
2563 const unsigned clear_ret = get_op_uint(ctx, 0);
2564 call_frame_t *frame = ctx->call_ctx;
2565
2566 TRACE("\n");
2567
2568 if(clear_ret)
2569 jsval_release(steal_ret(frame));
2570
2571 if((frame->flags & EXEC_CONSTRUCTOR) && !is_object_instance(frame->ret)) {
2572 jsval_release(frame->ret);
2573 IDispatch_AddRef(frame->this_obj);
2574 frame->ret = jsval_disp(frame->this_obj);
2575 }
2576
2577 jmp_abs(ctx, -1);
2578 return S_OK;
2579 }
2580
2581 static HRESULT interp_setret(script_ctx_t *ctx)
2582 {
2583 call_frame_t *frame = ctx->call_ctx;
2584
2585 TRACE("\n");
2586
2587 jsval_release(frame->ret);
2588 frame->ret = stack_pop(ctx);
2589 return S_OK;
2590 }
2591
2592 static HRESULT interp_push_ret(script_ctx_t *ctx)
2593 {
2594 call_frame_t *frame = ctx->call_ctx;
2595 HRESULT hres;
2596
2597 TRACE("\n");
2598
2599 hres = stack_push(ctx, frame->ret);
2600 if(SUCCEEDED(hres))
2601 frame->ret = jsval_undefined();
2602 return hres;
2603 }
2604
2605 typedef HRESULT (*op_func_t)(script_ctx_t*);
2606
2607 static const op_func_t op_funcs[] = {
2608 #define X(x,a,b,c) interp_##x,
2609 OP_LIST
2610 #undef X
2611 };
2612
2613 static const unsigned op_move[] = {
2614 #define X(a,x,b,c) x,
2615 OP_LIST
2616 #undef X
2617 };
2618
2619 static void pop_call_frame(script_ctx_t *ctx)
2620 {
2621 call_frame_t *frame = ctx->call_ctx;
2622
2623 frame->stack_base -= frame->pop_locals + frame->pop_variables;
2624
2625 assert(frame->scope == frame->base_scope);
2626
2627 /* If current scope will be kept alive, we need to transfer local variables to its variable object. */
2628 if(frame->scope && frame->scope->ref > 1) {
2629 HRESULT hres = detach_variable_object(ctx, frame, TRUE);
2630 if(FAILED(hres))
2631 ERR("Failed to detach variable object: %08x\n", hres);
2632 }
2633
2634 if(frame->arguments_obj)
2635 detach_arguments_object(frame->arguments_obj);
2636 if(frame->scope)
2637 scope_release(frame->scope);
2638
2639 if(frame->pop_variables)
2640 stack_popn(ctx, frame->pop_variables);
2641 stack_popn(ctx, frame->pop_locals);
2642
2643 ctx->call_ctx = frame->prev_frame;
2644
2645 if(frame->function_instance)
2646 jsdisp_release(frame->function_instance);
2647 if(frame->variable_obj)
2648 jsdisp_release(frame->variable_obj);
2649 if(frame->this_obj)
2650 IDispatch_Release(frame->this_obj);
2651 jsval_release(frame->ret);
2652 release_bytecode(frame->bytecode);
2653 heap_free(frame);
2654 }
2655
2656 static void print_backtrace(script_ctx_t *ctx)
2657 {
2658 unsigned depth = 0, i;
2659 call_frame_t *frame;
2660
2661 for(frame = ctx->call_ctx; frame; frame = frame->prev_frame) {
2662 WARN("%u\t", depth);
2663 depth++;
2664
2665 if(frame->this_obj && frame->this_obj != to_disp(ctx->global) && frame->this_obj != ctx->host_global)
2666 WARN("%p->", frame->this_obj);
2667 WARN("%s(", frame->function->name ? debugstr_w(frame->function->name) : "[unnamed]");
2668 if(frame->base_scope && frame->base_scope->frame) {
2669 for(i=0; i < frame->argc; i++) {
2670 if(i < frame->function->param_cnt)
2671 WARN("%s%s=%s", i ? ", " : "", debugstr_w(frame->function->params[i]),
2672 debugstr_jsval(ctx->stack[local_off(frame, -i-1)]));
2673 else
2674 WARN("%s%s", i ? ", " : "", debugstr_jsval(ctx->stack[local_off(frame, -i-1)]));
2675 }
2676 }else {
2677 WARN("[detached frame]");
2678 }
2679 WARN(")\n");
2680
2681 if(!(frame->flags & EXEC_RETURN_TO_INTERP)) {
2682 WARN("%u\t[native code]\n", depth);
2683 depth++;
2684 }
2685 }
2686 }
2687
2688 static HRESULT unwind_exception(script_ctx_t *ctx, HRESULT exception_hres)
2689 {
2690 except_frame_t *except_frame;
2691 call_frame_t *frame;
2692 jsval_t except_val;
2693 unsigned catch_off;
2694 HRESULT hres;
2695
2696 if(WARN_ON(jscript)) {
2697 jsdisp_t *error_obj;
2698 jsval_t msg;
2699
2700 static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
2701
2702 WARN("Exception %08x %s", exception_hres, debugstr_jsval(ctx->ei.val));
2703 if(jsval_type(ctx->ei.val) == JSV_OBJECT) {
2704 error_obj = to_jsdisp(get_object(ctx->ei.val));
2705 if(error_obj) {
2706 hres = jsdisp_propget_name(error_obj, messageW, &msg);
2707 if(SUCCEEDED(hres)) {
2708 WARN(" (message %s)", debugstr_jsval(msg));
2709 jsval_release(msg);
2710 }
2711 }
2712 }
2713 WARN(" in:\n");
2714
2715 print_backtrace(ctx);
2716 }
2717
2718 for(frame = ctx->call_ctx; !frame->except_frame; frame = ctx->call_ctx) {
2719 DWORD flags;
2720
2721 while(frame->scope != frame->base_scope)
2722 scope_pop(&frame->scope);
2723
2724 stack_popn(ctx, ctx->stack_top-frame->stack_base);
2725
2726 flags = frame->flags;
2727 pop_call_frame(ctx);
2728 if(!(flags & EXEC_RETURN_TO_INTERP))
2729 return exception_hres;
2730 }
2731
2732 except_frame = frame->except_frame;
2733 catch_off = except_frame->catch_off;
2734
2735 assert(except_frame->stack_top <= ctx->stack_top);
2736 stack_popn(ctx, ctx->stack_top - except_frame->stack_top);
2737
2738 while(except_frame->scope != frame->scope)
2739 scope_pop(&frame->scope);
2740
2741 frame->ip = catch_off ? catch_off : except_frame->finally_off;
2742 if(catch_off) assert(frame->bytecode->instrs[frame->ip].op == OP_enter_catch);
2743
2744 except_val = ctx->ei.val;
2745 ctx->ei.val = jsval_undefined();
2746 clear_ei(ctx);
2747
2748 /* keep current except_frame if we're entering catch block with finally block associated */
2749 if(catch_off && except_frame->finally_off) {
2750 except_frame->catch_off = 0;
2751 }else {
2752 frame->except_frame = except_frame->next;
2753 heap_free(except_frame);
2754 }
2755
2756 hres = stack_push(ctx, except_val);
2757 if(FAILED(hres))
2758 return hres;
2759
2760 if(!catch_off)
2761 hres = stack_push(ctx, jsval_bool(FALSE));
2762 return hres;
2763 }
2764
2765 static HRESULT enter_bytecode(script_ctx_t *ctx, jsval_t *r)
2766 {
2767 call_frame_t *frame;
2768 jsop_t op;
2769 HRESULT hres = S_OK;
2770
2771 TRACE("\n");
2772
2773 while(1) {
2774 frame = ctx->call_ctx;
2775 op = frame->bytecode->instrs[frame->ip].op;
2776 hres = op_funcs[op](ctx);
2777 if(FAILED(hres)) {
2778 hres = unwind_exception(ctx, hres);
2779 if(FAILED(hres))
2780 return hres;
2781 }else if(frame->ip == -1) {
2782 const DWORD return_to_interp = frame->flags & EXEC_RETURN_TO_INTERP;
2783
2784 assert(ctx->stack_top == frame->stack_base);
2785 assert(frame->scope == frame->base_scope);
2786
2787 if(return_to_interp) {
2788 clear_ret(frame->prev_frame);
2789 frame->prev_frame->ret = steal_ret(frame);
2790 }else if(r) {
2791 *r = steal_ret(frame);
2792 }
2793 pop_call_frame(ctx);
2794 if(!return_to_interp)
2795 break;
2796 }else {
2797 frame->ip += op_move[op];
2798 }
2799 }
2800
2801 return S_OK;
2802 }
2803
2804 static HRESULT bind_event_target(script_ctx_t *ctx, function_code_t *func, jsdisp_t *func_obj)
2805 {
2806 IBindEventHandler *target;
2807 exprval_t exprval;
2808 IDispatch *disp;
2809 jsval_t v;
2810 HRESULT hres;
2811
2812 hres = identifier_eval(ctx, func->event_target, &exprval);
2813 if(FAILED(hres))
2814 return hres;
2815
2816 hres = exprval_to_value(ctx, &exprval, &v);
2817 if(FAILED(hres))
2818 return hres;
2819
2820 if(!is_object_instance(v)) {
2821 FIXME("Can't bind to %s\n", debugstr_jsval(v));
2822 jsval_release(v);
2823 }
2824
2825 disp = get_object(v);
2826 hres = IDispatch_QueryInterface(disp, &IID_IBindEventHandler, (void**)&target);
2827 if(SUCCEEDED(hres)) {
2828 hres = IBindEventHandler_BindHandler(target, func->name, (IDispatch*)&func_obj->IDispatchEx_iface);
2829 IBindEventHandler_Release(target);
2830 if(FAILED(hres))
2831 WARN("BindEvent failed: %08x\n", hres);
2832 }else {
2833 FIXME("No IBindEventHandler, not yet supported binding\n");
2834 }
2835
2836 IDispatch_Release(disp);
2837 return hres;
2838 }
2839
2840 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)
2841 {
2842 const unsigned orig_stack = ctx->stack_top;
2843 scope_chain_t *scope;
2844 unsigned i;
2845 jsval_t v;
2846 HRESULT hres;
2847
2848 /* If arguments are already on the stack, we may use them. */
2849 if(argv + argc == ctx->stack + ctx->stack_top) {
2850 frame->arguments_off = argv - ctx->stack;
2851 i = argc;
2852 }else {
2853 frame->arguments_off = ctx->stack_top;
2854 for(i = 0; i < argc; i++) {
2855 hres = jsval_copy(argv[i], &v);
2856 if(SUCCEEDED(hres))
2857 hres = stack_push(ctx, v);
2858 if(FAILED(hres)) {
2859 stack_popn(ctx, i);
2860 return hres;
2861 }
2862 }
2863 }
2864
2865 /* If fewer than declared arguments were passed, fill remaining with undefined value. */
2866 for(; i < frame->function->param_cnt; i++) {
2867 hres = stack_push(ctx, jsval_undefined());
2868 if(FAILED(hres)) {
2869 stack_popn(ctx, ctx->stack_top - orig_stack);
2870 return hres;
2871 }
2872 }
2873
2874 frame->pop_locals = ctx->stack_top - orig_stack;
2875
2876 frame->variables_off = ctx->stack_top;
2877
2878 for(i = 0; i < frame->function->var_cnt; i++) {
2879 hres = stack_push(ctx, jsval_undefined());
2880 if(FAILED(hres)) {
2881 stack_popn(ctx, ctx->stack_top - orig_stack);
2882 return hres;
2883 }
2884 }
2885
2886 frame->pop_variables = i;
2887
2888 hres = scope_push(scope_chain, variable_object, to_disp(variable_object), &scope);
2889 if(FAILED(hres)) {
2890 stack_popn(ctx, ctx->stack_top - orig_stack);
2891 return hres;
2892 }
2893
2894 for(i = 0; i < frame->function->func_cnt; i++) {
2895 if(frame->function->funcs[i].name && !frame->function->funcs[i].event_target) {
2896 jsdisp_t *func_obj;
2897 unsigned off;
2898
2899 hres = create_source_function(ctx, frame->bytecode, frame->function->funcs+i, scope, &func_obj);
2900 if(FAILED(hres)) {
2901 stack_popn(ctx, ctx->stack_top - orig_stack);
2902 scope_release(scope);
2903 return hres;
2904 }
2905
2906 off = local_off(frame, frame->function->funcs[i].local_ref);
2907 jsval_release(ctx->stack[off]);
2908 ctx->stack[off] = jsval_obj(func_obj);
2909 }
2910 }
2911
2912 scope->frame = frame;
2913 frame->base_scope = frame->scope = scope;
2914 return S_OK;
2915 }
2916
2917 HRESULT exec_source(script_ctx_t *ctx, DWORD flags, bytecode_t *bytecode, function_code_t *function, scope_chain_t *scope,
2918 IDispatch *this_obj, jsdisp_t *function_instance, jsdisp_t *variable_obj, unsigned argc, jsval_t *argv, jsval_t *r)
2919 {
2920 call_frame_t *frame;
2921 unsigned i;
2922 HRESULT hres;
2923
2924 for(i = 0; i < function->func_cnt; i++) {
2925 jsdisp_t *func_obj;
2926
2927 if(!function->funcs[i].event_target)
2928 continue;
2929
2930 hres = create_source_function(ctx, bytecode, function->funcs+i, scope, &func_obj);
2931 if(FAILED(hres))
2932 return hres;
2933
2934 hres = bind_event_target(ctx, function->funcs+i, func_obj);
2935 jsdisp_release(func_obj);
2936 if(FAILED(hres))
2937 return hres;
2938 }
2939
2940 if(flags & (EXEC_GLOBAL | EXEC_EVAL)) {
2941 for(i=0; i < function->var_cnt; i++) {
2942 TRACE("[%d] %s %d\n", i, debugstr_w(function->variables[i].name), function->variables[i].func_id);
2943 if(function->variables[i].func_id != -1) {
2944 jsdisp_t *func_obj;
2945
2946 hres = create_source_function(ctx, bytecode, function->funcs+function->variables[i].func_id, scope, &func_obj);
2947 if(FAILED(hres))
2948 return hres;
2949
2950 hres = jsdisp_propput_name(variable_obj, function->variables[i].name, jsval_obj(func_obj));
2951 jsdisp_release(func_obj);
2952 }else if(!(flags & EXEC_GLOBAL) || !lookup_global_members(ctx, function->variables[i].name, NULL)) {
2953 DISPID id = 0;
2954
2955 hres = jsdisp_get_id(variable_obj, function->variables[i].name, fdexNameEnsure, &id);
2956 if(FAILED(hres))
2957 return hres;
2958 }
2959 }
2960 }
2961
2962 /* ECMA-262 3rd Edition 11.2.3.7 */
2963 if(this_obj) {
2964 jsdisp_t *jsthis;
2965
2966 jsthis = iface_to_jsdisp(this_obj);
2967 if(jsthis) {
2968 if(jsthis->builtin_info->class == JSCLASS_GLOBAL || jsthis->builtin_info->class == JSCLASS_NONE)
2969 this_obj = NULL;
2970 jsdisp_release(jsthis);
2971 }
2972 }
2973
2974 if(ctx->call_ctx && (flags & EXEC_EVAL)) {
2975 hres = detach_variable_object(ctx, ctx->call_ctx, FALSE);
2976 if(FAILED(hres))
2977 return hres;
2978 }
2979
2980 frame = heap_alloc_zero(sizeof(*frame));
2981 if(!frame)
2982 return E_OUTOFMEMORY;
2983
2984 frame->function = function;
2985 frame->ret = jsval_undefined();
2986 frame->argc = argc;
2987 frame->bytecode = bytecode_addref(bytecode);
2988
2989 if(!(flags & (EXEC_GLOBAL|EXEC_EVAL))) {
2990 hres = setup_scope(ctx, frame, scope, variable_obj, argc, argv);
2991 if(FAILED(hres)) {
2992 release_bytecode(frame->bytecode);
2993 heap_free(frame);
2994 return hres;
2995 }
2996 }else if(scope) {
2997 frame->base_scope = frame->scope = scope_addref(scope);
2998 }
2999
3000 frame->ip = function->instr_off;
3001 frame->stack_base = ctx->stack_top;
3002 if(this_obj)
3003 frame->this_obj = this_obj;
3004 else if(ctx->host_global)
3005 frame->this_obj = ctx->host_global;
3006 else
3007 frame->this_obj = to_disp(ctx->global);
3008 IDispatch_AddRef(frame->this_obj);
3009
3010 if(function_instance)
3011 frame->function_instance = jsdisp_addref(function_instance);
3012
3013 frame->flags = flags;
3014 frame->variable_obj = jsdisp_addref(variable_obj);
3015
3016 frame->prev_frame = ctx->call_ctx;
3017 ctx->call_ctx = frame;
3018
3019 if(flags & EXEC_RETURN_TO_INTERP) {
3020 /*
3021 * We're called directly from interpreter, so we may just setup call frame and return.
3022 * Already running interpreter will take care of execution.
3023 */
3024 if(r)
3025 *r = jsval_undefined();
3026 return S_OK;
3027 }
3028
3029 return enter_bytecode(ctx, r);
3030 }