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)
63 for(i=0; i < function->func_code->param_cnt; i++) {
64 hres = jsdisp_propput_name(var_disp, function->func_code->params[i],
65 i < argc ? argv[i] : jsval_undefined());
73 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
80 static const builtin_info_t Arguments_info = {
82 {NULL, Arguments_value, 0},
88 static HRESULT create_arguments(script_ctx_t *ctx, IDispatch *calee, unsigned argc, jsval_t *argv, jsdisp_t **ret)
94 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
96 args = heap_alloc_zero(sizeof(jsdisp_t));
100 hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
106 for(i=0; i < argc; i++) {
107 hres = jsdisp_propput_idx(args, i, argv[i]);
112 if(SUCCEEDED(hres)) {
113 hres = jsdisp_propput_name(args, lengthW, jsval_number(argc));
116 hres = jsdisp_propput_name(args, caleeW, jsval_disp(calee));
120 jsdisp_release(args);
128 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, jsdisp_t *arg_disp,
129 unsigned argc, jsval_t *argv, jsdisp_t **ret)
134 hres = create_dispex(ctx, NULL, NULL, &var_disp);
138 hres = jsdisp_propput_name(var_disp, argumentsW, jsval_obj(arg_disp));
140 hres = init_parameters(var_disp, function, argc, argv);
142 jsdisp_release(var_disp);
150 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, unsigned argc, jsval_t *argv,
153 jsdisp_t *var_disp, *arg_disp;
154 exec_ctx_t *exec_ctx;
155 scope_chain_t *scope;
158 if(!function->func_code) {
159 FIXME("no source\n");
163 hres = create_arguments(ctx, to_disp(&function->dispex), argc, argv, &arg_disp);
167 hres = create_var_disp(ctx, function, arg_disp, argc, argv, &var_disp);
169 jsdisp_release(arg_disp);
173 hres = scope_push(function->scope_chain, var_disp, to_disp(var_disp), &scope);
174 if(SUCCEEDED(hres)) {
175 hres = create_exec_ctx(ctx, this_obj, var_disp, scope, FALSE, &exec_ctx);
176 scope_release(scope);
178 jsdisp_release(var_disp);
179 if(SUCCEEDED(hres)) {
182 prev_args = function->arguments;
183 function->arguments = arg_disp;
184 hres = exec_source(exec_ctx, function->code, function->func_code, FALSE, r);
185 function->arguments = prev_args;
188 jsdisp_release(arg_disp);
189 exec_release(exec_ctx);
193 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, unsigned argc, jsval_t *argv,
200 hres = create_object(ctx, &function->dispex, &this_obj);
204 hres = invoke_source(ctx, function, to_disp(this_obj), argc, argv, &var);
206 jsdisp_release(this_obj);
210 if(is_object_instance(var)) {
211 jsdisp_release(this_obj);
215 *r = jsval_obj(this_obj);
220 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags,
221 unsigned argc, jsval_t *argv, jsval_t *r)
227 set_disp(&vthis, this_disp);
228 else if(ctx->host_global)
229 set_disp(&vthis, ctx->host_global);
231 set_jsdisp(&vthis, ctx->global);
233 hres = function->value_proc(ctx, &vthis, flags, argc, argv, r);
235 vdisp_release(&vthis);
239 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj,
240 unsigned argc, jsval_t *argv, jsval_t *r)
242 if(function->value_proc)
243 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, argc, argv, r);
245 return invoke_source(ctx, function, this_obj, argc, argv, r);
248 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
252 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
253 static const WCHAR native_suffixW[] =
254 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
256 if(function->value_proc) {
259 name_len = strlenW(function->name);
260 str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
262 return E_OUTOFMEMORY;
264 memcpy(str, native_prefixW, sizeof(native_prefixW));
265 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
266 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
268 str = SysAllocStringLen(function->func_code->source, function->func_code->source_len);
270 return E_OUTOFMEMORY;
277 HRESULT Function_invoke(jsdisp_t *func_this, IDispatch *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
279 FunctionInstance *function;
281 TRACE("func %p this %p\n", func_this, jsthis);
283 assert(is_class(func_this, JSCLASS_FUNCTION));
284 function = (FunctionInstance*)func_this;
286 if(function->value_proc)
287 return invoke_value_proc(function->dispex.ctx, function, jsthis, flags, argc, argv, r);
289 if(flags == DISPATCH_CONSTRUCT)
290 return invoke_constructor(function->dispex.ctx, function, argc, argv, r);
292 assert(flags == DISPATCH_METHOD);
293 return invoke_source(function->dispex.ctx, function, jsthis, argc, argv, r);
296 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
299 FunctionInstance *This = function_from_vdisp(jsthis);
301 TRACE("%p %d\n", This, This->length);
304 case DISPATCH_PROPERTYGET:
305 *r = jsval_number(This->length);
308 FIXME("unimplemented flags %x\n", flags);
315 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
318 FunctionInstance *function;
324 if(!(function = function_this(jsthis)))
325 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
327 hres = function_to_string(function, &str);
332 *r = jsval_string(str);
338 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, unsigned *argc, jsval_t **ret)
344 hres = jsdisp_propget_name(arg_array, lengthW, &val);
348 hres = to_uint32(ctx, val, &length);
353 argv = heap_alloc(length * sizeof(*argv));
355 return E_OUTOFMEMORY;
357 for(i=0; i<length; i++) {
358 hres = jsdisp_get_idx(arg_array, i, argv+i);
359 if(hres == DISP_E_UNKNOWNNAME) {
360 argv[i] = jsval_undefined();
361 }else if(FAILED(hres)) {
363 jsval_release(argv[i]);
374 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
376 FunctionInstance *function;
377 jsval_t *args = NULL;
379 IDispatch *this_obj = NULL;
384 if(!(function = function_this(jsthis)))
385 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
388 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
389 hres = to_object(ctx, argv[0], &this_obj);
396 jsdisp_t *arg_array = NULL;
398 if(is_object_instance(argv[1])) {
399 arg_array = iface_to_jsdisp((IUnknown*)get_object(argv[1]));
401 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
402 jsdisp_release(arg_array);
408 hres = array_to_args(ctx, arg_array, &cnt, &args);
409 jsdisp_release(arg_array);
411 FIXME("throw TypeError\n");
417 hres = call_function(ctx, function, this_obj, cnt, args, r);
420 IDispatch_Release(this_obj);
421 for(i=0; i < cnt; i++)
422 jsval_release(args[i]);
427 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
430 FunctionInstance *function;
431 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);
450 hres = call_function(ctx, function, this_obj, cnt, argv+1, r);
453 IDispatch_Release(this_obj);
457 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
460 FunctionInstance *function;
464 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
465 ERR("dispex is not a function\n");
469 function = (FunctionInstance*)jsthis->u.jsdisp;
472 case DISPATCH_METHOD:
473 assert(function->value_proc != NULL);
474 return invoke_value_proc(ctx, function, NULL, flags, argc, argv, r);
476 case DISPATCH_PROPERTYGET: {
480 hres = function_to_string(function, &str);
484 *r = jsval_string(str);
488 case DISPATCH_CONSTRUCT:
489 assert(function->value_proc != NULL);
490 return invoke_value_proc(ctx, function, NULL, flags, argc, argv, r);
493 FIXME("not implemented flags %x\n", flags);
500 static HRESULT Function_arguments(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
501 unsigned argc, jsval_t *argv, jsval_t *r)
503 FunctionInstance *function = (FunctionInstance*)jsthis->u.jsdisp;
509 case DISPATCH_PROPERTYGET: {
510 *r = function->arguments ? jsval_obj(jsdisp_addref(function->arguments)) : jsval_null();
513 case DISPATCH_PROPERTYPUT:
516 FIXME("unimplemented flags %x\n", flags);
523 static void Function_destructor(jsdisp_t *dispex)
525 FunctionInstance *This = (FunctionInstance*)dispex;
528 release_bytecode(This->code);
529 if(This->scope_chain)
530 scope_release(This->scope_chain);
534 static const builtin_prop_t Function_props[] = {
535 {applyW, Function_apply, PROPF_METHOD|2},
536 {argumentsW, Function_arguments, 0},
537 {callW, Function_call, PROPF_METHOD|1},
538 {lengthW, Function_length, 0},
539 {toStringW, Function_toString, PROPF_METHOD}
542 static const builtin_info_t Function_info = {
544 {NULL, Function_value, 0},
545 sizeof(Function_props)/sizeof(*Function_props),
551 static const builtin_prop_t FunctionInst_props[] = {
552 {argumentsW, Function_arguments, 0},
553 {lengthW, Function_length, 0}
556 static const builtin_info_t FunctionInst_info = {
558 {NULL, Function_value, 0},
559 sizeof(FunctionInst_props)/sizeof(*FunctionInst_props),
565 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
566 BOOL funcprot, jsdisp_t *prototype, FunctionInstance **ret)
568 FunctionInstance *function;
571 function = heap_alloc_zero(sizeof(FunctionInstance));
573 return E_OUTOFMEMORY;
576 hres = init_dispex(&function->dispex, ctx, builtin_info, prototype);
577 else if(builtin_info)
578 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
580 hres = init_dispex_from_constr(&function->dispex, ctx, &FunctionInst_info, ctx->function_constr);
584 function->flags = flags;
585 function->length = flags & PROPF_ARGMASK;
591 static inline HRESULT set_prototype(script_ctx_t *ctx, jsdisp_t *dispex, jsdisp_t *prototype)
593 return jsdisp_propput_name(dispex, prototypeW, jsval_obj(prototype));
596 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
597 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
599 FunctionInstance *function;
602 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
607 hres = jsdisp_propput_const(&function->dispex, lengthW, jsval_number(function->length));
609 hres = set_prototype(ctx, &function->dispex, prototype);
611 jsdisp_release(&function->dispex);
615 function->value_proc = value_proc;
616 function->name = name;
618 *ret = &function->dispex;
622 static HRESULT set_constructor_prop(script_ctx_t *ctx, jsdisp_t *constr, jsdisp_t *prot)
624 static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
626 return jsdisp_propput_dontenum(prot, constructorW, jsval_obj(constr));
629 HRESULT create_builtin_constructor(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
630 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
635 hres = create_builtin_function(ctx, value_proc, name, builtin_info, flags, prototype, &constr);
639 hres = set_constructor_prop(ctx, constr, prototype);
641 jsdisp_release(constr);
649 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
650 scope_chain_t *scope_chain, jsdisp_t **ret)
652 FunctionInstance *function;
656 hres = create_object(ctx, NULL, &prototype);
660 hres = create_function(ctx, NULL, PROPF_CONSTR, FALSE, NULL, &function);
661 if(SUCCEEDED(hres)) {
662 hres = set_prototype(ctx, &function->dispex, prototype);
664 hres = set_constructor_prop(ctx, &function->dispex, prototype);
666 jsdisp_release(&function->dispex);
668 jsdisp_release(prototype);
673 scope_addref(scope_chain);
674 function->scope_chain = scope_chain;
677 bytecode_addref(code);
678 function->code = code;
679 function->func_code = func_code;
680 function->length = function->func_code->param_cnt;
682 *ret = &function->dispex;
686 static HRESULT construct_function(script_ctx_t *ctx, unsigned argc, jsval_t *argv, IDispatch **ret)
688 WCHAR *str = NULL, *ptr;
696 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
697 static const WCHAR function_beginW[] = {')',' ','{','\n'};
698 static const WCHAR function_endW[] = {'\n','}',0};
701 params = heap_alloc(argc*sizeof(BSTR));
703 return E_OUTOFMEMORY;
706 len = (argc-2)*2; /* separating commas */
707 for(i=0; i < argc; i++) {
708 hres = to_string(ctx, argv[i], params+i);
711 len += SysStringLen(params[i]);
715 if(SUCCEEDED(hres)) {
716 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
717 str = heap_alloc(len*sizeof(WCHAR));
719 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
720 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
723 l = SysStringLen(params[j]);
724 memcpy(ptr, params[j], l*sizeof(WCHAR));
732 memcpy(ptr, function_beginW, sizeof(function_beginW));
733 ptr += sizeof(function_beginW)/sizeof(WCHAR);
735 l = SysStringLen(params[argc-1]);
736 memcpy(ptr, params[argc-1], l*sizeof(WCHAR));
739 memcpy(ptr, function_endW, sizeof(function_endW));
741 TRACE("%s\n", debugstr_w(str));
743 hres = E_OUTOFMEMORY;
748 SysFreeString(params[i]);
753 hres = compile_script(ctx, str, NULL, NULL, FALSE, FALSE, &code);
758 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt) {
759 ERR("Invalid parser result!\n");
760 release_bytecode(code);
764 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
765 release_bytecode(code);
769 *ret = to_disp(function);
773 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
781 case DISPATCH_CONSTRUCT: {
784 hres = construct_function(ctx, argc, argv, &ret);
788 *r = jsval_disp(ret);
792 FIXME("unimplemented flags %x\n", flags);
799 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
806 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
808 FunctionInstance *prot, *constr;
811 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
813 hres = create_function(ctx, &Function_info, PROPF_CONSTR, TRUE, object_prototype, &prot);
817 prot->value_proc = FunctionProt_value;
818 prot->name = prototypeW;
820 hres = create_function(ctx, &FunctionInst_info, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
821 if(SUCCEEDED(hres)) {
822 constr->value_proc = FunctionConstr_value;
823 constr->name = FunctionW;
824 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
826 hres = set_constructor_prop(ctx, &constr->dispex, &prot->dispex);
828 jsdisp_release(&constr->dispex);
830 jsdisp_release(&prot->dispex);
834 ctx->function_constr = &constr->dispex;