reg/tests: Test line concatenation with comments, new lines and comma variations...
[wine.git] / dlls / jscript / bool.c
blob75c98c2403d197b46f928f4ec78dcaa818deb0a0
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_from_jsdisp(jsdisp_t *jsdisp)
39 return CONTAINING_RECORD(jsdisp, BoolInstance, dispex);
42 static inline BoolInstance *bool_from_vdisp(vdisp_t *vdisp)
44 return bool_from_jsdisp(vdisp->u.jsdisp);
47 static inline BoolInstance *bool_this(vdisp_t *jsthis)
49 return is_vclass(jsthis, JSCLASS_BOOLEAN) ? bool_from_vdisp(jsthis) : NULL;
52 BOOL bool_obj_value(jsdisp_t *obj)
54 assert(is_class(obj, JSCLASS_BOOLEAN));
55 return bool_from_jsdisp(obj)->val;
58 /* ECMA-262 3rd Edition 15.6.4.2 */
59 static HRESULT Bool_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
61 BoolInstance *bool;
63 static const WCHAR trueW[] = {'t','r','u','e',0};
64 static const WCHAR falseW[] = {'f','a','l','s','e',0};
66 TRACE("\n");
68 if(!(bool = bool_this(jsthis)))
69 return throw_type_error(ctx, JS_E_BOOLEAN_EXPECTED, NULL);
71 if(r) {
72 jsstr_t *val;
74 val = jsstr_alloc(bool->val ? trueW : falseW);
75 if(!val)
76 return E_OUTOFMEMORY;
78 *r = jsval_string(val);
81 return S_OK;
84 /* ECMA-262 3rd Edition 15.6.4.3 */
85 static HRESULT Bool_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
87 BoolInstance *bool;
89 TRACE("\n");
91 if(!(bool = bool_this(jsthis)))
92 return throw_type_error(ctx, JS_E_BOOLEAN_EXPECTED, NULL);
94 if(r)
95 *r = jsval_bool(bool->val);
96 return S_OK;
99 static HRESULT Bool_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
100 jsval_t *r)
102 TRACE("\n");
104 switch(flags) {
105 case INVOKE_FUNC:
106 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
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 {toStringW, Bool_toString, PROPF_METHOD},
118 {valueOfW, Bool_valueOf, PROPF_METHOD}
121 static const builtin_info_t Bool_info = {
122 JSCLASS_BOOLEAN,
123 {NULL, Bool_value, 0},
124 sizeof(Bool_props)/sizeof(*Bool_props),
125 Bool_props,
126 NULL,
127 NULL
130 static const builtin_info_t BoolInst_info = {
131 JSCLASS_BOOLEAN,
132 {NULL, Bool_value, 0},
133 0, NULL,
134 NULL,
135 NULL
138 static HRESULT BoolConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, 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 hres = create_bool(ctx, value, &bool);
155 if(FAILED(hres))
156 return hres;
158 *r = jsval_obj(bool);
159 return S_OK;
162 case INVOKE_FUNC:
163 if(r)
164 *r = jsval_bool(value);
165 return S_OK;
167 default:
168 FIXME("unimplemented flags %x\n", flags);
169 return E_NOTIMPL;
172 return S_OK;
175 static HRESULT alloc_bool(script_ctx_t *ctx, jsdisp_t *object_prototype, BoolInstance **ret)
177 BoolInstance *bool;
178 HRESULT hres;
180 bool = heap_alloc_zero(sizeof(BoolInstance));
181 if(!bool)
182 return E_OUTOFMEMORY;
184 if(object_prototype)
185 hres = init_dispex(&bool->dispex, ctx, &Bool_info, object_prototype);
186 else
187 hres = init_dispex_from_constr(&bool->dispex, ctx, &BoolInst_info, ctx->bool_constr);
189 if(FAILED(hres)) {
190 heap_free(bool);
191 return hres;
194 *ret = bool;
195 return S_OK;
198 HRESULT create_bool_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
200 BoolInstance *bool;
201 HRESULT hres;
203 static const WCHAR BooleanW[] = {'B','o','o','l','e','a','n',0};
205 hres = alloc_bool(ctx, object_prototype, &bool);
206 if(FAILED(hres))
207 return hres;
209 hres = create_builtin_constructor(ctx, BoolConstr_value, BooleanW, NULL,
210 PROPF_CONSTR|1, &bool->dispex, ret);
212 jsdisp_release(&bool->dispex);
213 return hres;
216 HRESULT create_bool(script_ctx_t *ctx, BOOL b, jsdisp_t **ret)
218 BoolInstance *bool;
219 HRESULT hres;
221 hres = alloc_bool(ctx, NULL, &bool);
222 if(FAILED(hres))
223 return hres;
225 bool->val = b;
227 *ret = &bool->dispex;
228 return S_OK;