jscript: Added Bool_valueOf implementation.
[wine] / dlls / jscript / bool.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 #include "wine/debug.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
24
25 typedef struct {
26     DispatchEx dispex;
27
28     VARIANT_BOOL val;
29 } BoolInstance;
30
31 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
32 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
33 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
34 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
35 static const WCHAR propertyIsEnumerableW[] =
36     {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
37 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
38
39 /* ECMA-262 3rd Edition    15.6.4.2 */
40 static HRESULT Bool_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
41         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
42 {
43     static const WCHAR trueW[] = {'t','r','u','e',0};
44     static const WCHAR falseW[] = {'f','a','l','s','e',0};
45
46     TRACE("\n");
47
48     if(!is_class(dispex, JSCLASS_BOOLEAN)) {
49         FIXME("throw TypeError\n");
50         return E_FAIL;
51     }
52
53     if(retv) {
54         BoolInstance *bool = (BoolInstance*)dispex;
55         BSTR val;
56
57         if(bool->val) val = SysAllocString(trueW);
58         else val = SysAllocString(falseW);
59
60         if(!val)
61             return E_OUTOFMEMORY;
62
63         V_VT(retv) = VT_BSTR;
64         V_BSTR(retv) = val;
65     }
66
67     return S_OK;
68 }
69
70 static HRESULT Bool_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
71         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
72 {
73     FIXME("\n");
74     return E_NOTIMPL;
75 }
76
77 /* ECMA-262 3rd Edition    15.6.4.3 */
78 static HRESULT Bool_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
79         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
80 {
81     TRACE("\n");
82
83     if(!is_class(dispex, JSCLASS_BOOLEAN)) {
84         FIXME("throw TypeError\n");
85         return E_FAIL;
86     }
87
88     if(retv) {
89         BoolInstance *bool = (BoolInstance*)dispex;
90
91         V_VT(retv) = VT_BOOL;
92         V_BOOL(retv) = bool->val;
93     }
94
95     return S_OK;
96 }
97
98 static HRESULT Bool_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
99         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
100 {
101     FIXME("\n");
102     return E_NOTIMPL;
103 }
104
105 static HRESULT Bool_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
106         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
107 {
108     FIXME("\n");
109     return E_NOTIMPL;
110 }
111
112 static HRESULT Bool_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
113         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
114 {
115     FIXME("\n");
116     return E_NOTIMPL;
117 }
118
119 static HRESULT Bool_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
120         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
121 {
122     FIXME("\n");
123     return E_NOTIMPL;
124 }
125
126 static const builtin_prop_t Bool_props[] = {
127     {hasOwnPropertyW,        Bool_hasOwnProperty,       PROPF_METHOD},
128     {isPrototypeOfW,         Bool_isPrototypeOf,        PROPF_METHOD},
129     {propertyIsEnumerableW,  Bool_propertyIsEnumerable, PROPF_METHOD},
130     {toLocaleStringW,        Bool_toLocaleString,       PROPF_METHOD},
131     {toStringW,              Bool_toString,             PROPF_METHOD},
132     {valueOfW,               Bool_valueOf,              PROPF_METHOD}
133 };
134
135 static const builtin_info_t Bool_info = {
136     JSCLASS_BOOLEAN,
137     {NULL, Bool_value, 0},
138     sizeof(Bool_props)/sizeof(*Bool_props),
139     Bool_props,
140     NULL,
141     NULL
142 };
143
144 static HRESULT BoolConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
145         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
146 {
147     HRESULT hres;
148     VARIANT_BOOL value = VARIANT_FALSE;
149
150     if(arg_cnt(dp)) {
151         hres = to_boolean(get_arg(dp,0), &value);
152         if(FAILED(hres))
153             return hres;
154     }
155
156     switch(flags) {
157     case DISPATCH_CONSTRUCT: {
158         DispatchEx *bool;
159
160         hres = create_bool(dispex->ctx, value, &bool);
161         if(FAILED(hres))
162             return hres;
163
164         V_VT(retv) = VT_DISPATCH;
165         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(bool);
166         return S_OK;
167     }
168
169     case INVOKE_FUNC:
170         if(retv) {
171             V_VT(retv) = VT_BOOL;
172             V_BOOL(retv) = value;
173         }
174         return S_OK;
175
176     default:
177         FIXME("unimplemented flags %x\n", flags);
178         return E_NOTIMPL;
179     }
180
181     return S_OK;
182 }
183
184 static HRESULT alloc_bool(script_ctx_t *ctx, BOOL use_constr, BoolInstance **ret)
185 {
186     BoolInstance *bool;
187     HRESULT hres;
188
189     bool = heap_alloc_zero(sizeof(BoolInstance));
190     if(!bool)
191         return E_OUTOFMEMORY;
192
193     if(use_constr)
194         hres = init_dispex_from_constr(&bool->dispex, ctx, &Bool_info, ctx->bool_constr);
195     else
196         hres = init_dispex(&bool->dispex, ctx, &Bool_info, NULL);
197
198     if(FAILED(hres)) {
199         heap_free(bool);
200         return hres;
201     }
202
203     *ret = bool;
204     return S_OK;
205 }
206
207 HRESULT create_bool_constr(script_ctx_t *ctx, DispatchEx **ret)
208 {
209     BoolInstance *bool;
210     HRESULT hres;
211
212     hres = alloc_bool(ctx, FALSE, &bool);
213     if(FAILED(hres))
214         return hres;
215
216     hres = create_builtin_function(ctx, BoolConstr_value, NULL, PROPF_CONSTR, &bool->dispex, ret);
217
218     jsdisp_release(&bool->dispex);
219     return hres;
220 }
221
222 HRESULT create_bool(script_ctx_t *ctx, VARIANT_BOOL b, DispatchEx **ret)
223 {
224     BoolInstance *bool;
225     HRESULT hres;
226
227     hres = alloc_bool(ctx, TRUE, &bool);
228     if(FAILED(hres))
229         return hres;
230
231     bool->val = b;
232
233     *ret = &bool->dispex;
234     return S_OK;
235 }