1 | /*
|
---|
2 | * This file is part of the KDE libraries
|
---|
3 | * Copyright (C) 2003-2006 Apple Computer, Inc
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Library General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Library General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Library General Public License
|
---|
16 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
18 | * Boston, MA 02110-1301, USA.
|
---|
19 | *
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "config.h"
|
---|
23 | #include "JSImmediate.h"
|
---|
24 |
|
---|
25 | #include "object.h"
|
---|
26 |
|
---|
27 | namespace KJS {
|
---|
28 |
|
---|
29 | JSObject *JSImmediate::toObject(const JSValue *v, ExecState *exec)
|
---|
30 | {
|
---|
31 | assert(isImmediate(v));
|
---|
32 | if (v == jsNull())
|
---|
33 | return throwError(exec, TypeError, "Null value");
|
---|
34 | else if (v == jsUndefined())
|
---|
35 | return throwError(exec, TypeError, "Undefined value");
|
---|
36 | else if (isBoolean(v)) {
|
---|
37 | List args;
|
---|
38 | args.append(const_cast<JSValue *>(v));
|
---|
39 | return exec->lexicalInterpreter()->builtinBoolean()->construct(exec, args);
|
---|
40 | } else {
|
---|
41 | ASSERT(isNumber(v));
|
---|
42 | List args;
|
---|
43 | args.append(const_cast<JSValue *>(v));
|
---|
44 | return exec->lexicalInterpreter()->builtinNumber()->construct(exec, args);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | UString JSImmediate::toString(const JSValue *v)
|
---|
49 | {
|
---|
50 | ASSERT(isImmediate(v));
|
---|
51 |
|
---|
52 | if (v == jsNull())
|
---|
53 | return "null";
|
---|
54 | else if (v == jsUndefined())
|
---|
55 | return "undefined";
|
---|
56 | else if (v == jsBoolean(true))
|
---|
57 | return "true";
|
---|
58 | else if (v == jsBoolean(false))
|
---|
59 | return "false";
|
---|
60 | else {
|
---|
61 | assert(isNumber(v));
|
---|
62 | double d = toDouble(v);
|
---|
63 | if (d == 0.0) // +0.0 or -0.0
|
---|
64 | return "0";
|
---|
65 | return UString::from(d);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | JSType JSImmediate::type(const JSValue *v)
|
---|
70 | {
|
---|
71 | ASSERT(isImmediate(v));
|
---|
72 |
|
---|
73 | uintptr_t tag = getTag(v);
|
---|
74 | if (tag == UndefinedType)
|
---|
75 | return v == jsUndefined() ? UndefinedType : NullType;
|
---|
76 | return static_cast<JSType>(tag);
|
---|
77 | }
|
---|
78 |
|
---|
79 | } // namespace KJS
|
---|