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;
75 for(param = function->parameters; param; param = param->next) {
76 hres = jsdisp_propput_name(var_disp, param->identifier, lcid,
77 i < cargs ? get_arg(dp,i) : &var_empty, ei, caller);
87 static HRESULT Arguments_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
88 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
94 static const builtin_info_t Arguments_info = {
96 {NULL, Arguments_value, 0},
102 static HRESULT create_arguments(script_ctx_t *ctx, LCID lcid, DISPPARAMS *dp,
103 jsexcept_t *ei, IServiceProvider *caller, DispatchEx **ret)
110 args = heap_alloc_zero(sizeof(DispatchEx));
112 return E_OUTOFMEMORY;
114 hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
120 for(i=0; i < arg_cnt(dp); i++) {
121 hres = jsdisp_propput_idx(args, i, lcid, get_arg(dp,i), ei, caller);
126 if(SUCCEEDED(hres)) {
128 V_I4(&var) = arg_cnt(dp);
129 hres = jsdisp_propput_name(args, lengthW, lcid, &var, ei, caller);
133 jsdisp_release(args);
141 static HRESULT create_var_disp(FunctionInstance *function, LCID lcid, DISPPARAMS *dp, jsexcept_t *ei,
142 IServiceProvider *caller, DispatchEx **ret)
144 DispatchEx *var_disp, *arg_disp;
147 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
149 hres = create_dispex(function->dispex.ctx, NULL, NULL, &var_disp);
153 hres = create_arguments(function->dispex.ctx, lcid, dp, ei, caller, &arg_disp);
154 if(SUCCEEDED(hres)) {
157 V_VT(&var) = VT_DISPATCH;
158 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(arg_disp);
159 hres = jsdisp_propput_name(var_disp, argumentsW, lcid, &var, ei, caller);
160 jsdisp_release(arg_disp);
164 hres = init_parameters(var_disp, function, lcid, dp, ei, caller);
166 jsdisp_release(var_disp);
174 static HRESULT invoke_source(FunctionInstance *function, IDispatch *this_obj, LCID lcid, DISPPARAMS *dp,
175 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
177 DispatchEx *var_disp;
178 exec_ctx_t *exec_ctx;
179 scope_chain_t *scope;
182 if(!function->source) {
183 FIXME("no source\n");
187 hres = create_var_disp(function, lcid, dp, ei, caller, &var_disp);
191 hres = scope_push(function->scope_chain, var_disp, &scope);
192 if(SUCCEEDED(hres)) {
193 hres = create_exec_ctx(this_obj, var_disp, scope, &exec_ctx);
194 scope_release(scope);
199 hres = exec_source(exec_ctx, function->parser, function->source, ei, retv);
200 exec_release(exec_ctx);
205 static HRESULT invoke_function(FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
206 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
210 if(!(this_obj = get_this(dp)))
211 this_obj = (IDispatch*)_IDispatchEx_(function->dispex.ctx->script_disp);
213 return invoke_source(function, this_obj, lcid, dp, retv, ei, caller);
216 static HRESULT invoke_constructor(FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
217 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
219 DispatchEx *this_obj;
222 hres = create_object(function->dispex.ctx, &function->dispex, &this_obj);
226 hres = invoke_source(function, (IDispatch*)_IDispatchEx_(this_obj), lcid, dp, retv, ei, caller);
228 jsdisp_release(this_obj);
232 V_VT(retv) = VT_DISPATCH;
233 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(this_obj);
237 static HRESULT invoke_value_proc(FunctionInstance *function, LCID lcid, WORD flags, DISPPARAMS *dp,
238 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
240 DispatchEx *this_obj = NULL;
241 IDispatch *this_disp;
244 this_disp = get_this(dp);
246 this_obj = iface_to_jsdisp((IUnknown*)this_disp);
248 hres = function->value_proc(this_obj ? this_obj : function->dispex.ctx->script_disp, lcid,
249 flags, dp, retv, ei, caller);
252 jsdisp_release(this_obj);
256 static HRESULT call_function(FunctionInstance *function, IDispatch *this_obj, LCID lcid, DISPPARAMS *args,
257 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
261 if(function->value_proc) {
262 DispatchEx *jsthis = NULL;
265 jsthis = iface_to_jsdisp((IUnknown*)this_obj);
267 FIXME("this_obj is not DispatchEx\n");
270 hres = function->value_proc(jsthis ? jsthis : function->dispex.ctx->script_disp, lcid,
271 DISPATCH_METHOD, args, retv, ei, caller);
274 jsdisp_release(jsthis);
276 hres = invoke_source(function,
277 this_obj ? this_obj : (IDispatch*)_IDispatchEx_(function->dispex.ctx->script_disp),
278 lcid, args, retv, ei, caller);
284 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
288 if(function->value_proc) {
289 FIXME("Builtin functions not implemented\n");
293 str = SysAllocStringLen(function->src_str, function->src_len);
295 return E_OUTOFMEMORY;
301 static HRESULT Function_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
302 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
304 FunctionInstance *This = (FunctionInstance*)dispex;
306 TRACE("%p %d\n", This, This->length);
309 case DISPATCH_PROPERTYGET:
311 V_I4(retv) = This->length;
314 FIXME("unimplemented flags %x\n", flags);
321 static HRESULT Function_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
322 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
324 FunctionInstance *function;
330 if(!is_class(dispex, JSCLASS_FUNCTION))
331 return throw_type_error(dispex->ctx, ei, IDS_NOT_FUNC, NULL);
333 function = (FunctionInstance*)dispex;
335 hres = function_to_string(function, &str);
340 V_VT(retv) = VT_BSTR;
348 static HRESULT array_to_args(DispatchEx *arg_array, LCID lcid, jsexcept_t *ei, IServiceProvider *caller,
355 hres = jsdisp_propget_name(arg_array, lengthW, lcid, &var, ei, NULL/*FIXME*/);
359 hres = to_uint32(arg_array->ctx, &var, ei, &length);
364 argv = heap_alloc(length * sizeof(VARIANT));
366 return E_OUTOFMEMORY;
368 for(i=0; i<length; i++) {
369 hres = jsdisp_propget_idx(arg_array, i, lcid, argv+i, ei, caller);
372 VariantClear(argv+i);
378 args->cArgs = length;
383 static HRESULT Function_apply(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
384 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
386 FunctionInstance *function;
387 DISPPARAMS args = {NULL,NULL,0,0};
394 if(!is_class(dispex, JSCLASS_FUNCTION)) {
395 FIXME("dispex is not a function\n");
399 function = (FunctionInstance*)dispex;
403 hres = to_object(dispex->ctx, get_arg(dp,0), &this_obj);
409 DispatchEx *arg_array = NULL;
411 if(V_VT(get_arg(dp,1)) == VT_DISPATCH) {
412 arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(get_arg(dp,1)));
413 if(!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS)) {
414 jsdisp_release(arg_array);
420 hres = array_to_args(arg_array, lcid, ei, caller, &args);
421 jsdisp_release(arg_array);
423 FIXME("throw TypeError\n");
428 hres = call_function(function, this_obj, lcid, &args, retv, ei, caller);
431 IDispatch_Release(this_obj);
432 for(i=0; i<args.cArgs; i++)
433 VariantClear(args.rgvarg+i);
434 heap_free(args.rgvarg);
438 static HRESULT Function_call(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
439 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
441 FunctionInstance *function;
442 DISPPARAMS args = {NULL,NULL,0,0};
443 IDispatch *this_obj = NULL;
449 if(!is_class(dispex, JSCLASS_FUNCTION)) {
450 FIXME("dispex is not a function\n");
454 function = (FunctionInstance*)dispex;
458 hres = to_object(dispex->ctx, get_arg(dp,0), &this_obj);
465 args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1;
467 hres = call_function(function, this_obj, lcid, &args, retv, ei, caller);
470 IDispatch_Release(this_obj);
474 HRESULT Function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
475 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
477 FunctionInstance *function;
481 if(dispex->builtin_info->class != JSCLASS_FUNCTION) {
482 ERR("dispex is not a function\n");
486 function = (FunctionInstance*)dispex;
489 case DISPATCH_METHOD:
490 if(function->value_proc)
491 return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
493 return invoke_function(function, lcid, dp, retv, ei, caller);
495 case DISPATCH_PROPERTYGET: {
499 hres = function_to_string(function, &str);
503 V_VT(retv) = VT_BSTR;
508 case DISPATCH_CONSTRUCT:
509 if(function->value_proc)
510 return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
512 return invoke_constructor(function, lcid, dp, retv, ei, caller);
515 FIXME("not implemented flags %x\n", flags);
522 static void Function_destructor(DispatchEx *dispex)
524 FunctionInstance *This = (FunctionInstance*)dispex;
527 parser_release(This->parser);
528 if(This->scope_chain)
529 scope_release(This->scope_chain);
533 static const builtin_prop_t Function_props[] = {
534 {applyW, Function_apply, PROPF_METHOD|2},
535 {callW, Function_call, PROPF_METHOD|1},
536 {lengthW, Function_length, 0},
537 {toStringW, Function_toString, PROPF_METHOD}
540 static const builtin_info_t Function_info = {
542 {NULL, Function_value, 0},
543 sizeof(Function_props)/sizeof(*Function_props),
549 static HRESULT FunctionConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
550 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
556 static HRESULT FunctionProt_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
557 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
563 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
564 BOOL funcprot, DispatchEx *prototype, FunctionInstance **ret)
566 FunctionInstance *function;
569 function = heap_alloc_zero(sizeof(FunctionInstance));
571 return E_OUTOFMEMORY;
574 hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
575 else if(builtin_info)
576 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
578 hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
582 function->flags = flags;
583 function->length = flags & PROPF_ARGMASK;
589 static HRESULT set_prototype(script_ctx_t *ctx, DispatchEx *dispex, DispatchEx *prototype)
594 V_VT(&var) = VT_DISPATCH;
595 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
596 memset(&jsexcept, 0, sizeof(jsexcept));
598 return jsdisp_propput_name(dispex, prototypeW, ctx->lcid, &var, &jsexcept, NULL/*FIXME*/);
601 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc,
602 const builtin_info_t *builtin_info, DWORD flags, DispatchEx *prototype, DispatchEx **ret)
604 FunctionInstance *function;
607 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
611 hres = set_prototype(ctx, &function->dispex, prototype);
613 jsdisp_release(&function->dispex);
617 function->value_proc = value_proc;
619 *ret = &function->dispex;
623 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
624 scope_chain_t *scope_chain, const WCHAR *src_str, DWORD src_len, DispatchEx **ret)
626 FunctionInstance *function;
627 DispatchEx *prototype;
632 hres = create_object(ctx->script, NULL, &prototype);
636 hres = create_function(ctx->script, NULL, PROPF_CONSTR, FALSE, NULL, &function);
637 if(SUCCEEDED(hres)) {
638 hres = set_prototype(ctx->script, &function->dispex, prototype);
640 jsdisp_release(&function->dispex);
642 jsdisp_release(prototype);
646 function->source = source;
647 function->parameters = parameters;
650 scope_addref(scope_chain);
651 function->scope_chain = scope_chain;
655 function->parser = ctx;
657 for(iter = parameters; iter; iter = iter->next)
659 function->length = length;
661 function->src_str = src_str;
662 function->src_len = src_len;
664 *ret = &function->dispex;
668 HRESULT init_function_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
670 FunctionInstance *prot, *constr;
673 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
677 prot->value_proc = FunctionProt_value;
679 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, &prot->dispex, &constr);
680 if(SUCCEEDED(hres)) {
681 constr->value_proc = FunctionConstr_value;
682 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
684 jsdisp_release(&constr->dispex);
686 jsdisp_release(&prot->dispex);
690 ctx->function_constr = &constr->dispex;