push 6377a023768d7480eb1b17e57456824888936b43
[wine/hacks.git] / dlls / jscript / bool.c
blobbfacebf3ec63f61101d7bb230202bc78a94ae7cf
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
3 * Copyright 2009 Piotr Caban
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.
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.
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
20 #include "jscript.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
26 typedef struct {
27 DispatchEx dispex;
29 VARIANT_BOOL val;
30 } BoolInstance;
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};
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)
44 static const WCHAR trueW[] = {'t','r','u','e',0};
45 static const WCHAR falseW[] = {'f','a','l','s','e',0};
47 TRACE("\n");
49 if(!is_class(dispex, JSCLASS_BOOLEAN))
50 return throw_type_error(dispex->ctx, ei, IDS_NOT_BOOL, NULL);
52 if(retv) {
53 BoolInstance *bool = (BoolInstance*)dispex;
54 BSTR val;
56 if(bool->val) val = SysAllocString(trueW);
57 else val = SysAllocString(falseW);
59 if(!val)
60 return E_OUTOFMEMORY;
62 V_VT(retv) = VT_BSTR;
63 V_BSTR(retv) = val;
66 return S_OK;
69 static HRESULT Bool_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
70 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
72 TRACE("\n");
73 return Bool_toString(dispex, lcid, flags, dp, retv, ei, sp);
76 /* ECMA-262 3rd Edition 15.6.4.3 */
77 static HRESULT Bool_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
78 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
80 TRACE("\n");
82 if(!is_class(dispex, JSCLASS_BOOLEAN))
83 return throw_type_error(dispex->ctx, ei, IDS_NOT_BOOL, NULL);
85 if(retv) {
86 BoolInstance *bool = (BoolInstance*)dispex;
88 V_VT(retv) = VT_BOOL;
89 V_BOOL(retv) = bool->val;
92 return S_OK;
95 static HRESULT Bool_hasOwnProperty(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 Bool_propertyIsEnumerable(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 Bool_isPrototypeOf(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 Bool_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
117 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
119 TRACE("\n");
121 switch(flags) {
122 case INVOKE_FUNC:
123 return throw_type_error(dispex->ctx, ei, IDS_NOT_FUNC, NULL);
124 default:
125 FIXME("unimplemented flags %x\n", flags);
126 return E_NOTIMPL;
129 return S_OK;
133 static const builtin_prop_t Bool_props[] = {
134 {hasOwnPropertyW, Bool_hasOwnProperty, PROPF_METHOD},
135 {isPrototypeOfW, Bool_isPrototypeOf, PROPF_METHOD},
136 {propertyIsEnumerableW, Bool_propertyIsEnumerable, PROPF_METHOD},
137 {toLocaleStringW, Bool_toLocaleString, PROPF_METHOD},
138 {toStringW, Bool_toString, PROPF_METHOD},
139 {valueOfW, Bool_valueOf, PROPF_METHOD}
142 static const builtin_info_t Bool_info = {
143 JSCLASS_BOOLEAN,
144 {NULL, Bool_value, 0},
145 sizeof(Bool_props)/sizeof(*Bool_props),
146 Bool_props,
147 NULL,
148 NULL
151 static HRESULT BoolConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
152 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
154 HRESULT hres;
155 VARIANT_BOOL value = VARIANT_FALSE;
157 if(arg_cnt(dp)) {
158 hres = to_boolean(get_arg(dp,0), &value);
159 if(FAILED(hres))
160 return hres;
163 switch(flags) {
164 case DISPATCH_CONSTRUCT: {
165 DispatchEx *bool;
167 hres = create_bool(dispex->ctx, value, &bool);
168 if(FAILED(hres))
169 return hres;
171 V_VT(retv) = VT_DISPATCH;
172 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(bool);
173 return S_OK;
176 case INVOKE_FUNC:
177 if(retv) {
178 V_VT(retv) = VT_BOOL;
179 V_BOOL(retv) = value;
181 return S_OK;
183 default:
184 FIXME("unimplemented flags %x\n", flags);
185 return E_NOTIMPL;
188 return S_OK;
191 static HRESULT alloc_bool(script_ctx_t *ctx, BOOL use_constr, BoolInstance **ret)
193 BoolInstance *bool;
194 HRESULT hres;
196 bool = heap_alloc_zero(sizeof(BoolInstance));
197 if(!bool)
198 return E_OUTOFMEMORY;
200 if(use_constr)
201 hres = init_dispex_from_constr(&bool->dispex, ctx, &Bool_info, ctx->bool_constr);
202 else
203 hres = init_dispex(&bool->dispex, ctx, &Bool_info, NULL);
205 if(FAILED(hres)) {
206 heap_free(bool);
207 return hres;
210 *ret = bool;
211 return S_OK;
214 HRESULT create_bool_constr(script_ctx_t *ctx, DispatchEx **ret)
216 BoolInstance *bool;
217 HRESULT hres;
219 hres = alloc_bool(ctx, FALSE, &bool);
220 if(FAILED(hres))
221 return hres;
223 hres = create_builtin_function(ctx, BoolConstr_value, NULL, PROPF_CONSTR, &bool->dispex, ret);
225 jsdisp_release(&bool->dispex);
226 return hres;
229 HRESULT create_bool(script_ctx_t *ctx, VARIANT_BOOL b, DispatchEx **ret)
231 BoolInstance *bool;
232 HRESULT hres;
234 hres = alloc_bool(ctx, TRUE, &bool);
235 if(FAILED(hres))
236 return hres;
238 bool->val = b;
240 *ret = &bool->dispex;
241 return S_OK;