aca6e26e1f49b8649553bc79e3ee0eae8f13574f
[reactos.git] / reactos / dll / win32 / jscript / function.c
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "jscript.h"
20
21 typedef struct {
22 jsdisp_t dispex;
23 builtin_invoke_t value_proc;
24 const WCHAR *name;
25 DWORD flags;
26 scope_chain_t *scope_chain;
27 bytecode_t *code;
28 function_code_t *func_code;
29 DWORD length;
30 } FunctionInstance;
31
32 typedef struct {
33 jsdisp_t jsdisp;
34 FunctionInstance *function;
35 jsval_t *buf;
36 call_frame_t *frame;
37 unsigned argc;
38 } ArgumentsInstance;
39
40 static inline FunctionInstance *function_from_jsdisp(jsdisp_t *jsdisp)
41 {
42 return CONTAINING_RECORD(jsdisp, FunctionInstance, dispex);
43 }
44
45 static inline FunctionInstance *function_from_vdisp(vdisp_t *vdisp)
46 {
47 return function_from_jsdisp(vdisp->u.jsdisp);
48 }
49
50 static inline FunctionInstance *function_this(vdisp_t *jsthis)
51 {
52 return is_vclass(jsthis, JSCLASS_FUNCTION) ? function_from_vdisp(jsthis) : NULL;
53 }
54
55 static inline ArgumentsInstance *arguments_from_jsdisp(jsdisp_t *jsdisp)
56 {
57 return CONTAINING_RECORD(jsdisp, ArgumentsInstance, jsdisp);
58 }
59
60 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
61
62 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
63 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
64 static const WCHAR applyW[] = {'a','p','p','l','y',0};
65 static const WCHAR callW[] = {'c','a','l','l',0};
66 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
67
68 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
69 jsval_t *r)
70 {
71 FIXME("\n");
72 return E_NOTIMPL;
73 }
74
75 static void Arguments_destructor(jsdisp_t *jsdisp)
76 {
77 ArgumentsInstance *arguments = (ArgumentsInstance*)jsdisp;
78
79 TRACE("(%p)\n", arguments);
80
81 if(arguments->buf) {
82 unsigned i;
83 for(i = 0; i < arguments->argc; i++)
84 jsval_release(arguments->buf[i]);
85 heap_free(arguments->buf);
86 }
87
88 jsdisp_release(&arguments->function->dispex);
89 heap_free(arguments);
90 }
91
92 static unsigned Arguments_idx_length(jsdisp_t *jsdisp)
93 {
94 ArgumentsInstance *arguments = (ArgumentsInstance*)jsdisp;
95 return arguments->argc;
96 }
97
98 static jsval_t *get_argument_ref(ArgumentsInstance *arguments, unsigned idx)
99 {
100 if(arguments->buf)
101 return arguments->buf + idx;
102 if(arguments->frame->base_scope->frame || idx >= arguments->frame->function->param_cnt)
103 return arguments->jsdisp.ctx->stack + arguments->frame->arguments_off + idx;
104 return NULL;
105 }
106
107 static HRESULT Arguments_idx_get(jsdisp_t *jsdisp, unsigned idx, jsval_t *r)
108 {
109 ArgumentsInstance *arguments = (ArgumentsInstance*)jsdisp;
110 jsval_t *ref;
111
112 TRACE("%p[%u]\n", arguments, idx);
113
114 if((ref = get_argument_ref(arguments, idx)))
115 return jsval_copy(*ref, r);
116
117 /* FIXME: Accessing by name won't work for duplicated argument names */
118 return jsdisp_propget_name(arguments->frame->base_scope->jsobj, arguments->function->func_code->params[idx], r);
119 }
120
121 static HRESULT Arguments_idx_put(jsdisp_t *jsdisp, unsigned idx, jsval_t val)
122 {
123 ArgumentsInstance *arguments = (ArgumentsInstance*)jsdisp;
124 jsval_t *ref;
125 HRESULT hres;
126
127 TRACE("%p[%u] = %s\n", arguments, idx, debugstr_jsval(val));
128
129 if((ref = get_argument_ref(arguments, idx))) {
130 jsval_t copy;
131 hres = jsval_copy(val, &copy);
132 if(FAILED(hres))
133 return hres;
134
135 jsval_release(*ref);
136 *ref = copy;
137 return S_OK;
138 }
139
140 /* FIXME: Accessing by name won't work for duplicated argument names */
141 return jsdisp_propput_name(arguments->frame->base_scope->jsobj, arguments->function->func_code->params[idx], val);
142 }
143
144 static const builtin_info_t Arguments_info = {
145 JSCLASS_ARGUMENTS,
146 {NULL, Arguments_value, 0},
147 0, NULL,
148 Arguments_destructor,
149 NULL,
150 Arguments_idx_length,
151 Arguments_idx_get,
152 Arguments_idx_put
153 };
154
155 HRESULT setup_arguments_object(script_ctx_t *ctx, call_frame_t *frame)
156 {
157 ArgumentsInstance *args;
158 HRESULT hres;
159
160 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
161
162 args = heap_alloc_zero(sizeof(*args));
163 if(!args)
164 return E_OUTOFMEMORY;
165
166 hres = init_dispex_from_constr(&args->jsdisp, ctx, &Arguments_info, ctx->object_constr);
167 if(FAILED(hres)) {
168 heap_free(args);
169 return hres;
170 }
171
172 args->function = function_from_jsdisp(jsdisp_addref(frame->function_instance));
173 args->argc = frame->argc;
174 args->frame = frame;
175
176 hres = jsdisp_propput_dontenum(&args->jsdisp, lengthW, jsval_number(args->argc));
177 if(SUCCEEDED(hres))
178 hres = jsdisp_propput_dontenum(&args->jsdisp, caleeW, jsval_disp(to_disp(&args->function->dispex)));
179 if(SUCCEEDED(hres))
180 hres = jsdisp_propput(frame->base_scope->jsobj, argumentsW, PROPF_DONTDELETE, jsval_obj(&args->jsdisp));
181 if(FAILED(hres)) {
182 jsdisp_release(&args->jsdisp);
183 return hres;
184 }
185
186 frame->arguments_obj = &args->jsdisp;
187 return S_OK;
188 }
189
190 void detach_arguments_object(jsdisp_t *args_disp)
191 {
192 ArgumentsInstance *arguments = arguments_from_jsdisp(args_disp);
193 call_frame_t *frame = arguments->frame;
194 const BOOL on_stack = frame->base_scope->frame == frame;
195 HRESULT hres;
196
197 /* Reset arguments value to cut the reference cycle. Note that since all activation contexts have
198 * their own arguments property, it's impossible to use prototype's one during name lookup */
199 jsdisp_propput_name(frame->base_scope->jsobj, argumentsW, jsval_undefined());
200 arguments->frame = NULL;
201
202 /* Don't bother coppying arguments if call frame holds the last reference. */
203 if(arguments->jsdisp.ref > 1) {
204 arguments->buf = heap_alloc(arguments->argc * sizeof(*arguments->buf));
205 if(arguments->buf) {
206 int i;
207
208 for(i = 0; i < arguments->argc ; i++) {
209 if(on_stack || i >= frame->function->param_cnt)
210 hres = jsval_copy(arguments->jsdisp.ctx->stack[frame->arguments_off + i], arguments->buf+i);
211 else
212 hres = jsdisp_propget_name(frame->base_scope->jsobj, frame->function->params[i], arguments->buf+i);
213 if(FAILED(hres))
214 arguments->buf[i] = jsval_undefined();
215 }
216 }else {
217 ERR("out of memory\n");
218 arguments->argc = 0;
219 }
220 }
221
222 jsdisp_release(frame->arguments_obj);
223 }
224
225 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, unsigned argc, jsval_t *argv,
226 BOOL is_constructor, BOOL caller_execs_source, jsval_t *r)
227 {
228 jsdisp_t *var_disp;
229 DWORD exec_flags = 0;
230 HRESULT hres;
231
232 if(ctx->state == SCRIPTSTATE_UNINITIALIZED || ctx->state == SCRIPTSTATE_CLOSED) {
233 WARN("Script engine state does not allow running code.\n");
234 return E_UNEXPECTED;
235 }
236
237 if(!function->func_code) {
238 FIXME("no source\n");
239 return E_FAIL;
240 }
241
242 hres = create_dispex(ctx, NULL, NULL, &var_disp);
243 if(FAILED(hres))
244 return hres;
245
246 if(caller_execs_source)
247 exec_flags |= EXEC_RETURN_TO_INTERP;
248 if(is_constructor)
249 exec_flags |= EXEC_CONSTRUCTOR;
250 hres = exec_source(ctx, exec_flags, function->code, function->func_code, function->scope_chain, this_obj,
251 &function->dispex, var_disp, argc, argv, r);
252
253 jsdisp_release(var_disp);
254 return hres;
255 }
256
257 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags,
258 unsigned argc, jsval_t *argv, jsval_t *r)
259 {
260 vdisp_t vthis;
261 HRESULT hres;
262
263 if(this_disp)
264 set_disp(&vthis, this_disp);
265 else if(ctx->host_global)
266 set_disp(&vthis, ctx->host_global);
267 else
268 set_jsdisp(&vthis, ctx->global);
269
270 hres = function->value_proc(ctx, &vthis, flags, argc, argv, r);
271
272 vdisp_release(&vthis);
273 return hres;
274 }
275
276 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj,
277 unsigned argc, jsval_t *argv, BOOL caller_execs_source, jsval_t *r)
278 {
279 if(function->value_proc)
280 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, argc, argv, r);
281
282 return invoke_source(ctx, function, this_obj, argc, argv, FALSE, caller_execs_source, r);
283 }
284
285 static HRESULT function_to_string(FunctionInstance *function, jsstr_t **ret)
286 {
287 jsstr_t *str;
288
289 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
290 static const WCHAR native_suffixW[] =
291 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
292
293 if(function->value_proc) {
294 DWORD name_len;
295 WCHAR *ptr;
296
297 name_len = strlenW(function->name);
298 ptr = jsstr_alloc_buf((sizeof(native_prefixW)+sizeof(native_suffixW))/sizeof(WCHAR) + name_len, &str);
299 if(!ptr)
300 return E_OUTOFMEMORY;
301
302 memcpy(ptr, native_prefixW, sizeof(native_prefixW));
303 memcpy(ptr += sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
304 memcpy(ptr + name_len, native_suffixW, sizeof(native_suffixW));
305 }else {
306 str = jsstr_alloc_len(function->func_code->source, function->func_code->source_len);
307 if(!str)
308 return E_OUTOFMEMORY;
309 }
310
311 *ret = str;
312 return S_OK;
313 }
314
315 HRESULT Function_invoke(jsdisp_t *func_this, IDispatch *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
316 {
317 const BOOL caller_execs_source = (flags & DISPATCH_JSCRIPT_CALLEREXECSSOURCE) != 0;
318 FunctionInstance *function;
319
320 TRACE("func %p this %p\n", func_this, jsthis);
321
322 assert(is_class(func_this, JSCLASS_FUNCTION));
323 function = (FunctionInstance*)func_this;
324
325 flags &= ~DISPATCH_JSCRIPT_INTERNAL_MASK;
326 if(function->value_proc)
327 return invoke_value_proc(function->dispex.ctx, function, jsthis, flags, argc, argv, r);
328
329 if(flags == DISPATCH_CONSTRUCT) {
330 jsdisp_t *this_obj;
331 HRESULT hres;
332
333 hres = create_object(function->dispex.ctx, &function->dispex, &this_obj);
334 if(FAILED(hres))
335 return hres;
336
337 hres = invoke_source(function->dispex.ctx, function, to_disp(this_obj), argc, argv, TRUE, caller_execs_source, r);
338 jsdisp_release(this_obj);
339 return hres;
340 }
341
342 assert(flags == DISPATCH_METHOD);
343 return invoke_source(function->dispex.ctx, function, jsthis, argc, argv, FALSE, caller_execs_source, r);
344 }
345
346 static HRESULT Function_get_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
347 {
348 TRACE("%p\n", jsthis);
349
350 *r = jsval_number(function_from_jsdisp(jsthis)->length);
351 return S_OK;
352 }
353
354 static HRESULT Function_set_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t value)
355 {
356 FIXME("\n");
357 return E_NOTIMPL;
358 }
359
360 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
361 jsval_t *r)
362 {
363 FunctionInstance *function;
364 jsstr_t *str;
365 HRESULT hres;
366
367 TRACE("\n");
368
369 if(!(function = function_this(jsthis)))
370 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
371
372 hres = function_to_string(function, &str);
373 if(FAILED(hres))
374 return hres;
375
376 if(r)
377 *r = jsval_string(str);
378 else
379 jsstr_release(str);
380 return S_OK;
381 }
382
383 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, unsigned *argc, jsval_t **ret)
384 {
385 jsval_t *argv, val;
386 DWORD length, i;
387 HRESULT hres;
388
389 hres = jsdisp_propget_name(arg_array, lengthW, &val);
390 if(FAILED(hres))
391 return hres;
392
393 hres = to_uint32(ctx, val, &length);
394 jsval_release(val);
395 if(FAILED(hres))
396 return hres;
397
398 argv = heap_alloc(length * sizeof(*argv));
399 if(!argv)
400 return E_OUTOFMEMORY;
401
402 for(i=0; i<length; i++) {
403 hres = jsdisp_get_idx(arg_array, i, argv+i);
404 if(hres == DISP_E_UNKNOWNNAME) {
405 argv[i] = jsval_undefined();
406 }else if(FAILED(hres)) {
407 while(i--)
408 jsval_release(argv[i]);
409 heap_free(argv);
410 return hres;
411 }
412 }
413
414 *argc = length;
415 *ret = argv;
416 return S_OK;
417 }
418
419 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
420 {
421 FunctionInstance *function;
422 jsval_t *args = NULL;
423 unsigned i, cnt = 0;
424 IDispatch *this_obj = NULL;
425 HRESULT hres = S_OK;
426
427 TRACE("\n");
428
429 if(!(function = function_this(jsthis)) && (jsthis->flags & VDISP_JSDISP))
430 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
431
432 if(argc) {
433 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
434 hres = to_object(ctx, argv[0], &this_obj);
435 if(FAILED(hres))
436 return hres;
437 }
438 }
439
440 if(argc >= 2) {
441 jsdisp_t *arg_array = NULL;
442
443 if(is_object_instance(argv[1])) {
444 arg_array = iface_to_jsdisp(get_object(argv[1]));
445 if(arg_array &&
446 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
447 jsdisp_release(arg_array);
448 arg_array = NULL;
449 }
450 }
451
452 if(arg_array) {
453 hres = array_to_args(ctx, arg_array, &cnt, &args);
454 jsdisp_release(arg_array);
455 }else {
456 FIXME("throw TypeError\n");
457 hres = E_FAIL;
458 }
459 }
460
461 if(SUCCEEDED(hres)) {
462 if(function) {
463 hres = call_function(ctx, function, this_obj, cnt, args, (flags & DISPATCH_JSCRIPT_CALLEREXECSSOURCE) != 0, r);
464 }else {
465 jsval_t res;
466 hres = disp_call_value(ctx, jsthis->u.disp, this_obj, DISPATCH_METHOD, cnt, args, &res);
467 if(SUCCEEDED(hres)) {
468 if(r)
469 *r = res;
470 else
471 jsval_release(res);
472 }
473 }
474 }
475
476 if(this_obj)
477 IDispatch_Release(this_obj);
478 for(i=0; i < cnt; i++)
479 jsval_release(args[i]);
480 heap_free(args);
481 return hres;
482 }
483
484 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
485 jsval_t *r)
486 {
487 FunctionInstance *function;
488 IDispatch *this_obj = NULL;
489 unsigned cnt = 0;
490 HRESULT hres;
491
492 TRACE("\n");
493
494 if(!(function = function_this(jsthis)))
495 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
496
497 if(argc) {
498 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
499 hres = to_object(ctx, argv[0], &this_obj);
500 if(FAILED(hres))
501 return hres;
502 }
503
504 cnt = argc-1;
505 }
506
507 hres = call_function(ctx, function, this_obj, cnt, argv+1, (flags & DISPATCH_JSCRIPT_CALLEREXECSSOURCE) != 0, r);
508
509 if(this_obj)
510 IDispatch_Release(this_obj);
511 return hres;
512 }
513
514 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
515 jsval_t *r)
516 {
517 FunctionInstance *function;
518
519 TRACE("\n");
520
521 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
522 ERR("dispex is not a function\n");
523 return E_FAIL;
524 }
525
526 function = (FunctionInstance*)jsthis->u.jsdisp;
527
528 assert(function->value_proc != NULL);
529 return invoke_value_proc(ctx, function, NULL, flags, argc, argv, r);
530 }
531
532 HRESULT Function_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
533 {
534 jsstr_t *str;
535 HRESULT hres;
536
537 TRACE("\n");
538
539 hres = function_to_string(function_from_jsdisp(jsthis), &str);
540 if(FAILED(hres))
541 return hres;
542
543 *r = jsval_string(str);
544 return S_OK;
545 }
546
547 static HRESULT Function_get_arguments(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
548 {
549 FunctionInstance *function = function_from_jsdisp(jsthis);
550 call_frame_t *frame;
551 HRESULT hres;
552
553 TRACE("\n");
554
555 for(frame = ctx->call_ctx; frame; frame = frame->prev_frame) {
556 if(frame->function_instance == &function->dispex) {
557 if(!frame->arguments_obj) {
558 hres = setup_arguments_object(ctx, frame);
559 if(FAILED(hres))
560 return hres;
561 }
562 *r = jsval_obj(jsdisp_addref(frame->arguments_obj));
563 return S_OK;
564 }
565 }
566
567 *r = jsval_null();
568 return S_OK;
569 }
570
571 static void Function_destructor(jsdisp_t *dispex)
572 {
573 FunctionInstance *This = (FunctionInstance*)dispex;
574
575 if(This->code)
576 release_bytecode(This->code);
577 if(This->scope_chain)
578 scope_release(This->scope_chain);
579 heap_free(This);
580 }
581
582 static const builtin_prop_t Function_props[] = {
583 {applyW, Function_apply, PROPF_METHOD|2},
584 {argumentsW, NULL, 0, Function_get_arguments, builtin_set_const},
585 {callW, Function_call, PROPF_METHOD|1},
586 {lengthW, NULL, 0, Function_get_length, Function_set_length},
587 {toStringW, Function_toString, PROPF_METHOD}
588 };
589
590 static const builtin_info_t Function_info = {
591 JSCLASS_FUNCTION,
592 DEFAULT_FUNCTION_VALUE,
593 sizeof(Function_props)/sizeof(*Function_props),
594 Function_props,
595 Function_destructor,
596 NULL
597 };
598
599 static const builtin_prop_t FunctionInst_props[] = {
600 {argumentsW, NULL, 0, Function_get_arguments, builtin_set_const},
601 {lengthW, NULL, 0, Function_get_length, Function_set_length}
602 };
603
604 static const builtin_info_t FunctionInst_info = {
605 JSCLASS_FUNCTION,
606 DEFAULT_FUNCTION_VALUE,
607 sizeof(FunctionInst_props)/sizeof(*FunctionInst_props),
608 FunctionInst_props,
609 Function_destructor,
610 NULL
611 };
612
613 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
614 BOOL funcprot, jsdisp_t *prototype, FunctionInstance **ret)
615 {
616 FunctionInstance *function;
617 HRESULT hres;
618
619 function = heap_alloc_zero(sizeof(FunctionInstance));
620 if(!function)
621 return E_OUTOFMEMORY;
622
623 if(funcprot)
624 hres = init_dispex(&function->dispex, ctx, builtin_info, prototype);
625 else if(builtin_info)
626 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
627 else
628 hres = init_dispex_from_constr(&function->dispex, ctx, &FunctionInst_info, ctx->function_constr);
629 if(FAILED(hres)) {
630 heap_free(function);
631 return hres;
632 }
633
634 function->flags = flags;
635 function->length = flags & PROPF_ARGMASK;
636
637 *ret = function;
638 return S_OK;
639 }
640
641 static inline HRESULT set_prototype(script_ctx_t *ctx, jsdisp_t *dispex, jsdisp_t *prototype)
642 {
643 return jsdisp_propput_dontenum(dispex, prototypeW, jsval_obj(prototype));
644 }
645
646 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
647 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
648 {
649 FunctionInstance *function;
650 HRESULT hres;
651
652 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
653 if(FAILED(hres))
654 return hres;
655
656 if(builtin_info)
657 hres = jsdisp_propput_const(&function->dispex, lengthW, jsval_number(function->length));
658 if(SUCCEEDED(hres))
659 hres = set_prototype(ctx, &function->dispex, prototype);
660 if(FAILED(hres)) {
661 jsdisp_release(&function->dispex);
662 return hres;
663 }
664
665 function->value_proc = value_proc;
666 function->name = name;
667
668 *ret = &function->dispex;
669 return S_OK;
670 }
671
672 static HRESULT set_constructor_prop(script_ctx_t *ctx, jsdisp_t *constr, jsdisp_t *prot)
673 {
674 static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
675
676 return jsdisp_propput_dontenum(prot, constructorW, jsval_obj(constr));
677 }
678
679 HRESULT create_builtin_constructor(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
680 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
681 {
682 jsdisp_t *constr;
683 HRESULT hres;
684
685 hres = create_builtin_function(ctx, value_proc, name, builtin_info, flags, prototype, &constr);
686 if(FAILED(hres))
687 return hres;
688
689 hres = set_constructor_prop(ctx, constr, prototype);
690 if(FAILED(hres)) {
691 jsdisp_release(constr);
692 return hres;
693 }
694
695 *ret = constr;
696 return S_OK;
697 }
698
699 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
700 scope_chain_t *scope_chain, jsdisp_t **ret)
701 {
702 FunctionInstance *function;
703 jsdisp_t *prototype;
704 HRESULT hres;
705
706 hres = create_object(ctx, NULL, &prototype);
707 if(FAILED(hres))
708 return hres;
709
710 hres = create_function(ctx, NULL, PROPF_CONSTR, FALSE, NULL, &function);
711 if(SUCCEEDED(hres)) {
712 hres = set_prototype(ctx, &function->dispex, prototype);
713 if(SUCCEEDED(hres))
714 hres = set_constructor_prop(ctx, &function->dispex, prototype);
715 if(FAILED(hres))
716 jsdisp_release(&function->dispex);
717 }
718 jsdisp_release(prototype);
719 if(FAILED(hres))
720 return hres;
721
722 if(scope_chain) {
723 scope_addref(scope_chain);
724 function->scope_chain = scope_chain;
725 }
726
727 bytecode_addref(code);
728 function->code = code;
729 function->func_code = func_code;
730 function->length = function->func_code->param_cnt;
731
732 *ret = &function->dispex;
733 return S_OK;
734 }
735
736 static HRESULT construct_function(script_ctx_t *ctx, unsigned argc, jsval_t *argv, IDispatch **ret)
737 {
738 WCHAR *str = NULL, *ptr;
739 unsigned len = 0, i = 0;
740 bytecode_t *code;
741 jsdisp_t *function;
742 jsstr_t **params = NULL;
743 int j = 0;
744 HRESULT hres = S_OK;
745
746 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
747 static const WCHAR function_beginW[] = {')',' ','{','\n'};
748 static const WCHAR function_endW[] = {'\n','}',0};
749
750 if(argc) {
751 params = heap_alloc(argc*sizeof(*params));
752 if(!params)
753 return E_OUTOFMEMORY;
754
755 if(argc > 2)
756 len = (argc-2)*2; /* separating commas */
757 for(i=0; i < argc; i++) {
758 hres = to_string(ctx, argv[i], params+i);
759 if(FAILED(hres))
760 break;
761 len += jsstr_length(params[i]);
762 }
763 }
764
765 if(SUCCEEDED(hres)) {
766 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
767 str = heap_alloc(len*sizeof(WCHAR));
768 if(str) {
769 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
770 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
771 if(argc > 1) {
772 while(1) {
773 ptr += jsstr_flush(params[j], ptr);
774 if(++j == argc-1)
775 break;
776 *ptr++ = ',';
777 *ptr++ = ' ';
778 }
779 }
780 memcpy(ptr, function_beginW, sizeof(function_beginW));
781 ptr += sizeof(function_beginW)/sizeof(WCHAR);
782 if(argc)
783 ptr += jsstr_flush(params[argc-1], ptr);
784 memcpy(ptr, function_endW, sizeof(function_endW));
785
786 TRACE("%s\n", debugstr_w(str));
787 }else {
788 hres = E_OUTOFMEMORY;
789 }
790 }
791
792 while(i)
793 jsstr_release(params[--i]);
794 heap_free(params);
795 if(FAILED(hres))
796 return hres;
797
798 hres = compile_script(ctx, str, NULL, NULL, FALSE, FALSE, &code);
799 heap_free(str);
800 if(FAILED(hres))
801 return hres;
802
803 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt != 1) {
804 ERR("Invalid parser result!\n");
805 release_bytecode(code);
806 return E_UNEXPECTED;
807 }
808
809 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
810 release_bytecode(code);
811 if(FAILED(hres))
812 return hres;
813
814 *ret = to_disp(function);
815 return S_OK;
816 }
817
818 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
819 jsval_t *r)
820 {
821 HRESULT hres;
822
823 TRACE("\n");
824
825 switch(flags) {
826 case DISPATCH_METHOD:
827 case DISPATCH_CONSTRUCT: {
828 IDispatch *ret;
829
830 hres = construct_function(ctx, argc, argv, &ret);
831 if(FAILED(hres))
832 return hres;
833
834 *r = jsval_disp(ret);
835 break;
836 }
837 default:
838 FIXME("unimplemented flags %x\n", flags);
839 return E_NOTIMPL;
840 }
841
842 return S_OK;
843 }
844
845 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
846 jsval_t *r)
847 {
848 FIXME("\n");
849 return E_NOTIMPL;
850 }
851
852 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
853 {
854 FunctionInstance *prot, *constr;
855 HRESULT hres;
856
857 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
858
859 hres = create_function(ctx, &Function_info, PROPF_CONSTR, TRUE, object_prototype, &prot);
860 if(FAILED(hres))
861 return hres;
862
863 prot->value_proc = FunctionProt_value;
864 prot->name = prototypeW;
865
866 hres = create_function(ctx, &FunctionInst_info, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
867 if(SUCCEEDED(hres)) {
868 constr->value_proc = FunctionConstr_value;
869 constr->name = FunctionW;
870 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
871 if(SUCCEEDED(hres))
872 hres = set_constructor_prop(ctx, &constr->dispex, &prot->dispex);
873 if(FAILED(hres))
874 jsdisp_release(&constr->dispex);
875 }
876 jsdisp_release(&prot->dispex);
877 if(FAILED(hres))
878 return hres;
879
880 ctx->function_constr = &constr->dispex;
881 return S_OK;
882 }