jscript: Make String_charCodeAt generic.
[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     HRESULT hres;
195
196     hres = create_object(function->dispex.ctx, &function->dispex, &this_obj);
197     if(FAILED(hres))
198         return hres;
199
200     hres = invoke_source(function, (IDispatch*)_IDispatchEx_(this_obj), lcid, dp, retv, ei, caller);
201     jsdisp_release(this_obj);
202     if(FAILED(hres))
203         return hres;
204
205     V_VT(retv) = VT_DISPATCH;
206     V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(this_obj);
207     return S_OK;
208 }
209
210 static HRESULT invoke_value_proc(FunctionInstance *function, LCID lcid, WORD flags, DISPPARAMS *dp,
211         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
212 {
213     DispatchEx *this_obj = NULL;
214     IDispatch *this_disp;
215     HRESULT hres;
216
217     this_disp = get_this(dp);
218     if(this_disp)
219         this_obj = iface_to_jsdisp((IUnknown*)this_disp);
220
221     hres = function->value_proc(this_obj ? this_obj : function->dispex.ctx->script_disp, lcid,
222                                 flags, dp, retv, ei, caller);
223
224     if(this_obj)
225         jsdisp_release(this_obj);
226     return hres;
227 }
228
229 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
230 {
231     BSTR str;
232
233     if(function->value_proc) {
234         FIXME("Builtin functions not implemented\n");
235         return E_NOTIMPL;
236     }
237
238     str = SysAllocStringLen(function->src_str, function->src_len);
239     if(!str)
240         return E_OUTOFMEMORY;
241
242     *ret = str;
243     return S_OK;
244 }
245
246 static HRESULT Function_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
247         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
248 {
249     FunctionInstance *This = (FunctionInstance*)dispex;
250
251     TRACE("%p %d\n", This, This->length);
252
253     switch(flags) {
254     case DISPATCH_PROPERTYGET:
255         V_VT(retv) = VT_I4;
256         V_I4(retv) = This->length;
257         break;
258     default:
259         FIXME("unimplemented flags %x\n", flags);
260         return E_NOTIMPL;
261     }
262
263     return S_OK;
264 }
265
266 static HRESULT Function_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
267         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
268 {
269     FunctionInstance *function;
270     BSTR str;
271     HRESULT hres;
272
273     TRACE("\n");
274
275     if(!is_class(dispex, JSCLASS_FUNCTION)) {
276         FIXME("throw TypeError\n");
277         return E_FAIL;
278     }
279
280     function = (FunctionInstance*)dispex;
281
282     hres = function_to_string(function, &str);
283     if(FAILED(hres))
284         return hres;
285
286     if(retv) {
287         V_VT(retv) = VT_BSTR;
288         V_BSTR(retv) = str;
289     }else {
290         SysFreeString(str);
291     }
292     return S_OK;
293 }
294
295 static HRESULT Function_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
296         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
297 {
298     FIXME("\n");
299     return E_NOTIMPL;
300 }
301
302 static HRESULT Function_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
303         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
304 {
305     WARN("should be inherited from Object\n");
306
307     if(retv) {
308         IDispatchEx_AddRef(_IDispatchEx_(dispex));
309
310         V_VT(retv) = VT_DISPATCH;
311         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(dispex);
312     }
313
314     return S_OK;
315 }
316
317 static HRESULT Function_apply(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
318         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
319 {
320     FIXME("\n");
321     return E_NOTIMPL;
322 }
323
324 static HRESULT Function_call(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
325         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
326 {
327     FIXME("\n");
328     return E_NOTIMPL;
329 }
330
331 static HRESULT Function_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
332         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
333 {
334     FIXME("\n");
335     return E_NOTIMPL;
336 }
337
338 static HRESULT Function_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
339         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
340 {
341     FIXME("\n");
342     return E_NOTIMPL;
343 }
344
345 static HRESULT Function_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
346         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
347 {
348     FIXME("\n");
349     return E_NOTIMPL;
350 }
351
352 HRESULT Function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
353         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
354 {
355     FunctionInstance *function;
356
357     TRACE("\n");
358
359     if(dispex->builtin_info->class != JSCLASS_FUNCTION) {
360         ERR("dispex is not a function\n");
361         return E_FAIL;
362     }
363
364     function = (FunctionInstance*)dispex;
365
366     switch(flags) {
367     case DISPATCH_METHOD:
368         if(function->value_proc)
369             return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
370
371         return invoke_function(function, lcid, dp, retv, ei, caller);
372
373     case DISPATCH_PROPERTYGET: {
374         HRESULT hres;
375         BSTR str;
376
377         hres = function_to_string(function, &str);
378         if(FAILED(hres))
379             return hres;
380
381         V_VT(retv) = VT_BSTR;
382         V_BSTR(retv) = str;
383         break;
384     }
385
386     case DISPATCH_CONSTRUCT:
387         if(function->value_proc)
388             return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
389
390         return invoke_constructor(function, lcid, dp, retv, ei, caller);
391
392     default:
393         FIXME("not implemented flags %x\n", flags);
394         return E_NOTIMPL;
395     }
396
397     return S_OK;
398 }
399
400 static void Function_destructor(DispatchEx *dispex)
401 {
402     FunctionInstance *This = (FunctionInstance*)dispex;
403
404     if(This->parser)
405         parser_release(This->parser);
406     if(This->scope_chain)
407         scope_release(This->scope_chain);
408     heap_free(This);
409 }
410
411 static const builtin_prop_t Function_props[] = {
412     {applyW,                 Function_apply,                 PROPF_METHOD},
413     {callW,                  Function_call,                  PROPF_METHOD},
414     {hasOwnPropertyW,        Function_hasOwnProperty,        PROPF_METHOD},
415     {isPrototypeOfW,         Function_isPrototypeOf,         PROPF_METHOD},
416     {lengthW,                Function_length,                0},
417     {propertyIsEnumerableW,  Function_propertyIsEnumerable,  PROPF_METHOD},
418     {toLocaleStringW,        Function_toLocaleString,        PROPF_METHOD},
419     {toStringW,              Function_toString,              PROPF_METHOD},
420     {valueOfW,               Function_valueOf,               PROPF_METHOD}
421 };
422
423 static const builtin_info_t Function_info = {
424     JSCLASS_FUNCTION,
425     {NULL, Function_value, 0},
426     sizeof(Function_props)/sizeof(*Function_props),
427     Function_props,
428     Function_destructor,
429     NULL
430 };
431
432 static HRESULT FunctionConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
433         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
434 {
435     FIXME("\n");
436     return E_NOTIMPL;
437 }
438
439 static HRESULT FunctionProt_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
440         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
441 {
442     FIXME("\n");
443     return E_NOTIMPL;
444 }
445
446 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
447         BOOL funcprot, DispatchEx *prototype, FunctionInstance **ret)
448 {
449     FunctionInstance *function;
450     HRESULT hres;
451
452     function = heap_alloc_zero(sizeof(FunctionInstance));
453     if(!function)
454         return E_OUTOFMEMORY;
455
456     if(funcprot)
457         hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
458     else if(builtin_info)
459         hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
460     else
461         hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
462     if(FAILED(hres))
463         return hres;
464
465     function->flags = flags;
466     function->length = flags & PROPF_ARGMASK;
467
468     if(prototype) {
469         jsexcept_t jsexcept;
470         VARIANT var;
471
472         V_VT(&var) = VT_DISPATCH;
473         V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
474         memset(&jsexcept, 0, sizeof(jsexcept));
475
476         hres = jsdisp_propput_name(&function->dispex, prototypeW, ctx->lcid, &var, &jsexcept, NULL/*FIXME*/);
477         if(FAILED(hres)) {
478             IDispatchEx_Release(_IDispatchEx_(&function->dispex));
479             return hres;
480         }
481     }
482
483     *ret = function;
484     return S_OK;
485 }
486
487 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc,
488         const builtin_info_t *builtin_info, DWORD flags, DispatchEx *prototype, DispatchEx **ret)
489 {
490     FunctionInstance *function;
491     HRESULT hres;
492
493     hres = create_function(ctx, builtin_info, flags, FALSE, prototype, &function);
494     if(FAILED(hres))
495         return hres;
496
497     function->value_proc = value_proc;
498
499     *ret = &function->dispex;
500     return S_OK;
501 }
502
503 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
504         scope_chain_t *scope_chain, const WCHAR *src_str, DWORD src_len, DispatchEx **ret)
505 {
506     FunctionInstance *function;
507     DispatchEx *prototype;
508     parameter_t *iter;
509     DWORD length = 0;
510     HRESULT hres;
511
512     hres = create_object(ctx->script, NULL, &prototype);
513     if(FAILED(hres))
514         return hres;
515
516     hres = create_function(ctx->script, NULL, PROPF_CONSTR, FALSE, prototype, &function);
517     jsdisp_release(prototype);
518     if(FAILED(hres))
519         return hres;
520
521     function->source = source;
522     function->parameters = parameters;
523
524     if(scope_chain) {
525         scope_addref(scope_chain);
526         function->scope_chain = scope_chain;
527     }
528
529     parser_addref(ctx);
530     function->parser = ctx;
531
532     for(iter = parameters; iter; iter = iter->next)
533         length++;
534     function->length = length;
535
536     function->src_str = src_str;
537     function->src_len = src_len;
538
539     *ret = &function->dispex;
540     return S_OK;
541 }
542
543 HRESULT init_function_constr(script_ctx_t *ctx)
544 {
545     FunctionInstance *prot, *constr;
546     HRESULT hres;
547
548     hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, NULL, &prot);
549     if(FAILED(hres))
550         return hres;
551
552     prot->value_proc = FunctionProt_value;
553
554     hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, &prot->dispex, &constr);
555     jsdisp_release(&prot->dispex);
556     if(FAILED(hres))
557         return hres;
558
559     constr->value_proc = FunctionConstr_value;
560     ctx->function_constr = &constr->dispex;
561     return hres;
562 }