richedit: Don't put cursor in the table row start paragraph.
[wine/hacks.git] / dlls / jscript / function.c
blob4c07cb3e58786760ecd4e4037ea75845c2eff3a9
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 "jscript.h"
20 #include "engine.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
26 typedef struct {
27 DispatchEx dispex;
28 builtin_invoke_t value_proc;
29 DWORD flags;
30 source_elements_t *source;
31 parameter_t *parameters;
32 scope_chain_t *scope_chain;
33 parser_ctx_t *parser;
34 DWORD length;
35 } FunctionInstance;
37 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
39 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
40 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
41 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
42 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
43 static const WCHAR applyW[] = {'a','p','p','l','y',0};
44 static const WCHAR callW[] = {'c','a','l','l',0};
45 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
46 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};
47 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
49 static IDispatch *get_this(DISPPARAMS *dp)
51 DWORD i;
53 for(i=0; i < dp->cNamedArgs; i++) {
54 if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
55 if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
56 return V_DISPATCH(dp->rgvarg+i);
58 WARN("This is not VT_DISPATCH\n");
59 return NULL;
63 TRACE("no this passed\n");
64 return NULL;
67 static HRESULT init_parameters(DispatchEx *var_disp, FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
68 jsexcept_t *ei, IServiceProvider *caller)
70 parameter_t *param;
71 VARIANT var_empty;
72 DWORD cargs, i=0;
73 HRESULT hres;
75 V_VT(&var_empty) = VT_EMPTY;
76 cargs = dp->cArgs - dp->cNamedArgs;
78 for(param = function->parameters; param; param = param->next) {
79 hres = jsdisp_propput_name(var_disp, param->identifier, lcid,
80 i < cargs ? dp->rgvarg + dp->cArgs-1 - i : &var_empty,
81 ei, caller);
82 if(FAILED(hres))
83 return hres;
85 i++;
88 return S_OK;
91 static HRESULT init_arguments(DispatchEx *arg_disp, FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
92 jsexcept_t *ei, IServiceProvider *caller)
94 VARIANT var;
95 DWORD i;
96 HRESULT hres;
98 for(i=0; i < dp->cArgs-dp->cNamedArgs; i++) {
99 hres = jsdisp_propput_idx(arg_disp, i, lcid, dp->rgvarg+dp->cArgs-1-i, ei, caller);
100 if(FAILED(hres))
101 return hres;
104 V_VT(&var) = VT_I4;
105 V_I4(&var) = dp->cArgs - dp->cNamedArgs;
106 return jsdisp_propput_name(arg_disp, lengthW, lcid, &var, ei, caller);
109 static HRESULT create_var_disp(FunctionInstance *function, LCID lcid, DISPPARAMS *dp, jsexcept_t *ei,
110 IServiceProvider *caller, DispatchEx **ret)
112 DispatchEx *var_disp, *arg_disp;
113 HRESULT hres;
115 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
117 hres = create_dispex(function->dispex.ctx, NULL, NULL, &var_disp);
118 if(FAILED(hres))
119 return hres;
121 hres = create_dispex(function->dispex.ctx, NULL, NULL, &arg_disp);
122 if(SUCCEEDED(hres)) {
123 hres = init_arguments(arg_disp, function, lcid, dp, ei, caller);
124 if(SUCCEEDED(hres)) {
125 VARIANT var;
127 V_VT(&var) = VT_DISPATCH;
128 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(arg_disp);
129 hres = jsdisp_propput_name(var_disp, argumentsW, lcid, &var, ei, caller);
132 jsdisp_release(arg_disp);
135 if(SUCCEEDED(hres))
136 hres = init_parameters(var_disp, function, lcid, dp, ei, caller);
137 if(FAILED(hres)) {
138 jsdisp_release(var_disp);
139 return hres;
142 *ret = var_disp;
143 return S_OK;
146 static HRESULT invoke_source(FunctionInstance *function, IDispatch *this_obj, LCID lcid, DISPPARAMS *dp,
147 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
149 DispatchEx *var_disp;
150 exec_ctx_t *exec_ctx;
151 scope_chain_t *scope;
152 HRESULT hres;
154 if(!function->source) {
155 FIXME("no source\n");
156 return E_FAIL;
159 hres = create_var_disp(function, lcid, dp, ei, caller, &var_disp);
160 if(FAILED(hres))
161 return hres;
163 hres = scope_push(function->scope_chain, var_disp, &scope);
164 if(SUCCEEDED(hres)) {
165 hres = create_exec_ctx(this_obj, var_disp, scope, &exec_ctx);
166 scope_release(scope);
168 if(FAILED(hres))
169 return hres;
171 hres = exec_source(exec_ctx, function->parser, function->source, ei, retv);
172 exec_release(exec_ctx);
174 return hres;
177 static HRESULT invoke_function(FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
178 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
180 IDispatch *this_obj;
182 if(!(this_obj = get_this(dp)))
183 this_obj = (IDispatch*)_IDispatchEx_(function->dispex.ctx->script_disp);
185 return invoke_source(function, this_obj, lcid, dp, retv, ei, caller);
188 static HRESULT invoke_constructor(FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
189 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
191 DispatchEx *this_obj;
192 VARIANT var;
193 HRESULT hres;
195 hres = create_object(function->dispex.ctx, &function->dispex, &this_obj);
196 if(FAILED(hres))
197 return hres;
199 hres = invoke_source(function, (IDispatch*)_IDispatchEx_(this_obj), lcid, dp, retv, ei, caller);
200 jsdisp_release(this_obj);
201 if(FAILED(hres))
202 return hres;
204 VariantClear(&var);
205 V_VT(retv) = VT_DISPATCH;
206 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(this_obj);
207 return S_OK;
210 static HRESULT invoke_value_proc(FunctionInstance *function, LCID lcid, WORD flags, DISPPARAMS *dp,
211 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
213 DispatchEx *this_obj = NULL;
214 IDispatch *this_disp;
215 HRESULT hres;
217 this_disp = get_this(dp);
218 if(this_disp)
219 this_obj = iface_to_jsdisp((IUnknown*)this_disp);
221 hres = function->value_proc(this_obj ? this_obj : function->dispex.ctx->script_disp, lcid,
222 flags, dp, retv, ei, caller);
224 if(this_obj)
225 jsdisp_release(this_obj);
226 return hres;
229 static HRESULT Function_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
230 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
232 FunctionInstance *This = (FunctionInstance*)dispex;
234 TRACE("%p %d\n", This, This->length);
236 switch(flags) {
237 case DISPATCH_PROPERTYGET:
238 V_VT(retv) = VT_I4;
239 V_I4(retv) = This->length;
240 break;
241 default:
242 FIXME("unimplemented flags %x\n", flags);
243 return E_NOTIMPL;
246 return S_OK;
249 static HRESULT Function_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
250 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
252 FIXME("\n");
253 return E_NOTIMPL;
256 static HRESULT Function_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
257 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
259 FIXME("\n");
260 return E_NOTIMPL;
263 static HRESULT Function_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
264 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
266 FIXME("\n");
267 return E_NOTIMPL;
270 static HRESULT Function_apply(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
271 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
273 FIXME("\n");
274 return E_NOTIMPL;
277 static HRESULT Function_call(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
278 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
280 FIXME("\n");
281 return E_NOTIMPL;
284 static HRESULT Function_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
285 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
287 FIXME("\n");
288 return E_NOTIMPL;
291 static HRESULT Function_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
292 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
294 FIXME("\n");
295 return E_NOTIMPL;
298 static HRESULT Function_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
299 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
301 FIXME("\n");
302 return E_NOTIMPL;
305 static HRESULT Function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
306 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
308 FunctionInstance *function;
310 TRACE("\n");
312 if(dispex->builtin_info->class != JSCLASS_FUNCTION) {
313 ERR("dispex is not a function\n");
314 return E_FAIL;
317 function = (FunctionInstance*)dispex;
319 switch(flags) {
320 case DISPATCH_METHOD:
321 if(function->value_proc)
322 return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
324 return invoke_function(function, lcid, dp, retv, ei, caller);
326 case DISPATCH_CONSTRUCT:
327 if(function->value_proc)
328 return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
330 return invoke_constructor(function, lcid, dp, retv, ei, caller);
332 default:
333 FIXME("not implemented flags %x\n", flags);
334 return E_NOTIMPL;
337 return S_OK;
340 static void Function_destructor(DispatchEx *dispex)
342 FunctionInstance *This = (FunctionInstance*)dispex;
344 if(This->parser)
345 parser_release(This->parser);
346 if(This->scope_chain)
347 scope_release(This->scope_chain);
348 heap_free(This);
351 static const builtin_prop_t Function_props[] = {
352 {applyW, Function_apply, PROPF_METHOD},
353 {callW, Function_call, PROPF_METHOD},
354 {hasOwnPropertyW, Function_hasOwnProperty, PROPF_METHOD},
355 {isPrototypeOfW, Function_isPrototypeOf, PROPF_METHOD},
356 {lengthW, Function_length, 0},
357 {propertyIsEnumerableW, Function_propertyIsEnumerable, PROPF_METHOD},
358 {toLocaleStringW, Function_toLocaleString, PROPF_METHOD},
359 {toStringW, Function_toString, PROPF_METHOD},
360 {valueOfW, Function_valueOf, PROPF_METHOD}
363 static const builtin_info_t Function_info = {
364 JSCLASS_FUNCTION,
365 {NULL, Function_value, 0},
366 sizeof(Function_props)/sizeof(*Function_props),
367 Function_props,
368 Function_destructor,
369 NULL
372 static HRESULT create_function(script_ctx_t *ctx, DWORD flags, DispatchEx *prototype, FunctionInstance **ret)
374 FunctionInstance *function;
375 HRESULT hres;
377 function = heap_alloc_zero(sizeof(FunctionInstance));
378 if(!function)
379 return E_OUTOFMEMORY;
381 hres = init_dispex(&function->dispex, ctx, &Function_info, NULL);
382 if(FAILED(hres))
383 return hres;
385 function->flags = flags;
386 function->length = flags & PROPF_ARGMASK;
388 if(prototype) {
389 jsexcept_t jsexcept;
390 VARIANT var;
392 V_VT(&var) = VT_DISPATCH;
393 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
394 memset(&jsexcept, 0, sizeof(jsexcept));
396 hres = jsdisp_propput_name(&function->dispex, prototypeW, ctx->lcid, &var, &jsexcept, NULL/*FIXME*/);
397 if(FAILED(hres)) {
398 IDispatchEx_Release(_IDispatchEx_(&function->dispex));
399 return hres;
403 *ret = function;
404 return S_OK;
407 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, DWORD flags,
408 DispatchEx *prototype, DispatchEx **ret)
410 FunctionInstance *function;
411 HRESULT hres;
413 hres = create_function(ctx, flags, prototype, &function);
414 if(FAILED(hres))
415 return hres;
417 function->value_proc = value_proc;
419 *ret = &function->dispex;
420 return S_OK;
423 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
424 scope_chain_t *scope_chain, DispatchEx **ret)
426 FunctionInstance *function;
427 DispatchEx *prototype;
428 parameter_t *iter;
429 DWORD length = 0;
430 HRESULT hres;
432 hres = create_object(ctx->script, NULL, &prototype);
433 if(FAILED(hres))
434 return hres;
436 hres = create_function(ctx->script, PROPF_CONSTR, prototype, &function);
437 jsdisp_release(prototype);
438 if(FAILED(hres))
439 return hres;
441 function->source = source;
442 function->parameters = parameters;
444 if(scope_chain) {
445 scope_addref(scope_chain);
446 function->scope_chain = scope_chain;
449 parser_addref(ctx);
450 function->parser = ctx;
452 for(iter = parameters; iter; iter = iter->next)
453 length++;
454 function->length = length;
456 *ret = &function->dispex;
457 return S_OK;