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 HRESULT
boolval_this(jsval_t vthis
, BOOL
*ret
)
43 *ret
= get_bool(vthis
);
44 else if(is_object_instance(vthis
) && (jsdisp
= to_jsdisp(get_object(vthis
))) && is_class(jsdisp
, JSCLASS_BOOLEAN
))
45 *ret
= bool_from_jsdisp(jsdisp
)->val
;
47 return JS_E_BOOLEAN_EXPECTED
;
51 BOOL
bool_obj_value(jsdisp_t
*obj
)
53 assert(is_class(obj
, JSCLASS_BOOLEAN
));
54 return bool_from_jsdisp(obj
)->val
;
57 /* ECMA-262 3rd Edition 15.6.4.2 */
58 static HRESULT
Bool_toString(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
65 hres
= boolval_this(vthis
, &boolval
);
72 val
= jsstr_alloc(boolval
? L
"true" : L
"false");
76 *r
= jsval_string(val
);
82 /* ECMA-262 3rd Edition 15.6.4.3 */
83 static HRESULT
Bool_valueOf(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
90 hres
= boolval_this(vthis
, &boolval
);
95 *r
= jsval_bool(boolval
);
99 static HRESULT
Bool_value(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
106 return JS_E_FUNCTION_EXPECTED
;
108 FIXME("unimplemented flags %x\n", flags
);
116 static const builtin_prop_t Bool_props
[] = {
117 {L
"toString", Bool_toString
, PROPF_METHOD
},
118 {L
"valueOf", Bool_valueOf
, PROPF_METHOD
}
121 static const builtin_info_t Bool_info
= {
124 ARRAY_SIZE(Bool_props
),
130 static const builtin_info_t BoolInst_info
= {
138 static HRESULT
BoolConstr_value(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
145 hres
= to_boolean(argv
[0], &value
);
151 case DISPATCH_CONSTRUCT
: {
157 hres
= create_bool(ctx
, value
, &bool);
161 *r
= jsval_obj(bool);
167 *r
= jsval_bool(value
);
171 FIXME("unimplemented flags %x\n", flags
);
178 static HRESULT
alloc_bool(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, BoolInstance
**ret
)
183 bool = calloc(1, sizeof(BoolInstance
));
185 return E_OUTOFMEMORY
;
188 hres
= init_dispex(&bool->dispex
, ctx
, &Bool_info
, object_prototype
);
190 hres
= init_dispex_from_constr(&bool->dispex
, ctx
, &BoolInst_info
, ctx
->bool_constr
);
201 HRESULT
create_bool_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
206 hres
= alloc_bool(ctx
, object_prototype
, &bool);
210 hres
= create_builtin_constructor(ctx
, BoolConstr_value
, L
"Boolean", NULL
,
211 PROPF_CONSTR
|1, &bool->dispex
, ret
);
213 jsdisp_release(&bool->dispex
);
217 HRESULT
create_bool(script_ctx_t
*ctx
, BOOL b
, jsdisp_t
**ret
)
222 hres
= alloc_bool(ctx
, NULL
, &bool);
228 *ret
= &bool->dispex
;