jscript: Set parameters on function call.
[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     DWORD length;
35 } FunctionInstance;
36
37 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
38
39 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
40 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
41 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
42 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
43 static const WCHAR applyW[] = {'a','p','p','l','y',0};
44 static const WCHAR callW[] = {'c','a','l','l',0};
45 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
46 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};
47 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
48
49 static IDispatch *get_this(DISPPARAMS *dp)
50 {
51     DWORD i;
52
53     for(i=0; i < dp->cNamedArgs; i++) {
54         if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
55             if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
56                 return V_DISPATCH(dp->rgvarg+i);
57
58             WARN("This is not VT_DISPATCH\n");
59             return NULL;
60         }
61     }
62
63     TRACE("no this passed\n");
64     return NULL;
65 }
66
67 static HRESULT init_parameters(DispatchEx *var_disp, FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
68         jsexcept_t *ei, IServiceProvider *caller)
69 {
70     parameter_t *param;
71     VARIANT var_empty;
72     DWORD cargs, i=0;
73     HRESULT hres;
74
75     V_VT(&var_empty) = VT_EMPTY;
76     cargs = dp->cArgs - dp->cNamedArgs;
77
78     for(param = function->parameters; param; param = param->next) {
79         hres = jsdisp_propput_name(var_disp, param->identifier, lcid,
80                 i < cargs ? dp->rgvarg + dp->cArgs-1 - i : &var_empty,
81                 ei, caller);
82         if(FAILED(hres))
83             return hres;
84
85         i++;
86     }
87
88     return S_OK;
89 }
90
91 static HRESULT create_var_disp(FunctionInstance *function, LCID lcid, DISPPARAMS *dp, jsexcept_t *ei,
92                                IServiceProvider *caller, DispatchEx **ret)
93 {
94     DispatchEx *var_disp;
95     HRESULT hres;
96
97     hres = create_dispex(function->dispex.ctx, NULL, NULL, &var_disp);
98     if(FAILED(hres))
99         return hres;
100
101     hres = init_parameters(var_disp, function, lcid, dp, ei, caller);
102     if(FAILED(hres)) {
103         jsdisp_release(var_disp);
104         return hres;
105     }
106
107     *ret = var_disp;
108     return S_OK;
109 }
110
111 static HRESULT invoke_source(FunctionInstance *function, IDispatch *this_obj, LCID lcid, DISPPARAMS *dp,
112         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
113 {
114     DispatchEx *var_disp;
115     exec_ctx_t *exec_ctx;
116     scope_chain_t *scope;
117     HRESULT hres;
118
119     if(!function->source) {
120         FIXME("no source\n");
121         return E_FAIL;
122     }
123
124     hres = create_var_disp(function, lcid, dp, ei, caller, &var_disp);
125     if(FAILED(hres))
126         return hres;
127
128     hres = scope_push(function->scope_chain, var_disp, &scope);
129     if(SUCCEEDED(hres)) {
130         hres = create_exec_ctx(this_obj, var_disp, scope, &exec_ctx);
131         scope_release(scope);
132     }
133     if(FAILED(hres))
134         return hres;
135
136     hres = exec_source(exec_ctx, function->parser, function->source, ei, retv);
137     exec_release(exec_ctx);
138
139     return hres;
140 }
141
142 static HRESULT invoke_function(FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
143         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
144 {
145     IDispatch *this_obj;
146
147     if(!(this_obj = get_this(dp)))
148         this_obj = (IDispatch*)_IDispatchEx_(function->dispex.ctx->script_disp);
149
150     return invoke_source(function, this_obj, lcid, dp, retv, ei, caller);
151 }
152
153 static HRESULT invoke_value_proc(FunctionInstance *function, LCID lcid, WORD flags, DISPPARAMS *dp,
154         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
155 {
156     DispatchEx *this_obj = NULL;
157     IDispatch *this_disp;
158     HRESULT hres;
159
160     this_disp = get_this(dp);
161     if(this_disp)
162         this_obj = iface_to_jsdisp((IUnknown*)this_disp);
163
164     hres = function->value_proc(this_obj ? this_obj : function->dispex.ctx->script_disp, lcid,
165                                 flags, dp, retv, ei, caller);
166
167     if(this_obj)
168         jsdisp_release(this_obj);
169     return hres;
170 }
171
172 static HRESULT Function_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
173         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
174 {
175     FunctionInstance *This = (FunctionInstance*)dispex;
176
177     TRACE("%p %d\n", This, This->length);
178
179     switch(flags) {
180     case DISPATCH_PROPERTYGET:
181         V_VT(retv) = VT_I4;
182         V_I4(retv) = This->length;
183         break;
184     default:
185         FIXME("unimplemented flags %x\n", flags);
186         return E_NOTIMPL;
187     }
188
189     return S_OK;
190 }
191
192 static HRESULT Function_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
193         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
194 {
195     FIXME("\n");
196     return E_NOTIMPL;
197 }
198
199 static HRESULT Function_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
200         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
201 {
202     FIXME("\n");
203     return E_NOTIMPL;
204 }
205
206 static HRESULT Function_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
207         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
208 {
209     FIXME("\n");
210     return E_NOTIMPL;
211 }
212
213 static HRESULT Function_apply(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
214         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
215 {
216     FIXME("\n");
217     return E_NOTIMPL;
218 }
219
220 static HRESULT Function_call(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
221         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
222 {
223     FIXME("\n");
224     return E_NOTIMPL;
225 }
226
227 static HRESULT Function_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
228         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
229 {
230     FIXME("\n");
231     return E_NOTIMPL;
232 }
233
234 static HRESULT Function_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
235         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
236 {
237     FIXME("\n");
238     return E_NOTIMPL;
239 }
240
241 static HRESULT Function_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
242         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
243 {
244     FIXME("\n");
245     return E_NOTIMPL;
246 }
247
248 static HRESULT Function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
249         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
250 {
251     FunctionInstance *function;
252
253     TRACE("\n");
254
255     if(dispex->builtin_info->class != JSCLASS_FUNCTION) {
256         ERR("dispex is not a function\n");
257         return E_FAIL;
258     }
259
260     function = (FunctionInstance*)dispex;
261
262     switch(flags) {
263     case DISPATCH_METHOD:
264         if(function->value_proc)
265             return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
266
267         return invoke_function(function, lcid, dp, retv, ei, caller);
268
269     default:
270         FIXME("not implemented flags %x\n", flags);
271         return E_NOTIMPL;
272     }
273
274     return S_OK;
275 }
276
277 static void Function_destructor(DispatchEx *dispex)
278 {
279     FunctionInstance *This = (FunctionInstance*)dispex;
280
281     if(This->parser)
282         parser_release(This->parser);
283     if(This->scope_chain)
284         scope_release(This->scope_chain);
285     heap_free(This);
286 }
287
288 static const builtin_prop_t Function_props[] = {
289     {applyW,                 Function_apply,                 PROPF_METHOD},
290     {callW,                  Function_call,                  PROPF_METHOD},
291     {hasOwnPropertyW,        Function_hasOwnProperty,        PROPF_METHOD},
292     {isPrototypeOfW,         Function_isPrototypeOf,         PROPF_METHOD},
293     {lengthW,                Function_length,                0},
294     {propertyIsEnumerableW,  Function_propertyIsEnumerable,  PROPF_METHOD},
295     {toLocaleStringW,        Function_toLocaleString,        PROPF_METHOD},
296     {toStringW,              Function_toString,              PROPF_METHOD},
297     {valueOfW,               Function_valueOf,               PROPF_METHOD}
298 };
299
300 static const builtin_info_t Function_info = {
301     JSCLASS_FUNCTION,
302     {NULL, Function_value, 0},
303     sizeof(Function_props)/sizeof(*Function_props),
304     Function_props,
305     Function_destructor,
306     NULL
307 };
308
309 static HRESULT create_function(script_ctx_t *ctx, DWORD flags, DispatchEx *prototype, FunctionInstance **ret)
310 {
311     FunctionInstance *function;
312     HRESULT hres;
313
314     function = heap_alloc_zero(sizeof(FunctionInstance));
315     if(!function)
316         return E_OUTOFMEMORY;
317
318     hres = init_dispex(&function->dispex, ctx, &Function_info, NULL);
319     if(FAILED(hres))
320         return hres;
321
322     function->flags = flags;
323     function->length = flags & PROPF_ARGMASK;
324
325     if(prototype) {
326         jsexcept_t jsexcept;
327         VARIANT var;
328
329         V_VT(&var) = VT_DISPATCH;
330         V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
331         memset(&jsexcept, 0, sizeof(jsexcept));
332
333         hres = jsdisp_propput_name(&function->dispex, prototypeW, ctx->lcid, &var, &jsexcept, NULL/*FIXME*/);
334         if(FAILED(hres)) {
335             IDispatchEx_Release(_IDispatchEx_(&function->dispex));
336             return hres;
337         }
338     }
339
340     *ret = function;
341     return S_OK;
342 }
343
344 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, DWORD flags,
345         DispatchEx *prototype, DispatchEx **ret)
346 {
347     FunctionInstance *function;
348     HRESULT hres;
349
350     hres = create_function(ctx, flags, prototype, &function);
351     if(FAILED(hres))
352         return hres;
353
354     function->value_proc = value_proc;
355
356     *ret = &function->dispex;
357     return S_OK;
358 }
359
360 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
361         scope_chain_t *scope_chain, DispatchEx **ret)
362 {
363     FunctionInstance *function;
364     parameter_t *iter;
365     DWORD length = 0;
366     HRESULT hres;
367
368     hres = create_function(ctx->script, PROPF_CONSTR, NULL, &function);
369     if(FAILED(hres))
370         return hres;
371
372     function->source = source;
373     function->parameters = parameters;
374
375     if(scope_chain) {
376         scope_addref(scope_chain);
377         function->scope_chain = scope_chain;
378     }
379
380     parser_addref(ctx);
381     function->parser = ctx;
382
383     for(iter = parameters; iter; iter = iter->next)
384         length++;
385     function->length = length;
386
387     *ret = &function->dispex;
388     return S_OK;
389 }