jscript: Added to_object(VT_BOOL) implementation.
[wine] / dlls / jscript / bool.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "jscript.h"
20
21 #include "wine/debug.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
24
25 typedef struct {
26     DispatchEx dispex;
27
28     VARIANT_BOOL val;
29 } BoolInstance;
30
31 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
32 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
33 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
34 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
35 static const WCHAR propertyIsEnumerableW[] =
36     {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
37 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
38
39 static HRESULT Bool_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
40         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
41 {
42     FIXME("\n");
43     return E_NOTIMPL;
44 }
45
46 static HRESULT Bool_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
47         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
48 {
49     FIXME("\n");
50     return E_NOTIMPL;
51 }
52
53 static HRESULT Bool_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
54         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
55 {
56     FIXME("\n");
57     return E_NOTIMPL;
58 }
59
60 static HRESULT Bool_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
61         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
62 {
63     FIXME("\n");
64     return E_NOTIMPL;
65 }
66
67 static HRESULT Bool_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
68         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
69 {
70     FIXME("\n");
71     return E_NOTIMPL;
72 }
73
74 static HRESULT Bool_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
75         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
76 {
77     FIXME("\n");
78     return E_NOTIMPL;
79 }
80
81 static HRESULT Bool_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
82         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
83 {
84     FIXME("\n");
85     return E_NOTIMPL;
86 }
87
88 static const builtin_prop_t Bool_props[] = {
89     {hasOwnPropertyW,        Bool_hasOwnProperty,       PROPF_METHOD},
90     {isPrototypeOfW,         Bool_isPrototypeOf,        PROPF_METHOD},
91     {propertyIsEnumerableW,  Bool_propertyIsEnumerable, PROPF_METHOD},
92     {toLocaleStringW,        Bool_toLocaleString,       PROPF_METHOD},
93     {toStringW,              Bool_toString,             PROPF_METHOD},
94     {valueOfW,               Bool_valueOf,              PROPF_METHOD}
95 };
96
97 static const builtin_info_t Bool_info = {
98     JSCLASS_BOOLEAN,
99     {NULL, Bool_value, 0},
100     sizeof(Bool_props)/sizeof(*Bool_props),
101     Bool_props,
102     NULL,
103     NULL
104 };
105
106 static HRESULT BoolConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
107         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
108 {
109     FIXME("\n");
110     return E_NOTIMPL;
111 }
112
113 static HRESULT alloc_bool(script_ctx_t *ctx, BOOL use_constr, BoolInstance **ret)
114 {
115     BoolInstance *bool;
116     HRESULT hres;
117
118     bool = heap_alloc_zero(sizeof(BoolInstance));
119     if(!bool)
120         return E_OUTOFMEMORY;
121
122     if(use_constr)
123         hres = init_dispex_from_constr(&bool->dispex, ctx, &Bool_info, ctx->bool_constr);
124     else
125         hres = init_dispex(&bool->dispex, ctx, &Bool_info, NULL);
126
127     if(FAILED(hres)) {
128         heap_free(bool);
129         return hres;
130     }
131
132     *ret = bool;
133     return S_OK;
134 }
135
136 HRESULT create_bool_constr(script_ctx_t *ctx, DispatchEx **ret)
137 {
138     BoolInstance *bool;
139     HRESULT hres;
140
141     hres = alloc_bool(ctx, FALSE, &bool);
142     if(FAILED(hres))
143         return hres;
144
145     hres = create_builtin_function(ctx, BoolConstr_value, PROPF_CONSTR, &bool->dispex, ret);
146
147     jsdisp_release(&bool->dispex);
148     return hres;
149 }
150
151 HRESULT create_bool(script_ctx_t *ctx, VARIANT_BOOL b, DispatchEx **ret)
152 {
153     BoolInstance *bool;
154     HRESULT hres;
155
156     hres = alloc_bool(ctx, TRUE, &bool);
157     if(FAILED(hres))
158         return hres;
159
160     bool->val = b;
161
162     *ret = &bool->dispex;
163     return S_OK;
164 }