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
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
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};
35 static inline BoolInstance *bool_this(vdisp_t *jsthis)
37 return is_vclass(jsthis, JSCLASS_BOOLEAN) ? (BoolInstance*)jsthis->u.jsdisp : NULL;
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, VARIANT *argv,
42 jsval_t *r, jsexcept_t *ei)
46 static const WCHAR trueW[] = {'t','r','u','e',0};
47 static const WCHAR falseW[] = {'f','a','l','s','e',0};
51 if(!(bool = bool_this(jsthis)))
52 return throw_type_error(ctx, ei, JS_E_BOOLEAN_EXPECTED, NULL);
57 if(bool->val) val = SysAllocString(trueW);
58 else val = SysAllocString(falseW);
63 *r = jsval_string(val);
69 /* ECMA-262 3rd Edition 15.6.4.3 */
70 static HRESULT Bool_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
71 jsval_t *r, jsexcept_t *ei)
77 if(!(bool = bool_this(jsthis)))
78 return throw_type_error(ctx, ei, JS_E_BOOLEAN_EXPECTED, NULL);
81 *r = jsval_bool(bool->val);
85 static HRESULT Bool_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
86 jsval_t *r, jsexcept_t *ei)
92 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
94 FIXME("unimplemented flags %x\n", flags);
102 static const builtin_prop_t Bool_props[] = {
103 {toStringW, Bool_toString, PROPF_METHOD},
104 {valueOfW, Bool_valueOf, PROPF_METHOD}
107 static const builtin_info_t Bool_info = {
109 {NULL, Bool_value, 0},
110 sizeof(Bool_props)/sizeof(*Bool_props),
116 static const builtin_info_t BoolInst_info = {
118 {NULL, Bool_value, 0},
124 static HRESULT BoolConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
125 jsval_t *r, jsexcept_t *ei)
128 VARIANT_BOOL value = VARIANT_FALSE;
131 hres = to_boolean(argv, &value);
137 case DISPATCH_CONSTRUCT: {
140 hres = create_bool(ctx, value, &bool);
144 *r = jsval_obj(bool);
150 *r = jsval_bool(value);
154 FIXME("unimplemented flags %x\n", flags);
161 static HRESULT alloc_bool(script_ctx_t *ctx, jsdisp_t *object_prototype, BoolInstance **ret)
166 bool = heap_alloc_zero(sizeof(BoolInstance));
168 return E_OUTOFMEMORY;
171 hres = init_dispex(&bool->dispex, ctx, &Bool_info, object_prototype);
173 hres = init_dispex_from_constr(&bool->dispex, ctx, &BoolInst_info, ctx->bool_constr);
184 HRESULT create_bool_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
189 static const WCHAR BooleanW[] = {'B','o','o','l','e','a','n',0};
191 hres = alloc_bool(ctx, object_prototype, &bool);
195 hres = create_builtin_constructor(ctx, BoolConstr_value, BooleanW, NULL,
196 PROPF_CONSTR|1, &bool->dispex, ret);
198 jsdisp_release(&bool->dispex);
202 HRESULT create_bool(script_ctx_t *ctx, VARIANT_BOOL b, jsdisp_t **ret)
207 hres = alloc_bool(ctx, NULL, &bool);
213 *ret = &bool->dispex;