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 HRESULT init_parameters(jsdisp_t *var_disp, FunctionInstance *function, unsigned argc, jsval_t *argv,
64 for(i=0; i < function->func_code->param_cnt; i++) {
65 hres = jsdisp_propput_name(var_disp, function->func_code->params[i],
66 i < argc ? argv[i] : jsval_undefined(), ei);
74 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
75 jsval_t *r, jsexcept_t *ei)
81 static const builtin_info_t Arguments_info = {
83 {NULL, Arguments_value, 0},
89 static HRESULT create_arguments(script_ctx_t *ctx, IDispatch *calee, unsigned argc, jsval_t *argv,
90 jsexcept_t *ei, jsdisp_t **ret)
96 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
98 args = heap_alloc_zero(sizeof(jsdisp_t));
100 return E_OUTOFMEMORY;
102 hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
108 for(i=0; i < argc; i++) {
109 hres = jsdisp_propput_idx(args, i, argv[i], ei);
114 if(SUCCEEDED(hres)) {
115 hres = jsdisp_propput_name(args, lengthW, jsval_number(argc), ei);
118 hres = jsdisp_propput_name(args, caleeW, jsval_disp(calee), ei);
122 jsdisp_release(args);
130 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, jsdisp_t *arg_disp,
131 unsigned argc, jsval_t *argv, jsexcept_t *ei, jsdisp_t **ret)
136 hres = create_dispex(ctx, NULL, NULL, &var_disp);
140 hres = jsdisp_propput_name(var_disp, argumentsW, jsval_obj(arg_disp), ei);
142 hres = init_parameters(var_disp, function, argc, argv, ei);
144 jsdisp_release(var_disp);
152 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, unsigned argc, jsval_t *argv,
153 jsval_t *r, jsexcept_t *ei)
155 jsdisp_t *var_disp, *arg_disp;
156 exec_ctx_t *exec_ctx;
157 scope_chain_t *scope;
160 if(!function->func_code) {
161 FIXME("no source\n");
165 hres = create_arguments(ctx, to_disp(&function->dispex), argc, argv, ei, &arg_disp);
169 hres = create_var_disp(ctx, function, arg_disp, argc, argv, ei, &var_disp);
171 jsdisp_release(arg_disp);
175 hres = scope_push(function->scope_chain, var_disp, to_disp(var_disp), &scope);
176 if(SUCCEEDED(hres)) {
177 hres = create_exec_ctx(ctx, this_obj, var_disp, scope, FALSE, &exec_ctx);
178 scope_release(scope);
180 jsdisp_release(var_disp);
181 if(SUCCEEDED(hres)) {
185 prev_args = function->arguments;
186 function->arguments = arg_disp;
187 hres = exec_source(exec_ctx, function->code, function->func_code, FALSE, ei, r ? &retv : NULL);
188 function->arguments = prev_args;
189 if(SUCCEEDED(hres) && r) {
190 hres = variant_to_jsval(&retv, r);
195 jsdisp_release(arg_disp);
196 exec_release(exec_ctx);
200 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, unsigned argc, jsval_t *argv,
201 jsval_t *r, jsexcept_t *ei)
207 hres = create_object(ctx, &function->dispex, &this_obj);
211 hres = invoke_source(ctx, function, to_disp(this_obj), argc, argv, &var, ei);
213 jsdisp_release(this_obj);
217 if(is_object_instance(var)) {
218 jsdisp_release(this_obj);
222 *r = jsval_obj(this_obj);
227 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags, unsigned argc, jsval_t *argv,
228 jsval_t *r, jsexcept_t *ei)
234 set_disp(&vthis, this_disp);
235 else if(ctx->host_global)
236 set_disp(&vthis, ctx->host_global);
238 set_jsdisp(&vthis, ctx->global);
240 hres = function->value_proc(ctx, &vthis, flags, argc, argv, r, ei);
242 vdisp_release(&vthis);
246 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj,
247 unsigned argc, jsval_t *argv, jsval_t *r, jsexcept_t *ei)
249 if(function->value_proc)
250 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, argc, argv, r, ei);
252 return invoke_source(ctx, function, this_obj, argc, argv, r, ei);
255 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
259 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
260 static const WCHAR native_suffixW[] =
261 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
263 if(function->value_proc) {
266 name_len = strlenW(function->name);
267 str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
269 return E_OUTOFMEMORY;
271 memcpy(str, native_prefixW, sizeof(native_prefixW));
272 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
273 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
275 str = SysAllocStringLen(function->func_code->source, function->func_code->source_len);
277 return E_OUTOFMEMORY;
284 HRESULT Function_invoke(jsdisp_t *func_this, IDispatch *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r, jsexcept_t *ei)
286 FunctionInstance *function;
288 TRACE("func %p this %p\n", func_this, jsthis);
290 assert(is_class(func_this, JSCLASS_FUNCTION));
291 function = (FunctionInstance*)func_this;
293 if(function->value_proc)
294 return invoke_value_proc(function->dispex.ctx, function, jsthis, flags, argc, argv, r, ei);
296 if(flags == DISPATCH_CONSTRUCT)
297 return invoke_constructor(function->dispex.ctx, function, argc, argv, r, ei);
299 assert(flags == DISPATCH_METHOD);
300 return invoke_source(function->dispex.ctx, function, jsthis, argc, argv, r, ei);
303 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
304 jsval_t *r, jsexcept_t *ei)
306 FunctionInstance *This = function_from_vdisp(jsthis);
308 TRACE("%p %d\n", This, This->length);
311 case DISPATCH_PROPERTYGET:
312 *r = jsval_number(This->length);
315 FIXME("unimplemented flags %x\n", flags);
322 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
323 jsval_t *r, jsexcept_t *ei)
325 FunctionInstance *function;
331 if(!(function = function_this(jsthis)))
332 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
334 hres = function_to_string(function, &str);
339 *r = jsval_string(str);
345 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, jsexcept_t *ei, unsigned *argc, jsval_t **ret)
351 hres = jsdisp_propget_name(arg_array, lengthW, &val, ei);
355 hres = to_uint32_jsval(ctx, val, ei, &length);
360 argv = heap_alloc(length * sizeof(*argv));
362 return E_OUTOFMEMORY;
364 for(i=0; i<length; i++) {
365 hres = jsdisp_get_idx(arg_array, i, argv+i, ei);
366 if(hres == DISP_E_UNKNOWNNAME) {
367 argv[i] = jsval_undefined();
368 }else if(FAILED(hres)) {
370 jsval_release(argv[i]);
381 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
382 jsval_t *r, jsexcept_t *ei)
384 FunctionInstance *function;
385 jsval_t *args = NULL;
387 IDispatch *this_obj = NULL;
392 if(!(function = function_this(jsthis)))
393 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
396 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
397 hres = to_object_jsval(ctx, argv[0], &this_obj);
404 jsdisp_t *arg_array = NULL;
406 if(is_object_instance(argv[1])) {
407 arg_array = iface_to_jsdisp((IUnknown*)get_object(argv[1]));
409 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
410 jsdisp_release(arg_array);
416 hres = array_to_args(ctx, arg_array, ei, &cnt, &args);
417 jsdisp_release(arg_array);
419 FIXME("throw TypeError\n");
425 hres = call_function(ctx, function, this_obj, cnt, args, r, ei);
428 IDispatch_Release(this_obj);
429 for(i=0; i < cnt; i++)
430 jsval_release(args[i]);
435 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
436 jsval_t *r, jsexcept_t *ei)
438 FunctionInstance *function;
439 IDispatch *this_obj = NULL;
445 if(!(function = function_this(jsthis)))
446 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
449 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
450 hres = to_object_jsval(ctx, argv[0], &this_obj);
458 hres = call_function(ctx, function, this_obj, cnt, argv+1, r, ei);
461 IDispatch_Release(this_obj);
465 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
466 jsval_t *r, jsexcept_t *ei)
468 FunctionInstance *function;
472 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
473 ERR("dispex is not a function\n");
477 function = (FunctionInstance*)jsthis->u.jsdisp;
480 case DISPATCH_METHOD:
481 assert(function->value_proc != NULL);
482 return invoke_value_proc(ctx, function, NULL, flags, argc, argv, r, ei);
484 case DISPATCH_PROPERTYGET: {
488 hres = function_to_string(function, &str);
492 *r = jsval_string(str);
496 case DISPATCH_CONSTRUCT:
497 assert(function->value_proc != NULL);
498 return invoke_value_proc(ctx, function, NULL, flags, argc, argv, r, ei);
501 FIXME("not implemented flags %x\n", flags);
508 static HRESULT Function_arguments(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
509 unsigned argc, jsval_t *argv, jsval_t *r, jsexcept_t *ei)
511 FunctionInstance *function = (FunctionInstance*)jsthis->u.jsdisp;
517 case DISPATCH_PROPERTYGET: {
518 *r = function->arguments ? jsval_obj(jsdisp_addref(function->arguments)) : jsval_null();
521 case DISPATCH_PROPERTYPUT:
524 FIXME("unimplemented flags %x\n", flags);
531 static void Function_destructor(jsdisp_t *dispex)
533 FunctionInstance *This = (FunctionInstance*)dispex;
536 release_bytecode(This->code);
537 if(This->scope_chain)
538 scope_release(This->scope_chain);
542 static const builtin_prop_t Function_props[] = {
543 {applyW, Function_apply, PROPF_METHOD|2},
544 {argumentsW, Function_arguments, 0},
545 {callW, Function_call, PROPF_METHOD|1},
546 {lengthW, Function_length, 0},
547 {toStringW, Function_toString, PROPF_METHOD}
550 static const builtin_info_t Function_info = {
552 {NULL, Function_value, 0},
553 sizeof(Function_props)/sizeof(*Function_props),
559 static const builtin_prop_t FunctionInst_props[] = {
560 {argumentsW, Function_arguments, 0},
561 {lengthW, Function_length, 0}
564 static const builtin_info_t FunctionInst_info = {
566 {NULL, Function_value, 0},
567 sizeof(FunctionInst_props)/sizeof(*FunctionInst_props),
573 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
574 BOOL funcprot, jsdisp_t *prototype, FunctionInstance **ret)
576 FunctionInstance *function;
579 function = heap_alloc_zero(sizeof(FunctionInstance));
581 return E_OUTOFMEMORY;
584 hres = init_dispex(&function->dispex, ctx, builtin_info, prototype);
585 else if(builtin_info)
586 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
588 hres = init_dispex_from_constr(&function->dispex, ctx, &FunctionInst_info, ctx->function_constr);
592 function->flags = flags;
593 function->length = flags & PROPF_ARGMASK;
599 static HRESULT set_prototype(script_ctx_t *ctx, jsdisp_t *dispex, jsdisp_t *prototype)
603 memset(&jsexcept, 0, sizeof(jsexcept));
604 return jsdisp_propput_name(dispex, prototypeW, jsval_obj(prototype), &jsexcept);
607 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
608 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
610 FunctionInstance *function;
613 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
620 num_set_int(&var, function->length);
621 hres = jsdisp_propput_const(&function->dispex, lengthW, &var);
625 hres = set_prototype(ctx, &function->dispex, prototype);
627 jsdisp_release(&function->dispex);
631 function->value_proc = value_proc;
632 function->name = name;
634 *ret = &function->dispex;
638 static HRESULT set_constructor_prop(script_ctx_t *ctx, jsdisp_t *constr, jsdisp_t *prot)
642 static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
644 V_VT(&v) = VT_DISPATCH;
645 V_DISPATCH(&v) = to_disp(constr);
646 return jsdisp_propput_dontenum(prot, constructorW, &v);
649 HRESULT create_builtin_constructor(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
650 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
655 hres = create_builtin_function(ctx, value_proc, name, builtin_info, flags, prototype, &constr);
659 hres = set_constructor_prop(ctx, constr, prototype);
661 jsdisp_release(constr);
669 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
670 scope_chain_t *scope_chain, jsdisp_t **ret)
672 FunctionInstance *function;
676 hres = create_object(ctx, NULL, &prototype);
680 hres = create_function(ctx, NULL, PROPF_CONSTR, FALSE, NULL, &function);
681 if(SUCCEEDED(hres)) {
682 hres = set_prototype(ctx, &function->dispex, prototype);
684 hres = set_constructor_prop(ctx, &function->dispex, prototype);
686 jsdisp_release(&function->dispex);
688 jsdisp_release(prototype);
693 scope_addref(scope_chain);
694 function->scope_chain = scope_chain;
697 bytecode_addref(code);
698 function->code = code;
699 function->func_code = func_code;
700 function->length = function->func_code->param_cnt;
702 *ret = &function->dispex;
706 static HRESULT construct_function(script_ctx_t *ctx, unsigned argc, jsval_t *argv, jsexcept_t *ei, IDispatch **ret)
708 WCHAR *str = NULL, *ptr;
716 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
717 static const WCHAR function_beginW[] = {')',' ','{','\n'};
718 static const WCHAR function_endW[] = {'\n','}',0};
721 params = heap_alloc(argc*sizeof(BSTR));
723 return E_OUTOFMEMORY;
726 len = (argc-2)*2; /* separating commas */
727 for(i=0; i < argc; i++) {
728 hres = to_string_jsval(ctx, argv[i], ei, params+i);
731 len += SysStringLen(params[i]);
735 if(SUCCEEDED(hres)) {
736 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
737 str = heap_alloc(len*sizeof(WCHAR));
739 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
740 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
743 l = SysStringLen(params[j]);
744 memcpy(ptr, params[j], l*sizeof(WCHAR));
752 memcpy(ptr, function_beginW, sizeof(function_beginW));
753 ptr += sizeof(function_beginW)/sizeof(WCHAR);
755 l = SysStringLen(params[argc-1]);
756 memcpy(ptr, params[argc-1], l*sizeof(WCHAR));
759 memcpy(ptr, function_endW, sizeof(function_endW));
761 TRACE("%s\n", debugstr_w(str));
763 hres = E_OUTOFMEMORY;
768 SysFreeString(params[i]);
773 hres = compile_script(ctx, str, NULL, FALSE, FALSE, &code);
778 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt) {
779 ERR("Invalid parser result!\n");
780 release_bytecode(code);
784 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
785 release_bytecode(code);
789 *ret = to_disp(function);
793 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
794 jsval_t *r, jsexcept_t *ei)
801 case DISPATCH_CONSTRUCT: {
804 hres = construct_function(ctx, argc, argv, ei, &ret);
808 *r = jsval_disp(ret);
812 FIXME("unimplemented flags %x\n", flags);
819 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
820 jsval_t *r, jsexcept_t *ei)
826 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
828 FunctionInstance *prot, *constr;
831 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
833 hres = create_function(ctx, &Function_info, PROPF_CONSTR, TRUE, object_prototype, &prot);
837 prot->value_proc = FunctionProt_value;
838 prot->name = prototypeW;
840 hres = create_function(ctx, &FunctionInst_info, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
841 if(SUCCEEDED(hres)) {
842 constr->value_proc = FunctionConstr_value;
843 constr->name = FunctionW;
844 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
846 hres = set_constructor_prop(ctx, &constr->dispex, &prot->dispex);
848 jsdisp_release(&constr->dispex);
850 jsdisp_release(&prot->dispex);
854 ctx->function_constr = &constr->dispex;