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