opengl32: Preserve the remainder of the version string when limiting the version...
[wine.git] / dlls / jscript / bool.c
blob57c5cf5b4c6fd9a52043914fd03867c30ac7ab20
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 inline BoolInstance *bool_from_jsdisp(jsdisp_t *jsdisp)
36 return CONTAINING_RECORD(jsdisp, BoolInstance, dispex);
39 static inline HRESULT boolval_this(jsval_t vthis, BOOL *ret)
41 jsdisp_t *jsdisp;
42 if(is_bool(vthis))
43 *ret = get_bool(vthis);
44 else if(is_object_instance(vthis) && (jsdisp = to_jsdisp(get_object(vthis))) && is_class(jsdisp, JSCLASS_BOOLEAN))
45 *ret = bool_from_jsdisp(jsdisp)->val;
46 else
47 return JS_E_BOOLEAN_EXPECTED;
48 return S_OK;
51 BOOL bool_obj_value(jsdisp_t *obj)
53 assert(is_class(obj, JSCLASS_BOOLEAN));
54 return bool_from_jsdisp(obj)->val;
57 /* ECMA-262 3rd Edition 15.6.4.2 */
58 static HRESULT Bool_toString(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
60 BOOL boolval;
61 HRESULT hres;
63 TRACE("\n");
65 hres = boolval_this(vthis, &boolval);
66 if(FAILED(hres))
67 return hres;
69 if(r) {
70 jsstr_t *val;
72 val = jsstr_alloc(boolval ? L"true" : L"false");
73 if(!val)
74 return E_OUTOFMEMORY;
76 *r = jsval_string(val);
79 return S_OK;
82 /* ECMA-262 3rd Edition 15.6.4.3 */
83 static HRESULT Bool_valueOf(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
85 BOOL boolval;
86 HRESULT hres;
88 TRACE("\n");
90 hres = boolval_this(vthis, &boolval);
91 if(FAILED(hres))
92 return hres;
94 if(r)
95 *r = jsval_bool(boolval);
96 return S_OK;
99 static HRESULT Bool_value(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
100 jsval_t *r)
102 TRACE("\n");
104 switch(flags) {
105 case INVOKE_FUNC:
106 return JS_E_FUNCTION_EXPECTED;
107 default:
108 FIXME("unimplemented flags %x\n", flags);
109 return E_NOTIMPL;
112 return S_OK;
116 static const builtin_prop_t Bool_props[] = {
117 {L"toString", Bool_toString, PROPF_METHOD},
118 {L"valueOf", Bool_valueOf, PROPF_METHOD}
121 static const builtin_info_t Bool_info = {
122 JSCLASS_BOOLEAN,
123 Bool_value,
124 ARRAY_SIZE(Bool_props),
125 Bool_props,
126 NULL,
127 NULL
130 static const builtin_info_t BoolInst_info = {
131 JSCLASS_BOOLEAN,
132 Bool_value,
133 0, NULL,
134 NULL,
135 NULL
138 static HRESULT BoolConstr_value(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
139 jsval_t *r)
141 BOOL value = FALSE;
142 HRESULT hres;
144 if(argc) {
145 hres = to_boolean(argv[0], &value);
146 if(FAILED(hres))
147 return hres;
150 switch(flags) {
151 case DISPATCH_CONSTRUCT: {
152 jsdisp_t *bool;
154 if(!r)
155 return S_OK;
157 hres = create_bool(ctx, value, &bool);
158 if(FAILED(hres))
159 return hres;
161 *r = jsval_obj(bool);
162 return S_OK;
165 case INVOKE_FUNC:
166 if(r)
167 *r = jsval_bool(value);
168 return S_OK;
170 default:
171 FIXME("unimplemented flags %x\n", flags);
172 return E_NOTIMPL;
175 return S_OK;
178 static HRESULT alloc_bool(script_ctx_t *ctx, jsdisp_t *object_prototype, BoolInstance **ret)
180 BoolInstance *bool;
181 HRESULT hres;
183 bool = calloc(1, sizeof(BoolInstance));
184 if(!bool)
185 return E_OUTOFMEMORY;
187 if(object_prototype)
188 hres = init_dispex(&bool->dispex, ctx, &Bool_info, object_prototype);
189 else
190 hres = init_dispex_from_constr(&bool->dispex, ctx, &BoolInst_info, ctx->bool_constr);
192 if(FAILED(hres)) {
193 free(bool);
194 return hres;
197 *ret = bool;
198 return S_OK;
201 HRESULT create_bool_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
203 BoolInstance *bool;
204 HRESULT hres;
206 hres = alloc_bool(ctx, object_prototype, &bool);
207 if(FAILED(hres))
208 return hres;
210 hres = create_builtin_constructor(ctx, BoolConstr_value, L"Boolean", NULL,
211 PROPF_CONSTR|1, &bool->dispex, ret);
213 jsdisp_release(&bool->dispex);
214 return hres;
217 HRESULT create_bool(script_ctx_t *ctx, BOOL b, jsdisp_t **ret)
219 BoolInstance *bool;
220 HRESULT hres;
222 hres = alloc_bool(ctx, NULL, &bool);
223 if(FAILED(hres))
224 return hres;
226 bool->val = b;
228 *ret = &bool->dispex;
229 return S_OK;