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