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 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
)
62 if(!(bool = bool_this(jsthis
)))
63 return JS_E_BOOLEAN_EXPECTED
;
68 val
= jsstr_alloc(bool->val
? L
"true" : L
"false");
72 *r
= jsval_string(val
);
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
)
85 if(!(bool = bool_this(jsthis
)))
86 return JS_E_BOOLEAN_EXPECTED
;
89 *r
= jsval_bool(bool->val
);
93 static HRESULT
Bool_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
100 return JS_E_FUNCTION_EXPECTED
;
102 FIXME("unimplemented flags %x\n", flags
);
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
= {
117 {NULL
, Bool_value
, 0},
118 ARRAY_SIZE(Bool_props
),
124 static const builtin_info_t BoolInst_info
= {
126 {NULL
, Bool_value
, 0},
132 static HRESULT
BoolConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
139 hres
= to_boolean(argv
[0], &value
);
145 case DISPATCH_CONSTRUCT
: {
148 hres
= create_bool(ctx
, value
, &bool);
152 *r
= jsval_obj(bool);
158 *r
= jsval_bool(value
);
162 FIXME("unimplemented flags %x\n", flags
);
169 static HRESULT
alloc_bool(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, BoolInstance
**ret
)
174 bool = heap_alloc_zero(sizeof(BoolInstance
));
176 return E_OUTOFMEMORY
;
179 hres
= init_dispex(&bool->dispex
, ctx
, &Bool_info
, object_prototype
);
181 hres
= init_dispex_from_constr(&bool->dispex
, ctx
, &BoolInst_info
, ctx
->bool_constr
);
192 HRESULT
create_bool_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
197 hres
= alloc_bool(ctx
, object_prototype
, &bool);
201 hres
= create_builtin_constructor(ctx
, BoolConstr_value
, L
"Boolean", NULL
,
202 PROPF_CONSTR
|1, &bool->dispex
, ret
);
204 jsdisp_release(&bool->dispex
);
208 HRESULT
create_bool(script_ctx_t
*ctx
, BOOL b
, jsdisp_t
**ret
)
213 hres
= alloc_bool(ctx
, NULL
, &bool);
219 *ret
= &bool->dispex
;