wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / jscript / bool.c
blob44a8b7262de62d615e4218efc67650851c414899
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
3 * Copyright 2009 Piotr Caban
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.
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.
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
20 #include <assert.h>
22 #include "jscript.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28 typedef struct {
29 jsdisp_t dispex;
31 BOOL val;
32 } BoolInstance;
34 static inline BoolInstance *bool_from_jsdisp(jsdisp_t *jsdisp)
36 return CONTAINING_RECORD(jsdisp, BoolInstance, dispex);
39 static inline BoolInstance *bool_from_vdisp(vdisp_t *vdisp)
41 return bool_from_jsdisp(vdisp->u.jsdisp);
44 static inline BoolInstance *bool_this(vdisp_t *jsthis)
46 return is_vclass(jsthis, JSCLASS_BOOLEAN) ? bool_from_vdisp(jsthis) : NULL;
49 BOOL bool_obj_value(jsdisp_t *obj)
51 assert(is_class(obj, JSCLASS_BOOLEAN));
52 return bool_from_jsdisp(obj)->val;
55 /* ECMA-262 3rd Edition 15.6.4.2 */
56 static HRESULT Bool_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
58 BoolInstance *bool;
60 TRACE("\n");
62 if(!(bool = bool_this(jsthis)))
63 return JS_E_BOOLEAN_EXPECTED;
65 if(r) {
66 jsstr_t *val;
68 val = jsstr_alloc(bool->val ? L"true" : L"false");
69 if(!val)
70 return E_OUTOFMEMORY;
72 *r = jsval_string(val);
75 return S_OK;
78 /* ECMA-262 3rd Edition 15.6.4.3 */
79 static HRESULT Bool_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
81 BoolInstance *bool;
83 TRACE("\n");
85 if(!(bool = bool_this(jsthis)))
86 return JS_E_BOOLEAN_EXPECTED;
88 if(r)
89 *r = jsval_bool(bool->val);
90 return S_OK;
93 static HRESULT Bool_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
94 jsval_t *r)
96 TRACE("\n");
98 switch(flags) {
99 case INVOKE_FUNC:
100 return JS_E_FUNCTION_EXPECTED;
101 default:
102 FIXME("unimplemented flags %x\n", flags);
103 return E_NOTIMPL;
106 return S_OK;
110 static const builtin_prop_t Bool_props[] = {
111 {L"toString", Bool_toString, PROPF_METHOD},
112 {L"valueOf", Bool_valueOf, PROPF_METHOD}
115 static const builtin_info_t Bool_info = {
116 JSCLASS_BOOLEAN,
117 {NULL, Bool_value, 0},
118 ARRAY_SIZE(Bool_props),
119 Bool_props,
120 NULL,
121 NULL
124 static const builtin_info_t BoolInst_info = {
125 JSCLASS_BOOLEAN,
126 {NULL, Bool_value, 0},
127 0, NULL,
128 NULL,
129 NULL
132 static HRESULT BoolConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
133 jsval_t *r)
135 BOOL value = FALSE;
136 HRESULT hres;
138 if(argc) {
139 hres = to_boolean(argv[0], &value);
140 if(FAILED(hres))
141 return hres;
144 switch(flags) {
145 case DISPATCH_CONSTRUCT: {
146 jsdisp_t *bool;
148 hres = create_bool(ctx, value, &bool);
149 if(FAILED(hres))
150 return hres;
152 *r = jsval_obj(bool);
153 return S_OK;
156 case INVOKE_FUNC:
157 if(r)
158 *r = jsval_bool(value);
159 return S_OK;
161 default:
162 FIXME("unimplemented flags %x\n", flags);
163 return E_NOTIMPL;
166 return S_OK;
169 static HRESULT alloc_bool(script_ctx_t *ctx, jsdisp_t *object_prototype, BoolInstance **ret)
171 BoolInstance *bool;
172 HRESULT hres;
174 bool = heap_alloc_zero(sizeof(BoolInstance));
175 if(!bool)
176 return E_OUTOFMEMORY;
178 if(object_prototype)
179 hres = init_dispex(&bool->dispex, ctx, &Bool_info, object_prototype);
180 else
181 hres = init_dispex_from_constr(&bool->dispex, ctx, &BoolInst_info, ctx->bool_constr);
183 if(FAILED(hres)) {
184 heap_free(bool);
185 return hres;
188 *ret = bool;
189 return S_OK;
192 HRESULT create_bool_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
194 BoolInstance *bool;
195 HRESULT hres;
197 hres = alloc_bool(ctx, object_prototype, &bool);
198 if(FAILED(hres))
199 return hres;
201 hres = create_builtin_constructor(ctx, BoolConstr_value, L"Boolean", NULL,
202 PROPF_CONSTR|1, &bool->dispex, ret);
204 jsdisp_release(&bool->dispex);
205 return hres;
208 HRESULT create_bool(script_ctx_t *ctx, BOOL b, jsdisp_t **ret)
210 BoolInstance *bool;
211 HRESULT hres;
213 hres = alloc_bool(ctx, NULL, &bool);
214 if(FAILED(hres))
215 return hres;
217 bool->val = b;
219 *ret = &bool->dispex;
220 return S_OK;