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