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 inline FunctionInstance *function_from_vdisp(vdisp_t *vdisp)
42 return (FunctionInstance*)vdisp->u.jsdisp;
45 static inline FunctionInstance *function_this(vdisp_t *jsthis)
47 return is_vclass(jsthis, JSCLASS_FUNCTION) ? function_from_vdisp(jsthis) : NULL;
50 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
52 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
53 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
54 static const WCHAR applyW[] = {'a','p','p','l','y',0};
55 static const WCHAR callW[] = {'c','a','l','l',0};
57 static IDispatch *get_this(DISPPARAMS *dp)
61 for(i=0; i < dp->cNamedArgs; i++) {
62 if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
63 if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
64 return V_DISPATCH(dp->rgvarg+i);
66 WARN("This is not VT_DISPATCH\n");
71 TRACE("no this passed\n");
75 static HRESULT init_parameters(DispatchEx *var_disp, FunctionInstance *function, DISPPARAMS *dp,
76 jsexcept_t *ei, IServiceProvider *caller)
83 V_VT(&var_empty) = VT_EMPTY;
86 for(param = function->parameters; param; param = param->next) {
87 hres = jsdisp_propput_name(var_disp, param->identifier,
88 i < cargs ? get_arg(dp,i) : &var_empty, ei, caller);
98 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
99 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
105 static const builtin_info_t Arguments_info = {
107 {NULL, Arguments_value, 0},
113 static HRESULT create_arguments(script_ctx_t *ctx, IDispatch *calee, DISPPARAMS *dp,
114 jsexcept_t *ei, IServiceProvider *caller, DispatchEx **ret)
121 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
123 args = heap_alloc_zero(sizeof(DispatchEx));
125 return E_OUTOFMEMORY;
127 hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
133 for(i=0; i < arg_cnt(dp); i++) {
134 hres = jsdisp_propput_idx(args, i, get_arg(dp,i), ei, caller);
139 if(SUCCEEDED(hres)) {
141 V_I4(&var) = arg_cnt(dp);
142 hres = jsdisp_propput_name(args, lengthW, &var, ei, caller);
144 if(SUCCEEDED(hres)) {
145 V_VT(&var) = VT_DISPATCH;
146 V_DISPATCH(&var) = calee;
147 hres = jsdisp_propput_name(args, caleeW, &var, ei, caller);
152 jsdisp_release(args);
160 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp, jsexcept_t *ei,
161 IServiceProvider *caller, DispatchEx **ret)
163 DispatchEx *var_disp, *arg_disp;
166 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
168 hres = create_dispex(ctx, NULL, NULL, &var_disp);
172 hres = create_arguments(ctx, (IDispatch*)_IDispatchEx_(&function->dispex),
173 dp, ei, caller, &arg_disp);
174 if(SUCCEEDED(hres)) {
177 V_VT(&var) = VT_DISPATCH;
178 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(arg_disp);
179 hres = jsdisp_propput_name(var_disp, argumentsW, &var, ei, caller);
180 jsdisp_release(arg_disp);
184 hres = init_parameters(var_disp, function, dp, ei, caller);
186 jsdisp_release(var_disp);
194 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *dp,
195 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
197 DispatchEx *var_disp;
198 exec_ctx_t *exec_ctx;
199 scope_chain_t *scope;
202 if(!function->source) {
203 FIXME("no source\n");
207 hres = create_var_disp(ctx, function, dp, ei, caller, &var_disp);
211 hres = scope_push(function->scope_chain, var_disp, &scope);
212 if(SUCCEEDED(hres)) {
213 hres = create_exec_ctx(this_obj, var_disp, scope, &exec_ctx);
214 scope_release(scope);
219 hres = exec_source(exec_ctx, function->parser, function->source, ei, retv);
220 exec_release(exec_ctx);
225 static HRESULT invoke_function(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp,
226 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
230 if(!(this_obj = get_this(dp)))
231 this_obj = (IDispatch*)_IDispatchEx_(ctx->script_disp);
233 return invoke_source(ctx, function, this_obj, dp, retv, ei, caller);
236 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp,
237 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
239 DispatchEx *this_obj;
242 hres = create_object(ctx, &function->dispex, &this_obj);
246 hres = invoke_source(ctx, function, (IDispatch*)_IDispatchEx_(this_obj), dp, retv, ei, caller);
248 jsdisp_release(this_obj);
252 V_VT(retv) = VT_DISPATCH;
253 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(this_obj);
257 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, WORD flags, DISPPARAMS *dp,
258 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
260 IDispatch *this_disp;
264 this_disp = get_this(dp);
266 set_disp(&vthis, this_disp);
268 set_jsdisp(&vthis, ctx->script_disp);
270 hres = function->value_proc(ctx, &vthis, flags, dp, retv, ei, caller);
272 vdisp_release(&vthis);
276 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *args,
277 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
281 if(function->value_proc) {
285 set_disp(&vthis, this_obj);
287 set_jsdisp(&vthis, ctx->script_disp);
289 hres = function->value_proc(ctx, &vthis, DISPATCH_METHOD, args, retv, ei, caller);
290 vdisp_release(&vthis);
292 hres = invoke_source(ctx, function, this_obj ? this_obj : (IDispatch*)_IDispatchEx_(ctx->script_disp),
293 args, retv, ei, caller);
299 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
303 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
304 static const WCHAR native_suffixW[] =
305 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
307 if(function->value_proc) {
310 name_len = strlenW(function->name);
311 str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
313 return E_OUTOFMEMORY;
315 memcpy(str, native_prefixW, sizeof(native_prefixW));
316 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
317 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
319 str = SysAllocStringLen(function->src_str, function->src_len);
321 return E_OUTOFMEMORY;
328 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
329 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
331 FunctionInstance *This = function_from_vdisp(jsthis);
333 TRACE("%p %d\n", This, This->length);
336 case DISPATCH_PROPERTYGET:
338 V_I4(retv) = This->length;
341 FIXME("unimplemented flags %x\n", flags);
348 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
349 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
351 FunctionInstance *function;
357 if(!(function = function_this(jsthis)))
358 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
360 hres = function_to_string(function, &str);
365 V_VT(retv) = VT_BSTR;
373 static HRESULT array_to_args(script_ctx_t *ctx, DispatchEx *arg_array, jsexcept_t *ei, IServiceProvider *caller,
380 hres = jsdisp_propget_name(arg_array, lengthW, &var, ei, NULL/*FIXME*/);
384 hres = to_uint32(ctx, &var, ei, &length);
389 argv = heap_alloc(length * sizeof(VARIANT));
391 return E_OUTOFMEMORY;
393 for(i=0; i<length; i++) {
394 hres = jsdisp_propget_idx(arg_array, i, argv+i, ei, caller);
397 VariantClear(argv+i);
403 args->cArgs = length;
408 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
409 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
411 FunctionInstance *function;
412 DISPPARAMS args = {NULL,NULL,0,0};
414 IDispatch *this_obj = NULL;
419 if(!(function = function_from_vdisp(jsthis))) {
420 FIXME("dispex is not a function\n");
427 hres = to_object(ctx, get_arg(dp,0), &this_obj);
433 DispatchEx *arg_array = NULL;
435 if(V_VT(get_arg(dp,1)) == VT_DISPATCH) {
436 arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(get_arg(dp,1)));
438 !is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
439 jsdisp_release(arg_array);
445 hres = array_to_args(ctx, arg_array, ei, caller, &args);
446 jsdisp_release(arg_array);
448 FIXME("throw TypeError\n");
453 hres = call_function(ctx, function, this_obj, &args, retv, ei, caller);
456 IDispatch_Release(this_obj);
457 for(i=0; i<args.cArgs; i++)
458 VariantClear(args.rgvarg+i);
459 heap_free(args.rgvarg);
463 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
464 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
466 FunctionInstance *function;
467 DISPPARAMS args = {NULL,NULL,0,0};
468 IDispatch *this_obj = NULL;
474 if(!(function = function_from_vdisp(jsthis))) {
475 FIXME("dispex is not a function\n");
481 hres = to_object(ctx, get_arg(dp,0), &this_obj);
488 args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1;
490 hres = call_function(ctx, function, this_obj, &args, retv, ei, caller);
493 IDispatch_Release(this_obj);
497 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
498 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
500 FunctionInstance *function;
504 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
505 ERR("dispex is not a function\n");
509 function = (FunctionInstance*)jsthis->u.jsdisp;
512 case DISPATCH_METHOD:
513 if(function->value_proc)
514 return invoke_value_proc(ctx, function, flags, dp, retv, ei, caller);
516 return invoke_function(ctx, function, dp, retv, ei, caller);
518 case DISPATCH_PROPERTYGET: {
522 hres = function_to_string(function, &str);
526 V_VT(retv) = VT_BSTR;
531 case DISPATCH_CONSTRUCT:
532 if(function->value_proc)
533 return invoke_value_proc(ctx, function, flags, dp, retv, ei, caller);
535 return invoke_constructor(ctx, function, dp, retv, ei, caller);
538 FIXME("not implemented flags %x\n", flags);
545 static void Function_destructor(DispatchEx *dispex)
547 FunctionInstance *This = (FunctionInstance*)dispex;
550 parser_release(This->parser);
551 if(This->scope_chain)
552 scope_release(This->scope_chain);
556 static const builtin_prop_t Function_props[] = {
557 {applyW, Function_apply, PROPF_METHOD|2},
558 {callW, Function_call, PROPF_METHOD|1},
559 {lengthW, Function_length, 0},
560 {toStringW, Function_toString, PROPF_METHOD}
563 static const builtin_info_t Function_info = {
565 {NULL, Function_value, 0},
566 sizeof(Function_props)/sizeof(*Function_props),
572 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
573 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
579 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
580 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
586 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
587 BOOL funcprot, DispatchEx *prototype, FunctionInstance **ret)
589 FunctionInstance *function;
592 function = heap_alloc_zero(sizeof(FunctionInstance));
594 return E_OUTOFMEMORY;
597 hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
598 else if(builtin_info)
599 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
601 hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
605 function->flags = flags;
606 function->length = flags & PROPF_ARGMASK;
612 static HRESULT set_prototype(script_ctx_t *ctx, DispatchEx *dispex, DispatchEx *prototype)
617 V_VT(&var) = VT_DISPATCH;
618 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
619 memset(&jsexcept, 0, sizeof(jsexcept));
621 return jsdisp_propput_name(dispex, prototypeW, &var, &jsexcept, NULL/*FIXME*/);
624 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
625 const builtin_info_t *builtin_info, DWORD flags, DispatchEx *prototype, DispatchEx **ret)
627 FunctionInstance *function;
630 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
634 hres = set_prototype(ctx, &function->dispex, prototype);
636 jsdisp_release(&function->dispex);
640 function->value_proc = value_proc;
641 function->name = name;
643 *ret = &function->dispex;
647 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
648 scope_chain_t *scope_chain, const WCHAR *src_str, DWORD src_len, DispatchEx **ret)
650 FunctionInstance *function;
651 DispatchEx *prototype;
656 hres = create_object(ctx->script, NULL, &prototype);
660 hres = create_function(ctx->script, NULL, PROPF_CONSTR, FALSE, NULL, &function);
661 if(SUCCEEDED(hres)) {
662 hres = set_prototype(ctx->script, &function->dispex, prototype);
664 jsdisp_release(&function->dispex);
666 jsdisp_release(prototype);
670 function->source = source;
671 function->parameters = parameters;
674 scope_addref(scope_chain);
675 function->scope_chain = scope_chain;
679 function->parser = ctx;
681 for(iter = parameters; iter; iter = iter->next)
683 function->length = length;
685 function->src_str = src_str;
686 function->src_len = src_len;
688 *ret = &function->dispex;
692 HRESULT init_function_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
694 FunctionInstance *prot, *constr;
697 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
699 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
703 prot->value_proc = FunctionProt_value;
704 prot->name = prototypeW;
706 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, &prot->dispex, &constr);
707 if(SUCCEEDED(hres)) {
708 constr->value_proc = FunctionConstr_value;
709 constr->name = FunctionW;
710 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
712 jsdisp_release(&constr->dispex);
714 jsdisp_release(&prot->dispex);
718 ctx->function_constr = &constr->dispex;