jscript: Make String_indexOf generic.
[wine] / dlls / jscript / bool.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  * Copyright 2009 Piotr Caban
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include "jscript.h"
21
22 #include "wine/debug.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
25
26 typedef struct {
27     DispatchEx dispex;
28
29     VARIANT_BOOL val;
30 } BoolInstance;
31
32 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
33 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
34 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
35 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
36 static const WCHAR propertyIsEnumerableW[] =
37     {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
38 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
39
40 /* ECMA-262 3rd Edition    15.6.4.2 */
41 static HRESULT Bool_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
42         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
43 {
44     static const WCHAR trueW[] = {'t','r','u','e',0};
45     static const WCHAR falseW[] = {'f','a','l','s','e',0};
46
47     TRACE("\n");
48
49     if(!is_class(dispex, JSCLASS_BOOLEAN)) {
50         FIXME("throw TypeError\n");
51         return E_FAIL;
52     }
53
54     if(retv) {
55         BoolInstance *bool = (BoolInstance*)dispex;
56         BSTR val;
57
58         if(bool->val) val = SysAllocString(trueW);
59         else val = SysAllocString(falseW);
60
61         if(!val)
62             return E_OUTOFMEMORY;
63
64         V_VT(retv) = VT_BSTR;
65         V_BSTR(retv) = val;
66     }
67
68     return S_OK;
69 }
70
71 static HRESULT Bool_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
72         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
73 {
74     TRACE("\n");
75     return Bool_toString(dispex, lcid, flags, dp, retv, ei, sp);
76 }
77
78 /* ECMA-262 3rd Edition    15.6.4.3 */
79 static HRESULT Bool_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
80         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
81 {
82     TRACE("\n");
83
84     if(!is_class(dispex, JSCLASS_BOOLEAN)) {
85         FIXME("throw TypeError\n");
86         return E_FAIL;
87     }
88
89     if(retv) {
90         BoolInstance *bool = (BoolInstance*)dispex;
91
92         V_VT(retv) = VT_BOOL;
93         V_BOOL(retv) = bool->val;
94     }
95
96     return S_OK;
97 }
98
99 static HRESULT Bool_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
100         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
101 {
102     FIXME("\n");
103     return E_NOTIMPL;
104 }
105
106 static HRESULT Bool_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
107         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
108 {
109     FIXME("\n");
110     return E_NOTIMPL;
111 }
112
113 static HRESULT Bool_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
114         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
115 {
116     FIXME("\n");
117     return E_NOTIMPL;
118 }
119
120 static HRESULT Bool_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
121         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
122 {
123     FIXME("\n");
124     return E_NOTIMPL;
125 }
126
127 static const builtin_prop_t Bool_props[] = {
128     {hasOwnPropertyW,        Bool_hasOwnProperty,       PROPF_METHOD},
129     {isPrototypeOfW,         Bool_isPrototypeOf,        PROPF_METHOD},
130     {propertyIsEnumerableW,  Bool_propertyIsEnumerable, PROPF_METHOD},
131     {toLocaleStringW,        Bool_toLocaleString,       PROPF_METHOD},
132     {toStringW,              Bool_toString,             PROPF_METHOD},
133     {valueOfW,               Bool_valueOf,              PROPF_METHOD}
134 };
135
136 static const builtin_info_t Bool_info = {
137     JSCLASS_BOOLEAN,
138     {NULL, Bool_value, 0},
139     sizeof(Bool_props)/sizeof(*Bool_props),
140     Bool_props,
141     NULL,
142     NULL
143 };
144
145 static HRESULT BoolConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
146         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
147 {
148     HRESULT hres;
149     VARIANT_BOOL value = VARIANT_FALSE;
150
151     if(arg_cnt(dp)) {
152         hres = to_boolean(get_arg(dp,0), &value);
153         if(FAILED(hres))
154             return hres;
155     }
156
157     switch(flags) {
158     case DISPATCH_CONSTRUCT: {
159         DispatchEx *bool;
160
161         hres = create_bool(dispex->ctx, value, &bool);
162         if(FAILED(hres))
163             return hres;
164
165         V_VT(retv) = VT_DISPATCH;
166         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(bool);
167         return S_OK;
168     }
169
170     case INVOKE_FUNC:
171         if(retv) {
172             V_VT(retv) = VT_BOOL;
173             V_BOOL(retv) = value;
174         }
175         return S_OK;
176
177     default:
178         FIXME("unimplemented flags %x\n", flags);
179         return E_NOTIMPL;
180     }
181
182     return S_OK;
183 }
184
185 static HRESULT alloc_bool(script_ctx_t *ctx, BOOL use_constr, BoolInstance **ret)
186 {
187     BoolInstance *bool;
188     HRESULT hres;
189
190     bool = heap_alloc_zero(sizeof(BoolInstance));
191     if(!bool)
192         return E_OUTOFMEMORY;
193
194     if(use_constr)
195         hres = init_dispex_from_constr(&bool->dispex, ctx, &Bool_info, ctx->bool_constr);
196     else
197         hres = init_dispex(&bool->dispex, ctx, &Bool_info, NULL);
198
199     if(FAILED(hres)) {
200         heap_free(bool);
201         return hres;
202     }
203
204     *ret = bool;
205     return S_OK;
206 }
207
208 HRESULT create_bool_constr(script_ctx_t *ctx, DispatchEx **ret)
209 {
210     BoolInstance *bool;
211     HRESULT hres;
212
213     hres = alloc_bool(ctx, FALSE, &bool);
214     if(FAILED(hres))
215         return hres;
216
217     hres = create_builtin_function(ctx, BoolConstr_value, NULL, PROPF_CONSTR, &bool->dispex, ret);
218
219     jsdisp_release(&bool->dispex);
220     return hres;
221 }
222
223 HRESULT create_bool(script_ctx_t *ctx, VARIANT_BOOL b, DispatchEx **ret)
224 {
225     BoolInstance *bool;
226     HRESULT hres;
227
228     hres = alloc_bool(ctx, TRUE, &bool);
229     if(FAILED(hres))
230         return hres;
231
232     bool->val = b;
233
234     *ret = &bool->dispex;
235     return S_OK;
236 }