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