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;
42 FunctionInstance *function;
46 static inline FunctionInstance *function_from_vdisp(vdisp_t *vdisp)
48 return (FunctionInstance*)vdisp->u.jsdisp;
51 static inline FunctionInstance *function_this(vdisp_t *jsthis)
53 return is_vclass(jsthis, JSCLASS_FUNCTION) ? function_from_vdisp(jsthis) : NULL;
56 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
58 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
59 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
60 static const WCHAR applyW[] = {'a','p','p','l','y',0};
61 static const WCHAR callW[] = {'c','a','l','l',0};
62 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
64 static HRESULT init_parameters(jsdisp_t *var_disp, FunctionInstance *function, unsigned argc, jsval_t *argv)
69 for(i=0; i < function->func_code->param_cnt; i++) {
70 hres = jsdisp_propput_name(var_disp, function->func_code->params[i],
71 i < argc ? argv[i] : jsval_undefined());
79 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
86 static void Arguments_destructor(jsdisp_t *jsdisp)
88 ArgumentsInstance *arguments = (ArgumentsInstance*)jsdisp;
90 jsdisp_release(&arguments->function->dispex);
91 jsdisp_release(arguments->var_obj);
95 static unsigned Arguments_idx_length(jsdisp_t *jsdisp)
97 ArgumentsInstance *arguments = (ArgumentsInstance*)jsdisp;
98 return arguments->function->length;
101 static HRESULT Arguments_idx_get(jsdisp_t *jsdisp, unsigned idx, jsval_t *res)
103 ArgumentsInstance *arguments = (ArgumentsInstance*)jsdisp;
105 TRACE("%p[%u]\n", arguments, idx);
107 /* FIXME: Accessing by name won't work for duplicated argument names */
108 return jsdisp_propget_name(arguments->var_obj, arguments->function->func_code->params[idx], res);
111 static HRESULT Arguments_idx_put(jsdisp_t *jsdisp, unsigned idx, jsval_t val)
113 ArgumentsInstance *arguments = (ArgumentsInstance*)jsdisp;
115 TRACE("%p[%u] = %s\n", arguments, idx, debugstr_jsval(val));
117 /* FIXME: Accessing by name won't work for duplicated argument names */
118 return jsdisp_propput_name(arguments->var_obj, arguments->function->func_code->params[idx], val);
121 static const builtin_info_t Arguments_info = {
123 {NULL, Arguments_value, 0},
125 Arguments_destructor,
127 Arguments_idx_length,
132 static HRESULT create_arguments(script_ctx_t *ctx, FunctionInstance *calee, jsdisp_t *var_obj,
133 unsigned argc, jsval_t *argv, jsdisp_t **ret)
135 ArgumentsInstance *args;
139 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
141 args = heap_alloc_zero(sizeof(*args));
143 return E_OUTOFMEMORY;
145 hres = init_dispex_from_constr(&args->jsdisp, ctx, &Arguments_info, ctx->object_constr);
151 jsdisp_addref(&calee->dispex);
152 args->function = calee;
153 args->var_obj = jsdisp_addref(var_obj);
155 /* Store unnamed arguments directly in arguments object */
156 for(i = calee->length; i < argc; i++) {
159 static const WCHAR formatW[] = {'%','d',0};
161 sprintfW(buf, formatW, i);
162 hres = jsdisp_propput_dontenum(&args->jsdisp, buf, argv[i]);
167 if(SUCCEEDED(hres)) {
168 hres = jsdisp_propput_dontenum(&args->jsdisp, lengthW, jsval_number(argc));
170 hres = jsdisp_propput_dontenum(&args->jsdisp, caleeW, jsval_disp(to_disp(&calee->dispex)));
173 jsdisp_release(&args->jsdisp);
177 *ret = &args->jsdisp;
181 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, unsigned argc, jsval_t *argv, jsdisp_t **ret)
186 hres = create_dispex(ctx, NULL, NULL, &var_disp);
190 hres = init_parameters(var_disp, function, argc, argv);
192 jsdisp_release(var_disp);
200 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, unsigned argc, jsval_t *argv,
203 jsdisp_t *var_disp, *arg_disp;
204 exec_ctx_t *exec_ctx;
205 scope_chain_t *scope;
208 if(!function->func_code) {
209 FIXME("no source\n");
213 hres = create_var_disp(ctx, function, argc, argv, &var_disp);
217 hres = create_arguments(ctx, function, var_disp, argc, argv, &arg_disp);
219 jsdisp_release(var_disp);
223 hres = jsdisp_propput_name(var_disp, argumentsW, jsval_obj(arg_disp));
224 if(SUCCEEDED(hres)) {
225 hres = scope_push(function->scope_chain, var_disp, to_disp(var_disp), &scope);
226 if(SUCCEEDED(hres)) {
227 hres = create_exec_ctx(ctx, this_obj, var_disp, scope, FALSE, &exec_ctx);
228 scope_release(scope);
231 jsdisp_release(var_disp);
232 if(SUCCEEDED(hres)) {
235 prev_args = function->arguments;
236 function->arguments = arg_disp;
237 hres = exec_source(exec_ctx, function->code, function->func_code, FALSE, r);
238 function->arguments = prev_args;
241 jsdisp_release(arg_disp);
242 exec_release(exec_ctx);
246 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, unsigned argc, jsval_t *argv,
253 hres = create_object(ctx, &function->dispex, &this_obj);
257 hres = invoke_source(ctx, function, to_disp(this_obj), argc, argv, &var);
259 jsdisp_release(this_obj);
263 if(is_object_instance(var)) {
264 jsdisp_release(this_obj);
268 *r = jsval_obj(this_obj);
273 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags,
274 unsigned argc, jsval_t *argv, jsval_t *r)
280 set_disp(&vthis, this_disp);
281 else if(ctx->host_global)
282 set_disp(&vthis, ctx->host_global);
284 set_jsdisp(&vthis, ctx->global);
286 hres = function->value_proc(ctx, &vthis, flags, argc, argv, r);
288 vdisp_release(&vthis);
292 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj,
293 unsigned argc, jsval_t *argv, jsval_t *r)
295 if(function->value_proc)
296 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, argc, argv, r);
298 return invoke_source(ctx, function, this_obj, argc, argv, r);
301 static HRESULT function_to_string(FunctionInstance *function, jsstr_t **ret)
305 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
306 static const WCHAR native_suffixW[] =
307 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
309 if(function->value_proc) {
312 name_len = strlenW(function->name);
313 str = jsstr_alloc_buf((sizeof(native_prefixW)+sizeof(native_suffixW))/sizeof(WCHAR) + name_len);
315 return E_OUTOFMEMORY;
317 memcpy(str->str, native_prefixW, sizeof(native_prefixW));
318 memcpy(str->str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
319 memcpy(str->str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
321 str = jsstr_alloc_len(function->func_code->source, function->func_code->source_len);
323 return E_OUTOFMEMORY;
330 HRESULT Function_invoke(jsdisp_t *func_this, IDispatch *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
332 FunctionInstance *function;
334 TRACE("func %p this %p\n", func_this, jsthis);
336 assert(is_class(func_this, JSCLASS_FUNCTION));
337 function = (FunctionInstance*)func_this;
339 if(function->value_proc)
340 return invoke_value_proc(function->dispex.ctx, function, jsthis, flags, argc, argv, r);
342 if(flags == DISPATCH_CONSTRUCT)
343 return invoke_constructor(function->dispex.ctx, function, argc, argv, r);
345 assert(flags == DISPATCH_METHOD);
346 return invoke_source(function->dispex.ctx, function, jsthis, argc, argv, r);
349 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
352 FunctionInstance *This = function_from_vdisp(jsthis);
354 TRACE("%p %d\n", This, This->length);
357 case DISPATCH_PROPERTYGET:
358 *r = jsval_number(This->length);
361 FIXME("unimplemented flags %x\n", flags);
368 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
371 FunctionInstance *function;
377 if(!(function = function_this(jsthis)))
378 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
380 hres = function_to_string(function, &str);
385 *r = jsval_string(str);
391 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, unsigned *argc, jsval_t **ret)
397 hres = jsdisp_propget_name(arg_array, lengthW, &val);
401 hres = to_uint32(ctx, val, &length);
406 argv = heap_alloc(length * sizeof(*argv));
408 return E_OUTOFMEMORY;
410 for(i=0; i<length; i++) {
411 hres = jsdisp_get_idx(arg_array, i, argv+i);
412 if(hres == DISP_E_UNKNOWNNAME) {
413 argv[i] = jsval_undefined();
414 }else if(FAILED(hres)) {
416 jsval_release(argv[i]);
427 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
429 FunctionInstance *function;
430 jsval_t *args = NULL;
432 IDispatch *this_obj = NULL;
437 if(!(function = function_this(jsthis)))
438 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
441 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
442 hres = to_object(ctx, argv[0], &this_obj);
449 jsdisp_t *arg_array = NULL;
451 if(is_object_instance(argv[1])) {
452 arg_array = iface_to_jsdisp((IUnknown*)get_object(argv[1]));
454 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
455 jsdisp_release(arg_array);
461 hres = array_to_args(ctx, arg_array, &cnt, &args);
462 jsdisp_release(arg_array);
464 FIXME("throw TypeError\n");
470 hres = call_function(ctx, function, this_obj, cnt, args, r);
473 IDispatch_Release(this_obj);
474 for(i=0; i < cnt; i++)
475 jsval_release(args[i]);
480 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
483 FunctionInstance *function;
484 IDispatch *this_obj = NULL;
490 if(!(function = function_this(jsthis)))
491 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
494 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
495 hres = to_object(ctx, argv[0], &this_obj);
503 hres = call_function(ctx, function, this_obj, cnt, argv+1, r);
506 IDispatch_Release(this_obj);
510 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
513 FunctionInstance *function;
517 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
518 ERR("dispex is not a function\n");
522 function = (FunctionInstance*)jsthis->u.jsdisp;
525 case DISPATCH_METHOD:
526 assert(function->value_proc != NULL);
527 return invoke_value_proc(ctx, function, NULL, flags, argc, argv, r);
529 case DISPATCH_PROPERTYGET: {
533 hres = function_to_string(function, &str);
537 *r = jsval_string(str);
541 case DISPATCH_CONSTRUCT:
542 assert(function->value_proc != NULL);
543 return invoke_value_proc(ctx, function, NULL, flags, argc, argv, r);
546 FIXME("not implemented flags %x\n", flags);
553 static HRESULT Function_arguments(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
554 unsigned argc, jsval_t *argv, jsval_t *r)
556 FunctionInstance *function = (FunctionInstance*)jsthis->u.jsdisp;
562 case DISPATCH_PROPERTYGET: {
563 *r = function->arguments ? jsval_obj(jsdisp_addref(function->arguments)) : jsval_null();
566 case DISPATCH_PROPERTYPUT:
569 FIXME("unimplemented flags %x\n", flags);
576 static void Function_destructor(jsdisp_t *dispex)
578 FunctionInstance *This = (FunctionInstance*)dispex;
581 release_bytecode(This->code);
582 if(This->scope_chain)
583 scope_release(This->scope_chain);
587 static const builtin_prop_t Function_props[] = {
588 {applyW, Function_apply, PROPF_METHOD|2},
589 {argumentsW, Function_arguments, 0},
590 {callW, Function_call, PROPF_METHOD|1},
591 {lengthW, Function_length, 0},
592 {toStringW, Function_toString, PROPF_METHOD}
595 static const builtin_info_t Function_info = {
597 {NULL, Function_value, 0},
598 sizeof(Function_props)/sizeof(*Function_props),
604 static const builtin_prop_t FunctionInst_props[] = {
605 {argumentsW, Function_arguments, 0},
606 {lengthW, Function_length, 0}
609 static const builtin_info_t FunctionInst_info = {
611 {NULL, Function_value, 0},
612 sizeof(FunctionInst_props)/sizeof(*FunctionInst_props),
618 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
619 BOOL funcprot, jsdisp_t *prototype, FunctionInstance **ret)
621 FunctionInstance *function;
624 function = heap_alloc_zero(sizeof(FunctionInstance));
626 return E_OUTOFMEMORY;
629 hres = init_dispex(&function->dispex, ctx, builtin_info, prototype);
630 else if(builtin_info)
631 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
633 hres = init_dispex_from_constr(&function->dispex, ctx, &FunctionInst_info, ctx->function_constr);
639 function->flags = flags;
640 function->length = flags & PROPF_ARGMASK;
646 static inline HRESULT set_prototype(script_ctx_t *ctx, jsdisp_t *dispex, jsdisp_t *prototype)
648 return jsdisp_propput_dontenum(dispex, prototypeW, jsval_obj(prototype));
651 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
652 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
654 FunctionInstance *function;
657 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
662 hres = jsdisp_propput_const(&function->dispex, lengthW, jsval_number(function->length));
664 hres = set_prototype(ctx, &function->dispex, prototype);
666 jsdisp_release(&function->dispex);
670 function->value_proc = value_proc;
671 function->name = name;
673 *ret = &function->dispex;
677 static HRESULT set_constructor_prop(script_ctx_t *ctx, jsdisp_t *constr, jsdisp_t *prot)
679 static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
681 return jsdisp_propput_dontenum(prot, constructorW, jsval_obj(constr));
684 HRESULT create_builtin_constructor(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
685 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
690 hres = create_builtin_function(ctx, value_proc, name, builtin_info, flags, prototype, &constr);
694 hres = set_constructor_prop(ctx, constr, prototype);
696 jsdisp_release(constr);
704 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
705 scope_chain_t *scope_chain, jsdisp_t **ret)
707 FunctionInstance *function;
711 hres = create_object(ctx, NULL, &prototype);
715 hres = create_function(ctx, NULL, PROPF_CONSTR, FALSE, NULL, &function);
716 if(SUCCEEDED(hres)) {
717 hres = set_prototype(ctx, &function->dispex, prototype);
719 hres = set_constructor_prop(ctx, &function->dispex, prototype);
721 jsdisp_release(&function->dispex);
723 jsdisp_release(prototype);
728 scope_addref(scope_chain);
729 function->scope_chain = scope_chain;
732 bytecode_addref(code);
733 function->code = code;
734 function->func_code = func_code;
735 function->length = function->func_code->param_cnt;
737 *ret = &function->dispex;
741 static HRESULT construct_function(script_ctx_t *ctx, unsigned argc, jsval_t *argv, IDispatch **ret)
743 WCHAR *str = NULL, *ptr;
747 jsstr_t **params = NULL;
751 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
752 static const WCHAR function_beginW[] = {')',' ','{','\n'};
753 static const WCHAR function_endW[] = {'\n','}',0};
756 params = heap_alloc(argc*sizeof(*params));
758 return E_OUTOFMEMORY;
761 len = (argc-2)*2; /* separating commas */
762 for(i=0; i < argc; i++) {
763 hres = to_string(ctx, argv[i], params+i);
766 len += jsstr_length(params[i]);
770 if(SUCCEEDED(hres)) {
771 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
772 str = heap_alloc(len*sizeof(WCHAR));
774 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
775 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
778 l = jsstr_length(params[j]);
779 memcpy(ptr, params[j]->str, l*sizeof(WCHAR));
787 memcpy(ptr, function_beginW, sizeof(function_beginW));
788 ptr += sizeof(function_beginW)/sizeof(WCHAR);
790 l = jsstr_length(params[argc-1]);
791 memcpy(ptr, params[argc-1]->str, l*sizeof(WCHAR));
794 memcpy(ptr, function_endW, sizeof(function_endW));
796 TRACE("%s\n", debugstr_w(str));
798 hres = E_OUTOFMEMORY;
803 jsstr_release(params[i]);
808 hres = compile_script(ctx, str, NULL, NULL, FALSE, FALSE, &code);
813 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt) {
814 ERR("Invalid parser result!\n");
815 release_bytecode(code);
819 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
820 release_bytecode(code);
824 *ret = to_disp(function);
828 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
836 case DISPATCH_CONSTRUCT: {
839 hres = construct_function(ctx, argc, argv, &ret);
843 *r = jsval_disp(ret);
847 FIXME("unimplemented flags %x\n", flags);
854 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
861 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
863 FunctionInstance *prot, *constr;
866 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
868 hres = create_function(ctx, &Function_info, PROPF_CONSTR, TRUE, object_prototype, &prot);
872 prot->value_proc = FunctionProt_value;
873 prot->name = prototypeW;
875 hres = create_function(ctx, &FunctionInst_info, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
876 if(SUCCEEDED(hres)) {
877 constr->value_proc = FunctionConstr_value;
878 constr->name = FunctionW;
879 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
881 hres = set_constructor_prop(ctx, &constr->dispex, &prot->dispex);
883 jsdisp_release(&constr->dispex);
885 jsdisp_release(&prot->dispex);
889 ctx->function_constr = &constr->dispex;