jscript: Use prototype for builtin Boolean properties.
[wine/multimedia.git] / dlls / jscript / bool.c
blobd5dfcb49da16dcd18dd32be46b7687e1bc5ced79
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 jsdisp_t 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 valueOfW[] = {'v','a','l','u','e','O','f',0};
35 static inline BoolInstance *bool_this(vdisp_t *jsthis)
37 return is_vclass(jsthis, JSCLASS_BOOLEAN) ? (BoolInstance*)jsthis->u.jsdisp : NULL;
40 /* ECMA-262 3rd Edition 15.6.4.2 */
41 static HRESULT Bool_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
42 VARIANT *retv, jsexcept_t *ei)
44 BoolInstance *bool;
46 static const WCHAR trueW[] = {'t','r','u','e',0};
47 static const WCHAR falseW[] = {'f','a','l','s','e',0};
49 TRACE("\n");
51 if(!(bool = bool_this(jsthis)))
52 return throw_type_error(ctx, ei, JS_E_BOOLEAN_EXPECTED, NULL);
54 if(retv) {
55 BSTR val;
57 if(bool->val) val = SysAllocString(trueW);
58 else val = SysAllocString(falseW);
60 if(!val)
61 return E_OUTOFMEMORY;
63 V_VT(retv) = VT_BSTR;
64 V_BSTR(retv) = val;
67 return S_OK;
70 /* ECMA-262 3rd Edition 15.6.4.3 */
71 static HRESULT Bool_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
72 VARIANT *retv, jsexcept_t *ei)
74 BoolInstance *bool;
76 TRACE("\n");
78 if(!(bool = bool_this(jsthis)))
79 return throw_type_error(ctx, ei, JS_E_BOOLEAN_EXPECTED, NULL);
81 if(retv) {
82 V_VT(retv) = VT_BOOL;
83 V_BOOL(retv) = bool->val;
86 return S_OK;
89 static HRESULT Bool_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
90 VARIANT *retv, jsexcept_t *ei)
92 TRACE("\n");
94 switch(flags) {
95 case INVOKE_FUNC:
96 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
97 default:
98 FIXME("unimplemented flags %x\n", flags);
99 return E_NOTIMPL;
102 return S_OK;
106 static const builtin_prop_t Bool_props[] = {
107 {toStringW, Bool_toString, PROPF_METHOD},
108 {valueOfW, Bool_valueOf, PROPF_METHOD}
111 static const builtin_info_t Bool_info = {
112 JSCLASS_BOOLEAN,
113 {NULL, Bool_value, 0},
114 sizeof(Bool_props)/sizeof(*Bool_props),
115 Bool_props,
116 NULL,
117 NULL
120 static const builtin_info_t BoolInst_info = {
121 JSCLASS_BOOLEAN,
122 {NULL, Bool_value, 0},
123 0, NULL,
124 NULL,
125 NULL
128 static HRESULT BoolConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
129 VARIANT *retv, jsexcept_t *ei)
131 HRESULT hres;
132 VARIANT_BOOL value = VARIANT_FALSE;
134 if(argc) {
135 hres = to_boolean(argv, &value);
136 if(FAILED(hres))
137 return hres;
140 switch(flags) {
141 case DISPATCH_CONSTRUCT: {
142 jsdisp_t *bool;
144 hres = create_bool(ctx, value, &bool);
145 if(FAILED(hres))
146 return hres;
148 var_set_jsdisp(retv, bool);
149 return S_OK;
152 case INVOKE_FUNC:
153 if(retv) {
154 V_VT(retv) = VT_BOOL;
155 V_BOOL(retv) = value;
157 return S_OK;
159 default:
160 FIXME("unimplemented flags %x\n", flags);
161 return E_NOTIMPL;
164 return S_OK;
167 static HRESULT alloc_bool(script_ctx_t *ctx, jsdisp_t *object_prototype, BoolInstance **ret)
169 BoolInstance *bool;
170 HRESULT hres;
172 bool = heap_alloc_zero(sizeof(BoolInstance));
173 if(!bool)
174 return E_OUTOFMEMORY;
176 if(object_prototype)
177 hres = init_dispex(&bool->dispex, ctx, &Bool_info, object_prototype);
178 else
179 hres = init_dispex_from_constr(&bool->dispex, ctx, &BoolInst_info, ctx->bool_constr);
181 if(FAILED(hres)) {
182 heap_free(bool);
183 return hres;
186 *ret = bool;
187 return S_OK;
190 HRESULT create_bool_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
192 BoolInstance *bool;
193 HRESULT hres;
195 static const WCHAR BooleanW[] = {'B','o','o','l','e','a','n',0};
197 hres = alloc_bool(ctx, object_prototype, &bool);
198 if(FAILED(hres))
199 return hres;
201 hres = create_builtin_constructor(ctx, BoolConstr_value, BooleanW, NULL,
202 PROPF_CONSTR|1, &bool->dispex, ret);
204 jsdisp_release(&bool->dispex);
205 return hres;
208 HRESULT create_bool(script_ctx_t *ctx, VARIANT_BOOL b, jsdisp_t **ret)
210 BoolInstance *bool;
211 HRESULT hres;
213 hres = alloc_bool(ctx, NULL, &bool);
214 if(FAILED(hres))
215 return hres;
217 bool->val = b;
219 *ret = &bool->dispex;
220 return S_OK;