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 "config.h"
20 #include "wine/port.h"
21
22 #include <math.h>
23 #include <assert.h>
24
25 #include "jscript.h"
26 #include "engine.h"
27
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
31
32 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
33 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
34 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
35 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
36 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
37 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
38 static const WCHAR unknownW[] = {'u','n','k','n','o','w','n',0};
39
40 struct _except_frame_t {
41 unsigned stack_top;
42 scope_chain_t *scope;
43 unsigned catch_off;
44 BSTR ident;
45
46 except_frame_t *next;
47 };
48
49 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
50 {
51 if(!ctx->stack_size) {
52 ctx->stack = heap_alloc(16*sizeof(VARIANT));
53 if(!ctx->stack)
54 return E_OUTOFMEMORY;
55 ctx->stack_size = 16;
56 }else if(ctx->stack_size == ctx->top) {
57 VARIANT *new_stack;
58
59 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(VARIANT));
60 if(!new_stack) {
61 VariantClear(v);
62 return E_OUTOFMEMORY;
63 }
64
65 ctx->stack = new_stack;
66 ctx->stack_size *= 2;
67 }
68
69 ctx->stack[ctx->top++] = *v;
70 return S_OK;
71 }
72
73 static HRESULT stack_push_bool(exec_ctx_t *ctx, BOOL b)
74 {
75 VARIANT v;
76
77 V_VT(&v) = VT_BOOL;
78 V_BOOL(&v) = b ? VARIANT_TRUE : VARIANT_FALSE;
79 return stack_push(ctx, &v);
80 }
81
82 static inline HRESULT stack_push_number(exec_ctx_t *ctx, double number)
83 {
84 VARIANT v;
85
86 num_set_val(&v, number);
87 return stack_push(ctx, &v);
88 }
89
90 static inline HRESULT stack_push_int(exec_ctx_t *ctx, INT n)
91 {
92 VARIANT v;
93
94 V_VT(&v) = VT_I4;
95 V_I4(&v) = n;
96 return stack_push(ctx, &v);
97 }
98
99 static inline HRESULT stack_push_string(exec_ctx_t *ctx, const WCHAR *str)
100 {
101 VARIANT v;
102
103 V_VT(&v) = VT_BSTR;
104 V_BSTR(&v) = SysAllocString(str);
105 return V_BSTR(&v) ? stack_push(ctx, &v) : E_OUTOFMEMORY;
106 }
107
108 static HRESULT stack_push_objid(exec_ctx_t *ctx, IDispatch *disp, DISPID id)
109 {
110 VARIANT v;
111 HRESULT hres;
112
113 V_VT(&v) = VT_DISPATCH;
114 V_DISPATCH(&v) = disp;
115 hres = stack_push(ctx, &v);
116 if(FAILED(hres))
117 return hres;
118
119 V_VT(&v) = VT_INT;
120 V_INT(&v) = id;
121 return stack_push(ctx, &v);
122 }
123
124 static inline VARIANT *stack_top(exec_ctx_t *ctx)
125 {
126 assert(ctx->top);
127 return ctx->stack + ctx->top-1;
128 }
129
130 static inline VARIANT *stack_topn(exec_ctx_t *ctx, unsigned n)
131 {
132 assert(ctx->top > n);
133 return ctx->stack + ctx->top-1-n;
134 }
135
136 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
137 {
138 assert(ctx->top);
139 return ctx->stack + --ctx->top;
140 }
141
142 static void stack_popn(exec_ctx_t *ctx, unsigned n)
143 {
144 while(n--)
145 VariantClear(stack_pop(ctx));
146 }
147
148 static HRESULT stack_pop_number(exec_ctx_t *ctx, VARIANT *r)
149 {
150 VARIANT *v;
151 HRESULT hres;
152
153 v = stack_pop(ctx);
154 hres = to_number(ctx->parser->script, v, ctx->ei, r);
155 VariantClear(v);
156 return hres;
157 }
158
159 static HRESULT stack_pop_object(exec_ctx_t *ctx, IDispatch **r)
160 {
161 VARIANT *v;
162 HRESULT hres;
163
164 v = stack_pop(ctx);
165 if(V_VT(v) == VT_DISPATCH) {
166 if(!V_DISPATCH(v))
167 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_REQUIRED, NULL);
168 *r = V_DISPATCH(v);
169 return S_OK;
170 }
171
172 hres = to_object(ctx->parser->script, v, r);
173 VariantClear(v);
174 return hres;
175 }
176
177 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
178 {
179 return to_int32(ctx->parser->script, stack_pop(ctx), ctx->ei, r);
180 }
181
182 static inline HRESULT stack_pop_uint(exec_ctx_t *ctx, DWORD *r)
183 {
184 return to_uint32(ctx->parser->script, stack_pop(ctx), ctx->ei, r);
185 }
186
187 static inline IDispatch *stack_pop_objid(exec_ctx_t *ctx, DISPID *id)
188 {
189 assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
190
191 *id = V_INT(stack_pop(ctx));
192 return V_DISPATCH(stack_pop(ctx));
193 }
194
195 static inline IDispatch *stack_topn_objid(exec_ctx_t *ctx, unsigned n, DISPID *id)
196 {
197 assert(V_VT(stack_topn(ctx, n)) == VT_INT && V_VT(stack_topn(ctx, n+1)) == VT_DISPATCH);
198
199 *id = V_INT(stack_topn(ctx, n));
200 return V_DISPATCH(stack_topn(ctx, n+1));
201 }
202
203 static void exprval_release(exprval_t *val)
204 {
205 switch(val->type) {
206 case EXPRVAL_VARIANT:
207 if(V_VT(&val->u.var) != VT_EMPTY)
208 VariantClear(&val->u.var);
209 return;
210 case EXPRVAL_IDREF:
211 if(val->u.idref.disp)
212 IDispatch_Release(val->u.idref.disp);
213 return;
214 case EXPRVAL_INVALID:
215 return;
216 }
217 }
218
219 /* ECMA-262 3rd Edition 8.7.1 */
220 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
221 {
222 V_VT(ret) = VT_EMPTY;
223
224 switch(val->type) {
225 case EXPRVAL_VARIANT:
226 return VariantCopy(ret, &val->u.var);
227 case EXPRVAL_IDREF:
228 if(!val->u.idref.disp) {
229 FIXME("throw ReferenceError\n");
230 return E_FAIL;
231 }
232
233 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei, NULL/*FIXME*/);
234 case EXPRVAL_INVALID:
235 assert(0);
236 }
237
238 ERR("type %d\n", val->type);
239 return E_FAIL;
240 }
241
242 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
243 {
244 if(val->type == EXPRVAL_VARIANT) {
245 *ret = val->u.var;
246 V_VT(&val->u.var) = VT_EMPTY;
247 return S_OK;
248 }
249
250 return exprval_value(ctx, val, ei, ret);
251 }
252
253 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
254 {
255 val->type = EXPRVAL_IDREF;
256 val->u.idref.disp = disp;
257 val->u.idref.id = id;
258
259 if(disp)
260 IDispatch_AddRef(disp);
261 }
262
263 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
264 {
265 scope_chain_t *new_scope;
266
267 new_scope = heap_alloc(sizeof(scope_chain_t));
268 if(!new_scope)
269 return E_OUTOFMEMORY;
270
271 new_scope->ref = 1;
272
273 jsdisp_addref(obj);
274 new_scope->obj = obj;
275
276 if(scope) {
277 scope_addref(scope);
278 new_scope->next = scope;
279 }else {
280 new_scope->next = NULL;
281 }
282
283 *ret = new_scope;
284 return S_OK;
285 }
286
287 static void scope_pop(scope_chain_t **scope)
288 {
289 scope_chain_t *tmp;
290
291 tmp = *scope;
292 *scope = tmp->next;
293 scope_release(tmp);
294 }
295
296 void scope_release(scope_chain_t *scope)
297 {
298 if(--scope->ref)
299 return;
300
301 if(scope->next)
302 scope_release(scope->next);
303
304 jsdisp_release(scope->obj);
305 heap_free(scope);
306 }
307
308 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
309 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
310 {
311 exec_ctx_t *ctx;
312
313 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
314 if(!ctx)
315 return E_OUTOFMEMORY;
316
317 ctx->ref = 1;
318 ctx->is_global = is_global;
319
320 if(this_obj)
321 ctx->this_obj = this_obj;
322 else if(script_ctx->host_global)
323 ctx->this_obj = script_ctx->host_global;
324 else
325 ctx->this_obj = to_disp(script_ctx->global);
326 IDispatch_AddRef(ctx->this_obj);
327
328 jsdisp_addref(var_disp);
329 ctx->var_disp = var_disp;
330
331 if(scope) {
332 scope_addref(scope);
333 ctx->scope_chain = scope;
334 }
335
336 *ret = ctx;
337 return S_OK;
338 }
339
340 void exec_release(exec_ctx_t *ctx)
341 {
342 if(--ctx->ref)
343 return;
344
345 if(ctx->scope_chain)
346 scope_release(ctx->scope_chain);
347 if(ctx->var_disp)
348 jsdisp_release(ctx->var_disp);
349 if(ctx->this_obj)
350 IDispatch_Release(ctx->this_obj);
351 heap_free(ctx->stack);
352 heap_free(ctx);
353 }
354
355 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
356 {
357 IDispatchEx *dispex;
358 HRESULT hres;
359
360 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
361 if(FAILED(hres)) {
362 TRACE("unsing IDispatch\n");
363
364 *id = 0;
365 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
366 }
367
368 *id = 0;
369 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
370 IDispatchEx_Release(dispex);
371 return hres;
372 }
373
374 static inline BOOL is_null(const VARIANT *v)
375 {
376 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
377 }
378
379 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
380 {
381 IObjectIdentity *identity;
382 IUnknown *unk1, *unk2;
383 HRESULT hres;
384
385 if(disp1 == disp2) {
386 *ret = TRUE;
387 return S_OK;
388 }
389
390 if(!disp1 || !disp2) {
391 *ret = FALSE;
392 return S_OK;
393 }
394
395 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
396 if(FAILED(hres))
397 return hres;
398
399 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
400 if(FAILED(hres)) {
401 IUnknown_Release(unk1);
402 return hres;
403 }
404
405 if(unk1 == unk2) {
406 *ret = TRUE;
407 }else {
408 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
409 if(SUCCEEDED(hres)) {
410 hres = IObjectIdentity_IsEqualObject(identity, unk2);
411 IObjectIdentity_Release(identity);
412 *ret = hres == S_OK;
413 }else {
414 *ret = FALSE;
415 }
416 }
417
418 IUnknown_Release(unk1);
419 IUnknown_Release(unk2);
420 return S_OK;
421 }
422
423 /* ECMA-262 3rd Edition 11.9.6 */
424 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
425 {
426 TRACE("\n");
427
428 if(V_VT(lval) != V_VT(rval)) {
429 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
430 *ret = num_val(lval) == num_val(rval);
431 else if(is_null(lval))
432 *ret = is_null(rval);
433 else
434 *ret = FALSE;
435 return S_OK;
436 }
437
438 switch(V_VT(lval)) {
439 case VT_EMPTY:
440 case VT_NULL:
441 *ret = VARIANT_TRUE;
442 break;
443 case VT_I4:
444 *ret = V_I4(lval) == V_I4(rval);
445 break;
446 case VT_R8:
447 *ret = V_R8(lval) == V_R8(rval);
448 break;
449 case VT_BSTR:
450 if(!V_BSTR(lval))
451 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
452 else if(!V_BSTR(rval))
453 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
454 else
455 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
456 break;
457 case VT_DISPATCH:
458 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
459 case VT_BOOL:
460 *ret = !V_BOOL(lval) == !V_BOOL(rval);
461 break;
462 default:
463 FIXME("unimplemented vt %d\n", V_VT(lval));
464 return E_NOTIMPL;
465 }
466
467 return S_OK;
468 }
469
470 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
471 {
472 named_item_t *item;
473 DISPID id;
474 HRESULT hres;
475
476 for(item = ctx->named_items; item; item = item->next) {
477 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
478 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
479 if(SUCCEEDED(hres)) {
480 if(ret)
481 exprval_set_idref(ret, item->disp, id);
482 return TRUE;
483 }
484 }
485 }
486
487 return FALSE;
488 }
489
490 /* ECMA-262 3rd Edition 10.1.4 */
491 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
492 {
493 scope_chain_t *scope;
494 named_item_t *item;
495 DISPID id = 0;
496 HRESULT hres;
497
498 TRACE("%s\n", debugstr_w(identifier));
499
500 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
501 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
502 if(SUCCEEDED(hres)) {
503 exprval_set_idref(ret, to_disp(scope->obj), id);
504 return S_OK;
505 }
506 }
507
508 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
509 if(SUCCEEDED(hres)) {
510 exprval_set_idref(ret, to_disp(ctx->global), id);
511 return S_OK;
512 }
513
514 for(item = ctx->named_items; item; item = item->next) {
515 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
516 if(!item->disp) {
517 IUnknown *unk;
518
519 if(!ctx->site)
520 break;
521
522 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
523 SCRIPTINFO_IUNKNOWN, &unk, NULL);
524 if(FAILED(hres)) {
525 WARN("GetItemInfo failed: %08x\n", hres);
526 break;
527 }
528
529 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
530 IUnknown_Release(unk);
531 if(FAILED(hres)) {
532 WARN("object does not implement IDispatch\n");
533 break;
534 }
535 }
536
537 ret->type = EXPRVAL_VARIANT;
538 V_VT(&ret->u.var) = VT_DISPATCH;
539 V_DISPATCH(&ret->u.var) = item->disp;
540 IDispatch_AddRef(item->disp);
541 return S_OK;
542 }
543 }
544
545 if(lookup_global_members(ctx, identifier, ret))
546 return S_OK;
547
548 ret->type = EXPRVAL_INVALID;
549 return S_OK;
550 }
551
552 /* ECMA-262 3rd Edition 12.2 */
553 static HRESULT interp_var_set(exec_ctx_t *ctx)
554 {
555 const BSTR name = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
556 VARIANT *v;
557 HRESULT hres;
558
559 TRACE("%s\n", debugstr_w(name));
560
561 v = stack_pop(ctx);
562 hres = jsdisp_propput_name(ctx->var_disp, name, v, ctx->ei, NULL/*FIXME*/);
563 VariantClear(v);
564 return hres;
565 }
566
567 /* ECMA-262 3rd Edition 12.6.4 */
568 static HRESULT interp_forin(exec_ctx_t *ctx)
569 {
570 const HRESULT arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
571 IDispatch *var_obj, *obj = NULL;
572 IDispatchEx *dispex;
573 DISPID id, var_id;
574 BSTR name = NULL;
575 VARIANT *val;
576 HRESULT hres;
577
578 TRACE("\n");
579
580 val = stack_pop(ctx);
581
582 assert(V_VT(stack_top(ctx)) == VT_I4);
583 id = V_I4(stack_top(ctx));
584
585 var_obj = stack_topn_objid(ctx, 1, &var_id);
586 if(!var_obj) {
587 FIXME("invalid ref\n");
588 VariantClear(val);
589 return E_FAIL;
590 }
591
592 if(V_VT(stack_topn(ctx, 3)) == VT_DISPATCH)
593 obj = V_DISPATCH(stack_topn(ctx, 3));
594
595 if(obj) {
596 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
597 if(SUCCEEDED(hres)) {
598 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
599 if(hres == S_OK)
600 hres = IDispatchEx_GetMemberName(dispex, id, &name);
601 IDispatchEx_Release(dispex);
602 if(FAILED(hres)) {
603 VariantClear(val);
604 return hres;
605 }
606 }else {
607 TRACE("No IDispatchEx\n");
608 }
609 }
610
611 if(name) {
612 VARIANT v;
613
614 VariantClear(val);
615
616 V_I4(stack_top(ctx)) = id;
617
618 V_VT(&v) = VT_BSTR;
619 V_BSTR(&v) = name;
620 hres = disp_propput(ctx->parser->script, var_obj, var_id, &v, ctx->ei, NULL/*FIXME*/);
621 SysFreeString(name);
622 if(FAILED(hres))
623 return hres;
624
625 ctx->ip++;
626 }else {
627 stack_popn(ctx, 4);
628 ctx->ip = arg;
629 return stack_push(ctx, val);
630 }
631 return S_OK;
632 }
633
634 /* ECMA-262 3rd Edition 12.10 */
635 static HRESULT interp_push_scope(exec_ctx_t *ctx)
636 {
637 IDispatch *disp;
638 jsdisp_t *obj;
639 VARIANT *v;
640 HRESULT hres;
641
642 TRACE("\n");
643
644 v = stack_pop(ctx);
645 hres = to_object(ctx->parser->script, v, &disp);
646 VariantClear(v);
647 if(FAILED(hres))
648 return hres;
649
650 obj = to_jsdisp(disp);
651 if(!obj) {
652 IDispatch_Release(disp);
653 FIXME("disp is not jsdisp\n");
654 return E_NOTIMPL;
655 }
656
657 hres = scope_push(ctx->scope_chain, obj, &ctx->scope_chain);
658 jsdisp_release(obj);
659 return hres;
660 }
661
662 /* ECMA-262 3rd Edition 12.10 */
663 static HRESULT interp_pop_scope(exec_ctx_t *ctx)
664 {
665 TRACE("\n");
666
667 scope_pop(&ctx->scope_chain);
668 return S_OK;
669 }
670
671 /* ECMA-262 3rd Edition 12.13 */
672 static HRESULT interp_case(exec_ctx_t *ctx)
673 {
674 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
675 VARIANT *v;
676 BOOL b;
677 HRESULT hres;
678
679 TRACE("\n");
680
681 v = stack_pop(ctx);
682 hres = equal2_values(stack_top(ctx), v, &b);
683 VariantClear(v);
684 if(FAILED(hres))
685 return hres;
686
687 if(b) {
688 stack_popn(ctx, 1);
689 ctx->ip = arg;
690 }else {
691 ctx->ip++;
692 }
693 return S_OK;
694 }
695
696 /* ECMA-262 3rd Edition 12.13 */
697 static HRESULT interp_throw(exec_ctx_t *ctx)
698 {
699 TRACE("\n");
700
701 ctx->ei->var = *stack_pop(ctx);
702 return DISP_E_EXCEPTION;
703 }
704
705 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
706 {
707 const HRESULT arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
708
709 TRACE("%08x\n", arg);
710
711 return throw_reference_error(ctx->parser->script, ctx->ei, arg, NULL);
712 }
713
714 static HRESULT interp_throw_type(exec_ctx_t *ctx)
715 {
716 const HRESULT hres = ctx->parser->code->instrs[ctx->ip].arg1.uint;
717 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg2.str;
718
719 TRACE("%08x %s\n", hres, debugstr_w(str));
720
721 return throw_type_error(ctx->parser->script, ctx->ei, hres, str);
722 }
723
724 /* ECMA-262 3rd Edition 12.14 */
725 static HRESULT interp_push_except(exec_ctx_t *ctx)
726 {
727 const unsigned arg1 = ctx->parser->code->instrs[ctx->ip].arg1.uint;
728 const BSTR arg2 = ctx->parser->code->instrs[ctx->ip].arg2.bstr;
729 except_frame_t *except;
730 unsigned stack_top;
731
732 TRACE("\n");
733
734 stack_top = ctx->top;
735
736 if(!arg2) {
737 HRESULT hres;
738
739 hres = stack_push_bool(ctx, TRUE);
740 if(FAILED(hres))
741 return hres;
742 hres = stack_push_bool(ctx, TRUE);
743 if(FAILED(hres))
744 return hres;
745 }
746
747 except = heap_alloc(sizeof(*except));
748 if(!except)
749 return E_OUTOFMEMORY;
750
751 except->stack_top = stack_top;
752 except->scope = ctx->scope_chain;
753 except->catch_off = arg1;
754 except->ident = arg2;
755 except->next = ctx->except_frame;
756 ctx->except_frame = except;
757 return S_OK;
758 }
759
760 /* ECMA-262 3rd Edition 12.14 */
761 static HRESULT interp_pop_except(exec_ctx_t *ctx)
762 {
763 except_frame_t *except;
764
765 TRACE("\n");
766
767 except = ctx->except_frame;
768 assert(except != NULL);
769
770 ctx->except_frame = except->next;
771 heap_free(except);
772 return S_OK;
773 }
774
775 /* ECMA-262 3rd Edition 12.14 */
776 static HRESULT interp_end_finally(exec_ctx_t *ctx)
777 {
778 VARIANT *v;
779
780 TRACE("\n");
781
782 v = stack_pop(ctx);
783
784 assert(V_VT(stack_top(ctx)) == VT_BOOL);
785 if(!V_BOOL(stack_top(ctx))) {
786 TRACE("passing exception\n");
787
788 VariantClear(v);
789 stack_popn(ctx, 1);
790 ctx->ei->var = *stack_pop(ctx);
791 return DISP_E_EXCEPTION;
792 }
793
794 stack_popn(ctx, 2);
795 return stack_push(ctx, v);
796 }
797
798 /* ECMA-262 3rd Edition 13 */
799 static HRESULT interp_func(exec_ctx_t *ctx)
800 {
801 function_expression_t *expr = ctx->parser->code->instrs[ctx->ip].arg1.func;
802 jsdisp_t *dispex;
803 VARIANT v;
804 HRESULT hres;
805
806 TRACE("\n");
807
808 hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain,
809 expr->src_str, expr->src_len, &dispex);
810 if(FAILED(hres))
811 return hres;
812
813 var_set_jsdisp(&v, dispex);
814 return stack_push(ctx, &v);
815 }
816
817 /* ECMA-262 3rd Edition 11.2.1 */
818 static HRESULT interp_array(exec_ctx_t *ctx)
819 {
820 VARIANT v, *namev;
821 IDispatch *obj;
822 DISPID id;
823 BSTR name;
824 HRESULT hres;
825
826 TRACE("\n");
827
828 namev = stack_pop(ctx);
829
830 hres = stack_pop_object(ctx, &obj);
831 if(FAILED(hres)) {
832 VariantClear(namev);
833 return hres;
834 }
835
836 hres = to_string(ctx->parser->script, namev, ctx->ei, &name);
837 VariantClear(namev);
838 if(FAILED(hres)) {
839 IDispatch_Release(obj);
840 return hres;
841 }
842
843 hres = disp_get_id(ctx->parser->script, obj, name, 0, &id);
844 SysFreeString(name);
845 if(SUCCEEDED(hres)) {
846 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
847 }else if(hres == DISP_E_UNKNOWNNAME) {
848 V_VT(&v) = VT_EMPTY;
849 hres = S_OK;
850 }
851 IDispatch_Release(obj);
852 if(FAILED(hres))
853 return hres;
854
855 return stack_push(ctx, &v);
856 }
857
858 /* ECMA-262 3rd Edition 11.2.1 */
859 static HRESULT interp_member(exec_ctx_t *ctx)
860 {
861 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
862 IDispatch *obj;
863 VARIANT v;
864 DISPID id;
865 HRESULT hres;
866
867 TRACE("\n");
868
869 hres = stack_pop_object(ctx, &obj);
870 if(FAILED(hres))
871 return hres;
872
873 hres = disp_get_id(ctx->parser->script, obj, arg, 0, &id);
874 if(SUCCEEDED(hres)) {
875 V_VT(&v) = VT_EMPTY;
876 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
877 }else if(hres == DISP_E_UNKNOWNNAME) {
878 V_VT(&v) = VT_EMPTY;
879 hres = S_OK;
880 }
881 IDispatch_Release(obj);
882 if(FAILED(hres))
883 return hres;
884
885 return stack_push(ctx, &v);
886 }
887
888 /* ECMA-262 3rd Edition 11.2.1 */
889 static HRESULT interp_memberid(exec_ctx_t *ctx)
890 {
891 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
892 VARIANT *objv, *namev;
893 IDispatch *obj;
894 BSTR name;
895 DISPID id;
896 HRESULT hres;
897
898 TRACE("%x\n", arg);
899
900 namev = stack_pop(ctx);
901 objv = stack_pop(ctx);
902
903 hres = to_object(ctx->parser->script, objv, &obj);
904 VariantClear(objv);
905 if(SUCCEEDED(hres)) {
906 hres = to_string(ctx->parser->script, namev, ctx->ei, &name);
907 if(FAILED(hres))
908 IDispatch_Release(obj);
909 }
910 VariantClear(namev);
911 if(FAILED(hres))
912 return hres;
913
914 hres = disp_get_id(ctx->parser->script, obj, name, arg, &id);
915 SysFreeString(name);
916 if(FAILED(hres)) {
917 IDispatch_Release(obj);
918 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
919 obj = NULL;
920 id = JS_E_INVALID_PROPERTY;
921 }else {
922 return hres;
923 }
924 }
925
926 return stack_push_objid(ctx, obj, id);
927 }
928
929 /* ECMA-262 3rd Edition 11.2.1 */
930 static HRESULT interp_refval(exec_ctx_t *ctx)
931 {
932 IDispatch *disp;
933 VARIANT v;
934 DISPID id;
935 HRESULT hres;
936
937 TRACE("\n");
938
939 disp = stack_topn_objid(ctx, 0, &id);
940 if(!disp)
941 return throw_reference_error(ctx->parser->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
942
943 hres = disp_propget(ctx->parser->script, disp, id, &v, ctx->ei, NULL/*FIXME*/);
944 if(FAILED(hres))
945 return hres;
946
947 return stack_push(ctx, &v);
948 }
949
950 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
951 {
952 VARIANT tmp;
953 unsigned i;
954
955 dp->cArgs = arg_cnt;
956 dp->rgdispidNamedArgs = NULL;
957 dp->cNamedArgs = 0;
958
959 assert(ctx->top >= arg_cnt);
960
961 for(i=1; i*2 <= arg_cnt; i++) {
962 tmp = ctx->stack[ctx->top-i];
963 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
964 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
965 }
966
967 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
968 }
969
970 /* ECMA-262 3rd Edition 11.2.2 */
971 static HRESULT interp_new(exec_ctx_t *ctx)
972 {
973 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
974 VARIANT *constr, v;
975 DISPPARAMS dp;
976 HRESULT hres;
977
978 TRACE("%d\n", arg);
979
980 constr = stack_topn(ctx, arg);
981
982 /* NOTE: Should use to_object here */
983
984 if(V_VT(constr) == VT_NULL)
985 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
986 else if(V_VT(constr) != VT_DISPATCH)
987 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_INVALID_ACTION, NULL);
988 else if(!V_DISPATCH(constr))
989 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
990
991 jsstack_to_dp(ctx, arg, &dp);
992 hres = disp_call(ctx->parser->script, V_DISPATCH(constr), DISPID_VALUE,
993 DISPATCH_CONSTRUCT, &dp, &v, ctx->ei, NULL/*FIXME*/);
994 if(FAILED(hres))
995 return hres;
996
997 stack_popn(ctx, arg+1);
998 return stack_push(ctx, &v);
999 }
1000
1001 /* ECMA-262 3rd Edition 11.2.3 */
1002 static HRESULT interp_call(exec_ctx_t *ctx)
1003 {
1004 const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1005 const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1006 VARIANT v, *objv;
1007 DISPPARAMS dp;
1008 HRESULT hres;
1009
1010 TRACE("%d %d\n", argn, do_ret);
1011
1012 objv = stack_topn(ctx, argn);
1013 if(V_VT(objv) != VT_DISPATCH)
1014 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1015
1016 jsstack_to_dp(ctx, argn, &dp);
1017 hres = disp_call(ctx->parser->script, V_DISPATCH(objv), DISPID_VALUE, DISPATCH_METHOD, &dp,
1018 do_ret ? &v : NULL, ctx->ei, NULL/*FIXME*/);
1019 if(FAILED(hres))
1020 return hres;
1021
1022 stack_popn(ctx, argn+1);
1023 return do_ret ? stack_push(ctx, &v) : S_OK;
1024
1025 }
1026
1027 /* ECMA-262 3rd Edition 11.2.3 */
1028 static HRESULT interp_call_member(exec_ctx_t *ctx)
1029 {
1030 const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1031 const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1032 IDispatch *obj;
1033 DISPPARAMS dp;
1034 VARIANT v;
1035 DISPID id;
1036 HRESULT hres;
1037
1038 TRACE("%d %d\n", argn, do_ret);
1039
1040 obj = stack_topn_objid(ctx, argn, &id);
1041 if(!obj)
1042 return throw_type_error(ctx->parser->script, ctx->ei, id, NULL);
1043
1044 jsstack_to_dp(ctx, argn, &dp);
1045 hres = disp_call(ctx->parser->script, obj, id, DISPATCH_METHOD, &dp, do_ret ? &v : NULL, ctx->ei, NULL/*FIXME*/);
1046 if(FAILED(hres))
1047 return hres;
1048
1049 stack_popn(ctx, argn+2);
1050 return do_ret ? stack_push(ctx, &v) : S_OK;
1051
1052 }
1053
1054 /* ECMA-262 3rd Edition 11.1.1 */
1055 static HRESULT interp_this(exec_ctx_t *ctx)
1056 {
1057 VARIANT v;
1058
1059 TRACE("\n");
1060
1061 V_VT(&v) = VT_DISPATCH;
1062 V_DISPATCH(&v) = ctx->this_obj;
1063 IDispatch_AddRef(ctx->this_obj);
1064 return stack_push(ctx, &v);
1065 }
1066
1067 /* ECMA-262 3rd Edition 10.1.4 */
1068 static HRESULT interp_ident(exec_ctx_t *ctx)
1069 {
1070 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1071 exprval_t exprval;
1072 VARIANT v;
1073 HRESULT hres;
1074
1075 TRACE("%s\n", debugstr_w(arg));
1076
1077 hres = identifier_eval(ctx->parser->script, arg, &exprval);
1078 if(FAILED(hres))
1079 return hres;
1080
1081 if(exprval.type == EXPRVAL_INVALID)
1082 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_UNDEFINED_VARIABLE, arg);
1083
1084 hres = exprval_to_value(ctx->parser->script, &exprval, ctx->ei, &v);
1085 exprval_release(&exprval);
1086 if(FAILED(hres))
1087 return hres;
1088
1089 return stack_push(ctx, &v);
1090 }
1091
1092 /* ECMA-262 3rd Edition 10.1.4 */
1093 static HRESULT interp_identid(exec_ctx_t *ctx)
1094 {
1095 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1096 const unsigned flags = ctx->parser->code->instrs[ctx->ip].arg2.uint;
1097 exprval_t exprval;
1098 HRESULT hres;
1099
1100 TRACE("%s %x\n", debugstr_w(arg), flags);
1101
1102 hres = identifier_eval(ctx->parser->script, arg, &exprval);
1103 if(FAILED(hres))
1104 return hres;
1105
1106 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1107 DISPID id;
1108
1109 hres = jsdisp_get_id(ctx->parser->script->global, arg, fdexNameEnsure, &id);
1110 if(FAILED(hres))
1111 return hres;
1112
1113 exprval_set_idref(&exprval, to_disp(ctx->parser->script->global), id);
1114 }
1115
1116 if(exprval.type != EXPRVAL_IDREF) {
1117 WARN("invalid ref\n");
1118 exprval_release(&exprval);
1119 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1120 }
1121
1122 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1123 }
1124
1125 /* ECMA-262 3rd Edition 7.8.1 */
1126 static HRESULT interp_null(exec_ctx_t *ctx)
1127 {
1128 VARIANT v;
1129
1130 TRACE("\n");
1131
1132 V_VT(&v) = VT_NULL;
1133 return stack_push(ctx, &v);
1134 }
1135
1136 /* ECMA-262 3rd Edition 7.8.2 */
1137 static HRESULT interp_bool(exec_ctx_t *ctx)
1138 {
1139 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1140
1141 TRACE("%s\n", arg ? "true" : "false");
1142
1143 return stack_push_bool(ctx, arg);
1144 }
1145
1146 /* ECMA-262 3rd Edition 7.8.3 */
1147 static HRESULT interp_int(exec_ctx_t *ctx)
1148 {
1149 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1150 VARIANT v;
1151
1152 TRACE("%d\n", arg);
1153
1154 V_VT(&v) = VT_I4;
1155 V_I4(&v) = arg;
1156 return stack_push(ctx, &v);
1157 }
1158
1159 /* ECMA-262 3rd Edition 7.8.3 */
1160 static HRESULT interp_double(exec_ctx_t *ctx)
1161 {
1162 const double arg = *ctx->parser->code->instrs[ctx->ip].arg1.dbl;
1163 VARIANT v;
1164
1165 TRACE("%lf\n", arg);
1166
1167 V_VT(&v) = VT_R8;
1168 V_R8(&v) = arg;
1169 return stack_push(ctx, &v);
1170 }
1171
1172 /* ECMA-262 3rd Edition 7.8.4 */
1173 static HRESULT interp_str(exec_ctx_t *ctx)
1174 {
1175 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg1.str;
1176 VARIANT v;
1177
1178 TRACE("%s\n", debugstr_w(str));
1179
1180 V_VT(&v) = VT_BSTR;
1181 V_BSTR(&v) = SysAllocString(str);
1182 if(!V_BSTR(&v))
1183 return E_OUTOFMEMORY;
1184
1185 return stack_push(ctx, &v);
1186 }
1187
1188 /* ECMA-262 3rd Edition 7.8 */
1189 static HRESULT interp_regexp(exec_ctx_t *ctx)
1190 {
1191 const WCHAR *source = ctx->parser->code->instrs[ctx->ip].arg1.str;
1192 const LONG flags = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1193 jsdisp_t *regexp;
1194 VARIANT v;
1195 HRESULT hres;
1196
1197 TRACE("%s %x\n", debugstr_w(source), flags);
1198
1199 hres = create_regexp(ctx->parser->script, source, strlenW(source), flags, ®exp);
1200 if(FAILED(hres))
1201 return hres;
1202
1203 var_set_jsdisp(&v, regexp);
1204 return stack_push(ctx, &v);
1205 }
1206
1207 /* ECMA-262 3rd Edition 11.1.4 */
1208 static HRESULT interp_carray(exec_ctx_t *ctx)
1209 {
1210 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1211 jsdisp_t *array;
1212 VARIANT *v, r;
1213 unsigned i;
1214 HRESULT hres;
1215
1216 TRACE("%u\n", arg);
1217
1218 hres = create_array(ctx->parser->script, arg, &array);
1219 if(FAILED(hres))
1220 return hres;
1221
1222 i = arg;
1223 while(i--) {
1224 v = stack_pop(ctx);
1225 hres = jsdisp_propput_idx(array, i, v, ctx->ei, NULL/*FIXME*/);
1226 VariantClear(v);
1227 if(FAILED(hres)) {
1228 jsdisp_release(array);
1229 return hres;
1230 }
1231 }
1232
1233 var_set_jsdisp(&r, array);
1234 return stack_push(ctx, &r);
1235 }
1236
1237 /* ECMA-262 3rd Edition 11.1.5 */
1238 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1239 {
1240 jsdisp_t *obj;
1241 VARIANT v;
1242 HRESULT hres;
1243
1244 TRACE("\n");
1245
1246 hres = create_object(ctx->parser->script, NULL, &obj);
1247 if(FAILED(hres))
1248 return hres;
1249
1250 var_set_jsdisp(&v, obj);
1251 return stack_push(ctx, &v);
1252 }
1253
1254 /* ECMA-262 3rd Edition 11.1.5 */
1255 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1256 {
1257 const BSTR name = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1258 jsdisp_t *obj;
1259 VARIANT *v;
1260 HRESULT hres;
1261
1262 TRACE("%s\n", debugstr_w(name));
1263
1264 v = stack_pop(ctx);
1265
1266 assert(V_VT(stack_top(ctx)) == VT_DISPATCH);
1267 obj = as_jsdisp(V_DISPATCH(stack_top(ctx)));
1268
1269 hres = jsdisp_propput_name(obj, name, v, ctx->ei, NULL/*FIXME*/);
1270 VariantClear(v);
1271 return hres;
1272 }
1273
1274 /* ECMA-262 3rd Edition 11.11 */
1275 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1276 {
1277 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1278 VARIANT_BOOL b;
1279 HRESULT hres;
1280
1281 TRACE("\n");
1282
1283 hres = to_boolean(stack_top(ctx), &b);
1284 if(FAILED(hres))
1285 return hres;
1286
1287 if(b) {
1288 ctx->ip = arg;
1289 }else {
1290 stack_popn(ctx, 1);
1291 ctx->ip++;
1292 }
1293 return S_OK;
1294 }
1295
1296 /* ECMA-262 3rd Edition 11.11 */
1297 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1298 {
1299 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1300 VARIANT_BOOL b;
1301 HRESULT hres;
1302
1303 TRACE("\n");
1304
1305 hres = to_boolean(stack_top(ctx), &b);
1306 if(FAILED(hres))
1307 return hres;
1308
1309 if(b) {
1310 stack_popn(ctx, 1);
1311 ctx->ip++;
1312 }else {
1313 ctx->ip = arg;
1314 }
1315 return S_OK;
1316 }
1317
1318 /* ECMA-262 3rd Edition 11.10 */
1319 static HRESULT interp_or(exec_ctx_t *ctx)
1320 {
1321 INT l, r;
1322 HRESULT hres;
1323
1324 TRACE("\n");
1325
1326 hres = stack_pop_int(ctx, &r);
1327 if(FAILED(hres))
1328 return hres;
1329
1330 hres = stack_pop_int(ctx, &l);
1331 if(FAILED(hres))
1332 return hres;
1333
1334 return stack_push_int(ctx, l|r);
1335 }
1336
1337 /* ECMA-262 3rd Edition 11.10 */
1338 static HRESULT interp_xor(exec_ctx_t *ctx)
1339 {
1340 INT l, r;
1341 HRESULT hres;
1342
1343 TRACE("\n");
1344
1345 hres = stack_pop_int(ctx, &r);
1346 if(FAILED(hres))
1347 return hres;
1348
1349 hres = stack_pop_int(ctx, &l);
1350 if(FAILED(hres))
1351 return hres;
1352
1353 return stack_push_int(ctx, l^r);
1354 }
1355
1356 /* ECMA-262 3rd Edition 11.10 */
1357 static HRESULT interp_and(exec_ctx_t *ctx)
1358 {
1359 INT l, r;
1360 HRESULT hres;
1361
1362 TRACE("\n");
1363
1364 hres = stack_pop_int(ctx, &r);
1365 if(FAILED(hres))
1366 return hres;
1367
1368 hres = stack_pop_int(ctx, &l);
1369 if(FAILED(hres))
1370 return hres;
1371
1372 return stack_push_int(ctx, l&r);
1373 }
1374
1375 /* ECMA-262 3rd Edition 11.8.6 */
1376 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1377 {
1378 jsdisp_t *obj, *iter, *tmp = NULL;
1379 VARIANT prot, *v;
1380 BOOL ret = FALSE;
1381 HRESULT hres;
1382
1383 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1384
1385 v = stack_pop(ctx);
1386 if(V_VT(v) != VT_DISPATCH || !V_DISPATCH(v)) {
1387 VariantClear(v);
1388 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1389 }
1390
1391 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1392 IDispatch_Release(V_DISPATCH(v));
1393 if(!obj) {
1394 FIXME("non-jsdisp objects not supported\n");
1395 return E_FAIL;
1396 }
1397
1398 if(is_class(obj, JSCLASS_FUNCTION)) {
1399 hres = jsdisp_propget_name(obj, prototypeW, &prot, ctx->ei, NULL/*FIXME*/);
1400 }else {
1401 hres = throw_type_error(ctx->parser->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1402 }
1403 jsdisp_release(obj);
1404 if(FAILED(hres))
1405 return hres;
1406
1407 v = stack_pop(ctx);
1408
1409 if(V_VT(&prot) == VT_DISPATCH) {
1410 if(V_VT(v) == VT_DISPATCH)
1411 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1412 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1413 hres = disp_cmp(V_DISPATCH(&prot), to_disp(iter), &ret);
1414 if(FAILED(hres))
1415 break;
1416 }
1417
1418 if(tmp)
1419 jsdisp_release(tmp);
1420 }else {
1421 FIXME("prototype is not an object\n");
1422 hres = E_FAIL;
1423 }
1424
1425 VariantClear(&prot);
1426 VariantClear(v);
1427 if(FAILED(hres))
1428 return hres;
1429
1430 return stack_push_bool(ctx, ret);
1431 }
1432
1433 /* ECMA-262 3rd Edition 11.8.7 */
1434 static HRESULT interp_in(exec_ctx_t *ctx)
1435 {
1436 VARIANT *obj, *v;
1437 DISPID id = 0;
1438 BOOL ret;
1439 BSTR str;
1440 HRESULT hres;
1441
1442 TRACE("\n");
1443
1444 obj = stack_pop(ctx);
1445 v = stack_pop(ctx);
1446
1447 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
1448 VariantClear(obj);
1449 VariantClear(v);
1450 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1451 }
1452
1453 hres = to_string(ctx->parser->script, v, ctx->ei, &str);
1454 VariantClear(v);
1455 if(FAILED(hres)) {
1456 IDispatch_Release(V_DISPATCH(obj));
1457 return hres;
1458 }
1459
1460 hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
1461 IDispatch_Release(V_DISPATCH(obj));
1462 SysFreeString(str);
1463 if(SUCCEEDED(hres))
1464 ret = TRUE;
1465 else if(hres == DISP_E_UNKNOWNNAME)
1466 ret = FALSE;
1467 else
1468 return hres;
1469
1470 return stack_push_bool(ctx, ret);
1471 }
1472
1473 /* ECMA-262 3rd Edition 11.6.1 */
1474 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1475 {
1476 VARIANT r, l;
1477 HRESULT hres;
1478
1479 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
1480 if(FAILED(hres))
1481 return hres;
1482
1483 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
1484 if(FAILED(hres)) {
1485 VariantClear(&l);
1486 return hres;
1487 }
1488
1489 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1490 BSTR lstr = NULL, rstr = NULL;
1491
1492 if(V_VT(&l) == VT_BSTR)
1493 lstr = V_BSTR(&l);
1494 else
1495 hres = to_string(ctx, &l, ei, &lstr);
1496
1497 if(SUCCEEDED(hres)) {
1498 if(V_VT(&r) == VT_BSTR)
1499 rstr = V_BSTR(&r);
1500 else
1501 hres = to_string(ctx, &r, ei, &rstr);
1502 }
1503
1504 if(SUCCEEDED(hres)) {
1505 int len1, len2;
1506
1507 len1 = SysStringLen(lstr);
1508 len2 = SysStringLen(rstr);
1509
1510 V_VT(retv) = VT_BSTR;
1511 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1512 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1513 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
1514 }
1515
1516 if(V_VT(&l) != VT_BSTR)
1517 SysFreeString(lstr);
1518 if(V_VT(&r) != VT_BSTR)
1519 SysFreeString(rstr);
1520 }else {
1521 VARIANT nl, nr;
1522
1523 hres = to_number(ctx, &l, ei, &nl);
1524 if(SUCCEEDED(hres)) {
1525 hres = to_number(ctx, &r, ei, &nr);
1526 if(SUCCEEDED(hres))
1527 num_set_val(retv, num_val(&nl) + num_val(&nr));
1528 }
1529 }
1530
1531 VariantClear(&r);
1532 VariantClear(&l);
1533 return hres;
1534 }
1535
1536 /* ECMA-262 3rd Edition 11.6.1 */
1537 static HRESULT interp_add(exec_ctx_t *ctx)
1538 {
1539 VARIANT *l, *r, ret;
1540 HRESULT hres;
1541
1542 r = stack_pop(ctx);
1543 l = stack_pop(ctx);
1544
1545 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
1546
1547 hres = add_eval(ctx->parser->script, l, r, ctx->ei, &ret);
1548 VariantClear(l);
1549 VariantClear(r);
1550 if(FAILED(hres))
1551 return hres;
1552
1553 return stack_push(ctx, &ret);
1554 }
1555
1556 /* ECMA-262 3rd Edition 11.6.2 */
1557 static HRESULT interp_sub(exec_ctx_t *ctx)
1558 {
1559 VARIANT l, r;
1560 HRESULT hres;
1561
1562 TRACE("\n");
1563
1564 hres = stack_pop_number(ctx, &r);
1565 if(FAILED(hres))
1566 return hres;
1567
1568 hres = stack_pop_number(ctx, &l);
1569 if(FAILED(hres))
1570 return hres;
1571
1572 return stack_push_number(ctx, num_val(&l)-num_val(&r));
1573 }
1574
1575 /* ECMA-262 3rd Edition 11.5.1 */
1576 static HRESULT interp_mul(exec_ctx_t *ctx)
1577 {
1578 VARIANT l, r;
1579 HRESULT hres;
1580
1581 TRACE("\n");
1582
1583 hres = stack_pop_number(ctx, &r);
1584 if(FAILED(hres))
1585 return hres;
1586
1587 hres = stack_pop_number(ctx, &l);
1588 if(FAILED(hres))
1589 return hres;
1590
1591 return stack_push_number(ctx, num_val(&l)*num_val(&r));
1592 }
1593
1594 /* ECMA-262 3rd Edition 11.5.2 */
1595 static HRESULT interp_div(exec_ctx_t *ctx)
1596 {
1597 VARIANT l, r;
1598 HRESULT hres;
1599
1600 TRACE("\n");
1601
1602 hres = stack_pop_number(ctx, &r);
1603 if(FAILED(hres))
1604 return hres;
1605
1606 hres = stack_pop_number(ctx, &l);
1607 if(FAILED(hres))
1608 return hres;
1609
1610 return stack_push_number(ctx, num_val(&l)/num_val(&r));
1611 }
1612
1613 /* ECMA-262 3rd Edition 11.5.3 */
1614 static HRESULT interp_mod(exec_ctx_t *ctx)
1615 {
1616 VARIANT l, r;
1617 HRESULT hres;
1618
1619 TRACE("\n");
1620
1621 hres = stack_pop_number(ctx, &r);
1622 if(FAILED(hres))
1623 return hres;
1624
1625 hres = stack_pop_number(ctx, &l);
1626 if(FAILED(hres))
1627 return hres;
1628
1629 return stack_push_number(ctx, fmod(num_val(&l), num_val(&r)));
1630 }
1631
1632 /* ECMA-262 3rd Edition 11.4.2 */
1633 static HRESULT interp_delete(exec_ctx_t *ctx)
1634 {
1635 VARIANT *obj_var, *name_var;
1636 IDispatchEx *dispex;
1637 IDispatch *obj;
1638 BSTR name;
1639 BOOL ret;
1640 HRESULT hres;
1641
1642 TRACE("\n");
1643
1644 name_var = stack_pop(ctx);
1645 obj_var = stack_pop(ctx);
1646
1647 hres = to_object(ctx->parser->script, obj_var, &obj);
1648 VariantClear(obj_var);
1649 if(FAILED(hres)) {
1650 VariantClear(name_var);
1651 return hres;
1652 }
1653
1654 hres = to_string(ctx->parser->script, name_var, ctx->ei, &name);
1655 VariantClear(name_var);
1656 if(FAILED(hres)) {
1657 IDispatch_Release(obj);
1658 return hres;
1659 }
1660
1661 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
1662 if(SUCCEEDED(hres)) {
1663 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
1664 ret = TRUE;
1665 IDispatchEx_Release(dispex);
1666 }else {
1667 hres = S_OK;
1668 ret = FALSE;
1669 }
1670
1671 IDispatch_Release(obj);
1672 SysFreeString(name);
1673 if(FAILED(hres))
1674 return hres;
1675
1676 return stack_push_bool(ctx, ret);
1677 }
1678
1679 /* ECMA-262 3rd Edition 11.4.2 */
1680 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1681 {
1682 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1683 IDispatchEx *dispex;
1684 exprval_t exprval;
1685 BOOL ret = FALSE;
1686 HRESULT hres;
1687
1688 TRACE("%s\n", debugstr_w(arg));
1689
1690 hres = identifier_eval(ctx->parser->script, arg, &exprval);
1691 if(FAILED(hres))
1692 return hres;
1693
1694 if(exprval.type != EXPRVAL_IDREF) {
1695 FIXME("Unsupported exprval\n");
1696 exprval_release(&exprval);
1697 return E_NOTIMPL;
1698 }
1699
1700 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
1701 IDispatch_Release(exprval.u.idref.disp);
1702 if(SUCCEEDED(hres)) {
1703 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
1704 IDispatchEx_Release(dispex);
1705 if(FAILED(hres))
1706 return hres;
1707
1708 ret = TRUE;
1709 }
1710
1711 return stack_push_bool(ctx, ret);
1712 }
1713
1714 /* ECMA-262 3rd Edition 11.4.2 */
1715 static HRESULT interp_void(exec_ctx_t *ctx)
1716 {
1717 VARIANT v;
1718
1719 TRACE("\n");
1720
1721 stack_popn(ctx, 1);
1722
1723 V_VT(&v) = VT_EMPTY;
1724 return stack_push(ctx, &v);
1725 }
1726
1727 /* ECMA-262 3rd Edition 11.4.3 */
1728 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
1729 {
1730 switch(V_VT(v)) {
1731 case VT_EMPTY:
1732 *ret = undefinedW;
1733 break;
1734 case VT_NULL:
1735 *ret = objectW;
1736 break;
1737 case VT_BOOL:
1738 *ret = booleanW;
1739 break;
1740 case VT_I4:
1741 case VT_R8:
1742 *ret = numberW;
1743 break;
1744 case VT_BSTR:
1745 *ret = stringW;
1746 break;
1747 case VT_DISPATCH: {
1748 jsdisp_t *dispex;
1749
1750 if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
1751 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1752 jsdisp_release(dispex);
1753 }else {
1754 *ret = objectW;
1755 }
1756 break;
1757 }
1758 default:
1759 FIXME("unhandled vt %d\n", V_VT(v));
1760 return E_NOTIMPL;
1761 }
1762
1763 return S_OK;
1764 }
1765
1766 /* ECMA-262 3rd Edition 11.4.3 */
1767 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1768 {
1769 const WCHAR *ret;
1770 IDispatch *obj;
1771 VARIANT v;
1772 DISPID id;
1773 HRESULT hres;
1774
1775 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1776
1777 TRACE("\n");
1778
1779 obj = stack_pop_objid(ctx, &id);
1780 if(!obj)
1781 return stack_push_string(ctx, undefinedW);
1782
1783 V_VT(&v) = VT_EMPTY;
1784 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
1785 IDispatch_Release(obj);
1786 if(FAILED(hres))
1787 return stack_push_string(ctx, unknownW);
1788
1789 hres = typeof_string(&v, &ret);
1790 VariantClear(&v);
1791 if(FAILED(hres))
1792 return hres;
1793
1794 return stack_push_string(ctx, ret);
1795 }
1796
1797 /* ECMA-262 3rd Edition 11.4.3 */
1798 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1799 {
1800 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1801 exprval_t exprval;
1802 const WCHAR *ret;
1803 VARIANT v;
1804 HRESULT hres;
1805
1806 TRACE("%s\n", debugstr_w(arg));
1807
1808 hres = identifier_eval(ctx->parser->script, arg, &exprval);
1809 if(FAILED(hres))
1810 return hres;
1811
1812 if(exprval.type == EXPRVAL_INVALID) {
1813 hres = stack_push_string(ctx, undefinedW);
1814 exprval_release(&exprval);
1815 return hres;
1816 }
1817
1818 hres = exprval_to_value(ctx->parser->script, &exprval, ctx->ei, &v);
1819 exprval_release(&exprval);
1820 if(FAILED(hres))
1821 return hres;
1822
1823 hres = typeof_string(&v, &ret);
1824 VariantClear(&v);
1825 if(FAILED(hres))
1826 return hres;
1827
1828 return stack_push_string(ctx, ret);
1829 }
1830
1831 /* ECMA-262 3rd Edition 11.4.3 */
1832 static HRESULT interp_typeof(exec_ctx_t *ctx)
1833 {
1834 const WCHAR *ret;
1835 VARIANT *v;
1836 HRESULT hres;
1837
1838 TRACE("\n");
1839
1840 v = stack_pop(ctx);
1841 hres = typeof_string(v, &ret);
1842 VariantClear(v);
1843 if(FAILED(hres))
1844 return hres;
1845
1846 return stack_push_string(ctx, ret);
1847 }
1848
1849 /* ECMA-262 3rd Edition 11.4.7 */
1850 static HRESULT interp_minus(exec_ctx_t *ctx)
1851 {
1852 VARIANT n;
1853 HRESULT hres;
1854
1855 TRACE("\n");
1856
1857 hres = stack_pop_number(ctx, &n);
1858 if(FAILED(hres))
1859 return hres;
1860
1861 return stack_push_number(ctx, -num_val(&n));
1862 }
1863
1864 /* ECMA-262 3rd Edition 11.4.6 */
1865 static HRESULT interp_tonum(exec_ctx_t *ctx)
1866 {
1867 VARIANT *v, num;
1868 HRESULT hres;
1869
1870 TRACE("\n");
1871
1872 v = stack_pop(ctx);
1873 hres = to_number(ctx->parser->script, v, ctx->ei, &num);
1874 VariantClear(v);
1875 if(FAILED(hres))
1876 return hres;
1877
1878 return stack_push(ctx, &num);
1879 }
1880
1881 /* ECMA-262 3rd Edition 11.3.1 */
1882 static HRESULT interp_postinc(exec_ctx_t *ctx)
1883 {
1884 const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1885 IDispatch *obj;
1886 DISPID id;
1887 VARIANT v;
1888 HRESULT hres;
1889
1890 TRACE("%d\n", arg);
1891
1892 obj = stack_pop_objid(ctx, &id);
1893 if(!obj)
1894 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1895
1896 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
1897 if(SUCCEEDED(hres)) {
1898 VARIANT n, inc;
1899
1900 hres = to_number(ctx->parser->script, &v, ctx->ei, &n);
1901 if(SUCCEEDED(hres)) {
1902 num_set_val(&inc, num_val(&n)+(double)arg);
1903 hres = disp_propput(ctx->parser->script, obj, id, &inc, ctx->ei, NULL/*FIXME*/);
1904 }
1905 if(FAILED(hres))
1906 VariantClear(&v);
1907 }
1908 IDispatch_Release(obj);
1909 if(FAILED(hres))
1910 return hres;
1911
1912 return stack_push(ctx, &v);
1913 }
1914
1915 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1916 static HRESULT interp_preinc(exec_ctx_t *ctx)
1917 {
1918 const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1919 IDispatch *obj;
1920 DISPID id;
1921 VARIANT v;
1922 HRESULT hres;
1923
1924 TRACE("%d\n", arg);
1925
1926 obj = stack_pop_objid(ctx, &id);
1927 if(!obj)
1928 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1929
1930 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
1931 if(SUCCEEDED(hres)) {
1932 VARIANT n;
1933
1934 hres = to_number(ctx->parser->script, &v, ctx->ei, &n);
1935 VariantClear(&v);
1936 if(SUCCEEDED(hres)) {
1937 num_set_val(&v, num_val(&n)+(double)arg);
1938 hres = disp_propput(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
1939 }
1940 }
1941 IDispatch_Release(obj);
1942 if(FAILED(hres))
1943 return hres;
1944
1945 return stack_push(ctx, &v);
1946 }
1947
1948 /* ECMA-262 3rd Edition 11.9.3 */
1949 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
1950 {
1951 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
1952 return equal2_values(lval, rval, ret);
1953
1954 /* FIXME: NULL disps should be handled in more general way */
1955 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
1956 VARIANT v;
1957 V_VT(&v) = VT_NULL;
1958 return equal_values(ctx, &v, rval, ei, ret);
1959 }
1960
1961 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
1962 VARIANT v;
1963 V_VT(&v) = VT_NULL;
1964 return equal_values(ctx, lval, &v, ei, ret);
1965 }
1966
1967 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
1968 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
1969 *ret = TRUE;
1970 return S_OK;
1971 }
1972
1973 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
1974 VARIANT v;
1975 HRESULT hres;
1976
1977 hres = to_number(ctx, lval, ei, &v);
1978 if(FAILED(hres))
1979 return hres;
1980
1981 return equal_values(ctx, &v, rval, ei, ret);
1982 }
1983
1984 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
1985 VARIANT v;
1986 HRESULT hres;
1987
1988 hres = to_number(ctx, rval, ei, &v);
1989 if(FAILED(hres))
1990 return hres;
1991
1992 return equal_values(ctx, lval, &v, ei, ret);
1993 }
1994
1995 if(V_VT(rval) == VT_BOOL) {
1996 VARIANT v;
1997
1998 V_VT(&v) = VT_I4;
1999 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2000 return equal_values(ctx, lval, &v, ei, ret);
2001 }
2002
2003 if(V_VT(lval) == VT_BOOL) {
2004 VARIANT v;
2005
2006 V_VT(&v) = VT_I4;
2007 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2008 return equal_values(ctx, &v, rval, ei, ret);
2009 }
2010
2011
2012 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2013 VARIANT v;
2014 HRESULT hres;
2015
2016 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2017 if(FAILED(hres))
2018 return hres;
2019
2020 hres = equal_values(ctx, lval, &v, ei, ret);
2021
2022 VariantClear(&v);
2023 return hres;
2024 }
2025
2026
2027 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2028 VARIANT v;
2029 HRESULT hres;
2030
2031 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2032 if(FAILED(hres))
2033 return hres;
2034
2035 hres = equal_values(ctx, &v, rval, ei, ret);
2036
2037 VariantClear(&v);
2038 return hres;
2039 }
2040
2041
2042 *ret = FALSE;
2043 return S_OK;
2044 }
2045
2046 /* ECMA-262 3rd Edition 11.9.1 */
2047 static HRESULT interp_eq(exec_ctx_t *ctx)
2048 {
2049 VARIANT *l, *r;
2050 BOOL b;
2051 HRESULT hres;
2052
2053 r = stack_pop(ctx);
2054 l = stack_pop(ctx);
2055
2056 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2057
2058 hres = equal_values(ctx->parser->script, l, r, ctx->ei, &b);
2059 VariantClear(l);
2060 VariantClear(r);
2061 if(FAILED(hres))
2062 return hres;
2063
2064 return stack_push_bool(ctx, b);
2065 }
2066
2067 /* ECMA-262 3rd Edition 11.9.2 */
2068 static HRESULT interp_neq(exec_ctx_t *ctx)
2069 {
2070 VARIANT *l, *r;
2071 BOOL b;
2072 HRESULT hres;
2073
2074 r = stack_pop(ctx);
2075 l = stack_pop(ctx);
2076
2077 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2078
2079 hres = equal_values(ctx->parser->script, l, r, ctx->ei, &b);
2080 VariantClear(l);
2081 VariantClear(r);
2082 if(FAILED(hres))
2083 return hres;
2084
2085 return stack_push_bool(ctx, !b);
2086 }
2087
2088 /* ECMA-262 3rd Edition 11.9.4 */
2089 static HRESULT interp_eq2(exec_ctx_t *ctx)
2090 {
2091 VARIANT *l, *r;
2092 BOOL b;
2093 HRESULT hres;
2094
2095 TRACE("\n");
2096
2097 r = stack_pop(ctx);
2098 l = stack_pop(ctx);
2099
2100 hres = equal2_values(r, l, &b);
2101 VariantClear(l);
2102 VariantClear(r);
2103 if(FAILED(hres))
2104 return hres;
2105
2106 return stack_push_bool(ctx, b);
2107 }
2108
2109 /* ECMA-262 3rd Edition 11.9.5 */
2110 static HRESULT interp_neq2(exec_ctx_t *ctx)
2111 {
2112 VARIANT *l, *r;
2113 BOOL b;
2114 HRESULT hres;
2115
2116 TRACE("\n");
2117
2118 r = stack_pop(ctx);
2119 l = stack_pop(ctx);
2120
2121 hres = equal2_values(r, l, &b);
2122 VariantClear(l);
2123 VariantClear(r);
2124 if(FAILED(hres))
2125 return hres;
2126
2127 return stack_push_bool(ctx, !b);
2128 }
2129
2130 /* ECMA-262 3rd Edition 11.8.5 */
2131 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2132 {
2133 VARIANT l, r, ln, rn;
2134 HRESULT hres;
2135
2136 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2137 if(FAILED(hres))
2138 return hres;
2139
2140 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2141 if(FAILED(hres)) {
2142 VariantClear(&l);
2143 return hres;
2144 }
2145
2146 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2147 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2148 SysFreeString(V_BSTR(&l));
2149 SysFreeString(V_BSTR(&r));
2150 return S_OK;
2151 }
2152
2153 hres = to_number(ctx, &l, ei, &ln);
2154 VariantClear(&l);
2155 if(SUCCEEDED(hres))
2156 hres = to_number(ctx, &r, ei, &rn);
2157 VariantClear(&r);
2158 if(FAILED(hres))
2159 return hres;
2160
2161 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
2162 *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
2163 }else {
2164 DOUBLE ld = num_val(&ln);
2165 DOUBLE rd = num_val(&rn);
2166
2167 *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
2168 }
2169
2170 return S_OK;
2171 }
2172
2173 /* ECMA-262 3rd Edition 11.8.1 */
2174 static HRESULT interp_lt(exec_ctx_t *ctx)
2175 {
2176 VARIANT *l, *r;
2177 BOOL b;
2178 HRESULT hres;
2179
2180 r = stack_pop(ctx);
2181 l = stack_pop(ctx);
2182
2183 TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2184
2185 hres = less_eval(ctx->parser->script, l, r, FALSE, ctx->ei, &b);
2186 VariantClear(l);
2187 VariantClear(r);
2188 if(FAILED(hres))
2189 return hres;
2190
2191 return stack_push_bool(ctx, b);
2192 }
2193
2194 /* ECMA-262 3rd Edition 11.8.1 */
2195 static HRESULT interp_lteq(exec_ctx_t *ctx)
2196 {
2197 VARIANT *l, *r;
2198 BOOL b;
2199 HRESULT hres;
2200
2201 r = stack_pop(ctx);
2202 l = stack_pop(ctx);
2203
2204 TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2205
2206 hres = less_eval(ctx->parser->script, r, l, TRUE, ctx->ei, &b);
2207 VariantClear(l);
2208 VariantClear(r);
2209 if(FAILED(hres))
2210 return hres;
2211
2212 return stack_push_bool(ctx, b);
2213 }
2214
2215 /* ECMA-262 3rd Edition 11.8.2 */
2216 static HRESULT interp_gt(exec_ctx_t *ctx)
2217 {
2218 VARIANT *l, *r;
2219 BOOL b;
2220 HRESULT hres;
2221
2222 r = stack_pop(ctx);
2223 l = stack_pop(ctx);
2224
2225 TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2226
2227 hres = less_eval(ctx->parser->script, r, l, FALSE, ctx->ei, &b);
2228 VariantClear(l);
2229 VariantClear(r);
2230 if(FAILED(hres))
2231 return hres;
2232
2233 return stack_push_bool(ctx, b);
2234 }
2235
2236 /* ECMA-262 3rd Edition 11.8.4 */
2237 static HRESULT interp_gteq(exec_ctx_t *ctx)
2238 {
2239 VARIANT *l, *r;
2240 BOOL b;
2241 HRESULT hres;
2242
2243 r = stack_pop(ctx);
2244 l = stack_pop(ctx);
2245
2246 TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
2247
2248 hres = less_eval(ctx->parser->script, l, r, TRUE, ctx->ei, &b);
2249 VariantClear(l);
2250 VariantClear(r);
2251 if(FAILED(hres))
2252 return hres;
2253
2254 return stack_push_bool(ctx, b);
2255 }
2256
2257 /* ECMA-262 3rd Edition 11.4.8 */
2258 static HRESULT interp_bneg(exec_ctx_t *ctx)
2259 {
2260 VARIANT *v, r;
2261 INT i;
2262 HRESULT hres;
2263
2264 TRACE("\n");
2265
2266 v = stack_pop(ctx);
2267 hres = to_int32(ctx->parser->script, v, ctx->ei, &i);
2268 VariantClear(v);
2269 if(FAILED(hres))
2270 return hres;
2271
2272 V_VT(&r) = VT_I4;
2273 V_I4(&r) = ~i;
2274 return stack_push(ctx, &r);
2275 }
2276
2277 /* ECMA-262 3rd Edition 11.4.9 */
2278 static HRESULT interp_neg(exec_ctx_t *ctx)
2279 {
2280 VARIANT *v;
2281 VARIANT_BOOL b;
2282 HRESULT hres;
2283
2284 TRACE("\n");
2285
2286 v = stack_pop(ctx);
2287 hres = to_boolean(v, &b);
2288 VariantClear(v);
2289 if(FAILED(hres))
2290 return hres;
2291
2292 return stack_push_bool(ctx, !b);
2293 }
2294
2295 /* ECMA-262 3rd Edition 11.7.1 */
2296 static HRESULT interp_lshift(exec_ctx_t *ctx)
2297 {
2298 DWORD r;
2299 INT l;
2300 HRESULT hres;
2301
2302 hres = stack_pop_uint(ctx, &r);
2303 if(FAILED(hres))
2304 return hres;
2305
2306 hres = stack_pop_int(ctx, &l);
2307 if(FAILED(hres))
2308 return hres;
2309
2310 return stack_push_int(ctx, l << (r&0x1f));
2311 }
2312
2313 /* ECMA-262 3rd Edition 11.7.2 */
2314 static HRESULT interp_rshift(exec_ctx_t *ctx)
2315 {
2316 DWORD r;
2317 INT l;
2318 HRESULT hres;
2319
2320 hres = stack_pop_uint(ctx, &r);
2321 if(FAILED(hres))
2322 return hres;
2323
2324 hres = stack_pop_int(ctx, &l);
2325 if(FAILED(hres))
2326 return hres;
2327
2328 return stack_push_int(ctx, l >> (r&0x1f));
2329 }
2330
2331 /* ECMA-262 3rd Edition 11.7.3 */
2332 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2333 {
2334 DWORD r, l;
2335 HRESULT hres;
2336
2337 hres = stack_pop_uint(ctx, &r);
2338 if(FAILED(hres))
2339 return hres;
2340
2341 hres = stack_pop_uint(ctx, &l);
2342 if(FAILED(hres))
2343 return hres;
2344
2345 return stack_push_int(ctx, l >> (r&0x1f));
2346 }
2347
2348 /* ECMA-262 3rd Edition 11.13.1 */
2349 static HRESULT interp_assign(exec_ctx_t *ctx)
2350 {
2351 IDispatch *disp;
2352 DISPID id;
2353 VARIANT *v;
2354 HRESULT hres;
2355
2356 TRACE("\n");
2357
2358 v = stack_pop(ctx);
2359 disp = stack_pop_objid(ctx, &id);
2360
2361 if(!disp)
2362 return throw_reference_error(ctx->parser->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2363
2364 hres = disp_propput(ctx->parser->script, disp, id, v, ctx->ei, NULL/*FIXME*/);
2365 IDispatch_Release(disp);
2366 if(FAILED(hres)) {
2367 VariantClear(v);
2368 return hres;
2369 }
2370
2371 return stack_push(ctx, v);
2372 }
2373
2374 static HRESULT interp_undefined(exec_ctx_t *ctx)
2375 {
2376 VARIANT v;
2377
2378 TRACE("\n");
2379
2380 V_VT(&v) = VT_EMPTY;
2381 return stack_push(ctx, &v);
2382 }
2383
2384 static HRESULT interp_jmp(exec_ctx_t *ctx)
2385 {
2386 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2387
2388 TRACE("\n");
2389
2390 ctx->ip = arg;
2391 return S_OK;
2392 }
2393
2394 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2395 {
2396 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2397 VARIANT_BOOL b;
2398 VARIANT *v;
2399 HRESULT hres;
2400
2401 TRACE("\n");
2402
2403 v = stack_pop(ctx);
2404 hres = to_boolean(v, &b);
2405 VariantClear(v);
2406 if(FAILED(hres))
2407 return hres;
2408
2409 if(b)
2410 ctx->ip++;
2411 else
2412 ctx->ip = arg;
2413 return S_OK;
2414 }
2415
2416 static HRESULT interp_pop(exec_ctx_t *ctx)
2417 {
2418 TRACE("\n");
2419
2420 stack_popn(ctx, 1);
2421 return S_OK;
2422 }
2423
2424 static HRESULT interp_ret(exec_ctx_t *ctx)
2425 {
2426 TRACE("\n");
2427
2428 ctx->ip = -1;
2429 return S_OK;
2430 }
2431
2432 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2433
2434 static const op_func_t op_funcs[] = {
2435 #define X(x,a,b,c) interp_##x,
2436 OP_LIST
2437 #undef X
2438 };
2439
2440 static const unsigned op_move[] = {
2441 #define X(a,x,b,c) x,
2442 OP_LIST
2443 #undef X
2444 };
2445
2446 static HRESULT unwind_exception(exec_ctx_t *ctx)
2447 {
2448 except_frame_t *except_frame;
2449 VARIANT except_val;
2450 BSTR ident;
2451 HRESULT hres;
2452
2453 except_frame = ctx->except_frame;
2454 ctx->except_frame = except_frame->next;
2455
2456 assert(except_frame->stack_top <= ctx->top);
2457 stack_popn(ctx, ctx->top - except_frame->stack_top);
2458
2459 while(except_frame->scope != ctx->scope_chain)
2460 scope_pop(&ctx->scope_chain);
2461
2462 ctx->ip = except_frame->catch_off;
2463
2464 except_val = ctx->ei->var;
2465 memset(ctx->ei, 0, sizeof(*ctx->ei));
2466
2467 ident = except_frame->ident;
2468 heap_free(except_frame);
2469
2470 if(ident) {
2471 jsdisp_t *scope_obj;
2472
2473 hres = create_dispex(ctx->parser->script, NULL, NULL, &scope_obj);
2474 if(SUCCEEDED(hres)) {
2475 hres = jsdisp_propput_name(scope_obj, ident, &except_val, ctx->ei, NULL/*FIXME*/);
2476 if(FAILED(hres))
2477 jsdisp_release(scope_obj);
2478 }
2479 VariantClear(&except_val);
2480 if(FAILED(hres))
2481 return hres;
2482
2483 hres = scope_push(ctx->scope_chain, scope_obj, &ctx->scope_chain);
2484 jsdisp_release(scope_obj);
2485 }else {
2486 VARIANT v;
2487
2488 hres = stack_push(ctx, &except_val);
2489 if(FAILED(hres))
2490 return hres;
2491
2492 hres = stack_push_bool(ctx, FALSE);
2493 if(FAILED(hres))
2494 return hres;
2495
2496 V_VT(&v) = VT_EMPTY;
2497 hres = stack_push(ctx, &v);
2498 }
2499
2500 return hres;
2501 }
2502
2503 static HRESULT enter_bytecode(script_ctx_t *ctx, unsigned ip, jsexcept_t *ei, VARIANT *ret)
2504 {
2505 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2506 except_frame_t *prev_except_frame;
2507 unsigned prev_ip, prev_top;
2508 scope_chain_t *prev_scope;
2509 jsexcept_t *prev_ei;
2510 jsop_t op;
2511 HRESULT hres = S_OK;
2512
2513 TRACE("\n");
2514
2515 prev_top = exec_ctx->top;
2516 prev_scope = exec_ctx->scope_chain;
2517 prev_except_frame = exec_ctx->except_frame;
2518 prev_ip = exec_ctx->ip;
2519 prev_ei = exec_ctx->ei;
2520 exec_ctx->ip = ip;
2521 exec_ctx->ei = ei;
2522 exec_ctx->except_frame = NULL;
2523
2524 while(exec_ctx->ip != -1) {
2525 op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
2526 hres = op_funcs[op](exec_ctx);
2527 if(FAILED(hres)) {
2528 TRACE("EXCEPTION\n");
2529
2530 if(!exec_ctx->except_frame)
2531 break;
2532
2533 hres = unwind_exception(exec_ctx);
2534 if(FAILED(hres))
2535 break;
2536 }else {
2537 exec_ctx->ip += op_move[op];
2538 }
2539 }
2540
2541 exec_ctx->ip = prev_ip;
2542 exec_ctx->ei = prev_ei;
2543 exec_ctx->except_frame = prev_except_frame;
2544
2545 if(FAILED(hres)) {
2546 while(exec_ctx->scope_chain != prev_scope)
2547 scope_pop(&exec_ctx->scope_chain);
2548 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2549 return hres;
2550 }
2551
2552 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2553 assert(exec_ctx->scope_chain == prev_scope);
2554
2555 if(exec_ctx->top == prev_top)
2556 V_VT(ret) = VT_EMPTY;
2557 else
2558 *ret = *stack_pop(exec_ctx);
2559 return S_OK;
2560 }
2561
2562 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, BOOL from_eval,
2563 jsexcept_t *ei, VARIANT *retv)
2564 {
2565 script_ctx_t *script = parser->script;
2566 function_declaration_t *func;
2567 parser_ctx_t *prev_parser;
2568 var_list_t *var;
2569 VARIANT val;
2570 exec_ctx_t *prev_ctx;
2571 HRESULT hres = S_OK;
2572
2573 for(func = source->functions; func; func = func->next) {
2574 jsdisp_t *func_obj;
2575 VARIANT var;
2576
2577 hres = create_source_function(parser, func->expr->parameter_list, func->expr->source_elements,
2578 ctx->scope_chain, func->expr->src_str, func->expr->src_len, &func_obj);
2579 if(FAILED(hres))
2580 return hres;
2581
2582 var_set_jsdisp(&var, func_obj);
2583 hres = jsdisp_propput_name(ctx->var_disp, func->expr->identifier, &var, ei, NULL);
2584 jsdisp_release(func_obj);
2585 if(FAILED(hres))
2586 return hres;
2587 }
2588
2589 for(var = source->variables; var; var = var->next) {
2590 DISPID id = 0;
2591 BSTR name;
2592
2593 name = SysAllocString(var->identifier);
2594 if(!name)
2595 return E_OUTOFMEMORY;
2596
2597 if(!ctx->is_global || !lookup_global_members(parser->script, name, NULL))
2598 hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
2599 SysFreeString(name);
2600 if(FAILED(hres))
2601 return hres;
2602 }
2603
2604 prev_ctx = script->exec_ctx;
2605 script->exec_ctx = ctx;
2606
2607 prev_parser = ctx->parser;
2608 ctx->parser = parser;
2609
2610 if(source->statement) {
2611 if(!source->instr_off) {
2612 hres = compile_subscript_stat(ctx->parser, source->statement, from_eval, &source->instr_off);
2613 if(FAILED(hres) && is_jscript_error(hres))
2614 hres = throw_syntax_error(script, ei, hres, NULL);
2615 }
2616 if(SUCCEEDED(hres))
2617 hres = enter_bytecode(script, source->instr_off, ei, &val);
2618 }else {
2619 V_VT(&val) = VT_EMPTY;
2620 }
2621
2622 script->exec_ctx = prev_ctx;
2623 ctx->parser = prev_parser;
2624
2625 if(FAILED(hres)) {
2626 VariantClear(&val);
2627 return hres;
2628 }
2629
2630 if(retv)
2631 *retv = val;
2632 else
2633 VariantClear(&val);
2634 return S_OK;
2635 }
2636
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.