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 "JSImmediate.h" |
23 | #include "object.h" |
24 | |
25 | namespace KJS { |
26 | |
27 | JSObject *JSImmediate::toObject(const JSValue *v, ExecState *exec) |
28 | { |
29 | assert(isImmediate(v)); |
30 | if (v == jsNull()) |
31 | return throwError(exec, TypeError, "Null value" ); |
32 | else if (v == jsUndefined()) |
33 | return throwError(exec, TypeError, "Undefined value" ); |
34 | else if (isBoolean(v)) { |
35 | List args; |
36 | args.append(const_cast<JSValue *>(v)); |
37 | return exec->lexicalInterpreter()->builtinBoolean()->construct(exec, args); |
38 | } else { |
39 | ASSERT(isNumber(v)); |
40 | List args; |
41 | args.append(const_cast<JSValue *>(v)); |
42 | return exec->lexicalInterpreter()->builtinNumber()->construct(exec, args); |
43 | } |
44 | } |
45 | |
46 | UString JSImmediate::toString(const JSValue *v) |
47 | { |
48 | ASSERT(isImmediate(v)); |
49 | |
50 | if (v == jsNull()) |
51 | return "null" ; |
52 | else if (v == jsUndefined()) |
53 | return "undefined" ; |
54 | else if (v == jsBoolean(true)) |
55 | return "true" ; |
56 | else if (v == jsBoolean(false)) |
57 | return "false" ; |
58 | else { |
59 | assert(isNumber(v)); |
60 | double d = toDouble(v); |
61 | if (d == 0.0) // +0.0 or -0.0 |
62 | return "0" ; |
63 | return UString::from(d); |
64 | } |
65 | } |
66 | |
67 | JSType JSImmediate::type(const JSValue *v) |
68 | { |
69 | ASSERT(isImmediate(v)); |
70 | |
71 | uintptr_t tag = getTag(v); |
72 | if (tag == UndefinedType) |
73 | return v == jsUndefined() ? UndefinedType : NullType; |
74 | return static_cast<JSType>(tag); |
75 | } |
76 | |
77 | } // namespace KJS |
78 | |