jscript: Add static functions to variable objects.
[wine/hacks.git] / dlls / jscript / function.c
blob96fbf787ec52c88897b5649dec36d50da5cd0c1a
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 lengthW[] = {'l','e','n','g','t','h',0};
38 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
39 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
40 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
41 static const WCHAR applyW[] = {'a','p','p','l','y',0};
42 static const WCHAR callW[] = {'c','a','l','l',0};
43 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
44 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};
45 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
47 static HRESULT Function_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
48 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
50 FunctionInstance *This = (FunctionInstance*)dispex;
52 TRACE("%p %d\n", This, This->length);
54 switch(flags) {
55 case DISPATCH_PROPERTYGET:
56 V_VT(retv) = VT_I4;
57 V_I4(retv) = This->length;
58 break;
59 default:
60 FIXME("unimplemented flags %x\n", flags);
61 return E_NOTIMPL;
64 return S_OK;
67 static HRESULT Function_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
68 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
70 FIXME("\n");
71 return E_NOTIMPL;
74 static HRESULT Function_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
75 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
77 FIXME("\n");
78 return E_NOTIMPL;
81 static HRESULT Function_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
82 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
84 FIXME("\n");
85 return E_NOTIMPL;
88 static HRESULT Function_apply(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
89 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
91 FIXME("\n");
92 return E_NOTIMPL;
95 static HRESULT Function_call(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
96 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
98 FIXME("\n");
99 return E_NOTIMPL;
102 static HRESULT Function_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
103 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
105 FIXME("\n");
106 return E_NOTIMPL;
109 static HRESULT Function_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
110 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
112 FIXME("\n");
113 return E_NOTIMPL;
116 static HRESULT Function_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
117 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
119 FIXME("\n");
120 return E_NOTIMPL;
123 static HRESULT Function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
124 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
126 FIXME("\n");
127 return E_NOTIMPL;
130 static void Function_destructor(DispatchEx *dispex)
132 FunctionInstance *This = (FunctionInstance*)dispex;
134 if(This->parser)
135 parser_release(This->parser);
136 if(This->scope_chain)
137 scope_release(This->scope_chain);
138 heap_free(This);
141 static const builtin_prop_t Function_props[] = {
142 {applyW, Function_apply, PROPF_METHOD},
143 {callW, Function_call, PROPF_METHOD},
144 {hasOwnPropertyW, Function_hasOwnProperty, PROPF_METHOD},
145 {isPrototypeOfW, Function_isPrototypeOf, PROPF_METHOD},
146 {lengthW, Function_length, 0},
147 {propertyIsEnumerableW, Function_propertyIsEnumerable, PROPF_METHOD},
148 {toLocaleStringW, Function_toLocaleString, PROPF_METHOD},
149 {toStringW, Function_toString, PROPF_METHOD},
150 {valueOfW, Function_valueOf, PROPF_METHOD}
153 static const builtin_info_t Function_info = {
154 JSCLASS_FUNCTION,
155 {NULL, Function_value, 0},
156 sizeof(Function_props)/sizeof(*Function_props),
157 Function_props,
158 Function_destructor,
159 NULL
162 static HRESULT create_function(script_ctx_t *ctx, DWORD flags, DispatchEx *prototype, FunctionInstance **ret)
164 FunctionInstance *function;
165 HRESULT hres;
167 function = heap_alloc_zero(sizeof(FunctionInstance));
168 if(!function)
169 return E_OUTOFMEMORY;
171 hres = init_dispex(&function->dispex, ctx, &Function_info, NULL);
172 if(FAILED(hres))
173 return hres;
175 function->flags = flags;
176 function->length = flags & PROPF_ARGMASK;
178 if(prototype) {
179 hres = jsdisp_set_prototype(&function->dispex, prototype);
180 if(FAILED(hres)) {
181 IDispatchEx_Release(_IDispatchEx_(&function->dispex));
182 return hres;
186 *ret = function;
187 return S_OK;
190 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, DWORD flags,
191 DispatchEx *prototype, DispatchEx **ret)
193 FunctionInstance *function;
194 HRESULT hres;
196 hres = create_function(ctx, flags, prototype, &function);
197 if(FAILED(hres))
198 return hres;
200 function->value_proc = value_proc;
202 *ret = &function->dispex;
203 return S_OK;
206 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
207 scope_chain_t *scope_chain, DispatchEx **ret)
209 FunctionInstance *function;
210 parameter_t *iter;
211 DWORD length = 0;
212 HRESULT hres;
214 hres = create_function(ctx->script, PROPF_CONSTR, NULL, &function);
215 if(FAILED(hres))
216 return hres;
218 function->source = source;
219 function->parameters = parameters;
221 if(scope_chain) {
222 scope_addref(scope_chain);
223 function->scope_chain = scope_chain;
226 parser_addref(ctx);
227 function->parser = ctx;
229 for(iter = parameters; iter; iter = iter->next)
230 length++;
231 function->length = length;
233 *ret = &function->dispex;
234 return S_OK;