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
++) {
109 static const WCHAR formatW
[] = {'%','d',0};
111 sprintfW(buf
, formatW
, i
);
112 hres
= jsdisp_propput_dontenum(args
, buf
, argv
[i
]);
117 if(SUCCEEDED(hres
)) {
118 hres
= jsdisp_propput_dontenum(args
, lengthW
, jsval_number(argc
));
121 hres
= jsdisp_propput_dontenum(args
, caleeW
, jsval_disp(calee
));
125 jsdisp_release(args
);
133 static HRESULT
create_var_disp(script_ctx_t
*ctx
, FunctionInstance
*function
, jsdisp_t
*arg_disp
,
134 unsigned argc
, jsval_t
*argv
, jsdisp_t
**ret
)
139 hres
= create_dispex(ctx
, NULL
, NULL
, &var_disp
);
143 hres
= jsdisp_propput_name(var_disp
, argumentsW
, jsval_obj(arg_disp
));
145 hres
= init_parameters(var_disp
, function
, argc
, argv
);
147 jsdisp_release(var_disp
);
155 static HRESULT
invoke_source(script_ctx_t
*ctx
, FunctionInstance
*function
, IDispatch
*this_obj
, unsigned argc
, jsval_t
*argv
,
158 jsdisp_t
*var_disp
, *arg_disp
;
159 exec_ctx_t
*exec_ctx
;
160 scope_chain_t
*scope
;
163 if(!function
->func_code
) {
164 FIXME("no source\n");
168 hres
= create_arguments(ctx
, to_disp(&function
->dispex
), argc
, argv
, &arg_disp
);
172 hres
= create_var_disp(ctx
, function
, arg_disp
, argc
, argv
, &var_disp
);
174 jsdisp_release(arg_disp
);
178 hres
= scope_push(function
->scope_chain
, var_disp
, to_disp(var_disp
), &scope
);
179 if(SUCCEEDED(hres
)) {
180 hres
= create_exec_ctx(ctx
, this_obj
, var_disp
, scope
, FALSE
, &exec_ctx
);
181 scope_release(scope
);
183 jsdisp_release(var_disp
);
184 if(SUCCEEDED(hres
)) {
187 prev_args
= function
->arguments
;
188 function
->arguments
= arg_disp
;
189 hres
= exec_source(exec_ctx
, function
->code
, function
->func_code
, FALSE
, r
);
190 function
->arguments
= prev_args
;
193 jsdisp_release(arg_disp
);
194 exec_release(exec_ctx
);
198 static HRESULT
invoke_constructor(script_ctx_t
*ctx
, FunctionInstance
*function
, unsigned argc
, jsval_t
*argv
,
205 hres
= create_object(ctx
, &function
->dispex
, &this_obj
);
209 hres
= invoke_source(ctx
, function
, to_disp(this_obj
), argc
, argv
, &var
);
211 jsdisp_release(this_obj
);
215 if(is_object_instance(var
)) {
216 jsdisp_release(this_obj
);
220 *r
= jsval_obj(this_obj
);
225 static HRESULT
invoke_value_proc(script_ctx_t
*ctx
, FunctionInstance
*function
, IDispatch
*this_disp
, WORD flags
,
226 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
232 set_disp(&vthis
, this_disp
);
233 else if(ctx
->host_global
)
234 set_disp(&vthis
, ctx
->host_global
);
236 set_jsdisp(&vthis
, ctx
->global
);
238 hres
= function
->value_proc(ctx
, &vthis
, flags
, argc
, argv
, r
);
240 vdisp_release(&vthis
);
244 static HRESULT
call_function(script_ctx_t
*ctx
, FunctionInstance
*function
, IDispatch
*this_obj
,
245 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
247 if(function
->value_proc
)
248 return invoke_value_proc(ctx
, function
, this_obj
, DISPATCH_METHOD
, argc
, argv
, r
);
250 return invoke_source(ctx
, function
, this_obj
, argc
, argv
, r
);
253 static HRESULT
function_to_string(FunctionInstance
*function
, jsstr_t
**ret
)
257 static const WCHAR native_prefixW
[] = {'\n','f','u','n','c','t','i','o','n',' '};
258 static const WCHAR native_suffixW
[] =
259 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
261 if(function
->value_proc
) {
264 name_len
= strlenW(function
->name
);
265 str
= jsstr_alloc_buf((sizeof(native_prefixW
)+sizeof(native_suffixW
))/sizeof(WCHAR
) + name_len
);
267 return E_OUTOFMEMORY
;
269 memcpy(str
->str
, native_prefixW
, sizeof(native_prefixW
));
270 memcpy(str
->str
+ sizeof(native_prefixW
)/sizeof(WCHAR
), function
->name
, name_len
*sizeof(WCHAR
));
271 memcpy(str
->str
+ sizeof(native_prefixW
)/sizeof(WCHAR
) + name_len
, native_suffixW
, sizeof(native_suffixW
));
273 str
= jsstr_alloc_len(function
->func_code
->source
, function
->func_code
->source_len
);
275 return E_OUTOFMEMORY
;
282 HRESULT
Function_invoke(jsdisp_t
*func_this
, IDispatch
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
284 FunctionInstance
*function
;
286 TRACE("func %p this %p\n", func_this
, jsthis
);
288 assert(is_class(func_this
, JSCLASS_FUNCTION
));
289 function
= (FunctionInstance
*)func_this
;
291 if(function
->value_proc
)
292 return invoke_value_proc(function
->dispex
.ctx
, function
, jsthis
, flags
, argc
, argv
, r
);
294 if(flags
== DISPATCH_CONSTRUCT
)
295 return invoke_constructor(function
->dispex
.ctx
, function
, argc
, argv
, r
);
297 assert(flags
== DISPATCH_METHOD
);
298 return invoke_source(function
->dispex
.ctx
, function
, jsthis
, argc
, argv
, r
);
301 static HRESULT
Function_length(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
304 FunctionInstance
*This
= function_from_vdisp(jsthis
);
306 TRACE("%p %d\n", This
, This
->length
);
309 case DISPATCH_PROPERTYGET
:
310 *r
= jsval_number(This
->length
);
313 FIXME("unimplemented flags %x\n", flags
);
320 static HRESULT
Function_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
323 FunctionInstance
*function
;
329 if(!(function
= function_this(jsthis
)))
330 return throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
332 hres
= function_to_string(function
, &str
);
337 *r
= jsval_string(str
);
343 static HRESULT
array_to_args(script_ctx_t
*ctx
, jsdisp_t
*arg_array
, unsigned *argc
, jsval_t
**ret
)
349 hres
= jsdisp_propget_name(arg_array
, lengthW
, &val
);
353 hres
= to_uint32(ctx
, val
, &length
);
358 argv
= heap_alloc(length
* sizeof(*argv
));
360 return E_OUTOFMEMORY
;
362 for(i
=0; i
<length
; i
++) {
363 hres
= jsdisp_get_idx(arg_array
, i
, argv
+i
);
364 if(hres
== DISP_E_UNKNOWNNAME
) {
365 argv
[i
] = jsval_undefined();
366 }else if(FAILED(hres
)) {
368 jsval_release(argv
[i
]);
379 static HRESULT
Function_apply(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
381 FunctionInstance
*function
;
382 jsval_t
*args
= NULL
;
384 IDispatch
*this_obj
= NULL
;
389 if(!(function
= function_this(jsthis
)))
390 return throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
393 if(!is_undefined(argv
[0]) && !is_null(argv
[0])) {
394 hres
= to_object(ctx
, argv
[0], &this_obj
);
401 jsdisp_t
*arg_array
= NULL
;
403 if(is_object_instance(argv
[1])) {
404 arg_array
= iface_to_jsdisp((IUnknown
*)get_object(argv
[1]));
406 (!is_class(arg_array
, JSCLASS_ARRAY
) && !is_class(arg_array
, JSCLASS_ARGUMENTS
) )) {
407 jsdisp_release(arg_array
);
413 hres
= array_to_args(ctx
, arg_array
, &cnt
, &args
);
414 jsdisp_release(arg_array
);
416 FIXME("throw TypeError\n");
422 hres
= call_function(ctx
, function
, this_obj
, cnt
, args
, r
);
425 IDispatch_Release(this_obj
);
426 for(i
=0; i
< cnt
; i
++)
427 jsval_release(args
[i
]);
432 static HRESULT
Function_call(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
435 FunctionInstance
*function
;
436 IDispatch
*this_obj
= NULL
;
442 if(!(function
= function_this(jsthis
)))
443 return throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
446 if(!is_undefined(argv
[0]) && !is_null(argv
[0])) {
447 hres
= to_object(ctx
, argv
[0], &this_obj
);
455 hres
= call_function(ctx
, function
, this_obj
, cnt
, argv
+1, r
);
458 IDispatch_Release(this_obj
);
462 HRESULT
Function_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
465 FunctionInstance
*function
;
469 if(!is_vclass(jsthis
, JSCLASS_FUNCTION
)) {
470 ERR("dispex is not a function\n");
474 function
= (FunctionInstance
*)jsthis
->u
.jsdisp
;
477 case DISPATCH_METHOD
:
478 assert(function
->value_proc
!= NULL
);
479 return invoke_value_proc(ctx
, function
, NULL
, flags
, argc
, argv
, r
);
481 case DISPATCH_PROPERTYGET
: {
485 hres
= function_to_string(function
, &str
);
489 *r
= jsval_string(str
);
493 case DISPATCH_CONSTRUCT
:
494 assert(function
->value_proc
!= NULL
);
495 return invoke_value_proc(ctx
, function
, NULL
, flags
, argc
, argv
, r
);
498 FIXME("not implemented flags %x\n", flags
);
505 static HRESULT
Function_arguments(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
,
506 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
508 FunctionInstance
*function
= (FunctionInstance
*)jsthis
->u
.jsdisp
;
514 case DISPATCH_PROPERTYGET
: {
515 *r
= function
->arguments
? jsval_obj(jsdisp_addref(function
->arguments
)) : jsval_null();
518 case DISPATCH_PROPERTYPUT
:
521 FIXME("unimplemented flags %x\n", flags
);
528 static void Function_destructor(jsdisp_t
*dispex
)
530 FunctionInstance
*This
= (FunctionInstance
*)dispex
;
533 release_bytecode(This
->code
);
534 if(This
->scope_chain
)
535 scope_release(This
->scope_chain
);
539 static const builtin_prop_t Function_props
[] = {
540 {applyW
, Function_apply
, PROPF_METHOD
|2},
541 {argumentsW
, Function_arguments
, 0},
542 {callW
, Function_call
, PROPF_METHOD
|1},
543 {lengthW
, Function_length
, 0},
544 {toStringW
, Function_toString
, PROPF_METHOD
}
547 static const builtin_info_t Function_info
= {
549 {NULL
, Function_value
, 0},
550 sizeof(Function_props
)/sizeof(*Function_props
),
556 static const builtin_prop_t FunctionInst_props
[] = {
557 {argumentsW
, Function_arguments
, 0},
558 {lengthW
, Function_length
, 0}
561 static const builtin_info_t FunctionInst_info
= {
563 {NULL
, Function_value
, 0},
564 sizeof(FunctionInst_props
)/sizeof(*FunctionInst_props
),
570 static HRESULT
create_function(script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, DWORD flags
,
571 BOOL funcprot
, jsdisp_t
*prototype
, FunctionInstance
**ret
)
573 FunctionInstance
*function
;
576 function
= heap_alloc_zero(sizeof(FunctionInstance
));
578 return E_OUTOFMEMORY
;
581 hres
= init_dispex(&function
->dispex
, ctx
, builtin_info
, prototype
);
582 else if(builtin_info
)
583 hres
= init_dispex_from_constr(&function
->dispex
, ctx
, builtin_info
, ctx
->function_constr
);
585 hres
= init_dispex_from_constr(&function
->dispex
, ctx
, &FunctionInst_info
, ctx
->function_constr
);
591 function
->flags
= flags
;
592 function
->length
= flags
& PROPF_ARGMASK
;
598 static inline HRESULT
set_prototype(script_ctx_t
*ctx
, jsdisp_t
*dispex
, jsdisp_t
*prototype
)
600 return jsdisp_propput_dontenum(dispex
, prototypeW
, jsval_obj(prototype
));
603 HRESULT
create_builtin_function(script_ctx_t
*ctx
, builtin_invoke_t value_proc
, const WCHAR
*name
,
604 const builtin_info_t
*builtin_info
, DWORD flags
, jsdisp_t
*prototype
, jsdisp_t
**ret
)
606 FunctionInstance
*function
;
609 hres
= create_function(ctx
, builtin_info
, flags
, FALSE
, NULL
, &function
);
614 hres
= jsdisp_propput_const(&function
->dispex
, lengthW
, jsval_number(function
->length
));
616 hres
= set_prototype(ctx
, &function
->dispex
, prototype
);
618 jsdisp_release(&function
->dispex
);
622 function
->value_proc
= value_proc
;
623 function
->name
= name
;
625 *ret
= &function
->dispex
;
629 static HRESULT
set_constructor_prop(script_ctx_t
*ctx
, jsdisp_t
*constr
, jsdisp_t
*prot
)
631 static const WCHAR constructorW
[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
633 return jsdisp_propput_dontenum(prot
, constructorW
, jsval_obj(constr
));
636 HRESULT
create_builtin_constructor(script_ctx_t
*ctx
, builtin_invoke_t value_proc
, const WCHAR
*name
,
637 const builtin_info_t
*builtin_info
, DWORD flags
, jsdisp_t
*prototype
, jsdisp_t
**ret
)
642 hres
= create_builtin_function(ctx
, value_proc
, name
, builtin_info
, flags
, prototype
, &constr
);
646 hres
= set_constructor_prop(ctx
, constr
, prototype
);
648 jsdisp_release(constr
);
656 HRESULT
create_source_function(script_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func_code
,
657 scope_chain_t
*scope_chain
, jsdisp_t
**ret
)
659 FunctionInstance
*function
;
663 hres
= create_object(ctx
, NULL
, &prototype
);
667 hres
= create_function(ctx
, NULL
, PROPF_CONSTR
, FALSE
, NULL
, &function
);
668 if(SUCCEEDED(hres
)) {
669 hres
= set_prototype(ctx
, &function
->dispex
, prototype
);
671 hres
= set_constructor_prop(ctx
, &function
->dispex
, prototype
);
673 jsdisp_release(&function
->dispex
);
675 jsdisp_release(prototype
);
680 scope_addref(scope_chain
);
681 function
->scope_chain
= scope_chain
;
684 bytecode_addref(code
);
685 function
->code
= code
;
686 function
->func_code
= func_code
;
687 function
->length
= function
->func_code
->param_cnt
;
689 *ret
= &function
->dispex
;
693 static HRESULT
construct_function(script_ctx_t
*ctx
, unsigned argc
, jsval_t
*argv
, IDispatch
**ret
)
695 WCHAR
*str
= NULL
, *ptr
;
699 jsstr_t
**params
= NULL
;
703 static const WCHAR function_anonymousW
[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
704 static const WCHAR function_beginW
[] = {')',' ','{','\n'};
705 static const WCHAR function_endW
[] = {'\n','}',0};
708 params
= heap_alloc(argc
*sizeof(*params
));
710 return E_OUTOFMEMORY
;
713 len
= (argc
-2)*2; /* separating commas */
714 for(i
=0; i
< argc
; i
++) {
715 hres
= to_string(ctx
, argv
[i
], params
+i
);
718 len
+= jsstr_length(params
[i
]);
722 if(SUCCEEDED(hres
)) {
723 len
+= (sizeof(function_anonymousW
) + sizeof(function_beginW
) + sizeof(function_endW
)) / sizeof(WCHAR
);
724 str
= heap_alloc(len
*sizeof(WCHAR
));
726 memcpy(str
, function_anonymousW
, sizeof(function_anonymousW
));
727 ptr
= str
+ sizeof(function_anonymousW
)/sizeof(WCHAR
);
730 l
= jsstr_length(params
[j
]);
731 memcpy(ptr
, params
[j
]->str
, l
*sizeof(WCHAR
));
739 memcpy(ptr
, function_beginW
, sizeof(function_beginW
));
740 ptr
+= sizeof(function_beginW
)/sizeof(WCHAR
);
742 l
= jsstr_length(params
[argc
-1]);
743 memcpy(ptr
, params
[argc
-1]->str
, l
*sizeof(WCHAR
));
746 memcpy(ptr
, function_endW
, sizeof(function_endW
));
748 TRACE("%s\n", debugstr_w(str
));
750 hres
= E_OUTOFMEMORY
;
755 jsstr_release(params
[i
]);
760 hres
= compile_script(ctx
, str
, NULL
, NULL
, FALSE
, FALSE
, &code
);
765 if(code
->global_code
.func_cnt
!= 1 || code
->global_code
.var_cnt
) {
766 ERR("Invalid parser result!\n");
767 release_bytecode(code
);
771 hres
= create_source_function(ctx
, code
, code
->global_code
.funcs
, NULL
, &function
);
772 release_bytecode(code
);
776 *ret
= to_disp(function
);
780 static HRESULT
FunctionConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
788 case DISPATCH_CONSTRUCT
: {
791 hres
= construct_function(ctx
, argc
, argv
, &ret
);
795 *r
= jsval_disp(ret
);
799 FIXME("unimplemented flags %x\n", flags
);
806 static HRESULT
FunctionProt_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
813 HRESULT
init_function_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
)
815 FunctionInstance
*prot
, *constr
;
818 static const WCHAR FunctionW
[] = {'F','u','n','c','t','i','o','n',0};
820 hres
= create_function(ctx
, &Function_info
, PROPF_CONSTR
, TRUE
, object_prototype
, &prot
);
824 prot
->value_proc
= FunctionProt_value
;
825 prot
->name
= prototypeW
;
827 hres
= create_function(ctx
, &FunctionInst_info
, PROPF_CONSTR
|1, TRUE
, &prot
->dispex
, &constr
);
828 if(SUCCEEDED(hres
)) {
829 constr
->value_proc
= FunctionConstr_value
;
830 constr
->name
= FunctionW
;
831 hres
= set_prototype(ctx
, &constr
->dispex
, &prot
->dispex
);
833 hres
= set_constructor_prop(ctx
, &constr
->dispex
, &prot
->dispex
);
835 jsdisp_release(&constr
->dispex
);
837 jsdisp_release(&prot
->dispex
);
841 ctx
->function_constr
= &constr
->dispex
;