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)) {
184 prev_args = function->arguments;
185 function->arguments = arg_disp;
186 hres = exec_source(exec_ctx, function->code, function->func_code, FALSE, ei, r);
187 function->arguments = prev_args;
190 jsdisp_release(arg_disp);
191 exec_release(exec_ctx);
195 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, unsigned argc, jsval_t *argv,
196 jsval_t *r, jsexcept_t *ei)
202 hres = create_object(ctx, &function->dispex, &this_obj);
206 hres = invoke_source(ctx, function, to_disp(this_obj), argc, argv, &var, ei);
208 jsdisp_release(this_obj);
212 if(is_object_instance(var)) {
213 jsdisp_release(this_obj);
217 *r = jsval_obj(this_obj);
222 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags, unsigned argc, jsval_t *argv,
223 jsval_t *r, jsexcept_t *ei)
229 set_disp(&vthis, this_disp);
230 else if(ctx->host_global)
231 set_disp(&vthis, ctx->host_global);
233 set_jsdisp(&vthis, ctx->global);
235 hres = function->value_proc(ctx, &vthis, flags, argc, argv, r, ei);
237 vdisp_release(&vthis);
241 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj,
242 unsigned argc, jsval_t *argv, jsval_t *r, jsexcept_t *ei)
244 if(function->value_proc)
245 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, argc, argv, r, ei);
247 return invoke_source(ctx, function, this_obj, argc, argv, r, ei);
250 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
254 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
255 static const WCHAR native_suffixW[] =
256 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
258 if(function->value_proc) {
261 name_len = strlenW(function->name);
262 str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
264 return E_OUTOFMEMORY;
266 memcpy(str, native_prefixW, sizeof(native_prefixW));
267 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
268 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
270 str = SysAllocStringLen(function->func_code->source, function->func_code->source_len);
272 return E_OUTOFMEMORY;
279 HRESULT Function_invoke(jsdisp_t *func_this, IDispatch *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r, jsexcept_t *ei)
281 FunctionInstance *function;
283 TRACE("func %p this %p\n", func_this, jsthis);
285 assert(is_class(func_this, JSCLASS_FUNCTION));
286 function = (FunctionInstance*)func_this;
288 if(function->value_proc)
289 return invoke_value_proc(function->dispex.ctx, function, jsthis, flags, argc, argv, r, ei);
291 if(flags == DISPATCH_CONSTRUCT)
292 return invoke_constructor(function->dispex.ctx, function, argc, argv, r, ei);
294 assert(flags == DISPATCH_METHOD);
295 return invoke_source(function->dispex.ctx, function, jsthis, argc, argv, r, ei);
298 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
299 jsval_t *r, jsexcept_t *ei)
301 FunctionInstance *This = function_from_vdisp(jsthis);
303 TRACE("%p %d\n", This, This->length);
306 case DISPATCH_PROPERTYGET:
307 *r = jsval_number(This->length);
310 FIXME("unimplemented flags %x\n", flags);
317 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
318 jsval_t *r, jsexcept_t *ei)
320 FunctionInstance *function;
326 if(!(function = function_this(jsthis)))
327 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
329 hres = function_to_string(function, &str);
334 *r = jsval_string(str);
340 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, jsexcept_t *ei, unsigned *argc, jsval_t **ret)
346 hres = jsdisp_propget_name(arg_array, lengthW, &val, ei);
350 hres = to_uint32(ctx, val, ei, &length);
355 argv = heap_alloc(length * sizeof(*argv));
357 return E_OUTOFMEMORY;
359 for(i=0; i<length; i++) {
360 hres = jsdisp_get_idx(arg_array, i, argv+i, ei);
361 if(hres == DISP_E_UNKNOWNNAME) {
362 argv[i] = jsval_undefined();
363 }else if(FAILED(hres)) {
365 jsval_release(argv[i]);
376 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
377 jsval_t *r, jsexcept_t *ei)
379 FunctionInstance *function;
380 jsval_t *args = NULL;
382 IDispatch *this_obj = NULL;
387 if(!(function = function_this(jsthis)))
388 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
391 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
392 hres = to_object_jsval(ctx, argv[0], &this_obj);
399 jsdisp_t *arg_array = NULL;
401 if(is_object_instance(argv[1])) {
402 arg_array = iface_to_jsdisp((IUnknown*)get_object(argv[1]));
404 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
405 jsdisp_release(arg_array);
411 hres = array_to_args(ctx, arg_array, ei, &cnt, &args);
412 jsdisp_release(arg_array);
414 FIXME("throw TypeError\n");
420 hres = call_function(ctx, function, this_obj, cnt, args, r, ei);
423 IDispatch_Release(this_obj);
424 for(i=0; i < cnt; i++)
425 jsval_release(args[i]);
430 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
431 jsval_t *r, jsexcept_t *ei)
433 FunctionInstance *function;
434 IDispatch *this_obj = NULL;
440 if(!(function = function_this(jsthis)))
441 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
444 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
445 hres = to_object_jsval(ctx, argv[0], &this_obj);
453 hres = call_function(ctx, function, this_obj, cnt, argv+1, r, ei);
456 IDispatch_Release(this_obj);
460 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
461 jsval_t *r, jsexcept_t *ei)
463 FunctionInstance *function;
467 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
468 ERR("dispex is not a function\n");
472 function = (FunctionInstance*)jsthis->u.jsdisp;
475 case DISPATCH_METHOD:
476 assert(function->value_proc != NULL);
477 return invoke_value_proc(ctx, function, NULL, flags, argc, argv, r, ei);
479 case DISPATCH_PROPERTYGET: {
483 hres = function_to_string(function, &str);
487 *r = jsval_string(str);
491 case DISPATCH_CONSTRUCT:
492 assert(function->value_proc != NULL);
493 return invoke_value_proc(ctx, function, NULL, flags, argc, argv, r, ei);
496 FIXME("not implemented flags %x\n", flags);
503 static HRESULT Function_arguments(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
504 unsigned argc, jsval_t *argv, jsval_t *r, jsexcept_t *ei)
506 FunctionInstance *function = (FunctionInstance*)jsthis->u.jsdisp;
512 case DISPATCH_PROPERTYGET: {
513 *r = function->arguments ? jsval_obj(jsdisp_addref(function->arguments)) : jsval_null();
516 case DISPATCH_PROPERTYPUT:
519 FIXME("unimplemented flags %x\n", flags);
526 static void Function_destructor(jsdisp_t *dispex)
528 FunctionInstance *This = (FunctionInstance*)dispex;
531 release_bytecode(This->code);
532 if(This->scope_chain)
533 scope_release(This->scope_chain);
537 static const builtin_prop_t Function_props[] = {
538 {applyW, Function_apply, PROPF_METHOD|2},
539 {argumentsW, Function_arguments, 0},
540 {callW, Function_call, PROPF_METHOD|1},
541 {lengthW, Function_length, 0},
542 {toStringW, Function_toString, PROPF_METHOD}
545 static const builtin_info_t Function_info = {
547 {NULL, Function_value, 0},
548 sizeof(Function_props)/sizeof(*Function_props),
554 static const builtin_prop_t FunctionInst_props[] = {
555 {argumentsW, Function_arguments, 0},
556 {lengthW, Function_length, 0}
559 static const builtin_info_t FunctionInst_info = {
561 {NULL, Function_value, 0},
562 sizeof(FunctionInst_props)/sizeof(*FunctionInst_props),
568 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
569 BOOL funcprot, jsdisp_t *prototype, FunctionInstance **ret)
571 FunctionInstance *function;
574 function = heap_alloc_zero(sizeof(FunctionInstance));
576 return E_OUTOFMEMORY;
579 hres = init_dispex(&function->dispex, ctx, builtin_info, prototype);
580 else if(builtin_info)
581 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
583 hres = init_dispex_from_constr(&function->dispex, ctx, &FunctionInst_info, ctx->function_constr);
587 function->flags = flags;
588 function->length = flags & PROPF_ARGMASK;
594 static HRESULT set_prototype(script_ctx_t *ctx, jsdisp_t *dispex, jsdisp_t *prototype)
598 memset(&jsexcept, 0, sizeof(jsexcept));
599 return jsdisp_propput_name(dispex, prototypeW, jsval_obj(prototype), &jsexcept);
602 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
603 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
605 FunctionInstance *function;
608 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
613 hres = jsdisp_propput_const(&function->dispex, lengthW, jsval_number(function->length));
615 hres = set_prototype(ctx, &function->dispex, prototype);
617 jsdisp_release(&function->dispex);
621 function->value_proc = value_proc;
622 function->name = name;
624 *ret = &function->dispex;
628 static HRESULT set_constructor_prop(script_ctx_t *ctx, jsdisp_t *constr, jsdisp_t *prot)
630 static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
632 return jsdisp_propput_dontenum(prot, constructorW, jsval_obj(constr));
635 HRESULT create_builtin_constructor(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
636 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
641 hres = create_builtin_function(ctx, value_proc, name, builtin_info, flags, prototype, &constr);
645 hres = set_constructor_prop(ctx, constr, prototype);
647 jsdisp_release(constr);
655 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
656 scope_chain_t *scope_chain, jsdisp_t **ret)
658 FunctionInstance *function;
662 hres = create_object(ctx, NULL, &prototype);
666 hres = create_function(ctx, NULL, PROPF_CONSTR, FALSE, NULL, &function);
667 if(SUCCEEDED(hres)) {
668 hres = set_prototype(ctx, &function->dispex, prototype);
670 hres = set_constructor_prop(ctx, &function->dispex, prototype);
672 jsdisp_release(&function->dispex);
674 jsdisp_release(prototype);
679 scope_addref(scope_chain);
680 function->scope_chain = scope_chain;
683 bytecode_addref(code);
684 function->code = code;
685 function->func_code = func_code;
686 function->length = function->func_code->param_cnt;
688 *ret = &function->dispex;
692 static HRESULT construct_function(script_ctx_t *ctx, unsigned argc, jsval_t *argv, jsexcept_t *ei, IDispatch **ret)
694 WCHAR *str = NULL, *ptr;
702 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
703 static const WCHAR function_beginW[] = {')',' ','{','\n'};
704 static const WCHAR function_endW[] = {'\n','}',0};
707 params = heap_alloc(argc*sizeof(BSTR));
709 return E_OUTOFMEMORY;
712 len = (argc-2)*2; /* separating commas */
713 for(i=0; i < argc; i++) {
714 hres = to_string(ctx, argv[i], ei, params+i);
717 len += SysStringLen(params[i]);
721 if(SUCCEEDED(hres)) {
722 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
723 str = heap_alloc(len*sizeof(WCHAR));
725 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
726 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
729 l = SysStringLen(params[j]);
730 memcpy(ptr, params[j], l*sizeof(WCHAR));
738 memcpy(ptr, function_beginW, sizeof(function_beginW));
739 ptr += sizeof(function_beginW)/sizeof(WCHAR);
741 l = SysStringLen(params[argc-1]);
742 memcpy(ptr, params[argc-1], l*sizeof(WCHAR));
745 memcpy(ptr, function_endW, sizeof(function_endW));
747 TRACE("%s\n", debugstr_w(str));
749 hres = E_OUTOFMEMORY;
754 SysFreeString(params[i]);
759 hres = compile_script(ctx, str, NULL, FALSE, FALSE, &code);
764 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt) {
765 ERR("Invalid parser result!\n");
766 release_bytecode(code);
770 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
771 release_bytecode(code);
775 *ret = to_disp(function);
779 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
780 jsval_t *r, jsexcept_t *ei)
787 case DISPATCH_CONSTRUCT: {
790 hres = construct_function(ctx, argc, argv, ei, &ret);
794 *r = jsval_disp(ret);
798 FIXME("unimplemented flags %x\n", flags);
805 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
806 jsval_t *r, jsexcept_t *ei)
812 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
814 FunctionInstance *prot, *constr;
817 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
819 hres = create_function(ctx, &Function_info, PROPF_CONSTR, TRUE, object_prototype, &prot);
823 prot->value_proc = FunctionProt_value;
824 prot->name = prototypeW;
826 hres = create_function(ctx, &FunctionInst_info, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
827 if(SUCCEEDED(hres)) {
828 constr->value_proc = FunctionConstr_value;
829 constr->name = FunctionW;
830 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
832 hres = set_constructor_prop(ctx, &constr->dispex, &prot->dispex);
834 jsdisp_release(&constr->dispex);
836 jsdisp_release(&prot->dispex);
840 ctx->function_constr = &constr->dispex;