27 #include "interpreter.h"
36 #include "collector.h"
37 #include "operations.h"
38 #include "error_object.h"
40 #include "simple_number.h"
46 ValueImp::ValueImp() :
57 _flags |= VI_DESTRUCTED;
66 bool ValueImp::marked()
const
69 return SimpleNumber::is(
this) || (_flags & VI_MARKED);
72 void ValueImp::setGcAllowed()
77 if (!SimpleNumber::is(
this))
78 _flags |= VI_GCALLOWED;
81 void* ValueImp::operator
new(
size_t s)
86 void ValueImp::operator
delete(
void*)
91 bool ValueImp::toUInt32(
unsigned&)
const
97 int ValueImp::toInteger(
ExecState *exec)
const
100 if (dispatchToUInt32(i))
101 return static_cast<int>(i);
102 double d = roundValue(exec,
Value(
const_cast<ValueImp*
>(
this)));
105 return static_cast<int>(d);
108 int ValueImp::toInt32(
ExecState *exec)
const
111 if (dispatchToUInt32(i))
114 double d = roundValue(exec,
Value(
const_cast<ValueImp*
>(
this)));
115 if (isNaN(d) || isInf(d) || d == 0.0)
117 double d32 = fmod(d, D32);
124 if (d32 >= D32 / 2.0)
127 return static_cast<int>(d32);
130 unsigned int ValueImp::toUInt32(
ExecState *exec)
const
133 if (dispatchToUInt32(i))
136 double d = roundValue(exec,
Value(
const_cast<ValueImp*
>(
this)));
137 if (isNaN(d) || isInf(d) || d == 0.0)
139 double d32 = fmod(d, D32);
149 return static_cast<unsigned int>(d32);
152 unsigned short ValueImp::toUInt16(
ExecState *exec)
const
155 if (dispatchToUInt32(i))
156 return (
unsigned short)i;
158 double d = roundValue(exec,
Value(
const_cast<ValueImp*
>(
this)));
159 double d16 = fmod(d, D16);
162 int t_int =
static_cast<int>(d16);
163 return static_cast<unsigned short>(t_int);
169 Type ValueImp::dispatchType()
const
171 if (SimpleNumber::is(
this))
176 Value ValueImp::dispatchToPrimitive(
ExecState *exec, Type preferredType)
const
178 if (SimpleNumber::is(
this))
180 return toPrimitive(exec, preferredType);
183 bool ValueImp::dispatchToBoolean(
ExecState *exec)
const
185 if (SimpleNumber::is(
this))
186 return SimpleNumber::value(
this);
187 return toBoolean(exec);
190 double ValueImp::dispatchToNumber(
ExecState *exec)
const
192 if (SimpleNumber::is(
this))
193 return SimpleNumber::value(
this);
194 return toNumber(exec);
199 if (SimpleNumber::is(
this))
201 return toString(exec);
206 if (SimpleNumber::is(
this))
207 return static_cast<const NumberImp *
>(
this)->NumberImp::toObject(exec);
208 return toObject(exec);
211 bool ValueImp::dispatchToUInt32(
unsigned& result)
const
213 if (SimpleNumber::is(
this)) {
214 long i = SimpleNumber::value(
this);
217 result = (unsigned)i;
220 return toUInt32(result);
228 #ifdef DEBUG_COLLECTOR
229 assert (!(rep && !SimpleNumber::is(rep) && *((uint32_t *)rep) == 0 ));
230 assert (!(rep && !SimpleNumber::is(rep) && rep->_flags & ValueImp::VI_MARKED));
240 Value::Value(
const Value &v)
243 #ifdef DEBUG_COLLECTOR
244 assert (!(rep && !SimpleNumber::is(rep) && *((uint32_t *)rep) == 0 ));
245 assert (!(rep && !SimpleNumber::is(rep) && rep->_flags & ValueImp::VI_MARKED));
285 Undefined::Undefined() :
Value(UndefinedImp::staticUndefined)
299 Null::Null() :
Value(NullImp::staticNull)
313 Boolean::Boolean(
bool b)
314 :
Value(b ? BooleanImp::staticTrue : BooleanImp::staticFalse)
318 bool Boolean::value()
const
321 return ((BooleanImp*)rep)->value();
327 return static_cast<BooleanImp*
>(0);
329 return static_cast<BooleanImp*
>(v.imp());
334 String::String(
const UString &s) :
Value(new StringImp(s))
338 fprintf(stderr,
"WARNING: KJS::String constructed from null string\n");
345 return ((StringImp*)rep)->value();
353 return String(
static_cast<StringImp*
>(v.imp()));
358 Number::Number(
int i)
359 :
Value(SimpleNumber::fits(i) ? SimpleNumber::make(i) : new NumberImp(static_cast<double>(i))) { }
361 Number::Number(
unsigned int u)
362 :
Value(SimpleNumber::fits(u) ? SimpleNumber::make(u) : new NumberImp(static_cast<double>(u))) { }
365 #if defined(__alpha) && !defined(_IEEE_FP)
366 Number::Number(
double d)
367 :
Value(KJS::isNaN(d) ? NumberImp::staticNaN : (SimpleNumber::fits(d) ? SimpleNumber::make((long)d) : new NumberImp(d))) { }
369 Number::Number(
double 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.