source: webkit/trunk/JavaScriptCore/kjs/internal.cpp@ 26688

Last change on this file since 26688 was 26688, checked in by ggaren, 18 years ago

Reviewed by Darin Adler.


Global replace of assert with ASSERT.

  • Property svn:eol-style set to native
File size: 6.4 KB
Line 
1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 1999-2002 Harri Porten ([email protected])
4 * Copyright (C) 2001 Peter Kelly ([email protected])
5 * Copyright (C) 2004 Apple Computer, Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#include "config.h"
25#include "internal.h"
26
27#include "array_object.h"
28#include "bool_object.h"
29#include "collector.h"
30#include "context.h"
31#include "date_object.h"
32#include "debugger.h"
33#include "error_object.h"
34#include "function_object.h"
35#include "lexer.h"
36#include "math_object.h"
37#include "nodes.h"
38#include "number_object.h"
39#include "object.h"
40#include "object_object.h"
41#include "operations.h"
42#include "regexp_object.h"
43#include "string_object.h"
44#include <math.h>
45#include <stdio.h>
46#include <wtf/Assertions.h>
47#include <wtf/HashMap.h>
48#include <wtf/HashSet.h>
49#include <wtf/Vector.h>
50
51namespace KJS {
52
53#if PLATFORM(WIN_OS)
54#define copysign _copysign
55#endif
56
57// ------------------------------ StringImp ------------------------------------
58
59JSValue *StringImp::toPrimitive(ExecState *, JSType) const
60{
61 return const_cast<StringImp *>(this);
62}
63
64bool StringImp::toBoolean(ExecState *) const
65{
66 return (val.size() > 0);
67}
68
69double StringImp::toNumber(ExecState *) const
70{
71 return val.toDouble();
72}
73
74UString StringImp::toString(ExecState *) const
75{
76 return val;
77}
78
79JSObject* StringImp::toObject(ExecState *exec) const
80{
81 return new StringInstance(exec->lexicalInterpreter()->builtinStringPrototype(), const_cast<StringImp*>(this));
82}
83
84// ------------------------------ NumberImp ------------------------------------
85
86JSValue *NumberImp::toPrimitive(ExecState *, JSType) const
87{
88 return const_cast<NumberImp *>(this);
89}
90
91bool NumberImp::toBoolean(ExecState *) const
92{
93 return val < 0.0 || val > 0.0; // false for NaN
94}
95
96double NumberImp::toNumber(ExecState *) const
97{
98 return val;
99}
100
101UString NumberImp::toString(ExecState *) const
102{
103 if (val == 0.0) // +0.0 or -0.0
104 return "0";
105 return UString::from(val);
106}
107
108JSObject *NumberImp::toObject(ExecState *exec) const
109{
110 List args;
111 args.append(const_cast<NumberImp*>(this));
112 return static_cast<JSObject *>(exec->lexicalInterpreter()->builtinNumber()->construct(exec,args));
113}
114
115// FIXME: We can optimize this to work like JSValue::getUInt32. I'm ignoring it for now
116// because it never shows up on profiles.
117bool NumberImp::getUInt32(uint32_t& uint32) const
118{
119 uint32 = (uint32_t)val;
120 return (double)uint32 == val;
121}
122
123// --------------------------- GetterSetterImp ---------------------------------
124void GetterSetterImp::mark()
125{
126 JSCell::mark();
127
128 if (getter && !getter->marked())
129 getter->mark();
130 if (setter && !setter->marked())
131 setter->mark();
132}
133
134JSValue *GetterSetterImp::toPrimitive(ExecState*, JSType) const
135{
136 ASSERT(false);
137 return jsNull();
138}
139
140bool GetterSetterImp::toBoolean(ExecState*) const
141{
142 ASSERT(false);
143 return false;
144}
145
146double GetterSetterImp::toNumber(ExecState *) const
147{
148 ASSERT(false);
149 return 0.0;
150}
151
152UString GetterSetterImp::toString(ExecState *) const
153{
154 ASSERT(false);
155 return UString::null();
156}
157
158JSObject *GetterSetterImp::toObject(ExecState *exec) const
159{
160 ASSERT(false);
161 return jsNull()->toObject(exec);
162}
163
164// ------------------------------ LabelStack -----------------------------------
165
166bool LabelStack::push(const Identifier &id)
167{
168 if (contains(id))
169 return false;
170
171 StackElem *newtos = new StackElem;
172 newtos->id = id;
173 newtos->prev = tos;
174 tos = newtos;
175 return true;
176}
177
178bool LabelStack::contains(const Identifier &id) const
179{
180 if (id.isEmpty())
181 return true;
182
183 for (StackElem *curr = tos; curr; curr = curr->prev)
184 if (curr->id == id)
185 return true;
186
187 return false;
188}
189
190// ------------------------------ InternalFunctionImp --------------------------
191
192const ClassInfo InternalFunctionImp::info = {"Function", 0, 0, 0};
193
194InternalFunctionImp::InternalFunctionImp()
195{
196}
197
198InternalFunctionImp::InternalFunctionImp(FunctionPrototype* funcProto)
199 : JSObject(funcProto)
200{
201}
202
203InternalFunctionImp::InternalFunctionImp(FunctionPrototype* funcProto, const Identifier& name)
204 : JSObject(funcProto)
205 , m_name(name)
206{
207}
208
209bool InternalFunctionImp::implementsCall() const
210{
211 return true;
212}
213
214bool InternalFunctionImp::implementsHasInstance() const
215{
216 return true;
217}
218
219// ------------------------------ global functions -----------------------------
220
221double roundValue(ExecState *exec, JSValue *v)
222{
223 double d = v->toNumber(exec);
224 double ad = fabs(d);
225 if (ad == 0 || isNaN(d) || isInf(d))
226 return d;
227 return copysign(floor(ad), d);
228}
229
230#ifndef NDEBUG
231#include <stdio.h>
232void printInfo(ExecState *exec, const char *s, JSValue *o, int lineno)
233{
234 if (!o)
235 fprintf(stderr, "KJS: %s: (null)", s);
236 else {
237 JSValue *v = o;
238
239 UString name;
240 switch (v->type()) {
241 case UnspecifiedType:
242 name = "Unspecified";
243 break;
244 case UndefinedType:
245 name = "Undefined";
246 break;
247 case NullType:
248 name = "Null";
249 break;
250 case BooleanType:
251 name = "Boolean";
252 break;
253 case StringType:
254 name = "String";
255 break;
256 case NumberType:
257 name = "Number";
258 break;
259 case ObjectType:
260 name = static_cast<JSObject *>(v)->className();
261 if (name.isNull())
262 name = "(unknown class)";
263 break;
264 case GetterSetterType:
265 name = "GetterSetter";
266 break;
267 }
268 UString vString = v->toString(exec);
269 if ( vString.size() > 50 )
270 vString = vString.substr( 0, 50 ) + "...";
271 // Can't use two UString::ascii() in the same fprintf call
272 CString tempString( vString.cstring() );
273
274 fprintf(stderr, "KJS: %s: %s : %s (%p)",
275 s, tempString.c_str(), name.ascii(), (void*)v);
276
277 if (lineno >= 0)
278 fprintf(stderr, ", line %d\n",lineno);
279 else
280 fprintf(stderr, "\n");
281 }
282}
283#endif
284
285}
Note: See TracBrowser for help on using the repository browser.