jscript: Avoid using dispex->ctx.
[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     DispatchEx 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 /* ECMA-262 3rd Edition    15.6.4.2 */
36 static HRESULT Bool_toString(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
37         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
38 {
39     static const WCHAR trueW[] = {'t','r','u','e',0};
40     static const WCHAR falseW[] = {'f','a','l','s','e',0};
41
42     TRACE("\n");
43
44     if(!is_class(dispex, JSCLASS_BOOLEAN))
45         return throw_type_error(ctx, ei, IDS_NOT_BOOL, NULL);
46
47     if(retv) {
48         BoolInstance *bool = (BoolInstance*)dispex;
49         BSTR val;
50
51         if(bool->val) val = SysAllocString(trueW);
52         else val = SysAllocString(falseW);
53
54         if(!val)
55             return E_OUTOFMEMORY;
56
57         V_VT(retv) = VT_BSTR;
58         V_BSTR(retv) = val;
59     }
60
61     return S_OK;
62 }
63
64 /* ECMA-262 3rd Edition    15.6.4.3 */
65 static HRESULT Bool_valueOf(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
66         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
67 {
68     TRACE("\n");
69
70     if(!is_class(dispex, JSCLASS_BOOLEAN))
71         return throw_type_error(ctx, ei, IDS_NOT_BOOL, NULL);
72
73     if(retv) {
74         BoolInstance *bool = (BoolInstance*)dispex;
75
76         V_VT(retv) = VT_BOOL;
77         V_BOOL(retv) = bool->val;
78     }
79
80     return S_OK;
81 }
82
83 static HRESULT Bool_value(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
84         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
85 {
86     TRACE("\n");
87
88     switch(flags) {
89     case INVOKE_FUNC:
90         return throw_type_error(ctx, ei, IDS_NOT_FUNC, 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 HRESULT BoolConstr_value(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
115         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
116 {
117     HRESULT hres;
118     VARIANT_BOOL value = VARIANT_FALSE;
119
120     if(arg_cnt(dp)) {
121         hres = to_boolean(get_arg(dp,0), &value);
122         if(FAILED(hres))
123             return hres;
124     }
125
126     switch(flags) {
127     case DISPATCH_CONSTRUCT: {
128         DispatchEx *bool;
129
130         hres = create_bool(ctx, value, &bool);
131         if(FAILED(hres))
132             return hres;
133
134         V_VT(retv) = VT_DISPATCH;
135         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(bool);
136         return S_OK;
137     }
138
139     case INVOKE_FUNC:
140         if(retv) {
141             V_VT(retv) = VT_BOOL;
142             V_BOOL(retv) = value;
143         }
144         return S_OK;
145
146     default:
147         FIXME("unimplemented flags %x\n", flags);
148         return E_NOTIMPL;
149     }
150
151     return S_OK;
152 }
153
154 static HRESULT alloc_bool(script_ctx_t *ctx, DispatchEx *object_prototype, BoolInstance **ret)
155 {
156     BoolInstance *bool;
157     HRESULT hres;
158
159     bool = heap_alloc_zero(sizeof(BoolInstance));
160     if(!bool)
161         return E_OUTOFMEMORY;
162
163     if(object_prototype)
164         hres = init_dispex(&bool->dispex, ctx, &Bool_info, object_prototype);
165     else
166         hres = init_dispex_from_constr(&bool->dispex, ctx, &Bool_info, ctx->bool_constr);
167
168     if(FAILED(hres)) {
169         heap_free(bool);
170         return hres;
171     }
172
173     *ret = bool;
174     return S_OK;
175 }
176
177 HRESULT create_bool_constr(script_ctx_t *ctx, DispatchEx *object_prototype, DispatchEx **ret)
178 {
179     BoolInstance *bool;
180     HRESULT hres;
181
182     static const WCHAR BooleanW[] = {'B','o','o','l','e','a','n',0};
183
184     hres = alloc_bool(ctx, object_prototype, &bool);
185     if(FAILED(hres))
186         return hres;
187
188     hres = create_builtin_function(ctx, BoolConstr_value, BooleanW, NULL, PROPF_CONSTR, &bool->dispex, ret);
189
190     jsdisp_release(&bool->dispex);
191     return hres;
192 }
193
194 HRESULT create_bool(script_ctx_t *ctx, VARIANT_BOOL b, DispatchEx **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 }