Changeset 798 in webkit for trunk/JavaScriptCore/kjs/number_object.cpp
- Timestamp:
- Mar 21, 2002, 4:31:57 PM (23 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/number_object.cpp
r6 r798 1 // -*- c-basic-offset: 2 -*- 1 2 /* 2 3 * This file is part of the KDE libraries … … 16 17 * License along with this library; if not, write to the Free Software 17 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * 20 * $Id$ 18 21 */ 19 22 20 #include "kjs.h" 23 #include "value.h" 24 #include "object.h" 25 #include "types.h" 26 #include "interpreter.h" 21 27 #include "operations.h" 22 28 #include "number_object.h" 29 #include "error_object.h" 30 31 #include "number_object.lut.h" 23 32 24 33 using namespace KJS; 25 34 26 NumberObject::NumberObject(const Object& funcProto, const Object &numProto) 27 : ConstructorImp(funcProto, 1) 35 36 // ------------------------------ NumberInstanceImp ---------------------------- 37 38 const ClassInfo NumberInstanceImp::info = {"Number", 0, 0, 0}; 39 40 NumberInstanceImp::NumberInstanceImp(const Object &proto) 41 : ObjectImp(proto) 28 42 { 29 // Number.Prototype 30 setPrototypeProperty(numProto); 43 } 44 // ------------------------------ NumberPrototypeImp --------------------------- 45 46 // ECMA 15.7.4 47 48 NumberPrototypeImp::NumberPrototypeImp(ExecState *exec, 49 ObjectPrototypeImp *objProto, 50 FunctionPrototypeImp *funcProto) 51 : NumberInstanceImp(Object(objProto)) 52 { 53 Value protect(this); 54 setInternalValue(Number(0)); 55 56 // The constructor will be added later, after NumberObjectImp has been constructed 57 58 put(exec,"toString", Object(new NumberProtoFuncImp(exec,funcProto,NumberProtoFuncImp::ToString, 1)), DontEnum); 59 put(exec,"toLocaleString", Object(new NumberProtoFuncImp(exec,funcProto,NumberProtoFuncImp::ToLocaleString, 0)), DontEnum); 60 put(exec,"valueOf", Object(new NumberProtoFuncImp(exec,funcProto,NumberProtoFuncImp::ValueOf, 0)), DontEnum); 31 61 } 32 62 33 // ECMA 15.7.3 34 KJSO NumberObject::get(const UString &p) const 63 64 // ------------------------------ NumberProtoFuncImp --------------------------- 65 66 NumberProtoFuncImp::NumberProtoFuncImp(ExecState *exec, 67 FunctionPrototypeImp *funcProto, int i, int len) 68 : InternalFunctionImp(funcProto), id(i) 35 69 { 36 double d; 37 38 if (p == "NaN") 39 d = NaN; 40 else if (p == "NEGATIVE_INFINITY") 41 d = -Inf; 42 else if (p == "POSITIVE_INFINITY") 43 d = Inf; 44 else 45 return Imp::get(p); 46 47 return Number(d); 70 Value protect(this); 71 put(exec,"length",Number(len),DontDelete|ReadOnly|DontEnum); 48 72 } 49 73 74 75 bool NumberProtoFuncImp::implementsCall() const 76 { 77 return true; 78 } 79 80 // ECMA 15.7.4.2 - 15.7.4.7 81 Value NumberProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &/*args*/) 82 { 83 Value result; 84 85 // no generic function. "this" has to be a Number object 86 if (!thisObj.inherits(&NumberInstanceImp::info)) { 87 Object err = Error::create(exec,TypeError); 88 exec->setException(err); 89 return err; 90 } 91 92 // execute "toString()" or "valueOf()", respectively 93 Value v = thisObj.internalValue(); 94 switch (id) { 95 case ToString: 96 case ToLocaleString: /* TODO */ 97 result = String(v.toString(exec)); 98 break; 99 case ValueOf: 100 result = Number(v.toNumber(exec)); 101 break; 102 } 103 104 return result; 105 } 106 107 // ------------------------------ NumberObjectImp ------------------------------ 108 109 const ClassInfo NumberObjectImp::info = {"Number", &InternalFunctionImp::info, &numberTable, 0}; 110 //const ClassInfo NumberObjectImp::info = {"Number", 0, &numberTable, 0}; 111 112 /* Source for number_object.lut.h 113 @begin numberTable 5 114 NaN NumberObjectImp::NaNValue DontEnum 115 NEGATIVE_INFINITY NumberObjectImp::NegInfinity DontEnum 116 POSITIVE_INFINITY NumberObjectImp::PosInfinity DontEnum 117 MAX_VALUE NumberObjectImp::MaxValue DontEnum 118 MIN_VALUE NumberObjectImp::MinValue DontEnum 119 @end 120 */ 121 NumberObjectImp::NumberObjectImp(ExecState *exec, 122 FunctionPrototypeImp *funcProto, 123 NumberPrototypeImp *numberProto) 124 : InternalFunctionImp(funcProto) 125 { 126 Value protect(this); 127 // Number.Prototype 128 put(exec,"prototype", Value(numberProto),DontEnum|DontDelete|ReadOnly); 129 130 // no. of arguments for constructor 131 put(exec,"length", Number(1), ReadOnly|DontDelete|DontEnum); 132 } 133 134 Value NumberObjectImp::get(ExecState *exec, const UString &propertyName) const 135 { 136 return lookupGetValue<NumberObjectImp, InternalFunctionImp>( exec, propertyName, &numberTable, this ); 137 } 138 139 Value NumberObjectImp::getValueProperty(ExecState *, int token) const 140 { 141 // ECMA 15.7.3 142 switch(token) { 143 case NaNValue: 144 return Number(NaN); 145 case NegInfinity: 146 return Number(-Inf); 147 case PosInfinity: 148 return Number(Inf); 149 case MaxValue: 150 return Number(1.7976931348623157E+308); 151 case MinValue: 152 return Number(5E-324); 153 } 154 return Null(); 155 } 156 157 bool NumberObjectImp::implementsConstruct() const 158 { 159 return true; 160 } 161 162 50 163 // ECMA 15.7.1 51 Completion NumberObject::execute(const List &args)164 Object NumberObjectImp::construct(ExecState *exec, const List &args) 52 165 { 166 Object proto = exec->interpreter()->builtinNumberPrototype(); 167 Object obj(new NumberInstanceImp(proto)); 168 53 169 Number n; 54 170 if (args.isEmpty()) 55 171 n = Number(0); 56 172 else 57 n = args[0].toNumber( );173 n = args[0].toNumber(exec); 58 174 59 return Completion(ReturnValue, n); 175 obj.setInternalValue(n); 176 177 return obj; 178 } 179 180 bool NumberObjectImp::implementsCall() const 181 { 182 return true; 60 183 } 61 184 62 185 // ECMA 15.7.2 63 Object NumberObject::construct(const List &args)186 Value NumberObjectImp::call(ExecState *exec, Object &/*thisObj*/, const List &args) 64 187 { 65 Number n;66 188 if (args.isEmpty()) 67 n =Number(0);189 return Number(0); 68 190 else 69 n = args[0].toNumber(); 70 71 return Object::create(NumberClass, n); 191 return Number(args[0].toNumber(exec)); 72 192 } 73 74 class NumberProtoFunc : public InternalFunctionImp {75 public:76 NumberProtoFunc(int i) : id (i) { }77 Completion execute(const List &);78 enum { ToString, ToLocaleString, ValueOf };79 private:80 int id;81 };82 83 // ECMA 15.7.4.2 - 15.7.4.784 Completion NumberProtoFunc::execute(const List &)85 {86 KJSO result;87 88 Object thisObj = Object::dynamicCast(thisValue());89 90 // no generic function. "this" has to be a Number object91 if (thisObj.isNull() || thisObj.getClass() != NumberClass) {92 result = Error::create(TypeError);93 return Completion(ReturnValue, result);94 }95 96 // execute "toString()" or "valueOf()", respectively97 KJSO v = thisObj.internalValue();98 switch (id) {99 case ToString:100 case ToLocaleString: /* TODO */101 result = v.toString();102 break;103 case ValueOf:104 result = v.toNumber();105 break;106 }107 108 return Completion(ReturnValue, result);109 }110 111 // ECMA 15.7.4112 NumberPrototype::NumberPrototype(const Object& proto)113 : ObjectImp(NumberClass, Number(0), proto)114 {115 // The constructor will be added later in NumberObject's constructor116 }117 118 KJSO NumberPrototype::get(const UString &p) const119 {120 int t;121 if (p == "toString")122 t = NumberProtoFunc::ToString;123 else if (p == "toLocaleString")124 t = NumberProtoFunc::ToLocaleString;125 else if (p == "valueOf")126 t = NumberProtoFunc::ValueOf;127 else128 return Imp::get(p);129 130 return Function(new NumberProtoFunc(t));131 }
Note:
See TracChangeset
for help on using the changeset viewer.