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 init_arguments(DispatchEx *arg_disp, FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
89 jsexcept_t *ei, IServiceProvider *caller)
95 for(i=0; i < dp->cArgs-dp->cNamedArgs; i++) {
96 hres = jsdisp_propput_idx(arg_disp, i, lcid, dp->rgvarg+dp->cArgs-1-i, ei, caller);
102 V_I4(&var) = dp->cArgs - dp->cNamedArgs;
103 return jsdisp_propput_name(arg_disp, lengthW, lcid, &var, ei, caller);
106 static HRESULT create_var_disp(FunctionInstance *function, LCID lcid, DISPPARAMS *dp, jsexcept_t *ei,
107 IServiceProvider *caller, DispatchEx **ret)
109 DispatchEx *var_disp, *arg_disp;
112 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
114 hres = create_dispex(function->dispex.ctx, NULL, NULL, &var_disp);
118 hres = create_dispex(function->dispex.ctx, NULL, NULL, &arg_disp);
119 if(SUCCEEDED(hres)) {
120 hres = init_arguments(arg_disp, function, lcid, dp, ei, caller);
121 if(SUCCEEDED(hres)) {
124 V_VT(&var) = VT_DISPATCH;
125 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(arg_disp);
126 hres = jsdisp_propput_name(var_disp, argumentsW, lcid, &var, ei, caller);
129 jsdisp_release(arg_disp);
133 hres = init_parameters(var_disp, function, lcid, dp, ei, caller);
135 jsdisp_release(var_disp);
143 static HRESULT invoke_source(FunctionInstance *function, IDispatch *this_obj, LCID lcid, DISPPARAMS *dp,
144 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
146 DispatchEx *var_disp;
147 exec_ctx_t *exec_ctx;
148 scope_chain_t *scope;
151 if(!function->source) {
152 FIXME("no source\n");
156 hres = create_var_disp(function, lcid, dp, ei, caller, &var_disp);
160 hres = scope_push(function->scope_chain, var_disp, &scope);
161 if(SUCCEEDED(hres)) {
162 hres = create_exec_ctx(this_obj, var_disp, scope, &exec_ctx);
163 scope_release(scope);
168 hres = exec_source(exec_ctx, function->parser, function->source, ei, retv);
169 exec_release(exec_ctx);
174 static HRESULT invoke_function(FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
175 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
179 if(!(this_obj = get_this(dp)))
180 this_obj = (IDispatch*)_IDispatchEx_(function->dispex.ctx->script_disp);
182 return invoke_source(function, this_obj, lcid, dp, retv, ei, caller);
185 static HRESULT invoke_constructor(FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
186 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
188 DispatchEx *this_obj;
191 hres = create_object(function->dispex.ctx, &function->dispex, &this_obj);
195 hres = invoke_source(function, (IDispatch*)_IDispatchEx_(this_obj), lcid, dp, retv, ei, caller);
197 jsdisp_release(this_obj);
201 V_VT(retv) = VT_DISPATCH;
202 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(this_obj);
206 static HRESULT invoke_value_proc(FunctionInstance *function, LCID lcid, WORD flags, DISPPARAMS *dp,
207 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
209 DispatchEx *this_obj = NULL;
210 IDispatch *this_disp;
213 this_disp = get_this(dp);
215 this_obj = iface_to_jsdisp((IUnknown*)this_disp);
217 hres = function->value_proc(this_obj ? this_obj : function->dispex.ctx->script_disp, lcid,
218 flags, dp, retv, ei, caller);
221 jsdisp_release(this_obj);
225 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
229 if(function->value_proc) {
230 FIXME("Builtin functions not implemented\n");
234 str = SysAllocStringLen(function->src_str, function->src_len);
236 return E_OUTOFMEMORY;
242 static HRESULT Function_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
243 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
245 FunctionInstance *This = (FunctionInstance*)dispex;
247 TRACE("%p %d\n", This, This->length);
250 case DISPATCH_PROPERTYGET:
252 V_I4(retv) = This->length;
255 FIXME("unimplemented flags %x\n", flags);
262 static HRESULT Function_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
263 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
265 FunctionInstance *function;
271 if(!is_class(dispex, JSCLASS_FUNCTION))
272 return throw_type_error(dispex->ctx, ei, IDS_NOT_FUNC, NULL);
274 function = (FunctionInstance*)dispex;
276 hres = function_to_string(function, &str);
281 V_VT(retv) = VT_BSTR;
289 static HRESULT Function_apply(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
290 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
296 static HRESULT Function_call(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
297 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
303 HRESULT Function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
304 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
306 FunctionInstance *function;
310 if(dispex->builtin_info->class != JSCLASS_FUNCTION) {
311 ERR("dispex is not a function\n");
315 function = (FunctionInstance*)dispex;
318 case DISPATCH_METHOD:
319 if(function->value_proc)
320 return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
322 return invoke_function(function, lcid, dp, retv, ei, caller);
324 case DISPATCH_PROPERTYGET: {
328 hres = function_to_string(function, &str);
332 V_VT(retv) = VT_BSTR;
337 case DISPATCH_CONSTRUCT:
338 if(function->value_proc)
339 return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
341 return invoke_constructor(function, lcid, dp, retv, ei, caller);
344 FIXME("not implemented flags %x\n", flags);
351 static void Function_destructor(DispatchEx *dispex)
353 FunctionInstance *This = (FunctionInstance*)dispex;
356 parser_release(This->parser);
357 if(This->scope_chain)
358 scope_release(This->scope_chain);
362 static const builtin_prop_t Function_props[] = {
363 {applyW, Function_apply, PROPF_METHOD|2},
364 {callW, Function_call, PROPF_METHOD|1},
365 {lengthW, Function_length, 0},
366 {toStringW, Function_toString, PROPF_METHOD}
369 static const builtin_info_t Function_info = {
371 {NULL, Function_value, 0},
372 sizeof(Function_props)/sizeof(*Function_props),
378 static HRESULT FunctionConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
379 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
385 static HRESULT FunctionProt_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
386 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
392 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
393 BOOL funcprot, DispatchEx *prototype, FunctionInstance **ret)
395 FunctionInstance *function;
398 function = heap_alloc_zero(sizeof(FunctionInstance));
400 return E_OUTOFMEMORY;
403 hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
404 else if(builtin_info)
405 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
407 hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
411 function->flags = flags;
412 function->length = flags & PROPF_ARGMASK;
418 static HRESULT set_prototype(script_ctx_t *ctx, DispatchEx *dispex, DispatchEx *prototype)
423 V_VT(&var) = VT_DISPATCH;
424 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
425 memset(&jsexcept, 0, sizeof(jsexcept));
427 return jsdisp_propput_name(dispex, prototypeW, ctx->lcid, &var, &jsexcept, NULL/*FIXME*/);
430 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc,
431 const builtin_info_t *builtin_info, DWORD flags, DispatchEx *prototype, DispatchEx **ret)
433 FunctionInstance *function;
436 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
440 hres = set_prototype(ctx, &function->dispex, prototype);
442 jsdisp_release(&function->dispex);
446 function->value_proc = value_proc;
448 *ret = &function->dispex;
452 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
453 scope_chain_t *scope_chain, const WCHAR *src_str, DWORD src_len, DispatchEx **ret)
455 FunctionInstance *function;
456 DispatchEx *prototype;
461 hres = create_object(ctx->script, NULL, &prototype);
465 hres = create_function(ctx->script, NULL, PROPF_CONSTR, FALSE, NULL, &function);
466 if(SUCCEEDED(hres)) {
467 hres = set_prototype(ctx->script, &function->dispex, prototype);
469 jsdisp_release(&function->dispex);
471 jsdisp_release(prototype);
475 function->source = source;
476 function->parameters = parameters;
479 scope_addref(scope_chain);
480 function->scope_chain = scope_chain;
484 function->parser = ctx;
486 for(iter = parameters; iter; iter = iter->next)
488 function->length = length;
490 function->src_str = src_str;
491 function->src_len = src_len;
493 *ret = &function->dispex;
497 HRESULT init_function_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
499 FunctionInstance *prot, *constr;
502 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
506 prot->value_proc = FunctionProt_value;
508 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, &prot->dispex, &constr);
509 if(SUCCEEDED(hres)) {
510 constr->value_proc = FunctionConstr_value;
511 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
513 jsdisp_release(&constr->dispex);
515 jsdisp_release(&prot->dispex);
519 ctx->function_constr = &constr->dispex;