include: Some more definitions for dwrite_3.idl.
[wine.git] / dlls / jscript / bool.c
blob7cd0c21f64075f806bb0af07565e25106ae7bebc
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 <assert.h>
22 #include "jscript.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28 typedef struct {
29 jsdisp_t dispex;
31 BOOL val;
32 } BoolInstance;
34 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
35 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
37 static inline BoolInstance *bool_this(vdisp_t *jsthis)
39 return is_vclass(jsthis, JSCLASS_BOOLEAN) ? (BoolInstance*)jsthis->u.jsdisp : NULL;
42 BOOL bool_obj_value(jsdisp_t *obj)
44 assert(is_class(obj, JSCLASS_BOOLEAN));
45 return ((BoolInstance*)obj)->val;
48 /* ECMA-262 3rd Edition 15.6.4.2 */
49 static HRESULT Bool_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
51 BoolInstance *bool;
53 static const WCHAR trueW[] = {'t','r','u','e',0};
54 static const WCHAR falseW[] = {'f','a','l','s','e',0};
56 TRACE("\n");
58 if(!(bool = bool_this(jsthis)))
59 return throw_type_error(ctx, JS_E_BOOLEAN_EXPECTED, NULL);
61 if(r) {
62 jsstr_t *val;
64 val = jsstr_alloc(bool->val ? trueW : falseW);
65 if(!val)
66 return E_OUTOFMEMORY;
68 *r = jsval_string(val);
71 return S_OK;
74 /* ECMA-262 3rd Edition 15.6.4.3 */
75 static HRESULT Bool_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
77 BoolInstance *bool;
79 TRACE("\n");
81 if(!(bool = bool_this(jsthis)))
82 return throw_type_error(ctx, JS_E_BOOLEAN_EXPECTED, NULL);
84 if(r)
85 *r = jsval_bool(bool->val);
86 return S_OK;
89 static HRESULT Bool_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
90 jsval_t *r)
92 TRACE("\n");
94 switch(flags) {
95 case INVOKE_FUNC:
96 return throw_type_error(ctx, 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, jsval_t *argv,
129 jsval_t *r)
131 BOOL value = FALSE;
132 HRESULT hres;
134 if(argc) {
135 hres = to_boolean(argv[0], &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 *r = jsval_obj(bool);
149 return S_OK;
152 case INVOKE_FUNC:
153 if(r)
154 *r = jsval_bool(value);
155 return S_OK;
157 default:
158 FIXME("unimplemented flags %x\n", flags);
159 return E_NOTIMPL;
162 return S_OK;
165 static HRESULT alloc_bool(script_ctx_t *ctx, jsdisp_t *object_prototype, BoolInstance **ret)
167 BoolInstance *bool;
168 HRESULT hres;
170 bool = heap_alloc_zero(sizeof(BoolInstance));
171 if(!bool)
172 return E_OUTOFMEMORY;
174 if(object_prototype)
175 hres = init_dispex(&bool->dispex, ctx, &Bool_info, object_prototype);
176 else
177 hres = init_dispex_from_constr(&bool->dispex, ctx, &BoolInst_info, ctx->bool_constr);
179 if(FAILED(hres)) {
180 heap_free(bool);
181 return hres;
184 *ret = bool;
185 return S_OK;
188 HRESULT create_bool_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
190 BoolInstance *bool;
191 HRESULT hres;
193 static const WCHAR BooleanW[] = {'B','o','o','l','e','a','n',0};
195 hres = alloc_bool(ctx, object_prototype, &bool);
196 if(FAILED(hres))
197 return hres;
199 hres = create_builtin_constructor(ctx, BoolConstr_value, BooleanW, NULL,
200 PROPF_CONSTR|1, &bool->dispex, ret);
202 jsdisp_release(&bool->dispex);
203 return hres;
206 HRESULT create_bool(script_ctx_t *ctx, BOOL b, jsdisp_t **ret)
208 BoolInstance *bool;
209 HRESULT hres;
211 hres = alloc_bool(ctx, NULL, &bool);
212 if(FAILED(hres))
213 return hres;
215 bool->val = b;
217 *ret = &bool->dispex;
218 return S_OK;