]>
git.saurik.com Git - apple/javascriptcore.git/blob - kjs/internal.cpp
2 * Copyright (C) 1999-2002 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
4 * Copyright (C) 2004, 2007, 2008 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
26 #include "ExecState.h"
27 #include "array_object.h"
28 #include "bool_object.h"
29 #include "collector.h"
30 #include "date_object.h"
32 #include "error_object.h"
33 #include "function_object.h"
35 #include "math_object.h"
37 #include "number_object.h"
39 #include "object_object.h"
40 #include "operations.h"
41 #include "regexp_object.h"
42 #include "string_object.h"
45 #include <wtf/Assertions.h>
46 #include <wtf/HashMap.h>
47 #include <wtf/HashSet.h>
48 #include <wtf/Vector.h>
52 // ------------------------------ StringImp ------------------------------------
54 JSValue
* StringImp::toPrimitive(ExecState
*, JSType
) const
56 return const_cast<StringImp
*>(this);
59 bool StringImp::getPrimitiveNumber(ExecState
*, double& number
, JSValue
*& value
)
62 number
= val
.toDouble();
66 bool StringImp::toBoolean(ExecState
*) const
68 return (val
.size() > 0);
71 double StringImp::toNumber(ExecState
*) const
73 return val
.toDouble();
76 UString
StringImp::toString(ExecState
*) const
81 JSObject
* StringImp::toObject(ExecState
*exec
) const
83 return new StringInstance(exec
->lexicalGlobalObject()->stringPrototype(), const_cast<StringImp
*>(this));
86 // ------------------------------ NumberImp ------------------------------------
88 JSValue
* NumberImp::toPrimitive(ExecState
*, JSType
) const
90 return const_cast<NumberImp
*>(this);
93 bool NumberImp::getPrimitiveNumber(ExecState
*, double& number
, JSValue
*& value
)
100 bool NumberImp::toBoolean(ExecState
*) const
102 return val
< 0.0 || val
> 0.0; // false for NaN
105 double NumberImp::toNumber(ExecState
*) const
110 UString
NumberImp::toString(ExecState
*) const
112 if (val
== 0.0) // +0.0 or -0.0
114 return UString::from(val
);
117 JSObject
*NumberImp::toObject(ExecState
*exec
) const
120 args
.append(const_cast<NumberImp
*>(this));
121 return static_cast<JSObject
*>(exec
->lexicalGlobalObject()->numberConstructor()->construct(exec
,args
));
124 bool NumberImp::getUInt32(uint32_t& uint32
) const
126 uint32
= static_cast<uint32_t>(val
);
127 return uint32
== val
;
130 bool NumberImp::getTruncatedInt32(int32_t& int32
) const
132 if (!(val
>= -2147483648.0 && val
< 2147483648.0))
134 int32
= static_cast<int32_t>(val
);
138 bool NumberImp::getTruncatedUInt32(uint32_t& uint32
) const
140 if (!(val
>= 0.0 && val
< 4294967296.0))
142 uint32
= static_cast<uint32_t>(val
);
146 // --------------------------- GetterSetterImp ---------------------------------
147 void GetterSetterImp::mark()
151 if (getter
&& !getter
->marked())
153 if (setter
&& !setter
->marked())
157 JSValue
* GetterSetterImp::toPrimitive(ExecState
*, JSType
) const
163 bool GetterSetterImp::getPrimitiveNumber(ExecState
*, double& number
, JSValue
*& value
)
165 ASSERT_NOT_REACHED();
171 bool GetterSetterImp::toBoolean(ExecState
*) const
177 double GetterSetterImp::toNumber(ExecState
*) const
183 UString
GetterSetterImp::toString(ExecState
*) const
186 return UString::null();
189 JSObject
*GetterSetterImp::toObject(ExecState
*exec
) const
192 return jsNull()->toObject(exec
);
195 // ------------------------------ LabelStack -----------------------------------
197 bool LabelStack::push(const Identifier
&id
)
202 StackElem
*newtos
= new StackElem
;
209 bool LabelStack::contains(const Identifier
&id
) const
214 for (StackElem
*curr
= tos
; curr
; curr
= curr
->prev
)
221 // ------------------------------ InternalFunctionImp --------------------------
223 const ClassInfo
InternalFunctionImp::info
= { "Function", 0, 0 };
225 InternalFunctionImp::InternalFunctionImp()
229 InternalFunctionImp::InternalFunctionImp(FunctionPrototype
* funcProto
, const Identifier
& name
)
230 : JSObject(funcProto
)
235 bool InternalFunctionImp::implementsCall() const
240 bool InternalFunctionImp::implementsHasInstance() const
245 // ------------------------------ global functions -----------------------------
249 void printInfo(ExecState
*exec
, const char *s
, JSValue
*o
, int lineno
)
252 fprintf(stderr
, "KJS: %s: (null)", s
);
258 case UnspecifiedType
:
259 name
= "Unspecified";
277 name
= static_cast<JSObject
*>(v
)->className();
279 name
= "(unknown class)";
281 case GetterSetterType
:
282 name
= "GetterSetter";
285 UString vString
= v
->toString(exec
);
286 if ( vString
.size() > 50 )
287 vString
= vString
.substr( 0, 50 ) + "...";
288 // Can't use two UString::ascii() in the same fprintf call
289 CString
tempString( vString
.cstring() );
291 fprintf(stderr
, "KJS: %s: %s : %s (%p)",
292 s
, tempString
.c_str(), name
.ascii(), (void*)v
);
295 fprintf(stderr
, ", line %d\n",lineno
);
297 fprintf(stderr
, "\n");