28 #include "interpreter.h"
37 #include "collector.h"
38 #include "operations.h"
39 #include "error_object.h"
41 #include "simple_number.h"
47 ValueImp::ValueImp() :
58 _flags |= VI_DESTRUCTED;
67 bool ValueImp::marked()
const
70 return SimpleNumber::is(
this) || (_flags & VI_MARKED);
73 void ValueImp::setGcAllowed()
78 if (!SimpleNumber::is(
this))
79 _flags |= VI_GCALLOWED;
82 void* ValueImp::operator
new(
size_t s)
87 void ValueImp::operator
delete(
void*)
92 bool ValueImp::toUInt32(
unsigned&)
const
98 int ValueImp::toInteger(
ExecState *exec)
const
101 if (dispatchToUInt32(i))
102 return static_cast<int>(i);
103 double d = roundValue(exec,
Value(
const_cast<ValueImp*
>(
this)));
106 return static_cast<int>(d);
109 int ValueImp::toInt32(
ExecState *exec)
const
112 if (dispatchToUInt32(i))
115 double d = roundValue(exec,
Value(
const_cast<ValueImp*
>(
this)));
116 if (isNaN(d) || isInf(d) || d == 0.0)
118 double d32 = fmod(d, D32);
125 if (d32 >= D32 / 2.0)
128 return static_cast<int>(d32);
131 unsigned int ValueImp::toUInt32(
ExecState *exec)
const
134 if (dispatchToUInt32(i))
137 double d = roundValue(exec,
Value(
const_cast<ValueImp*
>(
this)));
138 if (isNaN(d) || isInf(d) || d == 0.0)
140 double d32 = fmod(d, D32);
150 return static_cast<unsigned int>(d32);
153 unsigned short ValueImp::toUInt16(
ExecState *exec)
const
156 if (dispatchToUInt32(i))
157 return (
unsigned short)i;
159 double d = roundValue(exec,
Value(
const_cast<ValueImp*
>(
this)));
160 double d16 = fmod(d, D16);
163 int t_int =
static_cast<int>(d16);
164 return static_cast<unsigned short>(t_int);
170 Type ValueImp::dispatchType()
const
172 if (SimpleNumber::is(
this))
177 Value ValueImp::dispatchToPrimitive(
ExecState *exec, Type preferredType)
const
179 if (SimpleNumber::is(
this))
181 return toPrimitive(exec, preferredType);
184 bool ValueImp::dispatchToBoolean(
ExecState *exec)
const
186 if (SimpleNumber::is(
this))
187 return SimpleNumber::value(
this);
188 return toBoolean(exec);
191 double ValueImp::dispatchToNumber(
ExecState *exec)
const
193 if (SimpleNumber::is(
this))
194 return SimpleNumber::value(
this);
195 return toNumber(exec);
200 if (SimpleNumber::is(
this))
202 return toString(exec);
207 if (SimpleNumber::is(
this))
208 return static_cast<const NumberImp *
>(
this)->NumberImp::toObject(exec);
209 return toObject(exec);
212 bool ValueImp::dispatchToUInt32(
unsigned& result)
const
214 if (SimpleNumber::is(
this)) {
215 long i = SimpleNumber::value(
this);
218 result = (unsigned)i;
221 return toUInt32(result);
229 #ifdef DEBUG_COLLECTOR
230 assert (!(rep && !SimpleNumber::is(rep) && *((uint32_t *)rep) == 0 ));
231 assert (!(rep && !SimpleNumber::is(rep) && rep->_flags & ValueImp::VI_MARKED));
241 Value::Value(
const Value &v)
244 #ifdef DEBUG_COLLECTOR
245 assert (!(rep && !SimpleNumber::is(rep) && *((uint32_t *)rep) == 0 ));
246 assert (!(rep && !SimpleNumber::is(rep) && rep->_flags & ValueImp::VI_MARKED));
286 Undefined::Undefined() :
Value(UndefinedImp::staticUndefined)
300 Null::Null() :
Value(NullImp::staticNull)
314 Boolean::Boolean(
bool b)
315 :
Value(b ? BooleanImp::staticTrue : BooleanImp::staticFalse)
319 bool Boolean::value()
const
322 return ((BooleanImp*)rep)->value();
328 return static_cast<BooleanImp*
>(0);
330 return static_cast<BooleanImp*
>(v.imp());
335 String::String(
const UString &s) :
Value(new StringImp(s))
339 fprintf(stderr,
"WARNING: KJS::String constructed from null string\n");
346 return ((StringImp*)rep)->value();
354 return String(
static_cast<StringImp*
>(v.imp()));
359 Number::Number(
int i)
360 :
Value(SimpleNumber::fits(i) ? SimpleNumber::make(i) : new NumberImp(static_cast<double>(i))) { }
362 Number::Number(
unsigned int u)
363 :
Value(SimpleNumber::fits(u) ? SimpleNumber::make(u) : new NumberImp(static_cast<double>(u))) { }
365 Number::Number(
double d)
366 #if defined(__alpha) && !defined(_IEEE_FP)
368 :
Value(KJS::isNaN(d) ? NumberImp::staticNaN : (SimpleNumber::fits(d) ? SimpleNumber::make((
long)d) :
new NumberImp(d))) { }
370 :
Value(SimpleNumber::fits(d) ? SimpleNumber::make((
long)d) : (KJS::isNaN(d) ? NumberImp::staticNaN :
new NumberImp(d))) { }
373 Number::Number(
long int l)
374 :
Value(SimpleNumber::fits(l) ? SimpleNumber::make(l) : new NumberImp(static_cast<double>(l))) { }
376 Number::Number(
long unsigned int l)
377 :
Value(SimpleNumber::fits(l) ? SimpleNumber::make(l) : new NumberImp(static_cast<double>(l))) { }
382 return Number((NumberImp*)0);
384 return Number(
static_cast<NumberImp*
>(v.imp()));
387 double Number::value()
const
389 if (SimpleNumber::is(rep))
390 return (
double)SimpleNumber::value(rep);
392 return ((NumberImp*)rep)->value();
395 int Number::intValue()
const
397 if (SimpleNumber::is(rep))
398 return SimpleNumber::value(rep);
399 return (
int)((NumberImp*)rep)->value();
402 bool Number::isNaN()
const
404 return rep == NumberImp::staticNaN;
407 bool Number::isInf()
const
409 if (SimpleNumber::is(rep))
411 return KJS::isInf(((NumberImp*)rep)->value());
Represents an primitive Boolean value.
static Boolean dynamicCast(const Value &v)
Converts a Value into an Boolean.
static void * allocate(size_t s)
Register an object with the collector.
Represents the current state of script execution.
Represents an primitive Null value.
static Null dynamicCast(const Value &v)
Converts a Value into an Null.
Represents an primitive Number value.
static Number dynamicCast(const Value &v)
Converts a Value into an Number.
Represents an primitive String value.
static String dynamicCast(const Value &v)
Converts a Value into an String.
static UString from(int i)
Constructs a string from an int.
Represents an primitive Undefined value.
ValueImp is the base type for all primitives (Undefined, Null, Boolean, String, Number) and objects i...
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents.
bool isValid() const
Returns whether or not this is a valid value.
Type type() const
Returns the type of value.