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