1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
2 |
* |
* |
3 |
* ***** BEGIN LICENSE BLOCK ***** |
* ***** BEGIN LICENSE BLOCK ***** |
4 |
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
40 |
/* |
/* |
41 |
* JS boolean implementation. |
* JS boolean implementation. |
42 |
*/ |
*/ |
|
#include "jsstddef.h" |
|
43 |
#include "jstypes.h" |
#include "jstypes.h" |
44 |
|
#include "jsstdint.h" |
45 |
#include "jsutil.h" /* Added by JSIFY */ |
#include "jsutil.h" /* Added by JSIFY */ |
46 |
#include "jsapi.h" |
#include "jsapi.h" |
47 |
#include "jsatom.h" |
#include "jsatom.h" |
52 |
#include "jsnum.h" |
#include "jsnum.h" |
53 |
#include "jsobj.h" |
#include "jsobj.h" |
54 |
#include "jsstr.h" |
#include "jsstr.h" |
55 |
|
#include "jsvector.h" |
56 |
|
|
57 |
/* Check pseudo-booleans values. */ |
/* Check pseudo-booleans values. */ |
58 |
JS_STATIC_ASSERT(!(JSVAL_TRUE & JSVAL_HOLE_FLAG)); |
JS_STATIC_ASSERT(!(JSVAL_TRUE & JSVAL_HOLE_FLAG)); |
64 |
|
|
65 |
JSClass js_BooleanClass = { |
JSClass js_BooleanClass = { |
66 |
"Boolean", |
"Boolean", |
67 |
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_CACHED_PROTO(JSProto_Boolean), |
JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_HAS_CACHED_PROTO(JSProto_Boolean), |
68 |
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, |
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, |
69 |
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, |
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL, |
70 |
JSCLASS_NO_OPTIONAL_MEMBERS |
JSCLASS_NO_OPTIONAL_MEMBERS |
71 |
}; |
}; |
72 |
|
|
136 |
bval = (argc != 0) |
bval = (argc != 0) |
137 |
? BOOLEAN_TO_JSVAL(js_ValueToBoolean(argv[0])) |
? BOOLEAN_TO_JSVAL(js_ValueToBoolean(argv[0])) |
138 |
: JSVAL_FALSE; |
: JSVAL_FALSE; |
139 |
if (!JS_IsConstructing(cx)) { |
if (!JS_IsConstructing(cx)) |
140 |
*rval = bval; |
*rval = bval; |
141 |
return JS_TRUE; |
else |
142 |
} |
obj->fslots[JSSLOT_PRIMITIVE_THIS] = bval; |
143 |
STOBJ_SET_SLOT(obj, JSSLOT_PRIVATE, bval); |
return true; |
|
return JS_TRUE; |
|
144 |
} |
} |
145 |
|
|
146 |
JSObject * |
JSObject * |
152 |
NULL, boolean_methods, NULL, NULL); |
NULL, boolean_methods, NULL, NULL); |
153 |
if (!proto) |
if (!proto) |
154 |
return NULL; |
return NULL; |
155 |
STOBJ_SET_SLOT(proto, JSSLOT_PRIVATE, JSVAL_FALSE); |
proto->fslots[JSSLOT_PRIMITIVE_THIS] = JSVAL_FALSE; |
156 |
return proto; |
return proto; |
157 |
} |
} |
158 |
|
|
162 |
return ATOM_TO_STRING(cx->runtime->atomState.booleanAtoms[b ? 1 : 0]); |
return ATOM_TO_STRING(cx->runtime->atomState.booleanAtoms[b ? 1 : 0]); |
163 |
} |
} |
164 |
|
|
165 |
|
/* This function implements E-262-3 section 9.8, toString. */ |
166 |
|
JSBool |
167 |
|
js_BooleanToCharBuffer(JSContext *cx, JSBool b, JSCharBuffer &cb) |
168 |
|
{ |
169 |
|
return b ? js_AppendLiteral(cb, "true") : js_AppendLiteral(cb, "false"); |
170 |
|
} |
171 |
|
|
172 |
JSBool |
JSBool |
173 |
js_ValueToBoolean(jsval v) |
js_ValueToBoolean(jsval v) |
174 |
{ |
{ |
177 |
if (JSVAL_IS_OBJECT(v)) |
if (JSVAL_IS_OBJECT(v)) |
178 |
return JS_TRUE; |
return JS_TRUE; |
179 |
if (JSVAL_IS_STRING(v)) |
if (JSVAL_IS_STRING(v)) |
180 |
return JSSTRING_LENGTH(JSVAL_TO_STRING(v)) != 0; |
return JSVAL_TO_STRING(v)->length() != 0; |
181 |
if (JSVAL_IS_INT(v)) |
if (JSVAL_IS_INT(v)) |
182 |
return JSVAL_TO_INT(v) != 0; |
return JSVAL_TO_INT(v) != 0; |
183 |
if (JSVAL_IS_DOUBLE(v)) { |
if (JSVAL_IS_DOUBLE(v)) { |