00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00037
00038 const ClassInfo NumberInstanceImp::info = {"Number", 0, 0, 0};
00039
00040 NumberInstanceImp::NumberInstanceImp(const Object &proto)
00041 : ObjectImp(proto)
00042 {
00043 }
00044
00045
00046
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
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
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
00081 Value NumberProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &)
00082 {
00083 Value result;
00084
00085
00086 KJS_CHECK_THIS( NumberInstanceImp, thisObj );
00087
00088
00089 Value v = thisObj.internalValue();
00090 switch (id) {
00091 case ToString:
00092 case ToLocaleString:
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
00104
00105 const ClassInfo NumberObjectImp::info = {"Number", &InternalFunctionImp::info, &numberTable, 0};
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 NumberObjectImp::NumberObjectImp(ExecState *exec,
00118 FunctionPrototypeImp *funcProto,
00119 NumberPrototypeImp *numberProto)
00120 : InternalFunctionImp(funcProto)
00121 {
00122 Value protect(this);
00123
00124 put(exec,"prototype", Value(numberProto),DontEnum|DontDelete|ReadOnly);
00125
00126
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
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
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
00182 Value NumberObjectImp::call(ExecState *exec, Object &, const List &args)
00183 {
00184 if (args.isEmpty())
00185 return Number(0);
00186 else
00187 return Number(args[0].toNumber(exec));
00188 }