File: value.h

package info (click to toggle)
webkit 1.0.1-4%2Blenny2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 68,876 kB
  • ctags: 66,869
  • sloc: cpp: 369,003; ansic: 20,095; perl: 13,548; objc: 13,474; yacc: 2,562; python: 1,092; sh: 611; ruby: 405; makefile: 140; xml: 10
file content (616 lines) | stat: -rw-r--r-- 17,083 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
 *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
 *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
 *  Copyright (C) 2003, 2004, 2005, 2007, 2008 Apple Inc. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

#ifndef KJS_VALUE_H
#define KJS_VALUE_H

#include "CallData.h"
#include "ConstructData.h"
#include "JSImmediate.h"
#include "collector.h"
#include "ustring.h"
#include <stddef.h> // for size_t

namespace KJS {

class ExecState;
class Identifier;
class JSObject;
class JSCell;
class PropertySlot;

struct ClassInfo;
struct Instruction;

/**
 * JSValue is the base type for all primitives (Undefined, Null, Boolean,
 * String, Number) and objects in ECMAScript.
 *
 * Note: you should never inherit from JSValue as it is for primitive types
 * only (all of which are provided internally by KJS). Instead, inherit from
 * JSObject.
 */
class JSValue : Noncopyable {
    friend class JSCell; // so it can derive from this class
    friend class Collector; // so it can call asCell()
private:
    JSValue();
    virtual ~JSValue();

public:
    // Querying the type.
    JSType type() const;
    bool isUndefined() const;
    bool isNull() const;
    bool isUndefinedOrNull() const;
    bool isBoolean() const;
    bool isNumber() const;
    bool isString() const;
    bool isObject() const;
    bool isObject(const ClassInfo*) const;

    // Extracting the value.
    bool getBoolean(bool&) const;
    bool getBoolean() const; // false if not a boolean
    bool getNumber(double&) const;
    double getNumber() const; // NaN if not a number
    double uncheckedGetNumber() const;
    bool getString(UString&) const;
    UString getString() const; // null string if not a string
    JSObject* getObject(); // NULL if not an object
    const JSObject* getObject() const; // NULL if not an object

    CallType getCallData(CallData&);
    ConstructType getConstructData(ConstructData&);

    // Extracting integer values.
    bool getUInt32(uint32_t&) const;
    bool getTruncatedInt32(int32_t&) const;
    bool getTruncatedUInt32(uint32_t&) const;
    
    // Basic conversions.
    JSValue* toPrimitive(ExecState*, JSType preferredType = UnspecifiedType) const;
    bool getPrimitiveNumber(ExecState*, double& number, JSValue*&);

    bool toBoolean(ExecState*) const;

    // toNumber conversion is expected to be side effect free if an exception has
    // been set in the ExecState already.
    double toNumber(ExecState*) const;
    JSValue* toJSNumber(ExecState*) const; // Fast path for when you expect that the value is an immediate number.
    UString toString(ExecState*) const;
    JSObject* toObject(ExecState*) const;

    // Integer conversions.
    double toInteger(ExecState*) const;
    double toIntegerPreserveNaN(ExecState*) const;
    int32_t toInt32(ExecState*) const;
    int32_t toInt32(ExecState*, bool& ok) const;
    uint32_t toUInt32(ExecState*) const;
    uint32_t toUInt32(ExecState*, bool& ok) const;

    // These are identical logic to above, and faster than jsNumber(number)->toInt32(exec)
    static int32_t toInt32(double);
    static int32_t toUInt32(double);

    // Floating point conversions.
    float toFloat(ExecState*) const;

    // Garbage collection.
    void mark();
    bool marked() const;

    static int32_t toInt32SlowCase(double, bool& ok);
    static uint32_t toUInt32SlowCase(double, bool& ok);

    // Object operations, with the toObject operation included.
    JSValue* get(ExecState*, const Identifier& propertyName) const;
    JSValue* get(ExecState*, unsigned propertyName) const;
    void put(ExecState*, const Identifier& propertyName, JSValue*);
    void put(ExecState*, unsigned propertyName, JSValue*);
    JSObject* toThisObject(ExecState*) const;

private:
    bool getPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
    bool getPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
    int32_t toInt32SlowCase(ExecState*, bool& ok) const;
    uint32_t toUInt32SlowCase(ExecState*, bool& ok) const;

