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