push c4c845830c6aff14e1b16bbb8b4a57a7e0d6213f
[wine/hacks.git] / dlls / jscript / bool.c
blobab9ef435a2c38b07c18f36bf82c0db2282caa578
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 FIXME("throw TypeError\n");
51 return E_FAIL;
54 if(retv) {
55 BoolInstance *bool = (BoolInstance*)dispex;
56 BSTR val;
58 if(bool->val) val = SysAllocString(trueW);
59 else val = SysAllocString(falseW);
61 if(!val)
62 return E_OUTOFMEMORY;
64 V_VT(retv) = VT_BSTR;
65 V_BSTR(retv) = val;
68 return S_OK;
71 static HRESULT Bool_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
72 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
74 TRACE("\n");
75 return Bool_toString(dispex, lcid, flags, dp, retv, ei, sp);
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)
82 TRACE("\n");
84 if(!is_class(dispex, JSCLASS_BOOLEAN)) {
85 FIXME("throw TypeError\n");
86 return E_FAIL;
89 if(retv) {
90 BoolInstance *bool = (BoolInstance*)dispex;
92 V_VT(retv) = VT_BOOL;
93 V_BOOL(retv) = bool->val;
96 return S_OK;
99 static HRESULT Bool_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
100 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
102 FIXME("\n");
103 return E_NOTIMPL;
106 static HRESULT Bool_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
107 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
109 FIXME("\n");
110 return E_NOTIMPL;
113 static HRESULT Bool_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
114 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
116 FIXME("\n");
117 return E_NOTIMPL;
120 static HRESULT Bool_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
121 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
123 FIXME("\n");
124 return E_NOTIMPL;
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}
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
145 static HRESULT BoolConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
146 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
148 HRESULT hres;
149 VARIANT_BOOL value = VARIANT_FALSE;
151 if(arg_cnt(dp)) {
152 hres = to_boolean(get_arg(dp,0), &value);
153 if(FAILED(hres))
154 return hres;
157 switch(flags) {
158 case DISPATCH_CONSTRUCT: {
159 DispatchEx *bool;
161 hres = create_bool(dispex->ctx, value, &bool);
162 if(FAILED(hres))
163 return hres;
165 V_VT(retv) = VT_DISPATCH;
166 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(bool);
167 return S_OK;
170 case INVOKE_FUNC:
171 if(retv) {
172 V_VT(retv) = VT_BOOL;
173 V_BOOL(retv) = value;
175 return S_OK;
177 default:
178 FIXME("unimplemented flags %x\n", flags);
179 return E_NOTIMPL;
182 return S_OK;
185 static HRESULT alloc_bool(script_ctx_t *ctx, BOOL use_constr, BoolInstance **ret)
187 BoolInstance *bool;
188 HRESULT hres;
190 bool = heap_alloc_zero(sizeof(BoolInstance));
191 if(!bool)
192 return E_OUTOFMEMORY;
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);
199 if(FAILED(hres)) {
200 heap_free(bool);
201 return hres;
204 *ret = bool;
205 return S_OK;
208 HRESULT create_bool_constr(script_ctx_t *ctx, DispatchEx **ret)
210 BoolInstance *bool;
211 HRESULT hres;
213 hres = alloc_bool(ctx, FALSE, &bool);
214 if(FAILED(hres))
215 return hres;
217 hres = create_builtin_function(ctx, BoolConstr_value, NULL, PROPF_CONSTR, &bool->dispex, ret);
219 jsdisp_release(&bool->dispex);
220 return hres;
223 HRESULT create_bool(script_ctx_t *ctx, VARIANT_BOOL b, DispatchEx **ret)
225 BoolInstance *bool;
226 HRESULT hres;
228 hres = alloc_bool(ctx, TRUE, &bool);
229 if(FAILED(hres))
230 return hres;
232 bool->val = b;
234 *ret = &bool->dispex;
235 return S_OK;