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
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28 builtin_invoke_t value_proc;
31 parameter_t *parameters;
32 scope_chain_t *scope_chain;
34 function_code_t *func_code;
39 static inline FunctionInstance *function_from_vdisp(vdisp_t *vdisp)
41 return (FunctionInstance*)vdisp->u.jsdisp;
44 static inline FunctionInstance *function_this(vdisp_t *jsthis)
46 return is_vclass(jsthis, JSCLASS_FUNCTION) ? function_from_vdisp(jsthis) : NULL;
49 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
51 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
52 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
53 static const WCHAR applyW[] = {'a','p','p','l','y',0};
54 static const WCHAR callW[] = {'c','a','l','l',0};
55 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
57 static IDispatch *get_this(DISPPARAMS *dp)
61 for(i=0; i < dp->cNamedArgs; i++) {
62 if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
63 if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
64 return V_DISPATCH(dp->rgvarg+i);
66 WARN("This is not VT_DISPATCH\n");
71 TRACE("no this passed\n");
75 static HRESULT init_parameters(jsdisp_t *var_disp, FunctionInstance *function, DISPPARAMS *dp,
83 V_VT(&var_empty) = VT_EMPTY;
86 for(param = function->parameters; param; param = param->next) {
87 hres = jsdisp_propput_name(var_disp, param->identifier,
88 i < cargs ? get_arg(dp,i) : &var_empty, ei);
98 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
99 VARIANT *retv, jsexcept_t *ei)
105 static const builtin_info_t Arguments_info = {
107 {NULL, Arguments_value, 0},
113 static HRESULT create_arguments(script_ctx_t *ctx, IDispatch *calee, DISPPARAMS *dp,
114 jsexcept_t *ei, jsdisp_t **ret)
121 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
123 args = heap_alloc_zero(sizeof(jsdisp_t));
125 return E_OUTOFMEMORY;
127 hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
133 for(i=0; i < arg_cnt(dp); i++) {
134 hres = jsdisp_propput_idx(args, i, get_arg(dp,i), ei);
139 if(SUCCEEDED(hres)) {
141 V_I4(&var) = arg_cnt(dp);
142 hres = jsdisp_propput_name(args, lengthW, &var, ei);
144 if(SUCCEEDED(hres)) {
145 V_VT(&var) = VT_DISPATCH;
146 V_DISPATCH(&var) = calee;
147 hres = jsdisp_propput_name(args, caleeW, &var, ei);
152 jsdisp_release(args);
160 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, jsdisp_t *arg_disp,
161 DISPPARAMS *dp, jsexcept_t *ei, jsdisp_t **ret)
167 hres = create_dispex(ctx, NULL, NULL, &var_disp);
171 var_set_jsdisp(&var, arg_disp);
172 hres = jsdisp_propput_name(var_disp, argumentsW, &var, ei);
174 hres = init_parameters(var_disp, function, dp, ei);
176 jsdisp_release(var_disp);
184 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *dp,
185 VARIANT *retv, jsexcept_t *ei)
187 jsdisp_t *var_disp, *arg_disp;
188 exec_ctx_t *exec_ctx;
189 scope_chain_t *scope;
192 if(!function->func_code) {
193 FIXME("no source\n");
197 hres = create_arguments(ctx, to_disp(&function->dispex), dp, ei, &arg_disp);
201 hres = create_var_disp(ctx, function, arg_disp, dp, ei, &var_disp);
203 jsdisp_release(arg_disp);
207 hres = scope_push(function->scope_chain, var_disp, &scope);
208 if(SUCCEEDED(hres)) {
209 hres = create_exec_ctx(ctx, this_obj, var_disp, scope, FALSE, &exec_ctx);
210 scope_release(scope);
212 jsdisp_release(var_disp);
213 if(SUCCEEDED(hres)) {
216 prev_args = function->arguments;
217 function->arguments = arg_disp;
218 hres = exec_source(exec_ctx, function->code, function->func_code, FALSE, ei, retv);
219 function->arguments = prev_args;
221 jsdisp_release(arg_disp);
222 exec_release(exec_ctx);
228 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp,
229 VARIANT *retv, jsexcept_t *ei)
235 hres = create_object(ctx, &function->dispex, &this_obj);
239 hres = invoke_source(ctx, function, to_disp(this_obj), dp, &var, ei);
241 jsdisp_release(this_obj);
245 if(V_VT(&var) == VT_DISPATCH) {
246 jsdisp_release(this_obj);
247 V_VT(retv) = VT_DISPATCH;
248 V_DISPATCH(retv) = V_DISPATCH(&var);
251 var_set_jsdisp(retv, this_obj);
256 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags, DISPPARAMS *dp,
257 VARIANT *retv, jsexcept_t *ei)
263 set_disp(&vthis, this_disp);
264 else if(ctx->host_global)
265 set_disp(&vthis, ctx->host_global);
267 set_jsdisp(&vthis, ctx->global);
269 hres = function->value_proc(ctx, &vthis, flags, dp, retv, ei);
271 vdisp_release(&vthis);
275 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *args,
276 VARIANT *retv, jsexcept_t *ei)
278 if(function->value_proc)
279 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, args, retv, ei);
281 return invoke_source(ctx, function, this_obj, args, retv, ei);
284 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
288 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
289 static const WCHAR native_suffixW[] =
290 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
292 if(function->value_proc) {
295 name_len = strlenW(function->name);
296 str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
298 return E_OUTOFMEMORY;
300 memcpy(str, native_prefixW, sizeof(native_prefixW));
301 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
302 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
304 str = SysAllocStringLen(function->func_code->source, function->func_code->source_len);
306 return E_OUTOFMEMORY;
313 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
314 VARIANT *retv, jsexcept_t *ei)
316 FunctionInstance *This = function_from_vdisp(jsthis);
318 TRACE("%p %d\n", This, This->length);
321 case DISPATCH_PROPERTYGET:
323 V_I4(retv) = This->length;
326 FIXME("unimplemented flags %x\n", flags);
333 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
334 VARIANT *retv, jsexcept_t *ei)
336 FunctionInstance *function;
342 if(!(function = function_this(jsthis)))
343 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
345 hres = function_to_string(function, &str);
350 V_VT(retv) = VT_BSTR;
358 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, jsexcept_t *ei, DISPPARAMS *args)
364 hres = jsdisp_propget_name(arg_array, lengthW, &var, ei);
368 hres = to_uint32(ctx, &var, ei, &length);
373 argv = heap_alloc(length * sizeof(VARIANT));
375 return E_OUTOFMEMORY;
377 for(i=0; i<length; i++) {
378 hres = jsdisp_get_idx(arg_array, i, argv+i, ei);
379 if(hres == DISP_E_UNKNOWNNAME)
380 V_VT(argv+i) = VT_EMPTY;
381 else if(FAILED(hres)) {
383 VariantClear(argv+i);
389 args->cArgs = length;
394 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
395 VARIANT *retv, jsexcept_t *ei)
397 FunctionInstance *function;
398 DISPPARAMS args = {NULL,NULL,0,0};
400 IDispatch *this_obj = NULL;
405 if(!(function = function_this(jsthis)))
406 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
410 VARIANT *v = get_arg(dp,0);
412 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
413 hres = to_object(ctx, v, &this_obj);
420 jsdisp_t *arg_array = NULL;
422 if(V_VT(get_arg(dp,1)) == VT_DISPATCH) {
423 arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(get_arg(dp,1)));
425 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
426 jsdisp_release(arg_array);
432 hres = array_to_args(ctx, arg_array, ei, &args);
433 jsdisp_release(arg_array);
435 FIXME("throw TypeError\n");
441 hres = call_function(ctx, function, this_obj, &args, retv, ei);
444 IDispatch_Release(this_obj);
445 for(i=0; i<args.cArgs; i++)
446 VariantClear(args.rgvarg+i);
447 heap_free(args.rgvarg);
451 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
452 VARIANT *retv, jsexcept_t *ei)
454 FunctionInstance *function;
455 DISPPARAMS args = {NULL,NULL,0,0};
456 IDispatch *this_obj = NULL;
462 if(!(function = function_this(jsthis)))
463 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
467 VARIANT *v = get_arg(dp,0);
469 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
470 hres = to_object(ctx, v, &this_obj);
479 args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1;
481 hres = call_function(ctx, function, this_obj, &args, retv, ei);
484 IDispatch_Release(this_obj);
488 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
489 VARIANT *retv, jsexcept_t *ei)
491 FunctionInstance *function;
495 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
496 ERR("dispex is not a function\n");
500 function = (FunctionInstance*)jsthis->u.jsdisp;
503 case DISPATCH_METHOD:
504 if(function->value_proc)
505 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei);
507 return invoke_source(ctx, function, get_this(dp), dp, retv, ei);
509 case DISPATCH_PROPERTYGET: {
513 hres = function_to_string(function, &str);
517 V_VT(retv) = VT_BSTR;
522 case DISPATCH_CONSTRUCT:
523 if(function->value_proc)
524 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei);
526 return invoke_constructor(ctx, function, dp, retv, ei);
529 FIXME("not implemented flags %x\n", flags);
536 static HRESULT Function_arguments(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
537 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
539 FunctionInstance *function = (FunctionInstance*)jsthis->u.jsdisp;
545 case DISPATCH_PROPERTYGET: {
546 if(function->arguments) {
547 jsdisp_addref(function->arguments);
548 var_set_jsdisp(retv, function->arguments);
550 V_VT(retv) = VT_NULL;
554 case DISPATCH_PROPERTYPUT:
557 FIXME("unimplemented flags %x\n", flags);
564 static void Function_destructor(jsdisp_t *dispex)
566 FunctionInstance *This = (FunctionInstance*)dispex;
569 release_bytecode(This->code);
570 if(This->scope_chain)
571 scope_release(This->scope_chain);
575 static const builtin_prop_t Function_props[] = {
576 {applyW, Function_apply, PROPF_METHOD|2},
577 {argumentsW, Function_arguments, 0},
578 {callW, Function_call, PROPF_METHOD|1},
579 {lengthW, Function_length, 0},
580 {toStringW, Function_toString, PROPF_METHOD}
583 static const builtin_info_t Function_info = {
585 {NULL, Function_value, 0},
586 sizeof(Function_props)/sizeof(*Function_props),
592 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
593 BOOL funcprot, jsdisp_t *prototype, FunctionInstance **ret)
595 FunctionInstance *function;
598 function = heap_alloc_zero(sizeof(FunctionInstance));
600 return E_OUTOFMEMORY;
603 hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
604 else if(builtin_info)
605 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
607 hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
611 function->flags = flags;
612 function->length = flags & PROPF_ARGMASK;
618 static HRESULT set_prototype(script_ctx_t *ctx, jsdisp_t *dispex, jsdisp_t *prototype)
623 var_set_jsdisp(&var, prototype);
624 memset(&jsexcept, 0, sizeof(jsexcept));
626 return jsdisp_propput_name(dispex, prototypeW, &var, &jsexcept);
629 HRESULT create_builtin_function(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)
632 FunctionInstance *function;
635 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
643 V_I4(&var) = function->length;
644 hres = jsdisp_propput_const(&function->dispex, lengthW, &var);
648 hres = set_prototype(ctx, &function->dispex, prototype);
650 jsdisp_release(&function->dispex);
654 function->value_proc = value_proc;
655 function->name = name;
657 *ret = &function->dispex;
661 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, parameter_t *parameters, function_code_t *func_code,
662 scope_chain_t *scope_chain, jsdisp_t **ret)
664 FunctionInstance *function;
670 hres = create_object(ctx, NULL, &prototype);
674 hres = create_function(ctx, NULL, PROPF_CONSTR, FALSE, NULL, &function);
675 if(SUCCEEDED(hres)) {
676 hres = set_prototype(ctx, &function->dispex, prototype);
678 jsdisp_release(&function->dispex);
680 jsdisp_release(prototype);
684 function->func_code = func_code;
685 function->parameters = parameters;
688 scope_addref(scope_chain);
689 function->scope_chain = scope_chain;
692 bytecode_addref(code);
693 function->code = code;
695 for(iter = parameters; iter; iter = iter->next)
697 function->length = length;
699 *ret = &function->dispex;
703 static HRESULT construct_function(script_ctx_t *ctx, DISPPARAMS *dp, jsexcept_t *ei, IDispatch **ret)
705 function_expression_t *expr;
706 WCHAR *str = NULL, *ptr;
707 DWORD argc, len = 0, l;
714 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
715 static const WCHAR function_beginW[] = {')',' ','{','\n'};
716 static const WCHAR function_endW[] = {'\n','}',0};
720 params = heap_alloc(argc*sizeof(BSTR));
722 return E_OUTOFMEMORY;
725 len = (argc-2)*2; /* separating commas */
726 for(i=0; i < argc; i++) {
727 hres = to_string(ctx, get_arg(dp,i), ei, params+i);
730 len += SysStringLen(params[i]);
734 if(SUCCEEDED(hres)) {
735 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
736 str = heap_alloc(len*sizeof(WCHAR));
738 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
739 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
742 l = SysStringLen(params[j]);
743 memcpy(ptr, params[j], l*sizeof(WCHAR));
751 memcpy(ptr, function_beginW, sizeof(function_beginW));
752 ptr += sizeof(function_beginW)/sizeof(WCHAR);
754 l = SysStringLen(params[argc-1]);
755 memcpy(ptr, params[argc-1], l*sizeof(WCHAR));
758 memcpy(ptr, function_endW, sizeof(function_endW));
760 TRACE("%s\n", debugstr_w(str));
762 hres = E_OUTOFMEMORY;
767 SysFreeString(params[i]);
772 hres = compile_script(ctx, str, NULL, FALSE, FALSE, &code);
777 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt) {
778 ERR("Invalid parser result!\n");
779 release_bytecode(code);
782 expr = code->global_code.funcs[0].expr;
784 hres = create_source_function(ctx, code, expr->parameter_list, 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, DISPPARAMS *dp,
794 VARIANT *retv, jsexcept_t *ei)
801 case DISPATCH_CONSTRUCT: {
804 hres = construct_function(ctx, dp, ei, &ret);
808 V_VT(retv) = VT_DISPATCH;
809 V_DISPATCH(retv) = ret;
813 FIXME("unimplemented flags %x\n", flags);
820 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
821 VARIANT *retv, jsexcept_t *ei)
827 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
829 FunctionInstance *prot, *constr;
832 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
834 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
838 prot->value_proc = FunctionProt_value;
839 prot->name = prototypeW;
841 hres = create_function(ctx, NULL, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
842 if(SUCCEEDED(hres)) {
843 constr->value_proc = FunctionConstr_value;
844 constr->name = FunctionW;
845 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
847 jsdisp_release(&constr->dispex);
849 jsdisp_release(&prot->dispex);
853 ctx->function_constr = &constr->dispex;