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;
38 DispatchEx *arguments;
41 static inline FunctionInstance *function_from_vdisp(vdisp_t *vdisp)
43 return (FunctionInstance*)vdisp->u.jsdisp;
46 static inline FunctionInstance *function_this(vdisp_t *jsthis)
48 return is_vclass(jsthis, JSCLASS_FUNCTION) ? function_from_vdisp(jsthis) : NULL;
51 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
53 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
54 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
55 static const WCHAR applyW[] = {'a','p','p','l','y',0};
56 static const WCHAR callW[] = {'c','a','l','l',0};
57 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
59 static IDispatch *get_this(DISPPARAMS *dp)
63 for(i=0; i < dp->cNamedArgs; i++) {
64 if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
65 if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
66 return V_DISPATCH(dp->rgvarg+i);
68 WARN("This is not VT_DISPATCH\n");
73 TRACE("no this passed\n");
77 static HRESULT init_parameters(DispatchEx *var_disp, FunctionInstance *function, DISPPARAMS *dp,
78 jsexcept_t *ei, IServiceProvider *caller)
85 V_VT(&var_empty) = VT_EMPTY;
88 for(param = function->parameters; param; param = param->next) {
89 hres = jsdisp_propput_name(var_disp, param->identifier,
90 i < cargs ? get_arg(dp,i) : &var_empty, ei, caller);
100 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
101 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
107 static const builtin_info_t Arguments_info = {
109 {NULL, Arguments_value, 0},
115 static HRESULT create_arguments(script_ctx_t *ctx, IDispatch *calee, DISPPARAMS *dp,
116 jsexcept_t *ei, IServiceProvider *caller, DispatchEx **ret)
123 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
125 args = heap_alloc_zero(sizeof(DispatchEx));
127 return E_OUTOFMEMORY;
129 hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
135 for(i=0; i < arg_cnt(dp); i++) {
136 hres = jsdisp_propput_idx(args, i, get_arg(dp,i), ei, caller);
141 if(SUCCEEDED(hres)) {
143 V_I4(&var) = arg_cnt(dp);
144 hres = jsdisp_propput_name(args, lengthW, &var, ei, caller);
146 if(SUCCEEDED(hres)) {
147 V_VT(&var) = VT_DISPATCH;
148 V_DISPATCH(&var) = calee;
149 hres = jsdisp_propput_name(args, caleeW, &var, ei, caller);
154 jsdisp_release(args);
162 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, DispatchEx *arg_disp,
163 DISPPARAMS *dp, jsexcept_t *ei, IServiceProvider *caller, DispatchEx **ret)
165 DispatchEx *var_disp;
169 hres = create_dispex(ctx, NULL, NULL, &var_disp);
173 V_VT(&var) = VT_DISPATCH;
174 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(arg_disp);
175 hres = jsdisp_propput_name(var_disp, argumentsW, &var, ei, caller);
177 hres = init_parameters(var_disp, function, dp, ei, caller);
179 jsdisp_release(var_disp);
187 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *dp,
188 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
190 DispatchEx *var_disp, *arg_disp;
191 exec_ctx_t *exec_ctx;
192 scope_chain_t *scope;
195 if(!function->source) {
196 FIXME("no source\n");
200 hres = create_arguments(ctx, (IDispatch*)_IDispatchEx_(&function->dispex),
201 dp, ei, caller, &arg_disp);
205 hres = create_var_disp(ctx, function, arg_disp, dp, ei, caller, &var_disp);
207 jsdisp_release(arg_disp);
211 hres = scope_push(function->scope_chain, var_disp, &scope);
212 if(SUCCEEDED(hres)) {
213 hres = create_exec_ctx(ctx, this_obj, var_disp, scope, &exec_ctx);
214 scope_release(scope);
216 jsdisp_release(var_disp);
217 if(SUCCEEDED(hres)) {
218 DispatchEx *prev_args;
220 prev_args = function->arguments;
221 function->arguments = arg_disp;
222 hres = exec_source(exec_ctx, function->parser, function->source, EXECT_FUNCTION, ei, retv);
223 function->arguments = prev_args;
225 jsdisp_release(arg_disp);
226 exec_release(exec_ctx);
232 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp,
233 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
235 DispatchEx *this_obj;
239 hres = create_object(ctx, &function->dispex, &this_obj);
243 hres = invoke_source(ctx, function, (IDispatch*)_IDispatchEx_(this_obj), dp, &var, ei, caller);
245 jsdisp_release(this_obj);
249 V_VT(retv) = VT_DISPATCH;
250 if(V_VT(&var) == VT_DISPATCH) {
251 jsdisp_release(this_obj);
252 V_DISPATCH(retv) = V_DISPATCH(&var);
255 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(this_obj);
260 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags, DISPPARAMS *dp,
261 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
267 set_disp(&vthis, this_disp);
268 else if(ctx->host_global)
269 set_disp(&vthis, ctx->host_global);
271 set_jsdisp(&vthis, ctx->global);
273 hres = function->value_proc(ctx, &vthis, flags, dp, retv, ei, caller);
275 vdisp_release(&vthis);
279 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *args,
280 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
282 if(function->value_proc)
283 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, args, retv, ei, caller);
285 return invoke_source(ctx, function, this_obj, args, retv, ei, caller);
288 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
292 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
293 static const WCHAR native_suffixW[] =
294 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
296 if(function->value_proc) {
299 name_len = strlenW(function->name);
300 str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
302 return E_OUTOFMEMORY;
304 memcpy(str, native_prefixW, sizeof(native_prefixW));
305 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
306 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
308 str = SysAllocStringLen(function->src_str, function->src_len);
310 return E_OUTOFMEMORY;
317 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
318 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
320 FunctionInstance *This = function_from_vdisp(jsthis);
322 TRACE("%p %d\n", This, This->length);
325 case DISPATCH_PROPERTYGET:
327 V_I4(retv) = This->length;
330 FIXME("unimplemented flags %x\n", flags);
337 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
338 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
340 FunctionInstance *function;
346 if(!(function = function_this(jsthis)))
347 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
349 hres = function_to_string(function, &str);
354 V_VT(retv) = VT_BSTR;
362 static HRESULT array_to_args(script_ctx_t *ctx, DispatchEx *arg_array, jsexcept_t *ei, IServiceProvider *caller,
369 hres = jsdisp_propget_name(arg_array, lengthW, &var, ei, NULL/*FIXME*/);
373 hres = to_uint32(ctx, &var, ei, &length);
378 argv = heap_alloc(length * sizeof(VARIANT));
380 return E_OUTOFMEMORY;
382 for(i=0; i<length; i++) {
383 hres = jsdisp_get_idx(arg_array, i, argv+i, ei, caller);
384 if(hres == DISP_E_UNKNOWNNAME)
385 V_VT(argv+i) = VT_EMPTY;
386 else if(FAILED(hres)) {
388 VariantClear(argv+i);
394 args->cArgs = length;
399 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
400 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
402 FunctionInstance *function;
403 DISPPARAMS args = {NULL,NULL,0,0};
405 IDispatch *this_obj = NULL;
410 if(!(function = function_this(jsthis)))
411 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
415 VARIANT *v = get_arg(dp,0);
417 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
418 hres = to_object(ctx, v, &this_obj);
425 DispatchEx *arg_array = NULL;
427 if(V_VT(get_arg(dp,1)) == VT_DISPATCH) {
428 arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(get_arg(dp,1)));
430 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
431 jsdisp_release(arg_array);
437 hres = array_to_args(ctx, arg_array, ei, caller, &args);
438 jsdisp_release(arg_array);
440 FIXME("throw TypeError\n");
445 hres = call_function(ctx, function, this_obj, &args, retv, ei, caller);
448 IDispatch_Release(this_obj);
449 for(i=0; i<args.cArgs; i++)
450 VariantClear(args.rgvarg+i);
451 heap_free(args.rgvarg);
455 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
456 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
458 FunctionInstance *function;
459 DISPPARAMS args = {NULL,NULL,0,0};
460 IDispatch *this_obj = NULL;
466 if(!(function = function_this(jsthis)))
467 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
471 VARIANT *v = get_arg(dp,0);
473 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
474 hres = to_object(ctx, v, &this_obj);
483 args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1;
485 hres = call_function(ctx, function, this_obj, &args, retv, ei, caller);
488 IDispatch_Release(this_obj);
492 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
493 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
495 FunctionInstance *function;
499 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
500 ERR("dispex is not a function\n");
504 function = (FunctionInstance*)jsthis->u.jsdisp;
507 case DISPATCH_METHOD:
508 if(function->value_proc)
509 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei, caller);
511 return invoke_source(ctx, function, get_this(dp), dp, retv, ei, caller);
513 case DISPATCH_PROPERTYGET: {
517 hres = function_to_string(function, &str);
521 V_VT(retv) = VT_BSTR;
526 case DISPATCH_CONSTRUCT:
527 if(function->value_proc)
528 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei, caller);
530 return invoke_constructor(ctx, function, dp, retv, ei, caller);
533 FIXME("not implemented flags %x\n", flags);
540 static HRESULT Function_arguments(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
541 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
543 FunctionInstance *function = (FunctionInstance*)jsthis->u.jsdisp;
549 case DISPATCH_PROPERTYGET: {
550 if(function->arguments) {
551 IDispatchEx_AddRef(_IDispatchEx_(function->arguments));
552 V_VT(retv) = VT_DISPATCH;
553 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(function->arguments);
555 V_VT(retv) = VT_NULL;
559 case DISPATCH_PROPERTYPUT:
562 FIXME("unimplemented flags %x\n", flags);
569 static void Function_destructor(DispatchEx *dispex)
571 FunctionInstance *This = (FunctionInstance*)dispex;
574 parser_release(This->parser);
575 if(This->scope_chain)
576 scope_release(This->scope_chain);
580 static const builtin_prop_t Function_props[] = {
581 {applyW, Function_apply, PROPF_METHOD|2},
582 {argumentsW, Function_arguments, 0},
583 {callW, Function_call, PROPF_METHOD|1},
584 {lengthW, Function_length, 0},
585 {toStringW, Function_toString, PROPF_METHOD}
588 static const builtin_info_t Function_info = {
590 {NULL, Function_value, 0},
591 sizeof(Function_props)/sizeof(*Function_props),
597 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
598 BOOL funcprot, DispatchEx *prototype, FunctionInstance **ret)
600 FunctionInstance *function;
603 function = heap_alloc_zero(sizeof(FunctionInstance));
605 return E_OUTOFMEMORY;
608 hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
609 else if(builtin_info)
610 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
612 hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
616 function->flags = flags;
617 function->length = flags & PROPF_ARGMASK;
623 static HRESULT set_prototype(script_ctx_t *ctx, DispatchEx *dispex, DispatchEx *prototype)
628 V_VT(&var) = VT_DISPATCH;
629 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
630 memset(&jsexcept, 0, sizeof(jsexcept));
632 return jsdisp_propput_name(dispex, prototypeW, &var, &jsexcept, NULL/*FIXME*/);
635 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
636 const builtin_info_t *builtin_info, DWORD flags, DispatchEx *prototype, DispatchEx **ret)
638 FunctionInstance *function;
641 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
649 V_I4(&var) = function->length;
650 hres = jsdisp_propput_const(&function->dispex, lengthW, &var);
654 hres = set_prototype(ctx, &function->dispex, prototype);
656 jsdisp_release(&function->dispex);
660 function->value_proc = value_proc;
661 function->name = name;
663 *ret = &function->dispex;
667 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
668 scope_chain_t *scope_chain, const WCHAR *src_str, DWORD src_len, DispatchEx **ret)
670 FunctionInstance *function;
671 DispatchEx *prototype;
676 hres = create_object(ctx->script, NULL, &prototype);
680 hres = create_function(ctx->script, NULL, PROPF_CONSTR, FALSE, NULL, &function);
681 if(SUCCEEDED(hres)) {
682 hres = set_prototype(ctx->script, &function->dispex, prototype);
684 jsdisp_release(&function->dispex);
686 jsdisp_release(prototype);
690 function->source = source;
691 function->parameters = parameters;
694 scope_addref(scope_chain);
695 function->scope_chain = scope_chain;
699 function->parser = ctx;
701 for(iter = parameters; iter; iter = iter->next)
703 function->length = length;
705 function->src_str = src_str;
706 function->src_len = src_len;
708 *ret = &function->dispex;
712 static HRESULT construct_function(script_ctx_t *ctx, DISPPARAMS *dp, jsexcept_t *ei, IDispatch **ret)
714 function_expression_t *expr;
715 WCHAR *str = NULL, *ptr;
716 DWORD argc, len = 0, l;
717 parser_ctx_t *parser;
718 DispatchEx *function;
723 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
724 static const WCHAR function_beginW[] = {')',' ','{','\n'};
725 static const WCHAR function_endW[] = {'\n','}',0};
729 params = heap_alloc(argc*sizeof(BSTR));
731 return E_OUTOFMEMORY;
734 len = (argc-2)*2; /* separating commas */
735 for(i=0; i < argc; i++) {
736 hres = to_string(ctx, get_arg(dp,i), ei, params+i);
739 len += SysStringLen(params[i]);
743 if(SUCCEEDED(hres)) {
744 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
745 str = heap_alloc(len*sizeof(WCHAR));
747 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
748 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
751 l = SysStringLen(params[j]);
752 memcpy(ptr, params[j], l*sizeof(WCHAR));
760 memcpy(ptr, function_beginW, sizeof(function_beginW));
761 ptr += sizeof(function_beginW)/sizeof(WCHAR);
763 l = SysStringLen(params[argc-1]);
764 memcpy(ptr, params[argc-1], l*sizeof(WCHAR));
767 memcpy(ptr, function_endW, sizeof(function_endW));
769 TRACE("%s\n", debugstr_w(str));
771 hres = E_OUTOFMEMORY;
776 SysFreeString(params[i]);
781 hres = script_parse(ctx, str, NULL, &parser);
786 if(!parser->source || !parser->source->functions || parser->source->functions->next || parser->source->variables) {
787 ERR("Invalid parser result!\n");
788 parser_release(parser);
791 expr = parser->source->functions->expr;
793 hres = create_source_function(parser, expr->parameter_list, expr->source_elements, NULL, expr->src_str,
794 expr->src_len, &function);
795 parser_release(parser);
799 *ret = (IDispatch*)_IDispatchEx_(function);
803 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
804 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
811 case DISPATCH_CONSTRUCT: {
814 hres = construct_function(ctx, dp, ei, &ret);
818 V_VT(retv) = VT_DISPATCH;
819 V_DISPATCH(retv) = ret;
823 FIXME("unimplemented flags %x\n", flags);
830 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
831 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
837 HRESULT init_function_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
839 FunctionInstance *prot, *constr;
842 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
844 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
848 prot->value_proc = FunctionProt_value;
849 prot->name = prototypeW;
851 hres = create_function(ctx, NULL, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
852 if(SUCCEEDED(hres)) {
853 constr->value_proc = FunctionConstr_value;
854 constr->name = FunctionW;
855 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
857 jsdisp_release(&constr->dispex);
859 jsdisp_release(&prot->dispex);
863 ctx->function_constr = &constr->dispex;