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