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, VARIANT *argv,
65 V_VT(&var_empty) = VT_EMPTY;
67 for(i=0; i < function->func_code->param_cnt; i++) {
68 hres = jsdisp_propput_name(var_disp, function->func_code->params[i],
69 i < argc ? argv+i : &var_empty, ei);
77 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
78 VARIANT *retv, jsexcept_t *ei)
84 static const builtin_info_t Arguments_info = {
86 {NULL, Arguments_value, 0},
92 static HRESULT create_arguments(script_ctx_t *ctx, IDispatch *calee, unsigned argc, VARIANT *argv,
93 jsexcept_t *ei, jsdisp_t **ret)
100 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
102 args = heap_alloc_zero(sizeof(jsdisp_t));
104 return E_OUTOFMEMORY;
106 hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
112 for(i=0; i < argc; i++) {
113 hres = jsdisp_propput_idx(args, i, argv+i, ei);
118 if(SUCCEEDED(hres)) {
119 num_set_int(&var, argc);
120 hres = jsdisp_propput_name(args, lengthW, &var, ei);
122 if(SUCCEEDED(hres)) {
123 V_VT(&var) = VT_DISPATCH;
124 V_DISPATCH(&var) = calee;
125 hres = jsdisp_propput_name(args, caleeW, &var, ei);
130 jsdisp_release(args);
138 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, jsdisp_t *arg_disp,
139 unsigned argc, VARIANT *argv, jsexcept_t *ei, jsdisp_t **ret)
145 hres = create_dispex(ctx, NULL, NULL, &var_disp);
149 var_set_jsdisp(&var, arg_disp);
150 hres = jsdisp_propput_name(var_disp, argumentsW, &var, ei);
152 hres = init_parameters(var_disp, function, argc, argv, ei);
154 jsdisp_release(var_disp);
162 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, unsigned argc, VARIANT *argv,
163 VARIANT *retv, jsexcept_t *ei)
165 jsdisp_t *var_disp, *arg_disp;
166 exec_ctx_t *exec_ctx;
167 scope_chain_t *scope;
170 if(!function->func_code) {
171 FIXME("no source\n");
175 hres = create_arguments(ctx, to_disp(&function->dispex), argc, argv, ei, &arg_disp);
179 hres = create_var_disp(ctx, function, arg_disp, argc, argv, ei, &var_disp);
181 jsdisp_release(arg_disp);
185 hres = scope_push(function->scope_chain, var_disp, &scope);
186 if(SUCCEEDED(hres)) {
187 hres = create_exec_ctx(ctx, this_obj, var_disp, scope, FALSE, &exec_ctx);
188 scope_release(scope);
190 jsdisp_release(var_disp);
191 if(SUCCEEDED(hres)) {
194 prev_args = function->arguments;
195 function->arguments = arg_disp;
196 hres = exec_source(exec_ctx, function->code, function->func_code, FALSE, ei, retv);
197 function->arguments = prev_args;
199 jsdisp_release(arg_disp);
200 exec_release(exec_ctx);
206 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, unsigned argc, VARIANT *argv,
207 VARIANT *retv, jsexcept_t *ei)
213 hres = create_object(ctx, &function->dispex, &this_obj);
217 hres = invoke_source(ctx, function, to_disp(this_obj), argc, argv, &var, ei);
219 jsdisp_release(this_obj);
223 if(V_VT(&var) == VT_DISPATCH) {
224 jsdisp_release(this_obj);
225 V_VT(retv) = VT_DISPATCH;
226 V_DISPATCH(retv) = V_DISPATCH(&var);
229 var_set_jsdisp(retv, this_obj);
234 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags, unsigned argc, VARIANT *argv,
235 VARIANT *retv, jsexcept_t *ei)
241 set_disp(&vthis, this_disp);
242 else if(ctx->host_global)
243 set_disp(&vthis, ctx->host_global);
245 set_jsdisp(&vthis, ctx->global);
247 hres = function->value_proc(ctx, &vthis, flags, argc, argv, retv, ei);
249 vdisp_release(&vthis);
253 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj,
254 unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei)
256 if(function->value_proc)
257 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, argc, argv, retv, ei);
259 return invoke_source(ctx, function, this_obj, argc, argv, retv, ei);
262 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
266 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
267 static const WCHAR native_suffixW[] =
268 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
270 if(function->value_proc) {
273 name_len = strlenW(function->name);
274 str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
276 return E_OUTOFMEMORY;
278 memcpy(str, native_prefixW, sizeof(native_prefixW));
279 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
280 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
282 str = SysAllocStringLen(function->func_code->source, function->func_code->source_len);
284 return E_OUTOFMEMORY;
291 HRESULT Function_invoke(jsdisp_t *func_this, IDispatch *jsthis, WORD flags, unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei)
293 FunctionInstance *function;
295 TRACE("func %p this %p\n", func_this, jsthis);
297 assert(is_class(func_this, JSCLASS_FUNCTION));
298 function = (FunctionInstance*)func_this;
300 if(function->value_proc)
301 return invoke_value_proc(function->dispex.ctx, function, jsthis, flags, argc, argv, retv, ei);
303 if(flags == DISPATCH_CONSTRUCT)
304 return invoke_constructor(function->dispex.ctx, function, argc, argv, retv, ei);
306 assert(flags == DISPATCH_METHOD);
307 return invoke_source(function->dispex.ctx, function, jsthis, argc, argv, retv, ei);
310 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
311 VARIANT *retv, jsexcept_t *ei)
313 FunctionInstance *This = function_from_vdisp(jsthis);
315 TRACE("%p %d\n", This, This->length);
318 case DISPATCH_PROPERTYGET:
319 num_set_int(retv, This->length);
322 FIXME("unimplemented flags %x\n", flags);
329 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
330 VARIANT *retv, jsexcept_t *ei)
332 FunctionInstance *function;
338 if(!(function = function_this(jsthis)))
339 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
341 hres = function_to_string(function, &str);
346 V_VT(retv) = VT_BSTR;
354 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, jsexcept_t *ei, unsigned *argc, VARIANT **ret)
360 hres = jsdisp_propget_name(arg_array, lengthW, &var, ei);
364 hres = to_uint32(ctx, &var, ei, &length);
369 argv = heap_alloc(length * sizeof(VARIANT));
371 return E_OUTOFMEMORY;
373 for(i=0; i<length; i++) {
374 hres = jsdisp_get_idx(arg_array, i, argv+i, ei);
375 if(hres == DISP_E_UNKNOWNNAME)
376 V_VT(argv+i) = VT_EMPTY;
377 else if(FAILED(hres)) {
379 VariantClear(argv+i);
390 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
391 VARIANT *retv, jsexcept_t *ei)
393 FunctionInstance *function;
394 VARIANT *args = NULL;
396 IDispatch *this_obj = NULL;
401 if(!(function = function_this(jsthis)))
402 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
405 if(V_VT(argv) != VT_EMPTY && V_VT(argv) != VT_NULL) {
406 hres = to_object(ctx, argv, &this_obj);
413 jsdisp_t *arg_array = NULL;
415 if(V_VT(argv+1) == VT_DISPATCH) {
416 arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(argv+1));
418 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
419 jsdisp_release(arg_array);
425 hres = array_to_args(ctx, arg_array, ei, &cnt, &args);
426 jsdisp_release(arg_array);
428 FIXME("throw TypeError\n");
434 hres = call_function(ctx, function, this_obj, cnt, args, retv, ei);
437 IDispatch_Release(this_obj);
438 for(i=0; i < cnt; i++)
439 VariantClear(args+i);
444 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
445 VARIANT *retv, jsexcept_t *ei)
447 FunctionInstance *function;
448 IDispatch *this_obj = NULL;
454 if(!(function = function_this(jsthis)))
455 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
458 if(V_VT(argv) != VT_EMPTY && V_VT(argv) != VT_NULL) {
459 hres = to_object(ctx, argv, &this_obj);
467 hres = call_function(ctx, function, this_obj, cnt, argv+1, retv, ei);
470 IDispatch_Release(this_obj);
474 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
475 VARIANT *retv, jsexcept_t *ei)
477 FunctionInstance *function;
481 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
482 ERR("dispex is not a function\n");
486 function = (FunctionInstance*)jsthis->u.jsdisp;
489 case DISPATCH_METHOD:
490 assert(function->value_proc != NULL);
491 return invoke_value_proc(ctx, function, NULL, flags, argc, argv, retv, ei);
493 case DISPATCH_PROPERTYGET: {
497 hres = function_to_string(function, &str);
501 V_VT(retv) = VT_BSTR;
506 case DISPATCH_CONSTRUCT:
507 assert(function->value_proc != NULL);
508 return invoke_value_proc(ctx, function, NULL, flags, argc, argv, retv, ei);
511 FIXME("not implemented flags %x\n", flags);
518 static HRESULT Function_arguments(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
519 unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei)
521 FunctionInstance *function = (FunctionInstance*)jsthis->u.jsdisp;
527 case DISPATCH_PROPERTYGET: {
528 if(function->arguments) {
529 jsdisp_addref(function->arguments);
530 var_set_jsdisp(retv, function->arguments);
532 V_VT(retv) = VT_NULL;
536 case DISPATCH_PROPERTYPUT:
539 FIXME("unimplemented flags %x\n", flags);
546 static void Function_destructor(jsdisp_t *dispex)
548 FunctionInstance *This = (FunctionInstance*)dispex;
551 release_bytecode(This->code);
552 if(This->scope_chain)
553 scope_release(This->scope_chain);
557 static const builtin_prop_t Function_props[] = {
558 {applyW, Function_apply, PROPF_METHOD|2},
559 {argumentsW, Function_arguments, 0},
560 {callW, Function_call, PROPF_METHOD|1},
561 {lengthW, Function_length, 0},
562 {toStringW, Function_toString, PROPF_METHOD}
565 static const builtin_info_t Function_info = {
567 {NULL, Function_value, 0},
568 sizeof(Function_props)/sizeof(*Function_props),
574 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
575 BOOL funcprot, jsdisp_t *prototype, FunctionInstance **ret)
577 FunctionInstance *function;
580 function = heap_alloc_zero(sizeof(FunctionInstance));
582 return E_OUTOFMEMORY;
585 hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
586 else if(builtin_info)
587 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
589 hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
593 function->flags = flags;
594 function->length = flags & PROPF_ARGMASK;
600 static HRESULT set_prototype(script_ctx_t *ctx, jsdisp_t *dispex, jsdisp_t *prototype)
605 var_set_jsdisp(&var, prototype);
606 memset(&jsexcept, 0, sizeof(jsexcept));
608 return jsdisp_propput_name(dispex, prototypeW, &var, &jsexcept);
611 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
612 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
614 FunctionInstance *function;
617 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
624 num_set_int(&var, function->length);
625 hres = jsdisp_propput_const(&function->dispex, lengthW, &var);
629 hres = set_prototype(ctx, &function->dispex, prototype);
631 jsdisp_release(&function->dispex);
635 function->value_proc = value_proc;
636 function->name = name;
638 *ret = &function->dispex;
642 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
643 scope_chain_t *scope_chain, jsdisp_t **ret)
645 FunctionInstance *function;
649 hres = create_object(ctx, NULL, &prototype);
653 hres = create_function(ctx, NULL, PROPF_CONSTR, FALSE, NULL, &function);
654 if(SUCCEEDED(hres)) {
655 hres = set_prototype(ctx, &function->dispex, prototype);
657 jsdisp_release(&function->dispex);
659 jsdisp_release(prototype);
664 scope_addref(scope_chain);
665 function->scope_chain = scope_chain;
668 bytecode_addref(code);
669 function->code = code;
670 function->func_code = func_code;
671 function->length = function->func_code->param_cnt;
673 *ret = &function->dispex;
677 static HRESULT construct_function(script_ctx_t *ctx, unsigned argc, VARIANT *argv, jsexcept_t *ei, IDispatch **ret)
679 WCHAR *str = NULL, *ptr;
687 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
688 static const WCHAR function_beginW[] = {')',' ','{','\n'};
689 static const WCHAR function_endW[] = {'\n','}',0};
692 params = heap_alloc(argc*sizeof(BSTR));
694 return E_OUTOFMEMORY;
697 len = (argc-2)*2; /* separating commas */
698 for(i=0; i < argc; i++) {
699 hres = to_string(ctx, argv+i, ei, params+i);
702 len += SysStringLen(params[i]);
706 if(SUCCEEDED(hres)) {
707 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
708 str = heap_alloc(len*sizeof(WCHAR));
710 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
711 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
714 l = SysStringLen(params[j]);
715 memcpy(ptr, params[j], l*sizeof(WCHAR));
723 memcpy(ptr, function_beginW, sizeof(function_beginW));
724 ptr += sizeof(function_beginW)/sizeof(WCHAR);
726 l = SysStringLen(params[argc-1]);
727 memcpy(ptr, params[argc-1], l*sizeof(WCHAR));
730 memcpy(ptr, function_endW, sizeof(function_endW));
732 TRACE("%s\n", debugstr_w(str));
734 hres = E_OUTOFMEMORY;
739 SysFreeString(params[i]);
744 hres = compile_script(ctx, str, NULL, FALSE, FALSE, &code);
749 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt) {
750 ERR("Invalid parser result!\n");
751 release_bytecode(code);
755 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
756 release_bytecode(code);
760 *ret = to_disp(function);
764 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
765 VARIANT *retv, jsexcept_t *ei)
772 case DISPATCH_CONSTRUCT: {
775 hres = construct_function(ctx, argc, argv, ei, &ret);
779 V_VT(retv) = VT_DISPATCH;
780 V_DISPATCH(retv) = ret;
784 FIXME("unimplemented flags %x\n", flags);
791 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
792 VARIANT *retv, jsexcept_t *ei)
798 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
800 FunctionInstance *prot, *constr;
803 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
805 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
809 prot->value_proc = FunctionProt_value;
810 prot->name = prototypeW;
812 hres = create_function(ctx, NULL, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
813 if(SUCCEEDED(hres)) {
814 constr->value_proc = FunctionConstr_value;
815 constr->name = FunctionW;
816 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
818 jsdisp_release(&constr->dispex);
820 jsdisp_release(&prot->dispex);
824 ctx->function_constr = &constr->dispex;