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