jscript: Add Error_number handling to constructor and error throwing functions.
[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 toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
34 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
35 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
36 static const WCHAR propertyIsEnumerableW[] =
37     {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
38 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
39
40 /* ECMA-262 3rd Edition    15.6.4.2 */
41 static HRESULT Bool_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
42         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
43 {
44     static const WCHAR trueW[] = {'t','r','u','e',0};
45     static const WCHAR falseW[] = {'f','a','l','s','e',0};
46
47     TRACE("\n");
48
49     if(!is_class(dispex, JSCLASS_BOOLEAN))
50         return throw_type_error(dispex->ctx, ei, IDS_NOT_BOOL, NULL);
51
52     if(retv) {
53         BoolInstance *bool = (BoolInstance*)dispex;
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         V_VT(retv) = VT_BSTR;
63         V_BSTR(retv) = val;
64     }
65
66     return S_OK;
67 }
68
69 static HRESULT Bool_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
70         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
71 {
72     TRACE("\n");
73     return Bool_toString(dispex, lcid, flags, dp, retv, ei, sp);
74 }
75
76 /* ECMA-262 3rd Edition    15.6.4.3 */
77 static HRESULT Bool_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
78         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
79 {
80     TRACE("\n");
81
82     if(!is_class(dispex, JSCLASS_BOOLEAN))
83         return throw_type_error(dispex->ctx, ei, IDS_NOT_BOOL, NULL);
84
85     if(retv) {
86         BoolInstance *bool = (BoolInstance*)dispex;
87
88         V_VT(retv) = VT_BOOL;
89         V_BOOL(retv) = bool->val;
90     }
91
92     return S_OK;
93 }
94
95 static HRESULT Bool_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
96         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
97 {
98     FIXME("\n");
99     return E_NOTIMPL;
100 }
101
102 static HRESULT Bool_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
103         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
104 {
105     FIXME("\n");
106     return E_NOTIMPL;
107 }
108
109 static HRESULT Bool_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
110         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
111 {
112     FIXME("\n");
113     return E_NOTIMPL;
114 }
115
116 static HRESULT Bool_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
117         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
118 {
119     FIXME("\n");
120     return E_NOTIMPL;
121 }
122
123 static const builtin_prop_t Bool_props[] = {
124     {hasOwnPropertyW,        Bool_hasOwnProperty,       PROPF_METHOD},
125     {isPrototypeOfW,         Bool_isPrototypeOf,        PROPF_METHOD},
126     {propertyIsEnumerableW,  Bool_propertyIsEnumerable, PROPF_METHOD},
127     {toLocaleStringW,        Bool_toLocaleString,       PROPF_METHOD},
128     {toStringW,              Bool_toString,             PROPF_METHOD},
129     {valueOfW,               Bool_valueOf,              PROPF_METHOD}
130 };
131
132 static const builtin_info_t Bool_info = {
133     JSCLASS_BOOLEAN,
134     {NULL, Bool_value, 0},
135     sizeof(Bool_props)/sizeof(*Bool_props),
136     Bool_props,
137     NULL,
138     NULL
139 };
140
141 static HRESULT BoolConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
142         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
143 {
144     HRESULT hres;
145     VARIANT_BOOL value = VARIANT_FALSE;
146
147     if(arg_cnt(dp)) {
148         hres = to_boolean(get_arg(dp,0), &value);
149         if(FAILED(hres))
150             return hres;
151     }
152
153     switch(flags) {
154     case DISPATCH_CONSTRUCT: {
155         DispatchEx *bool;
156
157         hres = create_bool(dispex->ctx, value, &bool);
158         if(FAILED(hres))
159             return hres;
160
161         V_VT(retv) = VT_DISPATCH;
162         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(bool);
163         return S_OK;
164     }
165
166     case INVOKE_FUNC:
167         if(retv) {
168             V_VT(retv) = VT_BOOL;
169             V_BOOL(retv) = value;
170         }
171         return S_OK;
172
173     default:
174         FIXME("unimplemented flags %x\n", flags);
175         return E_NOTIMPL;
176     }
177
178     return S_OK;
179 }
180
181 static HRESULT alloc_bool(script_ctx_t *ctx, BOOL use_constr, BoolInstance **ret)
182 {
183     BoolInstance *bool;
184     HRESULT hres;
185
186     bool = heap_alloc_zero(sizeof(BoolInstance));
187     if(!bool)
188         return E_OUTOFMEMORY;
189
190     if(use_constr)
191         hres = init_dispex_from_constr(&bool->dispex, ctx, &Bool_info, ctx->bool_constr);
192     else
193         hres = init_dispex(&bool->dispex, ctx, &Bool_info, NULL);
194
195     if(FAILED(hres)) {
196         heap_free(bool);
197         return hres;
198     }
199
200     *ret = bool;
201     return S_OK;
202 }
203
204 HRESULT create_bool_constr(script_ctx_t *ctx, DispatchEx **ret)
205 {
206     BoolInstance *bool;
207     HRESULT hres;
208
209     hres = alloc_bool(ctx, FALSE, &bool);
210     if(FAILED(hres))
211         return hres;
212
213     hres = create_builtin_function(ctx, BoolConstr_value, NULL, PROPF_CONSTR, &bool->dispex, ret);
214
215     jsdisp_release(&bool->dispex);
216     return hres;
217 }
218
219 HRESULT create_bool(script_ctx_t *ctx, VARIANT_BOOL b, DispatchEx **ret)
220 {
221     BoolInstance *bool;
222     HRESULT hres;
223
224     hres = alloc_bool(ctx, TRUE, &bool);
225     if(FAILED(hres))
226         return hres;
227
228     bool->val = b;
229
230     *ret = &bool->dispex;
231     return S_OK;
232 }