2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
28 builtin_invoke_t value_proc
;
30 source_elements_t
*source
;
31 parameter_t
*parameters
;
32 scope_chain_t
*scope_chain
;
39 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
41 static const WCHAR lengthW
[] = {'l','e','n','g','t','h',0};
42 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
43 static const WCHAR toLocaleStringW
[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
44 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
45 static const WCHAR applyW
[] = {'a','p','p','l','y',0};
46 static const WCHAR callW
[] = {'c','a','l','l',0};
47 static const WCHAR hasOwnPropertyW
[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
48 static const WCHAR propertyIsEnumerableW
[] = {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
49 static const WCHAR isPrototypeOfW
[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
51 static IDispatch
*get_this(DISPPARAMS
*dp
)
55 for(i
=0; i
< dp
->cNamedArgs
; i
++) {
56 if(dp
->rgdispidNamedArgs
[i
] == DISPID_THIS
) {
57 if(V_VT(dp
->rgvarg
+i
) == VT_DISPATCH
)
58 return V_DISPATCH(dp
->rgvarg
+i
);
60 WARN("This is not VT_DISPATCH\n");
65 TRACE("no this passed\n");
69 static HRESULT
init_parameters(DispatchEx
*var_disp
, FunctionInstance
*function
, LCID lcid
, DISPPARAMS
*dp
,
70 jsexcept_t
*ei
, IServiceProvider
*caller
)
77 V_VT(&var_empty
) = VT_EMPTY
;
78 cargs
= dp
->cArgs
- dp
->cNamedArgs
;
80 for(param
= function
->parameters
; param
; param
= param
->next
) {
81 hres
= jsdisp_propput_name(var_disp
, param
->identifier
, lcid
,
82 i
< cargs
? dp
->rgvarg
+ dp
->cArgs
-1 - i
: &var_empty
,
93 static HRESULT
init_arguments(DispatchEx
*arg_disp
, FunctionInstance
*function
, LCID lcid
, DISPPARAMS
*dp
,
94 jsexcept_t
*ei
, IServiceProvider
*caller
)
100 for(i
=0; i
< dp
->cArgs
-dp
->cNamedArgs
; i
++) {
101 hres
= jsdisp_propput_idx(arg_disp
, i
, lcid
, dp
->rgvarg
+dp
->cArgs
-1-i
, ei
, caller
);
107 V_I4(&var
) = dp
->cArgs
- dp
->cNamedArgs
;
108 return jsdisp_propput_name(arg_disp
, lengthW
, lcid
, &var
, ei
, caller
);
111 static HRESULT
create_var_disp(FunctionInstance
*function
, LCID lcid
, DISPPARAMS
*dp
, jsexcept_t
*ei
,
112 IServiceProvider
*caller
, DispatchEx
**ret
)
114 DispatchEx
*var_disp
, *arg_disp
;
117 static const WCHAR argumentsW
[] = {'a','r','g','u','m','e','n','t','s',0};
119 hres
= create_dispex(function
->dispex
.ctx
, NULL
, NULL
, &var_disp
);
123 hres
= create_dispex(function
->dispex
.ctx
, NULL
, NULL
, &arg_disp
);
124 if(SUCCEEDED(hres
)) {
125 hres
= init_arguments(arg_disp
, function
, lcid
, dp
, ei
, caller
);
126 if(SUCCEEDED(hres
)) {
129 V_VT(&var
) = VT_DISPATCH
;
130 V_DISPATCH(&var
) = (IDispatch
*)_IDispatchEx_(arg_disp
);
131 hres
= jsdisp_propput_name(var_disp
, argumentsW
, lcid
, &var
, ei
, caller
);
134 jsdisp_release(arg_disp
);
138 hres
= init_parameters(var_disp
, function
, lcid
, dp
, ei
, caller
);
140 jsdisp_release(var_disp
);
148 static HRESULT
invoke_source(FunctionInstance
*function
, IDispatch
*this_obj
, LCID lcid
, DISPPARAMS
*dp
,
149 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
151 DispatchEx
*var_disp
;
152 exec_ctx_t
*exec_ctx
;
153 scope_chain_t
*scope
;
156 if(!function
->source
) {
157 FIXME("no source\n");
161 hres
= create_var_disp(function
, lcid
, dp
, ei
, caller
, &var_disp
);
165 hres
= scope_push(function
->scope_chain
, var_disp
, &scope
);
166 if(SUCCEEDED(hres
)) {
167 hres
= create_exec_ctx(this_obj
, var_disp
, scope
, &exec_ctx
);
168 scope_release(scope
);
173 hres
= exec_source(exec_ctx
, function
->parser
, function
->source
, ei
, retv
);
174 exec_release(exec_ctx
);
179 static HRESULT
invoke_function(FunctionInstance
*function
, LCID lcid
, DISPPARAMS
*dp
,
180 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
184 if(!(this_obj
= get_this(dp
)))
185 this_obj
= (IDispatch
*)_IDispatchEx_(function
->dispex
.ctx
->script_disp
);
187 return invoke_source(function
, this_obj
, lcid
, dp
, retv
, ei
, caller
);
190 static HRESULT
invoke_constructor(FunctionInstance
*function
, LCID lcid
, DISPPARAMS
*dp
,
191 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
193 DispatchEx
*this_obj
;
197 hres
= create_object(function
->dispex
.ctx
, &function
->dispex
, &this_obj
);
201 hres
= invoke_source(function
, (IDispatch
*)_IDispatchEx_(this_obj
), lcid
, dp
, retv
, ei
, caller
);
202 jsdisp_release(this_obj
);
207 V_VT(retv
) = VT_DISPATCH
;
208 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(this_obj
);
212 static HRESULT
invoke_value_proc(FunctionInstance
*function
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
213 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
215 DispatchEx
*this_obj
= NULL
;
216 IDispatch
*this_disp
;
219 this_disp
= get_this(dp
);
221 this_obj
= iface_to_jsdisp((IUnknown
*)this_disp
);
223 hres
= function
->value_proc(this_obj
? this_obj
: function
->dispex
.ctx
->script_disp
, lcid
,
224 flags
, dp
, retv
, ei
, caller
);
227 jsdisp_release(this_obj
);
231 static HRESULT
function_to_string(FunctionInstance
*function
, BSTR
*ret
)
235 if(function
->value_proc
) {
236 FIXME("Builtin functions not implemented\n");
240 str
= SysAllocStringLen(function
->src_str
, function
->src_len
);
242 return E_OUTOFMEMORY
;
248 static HRESULT
Function_length(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
249 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
251 FunctionInstance
*This
= (FunctionInstance
*)dispex
;
253 TRACE("%p %d\n", This
, This
->length
);
256 case DISPATCH_PROPERTYGET
:
258 V_I4(retv
) = This
->length
;
261 FIXME("unimplemented flags %x\n", flags
);
268 static HRESULT
Function_toString(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
269 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
271 FunctionInstance
*function
;
277 if(!is_class(dispex
, JSCLASS_FUNCTION
)) {
278 FIXME("throw TypeError\n");
282 function
= (FunctionInstance
*)dispex
;
284 hres
= function_to_string(function
, &str
);
289 V_VT(retv
) = VT_BSTR
;
297 static HRESULT
Function_toLocaleString(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
298 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
304 static HRESULT
Function_valueOf(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
305 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
311 static HRESULT
Function_apply(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
312 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
318 static HRESULT
Function_call(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
319 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
325 static HRESULT
Function_hasOwnProperty(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
326 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
332 static HRESULT
Function_propertyIsEnumerable(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
333 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
339 static HRESULT
Function_isPrototypeOf(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
340 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
346 static HRESULT
Function_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
347 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
349 FunctionInstance
*function
;
353 if(dispex
->builtin_info
->class != JSCLASS_FUNCTION
) {
354 ERR("dispex is not a function\n");
358 function
= (FunctionInstance
*)dispex
;
361 case DISPATCH_METHOD
:
362 if(function
->value_proc
)
363 return invoke_value_proc(function
, lcid
, flags
, dp
, retv
, ei
, caller
);
365 return invoke_function(function
, lcid
, dp
, retv
, ei
, caller
);
367 case DISPATCH_PROPERTYGET
: {
371 hres
= function_to_string(function
, &str
);
375 V_VT(retv
) = VT_BSTR
;
380 case DISPATCH_CONSTRUCT
:
381 if(function
->value_proc
)
382 return invoke_value_proc(function
, lcid
, flags
, dp
, retv
, ei
, caller
);
384 return invoke_constructor(function
, lcid
, dp
, retv
, ei
, caller
);
387 FIXME("not implemented flags %x\n", flags
);
394 static void Function_destructor(DispatchEx
*dispex
)
396 FunctionInstance
*This
= (FunctionInstance
*)dispex
;
399 parser_release(This
->parser
);
400 if(This
->scope_chain
)
401 scope_release(This
->scope_chain
);
405 static const builtin_prop_t Function_props
[] = {
406 {applyW
, Function_apply
, PROPF_METHOD
},
407 {callW
, Function_call
, PROPF_METHOD
},
408 {hasOwnPropertyW
, Function_hasOwnProperty
, PROPF_METHOD
},
409 {isPrototypeOfW
, Function_isPrototypeOf
, PROPF_METHOD
},
410 {lengthW
, Function_length
, 0},
411 {propertyIsEnumerableW
, Function_propertyIsEnumerable
, PROPF_METHOD
},
412 {toLocaleStringW
, Function_toLocaleString
, PROPF_METHOD
},
413 {toStringW
, Function_toString
, PROPF_METHOD
},
414 {valueOfW
, Function_valueOf
, PROPF_METHOD
}
417 static const builtin_info_t Function_info
= {
419 {NULL
, Function_value
, 0},
420 sizeof(Function_props
)/sizeof(*Function_props
),
426 static HRESULT
FunctionConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
427 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
433 static HRESULT
FunctionProt_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
434 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
440 static HRESULT
create_function(script_ctx_t
*ctx
, DWORD flags
, BOOL funcprot
, DispatchEx
*prototype
, FunctionInstance
**ret
)
442 FunctionInstance
*function
;
445 function
= heap_alloc_zero(sizeof(FunctionInstance
));
447 return E_OUTOFMEMORY
;
450 hres
= init_dispex(&function
->dispex
, ctx
, &Function_info
, prototype
);
452 hres
= init_dispex_from_constr(&function
->dispex
, ctx
, &Function_info
, ctx
->function_constr
);
456 function
->flags
= flags
;
457 function
->length
= flags
& PROPF_ARGMASK
;
463 V_VT(&var
) = VT_DISPATCH
;
464 V_DISPATCH(&var
) = (IDispatch
*)_IDispatchEx_(prototype
);
465 memset(&jsexcept
, 0, sizeof(jsexcept
));
467 hres
= jsdisp_propput_name(&function
->dispex
, prototypeW
, ctx
->lcid
, &var
, &jsexcept
, NULL
/*FIXME*/);
469 IDispatchEx_Release(_IDispatchEx_(&function
->dispex
));
478 HRESULT
create_builtin_function(script_ctx_t
*ctx
, builtin_invoke_t value_proc
, DWORD flags
,
479 DispatchEx
*prototype
, DispatchEx
**ret
)
481 FunctionInstance
*function
;
484 hres
= create_function(ctx
, flags
, FALSE
, prototype
, &function
);
488 function
->value_proc
= value_proc
;
490 *ret
= &function
->dispex
;
494 HRESULT
create_source_function(parser_ctx_t
*ctx
, parameter_t
*parameters
, source_elements_t
*source
,
495 scope_chain_t
*scope_chain
, const WCHAR
*src_str
, DWORD src_len
, DispatchEx
**ret
)
497 FunctionInstance
*function
;
498 DispatchEx
*prototype
;
503 hres
= create_object(ctx
->script
, NULL
, &prototype
);
507 hres
= create_function(ctx
->script
, PROPF_CONSTR
, FALSE
, prototype
, &function
);
508 jsdisp_release(prototype
);
512 function
->source
= source
;
513 function
->parameters
= parameters
;
516 scope_addref(scope_chain
);
517 function
->scope_chain
= scope_chain
;
521 function
->parser
= ctx
;
523 for(iter
= parameters
; iter
; iter
= iter
->next
)
525 function
->length
= length
;
527 function
->src_str
= src_str
;
528 function
->src_len
= src_len
;
530 *ret
= &function
->dispex
;
534 HRESULT
init_function_constr(script_ctx_t
*ctx
)
536 FunctionInstance
*prot
, *constr
;
539 hres
= create_function(ctx
, PROPF_CONSTR
, TRUE
, NULL
, &prot
);
543 prot
->value_proc
= FunctionProt_value
;
545 hres
= create_function(ctx
, PROPF_CONSTR
, TRUE
, &prot
->dispex
, &constr
);
546 jsdisp_release(&prot
->dispex
);
550 constr
->value_proc
= FunctionConstr_value
;
551 ctx
->function_constr
= &constr
->dispex
;