Back to index

lightning-sunbird  0.9+nobinonly
jsbool.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /*
00041  * JS boolean implementation.
00042  */
00043 #include "jsstddef.h"
00044 #include "jstypes.h"
00045 #include "jsutil.h" /* Added by JSIFY */
00046 #include "jsapi.h"
00047 #include "jsatom.h"
00048 #include "jsbool.h"
00049 #include "jscntxt.h"
00050 #include "jsconfig.h"
00051 #include "jsinterp.h"
00052 #include "jslock.h"
00053 #include "jsnum.h"
00054 #include "jsobj.h"
00055 #include "jsstr.h"
00056 
00057 JSClass js_BooleanClass = {
00058     "Boolean",
00059     JSCLASS_HAS_PRIVATE | JSCLASS_HAS_CACHED_PROTO(JSProto_Boolean),
00060     JS_PropertyStub,  JS_PropertyStub,  JS_PropertyStub,  JS_PropertyStub,
00061     JS_EnumerateStub, JS_ResolveStub,   JS_ConvertStub,   JS_FinalizeStub,
00062     JSCLASS_NO_OPTIONAL_MEMBERS
00063 };
00064 
00065 #if JS_HAS_TOSOURCE
00066 #include "jsprf.h"
00067 
00068 static JSBool
00069 bool_toSource(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
00070               jsval *rval)
00071 {
00072     jsval v;
00073     char buf[32];
00074     JSString *str;
00075 
00076     if (JSVAL_IS_BOOLEAN((jsval)obj)) {
00077         v = (jsval)obj;
00078     } else {
00079         if (!JS_InstanceOf(cx, obj, &js_BooleanClass, argv))
00080             return JS_FALSE;
00081         v = OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
00082         if (!JSVAL_IS_BOOLEAN(v))
00083             return js_obj_toSource(cx, obj, argc, argv, rval);
00084     }
00085     JS_snprintf(buf, sizeof buf, "(new %s(%s))",
00086                 js_BooleanClass.name,
00087                 js_boolean_strs[JSVAL_TO_BOOLEAN(v) ? 1 : 0]);
00088     str = JS_NewStringCopyZ(cx, buf);
00089     if (!str)
00090         return JS_FALSE;
00091     *rval = STRING_TO_JSVAL(str);
00092     return JS_TRUE;
00093 }
00094 #endif
00095 
00096 static JSBool
00097 bool_toString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
00098               jsval *rval)
00099 {
00100     jsval v;
00101     JSAtom *atom;
00102     JSString *str;
00103 
00104     if (JSVAL_IS_BOOLEAN((jsval)obj)) {
00105         v = (jsval)obj;
00106     } else {
00107         if (!JS_InstanceOf(cx, obj, &js_BooleanClass, argv))
00108             return JS_FALSE;
00109         v = OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
00110         if (!JSVAL_IS_BOOLEAN(v))
00111             return js_obj_toString(cx, obj, argc, argv, rval);
00112     }
00113     atom = cx->runtime->atomState.booleanAtoms[JSVAL_TO_BOOLEAN(v) ? 1 : 0];
00114     str = ATOM_TO_STRING(atom);
00115     if (!str)
00116         return JS_FALSE;
00117     *rval = STRING_TO_JSVAL(str);
00118     return JS_TRUE;
00119 }
00120 
00121 static JSBool
00122 bool_valueOf(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00123 {
00124     if (JSVAL_IS_BOOLEAN((jsval)obj)) {
00125         *rval = (jsval)obj;
00126         return JS_TRUE;
00127     }
00128     if (!JS_InstanceOf(cx, obj, &js_BooleanClass, argv))
00129         return JS_FALSE;
00130     *rval = OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
00131     return JS_TRUE;
00132 }
00133 
00134 static JSFunctionSpec boolean_methods[] = {
00135 #if JS_HAS_TOSOURCE
00136     {js_toSource_str,   bool_toSource,          0,JSFUN_THISP_BOOLEAN,0},
00137 #endif
00138     {js_toString_str,   bool_toString,          0,JSFUN_THISP_BOOLEAN,0},
00139     {js_valueOf_str,    bool_valueOf,           0,JSFUN_THISP_BOOLEAN,0},
00140     {0,0,0,0,0}
00141 };
00142 
00143 static JSBool
00144 Boolean(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00145 {
00146     JSBool b;
00147     jsval bval;
00148 
00149     if (argc != 0) {
00150         if (!js_ValueToBoolean(cx, argv[0], &b))
00151             return JS_FALSE;
00152         bval = BOOLEAN_TO_JSVAL(b);
00153     } else {
00154         bval = JSVAL_FALSE;
00155     }
00156     if (!(cx->fp->flags & JSFRAME_CONSTRUCTING)) {
00157         *rval = bval;
00158         return JS_TRUE;
00159     }
00160     OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, bval);
00161     return JS_TRUE;
00162 }
00163 
00164 JSObject *
00165 js_InitBooleanClass(JSContext *cx, JSObject *obj)
00166 {
00167     JSObject *proto;
00168 
00169     proto = JS_InitClass(cx, obj, NULL, &js_BooleanClass, Boolean, 1,
00170                         NULL, boolean_methods, NULL, NULL);
00171     if (!proto)
00172         return NULL;
00173     OBJ_SET_SLOT(cx, proto, JSSLOT_PRIVATE, JSVAL_FALSE);
00174     return proto;
00175 }
00176 
00177 JSObject *
00178 js_BooleanToObject(JSContext *cx, JSBool b)
00179 {
00180     JSObject *obj;
00181 
00182     obj = js_NewObject(cx, &js_BooleanClass, NULL, NULL);
00183     if (!obj)
00184         return NULL;
00185     OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, BOOLEAN_TO_JSVAL(b));
00186     return obj;
00187 }
00188 
00189 JSString *
00190 js_BooleanToString(JSContext *cx, JSBool b)
00191 {
00192     return ATOM_TO_STRING(cx->runtime->atomState.booleanAtoms[b ? 1 : 0]);
00193 }
00194 
00195 JSBool
00196 js_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp)
00197 {
00198     JSBool b;
00199     jsdouble d;
00200 
00201     if (JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v)) {
00202         b = JS_FALSE;
00203     } else if (JSVAL_IS_OBJECT(v)) {
00204         if (!JS_VERSION_IS_ECMA(cx)) {
00205             if (!OBJ_DEFAULT_VALUE(cx, JSVAL_TO_OBJECT(v), JSTYPE_BOOLEAN, &v))
00206                 return JS_FALSE;
00207             if (!JSVAL_IS_BOOLEAN(v))
00208                 v = JSVAL_TRUE;         /* non-null object is true */
00209             b = JSVAL_TO_BOOLEAN(v);
00210         } else {
00211             b = JS_TRUE;
00212         }
00213     } else if (JSVAL_IS_STRING(v)) {
00214         b = JSSTRING_LENGTH(JSVAL_TO_STRING(v)) ? JS_TRUE : JS_FALSE;
00215     } else if (JSVAL_IS_INT(v)) {
00216         b = JSVAL_TO_INT(v) ? JS_TRUE : JS_FALSE;
00217     } else if (JSVAL_IS_DOUBLE(v)) {
00218         d = *JSVAL_TO_DOUBLE(v);
00219         b = (!JSDOUBLE_IS_NaN(d) && d != 0) ? JS_TRUE : JS_FALSE;
00220     } else {
00221         JS_ASSERT(JSVAL_IS_BOOLEAN(v));
00222         b = JSVAL_TO_BOOLEAN(v);
00223     }
00224 
00225     *bp = b;
00226     return JS_TRUE;
00227 }