[PSAPI_WINETEST] Sync with Wine Staging 2.16. CORE-13762
[reactos.git] / dll / win32 / jscript / bool.c
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
3 * Copyright 2009 Piotr Caban
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #include "jscript.h"
21
22 typedef struct {
23 jsdisp_t dispex;
24
25 BOOL val;
26 } BoolInstance;
27
28 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
29 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
30
31 static inline BoolInstance *bool_from_jsdisp(jsdisp_t *jsdisp)
32 {
33 return CONTAINING_RECORD(jsdisp, BoolInstance, dispex);
34 }
35
36 static inline BoolInstance *bool_from_vdisp(vdisp_t *vdisp)
37 {
38 return bool_from_jsdisp(vdisp->u.jsdisp);
39 }
40
41 static inline BoolInstance *bool_this(vdisp_t *jsthis)
42 {
43 return is_vclass(jsthis, JSCLASS_BOOLEAN) ? bool_from_vdisp(jsthis) : NULL;
44 }
45
46 BOOL bool_obj_value(jsdisp_t *obj)
47 {
48 assert(is_class(obj, JSCLASS_BOOLEAN));
49 return bool_from_jsdisp(obj)->val;
50 }
51
52 /* ECMA-262 3rd Edition 15.6.4.2 */
53 static HRESULT Bool_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
54 {
55 BoolInstance *bool;
56
57 static const WCHAR trueW[] = {'t','r','u','e',0};
58 static const WCHAR falseW[] = {'f','a','l','s','e',0};
59
60 TRACE("\n");
61
62 if(!(bool = bool_this(jsthis)))
63 return throw_type_error(ctx, JS_E_BOOLEAN_EXPECTED, NULL);
64
65 if(r) {
66 jsstr_t *val;
67
68 val = jsstr_alloc(bool->val ? trueW : falseW);
69 if(!val)
70 return E_OUTOFMEMORY;
71
72 *r = jsval_string(val);
73 }
74
75 return S_OK;
76 }
77
78 /* ECMA-262 3rd Edition 15.6.4.3 */
79 static HRESULT Bool_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
80 {
81 BoolInstance *bool;
82
83 TRACE("\n");
84
85 if(!(bool = bool_this(jsthis)))
86 return throw_type_error(ctx, JS_E_BOOLEAN_EXPECTED, NULL);
87
88 if(r)
89 *r = jsval_bool(bool->val);
90 return S_OK;
91 }
92
93 static HRESULT Bool_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
94 jsval_t *r)
95 {
96 TRACE("\n");
97
98 switch(flags) {
99 case INVOKE_FUNC:
100 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
101 default:
102 FIXME("unimplemented flags %x\n", flags);
103 return E_NOTIMPL;
104 }
105
106 return S_OK;
107
108 }
109
110 static const builtin_prop_t Bool_props[] = {
111 {toStringW, Bool_toString, PROPF_METHOD},
112 {valueOfW, Bool_valueOf, PROPF_METHOD}
113 };
114
115 static const builtin_info_t Bool_info = {
116 JSCLASS_BOOLEAN,
117 {NULL, Bool_value, 0},
118 sizeof(Bool_props)/sizeof(*Bool_props),
119 Bool_props,
120 NULL,
121 NULL
122 };
123
124 static const builtin_info_t BoolInst_info = {
125 JSCLASS_BOOLEAN,
126 {NULL, Bool_value, 0},
127 0, NULL,
128 NULL,
129 NULL
130 };
131
132 static HRESULT BoolConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
133 jsval_t *r)
134 {
135 BOOL value = FALSE;
136 HRESULT hres;
137
138 if(argc) {
139 hres = to_boolean(argv[0], &value);
140 if(FAILED(hres))
141 return hres;
142 }
143
144 switch(flags) {
145 case DISPATCH_CONSTRUCT: {
146 jsdisp_t *bool;
147
148 hres = create_bool(ctx, value, &bool);
149 if(FAILED(hres))
150 return hres;
151
152 *r = jsval_obj(bool);
153 return S_OK;
154 }
155
156 case INVOKE_FUNC:
157 if(r)
158 *r = jsval_bool(value);
159 return S_OK;
160
161 default:
162 FIXME("unimplemented flags %x\n", flags);
163 return E_NOTIMPL;
164 }
165
166 return S_OK;
167 }
168
169 static HRESULT alloc_bool(script_ctx_t *ctx, jsdisp_t *object_prototype, BoolInstance **ret)
170 {
171 BoolInstance *bool;
172 HRESULT hres;
173
174 bool = heap_alloc_zero(sizeof(BoolInstance));
175 if(!bool)
176 return E_OUTOFMEMORY;
177
178 if(object_prototype)
179 hres = init_dispex(&bool->dispex, ctx, &Bool_info, object_prototype);
180 else
181 hres = init_dispex_from_constr(&bool->dispex, ctx, &BoolInst_info, ctx->bool_constr);
182
183 if(FAILED(hres)) {
184 heap_free(bool);
185 return hres;
186 }
187
188 *ret = bool;
189 return S_OK;
190 }
191
192 HRESULT create_bool_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
193 {
194 BoolInstance *bool;
195 HRESULT hres;
196
197 static const WCHAR BooleanW[] = {'B','o','o','l','e','a','n',0};
198
199 hres = alloc_bool(ctx, object_prototype, &bool);
200 if(FAILED(hres))
201 return hres;
202
203 hres = create_builtin_constructor(ctx, BoolConstr_value, BooleanW, NULL,
204 PROPF_CONSTR|1, &bool->dispex, ret);
205
206 jsdisp_release(&bool->dispex);
207 return hres;
208 }
209
210 HRESULT create_bool(script_ctx_t *ctx, BOOL b, jsdisp_t **ret)
211 {
212 BoolInstance *bool;
213 HRESULT hres;
214
215 hres = alloc_bool(ctx, NULL, &bool);
216 if(FAILED(hres))
217 return hres;
218
219 bool->val = b;
220
221 *ret = &bool->dispex;
222 return S_OK;
223 }