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
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28 builtin_invoke_t value_proc;
30 source_elements_t *source;
31 parameter_t *parameters;
32 scope_chain_t *scope_chain;
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 applyW[] = {'a','p','p','l','y',0};
44 static const WCHAR callW[] = {'c','a','l','l',0};
46 static IDispatch *get_this(DISPPARAMS *dp)
50 for(i=0; i < dp->cNamedArgs; i++) {
51 if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
52 if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
53 return V_DISPATCH(dp->rgvarg+i);
55 WARN("This is not VT_DISPATCH\n");
60 TRACE("no this passed\n");
64 static HRESULT init_parameters(DispatchEx *var_disp, FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
65 jsexcept_t *ei, IServiceProvider *caller)
72 V_VT(&var_empty) = VT_EMPTY;
73 cargs = dp->cArgs - dp->cNamedArgs;
75 for(param = function->parameters; param; param = param->next) {
76 hres = jsdisp_propput_name(var_disp, param->identifier, lcid,
77 i < cargs ? dp->rgvarg + dp->cArgs-1 - i : &var_empty,
88 static HRESULT Arguments_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
89 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
95 static const builtin_info_t Arguments_info = {
97 {NULL, Arguments_value, 0},
103 static HRESULT create_arguments(script_ctx_t *ctx, LCID lcid, DISPPARAMS *dp,
104 jsexcept_t *ei, IServiceProvider *caller, DispatchEx **ret)
111 args = heap_alloc_zero(sizeof(DispatchEx));
113 return E_OUTOFMEMORY;
115 hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
121 for(i=0; i < arg_cnt(dp); i++) {
122 hres = jsdisp_propput_idx(args, i, lcid, get_arg(dp,i), ei, caller);
127 if(SUCCEEDED(hres)) {
129 V_I4(&var) = arg_cnt(dp);
130 hres = jsdisp_propput_name(args, lengthW, lcid, &var, ei, caller);
134 jsdisp_release(args);
142 static HRESULT create_var_disp(FunctionInstance *function, LCID lcid, DISPPARAMS *dp, jsexcept_t *ei,
143 IServiceProvider *caller, DispatchEx **ret)
145 DispatchEx *var_disp, *arg_disp;
148 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
150 hres = create_dispex(function->dispex.ctx, NULL, NULL, &var_disp);
154 hres = create_arguments(function->dispex.ctx, lcid, dp, ei, caller, &arg_disp);
155 if(SUCCEEDED(hres)) {
158 V_VT(&var) = VT_DISPATCH;
159 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(arg_disp);
160 hres = jsdisp_propput_name(var_disp, argumentsW, lcid, &var, ei, caller);
161 jsdisp_release(arg_disp);
165 hres = init_parameters(var_disp, function, lcid, dp, ei, caller);
167 jsdisp_release(var_disp);
175 static HRESULT invoke_source(FunctionInstance *function, IDispatch *this_obj, LCID lcid, DISPPARAMS *dp,
176 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
178 DispatchEx *var_disp;
179 exec_ctx_t *exec_ctx;
180 scope_chain_t *scope;
183 if(!function->source) {
184 FIXME("no source\n");
188 hres = create_var_disp(function, lcid, dp, ei, caller, &var_disp);
192 hres = scope_push(function->scope_chain, var_disp, &scope);
193 if(SUCCEEDED(hres)) {
194 hres = create_exec_ctx(this_obj, var_disp, scope, &exec_ctx);
195 scope_release(scope);
200 hres = exec_source(exec_ctx, function->parser, function->source, ei, retv);
201 exec_release(exec_ctx);
206 static HRESULT invoke_function(FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
207 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
211 if(!(this_obj = get_this(dp)))
212 this_obj = (IDispatch*)_IDispatchEx_(function->dispex.ctx->script_disp);
214 return invoke_source(function, this_obj, lcid, dp, retv, ei, caller);
217 static HRESULT invoke_constructor(FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
218 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
220 DispatchEx *this_obj;
223 hres = create_object(function->dispex.ctx, &function->dispex, &this_obj);
227 hres = invoke_source(function, (IDispatch*)_IDispatchEx_(this_obj), lcid, dp, retv, ei, caller);
229 jsdisp_release(this_obj);
233 V_VT(retv) = VT_DISPATCH;
234 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(this_obj);
238 static HRESULT invoke_value_proc(FunctionInstance *function, LCID lcid, WORD flags, DISPPARAMS *dp,
239 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
241 DispatchEx *this_obj = NULL;
242 IDispatch *this_disp;
245 this_disp = get_this(dp);
247 this_obj = iface_to_jsdisp((IUnknown*)this_disp);
249 hres = function->value_proc(this_obj ? this_obj : function->dispex.ctx->script_disp, lcid,
250 flags, dp, retv, ei, caller);
253 jsdisp_release(this_obj);
257 static HRESULT call_function(FunctionInstance *function, IDispatch *this_obj, LCID lcid, DISPPARAMS *args,
258 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
262 if(function->value_proc) {
263 DispatchEx *jsthis = NULL;
266 jsthis = iface_to_jsdisp((IUnknown*)this_obj);
268 FIXME("this_obj is not DispatchEx\n");
271 hres = function->value_proc(jsthis ? jsthis : function->dispex.ctx->script_disp, lcid,
272 DISPATCH_METHOD, args, retv, ei, caller);
275 jsdisp_release(jsthis);
277 hres = invoke_source(function,
278 this_obj ? this_obj : (IDispatch*)_IDispatchEx_(function->dispex.ctx->script_disp),
279 lcid, args, retv, ei, caller);
285 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
289 if(function->value_proc) {
290 FIXME("Builtin functions not implemented\n");
294 str = SysAllocStringLen(function->src_str, function->src_len);
296 return E_OUTOFMEMORY;
302 static HRESULT Function_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
303 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
305 FunctionInstance *This = (FunctionInstance*)dispex;
307 TRACE("%p %d\n", This, This->length);
310 case DISPATCH_PROPERTYGET:
312 V_I4(retv) = This->length;
315 FIXME("unimplemented flags %x\n", flags);
322 static HRESULT Function_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
323 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
325 FunctionInstance *function;
331 if(!is_class(dispex, JSCLASS_FUNCTION))
332 return throw_type_error(dispex->ctx, ei, IDS_NOT_FUNC, NULL);
334 function = (FunctionInstance*)dispex;
336 hres = function_to_string(function, &str);
341 V_VT(retv) = VT_BSTR;
349 static HRESULT array_to_args(DispatchEx *arg_array, LCID lcid, jsexcept_t *ei, IServiceProvider *caller,
356 hres = jsdisp_propget_name(arg_array, lengthW, lcid, &var, ei, NULL/*FIXME*/);
360 hres = to_uint32(arg_array->ctx, &var, ei, &length);
365 argv = heap_alloc(length * sizeof(VARIANT));
367 return E_OUTOFMEMORY;
369 for(i=0; i<length; i++) {
370 hres = jsdisp_propget_idx(arg_array, i, lcid, argv+i, ei, caller);
373 VariantClear(argv+i);
379 args->cArgs = length;
384 static HRESULT Function_apply(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
385 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
387 FunctionInstance *function;
388 DISPPARAMS args = {NULL,NULL,0,0};
395 if(!is_class(dispex, JSCLASS_FUNCTION)) {
396 FIXME("dispex is not a function\n");
400 function = (FunctionInstance*)dispex;
404 hres = to_object(dispex->ctx, get_arg(dp,0), &this_obj);
410 DispatchEx *arg_array = NULL;
412 if(V_VT(get_arg(dp,1)) == VT_DISPATCH) {
413 arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(get_arg(dp,1)));
414 if(!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS)) {
415 jsdisp_release(arg_array);
421 hres = array_to_args(arg_array, lcid, ei, caller, &args);
422 jsdisp_release(arg_array);
424 FIXME("throw TypeError\n");
429 hres = call_function(function, this_obj, lcid, &args, retv, ei, caller);
432 IDispatch_Release(this_obj);
433 for(i=0; i<args.cArgs; i++)
434 VariantClear(args.rgvarg+i);
435 heap_free(args.rgvarg);
439 static HRESULT Function_call(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
440 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
442 FunctionInstance *function;
443 DISPPARAMS args = {NULL,NULL,0,0};
444 IDispatch *this_obj = NULL;
450 if(!is_class(dispex, JSCLASS_FUNCTION)) {
451 FIXME("dispex is not a function\n");
455 function = (FunctionInstance*)dispex;
459 hres = to_object(dispex->ctx, get_arg(dp,0), &this_obj);
466 args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1;
468 hres = call_function(function, this_obj, lcid, &args, retv, ei, caller);
471 IDispatch_Release(this_obj);
475 HRESULT Function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
476 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
478 FunctionInstance *function;
482 if(dispex->builtin_info->class != JSCLASS_FUNCTION) {
483 ERR("dispex is not a function\n");
487 function = (FunctionInstance*)dispex;
490 case DISPATCH_METHOD:
491 if(function->value_proc)
492 return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
494 return invoke_function(function, lcid, dp, retv, ei, caller);
496 case DISPATCH_PROPERTYGET: {
500 hres = function_to_string(function, &str);
504 V_VT(retv) = VT_BSTR;
509 case DISPATCH_CONSTRUCT:
510 if(function->value_proc)
511 return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
513 return invoke_constructor(function, lcid, dp, retv, ei, caller);
516 FIXME("not implemented flags %x\n", flags);
523 static void Function_destructor(DispatchEx *dispex)
525 FunctionInstance *This = (FunctionInstance*)dispex;
528 parser_release(This->parser);
529 if(This->scope_chain)
530 scope_release(This->scope_chain);
534 static const builtin_prop_t Function_props[] = {
535 {applyW, Function_apply, PROPF_METHOD|2},
536 {callW, Function_call, PROPF_METHOD|1},
537 {lengthW, Function_length, 0},
538 {toStringW, Function_toString, PROPF_METHOD}
541 static const builtin_info_t Function_info = {
543 {NULL, Function_value, 0},
544 sizeof(Function_props)/sizeof(*Function_props),
550 static HRESULT FunctionConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
551 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
557 static HRESULT FunctionProt_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
558 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
564 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
565 BOOL funcprot, DispatchEx *prototype, FunctionInstance **ret)
567 FunctionInstance *function;
570 function = heap_alloc_zero(sizeof(FunctionInstance));
572 return E_OUTOFMEMORY;
575 hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
576 else if(builtin_info)
577 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
579 hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
583 function->flags = flags;
584 function->length = flags & PROPF_ARGMASK;
590 static HRESULT set_prototype(script_ctx_t *ctx, DispatchEx *dispex, DispatchEx *prototype)
595 V_VT(&var) = VT_DISPATCH;
596 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
597 memset(&jsexcept, 0, sizeof(jsexcept));
599 return jsdisp_propput_name(dispex, prototypeW, ctx->lcid, &var, &jsexcept, NULL/*FIXME*/);
602 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc,
603 const builtin_info_t *builtin_info, DWORD flags, DispatchEx *prototype, DispatchEx **ret)
605 FunctionInstance *function;
608 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
612 hres = set_prototype(ctx, &function->dispex, prototype);
614 jsdisp_release(&function->dispex);
618 function->value_proc = value_proc;
620 *ret = &function->dispex;
624 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
625 scope_chain_t *scope_chain, const WCHAR *src_str, DWORD src_len, DispatchEx **ret)
627 FunctionInstance *function;
628 DispatchEx *prototype;
633 hres = create_object(ctx->script, NULL, &prototype);
637 hres = create_function(ctx->script, NULL, PROPF_CONSTR, FALSE, NULL, &function);
638 if(SUCCEEDED(hres)) {
639 hres = set_prototype(ctx->script, &function->dispex, prototype);
641 jsdisp_release(&function->dispex);
643 jsdisp_release(prototype);
647 function->source = source;
648 function->parameters = parameters;
651 scope_addref(scope_chain);
652 function->scope_chain = scope_chain;
656 function->parser = ctx;
658 for(iter = parameters; iter; iter = iter->next)
660 function->length = length;
662 function->src_str = src_str;
663 function->src_len = src_len;
665 *ret = &function->dispex;
669 HRESULT init_function_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
671 FunctionInstance *prot, *constr;
674 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
678 prot->value_proc = FunctionProt_value;
680 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, &prot->dispex, &constr);
681 if(SUCCEEDED(hres)) {
682 constr->value_proc = FunctionConstr_value;
683 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
685 jsdisp_release(&constr->dispex);
687 jsdisp_release(&prot->dispex);
691 ctx->function_constr = &constr->dispex;