jscript: Set arguments object 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 init_arguments(DispatchEx *arg_disp, FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
92         jsexcept_t *ei, IServiceProvider *caller)
93 {
94     VARIANT var;
95     DWORD i;
96     HRESULT hres;
97
98     for(i=0; i < dp->cArgs-dp->cNamedArgs; i++) {
99         hres = jsdisp_propput_idx(arg_disp, i, lcid, dp->rgvarg+dp->cArgs-1-i, ei, caller);
100         if(FAILED(hres))
101             return hres;
102     }
103
104     V_VT(&var) = VT_I4;
105     V_I4(&var) = dp->cArgs - dp->cNamedArgs;
106     return jsdisp_propput_name(arg_disp, lengthW, lcid, &var, ei, caller);
107 }
108
109 static HRESULT create_var_disp(FunctionInstance *function, LCID lcid, DISPPARAMS *dp, jsexcept_t *ei,
110                                IServiceProvider *caller, DispatchEx **ret)
111 {
112     DispatchEx *var_disp, *arg_disp;
113     HRESULT hres;
114
115     static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
116
117     hres = create_dispex(function->dispex.ctx, NULL, NULL, &var_disp);
118     if(FAILED(hres))
119         return hres;
120
121     hres = create_dispex(function->dispex.ctx, NULL, NULL, &arg_disp);
122     if(SUCCEEDED(hres)) {
123         hres = init_arguments(arg_disp, function, lcid, dp, ei, caller);
124         if(SUCCEEDED(hres)) {
125             VARIANT var;
126
127             V_VT(&var) = VT_DISPATCH;
128             V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(arg_disp);
129             hres = jsdisp_propput_name(var_disp, argumentsW, lcid, &var, ei, caller);
130         }
131
132         jsdisp_release(arg_disp);
133     }
134
135     if(SUCCEEDED(hres))
136         hres = init_parameters(var_disp, function, lcid, dp, ei, caller);
137     if(FAILED(hres)) {
138         jsdisp_release(var_disp);
139         return hres;
140     }
141
142     *ret = var_disp;
143     return S_OK;
144 }
145
146 static HRESULT invoke_source(FunctionInstance *function, IDispatch *this_obj, LCID lcid, DISPPARAMS *dp,
147         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
148 {
149     DispatchEx *var_disp;
150     exec_ctx_t *exec_ctx;
151     scope_chain_t *scope;
152     HRESULT hres;
153
154     if(!function->source) {
155         FIXME("no source\n");
156         return E_FAIL;
157     }
158
159     hres = create_var_disp(function, lcid, dp, ei, caller, &var_disp);
160     if(FAILED(hres))
161         return hres;
162
163     hres = scope_push(function->scope_chain, var_disp, &scope);
164     if(SUCCEEDED(hres)) {
165         hres = create_exec_ctx(this_obj, var_disp, scope, &exec_ctx);
166         scope_release(scope);
167     }
168     if(FAILED(hres))
169         return hres;
170
171     hres = exec_source(exec_ctx, function->parser, function->source, ei, retv);
172     exec_release(exec_ctx);
173
174     return hres;
175 }
176
177 static HRESULT invoke_function(FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
178         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
179 {
180     IDispatch *this_obj;
181
182     if(!(this_obj = get_this(dp)))
183         this_obj = (IDispatch*)_IDispatchEx_(function->dispex.ctx->script_disp);
184
185     return invoke_source(function, this_obj, lcid, dp, retv, ei, caller);
186 }
187
188 static HRESULT invoke_value_proc(FunctionInstance *function, LCID lcid, WORD flags, DISPPARAMS *dp,
189         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
190 {
191     DispatchEx *this_obj = NULL;
192     IDispatch *this_disp;
193     HRESULT hres;
194
195     this_disp = get_this(dp);
196     if(this_disp)
197         this_obj = iface_to_jsdisp((IUnknown*)this_disp);
198
199     hres = function->value_proc(this_obj ? this_obj : function->dispex.ctx->script_disp, lcid,
200                                 flags, dp, retv, ei, caller);
201
202     if(this_obj)
203         jsdisp_release(this_obj);
204     return hres;
205 }
206
207 static HRESULT Function_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
208         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
209 {
210     FunctionInstance *This = (FunctionInstance*)dispex;
211
212     TRACE("%p %d\n", This, This->length);
213
214     switch(flags) {
215     case DISPATCH_PROPERTYGET:
216         V_VT(retv) = VT_I4;
217         V_I4(retv) = This->length;
218         break;
219     default:
220         FIXME("unimplemented flags %x\n", flags);
221         return E_NOTIMPL;
222     }
223
224     return S_OK;
225 }
226
227 static HRESULT Function_toString(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_toLocaleString(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_valueOf(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_apply(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
249         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
250 {
251     FIXME("\n");
252     return E_NOTIMPL;
253 }
254
255 static HRESULT Function_call(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
256         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
257 {
258     FIXME("\n");
259     return E_NOTIMPL;
260 }
261
262 static HRESULT Function_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
263         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
264 {
265     FIXME("\n");
266     return E_NOTIMPL;
267 }
268
269 static HRESULT Function_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
270         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
271 {
272     FIXME("\n");
273     return E_NOTIMPL;
274 }
275
276 static HRESULT Function_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
277         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
278 {
279     FIXME("\n");
280     return E_NOTIMPL;
281 }
282
283 static HRESULT Function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
284         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
285 {
286     FunctionInstance *function;
287
288     TRACE("\n");
289
290     if(dispex->builtin_info->class != JSCLASS_FUNCTION) {
291         ERR("dispex is not a function\n");
292         return E_FAIL;
293     }
294
295     function = (FunctionInstance*)dispex;
296
297     switch(flags) {
298     case DISPATCH_METHOD:
299         if(function->value_proc)
300             return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
301
302         return invoke_function(function, lcid, dp, retv, ei, caller);
303
304     default:
305         FIXME("not implemented flags %x\n", flags);
306         return E_NOTIMPL;
307     }
308
309     return S_OK;
310 }
311
312 static void Function_destructor(DispatchEx *dispex)
313 {
314     FunctionInstance *This = (FunctionInstance*)dispex;
315
316     if(This->parser)
317         parser_release(This->parser);
318     if(This->scope_chain)
319         scope_release(This->scope_chain);
320     heap_free(This);
321 }
322
323 static const builtin_prop_t Function_props[] = {
324     {applyW,                 Function_apply,                 PROPF_METHOD},
325     {callW,                  Function_call,                  PROPF_METHOD},
326     {hasOwnPropertyW,        Function_hasOwnProperty,        PROPF_METHOD},
327     {isPrototypeOfW,         Function_isPrototypeOf,         PROPF_METHOD},
328     {lengthW,                Function_length,                0},
329     {propertyIsEnumerableW,  Function_propertyIsEnumerable,  PROPF_METHOD},
330     {toLocaleStringW,        Function_toLocaleString,        PROPF_METHOD},
331     {toStringW,              Function_toString,              PROPF_METHOD},
332     {valueOfW,               Function_valueOf,               PROPF_METHOD}
333 };
334
335 static const builtin_info_t Function_info = {
336     JSCLASS_FUNCTION,
337     {NULL, Function_value, 0},
338     sizeof(Function_props)/sizeof(*Function_props),
339     Function_props,
340     Function_destructor,
341     NULL
342 };
343
344 static HRESULT create_function(script_ctx_t *ctx, DWORD flags, DispatchEx *prototype, FunctionInstance **ret)
345 {
346     FunctionInstance *function;
347     HRESULT hres;
348
349     function = heap_alloc_zero(sizeof(FunctionInstance));
350     if(!function)
351         return E_OUTOFMEMORY;
352
353     hres = init_dispex(&function->dispex, ctx, &Function_info, NULL);
354     if(FAILED(hres))
355         return hres;
356
357     function->flags = flags;
358     function->length = flags & PROPF_ARGMASK;
359
360     if(prototype) {
361         jsexcept_t jsexcept;
362         VARIANT var;
363
364         V_VT(&var) = VT_DISPATCH;
365         V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
366         memset(&jsexcept, 0, sizeof(jsexcept));
367
368         hres = jsdisp_propput_name(&function->dispex, prototypeW, ctx->lcid, &var, &jsexcept, NULL/*FIXME*/);
369         if(FAILED(hres)) {
370             IDispatchEx_Release(_IDispatchEx_(&function->dispex));
371             return hres;
372         }
373     }
374
375     *ret = function;
376     return S_OK;
377 }
378
379 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, DWORD flags,
380         DispatchEx *prototype, DispatchEx **ret)
381 {
382     FunctionInstance *function;
383     HRESULT hres;
384
385     hres = create_function(ctx, flags, prototype, &function);
386     if(FAILED(hres))
387         return hres;
388
389     function->value_proc = value_proc;
390
391     *ret = &function->dispex;
392     return S_OK;
393 }
394
395 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
396         scope_chain_t *scope_chain, DispatchEx **ret)
397 {
398     FunctionInstance *function;
399     parameter_t *iter;
400     DWORD length = 0;
401     HRESULT hres;
402
403     hres = create_function(ctx->script, PROPF_CONSTR, NULL, &function);
404     if(FAILED(hres))
405         return hres;
406
407     function->source = source;
408     function->parameters = parameters;
409
410     if(scope_chain) {
411         scope_addref(scope_chain);
412         function->scope_chain = scope_chain;
413     }
414
415     parser_addref(ctx);
416     function->parser = ctx;
417
418     for(iter = parameters; iter; iter = iter->next)
419         length++;
420     function->length = length;
421
422     *ret = &function->dispex;
423     return S_OK;
424 }