kjs Library API Documentation

number_object.cpp

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Lesser General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Lesser General Public
00017  *  License along with this library; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  $Id: number_object.cpp,v 1.24 2002/10/24 17:03:16 faure Exp $
00021  */
00022 
00023 #include "value.h"
00024 #include "object.h"
00025 #include "types.h"
00026 #include "interpreter.h"
00027 #include "operations.h"
00028 #include "number_object.h"
00029 #include "error_object.h"
00030 
00031 #include "number_object.lut.h"
00032 
00033 using namespace KJS;
00034 
00035 
00036 // ------------------------------ NumberInstanceImp ----------------------------
00037 
00038 const ClassInfo NumberInstanceImp::info = {"Number", 0, 0, 0};
00039 
00040 NumberInstanceImp::NumberInstanceImp(const Object &proto)
00041   : ObjectImp(proto)
00042 {
00043 }
00044 // ------------------------------ NumberPrototypeImp ---------------------------
00045 
00046 // ECMA 15.7.4
00047 
00048 NumberPrototypeImp::NumberPrototypeImp(ExecState *exec,
00049                                        ObjectPrototypeImp *objProto,
00050                                        FunctionPrototypeImp *funcProto)
00051   : NumberInstanceImp(Object(objProto))
00052 {
00053   Value protect(this);
00054   setInternalValue(Number(0));
00055 
00056   // The constructor will be added later, after NumberObjectImp has been constructed
00057 
00058   put(exec,"toString",       Object(new NumberProtoFuncImp(exec,funcProto,NumberProtoFuncImp::ToString,       1)), DontEnum);
00059   put(exec,"toLocaleString", Object(new NumberProtoFuncImp(exec,funcProto,NumberProtoFuncImp::ToLocaleString, 0)), DontEnum);
00060   put(exec,"valueOf",        Object(new NumberProtoFuncImp(exec,funcProto,NumberProtoFuncImp::ValueOf,        0)), DontEnum);
00061 }
00062 
00063 
00064 // ------------------------------ NumberProtoFuncImp ---------------------------
00065 
00066 NumberProtoFuncImp::NumberProtoFuncImp(ExecState *exec,
00067                                        FunctionPrototypeImp *funcProto, int i, int len)
00068   : InternalFunctionImp(funcProto), id(i)
00069 {
00070   Value protect(this);
00071   put(exec,"length",Number(len),DontDelete|ReadOnly|DontEnum);
00072 }
00073 
00074 
00075 bool NumberProtoFuncImp::implementsCall() const
00076 {
00077   return true;
00078 }
00079 
00080 // ECMA 15.7.4.2 - 15.7.4.7
00081 Value NumberProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &/*args*/)
00082 {
00083   Value result;
00084 
00085   // no generic function. "this" has to be a Number object
00086   KJS_CHECK_THIS( NumberInstanceImp, thisObj );
00087 
00088   // execute "toString()" or "valueOf()", respectively
00089   Value v = thisObj.internalValue();
00090   switch (id) {
00091   case ToString:
00092   case ToLocaleString: /* TODO */
00093     result = String(v.toString(exec));
00094     break;
00095   case ValueOf:
00096     result = Number(v.toNumber(exec));
00097     break;
00098   }
00099 
00100   return result;
00101 }
00102 
00103 // ------------------------------ NumberObjectImp ------------------------------
00104 
00105 const ClassInfo NumberObjectImp::info = {"Number", &InternalFunctionImp::info, &numberTable, 0};
00106 //const ClassInfo NumberObjectImp::info = {"Number", 0, &numberTable, 0};
00107 
00108 /* Source for number_object.lut.h
00109 @begin numberTable 5
00110   NaN           NumberObjectImp::NaNValue   DontEnum
00111   NEGATIVE_INFINITY NumberObjectImp::NegInfinity    DontEnum
00112   POSITIVE_INFINITY NumberObjectImp::PosInfinity    DontEnum
00113   MAX_VALUE     NumberObjectImp::MaxValue   DontEnum
00114   MIN_VALUE     NumberObjectImp::MinValue   DontEnum
00115 @end
00116 */
00117 NumberObjectImp::NumberObjectImp(ExecState *exec,
00118                                  FunctionPrototypeImp *funcProto,
00119                                  NumberPrototypeImp *numberProto)
00120   : InternalFunctionImp(funcProto)
00121 {
00122   Value protect(this);
00123   // Number.Prototype
00124   put(exec,"prototype", Value(numberProto),DontEnum|DontDelete|ReadOnly);
00125 
00126   // no. of arguments for constructor
00127   put(exec,"length", Number(1), ReadOnly|DontDelete|DontEnum);
00128 }
00129 
00130 Value NumberObjectImp::get(ExecState *exec, const UString &propertyName) const
00131 {
00132   return lookupGetValue<NumberObjectImp, InternalFunctionImp>( exec, propertyName, &numberTable, this );
00133 }
00134 
00135 Value NumberObjectImp::getValueProperty(ExecState *, int token) const
00136 {
00137   // ECMA 15.7.3
00138   switch(token) {
00139   case NaNValue:
00140     return Number(NaN);
00141   case NegInfinity:
00142     return Number(-Inf);
00143   case PosInfinity:
00144     return Number(Inf);
00145   case MaxValue:
00146     return Number(1.7976931348623157E+308);
00147   case MinValue:
00148     return Number(5E-324);
00149   }
00150   return Null();
00151 }
00152 
00153 bool NumberObjectImp::implementsConstruct() const
00154 {
00155   return true;
00156 }
00157 
00158 
00159 // ECMA 15.7.1
00160 Object NumberObjectImp::construct(ExecState *exec, const List &args)
00161 {
00162   Object proto = exec->interpreter()->builtinNumberPrototype();
00163   Object obj(new NumberInstanceImp(proto));
00164 
00165   Number n;
00166   if (args.isEmpty())
00167     n = Number(0);
00168   else
00169     n = args[0].toNumber(exec);
00170 
00171   obj.setInternalValue(n);
00172 
00173   return obj;
00174 }
00175 
00176 bool NumberObjectImp::implementsCall() const
00177 {
00178   return true;
00179 }
00180 
00181 // ECMA 15.7.2
00182 Value NumberObjectImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
00183 {
00184   if (args.isEmpty())
00185     return Number(0);
00186   else
00187     return Number(args[0].toNumber(exec));
00188 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:15:18 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001