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 scope_chain_t *scope_chain;
33 function_code_t *func_code;
38 static inline FunctionInstance *function_from_vdisp(vdisp_t *vdisp)
40 return (FunctionInstance*)vdisp->u.jsdisp;
43 static inline FunctionInstance *function_this(vdisp_t *jsthis)
45 return is_vclass(jsthis, JSCLASS_FUNCTION) ? function_from_vdisp(jsthis) : NULL;
48 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
50 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
51 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
52 static const WCHAR applyW[] = {'a','p','p','l','y',0};
53 static const WCHAR callW[] = {'c','a','l','l',0};
54 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
56 static IDispatch *get_this(DISPPARAMS *dp)
60 for(i=0; i < dp->cNamedArgs; i++) {
61 if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
62 if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
63 return V_DISPATCH(dp->rgvarg+i);
65 WARN("This is not VT_DISPATCH\n");
70 TRACE("no this passed\n");
74 static HRESULT init_parameters(jsdisp_t *var_disp, FunctionInstance *function, DISPPARAMS *dp,
81 V_VT(&var_empty) = VT_EMPTY;
84 for(i=0; i < function->func_code->param_cnt; i++) {
85 hres = jsdisp_propput_name(var_disp, function->func_code->params[i],
86 i < cargs ? get_arg(dp,i) : &var_empty, ei);
94 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
95 VARIANT *retv, jsexcept_t *ei)
101 static const builtin_info_t Arguments_info = {
103 {NULL, Arguments_value, 0},
109 static HRESULT create_arguments(script_ctx_t *ctx, IDispatch *calee, DISPPARAMS *dp,
110 jsexcept_t *ei, jsdisp_t **ret)
117 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
119 args = heap_alloc_zero(sizeof(jsdisp_t));
121 return E_OUTOFMEMORY;
123 hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
129 for(i=0; i < arg_cnt(dp); i++) {
130 hres = jsdisp_propput_idx(args, i, get_arg(dp,i), ei);
135 if(SUCCEEDED(hres)) {
136 num_set_int(&var, arg_cnt(dp));
137 hres = jsdisp_propput_name(args, lengthW, &var, ei);
139 if(SUCCEEDED(hres)) {
140 V_VT(&var) = VT_DISPATCH;
141 V_DISPATCH(&var) = calee;
142 hres = jsdisp_propput_name(args, caleeW, &var, ei);
147 jsdisp_release(args);
155 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, jsdisp_t *arg_disp,
156 DISPPARAMS *dp, jsexcept_t *ei, jsdisp_t **ret)
162 hres = create_dispex(ctx, NULL, NULL, &var_disp);
166 var_set_jsdisp(&var, arg_disp);
167 hres = jsdisp_propput_name(var_disp, argumentsW, &var, ei);
169 hres = init_parameters(var_disp, function, dp, ei);
171 jsdisp_release(var_disp);
179 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *dp,
180 VARIANT *retv, jsexcept_t *ei)
182 jsdisp_t *var_disp, *arg_disp;
183 exec_ctx_t *exec_ctx;
184 scope_chain_t *scope;
187 if(!function->func_code) {
188 FIXME("no source\n");
192 hres = create_arguments(ctx, to_disp(&function->dispex), dp, ei, &arg_disp);
196 hres = create_var_disp(ctx, function, arg_disp, dp, ei, &var_disp);
198 jsdisp_release(arg_disp);
202 hres = scope_push(function->scope_chain, var_disp, &scope);
203 if(SUCCEEDED(hres)) {
204 hres = create_exec_ctx(ctx, this_obj, var_disp, scope, FALSE, &exec_ctx);
205 scope_release(scope);
207 jsdisp_release(var_disp);
208 if(SUCCEEDED(hres)) {
211 prev_args = function->arguments;
212 function->arguments = arg_disp;
213 hres = exec_source(exec_ctx, function->code, function->func_code, FALSE, ei, retv);
214 function->arguments = prev_args;
216 jsdisp_release(arg_disp);
217 exec_release(exec_ctx);
223 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp,
224 VARIANT *retv, jsexcept_t *ei)
230 hres = create_object(ctx, &function->dispex, &this_obj);
234 hres = invoke_source(ctx, function, to_disp(this_obj), dp, &var, ei);
236 jsdisp_release(this_obj);
240 if(V_VT(&var) == VT_DISPATCH) {
241 jsdisp_release(this_obj);
242 V_VT(retv) = VT_DISPATCH;
243 V_DISPATCH(retv) = V_DISPATCH(&var);
246 var_set_jsdisp(retv, this_obj);
251 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags, DISPPARAMS *dp,
252 VARIANT *retv, jsexcept_t *ei)
258 set_disp(&vthis, this_disp);
259 else if(ctx->host_global)
260 set_disp(&vthis, ctx->host_global);
262 set_jsdisp(&vthis, ctx->global);
264 hres = function->value_proc(ctx, &vthis, flags, dp, retv, ei);
266 vdisp_release(&vthis);
270 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *args,
271 VARIANT *retv, jsexcept_t *ei)
273 if(function->value_proc)
274 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, args, retv, ei);
276 return invoke_source(ctx, function, this_obj, args, retv, ei);
279 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
283 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
284 static const WCHAR native_suffixW[] =
285 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
287 if(function->value_proc) {
290 name_len = strlenW(function->name);
291 str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
293 return E_OUTOFMEMORY;
295 memcpy(str, native_prefixW, sizeof(native_prefixW));
296 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
297 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
299 str = SysAllocStringLen(function->func_code->source, function->func_code->source_len);
301 return E_OUTOFMEMORY;
308 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
309 VARIANT *retv, jsexcept_t *ei)
311 FunctionInstance *This = function_from_vdisp(jsthis);
313 TRACE("%p %d\n", This, This->length);
316 case DISPATCH_PROPERTYGET:
317 num_set_int(retv, This->length);
320 FIXME("unimplemented flags %x\n", flags);
327 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
328 VARIANT *retv, jsexcept_t *ei)
330 FunctionInstance *function;
336 if(!(function = function_this(jsthis)))
337 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
339 hres = function_to_string(function, &str);
344 V_VT(retv) = VT_BSTR;
352 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, jsexcept_t *ei, DISPPARAMS *args)
358 hres = jsdisp_propget_name(arg_array, lengthW, &var, ei);
362 hres = to_uint32(ctx, &var, ei, &length);
367 argv = heap_alloc(length * sizeof(VARIANT));
369 return E_OUTOFMEMORY;
371 for(i=0; i<length; i++) {
372 hres = jsdisp_get_idx(arg_array, i, argv+i, ei);
373 if(hres == DISP_E_UNKNOWNNAME)
374 V_VT(argv+i) = VT_EMPTY;
375 else if(FAILED(hres)) {
377 VariantClear(argv+i);
383 args->cArgs = length;
388 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
389 VARIANT *retv, jsexcept_t *ei)
391 FunctionInstance *function;
392 DISPPARAMS args = {NULL,NULL,0,0};
394 IDispatch *this_obj = NULL;
399 if(!(function = function_this(jsthis)))
400 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
404 VARIANT *v = get_arg(dp,0);
406 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
407 hres = to_object(ctx, v, &this_obj);
414 jsdisp_t *arg_array = NULL;
416 if(V_VT(get_arg(dp,1)) == VT_DISPATCH) {
417 arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(get_arg(dp,1)));
419 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
420 jsdisp_release(arg_array);
426 hres = array_to_args(ctx, arg_array, ei, &args);
427 jsdisp_release(arg_array);
429 FIXME("throw TypeError\n");
435 hres = call_function(ctx, function, this_obj, &args, retv, ei);
438 IDispatch_Release(this_obj);
439 for(i=0; i<args.cArgs; i++)
440 VariantClear(args.rgvarg+i);
441 heap_free(args.rgvarg);
445 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
446 VARIANT *retv, jsexcept_t *ei)
448 FunctionInstance *function;
449 DISPPARAMS args = {NULL,NULL,0,0};
450 IDispatch *this_obj = NULL;
456 if(!(function = function_this(jsthis)))
457 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
461 VARIANT *v = get_arg(dp,0);
463 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
464 hres = to_object(ctx, v, &this_obj);
473 args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1;
475 hres = call_function(ctx, function, this_obj, &args, retv, ei);
478 IDispatch_Release(this_obj);
482 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
483 VARIANT *retv, jsexcept_t *ei)
485 FunctionInstance *function;
489 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
490 ERR("dispex is not a function\n");
494 function = (FunctionInstance*)jsthis->u.jsdisp;
497 case DISPATCH_METHOD:
498 if(function->value_proc)
499 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei);
501 return invoke_source(ctx, function, get_this(dp), dp, retv, ei);
503 case DISPATCH_PROPERTYGET: {
507 hres = function_to_string(function, &str);
511 V_VT(retv) = VT_BSTR;
516 case DISPATCH_CONSTRUCT:
517 if(function->value_proc)
518 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei);
520 return invoke_constructor(ctx, function, dp, retv, ei);
523 FIXME("not implemented flags %x\n", flags);
530 static HRESULT Function_arguments(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
531 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
533 FunctionInstance *function = (FunctionInstance*)jsthis->u.jsdisp;
539 case DISPATCH_PROPERTYGET: {
540 if(function->arguments) {
541 jsdisp_addref(function->arguments);
542 var_set_jsdisp(retv, function->arguments);
544 V_VT(retv) = VT_NULL;
548 case DISPATCH_PROPERTYPUT:
551 FIXME("unimplemented flags %x\n", flags);
558 static void Function_destructor(jsdisp_t *dispex)
560 FunctionInstance *This = (FunctionInstance*)dispex;
563 release_bytecode(This->code);
564 if(This->scope_chain)
565 scope_release(This->scope_chain);
569 static const builtin_prop_t Function_props[] = {
570 {applyW, Function_apply, PROPF_METHOD|2},
571 {argumentsW, Function_arguments, 0},
572 {callW, Function_call, PROPF_METHOD|1},
573 {lengthW, Function_length, 0},
574 {toStringW, Function_toString, PROPF_METHOD}
577 static const builtin_info_t Function_info = {
579 {NULL, Function_value, 0},
580 sizeof(Function_props)/sizeof(*Function_props),
586 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
587 BOOL funcprot, jsdisp_t *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, jsdisp_t *dispex, jsdisp_t *prototype)
617 var_set_jsdisp(&var, prototype);
618 memset(&jsexcept, 0, sizeof(jsexcept));
620 return jsdisp_propput_name(dispex, prototypeW, &var, &jsexcept);
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, jsdisp_t *prototype, jsdisp_t **ret)
626 FunctionInstance *function;
629 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
636 num_set_int(&var, function->length);
637 hres = jsdisp_propput_const(&function->dispex, lengthW, &var);
641 hres = set_prototype(ctx, &function->dispex, prototype);
643 jsdisp_release(&function->dispex);
647 function->value_proc = value_proc;
648 function->name = name;
650 *ret = &function->dispex;
654 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
655 scope_chain_t *scope_chain, jsdisp_t **ret)
657 FunctionInstance *function;
661 hres = create_object(ctx, NULL, &prototype);
665 hres = create_function(ctx, NULL, PROPF_CONSTR, FALSE, NULL, &function);
666 if(SUCCEEDED(hres)) {
667 hres = set_prototype(ctx, &function->dispex, prototype);
669 jsdisp_release(&function->dispex);
671 jsdisp_release(prototype);
676 scope_addref(scope_chain);
677 function->scope_chain = scope_chain;
680 bytecode_addref(code);
681 function->code = code;
682 function->func_code = func_code;
683 function->length = function->func_code->param_cnt;
685 *ret = &function->dispex;
689 static HRESULT construct_function(script_ctx_t *ctx, DISPPARAMS *dp, jsexcept_t *ei, IDispatch **ret)
691 WCHAR *str = NULL, *ptr;
692 DWORD argc, len = 0, l;
699 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
700 static const WCHAR function_beginW[] = {')',' ','{','\n'};
701 static const WCHAR function_endW[] = {'\n','}',0};
705 params = heap_alloc(argc*sizeof(BSTR));
707 return E_OUTOFMEMORY;
710 len = (argc-2)*2; /* separating commas */
711 for(i=0; i < argc; i++) {
712 hres = to_string(ctx, get_arg(dp,i), ei, params+i);
715 len += SysStringLen(params[i]);
719 if(SUCCEEDED(hres)) {
720 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
721 str = heap_alloc(len*sizeof(WCHAR));
723 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
724 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
727 l = SysStringLen(params[j]);
728 memcpy(ptr, params[j], l*sizeof(WCHAR));
736 memcpy(ptr, function_beginW, sizeof(function_beginW));
737 ptr += sizeof(function_beginW)/sizeof(WCHAR);
739 l = SysStringLen(params[argc-1]);
740 memcpy(ptr, params[argc-1], l*sizeof(WCHAR));
743 memcpy(ptr, function_endW, sizeof(function_endW));
745 TRACE("%s\n", debugstr_w(str));
747 hres = E_OUTOFMEMORY;
752 SysFreeString(params[i]);
757 hres = compile_script(ctx, str, NULL, FALSE, FALSE, &code);
762 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt) {
763 ERR("Invalid parser result!\n");
764 release_bytecode(code);
768 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
769 release_bytecode(code);
773 *ret = to_disp(function);
777 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
778 VARIANT *retv, jsexcept_t *ei)
785 case DISPATCH_CONSTRUCT: {
788 hres = construct_function(ctx, dp, ei, &ret);
792 V_VT(retv) = VT_DISPATCH;
793 V_DISPATCH(retv) = ret;
797 FIXME("unimplemented flags %x\n", flags);
804 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
805 VARIANT *retv, jsexcept_t *ei)
811 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
813 FunctionInstance *prot, *constr;
816 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
818 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
822 prot->value_proc = FunctionProt_value;
823 prot->name = prototypeW;
825 hres = create_function(ctx, NULL, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
826 if(SUCCEEDED(hres)) {
827 constr->value_proc = FunctionConstr_value;
828 constr->name = FunctionW;
829 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
831 jsdisp_release(&constr->dispex);
833 jsdisp_release(&prot->dispex);
837 ctx->function_constr = &constr->dispex;