    // Implementation details.
    JSCell* asCell();
    const JSCell* asCell() const;
};

class JSCell : public JSValue {
    friend class Collector;
    friend class GetterSetterImp;
    friend class JSObject;
    friend class JSPropertyNameIterator;
    friend class JSValue;
    friend class NumberImp;
    friend class StringImp;
private:
    JSCell();
    virtual ~JSCell();

public:
    // Querying the type.
    virtual JSType type() const = 0;
    bool isNumber() const;
    bool isString() const;
    bool isObject() const;
    bool isObject(const ClassInfo*) const;

    // Extracting the value.
    bool getNumber(double&) const;
    double getNumber() const; // NaN if not a number
    bool getString(UString&) const;
    UString getString() const; // null string if not a string
    JSObject* getObject(); // NULL if not an object
    const JSObject* getObject() const; // NULL if not an object
    
    virtual CallType getCallData(CallData&);
    virtual ConstructType getConstructData(ConstructData&);

    // Extracting integer values.
    virtual bool getUInt32(uint32_t&) const;
    virtual bool getTruncatedInt32(int32_t&) const;
    virtual bool getTruncatedUInt32(uint32_t&) const;

    // Basic conversions.
    virtual JSValue* toPrimitive(ExecState*, JSType preferredType = UnspecifiedType) const = 0;
    virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue*&) = 0;
    virtual bool toBoolean(ExecState*) const = 0;
    virtual double toNumber(ExecState*) const = 0;
    virtual UString toString(ExecState*) const = 0;
    virtual JSObject* toObject(ExecState*) const = 0;

    // Garbage collection.
    void* operator new(size_t);
    virtual void mark();
    bool marked() const;

    // Object operations, with the toObject operation included.
    virtual void put(ExecState*, const Identifier& propertyName, JSValue*);
    virtual void put(ExecState*, unsigned propertyName, JSValue*);
    virtual JSObject* toThisObject(ExecState*) const;

private:
    // Base implementation, but for non-object classes implements getPropertySlot.
    virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
    virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
};

class NumberImp : public JSCell {
    friend JSValue* jsNumberCell(double);
public:
    double value() const { return val; }

    virtual JSType type() const;

    virtual JSValue* toPrimitive(ExecState*, JSType preferred = UnspecifiedType) const;
    virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue*& value);
    virtual bool toBoolean(ExecState*) const;
    virtual double toNumber(ExecState*) const;
    virtual UString toString(ExecState*) const;
    virtual JSObject* toObject(ExecState*) const;
    virtual JSObject* toThisObject(ExecState*) const;

    void* operator new(size_t size)
    {
#ifdef JAVASCRIPTCORE_BUILDING_ALL_IN_ONE_FILE
        return Collector::inlineAllocateNumber(size);
#else
        return Collector::allocateNumber(size);
#endif
    }

private:
    NumberImp(double v)
        : val(v)
    {
    }

    virtual bool getUInt32(uint32_t&) const;
    virtual bool getTruncatedInt32(int32_t&) const;
    virtual bool getTruncatedUInt32(uint32_t&) const;

    double val;
};

JSCell* jsString(const UString&); // returns empty string if passed null string
JSCell* jsString(const char* = ""); // returns empty string if passed 0

// should be used for strings that are owned by an object that will
// likely outlive the JSValue this makes, such as the parse tree or a
// DOM object that contains a UString
JSCell* jsOwnedString(const UString&); 

extern const double NaN;
extern const double Inf;

// Beware marking this function ALWAYS_INLINE: It takes a PIC branch, so
// inlining it may not always be a win.
inline JSValue* jsNumberCell(double d)
{
    return new NumberImp(d);
}

ALWAYS_INLINE JSValue* jsUndefined()
{
    return JSImmediate::undefinedImmediate();
}

inline JSValue* jsNull()
{
    return JSImmediate::nullImmediate();
}

inline JSValue* jsNaN()
{
    return jsNumberCell(NaN);
}

inline JSValue* jsBoolean(bool b)
{
    return b ? JSImmediate::trueImmediate() : JSImmediate::falseImmediate();
}

ALWAYS_INLINE JSValue* jsNumber(double d)
{
    JSValue* v = JSImmediate::from(d);
    return v ? v : jsNumberCell(d);
}

ALWAYS_INLINE JSValue* jsNumber(int i)
{
    JSValue* v = JSImmediate::from(i);
    return v ? v : jsNumberCell(i);
}

ALWAYS_INLINE JSValue* jsNumber(unsigned i)
{
    JSValue* v = JSImmediate::from(i);
    return v ? v : jsNumberCell(i);
}

ALWAYS_INLINE JSValue* jsNumber(long i)
{
    JSValue* v = JSImmediate::from(i);
    return v ? v : jsNumberCell(i);
}

