2009-06-10 18:29:44 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* ***** 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 ***** */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JS boolean implementation.
|
|
|
|
*/
|
|
|
|
#include "jstypes.h"
|
2009-03-18 11:38:16 -07:00
|
|
|
#include "jsstdint.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsutil.h" /* Added by JSIFY */
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsatom.h"
|
|
|
|
#include "jsbool.h"
|
|
|
|
#include "jscntxt.h"
|
2008-09-05 10:19:17 -07:00
|
|
|
#include "jsversion.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jslock.h"
|
|
|
|
#include "jsnum.h"
|
|
|
|
#include "jsobj.h"
|
|
|
|
#include "jsstr.h"
|
2009-06-30 17:19:42 -07:00
|
|
|
#include "jsvector.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-14 18:57:30 -07:00
|
|
|
#include "jsobjinlines.h"
|
|
|
|
|
2008-06-25 02:43:02 -07:00
|
|
|
/* Check pseudo-booleans values. */
|
2009-04-24 16:28:21 -07:00
|
|
|
JS_STATIC_ASSERT(!(JSVAL_TRUE & JSVAL_HOLE_FLAG));
|
|
|
|
JS_STATIC_ASSERT(!(JSVAL_FALSE & JSVAL_HOLE_FLAG));
|
|
|
|
JS_STATIC_ASSERT(!(JSVAL_VOID & JSVAL_HOLE_FLAG));
|
|
|
|
JS_STATIC_ASSERT((JSVAL_HOLE & JSVAL_HOLE_FLAG));
|
|
|
|
JS_STATIC_ASSERT((JSVAL_HOLE & ~JSVAL_HOLE_FLAG) == JSVAL_VOID);
|
|
|
|
JS_STATIC_ASSERT(!(JSVAL_ARETURN & JSVAL_HOLE_FLAG));
|
2008-06-25 02:43:02 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSClass js_BooleanClass = {
|
|
|
|
"Boolean",
|
2009-08-04 14:06:55 -07:00
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_HAS_CACHED_PROTO(JSProto_Boolean),
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
|
2009-07-22 09:23:21 -07:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL,
|
2007-03-22 10:30:00 -07:00
|
|
|
JSCLASS_NO_OPTIONAL_MEMBERS
|
|
|
|
};
|
|
|
|
|
|
|
|
#if JS_HAS_TOSOURCE
|
|
|
|
#include "jsprf.h"
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
bool_toSource(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsval v;
|
|
|
|
char buf[32];
|
|
|
|
JSString *str;
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
if (!js_GetPrimitiveThis(cx, vp, &js_BooleanClass, &v))
|
|
|
|
return JS_FALSE;
|
|
|
|
JS_ASSERT(JSVAL_IS_BOOLEAN(v));
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_snprintf(buf, sizeof buf, "(new %s(%s))",
|
|
|
|
js_BooleanClass.name,
|
2007-08-07 00:29:32 -07:00
|
|
|
JS_BOOLEAN_STR(JSVAL_TO_BOOLEAN(v)));
|
2007-03-22 10:30:00 -07:00
|
|
|
str = JS_NewStringCopyZ(cx, buf);
|
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = STRING_TO_JSVAL(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
bool_toString(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsval v;
|
|
|
|
JSAtom *atom;
|
|
|
|
JSString *str;
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
if (!js_GetPrimitiveThis(cx, vp, &js_BooleanClass, &v))
|
|
|
|
return JS_FALSE;
|
|
|
|
JS_ASSERT(JSVAL_IS_BOOLEAN(v));
|
2007-03-22 10:30:00 -07:00
|
|
|
atom = cx->runtime->atomState.booleanAtoms[JSVAL_TO_BOOLEAN(v) ? 1 : 0];
|
|
|
|
str = ATOM_TO_STRING(atom);
|
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = STRING_TO_JSVAL(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
bool_valueOf(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
return js_GetPrimitiveThis(cx, vp, &js_BooleanClass, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSFunctionSpec boolean_methods[] = {
|
|
|
|
#if JS_HAS_TOSOURCE
|
2008-08-08 09:02:50 -07:00
|
|
|
JS_FN(js_toSource_str, bool_toSource, 0, JSFUN_THISP_BOOLEAN),
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2008-08-08 09:02:50 -07:00
|
|
|
JS_FN(js_toString_str, bool_toString, 0, JSFUN_THISP_BOOLEAN),
|
|
|
|
JS_FN(js_valueOf_str, bool_valueOf, 0, JSFUN_THISP_BOOLEAN),
|
2008-10-17 11:57:34 -07:00
|
|
|
JS_FN(js_toJSON_str, bool_valueOf, 0, JSFUN_THISP_BOOLEAN),
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
Boolean(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
|
|
|
{
|
|
|
|
jsval bval;
|
|
|
|
|
2008-01-19 16:56:08 -08:00
|
|
|
bval = (argc != 0)
|
|
|
|
? BOOLEAN_TO_JSVAL(js_ValueToBoolean(argv[0]))
|
2008-01-19 18:52:23 -08:00
|
|
|
: JSVAL_FALSE;
|
2009-08-04 14:06:55 -07:00
|
|
|
if (!JS_IsConstructing(cx))
|
2007-03-22 10:30:00 -07:00
|
|
|
*rval = bval;
|
2009-08-04 14:06:55 -07:00
|
|
|
else
|
2010-04-14 18:57:30 -07:00
|
|
|
obj->setPrimitiveThis(bval);
|
2009-08-04 14:06:55 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
js_InitBooleanClass(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
JSObject *proto;
|
|
|
|
|
|
|
|
proto = JS_InitClass(cx, obj, NULL, &js_BooleanClass, Boolean, 1,
|
|
|
|
NULL, boolean_methods, NULL, NULL);
|
|
|
|
if (!proto)
|
|
|
|
return NULL;
|
2010-04-14 18:57:30 -07:00
|
|
|
proto->setPrimitiveThis(JSVAL_FALSE);
|
2007-03-22 10:30:00 -07:00
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSString *
|
|
|
|
js_BooleanToString(JSContext *cx, JSBool b)
|
|
|
|
{
|
|
|
|
return ATOM_TO_STRING(cx->runtime->atomState.booleanAtoms[b ? 1 : 0]);
|
|
|
|
}
|
|
|
|
|
2009-06-30 17:19:42 -07:00
|
|
|
/* This function implements E-262-3 section 9.8, toString. */
|
|
|
|
JSBool
|
2009-08-14 16:10:59 -07:00
|
|
|
js_BooleanToCharBuffer(JSContext *cx, JSBool b, JSCharBuffer &cb)
|
2009-06-30 17:19:42 -07:00
|
|
|
{
|
2009-08-14 16:10:59 -07:00
|
|
|
return b ? js_AppendLiteral(cb, "true") : js_AppendLiteral(cb, "false");
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool
|
2008-01-19 16:56:08 -08:00
|
|
|
js_ValueToBoolean(jsval v)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-01-19 16:56:08 -08:00
|
|
|
if (JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v))
|
|
|
|
return JS_FALSE;
|
|
|
|
if (JSVAL_IS_OBJECT(v))
|
|
|
|
return JS_TRUE;
|
|
|
|
if (JSVAL_IS_STRING(v))
|
2009-06-10 18:29:44 -07:00
|
|
|
return JSVAL_TO_STRING(v)->length() != 0;
|
2008-01-19 16:56:08 -08:00
|
|
|
if (JSVAL_IS_INT(v))
|
|
|
|
return JSVAL_TO_INT(v) != 0;
|
|
|
|
if (JSVAL_IS_DOUBLE(v)) {
|
|
|
|
jsdouble d;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
d = *JSVAL_TO_DOUBLE(v);
|
2008-01-19 16:56:08 -08:00
|
|
|
return !JSDOUBLE_IS_NaN(d) && d != 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-01-19 16:56:08 -08:00
|
|
|
JS_ASSERT(JSVAL_IS_BOOLEAN(v));
|
|
|
|
return JSVAL_TO_BOOLEAN(v);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|