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