ALWAYS_INLINE JSValue* jsNumber(unsigned long i)
{
    JSValue* v = JSImmediate::from(i);
    return v ? v : jsNumberCell(i);
}

ALWAYS_INLINE JSValue* jsNumber(long long i)
{
    JSValue* v = JSImmediate::from(i);
    return v ? v : jsNumberCell(static_cast<double>(i));
}

ALWAYS_INLINE JSValue* jsNumber(unsigned long long i)
{
    JSValue* v = JSImmediate::from(i);
    return v ? v : jsNumberCell(static_cast<double>(i));
}

ALWAYS_INLINE JSValue* jsNumberFromAnd(ExecState *exec, JSValue* v1, JSValue* v2)
{
    if (JSImmediate::areBothImmediateNumbers(v1, v2))
        return JSImmediate::andImmediateNumbers(v1, v2);
    return jsNumber(v1->toInt32(exec) & v2->toInt32(exec));
}

inline JSValue::JSValue()
{
}

inline JSValue::~JSValue()
{
}

inline JSCell::JSCell()
{
}

inline JSCell::~JSCell()
{
}

inline bool JSCell::isNumber() const
{
    return type() == NumberType;
}

inline bool JSCell::isString() const
{
    return type() == StringType;
}

inline bool JSCell::isObject() const
{
    return type() == ObjectType;
}

inline bool JSCell::marked() const
{
    return Collector::isCellMarked(this);
}

inline void JSCell::mark()
{
    return Collector::markCell(this);
}

ALWAYS_INLINE JSCell* JSValue::asCell()
{
    ASSERT(!JSImmediate::isImmediate(this));
    return static_cast<JSCell*>(this);
}

ALWAYS_INLINE const JSCell* JSValue::asCell() const
{
    ASSERT(!JSImmediate::isImmediate(this));
    return static_cast<const JSCell*>(this);
}

inline bool JSValue::isUndefined() const
{
    return this == jsUndefined();
}

inline bool JSValue::isNull() const
{
    return this == jsNull();
}

inline bool JSValue::isUndefinedOrNull() const
{
    return JSImmediate::isUndefinedOrNull(this);
}

inline bool JSValue::isBoolean() const
{
    return JSImmediate::isBoolean(this);
}

inline bool JSValue::isNumber() const
{
    return JSImmediate::isNumber(this) || (!JSImmediate::isImmediate(this) && asCell()->isNumber());
}

inline bool JSValue::isString() const
{
    return !JSImmediate::isImmediate(this) && asCell()->isString();
}

inline bool JSValue::isObject() const
{
    return !JSImmediate::isImmediate(this) && asCell()->isObject();
}

inline bool JSValue::getBoolean(bool& v) const
{
    if (JSImmediate::isBoolean(this)) {
        v = JSImmediate::toBoolean(this);
        return true;
    }
    
    return false;
}

inline bool JSValue::getBoolean() const
{
    return JSImmediate::isBoolean(this) ? JSImmediate::toBoolean(this) : false;
}

inline bool JSValue::getNumber(double& v) const
{
    if (JSImmediate::isImmediate(this)) {
        v = JSImmediate::toDouble(this);
        return true;
    }
    return asCell()->getNumber(v);
}

inline double JSValue::getNumber() const
{
    return JSImmediate::isImmediate(this) ? JSImmediate::toDouble(this) : asCell()->getNumber();
}

inline double JSValue::uncheckedGetNumber() const
{
    ASSERT(JSImmediate::isImmediate(this) || asCell()->isNumber());
    return JSImmediate::isImmediate(this) ? JSImmediate::toDouble(this) : static_cast<const NumberImp*>(this)->value();
}

inline bool JSValue::getString(UString& s) const
{
    return !JSImmediate::isImmediate(this) && asCell()->getString(s);
}

inline UString JSValue::getString() const
{
    return JSImmediate::isImmediate(this) ? UString() : asCell()->getString();
}

inline JSObject *JSValue::getObject()
{
    return JSImmediate::isImmediate(this) ? 0 : asCell()->getObject();
}

inline const JSObject *JSValue::getObject() const
{
    return JSImmediate::isImmediate(this) ? 0 : asCell()->getObject();
}

inline CallType JSValue::getCallData(CallData& callData)
{
    return JSImmediate::isImmediate(this) ? CallTypeNone : asCell()->getCallData(callData);
}

inline ConstructType JSValue::getConstructData(ConstructData& constructData)
{
    return JSImmediate::isImmediate(this) ? ConstructTypeNone : asCell()->getConstructData(constructData);
}

