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
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
30 builtin_invoke_t value_proc;
33 scope_chain_t *scope_chain;
35 function_code_t *func_code;
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};
56 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
58 static IDispatch *get_this(DISPPARAMS *dp)
62 for(i=0; i < dp->cNamedArgs; i++) {
63 if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
64 if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
65 return V_DISPATCH(dp->rgvarg+i);
67 WARN("This is not VT_DISPATCH\n");
72 TRACE("no this passed\n");
76 static HRESULT init_parameters(jsdisp_t *var_disp, FunctionInstance *function, DISPPARAMS *dp,
83 V_VT(&var_empty) = VT_EMPTY;
86 for(i=0; i < function->func_code->param_cnt; i++) {
87 hres = jsdisp_propput_name(var_disp, function->func_code->params[i],
88 i < cargs ? get_arg(dp,i) : &var_empty, ei);
96 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
97 VARIANT *retv, jsexcept_t *ei)
103 static const builtin_info_t Arguments_info = {
105 {NULL, Arguments_value, 0},
111 static HRESULT create_arguments(script_ctx_t *ctx, IDispatch *calee, DISPPARAMS *dp,
112 jsexcept_t *ei, jsdisp_t **ret)
119 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
121 args = heap_alloc_zero(sizeof(jsdisp_t));
123 return E_OUTOFMEMORY;
125 hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
131 for(i=0; i < arg_cnt(dp); i++) {
132 hres = jsdisp_propput_idx(args, i, get_arg(dp,i), ei);
137 if(SUCCEEDED(hres)) {
138 num_set_int(&var, arg_cnt(dp));
139 hres = jsdisp_propput_name(args, lengthW, &var, ei);
141 if(SUCCEEDED(hres)) {
142 V_VT(&var) = VT_DISPATCH;
143 V_DISPATCH(&var) = calee;
144 hres = jsdisp_propput_name(args, caleeW, &var, ei);
149 jsdisp_release(args);
157 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, jsdisp_t *arg_disp,
158 DISPPARAMS *dp, jsexcept_t *ei, jsdisp_t **ret)
164 hres = create_dispex(ctx, NULL, NULL, &var_disp);
168 var_set_jsdisp(&var, arg_disp);
169 hres = jsdisp_propput_name(var_disp, argumentsW, &var, ei);
171 hres = init_parameters(var_disp, function, dp, ei);
173 jsdisp_release(var_disp);
181 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *dp,
182 VARIANT *retv, jsexcept_t *ei)
184 jsdisp_t *var_disp, *arg_disp;
185 exec_ctx_t *exec_ctx;
186 scope_chain_t *scope;
189 if(!function->func_code) {
190 FIXME("no source\n");
194 hres = create_arguments(ctx, to_disp(&function->dispex), dp, ei, &arg_disp);
198 hres = create_var_disp(ctx, function, arg_disp, dp, ei, &var_disp);
200 jsdisp_release(arg_disp);
204 hres = scope_push(function->scope_chain, var_disp, &scope);
205 if(SUCCEEDED(hres)) {
206 hres = create_exec_ctx(ctx, this_obj, var_disp, scope, FALSE, &exec_ctx);
207 scope_release(scope);
209 jsdisp_release(var_disp);
210 if(SUCCEEDED(hres)) {
213 prev_args = function->arguments;
214 function->arguments = arg_disp;
215 hres = exec_source(exec_ctx, function->code, function->func_code, FALSE, ei, retv);
216 function->arguments = prev_args;
218 jsdisp_release(arg_disp);
219 exec_release(exec_ctx);
225 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp,
226 VARIANT *retv, jsexcept_t *ei)
232 hres = create_object(ctx, &function->dispex, &this_obj);
236 hres = invoke_source(ctx, function, to_disp(this_obj), dp, &var, ei);
238 jsdisp_release(this_obj);
242 if(V_VT(&var) == VT_DISPATCH) {
243 jsdisp_release(this_obj);
244 V_VT(retv) = VT_DISPATCH;
245 V_DISPATCH(retv) = V_DISPATCH(&var);
248 var_set_jsdisp(retv, this_obj);
253 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags, DISPPARAMS *dp,
254 VARIANT *retv, jsexcept_t *ei)
260 set_disp(&vthis, this_disp);
261 else if(ctx->host_global)
262 set_disp(&vthis, ctx->host_global);
264 set_jsdisp(&vthis, ctx->global);
266 hres = function->value_proc(ctx, &vthis, flags, dp, retv, ei);
268 vdisp_release(&vthis);
272 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *args,
273 VARIANT *retv, jsexcept_t *ei)
275 if(function->value_proc)
276 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, args, retv, ei);
278 return invoke_source(ctx, function, this_obj, args, retv, ei);
281 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
285 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
286 static const WCHAR native_suffixW[] =
287 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
289 if(function->value_proc) {
292 name_len = strlenW(function->name);
293 str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
295 return E_OUTOFMEMORY;
297 memcpy(str, native_prefixW, sizeof(native_prefixW));
298 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
299 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
301 str = SysAllocStringLen(function->func_code->source, function->func_code->source_len);
303 return E_OUTOFMEMORY;
310 HRESULT Function_invoke(jsdisp_t *func_this, IDispatch *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
312 FunctionInstance *function;
314 TRACE("func %p this %p\n", func_this, jsthis);
316 assert(is_class(func_this, JSCLASS_FUNCTION));
317 function = (FunctionInstance*)func_this;
319 if(function->value_proc)
320 return invoke_value_proc(function->dispex.ctx, function, jsthis, flags, dp, retv, ei);
322 if(flags == DISPATCH_CONSTRUCT)
323 return invoke_constructor(function->dispex.ctx, function, dp, retv, ei);
325 assert(flags == DISPATCH_METHOD);
326 return invoke_source(function->dispex.ctx, function, jsthis, dp, retv, ei);
329 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
330 VARIANT *retv, jsexcept_t *ei)
332 FunctionInstance *This = function_from_vdisp(jsthis);
334 TRACE("%p %d\n", This, This->length);
337 case DISPATCH_PROPERTYGET:
338 num_set_int(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)
351 FunctionInstance *function;
357 if(!(function = function_this(jsthis)))
358 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
360 hres = function_to_string(function, &str);
365 V_VT(retv) = VT_BSTR;
373 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, jsexcept_t *ei, DISPPARAMS *args)
379 hres = jsdisp_propget_name(arg_array, lengthW, &var, ei);
383 hres = to_uint32(ctx, &var, ei, &length);
388 argv = heap_alloc(length * sizeof(VARIANT));
390 return E_OUTOFMEMORY;
392 for(i=0; i<length; i++) {
393 hres = jsdisp_get_idx(arg_array, i, argv+i, ei);
394 if(hres == DISP_E_UNKNOWNNAME)
395 V_VT(argv+i) = VT_EMPTY;
396 else if(FAILED(hres)) {
398 VariantClear(argv+i);
404 args->cArgs = length;
409 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
410 VARIANT *retv, jsexcept_t *ei)
412 FunctionInstance *function;
413 DISPPARAMS args = {NULL,NULL,0,0};
415 IDispatch *this_obj = NULL;
420 if(!(function = function_this(jsthis)))
421 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
425 VARIANT *v = get_arg(dp,0);
427 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
428 hres = to_object(ctx, v, &this_obj);
435 jsdisp_t *arg_array = NULL;
437 if(V_VT(get_arg(dp,1)) == VT_DISPATCH) {
438 arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(get_arg(dp,1)));
440 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
441 jsdisp_release(arg_array);
447 hres = array_to_args(ctx, arg_array, ei, &args);
448 jsdisp_release(arg_array);
450 FIXME("throw TypeError\n");
456 hres = call_function(ctx, function, this_obj, &args, retv, ei);
459 IDispatch_Release(this_obj);
460 for(i=0; i<args.cArgs; i++)
461 VariantClear(args.rgvarg+i);
462 heap_free(args.rgvarg);
466 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
467 VARIANT *retv, jsexcept_t *ei)
469 FunctionInstance *function;
470 DISPPARAMS args = {NULL,NULL,0,0};
471 IDispatch *this_obj = NULL;
477 if(!(function = function_this(jsthis)))
478 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
482 VARIANT *v = get_arg(dp,0);
484 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
485 hres = to_object(ctx, v, &this_obj);
494 args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1;
496 hres = call_function(ctx, function, this_obj, &args, retv, ei);
499 IDispatch_Release(this_obj);
503 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
504 VARIANT *retv, jsexcept_t *ei)
506 FunctionInstance *function;
510 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
511 ERR("dispex is not a function\n");
515 function = (FunctionInstance*)jsthis->u.jsdisp;
518 case DISPATCH_METHOD:
519 if(function->value_proc)
520 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei);
522 return invoke_source(ctx, function, get_this(dp), dp, retv, ei);
524 case DISPATCH_PROPERTYGET: {
528 hres = function_to_string(function, &str);
532 V_VT(retv) = VT_BSTR;
537 case DISPATCH_CONSTRUCT:
538 if(function->value_proc)
539 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei);
541 return invoke_constructor(ctx, function, dp, retv, ei);
544 FIXME("not implemented flags %x\n", flags);
551 static HRESULT Function_arguments(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
552 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
554 FunctionInstance *function = (FunctionInstance*)jsthis->u.jsdisp;
560 case DISPATCH_PROPERTYGET: {
561 if(function->arguments) {
562 jsdisp_addref(function->arguments);
563 var_set_jsdisp(retv, function->arguments);
565 V_VT(retv) = VT_NULL;
569 case DISPATCH_PROPERTYPUT:
572 FIXME("unimplemented flags %x\n", flags);
579 static void Function_destructor(jsdisp_t *dispex)
581 FunctionInstance *This = (FunctionInstance*)dispex;
584 release_bytecode(This->code);
585 if(This->scope_chain)
586 scope_release(This->scope_chain);
590 static const builtin_prop_t Function_props[] = {
591 {applyW, Function_apply, PROPF_METHOD|2},
592 {argumentsW, Function_arguments, 0},
593 {callW, Function_call, PROPF_METHOD|1},
594 {lengthW, Function_length, 0},
595 {toStringW, Function_toString, PROPF_METHOD}
598 static const builtin_info_t Function_info = {
600 {NULL, Function_value, 0},
601 sizeof(Function_props)/sizeof(*Function_props),
607 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
608 BOOL funcprot, jsdisp_t *prototype, FunctionInstance **ret)
610 FunctionInstance *function;
613 function = heap_alloc_zero(sizeof(FunctionInstance));
615 return E_OUTOFMEMORY;
618 hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
619 else if(builtin_info)
620 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
622 hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
626 function->flags = flags;
627 function->length = flags & PROPF_ARGMASK;
633 static HRESULT set_prototype(script_ctx_t *ctx, jsdisp_t *dispex, jsdisp_t *prototype)
638 var_set_jsdisp(&var, prototype);
639 memset(&jsexcept, 0, sizeof(jsexcept));
641 return jsdisp_propput_name(dispex, prototypeW, &var, &jsexcept);
644 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
645 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
647 FunctionInstance *function;
650 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
657 num_set_int(&var, function->length);
658 hres = jsdisp_propput_const(&function->dispex, lengthW, &var);
662 hres = set_prototype(ctx, &function->dispex, prototype);
664 jsdisp_release(&function->dispex);
668 function->value_proc = value_proc;
669 function->name = name;
671 *ret = &function->dispex;
675 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
676 scope_chain_t *scope_chain, jsdisp_t **ret)
678 FunctionInstance *function;
682 hres = create_object(ctx, NULL, &prototype);
686 hres = create_function(ctx, NULL, PROPF_CONSTR, FALSE, NULL, &function);
687 if(SUCCEEDED(hres)) {
688 hres = set_prototype(ctx, &function->dispex, prototype);
690 jsdisp_release(&function->dispex);
692 jsdisp_release(prototype);
697 scope_addref(scope_chain);
698 function->scope_chain = scope_chain;
701 bytecode_addref(code);
702 function->code = code;
703 function->func_code = func_code;
704 function->length = function->func_code->param_cnt;
706 *ret = &function->dispex;
710 static HRESULT construct_function(script_ctx_t *ctx, DISPPARAMS *dp, jsexcept_t *ei, IDispatch **ret)
712 WCHAR *str = NULL, *ptr;
713 DWORD argc, len = 0, l;
720 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
721 static const WCHAR function_beginW[] = {')',' ','{','\n'};
722 static const WCHAR function_endW[] = {'\n','}',0};
726 params = heap_alloc(argc*sizeof(BSTR));
728 return E_OUTOFMEMORY;
731 len = (argc-2)*2; /* separating commas */
732 for(i=0; i < argc; i++) {
733 hres = to_string(ctx, get_arg(dp,i), ei, params+i);
736 len += SysStringLen(params[i]);
740 if(SUCCEEDED(hres)) {
741 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
742 str = heap_alloc(len*sizeof(WCHAR));
744 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
745 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
748 l = SysStringLen(params[j]);
749 memcpy(ptr, params[j], l*sizeof(WCHAR));
757 memcpy(ptr, function_beginW, sizeof(function_beginW));
758 ptr += sizeof(function_beginW)/sizeof(WCHAR);
760 l = SysStringLen(params[argc-1]);
761 memcpy(ptr, params[argc-1], l*sizeof(WCHAR));
764 memcpy(ptr, function_endW, sizeof(function_endW));
766 TRACE("%s\n", debugstr_w(str));
768 hres = E_OUTOFMEMORY;
773 SysFreeString(params[i]);
778 hres = compile_script(ctx, str, NULL, FALSE, FALSE, &code);
783 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt) {
784 ERR("Invalid parser result!\n");
785 release_bytecode(code);
789 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
790 release_bytecode(code);
794 *ret = to_disp(function);
798 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
799 VARIANT *retv, jsexcept_t *ei)
806 case DISPATCH_CONSTRUCT: {
809 hres = construct_function(ctx, dp, ei, &ret);
813 V_VT(retv) = VT_DISPATCH;
814 V_DISPATCH(retv) = ret;
818 FIXME("unimplemented flags %x\n", flags);
825 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
826 VARIANT *retv, jsexcept_t *ei)
832 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
834 FunctionInstance *prot, *constr;
837 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
839 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
843 prot->value_proc = FunctionProt_value;
844 prot->name = prototypeW;
846 hres = create_function(ctx, NULL, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
847 if(SUCCEEDED(hres)) {
848 constr->value_proc = FunctionConstr_value;
849 constr->name = FunctionW;
850 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
852 jsdisp_release(&constr->dispex);
854 jsdisp_release(&prot->dispex);
858 ctx->function_constr = &constr->dispex;