push 862965e7f7bbbfb960006fcded9111ae18c06ef6
[wine/hacks.git] / dlls / jscript / function.c
blobdac8d3e23b096b6f98322853bfa9cd71d926c36f
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
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.
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.
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
19 #include "jscript.h"
20 #include "engine.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
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;
39 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
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 applyW[] = {'a','p','p','l','y',0};
45 static const WCHAR callW[] = {'c','a','l','l',0};
46 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
47 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};
48 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
50 static IDispatch *get_this(DISPPARAMS *dp)
52 DWORD i;
54 for(i=0; i < dp->cNamedArgs; i++) {
55 if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
56 if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
57 return V_DISPATCH(dp->rgvarg+i);
59 WARN("This is not VT_DISPATCH\n");
60 return NULL;
64 TRACE("no this passed\n");
65 return NULL;
68 static HRESULT init_parameters(DispatchEx *var_disp, FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
69 jsexcept_t *ei, IServiceProvider *caller)
71 parameter_t *param;
72 VARIANT var_empty;
73 DWORD cargs, i=0;
74 HRESULT hres;
76 V_VT(&var_empty) = VT_EMPTY;
77 cargs = dp->cArgs - dp->cNamedArgs;
79 for(param = function->parameters; param; param = param->next) {
80 hres = jsdisp_propput_name(var_disp, param->identifier, lcid,
81 i < cargs ? dp->rgvarg + dp->cArgs-1 - i : &var_empty,
82 ei, caller);
83 if(FAILED(hres))
84 return hres;
86 i++;
89 return S_OK;
92 static HRESULT init_arguments(DispatchEx *arg_disp, FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
93 jsexcept_t *ei, IServiceProvider *caller)
95 VARIANT var;
96 DWORD i;
97 HRESULT hres;
99 for(i=0; i < dp->cArgs-dp->cNamedArgs; i++) {
100 hres = jsdisp_propput_idx(arg_disp, i, lcid, dp->rgvarg+dp->cArgs-1-i, ei, caller);
101 if(FAILED(hres))
102 return hres;
105 V_VT(&var) = VT_I4;
106 V_I4(&var) = dp->cArgs - dp->cNamedArgs;
107 return jsdisp_propput_name(arg_disp, lengthW, lcid, &var, ei, caller);
110 static HRESULT create_var_disp(FunctionInstance *function, LCID lcid, DISPPARAMS *dp, jsexcept_t *ei,
111 IServiceProvider *caller, DispatchEx **ret)
113 DispatchEx *var_disp, *arg_disp;
114 HRESULT hres;
116 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
118 hres = create_dispex(function->dispex.ctx, NULL, NULL, &var_disp);
119 if(FAILED(hres))
120 return hres;
122 hres = create_dispex(function->dispex.ctx, NULL, NULL, &arg_disp);
123 if(SUCCEEDED(hres)) {
124 hres = init_arguments(arg_disp, function, lcid, dp, ei, caller);
125 if(SUCCEEDED(hres)) {
126 VARIANT var;
128 V_VT(&var) = VT_DISPATCH;
129 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(arg_disp);
130 hres = jsdisp_propput_name(var_disp, argumentsW, lcid, &var, ei, caller);
133 jsdisp_release(arg_disp);
136 if(SUCCEEDED(hres))
137 hres = init_parameters(var_disp, function, lcid, dp, ei, caller);
138 if(FAILED(hres)) {
139 jsdisp_release(var_disp);
140 return hres;
143 *ret = var_disp;
144 return S_OK;
147 static HRESULT invoke_source(FunctionInstance *function, IDispatch *this_obj, LCID lcid, DISPPARAMS *dp,
148 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
150 DispatchEx *var_disp;
151 exec_ctx_t *exec_ctx;
152 scope_chain_t *scope;
153 HRESULT hres;
155 if(!function->source) {
156 FIXME("no source\n");
157 return E_FAIL;
160 hres = create_var_disp(function, lcid, dp, ei, caller, &var_disp);
161 if(FAILED(hres))
162 return hres;
164 hres = scope_push(function->scope_chain, var_disp, &scope);
165 if(SUCCEEDED(hres)) {
166 hres = create_exec_ctx(this_obj, var_disp, scope, &exec_ctx);
167 scope_release(scope);
169 if(FAILED(hres))
170 return hres;
172 hres = exec_source(exec_ctx, function->parser, function->source, ei, retv);
173 exec_release(exec_ctx);
175 return hres;
178 static HRESULT invoke_function(FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
179 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
181 IDispatch *this_obj;
183 if(!(this_obj = get_this(dp)))
184 this_obj = (IDispatch*)_IDispatchEx_(function->dispex.ctx->script_disp);
186 return invoke_source(function, this_obj, lcid, dp, retv, ei, caller);
189 static HRESULT invoke_constructor(FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
190 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
192 DispatchEx *this_obj;
193 HRESULT hres;
195 hres = create_object(function->dispex.ctx, &function->dispex, &this_obj);
196 if(FAILED(hres))
197 return hres;
199 hres = invoke_source(function, (IDispatch*)_IDispatchEx_(this_obj), lcid, dp, retv, ei, caller);
200 if(FAILED(hres)) {
201 jsdisp_release(this_obj);
202 return hres;
205 V_VT(retv) = VT_DISPATCH;
206 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(this_obj);
207 return S_OK;
210 static HRESULT invoke_value_proc(FunctionInstance *function, LCID lcid, WORD flags, DISPPARAMS *dp,
211 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
213 DispatchEx *this_obj = NULL;
214 IDispatch *this_disp;
215 HRESULT hres;
217 this_disp = get_this(dp);
218 if(this_disp)
219 this_obj = iface_to_jsdisp((IUnknown*)this_disp);
221 hres = function->value_proc(this_obj ? this_obj : function->dispex.ctx->script_disp, lcid,
222 flags, dp, retv, ei, caller);
224 if(this_obj)
225 jsdisp_release(this_obj);
226 return hres;
229 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
231 BSTR str;
233 if(function->value_proc) {
234 FIXME("Builtin functions not implemented\n");
235 return E_NOTIMPL;
238 str = SysAllocStringLen(function->src_str, function->src_len);
239 if(!str)
240 return E_OUTOFMEMORY;
242 *ret = str;
243 return S_OK;
246 static HRESULT Function_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
247 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
249 FunctionInstance *This = (FunctionInstance*)dispex;
251 TRACE("%p %d\n", This, This->length);
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;
263 return S_OK;
266 static HRESULT Function_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
267 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
269 FunctionInstance *function;
270 BSTR str;
271 HRESULT hres;
273 TRACE("\n");
275 if(!is_class(dispex, JSCLASS_FUNCTION)) {
276 FIXME("throw TypeError\n");
277 return E_FAIL;
280 function = (FunctionInstance*)dispex;
282 hres = function_to_string(function, &str);
283 if(FAILED(hres))
284 return hres;
286 if(retv) {
287 V_VT(retv) = VT_BSTR;
288 V_BSTR(retv) = str;
289 }else {
290 SysFreeString(str);
292 return S_OK;
295 static HRESULT Function_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
296 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
298 FIXME("\n");
299 return E_NOTIMPL;
302 static HRESULT Function_apply(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
303 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
305 FIXME("\n");
306 return E_NOTIMPL;
309 static HRESULT Function_call(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
310 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
312 FIXME("\n");
313 return E_NOTIMPL;
316 static HRESULT Function_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
317 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
319 FIXME("\n");
320 return E_NOTIMPL;
323 static HRESULT Function_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
324 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
326 FIXME("\n");
327 return E_NOTIMPL;
330 static HRESULT Function_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
331 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
333 FIXME("\n");
334 return E_NOTIMPL;
337 HRESULT Function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
338 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
340 FunctionInstance *function;
342 TRACE("\n");
344 if(dispex->builtin_info->class != JSCLASS_FUNCTION) {
345 ERR("dispex is not a function\n");
346 return E_FAIL;
349 function = (FunctionInstance*)dispex;
351 switch(flags) {
352 case DISPATCH_METHOD:
353 if(function->value_proc)
354 return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
356 return invoke_function(function, lcid, dp, retv, ei, caller);
358 case DISPATCH_PROPERTYGET: {
359 HRESULT hres;
360 BSTR str;
362 hres = function_to_string(function, &str);
363 if(FAILED(hres))
364 return hres;
366 V_VT(retv) = VT_BSTR;
367 V_BSTR(retv) = str;
368 break;
371 case DISPATCH_CONSTRUCT:
372 if(function->value_proc)
373 return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
375 return invoke_constructor(function, lcid, dp, retv, ei, caller);
377 default:
378 FIXME("not implemented flags %x\n", flags);
379 return E_NOTIMPL;
382 return S_OK;
385 static void Function_destructor(DispatchEx *dispex)
387 FunctionInstance *This = (FunctionInstance*)dispex;
389 if(This->parser)
390 parser_release(This->parser);
391 if(This->scope_chain)
392 scope_release(This->scope_chain);
393 heap_free(This);
396 static const builtin_prop_t Function_props[] = {
397 {applyW, Function_apply, PROPF_METHOD},
398 {callW, Function_call, PROPF_METHOD},
399 {hasOwnPropertyW, Function_hasOwnProperty, PROPF_METHOD},
400 {isPrototypeOfW, Function_isPrototypeOf, PROPF_METHOD},
401 {lengthW, Function_length, 0},
402 {propertyIsEnumerableW, Function_propertyIsEnumerable, PROPF_METHOD},
403 {toLocaleStringW, Function_toLocaleString, PROPF_METHOD},
404 {toStringW, Function_toString, PROPF_METHOD}
407 static const builtin_info_t Function_info = {
408 JSCLASS_FUNCTION,
409 {NULL, Function_value, 0},
410 sizeof(Function_props)/sizeof(*Function_props),
411 Function_props,
412 Function_destructor,
413 NULL
416 static HRESULT FunctionConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
417 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
419 FIXME("\n");
420 return E_NOTIMPL;
423 static HRESULT FunctionProt_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
424 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
426 FIXME("\n");
427 return E_NOTIMPL;
430 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
431 BOOL funcprot, DispatchEx *prototype, FunctionInstance **ret)
433 FunctionInstance *function;
434 HRESULT hres;
436 function = heap_alloc_zero(sizeof(FunctionInstance));
437 if(!function)
438 return E_OUTOFMEMORY;
440 if(funcprot)
441 hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
442 else if(builtin_info)
443 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
444 else
445 hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
446 if(FAILED(hres))
447 return hres;
449 function->flags = flags;
450 function->length = flags & PROPF_ARGMASK;
452 *ret = function;
453 return S_OK;
456 static HRESULT set_prototype(script_ctx_t *ctx, DispatchEx *dispex, DispatchEx *prototype)
458 jsexcept_t jsexcept;
459 VARIANT var;
461 V_VT(&var) = VT_DISPATCH;
462 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
463 memset(&jsexcept, 0, sizeof(jsexcept));
465 return jsdisp_propput_name(dispex, prototypeW, ctx->lcid, &var, &jsexcept, NULL/*FIXME*/);
468 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc,
469 const builtin_info_t *builtin_info, DWORD flags, DispatchEx *prototype, DispatchEx **ret)
471 FunctionInstance *function;
472 HRESULT hres;
474 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
475 if(FAILED(hres))
476 return hres;
478 hres = set_prototype(ctx, &function->dispex, prototype);
479 if(FAILED(hres)) {
480 jsdisp_release(&function->dispex);
481 return hres;
484 function->value_proc = value_proc;
486 *ret = &function->dispex;
487 return S_OK;
490 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
491 scope_chain_t *scope_chain, const WCHAR *src_str, DWORD src_len, DispatchEx **ret)
493 FunctionInstance *function;
494 DispatchEx *prototype;
495 parameter_t *iter;
496 DWORD length = 0;
497 HRESULT hres;
499 hres = create_object(ctx->script, NULL, &prototype);
500 if(FAILED(hres))
501 return hres;
503 hres = create_function(ctx->script, NULL, PROPF_CONSTR, FALSE, NULL, &function);
504 if(SUCCEEDED(hres)) {
505 hres = set_prototype(ctx->script, &function->dispex, prototype);
506 if(FAILED(hres))
507 jsdisp_release(&function->dispex);
509 jsdisp_release(prototype);
510 if(FAILED(hres))
511 return hres;
513 function->source = source;
514 function->parameters = parameters;
516 if(scope_chain) {
517 scope_addref(scope_chain);
518 function->scope_chain = scope_chain;
521 parser_addref(ctx);
522 function->parser = ctx;
524 for(iter = parameters; iter; iter = iter->next)
525 length++;
526 function->length = length;
528 function->src_str = src_str;
529 function->src_len = src_len;
531 *ret = &function->dispex;
532 return S_OK;
535 HRESULT init_function_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
537 FunctionInstance *prot, *constr;
538 HRESULT hres;
540 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
541 if(FAILED(hres))
542 return hres;
544 prot->value_proc = FunctionProt_value;
546 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, &prot->dispex, &constr);
547 if(SUCCEEDED(hres)) {
548 constr->value_proc = FunctionConstr_value;
549 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
550 if(FAILED(hres))
551 jsdisp_release(&constr->dispex);
553 jsdisp_release(&prot->dispex);
554 if(FAILED(hres))
555 return hres;
557 ctx->function_constr = &constr->dispex;
558 return S_OK;