gecko/js/src/jsbool.cpp

189 lines
5.3 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
2010-05-10 22:01:31 -07:00
#define __STDC_LIMIT_MACROS
/*
* JS boolean implementation.
*/
#include "jstypes.h"
#include "jsstdint.h"
#include "jsutil.h" /* Added by JSIFY */
#include "jsapi.h"
#include "jsatom.h"
#include "jsbool.h"
#include "jscntxt.h"
#include "jsversion.h"
#include "jslock.h"
#include "jsnum.h"
#include "jsobj.h"
#include "jsstr.h"
#include "jsvector.h"
2010-05-10 22:01:31 -07:00
#include "jsobjinlines.h"
2010-03-03 17:52:26 -08:00
2010-05-10 22:01:31 -07:00
using namespace js;
2010-05-10 22:01:31 -07:00
Class js_BooleanClass = {
"Boolean",
JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_HAS_CACHED_PROTO(JSProto_Boolean),
2010-05-10 22:01:31 -07:00
PropertyStub, PropertyStub, PropertyStub, PropertyStub,
EnumerateStub, ResolveStub, ConvertStub, NULL,
JSCLASS_NO_OPTIONAL_MEMBERS
};
#if JS_HAS_TOSOURCE
#include "jsprf.h"
static JSBool
2010-05-10 22:01:31 -07:00
bool_toSource(JSContext *cx, uintN argc, Value *vp)
{
2010-05-13 18:55:25 -07:00
const Value *primp;
if (!js_GetPrimitiveThis(cx, vp, &js_BooleanClass, &primp))
return JS_FALSE;
2010-05-13 18:55:25 -07:00
char buf[32];
JS_snprintf(buf, sizeof buf, "(new %s(%s))",
js_BooleanClass.name,
JS_BOOLEAN_STR(primp->toBoolean()));
2010-05-13 18:55:25 -07:00
JSString *str = JS_NewStringCopyZ(cx, buf);
if (!str)
return JS_FALSE;
2010-05-10 22:01:31 -07:00
vp->setString(str);
return JS_TRUE;
}
#endif
static JSBool
2010-05-10 22:01:31 -07:00
bool_toString(JSContext *cx, uintN argc, Value *vp)
{
2010-05-13 18:55:25 -07:00
const Value *primp;
if (!js_GetPrimitiveThis(cx, vp, &js_BooleanClass, &primp))
return JS_FALSE;
JSAtom *atom = cx->runtime->atomState.booleanAtoms[primp->toBoolean() ? 1 : 0];
2010-05-13 18:55:25 -07:00
JSString *str = ATOM_TO_STRING(atom);
if (!str)
return JS_FALSE;
2010-05-10 22:01:31 -07:00
vp->setString(str);
return JS_TRUE;
}
static JSBool
2010-05-10 22:01:31 -07:00
bool_valueOf(JSContext *cx, uintN argc, Value *vp)
{
2010-05-13 18:55:25 -07:00
const Value *primp;
if (!js_GetPrimitiveThis(cx, vp, &js_BooleanClass, &primp))
return JS_FALSE;
2010-05-17 22:15:51 -07:00
*vp = *primp;
2010-05-13 18:55:25 -07:00
return JS_TRUE;
}
static JSFunctionSpec boolean_methods[] = {
#if JS_HAS_TOSOURCE
JS_FN(js_toSource_str, bool_toSource, 0, JSFUN_THISP_BOOLEAN),
#endif
JS_FN(js_toString_str, bool_toString, 0, JSFUN_THISP_BOOLEAN),
JS_FN(js_valueOf_str, bool_valueOf, 0, JSFUN_THISP_BOOLEAN),
JS_FN(js_toJSON_str, bool_valueOf, 0, JSFUN_THISP_BOOLEAN),
JS_FS_END
};
static JSBool
2010-05-10 22:01:31 -07:00
Boolean(JSContext *cx, JSObject *obj, uintN argc, Value *argv, Value *rval)
{
2010-05-10 22:01:31 -07:00
Value bval;
2010-05-10 22:01:31 -07:00
if (argc != 0)
2010-05-24 13:26:38 -07:00
bval.setBoolean(!!js_ValueToBoolean(argv[0]));
2010-05-10 22:01:31 -07:00
else
bval.setBoolean(false);
if (!JS_IsConstructing(cx))
2010-05-17 22:15:51 -07:00
*rval = bval;
else
obj->setPrimitiveThis(bval);
return true;
}
JSObject *
js_InitBooleanClass(JSContext *cx, JSObject *obj)
{
JSObject *proto;
2010-05-10 22:01:31 -07:00
proto = js_InitClass(cx, obj, NULL, &js_BooleanClass, Boolean, 1,
NULL, boolean_methods, NULL, NULL);
if (!proto)
return NULL;
proto->setPrimitiveThis(BooleanValue(false));
return proto;
}
JSString *
js_BooleanToString(JSContext *cx, JSBool b)
{
return ATOM_TO_STRING(cx->runtime->atomState.booleanAtoms[b ? 1 : 0]);
}
/* This function implements E-262-3 section 9.8, toString. */
JSBool
js_BooleanToCharBuffer(JSContext *cx, JSBool b, JSCharBuffer &cb)
{
return b ? js_AppendLiteral(cb, "true") : js_AppendLiteral(cb, "false");
}
JSBool
2010-05-10 22:01:31 -07:00
js_ValueToBoolean(const Value &v)
{
2010-05-10 22:01:31 -07:00
if (v.isNullOrUndefined())
return JS_FALSE;
2010-05-10 22:01:31 -07:00
if (v.isObject())
return JS_TRUE;
2010-05-10 22:01:31 -07:00
if (v.isString())
return v.toString()->length() != 0;
2010-05-10 22:01:31 -07:00
if (v.isInt32())
return v.toInt32() != 0;
2010-05-10 22:01:31 -07:00
if (v.isDouble()) {
jsdouble d;
d = v.toDouble();
return !JSDOUBLE_IS_NaN(d) && d != 0;
}
2010-05-10 22:01:31 -07:00
JS_ASSERT(v.isBoolean());
return v.toBoolean();
}