jscript: Added Function.call implementation.
[wine] / dlls / jscript / function.c
1 /*
2  * Copyright 2008 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 #include "engine.h"
21
22 #include "wine/debug.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
25
26 typedef struct {
27     DispatchEx dispex;
28     builtin_invoke_t value_proc;
29     DWORD flags;
30     source_elements_t *source;
31     parameter_t *parameters;
32     scope_chain_t *scope_chain;
33     parser_ctx_t *parser;
34     const WCHAR *src_str;
35     DWORD src_len;
36     DWORD length;
37 } FunctionInstance;
38
39 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
40
41 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
42 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
43 static const WCHAR applyW[] = {'a','p','p','l','y',0};
44 static const WCHAR callW[] = {'c','a','l','l',0};
45
46 static IDispatch *get_this(DISPPARAMS *dp)
47 {
48     DWORD i;
49
50     for(i=0; i < dp->cNamedArgs; i++) {
51         if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
52             if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
53                 return V_DISPATCH(dp->rgvarg+i);
54
55             WARN("This is not VT_DISPATCH\n");
56             return NULL;
57         }
58     }
59
60     TRACE("no this passed\n");
61     return NULL;
62 }
63
64 static HRESULT init_parameters(DispatchEx *var_disp, FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
65         jsexcept_t *ei, IServiceProvider *caller)
66 {
67     parameter_t *param;
68     VARIANT var_empty;
69     DWORD cargs, i=0;
70     HRESULT hres;
71
72     V_VT(&var_empty) = VT_EMPTY;
73     cargs = dp->cArgs - dp->cNamedArgs;
74
75     for(param = function->parameters; param; param = param->next) {
76         hres = jsdisp_propput_name(var_disp, param->identifier, lcid,
77                 i < cargs ? dp->rgvarg + dp->cArgs-1 - i : &var_empty,
78                 ei, caller);
79         if(FAILED(hres))
80             return hres;
81
82         i++;
83     }
84
85     return S_OK;
86 }
87
88 HRESULT Arguments_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
89         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
90 {
91     FIXME("\n");
92     return E_NOTIMPL;
93 }
94
95 static const builtin_info_t Arguments_info = {
96     JSCLASS_ARGUMENTS,
97     {NULL, Arguments_value, 0},
98     0, NULL,
99     NULL,
100     NULL
101 };
102
103 static HRESULT create_arguments(script_ctx_t *ctx, LCID lcid, DISPPARAMS *dp,
104         jsexcept_t *ei, IServiceProvider *caller, DispatchEx **ret)
105 {
106     DispatchEx *args;
107     VARIANT var;
108     DWORD i;
109     HRESULT hres;
110
111     args = heap_alloc_zero(sizeof(DispatchEx));
112     if(!args)
113         return E_OUTOFMEMORY;
114
115     hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
116     if(FAILED(hres)) {
117         heap_free(args);
118         return hres;
119     }
120
121     for(i=0; i < arg_cnt(dp); i++) {
122         hres = jsdisp_propput_idx(args, i, lcid, get_arg(dp,i), ei, caller);
123         if(FAILED(hres))
124             break;
125     }
126
127     if(SUCCEEDED(hres)) {
128         V_VT(&var) = VT_I4;
129         V_I4(&var) = arg_cnt(dp);
130         hres = jsdisp_propput_name(args, lengthW, lcid, &var, ei, caller);
131     }
132
133     if(FAILED(hres)) {
134         jsdisp_release(args);
135         return hres;
136     }
137
138     *ret = args;
139     return S_OK;
140 }
141
142 static HRESULT create_var_disp(FunctionInstance *function, LCID lcid, DISPPARAMS *dp, jsexcept_t *ei,
143                                IServiceProvider *caller, DispatchEx **ret)
144 {
145     DispatchEx *var_disp, *arg_disp;
146     HRESULT hres;
147
148     static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
149
150     hres = create_dispex(function->dispex.ctx, NULL, NULL, &var_disp);
151     if(FAILED(hres))
152         return hres;
153
154     hres = create_arguments(function->dispex.ctx, lcid, dp, ei, caller, &arg_disp);
155     if(SUCCEEDED(hres)) {
156         VARIANT var;
157
158         V_VT(&var) = VT_DISPATCH;
159         V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(arg_disp);
160         hres = jsdisp_propput_name(var_disp, argumentsW, lcid, &var, ei, caller);
161         jsdisp_release(arg_disp);
162     }
163
164     if(SUCCEEDED(hres))
165         hres = init_parameters(var_disp, function, lcid, dp, ei, caller);
166     if(FAILED(hres)) {
167         jsdisp_release(var_disp);
168         return hres;
169     }
170
171     *ret = var_disp;
172     return S_OK;
173 }
174
175 static HRESULT invoke_source(FunctionInstance *function, IDispatch *this_obj, LCID lcid, DISPPARAMS *dp,
176         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
177 {
178     DispatchEx *var_disp;
179     exec_ctx_t *exec_ctx;
180     scope_chain_t *scope;
181     HRESULT hres;
182
183     if(!function->source) {
184         FIXME("no source\n");
185         return E_FAIL;
186     }
187
188     hres = create_var_disp(function, lcid, dp, ei, caller, &var_disp);
189     if(FAILED(hres))
190         return hres;
191
192     hres = scope_push(function->scope_chain, var_disp, &scope);
193     if(SUCCEEDED(hres)) {
194         hres = create_exec_ctx(this_obj, var_disp, scope, &exec_ctx);
195         scope_release(scope);
196     }
197     if(FAILED(hres))
198         return hres;
199
200     hres = exec_source(exec_ctx, function->parser, function->source, ei, retv);
201     exec_release(exec_ctx);
202
203     return hres;
204 }
205
206 static HRESULT invoke_function(FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
207         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
208 {
209     IDispatch *this_obj;
210
211     if(!(this_obj = get_this(dp)))
212         this_obj = (IDispatch*)_IDispatchEx_(function->dispex.ctx->script_disp);
213
214     return invoke_source(function, this_obj, lcid, dp, retv, ei, caller);
215 }
216
217 static HRESULT invoke_constructor(FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
218         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
219 {
220     DispatchEx *this_obj;
221     HRESULT hres;
222
223     hres = create_object(function->dispex.ctx, &function->dispex, &this_obj);
224     if(FAILED(hres))
225         return hres;
226
227     hres = invoke_source(function, (IDispatch*)_IDispatchEx_(this_obj), lcid, dp, retv, ei, caller);
228     if(FAILED(hres)) {
229         jsdisp_release(this_obj);
230         return hres;
231     }
232
233     V_VT(retv) = VT_DISPATCH;
234     V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(this_obj);
235     return S_OK;
236 }
237
238 static HRESULT invoke_value_proc(FunctionInstance *function, LCID lcid, WORD flags, DISPPARAMS *dp,
239         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
240 {
241     DispatchEx *this_obj = NULL;
242     IDispatch *this_disp;
243     HRESULT hres;
244
245     this_disp = get_this(dp);
246     if(this_disp)
247         this_obj = iface_to_jsdisp((IUnknown*)this_disp);
248
249     hres = function->value_proc(this_obj ? this_obj : function->dispex.ctx->script_disp, lcid,
250                                 flags, dp, retv, ei, caller);
251
252     if(this_obj)
253         jsdisp_release(this_obj);
254     return hres;
255 }
256
257 static HRESULT call_function(FunctionInstance *function, IDispatch *this_obj, LCID lcid, DISPPARAMS *args,
258         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
259 {
260     HRESULT hres;
261
262     if(function->value_proc) {
263         DispatchEx *jsthis = NULL;
264
265         if(this_obj) {
266             jsthis = iface_to_jsdisp((IUnknown*)this_obj);
267             if(!jsthis)
268                 FIXME("this_obj is not DispatchEx\n");
269         }
270
271         hres = function->value_proc(jsthis ? jsthis : function->dispex.ctx->script_disp, lcid,
272                 DISPATCH_METHOD, args, retv, ei, caller);
273
274         if(jsthis)
275             jsdisp_release(jsthis);
276     }else {
277         hres = invoke_source(function,
278                 this_obj ? this_obj : (IDispatch*)_IDispatchEx_(function->dispex.ctx->script_disp),
279                 lcid, args, retv, ei, caller);
280     }
281
282     return hres;
283 }
284
285 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
286 {
287     BSTR str;
288
289     if(function->value_proc) {
290         FIXME("Builtin functions not implemented\n");
291         return E_NOTIMPL;
292     }
293
294     str = SysAllocStringLen(function->src_str, function->src_len);
295     if(!str)
296         return E_OUTOFMEMORY;
297
298     *ret = str;
299     return S_OK;
300 }
301
302 static HRESULT Function_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
303         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
304 {
305     FunctionInstance *This = (FunctionInstance*)dispex;
306
307     TRACE("%p %d\n", This, This->length);
308
309     switch(flags) {
310     case DISPATCH_PROPERTYGET:
311         V_VT(retv) = VT_I4;
312         V_I4(retv) = This->length;
313         break;
314     default:
315         FIXME("unimplemented flags %x\n", flags);
316         return E_NOTIMPL;
317     }
318
319     return S_OK;
320 }
321
322 static HRESULT Function_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
323         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
324 {
325     FunctionInstance *function;
326     BSTR str;
327     HRESULT hres;
328
329     TRACE("\n");
330
331     if(!is_class(dispex, JSCLASS_FUNCTION))
332         return throw_type_error(dispex->ctx, ei, IDS_NOT_FUNC, NULL);
333
334     function = (FunctionInstance*)dispex;
335
336     hres = function_to_string(function, &str);
337     if(FAILED(hres))
338         return hres;
339
340     if(retv) {
341         V_VT(retv) = VT_BSTR;
342         V_BSTR(retv) = str;
343     }else {
344         SysFreeString(str);
345     }
346     return S_OK;
347 }
348
349 static HRESULT Function_apply(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
350         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
351 {
352     FIXME("\n");
353     return E_NOTIMPL;
354 }
355
356 static HRESULT Function_call(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
357         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
358 {
359     FunctionInstance *function;
360     DISPPARAMS args = {NULL,NULL,0,0};
361     IDispatch *this_obj = NULL;
362     DWORD argc;
363     HRESULT hres;
364
365     TRACE("\n");
366
367     if(!is_class(dispex, JSCLASS_FUNCTION)) {
368         FIXME("dispex is not a function\n");
369         return E_FAIL;
370     }
371
372     function = (FunctionInstance*)dispex;
373     argc = arg_cnt(dp);
374
375     if(argc) {
376         hres = to_object(dispex->ctx, get_arg(dp,0), &this_obj);
377         if(FAILED(hres))
378             return hres;
379         args.cArgs = argc-1;
380     }
381
382     if(args.cArgs)
383         args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1;
384
385     hres = call_function(function, this_obj, lcid, &args, retv, ei, caller);
386
387     if(this_obj)
388         IDispatch_Release(this_obj);
389     return hres;
390 }
391
392 HRESULT Function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
393         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
394 {
395     FunctionInstance *function;
396
397     TRACE("\n");
398
399     if(dispex->builtin_info->class != JSCLASS_FUNCTION) {
400         ERR("dispex is not a function\n");
401         return E_FAIL;
402     }
403
404     function = (FunctionInstance*)dispex;
405
406     switch(flags) {
407     case DISPATCH_METHOD:
408         if(function->value_proc)
409             return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
410
411         return invoke_function(function, lcid, dp, retv, ei, caller);
412
413     case DISPATCH_PROPERTYGET: {
414         HRESULT hres;
415         BSTR str;
416
417         hres = function_to_string(function, &str);
418         if(FAILED(hres))
419             return hres;
420
421         V_VT(retv) = VT_BSTR;
422         V_BSTR(retv) = str;
423         break;
424     }
425
426     case DISPATCH_CONSTRUCT:
427         if(function->value_proc)
428             return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
429
430         return invoke_constructor(function, lcid, dp, retv, ei, caller);
431
432     default:
433         FIXME("not implemented flags %x\n", flags);
434         return E_NOTIMPL;
435     }
436
437     return S_OK;
438 }
439
440 static void Function_destructor(DispatchEx *dispex)
441 {
442     FunctionInstance *This = (FunctionInstance*)dispex;
443
444     if(This->parser)
445         parser_release(This->parser);
446     if(This->scope_chain)
447         scope_release(This->scope_chain);
448     heap_free(This);
449 }
450
451 static const builtin_prop_t Function_props[] = {
452     {applyW,                 Function_apply,                 PROPF_METHOD|2},
453     {callW,                  Function_call,                  PROPF_METHOD|1},
454     {lengthW,                Function_length,                0},
455     {toStringW,              Function_toString,              PROPF_METHOD}
456 };
457
458 static const builtin_info_t Function_info = {
459     JSCLASS_FUNCTION,
460     {NULL, Function_value, 0},
461     sizeof(Function_props)/sizeof(*Function_props),
462     Function_props,
463     Function_destructor,
464     NULL
465 };
466
467 static HRESULT FunctionConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
468         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
469 {
470     FIXME("\n");
471     return E_NOTIMPL;
472 }
473
474 static HRESULT FunctionProt_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
475         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
476 {
477     FIXME("\n");
478     return E_NOTIMPL;
479 }
480
481 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
482         BOOL funcprot, DispatchEx *prototype, FunctionInstance **ret)
483 {
484     FunctionInstance *function;
485     HRESULT hres;
486
487     function = heap_alloc_zero(sizeof(FunctionInstance));
488     if(!function)
489         return E_OUTOFMEMORY;
490
491     if(funcprot)
492         hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
493     else if(builtin_info)
494         hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
495     else
496         hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
497     if(FAILED(hres))
498         return hres;
499
500     function->flags = flags;
501     function->length = flags & PROPF_ARGMASK;
502
503     *ret = function;
504     return S_OK;
505 }
506
507 static HRESULT set_prototype(script_ctx_t *ctx, DispatchEx *dispex, DispatchEx *prototype)
508 {
509     jsexcept_t jsexcept;
510     VARIANT var;
511
512     V_VT(&var) = VT_DISPATCH;
513     V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
514     memset(&jsexcept, 0, sizeof(jsexcept));
515
516     return jsdisp_propput_name(dispex, prototypeW, ctx->lcid, &var, &jsexcept, NULL/*FIXME*/);
517 }
518
519 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc,
520         const builtin_info_t *builtin_info, DWORD flags, DispatchEx *prototype, DispatchEx **ret)
521 {
522     FunctionInstance *function;
523     HRESULT hres;
524
525     hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
526     if(FAILED(hres))
527         return hres;
528
529     hres = set_prototype(ctx, &function->dispex, prototype);
530     if(FAILED(hres)) {
531         jsdisp_release(&function->dispex);
532         return hres;
533     }
534
535     function->value_proc = value_proc;
536
537     *ret = &function->dispex;
538     return S_OK;
539 }
540
541 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
542         scope_chain_t *scope_chain, const WCHAR *src_str, DWORD src_len, DispatchEx **ret)
543 {
544     FunctionInstance *function;
545     DispatchEx *prototype;
546     parameter_t *iter;
547     DWORD length = 0;
548     HRESULT hres;
549
550     hres = create_object(ctx->script, NULL, &prototype);
551     if(FAILED(hres))
552         return hres;
553
554     hres = create_function(ctx->script, NULL, PROPF_CONSTR, FALSE, NULL, &function);
555     if(SUCCEEDED(hres)) {
556         hres = set_prototype(ctx->script, &function->dispex, prototype);
557         if(FAILED(hres))
558             jsdisp_release(&function->dispex);
559     }
560     jsdisp_release(prototype);
561     if(FAILED(hres))
562         return hres;
563
564     function->source = source;
565     function->parameters = parameters;
566
567     if(scope_chain) {
568         scope_addref(scope_chain);
569         function->scope_chain = scope_chain;
570     }
571
572     parser_addref(ctx);
573     function->parser = ctx;
574
575     for(iter = parameters; iter; iter = iter->next)
576         length++;
577     function->length = length;
578
579     function->src_str = src_str;
580     function->src_len = src_len;
581
582     *ret = &function->dispex;
583     return S_OK;
584 }
585
586 HRESULT init_function_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
587 {
588     FunctionInstance *prot, *constr;
589     HRESULT hres;
590
591     hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
592     if(FAILED(hres))
593         return hres;
594
595     prot->value_proc = FunctionProt_value;
596
597     hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, &prot->dispex, &constr);
598     if(SUCCEEDED(hres)) {
599         constr->value_proc = FunctionConstr_value;
600         hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
601         if(FAILED(hres))
602             jsdisp_release(&constr->dispex);
603     }
604     jsdisp_release(&prot->dispex);
605     if(FAILED(hres))
606         return hres;
607
608     ctx->function_constr = &constr->dispex;
609     return S_OK;
610 }