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;
31 source_elements_t *source;
32 parameter_t *parameters;
33 scope_chain_t *scope_chain;
40 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
42 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
43 static const WCHAR toStringW[] = {'t','o','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};
47 static IDispatch *get_this(DISPPARAMS *dp)
51 for(i=0; i < dp->cNamedArgs; i++) {
52 if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
53 if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
54 return V_DISPATCH(dp->rgvarg+i);
56 WARN("This is not VT_DISPATCH\n");
61 TRACE("no this passed\n");
65 static HRESULT init_parameters(DispatchEx *var_disp, FunctionInstance *function, DISPPARAMS *dp,
66 jsexcept_t *ei, IServiceProvider *caller)
73 V_VT(&var_empty) = VT_EMPTY;
76 for(param = function->parameters; param; param = param->next) {
77 hres = jsdisp_propput_name(var_disp, param->identifier,
78 i < cargs ? get_arg(dp,i) : &var_empty, ei, caller);
88 static HRESULT Arguments_value(script_ctx_t *ctx, DispatchEx *dispex, 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, IDispatch *calee, DISPPARAMS *dp,
104 jsexcept_t *ei, IServiceProvider *caller, DispatchEx **ret)
111 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
113 args = heap_alloc_zero(sizeof(DispatchEx));
115 return E_OUTOFMEMORY;
117 hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
123 for(i=0; i < arg_cnt(dp); i++) {
124 hres = jsdisp_propput_idx(args, i, get_arg(dp,i), ei, caller);
129 if(SUCCEEDED(hres)) {
131 V_I4(&var) = arg_cnt(dp);
132 hres = jsdisp_propput_name(args, lengthW, &var, ei, caller);
134 if(SUCCEEDED(hres)) {
135 V_VT(&var) = VT_DISPATCH;
136 V_DISPATCH(&var) = calee;
137 hres = jsdisp_propput_name(args, caleeW, &var, ei, caller);
142 jsdisp_release(args);
150 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp, jsexcept_t *ei,
151 IServiceProvider *caller, DispatchEx **ret)
153 DispatchEx *var_disp, *arg_disp;
156 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
158 hres = create_dispex(ctx, NULL, NULL, &var_disp);
162 hres = create_arguments(ctx, (IDispatch*)_IDispatchEx_(&function->dispex),
163 dp, ei, caller, &arg_disp);
164 if(SUCCEEDED(hres)) {
167 V_VT(&var) = VT_DISPATCH;
168 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(arg_disp);
169 hres = jsdisp_propput_name(var_disp, argumentsW, &var, ei, caller);
170 jsdisp_release(arg_disp);
174 hres = init_parameters(var_disp, function, dp, ei, caller);
176 jsdisp_release(var_disp);
184 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *dp,
185 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
187 DispatchEx *var_disp;
188 exec_ctx_t *exec_ctx;
189 scope_chain_t *scope;
192 if(!function->source) {
193 FIXME("no source\n");
197 hres = create_var_disp(ctx, function, dp, ei, caller, &var_disp);
201 hres = scope_push(function->scope_chain, var_disp, &scope);
202 if(SUCCEEDED(hres)) {
203 hres = create_exec_ctx(this_obj, var_disp, scope, &exec_ctx);
204 scope_release(scope);
209 hres = exec_source(exec_ctx, function->parser, function->source, ei, retv);
210 exec_release(exec_ctx);
215 static HRESULT invoke_function(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp,
216 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
220 if(!(this_obj = get_this(dp)))
221 this_obj = (IDispatch*)_IDispatchEx_(ctx->script_disp);
223 return invoke_source(ctx, function, this_obj, dp, retv, ei, caller);
226 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp,
227 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
229 DispatchEx *this_obj;
232 hres = create_object(ctx, &function->dispex, &this_obj);
236 hres = invoke_source(ctx, function, (IDispatch*)_IDispatchEx_(this_obj), dp, retv, ei, caller);
238 jsdisp_release(this_obj);
242 V_VT(retv) = VT_DISPATCH;
243 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(this_obj);
247 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, WORD flags, DISPPARAMS *dp,
248 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
250 DispatchEx *this_obj = NULL;
251 IDispatch *this_disp;
254 this_disp = get_this(dp);
256 this_obj = iface_to_jsdisp((IUnknown*)this_disp);
258 hres = function->value_proc(ctx, this_obj ? this_obj : ctx->script_disp,
259 flags, dp, retv, ei, caller);
262 jsdisp_release(this_obj);
266 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *args,
267 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
271 if(function->value_proc) {
272 DispatchEx *jsthis = NULL;
275 jsthis = iface_to_jsdisp((IUnknown*)this_obj);
277 FIXME("this_obj is not DispatchEx\n");
280 hres = function->value_proc(ctx, jsthis ? jsthis : ctx->script_disp,
281 DISPATCH_METHOD, args, retv, ei, caller);
284 jsdisp_release(jsthis);
286 hres = invoke_source(ctx, function, this_obj ? this_obj : (IDispatch*)_IDispatchEx_(ctx->script_disp),
287 args, retv, ei, caller);
293 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
297 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
298 static const WCHAR native_suffixW[] =
299 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
301 if(function->value_proc) {
304 name_len = strlenW(function->name);
305 str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
307 return E_OUTOFMEMORY;
309 memcpy(str, native_prefixW, sizeof(native_prefixW));
310 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
311 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
313 str = SysAllocStringLen(function->src_str, function->src_len);
315 return E_OUTOFMEMORY;
322 static HRESULT Function_length(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
323 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
325 FunctionInstance *This = (FunctionInstance*)dispex;
327 TRACE("%p %d\n", This, This->length);
330 case DISPATCH_PROPERTYGET:
332 V_I4(retv) = This->length;
335 FIXME("unimplemented flags %x\n", flags);
342 static HRESULT Function_toString(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
343 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
345 FunctionInstance *function;
351 if(!is_class(dispex, JSCLASS_FUNCTION))
352 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
354 function = (FunctionInstance*)dispex;
356 hres = function_to_string(function, &str);
361 V_VT(retv) = VT_BSTR;
369 static HRESULT array_to_args(script_ctx_t *ctx, DispatchEx *arg_array, jsexcept_t *ei, IServiceProvider *caller,
376 hres = jsdisp_propget_name(arg_array, lengthW, &var, ei, NULL/*FIXME*/);
380 hres = to_uint32(ctx, &var, ei, &length);
385 argv = heap_alloc(length * sizeof(VARIANT));
387 return E_OUTOFMEMORY;
389 for(i=0; i<length; i++) {
390 hres = jsdisp_propget_idx(arg_array, i, argv+i, ei, caller);
393 VariantClear(argv+i);
399 args->cArgs = length;
404 static HRESULT Function_apply(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
405 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
407 FunctionInstance *function;
408 DISPPARAMS args = {NULL,NULL,0,0};
410 IDispatch *this_obj = NULL;
415 if(!is_class(dispex, JSCLASS_FUNCTION)) {
416 FIXME("dispex is not a function\n");
420 function = (FunctionInstance*)dispex;
424 hres = to_object(ctx, get_arg(dp,0), &this_obj);
430 DispatchEx *arg_array = NULL;
432 if(V_VT(get_arg(dp,1)) == VT_DISPATCH) {
433 arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(get_arg(dp,1)));
435 !is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
436 jsdisp_release(arg_array);
442 hres = array_to_args(ctx, arg_array, ei, caller, &args);
443 jsdisp_release(arg_array);
445 FIXME("throw TypeError\n");
450 hres = call_function(ctx, function, this_obj, &args, retv, ei, caller);
453 IDispatch_Release(this_obj);
454 for(i=0; i<args.cArgs; i++)
455 VariantClear(args.rgvarg+i);
456 heap_free(args.rgvarg);
460 static HRESULT Function_call(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
461 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
463 FunctionInstance *function;
464 DISPPARAMS args = {NULL,NULL,0,0};
465 IDispatch *this_obj = NULL;
471 if(!is_class(dispex, JSCLASS_FUNCTION)) {
472 FIXME("dispex is not a function\n");
476 function = (FunctionInstance*)dispex;
480 hres = to_object(ctx, get_arg(dp,0), &this_obj);
487 args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1;
489 hres = call_function(ctx, function, this_obj, &args, retv, ei, caller);
492 IDispatch_Release(this_obj);
496 HRESULT Function_value(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
497 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
499 FunctionInstance *function;
503 if(dispex->builtin_info->class != JSCLASS_FUNCTION) {
504 ERR("dispex is not a function\n");
508 function = (FunctionInstance*)dispex;
511 case DISPATCH_METHOD:
512 if(function->value_proc)
513 return invoke_value_proc(ctx, function, flags, dp, retv, ei, caller);
515 return invoke_function(ctx, function, dp, retv, ei, caller);
517 case DISPATCH_PROPERTYGET: {
521 hres = function_to_string(function, &str);
525 V_VT(retv) = VT_BSTR;
530 case DISPATCH_CONSTRUCT:
531 if(function->value_proc)
532 return invoke_value_proc(ctx, function, flags, dp, retv, ei, caller);
534 return invoke_constructor(ctx, function, dp, retv, ei, caller);
537 FIXME("not implemented flags %x\n", flags);
544 static void Function_destructor(DispatchEx *dispex)
546 FunctionInstance *This = (FunctionInstance*)dispex;
549 parser_release(This->parser);
550 if(This->scope_chain)
551 scope_release(This->scope_chain);
555 static const builtin_prop_t Function_props[] = {
556 {applyW, Function_apply, PROPF_METHOD|2},
557 {callW, Function_call, PROPF_METHOD|1},
558 {lengthW, Function_length, 0},
559 {toStringW, Function_toString, PROPF_METHOD}
562 static const builtin_info_t Function_info = {
564 {NULL, Function_value, 0},
565 sizeof(Function_props)/sizeof(*Function_props),
571 static HRESULT FunctionConstr_value(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
572 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
578 static HRESULT FunctionProt_value(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
579 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
585 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
586 BOOL funcprot, DispatchEx *prototype, FunctionInstance **ret)
588 FunctionInstance *function;
591 function = heap_alloc_zero(sizeof(FunctionInstance));
593 return E_OUTOFMEMORY;
596 hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
597 else if(builtin_info)
598 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
600 hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
604 function->flags = flags;
605 function->length = flags & PROPF_ARGMASK;
611 static HRESULT set_prototype(script_ctx_t *ctx, DispatchEx *dispex, DispatchEx *prototype)
616 V_VT(&var) = VT_DISPATCH;
617 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
618 memset(&jsexcept, 0, sizeof(jsexcept));
620 return jsdisp_propput_name(dispex, prototypeW, &var, &jsexcept, NULL/*FIXME*/);
623 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
624 const builtin_info_t *builtin_info, DWORD flags, DispatchEx *prototype, DispatchEx **ret)
626 FunctionInstance *function;
629 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
633 hres = set_prototype(ctx, &function->dispex, prototype);
635 jsdisp_release(&function->dispex);
639 function->value_proc = value_proc;
640 function->name = name;
642 *ret = &function->dispex;
646 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
647 scope_chain_t *scope_chain, const WCHAR *src_str, DWORD src_len, DispatchEx **ret)
649 FunctionInstance *function;
650 DispatchEx *prototype;
655 hres = create_object(ctx->script, NULL, &prototype);
659 hres = create_function(ctx->script, NULL, PROPF_CONSTR, FALSE, NULL, &function);
660 if(SUCCEEDED(hres)) {
661 hres = set_prototype(ctx->script, &function->dispex, prototype);
663 jsdisp_release(&function->dispex);
665 jsdisp_release(prototype);
669 function->source = source;
670 function->parameters = parameters;
673 scope_addref(scope_chain);
674 function->scope_chain = scope_chain;
678 function->parser = ctx;
680 for(iter = parameters; iter; iter = iter->next)
682 function->length = length;
684 function->src_str = src_str;
685 function->src_len = src_len;
687 *ret = &function->dispex;
691 HRESULT init_function_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
693 FunctionInstance *prot, *constr;
696 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
698 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
702 prot->value_proc = FunctionProt_value;
703 prot->name = prototypeW;
705 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, &prot->dispex, &constr);
706 if(SUCCEEDED(hres)) {
707 constr->value_proc = FunctionConstr_value;
708 constr->name = FunctionW;
709 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
711 jsdisp_release(&constr->dispex);
713 jsdisp_release(&prot->dispex);
717 ctx->function_constr = &constr->dispex;