jscript: Use wide-char string literals.
[wine.git] / dlls / jscript / function.c
blob9f6aa4b4ec6bac331f787ae270207fbb6930a611
1 /*
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
19 #include <assert.h>
21 #include "jscript.h"
22 #include "engine.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28 typedef struct _function_vtbl_t function_vtbl_t;
30 typedef struct {
31 jsdisp_t dispex;
32 const function_vtbl_t *vtbl;
33 DWORD flags;
34 DWORD length;
35 } FunctionInstance;
37 struct _function_vtbl_t {
38 HRESULT (*call)(script_ctx_t*,FunctionInstance*,IDispatch*,unsigned,unsigned,jsval_t*,jsval_t*);
39 HRESULT (*toString)(FunctionInstance*,jsstr_t**);
40 function_code_t* (*get_code)(FunctionInstance*);
41 void (*destructor)(FunctionInstance*);
44 typedef struct {
45 FunctionInstance function;
46 scope_chain_t *scope_chain;
47 bytecode_t *code;
48 function_code_t *func_code;
49 } InterpretedFunction;
51 typedef struct {
52 FunctionInstance function;
53 builtin_invoke_t proc;
54 const WCHAR *name;
55 } NativeFunction;
57 typedef struct {
58 FunctionInstance function;
59 FunctionInstance *target;
60 IDispatch *this;
61 unsigned argc;
62 jsval_t args[1];
63 } BindFunction;
65 typedef struct {
66 jsdisp_t jsdisp;
67 InterpretedFunction *function;
68 jsval_t *buf;
69 call_frame_t *frame;
70 unsigned argc;
71 } ArgumentsInstance;
73 static HRESULT create_bind_function(script_ctx_t*,FunctionInstance*,IDispatch*,unsigned,jsval_t*,jsdisp_t**r);
75 static inline FunctionInstance *function_from_jsdisp(jsdisp_t *jsdisp)
77 return CONTAINING_RECORD(jsdisp, FunctionInstance, dispex);
80 static inline FunctionInstance *function_from_vdisp(vdisp_t *vdisp)
82 return function_from_jsdisp(vdisp->u.jsdisp);
85 static inline FunctionInstance *function_this(vdisp_t *jsthis)
87 return is_vclass(jsthis, JSCLASS_FUNCTION) ? function_from_vdisp(jsthis) : NULL;
90 static inline ArgumentsInstance *arguments_from_jsdisp(jsdisp_t *jsdisp)
92 return CONTAINING_RECORD(jsdisp, ArgumentsInstance, jsdisp);
95 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
96 jsval_t *r)
98 FIXME("\n");
99 return E_NOTIMPL;
102 static void Arguments_destructor(jsdisp_t *jsdisp)
104 ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
106 TRACE("(%p)\n", arguments);
108 if(arguments->buf) {
109 unsigned i;
110 for(i = 0; i < arguments->argc; i++)
111 jsval_release(arguments->buf[i]);
112 heap_free(arguments->buf);
115 jsdisp_release(&arguments->function->function.dispex);
116 heap_free(arguments);
119 static unsigned Arguments_idx_length(jsdisp_t *jsdisp)
121 ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
122 return arguments->argc;
125 static jsval_t *get_argument_ref(ArgumentsInstance *arguments, unsigned idx)
127 if(arguments->buf)
128 return arguments->buf + idx;
129 if(arguments->frame->base_scope->frame || idx >= arguments->frame->function->param_cnt)
130 return arguments->jsdisp.ctx->stack + arguments->frame->arguments_off + idx;
131 return NULL;
134 static HRESULT Arguments_idx_get(jsdisp_t *jsdisp, unsigned idx, jsval_t *r)
136 ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
137 jsval_t *ref;
139 TRACE("%p[%u]\n", arguments, idx);
141 if((ref = get_argument_ref(arguments, idx)))
142 return jsval_copy(*ref, r);
144 /* FIXME: Accessing by name won't work for duplicated argument names */
145 return jsdisp_propget_name(arguments->frame->base_scope->jsobj,
146 arguments->function->func_code->params[idx], r);
149 static HRESULT Arguments_idx_put(jsdisp_t *jsdisp, unsigned idx, jsval_t val)
151 ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
152 jsval_t *ref;
153 HRESULT hres;
155 TRACE("%p[%u] = %s\n", arguments, idx, debugstr_jsval(val));
157 if((ref = get_argument_ref(arguments, idx))) {
158 jsval_t copy;
159 hres = jsval_copy(val, &copy);
160 if(FAILED(hres))
161 return hres;
163 jsval_release(*ref);
164 *ref = copy;
165 return S_OK;
168 /* FIXME: Accessing by name won't work for duplicated argument names */
169 return jsdisp_propput_name(arguments->frame->base_scope->jsobj,
170 arguments->function->func_code->params[idx], val);
173 static const builtin_info_t Arguments_info = {
174 JSCLASS_ARGUMENTS,
175 {NULL, Arguments_value, 0},
176 0, NULL,
177 Arguments_destructor,
178 NULL,
179 Arguments_idx_length,
180 Arguments_idx_get,
181 Arguments_idx_put
184 HRESULT setup_arguments_object(script_ctx_t *ctx, call_frame_t *frame)
186 ArgumentsInstance *args;
187 HRESULT hres;
189 args = heap_alloc_zero(sizeof(*args));
190 if(!args)
191 return E_OUTOFMEMORY;
193 hres = init_dispex_from_constr(&args->jsdisp, ctx, &Arguments_info, ctx->object_constr);
194 if(FAILED(hres)) {
195 heap_free(args);
196 return hres;
199 args->function = (InterpretedFunction*)function_from_jsdisp(jsdisp_addref(frame->function_instance));
200 args->argc = frame->argc;
201 args->frame = frame;
203 hres = jsdisp_define_data_property(&args->jsdisp, L"length", PROPF_WRITABLE | PROPF_CONFIGURABLE,
204 jsval_number(args->argc));
205 if(SUCCEEDED(hres))
206 hres = jsdisp_define_data_property(&args->jsdisp, L"callee", PROPF_WRITABLE | PROPF_CONFIGURABLE,
207 jsval_obj(&args->function->function.dispex));
208 if(SUCCEEDED(hres))
209 hres = jsdisp_propput(frame->base_scope->jsobj, L"arguments", PROPF_WRITABLE, jsval_obj(&args->jsdisp));
210 if(FAILED(hres)) {
211 jsdisp_release(&args->jsdisp);
212 return hres;
215 frame->arguments_obj = &args->jsdisp;
216 return S_OK;
219 void detach_arguments_object(jsdisp_t *args_disp)
221 ArgumentsInstance *arguments = arguments_from_jsdisp(args_disp);
222 call_frame_t *frame = arguments->frame;
223 const BOOL on_stack = frame->base_scope->frame == frame;
224 HRESULT hres;
226 /* Reset arguments value to cut the reference cycle. Note that since all activation contexts have
227 * their own arguments property, it's impossible to use prototype's one during name lookup */
228 jsdisp_propput_name(frame->base_scope->jsobj, L"arguments", jsval_undefined());
229 arguments->frame = NULL;
231 /* Don't bother coppying arguments if call frame holds the last reference. */
232 if(arguments->jsdisp.ref > 1) {
233 arguments->buf = heap_alloc(arguments->argc * sizeof(*arguments->buf));
234 if(arguments->buf) {
235 int i;
237 for(i = 0; i < arguments->argc ; i++) {
238 if(on_stack || i >= frame->function->param_cnt)
239 hres = jsval_copy(arguments->jsdisp.ctx->stack[frame->arguments_off + i], arguments->buf+i);
240 else
241 hres = jsdisp_propget_name(frame->base_scope->jsobj, frame->function->params[i], arguments->buf+i);
242 if(FAILED(hres))
243 arguments->buf[i] = jsval_undefined();
245 }else {
246 ERR("out of memory\n");
247 arguments->argc = 0;
251 jsdisp_release(frame->arguments_obj);
254 HRESULT Function_invoke(jsdisp_t *func_this, IDispatch *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
256 FunctionInstance *function;
258 TRACE("func %p this %p\n", func_this, jsthis);
260 assert(is_class(func_this, JSCLASS_FUNCTION));
261 function = function_from_jsdisp(func_this);
263 return function->vtbl->call(function->dispex.ctx, function, jsthis, flags, argc, argv, r);
266 static HRESULT Function_get_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
268 TRACE("%p\n", jsthis);
270 *r = jsval_number(function_from_jsdisp(jsthis)->length);
271 return S_OK;
274 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
275 jsval_t *r)
277 FunctionInstance *function;
278 jsstr_t *str;
279 HRESULT hres;
281 TRACE("\n");
283 if(!(function = function_this(jsthis)))
284 return JS_E_FUNCTION_EXPECTED;
286 hres = function->vtbl->toString(function, &str);
287 if(FAILED(hres))
288 return hres;
290 if(r)
291 *r = jsval_string(str);
292 else
293 jsstr_release(str);
294 return S_OK;
297 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, unsigned *argc, jsval_t **ret)
299 jsval_t *argv, val;
300 DWORD length, i;
301 HRESULT hres;
303 hres = jsdisp_propget_name(arg_array, L"length", &val);
304 if(FAILED(hres))
305 return hres;
307 hres = to_uint32(ctx, val, &length);
308 jsval_release(val);
309 if(FAILED(hres))
310 return hres;
312 argv = heap_alloc(length * sizeof(*argv));
313 if(!argv)
314 return E_OUTOFMEMORY;
316 for(i=0; i<length; i++) {
317 hres = jsdisp_get_idx(arg_array, i, argv+i);
318 if(hres == DISP_E_UNKNOWNNAME) {
319 argv[i] = jsval_undefined();
320 }else if(FAILED(hres)) {
321 while(i--)
322 jsval_release(argv[i]);
323 heap_free(argv);
324 return hres;
328 *argc = length;
329 *ret = argv;
330 return S_OK;
333 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
335 FunctionInstance *function;
336 jsval_t *args = NULL;
337 unsigned i, cnt = 0;
338 IDispatch *this_obj = NULL;
339 HRESULT hres = S_OK;
341 TRACE("\n");
343 if(!(function = function_this(jsthis)) && (jsthis->flags & VDISP_JSDISP))
344 return JS_E_FUNCTION_EXPECTED;
346 if(argc) {
347 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
348 hres = to_object(ctx, argv[0], &this_obj);
349 if(FAILED(hres))
350 return hres;
354 if(argc >= 2) {
355 jsdisp_t *arg_array = NULL;
357 if(is_object_instance(argv[1])) {
358 arg_array = iface_to_jsdisp(get_object(argv[1]));
359 if(arg_array &&
360 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
361 jsdisp_release(arg_array);
362 arg_array = NULL;
366 if(arg_array) {
367 hres = array_to_args(ctx, arg_array, &cnt, &args);
368 jsdisp_release(arg_array);
369 }else {
370 FIXME("throw TypeError\n");
371 hres = E_FAIL;
375 if(SUCCEEDED(hres)) {
376 if(function) {
377 hres = function->vtbl->call(ctx, function, this_obj, flags, cnt, args, r);
378 }else {
379 jsval_t res;
380 hres = disp_call_value(ctx, jsthis->u.disp, this_obj, DISPATCH_METHOD, cnt, args, &res);
381 if(SUCCEEDED(hres)) {
382 if(r)
383 *r = res;
384 else
385 jsval_release(res);
390 if(this_obj)
391 IDispatch_Release(this_obj);
392 for(i=0; i < cnt; i++)
393 jsval_release(args[i]);
394 heap_free(args);
395 return hres;
398 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
399 jsval_t *r)
401 FunctionInstance *function;
402 IDispatch *this_obj = NULL;
403 unsigned cnt = 0;
404 HRESULT hres;
406 TRACE("\n");
408 if(!(function = function_this(jsthis)))
409 return JS_E_FUNCTION_EXPECTED;
411 if(argc) {
412 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
413 hres = to_object(ctx, argv[0], &this_obj);
414 if(FAILED(hres))
415 return hres;
418 cnt = argc-1;
421 hres = function->vtbl->call(ctx, function, this_obj, flags, cnt, argv + 1, r);
423 if(this_obj)
424 IDispatch_Release(this_obj);
425 return hres;
428 static HRESULT Function_bind(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
429 jsval_t *r)
431 IDispatch *bound_this = NULL;
432 FunctionInstance *function;
433 jsdisp_t *new_function;
434 HRESULT hres;
436 TRACE("\n");
438 if(!(function = function_this(jsthis)))
439 return JS_E_FUNCTION_EXPECTED;
441 if(argc < 1) {
442 FIXME("no this argument\n");
443 return E_NOTIMPL;
446 if(is_object_instance(argv[0])) {
447 bound_this = get_object(argv[0]);
448 }else if(!is_null(argv[0])) {
449 FIXME("%s is not an object instance\n", debugstr_jsval(argv[0]));
450 return E_NOTIMPL;
453 hres = create_bind_function(ctx, function, bound_this, argc - 1, argv + 1, &new_function);
454 if(FAILED(hres))
455 return hres;
457 if(r)
458 *r = jsval_obj(new_function);
459 else
460 jsdisp_release(new_function);
461 return S_OK;
464 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
465 jsval_t *r)
467 FunctionInstance *function;
469 TRACE("\n");
471 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
472 ERR("dispex is not a function\n");
473 return E_FAIL;
476 function = function_from_jsdisp(jsthis->u.jsdisp);
477 return function->vtbl->call(ctx, function, NULL, flags, argc, argv, r);
480 HRESULT Function_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
482 FunctionInstance *function = function_from_jsdisp(jsthis);
483 jsstr_t *str;
484 HRESULT hres;
486 TRACE("\n");
488 hres = function->vtbl->toString(function, &str);
489 if(FAILED(hres))
490 return hres;
492 *r = jsval_string(str);
493 return S_OK;
496 static HRESULT Function_get_arguments(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
498 FunctionInstance *function = function_from_jsdisp(jsthis);
499 call_frame_t *frame;
500 HRESULT hres;
502 TRACE("\n");
504 for(frame = ctx->call_ctx; frame; frame = frame->prev_frame) {
505 if(frame->function_instance == &function->dispex) {
506 if(!frame->arguments_obj) {
507 hres = setup_arguments_object(ctx, frame);
508 if(FAILED(hres))
509 return hres;
511 *r = jsval_obj(jsdisp_addref(frame->arguments_obj));
512 return S_OK;
516 *r = jsval_null();
517 return S_OK;
520 function_code_t *Function_get_code(jsdisp_t *jsthis)
522 FunctionInstance *function;
524 assert(is_class(jsthis, JSCLASS_FUNCTION));
525 function = function_from_jsdisp(jsthis);
527 return function->vtbl->get_code(function);
530 static void Function_destructor(jsdisp_t *dispex)
532 FunctionInstance *function = function_from_jsdisp(dispex);
533 function->vtbl->destructor(function);
534 heap_free(function);
537 static const builtin_prop_t Function_props[] = {
538 {L"apply", Function_apply, PROPF_METHOD|2},
539 {L"arguments", NULL, 0, Function_get_arguments},
540 {L"bind", Function_bind, PROPF_METHOD|PROPF_ES5|1},
541 {L"call", Function_call, PROPF_METHOD|1},
542 {L"length", NULL, 0, Function_get_length},
543 {L"toString", Function_toString, PROPF_METHOD}
546 static const builtin_info_t Function_info = {
547 JSCLASS_FUNCTION,
548 DEFAULT_FUNCTION_VALUE,
549 ARRAY_SIZE(Function_props),
550 Function_props,
551 Function_destructor,
552 NULL
555 static const builtin_prop_t FunctionInst_props[] = {
556 {L"arguments", NULL, 0, Function_get_arguments},
557 {L"length", NULL, 0, Function_get_length}
560 static const builtin_info_t FunctionInst_info = {
561 JSCLASS_FUNCTION,
562 DEFAULT_FUNCTION_VALUE,
563 ARRAY_SIZE(FunctionInst_props),
564 FunctionInst_props,
565 Function_destructor,
566 NULL
569 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, const function_vtbl_t *vtbl, size_t size,
570 DWORD flags, BOOL funcprot, jsdisp_t *prototype, void **ret)
572 FunctionInstance *function;
573 HRESULT hres;
575 function = heap_alloc_zero(size);
576 if(!function)
577 return E_OUTOFMEMORY;
579 if(funcprot)
580 hres = init_dispex(&function->dispex, ctx, builtin_info, prototype);
581 else if(builtin_info)
582 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
583 else
584 hres = init_dispex_from_constr(&function->dispex, ctx, &FunctionInst_info, ctx->function_constr);
585 if(FAILED(hres)) {
586 heap_free(function);
587 return hres;
590 function->vtbl = vtbl;
591 function->flags = flags;
592 function->length = flags & PROPF_ARGMASK;
594 *ret = function;
595 return S_OK;
598 static HRESULT NativeFunction_call(script_ctx_t *ctx, FunctionInstance *func, IDispatch *this_disp, unsigned flags,
599 unsigned argc, jsval_t *argv, jsval_t *r)
601 NativeFunction *function = (NativeFunction*)func;
602 vdisp_t vthis;
603 HRESULT hres;
605 if(this_disp)
606 set_disp(&vthis, this_disp);
607 else
608 set_disp(&vthis, lookup_global_host(ctx));
610 hres = function->proc(ctx, &vthis, flags & ~DISPATCH_JSCRIPT_INTERNAL_MASK, argc, argv, r);
612 vdisp_release(&vthis);
613 return hres;
616 static HRESULT NativeFunction_toString(FunctionInstance *func, jsstr_t **ret)
618 NativeFunction *function = (NativeFunction*)func;
619 DWORD name_len;
620 jsstr_t *str;
621 WCHAR *ptr;
623 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
624 static const WCHAR native_suffixW[] =
625 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
627 name_len = function->name ? lstrlenW(function->name) : 0;
628 str = jsstr_alloc_buf(ARRAY_SIZE(native_prefixW) + ARRAY_SIZE(native_suffixW) + name_len, &ptr);
629 if(!str)
630 return E_OUTOFMEMORY;
632 memcpy(ptr, native_prefixW, sizeof(native_prefixW));
633 ptr += ARRAY_SIZE(native_prefixW);
634 memcpy(ptr, function->name, name_len*sizeof(WCHAR));
635 ptr += name_len;
636 memcpy(ptr, native_suffixW, sizeof(native_suffixW));
638 *ret = str;
639 return S_OK;
642 static function_code_t *NativeFunction_get_code(FunctionInstance *function)
644 return NULL;
647 static void NativeFunction_destructor(FunctionInstance *function)
651 static const function_vtbl_t NativeFunctionVtbl = {
652 NativeFunction_call,
653 NativeFunction_toString,
654 NativeFunction_get_code,
655 NativeFunction_destructor
658 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
659 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
661 NativeFunction *function;
662 HRESULT hres;
664 hres = create_function(ctx, builtin_info, &NativeFunctionVtbl, sizeof(NativeFunction), flags, FALSE, NULL, (void**)&function);
665 if(FAILED(hres))
666 return hres;
668 if(builtin_info)
669 hres = jsdisp_define_data_property(&function->function.dispex, L"length", 0,
670 jsval_number(function->function.length));
671 if(SUCCEEDED(hres))
672 hres = jsdisp_define_data_property(&function->function.dispex, L"prototype", 0, jsval_obj(prototype));
673 if(FAILED(hres)) {
674 jsdisp_release(&function->function.dispex);
675 return hres;
678 function->proc = value_proc;
679 function->name = name;
681 *ret = &function->function.dispex;
682 return S_OK;
685 static HRESULT set_constructor_prop(script_ctx_t *ctx, jsdisp_t *constr, jsdisp_t *prot)
687 return jsdisp_define_data_property(prot, L"constructor", PROPF_WRITABLE | PROPF_CONFIGURABLE,
688 jsval_obj(constr));
691 HRESULT create_builtin_constructor(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
692 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
694 jsdisp_t *constr;
695 HRESULT hres;
697 hres = create_builtin_function(ctx, value_proc, name, builtin_info, flags, prototype, &constr);
698 if(FAILED(hres))
699 return hres;
701 hres = set_constructor_prop(ctx, constr, prototype);
702 if(FAILED(hres)) {
703 jsdisp_release(constr);
704 return hres;
707 *ret = constr;
708 return S_OK;
711 static HRESULT InterpretedFunction_call(script_ctx_t *ctx, FunctionInstance *func, IDispatch *this_obj, unsigned flags,
712 unsigned argc, jsval_t *argv, jsval_t *r)
714 InterpretedFunction *function = (InterpretedFunction*)func;
715 jsdisp_t *new_obj = NULL;
716 DWORD exec_flags = 0;
717 HRESULT hres;
719 TRACE("%p\n", function);
721 if(ctx->state == SCRIPTSTATE_UNINITIALIZED || ctx->state == SCRIPTSTATE_CLOSED) {
722 WARN("Script engine state does not allow running code.\n");
723 return E_UNEXPECTED;
726 if(flags & DISPATCH_CONSTRUCT) {
727 hres = create_object(ctx, &function->function.dispex, &new_obj);
728 if(FAILED(hres))
729 return hres;
730 this_obj = to_disp(new_obj);
733 if(flags & DISPATCH_JSCRIPT_CALLEREXECSSOURCE)
734 exec_flags |= EXEC_RETURN_TO_INTERP;
735 if(flags & DISPATCH_CONSTRUCT)
736 exec_flags |= EXEC_CONSTRUCTOR;
737 hres = exec_source(ctx, exec_flags, function->code, function->func_code, function->scope_chain, this_obj,
738 &function->function.dispex, argc, argv, r);
739 if(new_obj)
740 jsdisp_release(new_obj);
741 return hres;
744 static HRESULT InterpretedFunction_toString(FunctionInstance *func, jsstr_t **ret)
746 InterpretedFunction *function = (InterpretedFunction*)func;
748 *ret = jsstr_alloc_len(function->func_code->source, function->func_code->source_len);
749 return *ret ? S_OK : E_OUTOFMEMORY;
752 static function_code_t *InterpretedFunction_get_code(FunctionInstance *func)
754 InterpretedFunction *function = (InterpretedFunction*)func;
756 return function->func_code;
759 static void InterpretedFunction_destructor(FunctionInstance *func)
761 InterpretedFunction *function = (InterpretedFunction*)func;
763 release_bytecode(function->code);
764 if(function->scope_chain)
765 scope_release(function->scope_chain);
768 static const function_vtbl_t InterpretedFunctionVtbl = {
769 InterpretedFunction_call,
770 InterpretedFunction_toString,
771 InterpretedFunction_get_code,
772 InterpretedFunction_destructor
775 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
776 scope_chain_t *scope_chain, jsdisp_t **ret)
778 InterpretedFunction *function;
779 jsdisp_t *prototype;
780 HRESULT hres;
782 hres = create_object(ctx, NULL, &prototype);
783 if(FAILED(hres))
784 return hres;
786 hres = create_function(ctx, NULL, &InterpretedFunctionVtbl, sizeof(InterpretedFunction), PROPF_CONSTR,
787 FALSE, NULL, (void**)&function);
788 if(SUCCEEDED(hres)) {
789 hres = jsdisp_define_data_property(&function->function.dispex, L"prototype", PROPF_WRITABLE,
790 jsval_obj(prototype));
791 if(SUCCEEDED(hres))
792 hres = set_constructor_prop(ctx, &function->function.dispex, prototype);
793 if(FAILED(hres))
794 jsdisp_release(&function->function.dispex);
796 jsdisp_release(prototype);
797 if(FAILED(hres))
798 return hres;
800 if(scope_chain) {
801 scope_addref(scope_chain);
802 function->scope_chain = scope_chain;
805 bytecode_addref(code);
806 function->code = code;
807 function->func_code = func_code;
808 function->function.length = function->func_code->param_cnt;
810 *ret = &function->function.dispex;
811 return S_OK;
814 static HRESULT BindFunction_call(script_ctx_t *ctx, FunctionInstance *func, IDispatch *this_obj, unsigned flags,
815 unsigned argc, jsval_t *argv, jsval_t *r)
817 BindFunction *function = (BindFunction*)func;
818 jsval_t *call_args = NULL;
819 unsigned call_argc;
820 HRESULT hres;
822 TRACE("%p\n", function);
824 call_argc = function->argc + argc;
825 if(call_argc) {
826 call_args = heap_alloc(call_argc * sizeof(*call_args));
827 if(!call_args)
828 return E_OUTOFMEMORY;
830 if(function->argc)
831 memcpy(call_args, function->args, function->argc * sizeof(*call_args));
832 if(argc)
833 memcpy(call_args + function->argc, argv, argc * sizeof(*call_args));
836 hres = function->target->vtbl->call(ctx, function->target, function->this, flags, call_argc, call_args, r);
838 heap_free(call_args);
839 return hres;
842 static HRESULT BindFunction_toString(FunctionInstance *function, jsstr_t **ret)
844 *ret = jsstr_alloc(L"\nfunction() {\n [native code]\n}\n");
845 return *ret ? S_OK : E_OUTOFMEMORY;
848 static function_code_t *BindFunction_get_code(FunctionInstance *function)
850 return NULL;
853 static void BindFunction_destructor(FunctionInstance *func)
855 BindFunction *function = (BindFunction*)func;
856 unsigned i;
858 TRACE("%p\n", function);
860 for(i = 0; i < function->argc; i++)
861 jsval_release(function->args[i]);
862 jsdisp_release(&function->target->dispex);
863 if(function->this)
864 IDispatch_Release(function->this);
867 static const function_vtbl_t BindFunctionVtbl = {
868 BindFunction_call,
869 BindFunction_toString,
870 BindFunction_get_code,
871 BindFunction_destructor
874 static HRESULT create_bind_function(script_ctx_t *ctx, FunctionInstance *target, IDispatch *bound_this, unsigned argc,
875 jsval_t *argv, jsdisp_t **ret)
877 BindFunction *function;
878 HRESULT hres;
880 hres = create_function(ctx, NULL, &BindFunctionVtbl, FIELD_OFFSET(BindFunction, args[argc]), PROPF_METHOD,
881 FALSE, NULL, (void**)&function);
882 if(FAILED(hres))
883 return hres;
885 jsdisp_addref(&target->dispex);
886 function->target = target;
888 if(bound_this)
889 IDispatch_AddRef(function->this = bound_this);
891 for(function->argc = 0; function->argc < argc; function->argc++) {
892 hres = jsval_copy(argv[function->argc], function->args + function->argc);
893 if(FAILED(hres)) {
894 jsdisp_release(&function->function.dispex);
895 return hres;
899 function->function.length = target->length > argc ? target->length - argc : 0;
901 *ret = &function->function.dispex;
902 return S_OK;
905 static HRESULT construct_function(script_ctx_t *ctx, unsigned argc, jsval_t *argv, IDispatch **ret)
907 WCHAR *str = NULL, *ptr;
908 unsigned len = 0, i = 0;
909 bytecode_t *code;
910 jsdisp_t *function;
911 jsstr_t **params = NULL;
912 int j = 0;
913 HRESULT hres = S_OK;
915 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
916 static const WCHAR function_beginW[] = {')',' ','{','\n'};
917 static const WCHAR function_endW[] = L"\n}";
919 if(argc) {
920 params = heap_alloc(argc*sizeof(*params));
921 if(!params)
922 return E_OUTOFMEMORY;
924 if(argc > 2)
925 len = (argc-2)*2; /* separating commas */
926 for(i=0; i < argc; i++) {
927 hres = to_string(ctx, argv[i], params+i);
928 if(FAILED(hres))
929 break;
930 len += jsstr_length(params[i]);
934 if(SUCCEEDED(hres)) {
935 len += ARRAY_SIZE(function_anonymousW) + ARRAY_SIZE(function_beginW) + ARRAY_SIZE(function_endW);
936 str = heap_alloc(len*sizeof(WCHAR));
937 if(str) {
938 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
939 ptr = str + ARRAY_SIZE(function_anonymousW);
940 if(argc > 1) {
941 while(1) {
942 ptr += jsstr_flush(params[j], ptr);
943 if(++j == argc-1)
944 break;
945 *ptr++ = ',';
946 *ptr++ = ' ';
949 memcpy(ptr, function_beginW, sizeof(function_beginW));
950 ptr += ARRAY_SIZE(function_beginW);
951 if(argc)
952 ptr += jsstr_flush(params[argc-1], ptr);
953 memcpy(ptr, function_endW, sizeof(function_endW));
955 TRACE("%s\n", debugstr_w(str));
956 }else {
957 hres = E_OUTOFMEMORY;
961 while(i)
962 jsstr_release(params[--i]);
963 heap_free(params);
964 if(FAILED(hres))
965 return hres;
967 hres = compile_script(ctx, str, 0, 0, NULL, NULL, FALSE, FALSE,
968 ctx->call_ctx ? ctx->call_ctx->bytecode->named_item : NULL, &code);
969 heap_free(str);
970 if(FAILED(hres))
971 return hres;
973 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt != 1) {
974 ERR("Invalid parser result!\n");
975 release_bytecode(code);
976 return E_UNEXPECTED;
979 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
980 release_bytecode(code);
981 if(FAILED(hres))
982 return hres;
984 *ret = to_disp(function);
985 return S_OK;
988 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
989 jsval_t *r)
991 HRESULT hres;
993 TRACE("\n");
995 switch(flags) {
996 case DISPATCH_METHOD:
997 case DISPATCH_CONSTRUCT: {
998 IDispatch *ret;
1000 hres = construct_function(ctx, argc, argv, &ret);
1001 if(FAILED(hres))
1002 return hres;
1004 *r = jsval_disp(ret);
1005 break;
1007 default:
1008 FIXME("unimplemented flags %x\n", flags);
1009 return E_NOTIMPL;
1012 return S_OK;
1015 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1016 jsval_t *r)
1018 FIXME("\n");
1019 return E_NOTIMPL;
1022 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
1024 NativeFunction *prot, *constr;
1025 HRESULT hres;
1027 hres = create_function(ctx, &Function_info, &NativeFunctionVtbl, sizeof(NativeFunction), PROPF_CONSTR,
1028 TRUE, object_prototype, (void**)&prot);
1029 if(FAILED(hres))
1030 return hres;
1032 prot->proc = FunctionProt_value;
1033 prot->name = L"prototype";
1035 hres = create_function(ctx, &FunctionInst_info, &NativeFunctionVtbl, sizeof(NativeFunction), PROPF_CONSTR|1,
1036 TRUE, &prot->function.dispex, (void**)&constr);
1037 if(SUCCEEDED(hres)) {
1038 constr->proc = FunctionConstr_value;
1039 constr->name = L"Function";
1040 hres = jsdisp_define_data_property(&constr->function.dispex, L"prototype", 0, jsval_obj(&prot->function.dispex));
1041 if(SUCCEEDED(hres))
1042 hres = set_constructor_prop(ctx, &constr->function.dispex, &prot->function.dispex);
1043 if(FAILED(hres))
1044 jsdisp_release(&constr->function.dispex);
1046 jsdisp_release(&prot->function.dispex);
1047 if(FAILED(hres))
1048 return hres;
1050 ctx->function_constr = &constr->function.dispex;
1051 return S_OK;