ALWAYS_INLINE bool JSValue::getUInt32(uint32_t& v) const
{
    return JSImmediate::isImmediate(this) ? JSImmediate::getUInt32(this, v) : asCell()->getUInt32(v);
}

ALWAYS_INLINE bool JSValue::getTruncatedInt32(int32_t& v) const
{
    return JSImmediate::isImmediate(this) ? JSImmediate::getTruncatedInt32(this, v) : asCell()->getTruncatedInt32(v);
}

inline bool JSValue::getTruncatedUInt32(uint32_t& v) const
{
    return JSImmediate::isImmediate(this) ? JSImmediate::getTruncatedUInt32(this, v) : asCell()->getTruncatedUInt32(v);
}

inline void JSValue::mark()
{
    ASSERT(!JSImmediate::isImmediate(this)); // callers should check !marked() before calling mark()
    asCell()->mark();
}

inline bool JSValue::marked() const
{
    return JSImmediate::isImmediate(this) || asCell()->marked();
}

inline JSType JSValue::type() const
{
    return JSImmediate::isImmediate(this) ? JSImmediate::type(this) : asCell()->type();
}

inline JSValue* JSValue::toPrimitive(ExecState* exec, JSType preferredType) const
{
    return JSImmediate::isImmediate(this) ? const_cast<JSValue*>(this) : asCell()->toPrimitive(exec, preferredType);
}

inline bool JSValue::getPrimitiveNumber(ExecState* exec, double& number, JSValue*& value)
{
    if (JSImmediate::isImmediate(this)) {
        number = JSImmediate::toDouble(this);
        value = this;
        return true;
    }
    return asCell()->getPrimitiveNumber(exec, number, value);
}

inline bool JSValue::toBoolean(ExecState *exec) const
{
    return JSImmediate::isImmediate(this) ? JSImmediate::toBoolean(this) : asCell()->toBoolean(exec);
}

ALWAYS_INLINE double JSValue::toNumber(ExecState *exec) const
{
    return JSImmediate::isImmediate(this) ? JSImmediate::toDouble(this) : asCell()->toNumber(exec);
}

ALWAYS_INLINE JSValue* JSValue::toJSNumber(ExecState* exec) const
{
    return JSImmediate::isNumber(this) ? const_cast<JSValue*>(this) : jsNumber(this->toNumber(exec));
}

inline UString JSValue::toString(ExecState *exec) const
{
    return JSImmediate::isImmediate(this) ? JSImmediate::toString(this) : asCell()->toString(exec);
}

inline JSObject* JSValue::toObject(ExecState* exec) const
{
    return JSImmediate::isImmediate(this) ? JSImmediate::toObject(this, exec) : asCell()->toObject(exec);
}

ALWAYS_INLINE int32_t JSValue::toInt32(ExecState* exec) const
{
    int32_t i;
    if (getTruncatedInt32(i))
        return i;
    bool ok;
    return toInt32SlowCase(exec, ok);
}

inline uint32_t JSValue::toUInt32(ExecState* exec) const
{
    uint32_t i;
    if (getTruncatedUInt32(i))
        return i;
    bool ok;
    return toUInt32SlowCase(exec, ok);
}

inline int32_t JSValue::toInt32(double val)
{
    if (!(val >= -2147483648.0 && val < 2147483648.0)) {
        bool ignored;
        return toInt32SlowCase(val, ignored);
    }
    return static_cast<int32_t>(val);
}

inline int32_t JSValue::toUInt32(double val)
{
    if (!(val >= 0.0 && val < 4294967296.0)) {
        bool ignored;
        return toUInt32SlowCase(val, ignored);
    }
    return static_cast<uint32_t>(val);
}

inline int32_t JSValue::toInt32(ExecState* exec, bool& ok) const
{
    int32_t i;
    if (getTruncatedInt32(i)) {
        ok = true;
        return i;
    }
    return toInt32SlowCase(exec, ok);
}

inline uint32_t JSValue::toUInt32(ExecState* exec, bool& ok) const
{
    uint32_t i;
    if (getTruncatedUInt32(i)) {
        ok = true;
        return i;
    }
    return toUInt32SlowCase(exec, ok);
}

inline JSObject* JSValue::toThisObject(ExecState* exec) const
{
    if (UNLIKELY(JSImmediate::isImmediate(this)))
        return JSImmediate::toObject(this, exec);
    return asCell()->toThisObject(exec);
}

} // namespace KJS

#endif // KJS_VALUE_H