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
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
34 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
35 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
37 static inline BoolInstance
*bool_this(vdisp_t
*jsthis
)
39 return is_vclass(jsthis
, JSCLASS_BOOLEAN
) ? (BoolInstance
*)jsthis
->u
.jsdisp
: NULL
;
42 BOOL
bool_obj_value(jsdisp_t
*obj
)
44 assert(is_class(obj
, JSCLASS_BOOLEAN
));
45 return ((BoolInstance
*)obj
)->val
;
48 /* ECMA-262 3rd Edition 15.6.4.2 */
49 static HRESULT
Bool_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
53 static const WCHAR trueW
[] = {'t','r','u','e',0};
54 static const WCHAR falseW
[] = {'f','a','l','s','e',0};
58 if(!(bool = bool_this(jsthis
)))
59 return throw_type_error(ctx
, JS_E_BOOLEAN_EXPECTED
, NULL
);
64 val
= jsstr_alloc(bool->val
? trueW
: falseW
);
68 *r
= jsval_string(val
);
74 /* ECMA-262 3rd Edition 15.6.4.3 */
75 static HRESULT
Bool_valueOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
81 if(!(bool = bool_this(jsthis
)))
82 return throw_type_error(ctx
, JS_E_BOOLEAN_EXPECTED
, NULL
);
85 *r
= jsval_bool(bool->val
);
89 static HRESULT
Bool_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
96 return throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
98 FIXME("unimplemented flags %x\n", flags
);
106 static const builtin_prop_t Bool_props
[] = {
107 {toStringW
, Bool_toString
, PROPF_METHOD
},
108 {valueOfW
, Bool_valueOf
, PROPF_METHOD
}
111 static const builtin_info_t Bool_info
= {
113 {NULL
, Bool_value
, 0},
114 sizeof(Bool_props
)/sizeof(*Bool_props
),
120 static const builtin_info_t BoolInst_info
= {
122 {NULL
, Bool_value
, 0},
128 static HRESULT
BoolConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
135 hres
= to_boolean(argv
[0], &value
);
141 case DISPATCH_CONSTRUCT
: {
144 hres
= create_bool(ctx
, value
, &bool);
148 *r
= jsval_obj(bool);
154 *r
= jsval_bool(value
);
158 FIXME("unimplemented flags %x\n", flags
);
165 static HRESULT
alloc_bool(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, BoolInstance
**ret
)
170 bool = heap_alloc_zero(sizeof(BoolInstance
));
172 return E_OUTOFMEMORY
;
175 hres
= init_dispex(&bool->dispex
, ctx
, &Bool_info
, object_prototype
);
177 hres
= init_dispex_from_constr(&bool->dispex
, ctx
, &BoolInst_info
, ctx
->bool_constr
);
188 HRESULT
create_bool_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
193 static const WCHAR BooleanW
[] = {'B','o','o','l','e','a','n',0};
195 hres
= alloc_bool(ctx
, object_prototype
, &bool);
199 hres
= create_builtin_constructor(ctx
, BoolConstr_value
, BooleanW
, NULL
,
200 PROPF_CONSTR
|1, &bool->dispex
, ret
);
202 jsdisp_release(&bool->dispex
);
206 HRESULT
create_bool(script_ctx_t
*ctx
, BOOL b
, jsdisp_t
**ret
)
211 hres
= alloc_bool(ctx
, NULL
, &bool);
217 *ret
= &bool->dispex
;