msvcp: Sync fpos usage and istream<>::tellg.
[wine/multimedia.git] / dlls / jscript / function.c
blobc5bcd2ea9d3f49e9ba00797504c78cf9e842f05e
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 {
29 jsdisp_t dispex;
30 builtin_invoke_t value_proc;
31 const WCHAR *name;
32 DWORD flags;
33 scope_chain_t *scope_chain;
34 bytecode_t *code;
35 function_code_t *func_code;
36 DWORD length;
37 jsdisp_t *arguments;
38 } FunctionInstance;
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)
60 DWORD i=0;
61 HRESULT hres;
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());
66 if(FAILED(hres))
67 return hres;
70 return S_OK;
73 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
74 jsval_t *r)
76 FIXME("\n");
77 return E_NOTIMPL;
80 static const builtin_info_t Arguments_info = {
81 JSCLASS_ARGUMENTS,
82 {NULL, Arguments_value, 0},
83 0, NULL,
84 NULL,
85 NULL
88 static HRESULT create_arguments(script_ctx_t *ctx, IDispatch *calee, unsigned argc, jsval_t *argv, jsdisp_t **ret)
90 jsdisp_t *args;
91 DWORD i;
92 HRESULT hres;
94 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
96 args = heap_alloc_zero(sizeof(jsdisp_t));
97 if(!args)
98 return E_OUTOFMEMORY;
100 hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
101 if(FAILED(hres)) {
102 heap_free(args);
103 return hres;
106 for(i=0; i < argc; i++) {
107 hres = jsdisp_propput_idx(args, i, argv[i]);
108 if(FAILED(hres))
109 break;
112 if(SUCCEEDED(hres)) {
113 hres = jsdisp_propput_name(args, lengthW, jsval_number(argc));
115 if(SUCCEEDED(hres))
116 hres = jsdisp_propput_name(args, caleeW, jsval_disp(calee));
119 if(FAILED(hres)) {
120 jsdisp_release(args);
121 return hres;
124 *ret = args;
125 return S_OK;
128 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, jsdisp_t *arg_disp,
129 unsigned argc, jsval_t *argv, jsdisp_t **ret)
131 jsdisp_t *var_disp;
132 HRESULT hres;
134 hres = create_dispex(ctx, NULL, NULL, &var_disp);
135 if(FAILED(hres))
136 return hres;
138 hres = jsdisp_propput_name(var_disp, argumentsW, jsval_obj(arg_disp));
139 if(SUCCEEDED(hres))
140 hres = init_parameters(var_disp, function, argc, argv);
141 if(FAILED(hres)) {
142 jsdisp_release(var_disp);
143 return hres;
146 *ret = var_disp;
147 return S_OK;
150 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, unsigned argc, jsval_t *argv,
151 jsval_t *r)
153 jsdisp_t *var_disp, *arg_disp;
154 exec_ctx_t *exec_ctx;
155 scope_chain_t *scope;
156 HRESULT hres;
158 if(!function->func_code) {
159 FIXME("no source\n");
160 return E_FAIL;
163 hres = create_arguments(ctx, to_disp(&function->dispex), argc, argv, &arg_disp);
164 if(FAILED(hres))
165 return hres;
167 hres = create_var_disp(ctx, function, arg_disp, argc, argv, &var_disp);
168 if(FAILED(hres)) {
169 jsdisp_release(arg_disp);
170 return hres;
173 hres = scope_push(function->scope_chain, var_disp, to_disp(var_disp), &scope);
174 if(SUCCEEDED(hres)) {
175 hres = create_exec_ctx(ctx, this_obj, var_disp, scope, FALSE, &exec_ctx);
176 scope_release(scope);
178 jsdisp_release(var_disp);
179 if(SUCCEEDED(hres)) {
180 jsdisp_t *prev_args;
182 prev_args = function->arguments;
183 function->arguments = arg_disp;
184 hres = exec_source(exec_ctx, function->code, function->func_code, FALSE, r);
185 function->arguments = prev_args;
188 jsdisp_release(arg_disp);
189 exec_release(exec_ctx);
190 return hres;
193 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, unsigned argc, jsval_t *argv,
194 jsval_t *r)
196 jsdisp_t *this_obj;
197 jsval_t var;
198 HRESULT hres;
200 hres = create_object(ctx, &function->dispex, &this_obj);
201 if(FAILED(hres))
202 return hres;
204 hres = invoke_source(ctx, function, to_disp(this_obj), argc, argv, &var);
205 if(FAILED(hres)) {
206 jsdisp_release(this_obj);
207 return hres;
210 if(is_object_instance(var)) {
211 jsdisp_release(this_obj);
212 *r = var;
213 }else {
214 jsval_release(var);
215 *r = jsval_obj(this_obj);
217 return S_OK;
220 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags,
221 unsigned argc, jsval_t *argv, jsval_t *r)
223 vdisp_t vthis;
224 HRESULT hres;
226 if(this_disp)
227 set_disp(&vthis, this_disp);
228 else if(ctx->host_global)
229 set_disp(&vthis, ctx->host_global);
230 else
231 set_jsdisp(&vthis, ctx->global);
233 hres = function->value_proc(ctx, &vthis, flags, argc, argv, r);
235 vdisp_release(&vthis);
236 return hres;
239 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj,
240 unsigned argc, jsval_t *argv, jsval_t *r)
242 if(function->value_proc)
243 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, argc, argv, r);
245 return invoke_source(ctx, function, this_obj, argc, argv, r);
248 static HRESULT function_to_string(FunctionInstance *function, jsstr_t **ret)
250 jsstr_t *str;
252 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
253 static const WCHAR native_suffixW[] =
254 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
256 if(function->value_proc) {
257 DWORD name_len;
259 name_len = strlenW(function->name);
260 str = jsstr_alloc_buf((sizeof(native_prefixW)+sizeof(native_suffixW))/sizeof(WCHAR) + name_len);
261 if(!str)
262 return E_OUTOFMEMORY;
264 memcpy(str->str, native_prefixW, sizeof(native_prefixW));
265 memcpy(str->str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
266 memcpy(str->str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
267 }else {
268 str = jsstr_alloc_len(function->func_code->source, function->func_code->source_len);
269 if(!str)
270 return E_OUTOFMEMORY;
273 *ret = str;
274 return S_OK;
277 HRESULT Function_invoke(jsdisp_t *func_this, IDispatch *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
279 FunctionInstance *function;
281 TRACE("func %p this %p\n", func_this, jsthis);
283 assert(is_class(func_this, JSCLASS_FUNCTION));
284 function = (FunctionInstance*)func_this;
286 if(function->value_proc)
287 return invoke_value_proc(function->dispex.ctx, function, jsthis, flags, argc, argv, r);
289 if(flags == DISPATCH_CONSTRUCT)
290 return invoke_constructor(function->dispex.ctx, function, argc, argv, r);
292 assert(flags == DISPATCH_METHOD);
293 return invoke_source(function->dispex.ctx, function, jsthis, argc, argv, r);
296 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
297 jsval_t *r)
299 FunctionInstance *This = function_from_vdisp(jsthis);
301 TRACE("%p %d\n", This, This->length);
303 switch(flags) {
304 case DISPATCH_PROPERTYGET:
305 *r = jsval_number(This->length);
306 break;
307 default:
308 FIXME("unimplemented flags %x\n", flags);
309 return E_NOTIMPL;
312 return S_OK;
315 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
316 jsval_t *r)
318 FunctionInstance *function;
319 jsstr_t *str;
320 HRESULT hres;
322 TRACE("\n");
324 if(!(function = function_this(jsthis)))
325 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
327 hres = function_to_string(function, &str);
328 if(FAILED(hres))
329 return hres;
331 if(r)
332 *r = jsval_string(str);
333 else
334 jsstr_release(str);
335 return S_OK;
338 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, unsigned *argc, jsval_t **ret)
340 jsval_t *argv, val;
341 DWORD length, i;
342 HRESULT hres;
344 hres = jsdisp_propget_name(arg_array, lengthW, &val);
345 if(FAILED(hres))
346 return hres;
348 hres = to_uint32(ctx, val, &length);
349 jsval_release(val);
350 if(FAILED(hres))
351 return hres;
353 argv = heap_alloc(length * sizeof(*argv));
354 if(!argv)
355 return E_OUTOFMEMORY;
357 for(i=0; i<length; i++) {
358 hres = jsdisp_get_idx(arg_array, i, argv+i);
359 if(hres == DISP_E_UNKNOWNNAME) {
360 argv[i] = jsval_undefined();
361 }else if(FAILED(hres)) {
362 while(i--)
363 jsval_release(argv[i]);
364 heap_free(argv);
365 return hres;
369 *argc = length;
370 *ret = argv;
371 return S_OK;
374 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
376 FunctionInstance *function;
377 jsval_t *args = NULL;
378 unsigned i, cnt = 0;
379 IDispatch *this_obj = NULL;
380 HRESULT hres = S_OK;
382 TRACE("\n");
384 if(!(function = function_this(jsthis)))
385 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
387 if(argc) {
388 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
389 hres = to_object(ctx, argv[0], &this_obj);
390 if(FAILED(hres))
391 return hres;
395 if(argc >= 2) {
396 jsdisp_t *arg_array = NULL;
398 if(is_object_instance(argv[1])) {
399 arg_array = iface_to_jsdisp((IUnknown*)get_object(argv[1]));
400 if(arg_array &&
401 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
402 jsdisp_release(arg_array);
403 arg_array = NULL;
407 if(arg_array) {
408 hres = array_to_args(ctx, arg_array, &cnt, &args);
409 jsdisp_release(arg_array);
410 }else {
411 FIXME("throw TypeError\n");
412 hres = E_FAIL;
416 if(SUCCEEDED(hres))
417 hres = call_function(ctx, function, this_obj, cnt, args, r);
419 if(this_obj)
420 IDispatch_Release(this_obj);
421 for(i=0; i < cnt; i++)
422 jsval_release(args[i]);
423 heap_free(args);
424 return hres;
427 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
428 jsval_t *r)
430 FunctionInstance *function;
431 IDispatch *this_obj = NULL;
432 unsigned cnt = 0;
433 HRESULT hres;
435 TRACE("\n");
437 if(!(function = function_this(jsthis)))
438 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
440 if(argc) {
441 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
442 hres = to_object(ctx, argv[0], &this_obj);
443 if(FAILED(hres))
444 return hres;
447 cnt = argc-1;
450 hres = call_function(ctx, function, this_obj, cnt, argv+1, r);
452 if(this_obj)
453 IDispatch_Release(this_obj);
454 return hres;
457 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
458 jsval_t *r)
460 FunctionInstance *function;
462 TRACE("\n");
464 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
465 ERR("dispex is not a function\n");
466 return E_FAIL;
469 function = (FunctionInstance*)jsthis->u.jsdisp;
471 switch(flags) {
472 case DISPATCH_METHOD:
473 assert(function->value_proc != NULL);
474 return invoke_value_proc(ctx, function, NULL, flags, argc, argv, r);
476 case DISPATCH_PROPERTYGET: {
477 HRESULT hres;
478 jsstr_t *str;
480 hres = function_to_string(function, &str);
481 if(FAILED(hres))
482 return hres;
484 *r = jsval_string(str);
485 break;
488 case DISPATCH_CONSTRUCT:
489 assert(function->value_proc != NULL);
490 return invoke_value_proc(ctx, function, NULL, flags, argc, argv, r);
492 default:
493 FIXME("not implemented flags %x\n", flags);
494 return E_NOTIMPL;
497 return S_OK;
500 static HRESULT Function_arguments(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
501 unsigned argc, jsval_t *argv, jsval_t *r)
503 FunctionInstance *function = (FunctionInstance*)jsthis->u.jsdisp;
504 HRESULT hres = S_OK;
506 TRACE("\n");
508 switch(flags) {
509 case DISPATCH_PROPERTYGET: {
510 *r = function->arguments ? jsval_obj(jsdisp_addref(function->arguments)) : jsval_null();
511 break;
513 case DISPATCH_PROPERTYPUT:
514 break;
515 default:
516 FIXME("unimplemented flags %x\n", flags);
517 hres = E_NOTIMPL;
520 return hres;
523 static void Function_destructor(jsdisp_t *dispex)
525 FunctionInstance *This = (FunctionInstance*)dispex;
527 if(This->code)
528 release_bytecode(This->code);
529 if(This->scope_chain)
530 scope_release(This->scope_chain);
531 heap_free(This);
534 static const builtin_prop_t Function_props[] = {
535 {applyW, Function_apply, PROPF_METHOD|2},
536 {argumentsW, Function_arguments, 0},
537 {callW, Function_call, PROPF_METHOD|1},
538 {lengthW, Function_length, 0},
539 {toStringW, Function_toString, PROPF_METHOD}
542 static const builtin_info_t Function_info = {
543 JSCLASS_FUNCTION,
544 {NULL, Function_value, 0},
545 sizeof(Function_props)/sizeof(*Function_props),
546 Function_props,
547 Function_destructor,
548 NULL
551 static const builtin_prop_t FunctionInst_props[] = {
552 {argumentsW, Function_arguments, 0},
553 {lengthW, Function_length, 0}
556 static const builtin_info_t FunctionInst_info = {
557 JSCLASS_FUNCTION,
558 {NULL, Function_value, 0},
559 sizeof(FunctionInst_props)/sizeof(*FunctionInst_props),
560 FunctionInst_props,
561 Function_destructor,
562 NULL
565 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
566 BOOL funcprot, jsdisp_t *prototype, FunctionInstance **ret)
568 FunctionInstance *function;
569 HRESULT hres;
571 function = heap_alloc_zero(sizeof(FunctionInstance));
572 if(!function)
573 return E_OUTOFMEMORY;
575 if(funcprot)
576 hres = init_dispex(&function->dispex, ctx, builtin_info, prototype);
577 else if(builtin_info)
578 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
579 else
580 hres = init_dispex_from_constr(&function->dispex, ctx, &FunctionInst_info, ctx->function_constr);
581 if(FAILED(hres)) {
582 heap_free(function);
583 return hres;
586 function->flags = flags;
587 function->length = flags & PROPF_ARGMASK;
589 *ret = function;
590 return S_OK;
593 static inline HRESULT set_prototype(script_ctx_t *ctx, jsdisp_t *dispex, jsdisp_t *prototype)
595 return jsdisp_propput_name(dispex, prototypeW, jsval_obj(prototype));
598 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
599 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
601 FunctionInstance *function;
602 HRESULT hres;
604 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
605 if(FAILED(hres))
606 return hres;
608 if(builtin_info)
609 hres = jsdisp_propput_const(&function->dispex, lengthW, jsval_number(function->length));
610 if(SUCCEEDED(hres))
611 hres = set_prototype(ctx, &function->dispex, prototype);
612 if(FAILED(hres)) {
613 jsdisp_release(&function->dispex);
614 return hres;
617 function->value_proc = value_proc;
618 function->name = name;
620 *ret = &function->dispex;
621 return S_OK;
624 static HRESULT set_constructor_prop(script_ctx_t *ctx, jsdisp_t *constr, jsdisp_t *prot)
626 static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
628 return jsdisp_propput_dontenum(prot, constructorW, jsval_obj(constr));
631 HRESULT create_builtin_constructor(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
632 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
634 jsdisp_t *constr;
635 HRESULT hres;
637 hres = create_builtin_function(ctx, value_proc, name, builtin_info, flags, prototype, &constr);
638 if(FAILED(hres))
639 return hres;
641 hres = set_constructor_prop(ctx, constr, prototype);
642 if(FAILED(hres)) {
643 jsdisp_release(constr);
644 return hres;
647 *ret = constr;
648 return S_OK;
651 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
652 scope_chain_t *scope_chain, jsdisp_t **ret)
654 FunctionInstance *function;
655 jsdisp_t *prototype;
656 HRESULT hres;
658 hres = create_object(ctx, NULL, &prototype);
659 if(FAILED(hres))
660 return hres;
662 hres = create_function(ctx, NULL, PROPF_CONSTR, FALSE, NULL, &function);
663 if(SUCCEEDED(hres)) {
664 hres = set_prototype(ctx, &function->dispex, prototype);
665 if(SUCCEEDED(hres))
666 hres = set_constructor_prop(ctx, &function->dispex, prototype);
667 if(FAILED(hres))
668 jsdisp_release(&function->dispex);
670 jsdisp_release(prototype);
671 if(FAILED(hres))
672 return hres;
674 if(scope_chain) {
675 scope_addref(scope_chain);
676 function->scope_chain = scope_chain;
679 bytecode_addref(code);
680 function->code = code;
681 function->func_code = func_code;
682 function->length = function->func_code->param_cnt;
684 *ret = &function->dispex;
685 return S_OK;
688 static HRESULT construct_function(script_ctx_t *ctx, unsigned argc, jsval_t *argv, IDispatch **ret)
690 WCHAR *str = NULL, *ptr;
691 DWORD len = 0, l;
692 bytecode_t *code;
693 jsdisp_t *function;
694 jsstr_t **params = NULL;
695 int i=0, j=0;
696 HRESULT hres = S_OK;
698 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
699 static const WCHAR function_beginW[] = {')',' ','{','\n'};
700 static const WCHAR function_endW[] = {'\n','}',0};
702 if(argc) {
703 params = heap_alloc(argc*sizeof(*params));
704 if(!params)
705 return E_OUTOFMEMORY;
707 if(argc > 2)
708 len = (argc-2)*2; /* separating commas */
709 for(i=0; i < argc; i++) {
710 hres = to_string(ctx, argv[i], params+i);
711 if(FAILED(hres))
712 break;
713 len += jsstr_length(params[i]);
717 if(SUCCEEDED(hres)) {
718 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
719 str = heap_alloc(len*sizeof(WCHAR));
720 if(str) {
721 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
722 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
723 if(argc > 1) {
724 while(1) {
725 l = jsstr_length(params[j]);
726 memcpy(ptr, params[j]->str, l*sizeof(WCHAR));
727 ptr += l;
728 if(++j == argc-1)
729 break;
730 *ptr++ = ',';
731 *ptr++ = ' ';
734 memcpy(ptr, function_beginW, sizeof(function_beginW));
735 ptr += sizeof(function_beginW)/sizeof(WCHAR);
736 if(argc) {
737 l = jsstr_length(params[argc-1]);
738 memcpy(ptr, params[argc-1]->str, l*sizeof(WCHAR));
739 ptr += l;
741 memcpy(ptr, function_endW, sizeof(function_endW));
743 TRACE("%s\n", debugstr_w(str));
744 }else {
745 hres = E_OUTOFMEMORY;
749 while(--i >= 0)
750 jsstr_release(params[i]);
751 heap_free(params);
752 if(FAILED(hres))
753 return hres;
755 hres = compile_script(ctx, str, NULL, NULL, FALSE, FALSE, &code);
756 heap_free(str);
757 if(FAILED(hres))
758 return hres;
760 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt) {
761 ERR("Invalid parser result!\n");
762 release_bytecode(code);
763 return E_UNEXPECTED;
766 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
767 release_bytecode(code);
768 if(FAILED(hres))
769 return hres;
771 *ret = to_disp(function);
772 return S_OK;
775 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
776 jsval_t *r)
778 HRESULT hres;
780 TRACE("\n");
782 switch(flags) {
783 case DISPATCH_CONSTRUCT: {
784 IDispatch *ret;
786 hres = construct_function(ctx, argc, argv, &ret);
787 if(FAILED(hres))
788 return hres;
790 *r = jsval_disp(ret);
791 break;
793 default:
794 FIXME("unimplemented flags %x\n", flags);
795 return E_NOTIMPL;
798 return S_OK;
801 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
802 jsval_t *r)
804 FIXME("\n");
805 return E_NOTIMPL;
808 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
810 FunctionInstance *prot, *constr;
811 HRESULT hres;
813 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
815 hres = create_function(ctx, &Function_info, PROPF_CONSTR, TRUE, object_prototype, &prot);
816 if(FAILED(hres))
817 return hres;
819 prot->value_proc = FunctionProt_value;
820 prot->name = prototypeW;
822 hres = create_function(ctx, &FunctionInst_info, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
823 if(SUCCEEDED(hres)) {
824 constr->value_proc = FunctionConstr_value;
825 constr->name = FunctionW;
826 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
827 if(SUCCEEDED(hres))
828 hres = set_constructor_prop(ctx, &constr->dispex, &prot->dispex);
829 if(FAILED(hres))
830 jsdisp_release(&constr->dispex);
832 jsdisp_release(&prot->dispex);
833 if(FAILED(hres))
834 return hres;
836 ctx->function_constr = &constr->dispex;
837 return S_OK;