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

Last change on this file since 15118 was 14834, checked in by ggaren, 19 years ago

JavaScriptCore:

Reviewed by TimO, Maciej.


  • Merged InterpreterImp code into Interpreter, which implements all interpreter functionality now. This is part of my continuing quest to create an external notion of JS "execution context" that is unified and simple -- something to replace the mix of Context, ContextImp, ExecState, Interpreter, InterpreterImp, and JSRun.


All tests pass. Leaks test has not regressed from its baseline ~207 leaks
with ~3460 leaked nodes.

  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj:
  • JavaScriptCore.xcodeproj/project.pbxproj:
  • bindings/NP_jsobject.cpp:
  • bindings/objc/WebScriptObject.mm: (+[WebScriptObject throwException:]):
  • bindings/runtime_root.cpp:
  • bindings/runtime_root.h:
  • kjs/Context.cpp: (KJS::Context::Context):
  • kjs/ExecState.cpp: Added. (KJS::ExecState::lexicalInterpreter):
  • kjs/ExecState.h: Added. (KJS::ExecState::dynamicInterpreter):
  • kjs/SavedBuiltins.h: Added.
  • kjs/bool_object.cpp: (BooleanPrototype::BooleanPrototype):
  • kjs/collector.cpp: (KJS::Collector::collect): (KJS::Collector::numInterpreters):
  • kjs/context.h:
  • kjs/debugger.cpp: (Debugger::attach): (Debugger::detach):
  • kjs/function.cpp: (KJS::FunctionImp::callAsFunction): (KJS::GlobalFuncImp::callAsFunction):
  • kjs/function_object.cpp: (FunctionObjectImp::construct):
  • kjs/internal.cpp:
  • kjs/internal.h:
  • kjs/interpreter.cpp: (KJS::interpreterMap): (KJS::Interpreter::Interpreter): (KJS::Interpreter::init): (KJS::Interpreter::~Interpreter): (KJS::Interpreter::globalObject): (KJS::Interpreter::initGlobalObject): (KJS::Interpreter::globalExec): (KJS::Interpreter::checkSyntax): (KJS::Interpreter::evaluate): (KJS::Interpreter::builtinObject): (KJS::Interpreter::builtinFunction): (KJS::Interpreter::builtinArray): (KJS::Interpreter::builtinBoolean): (KJS::Interpreter::builtinString): (KJS::Interpreter::builtinNumber): (KJS::Interpreter::builtinDate): (KJS::Interpreter::builtinRegExp): (KJS::Interpreter::builtinError): (KJS::Interpreter::builtinObjectPrototype): (KJS::Interpreter::builtinFunctionPrototype): (KJS::Interpreter::builtinArrayPrototype): (KJS::Interpreter::builtinBooleanPrototype): (KJS::Interpreter::builtinStringPrototype): (KJS::Interpreter::builtinNumberPrototype): (KJS::Interpreter::builtinDatePrototype): (KJS::Interpreter::builtinRegExpPrototype): (KJS::Interpreter::builtinErrorPrototype): (KJS::Interpreter::builtinEvalError): (KJS::Interpreter::builtinRangeError): (KJS::Interpreter::builtinReferenceError): (KJS::Interpreter::builtinSyntaxError): (KJS::Interpreter::builtinTypeError): (KJS::Interpreter::builtinURIError): (KJS::Interpreter::builtinEvalErrorPrototype): (KJS::Interpreter::builtinRangeErrorPrototype): (KJS::Interpreter::builtinReferenceErrorPrototype): (KJS::Interpreter::builtinSyntaxErrorPrototype): (KJS::Interpreter::builtinTypeErrorPrototype): (KJS::Interpreter::builtinURIErrorPrototype): (KJS::Interpreter::mark): (KJS::Interpreter::interpreterWithGlobalObject): (KJS::Interpreter::saveBuiltins): (KJS::Interpreter::restoreBuiltins):
  • kjs/interpreter.h: (KJS::Interpreter::setCompatMode): (KJS::Interpreter::compatMode): (KJS::Interpreter::firstInterpreter): (KJS::Interpreter::nextInterpreter): (KJS::Interpreter::prevInterpreter): (KJS::Interpreter::debugger): (KJS::Interpreter::setDebugger): (KJS::Interpreter::setContext): (KJS::Interpreter::context):
  • kjs/nodes.cpp: (StatementNode::hitStatement): (RegExpNode::evaluate):
  • kjs/protect.h:

JavaScriptGlue:

Reviewed by TimO, Maciej.


  • JSGlue part of merging InterpreterImp into Interpreter.


Since there's now a kjs forwarding header, I moved all forwarding
headers into a "ForwardingHeaders" directory, like in WebCore.

  • ForwardingHeaders: Added.
  • ForwardingHeaders/kjs: Added.
  • ForwardingHeaders/kjs/ExecState.h: Added.
  • ForwardingHeaders/wtf: Added.
  • JavaScriptGlue.xcodeproj/project.pbxproj:
  • config.h:
  • wtf: Removed.
  • wtf/AlwaysInline.h: Removed.
  • wtf/Assertions.h: Removed.
  • wtf/FastMalloc.h: Removed.
  • wtf/Forward.h: Removed.
  • wtf/HashCountedSet.h: Removed.
  • wtf/HashSet.h: Removed.
  • wtf/Noncopyable.h: Removed.
  • wtf/OwnArrayPtr.h: Removed.
  • wtf/OwnPtr.h: Removed.
  • wtf/PassRefPtr.h: Removed.
  • wtf/Platform.h: Removed.
  • wtf/RefPtr.h: Removed.
  • wtf/Vector.h: Removed.

WebCore:

Reviewed by TimO, Maciej.

  • WebCore part of merging InterpreterImp into Interpreter. No test because there's no behavior change.


A substantive change here is that ScriptInterpreter::mark must now chain to
Interpreter::mark, since Interpreter needs to mark the things that
InterpreterImp used to mark.


  • WebCore.xcodeproj/project.pbxproj:
  • bindings/js/kjs_binding.cpp:
  • bindings/js/kjs_window.cpp: (KJS::Window::put):
  • bridge/mac/WebCoreFrameBridge.mm:
  • bridge/mac/WebCoreScriptDebugger.mm: (-[WebCoreScriptCallFrame evaluateWebScript:]):
  • kwq/KWQPageState.mm:
  • Property svn:eol-style set to native
File size: 6.9 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 <assert.h>
45#include <wtf/HashMap.h>
46#include <wtf/HashSet.h>
47#include <wtf/Vector.h>
48#include <math.h>
49#include <stdio.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(), val);
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
219bool InternalFunctionImp::hasInstance(ExecState *exec, JSValue *value)
220{
221 if (!value->isObject())
222 return false;
223
224 JSValue *prot = get(exec,prototypePropertyName);
225 if (!prot->isObject() && !prot->isNull()) {
226 throwError(exec, TypeError, "Invalid prototype encountered in instanceof operation.");
227 return false;
228 }
229
230 JSObject *v = static_cast<JSObject *>(value);
231 while ((v = v->prototype()->getObject())) {
232 if (v == prot)
233 return true;
234 }
235 return false;
236}
237
238// ------------------------------ global functions -----------------------------
239
240double roundValue(ExecState *exec, JSValue *v)
241{
242 double d = v->toNumber(exec);
243 double ad = fabs(d);
244 if (ad == 0 || isNaN(d) || isInf(d))
245 return d;
246 return copysign(floor(ad), d);
247}
248
249#ifndef NDEBUG
250#include <stdio.h>
251void printInfo(ExecState *exec, const char *s, JSValue *o, int lineno)
252{
253 if (!o)
254 fprintf(stderr, "KJS: %s: (null)", s);
255 else {
256 JSValue *v = o;
257
258 UString name;
259 switch (v->type()) {
260 case UnspecifiedType:
261 name = "Unspecified";
262 break;
263 case UndefinedType:
264 name = "Undefined";
265 break;
266 case NullType:
267 name = "Null";
268 break;
269 case BooleanType:
270 name = "Boolean";
271 break;
272 case StringType:
273 name = "String";
274 break;
275 case NumberType:
276 name = "Number";
277 break;
278 case ObjectType:
279 name = static_cast<JSObject *>(v)->className();
280 if (name.isNull())
281 name = "(unknown class)";
282 break;
283 case GetterSetterType:
284 name = "GetterSetter";
285 break;
286 }
287 UString vString = v->toString(exec);
288 if ( vString.size() > 50 )
289 vString = vString.substr( 0, 50 ) + "...";
290 // Can't use two UString::ascii() in the same fprintf call
291 CString tempString( vString.cstring() );
292
293 fprintf(stderr, "KJS: %s: %s : %s (%p)",
294 s, tempString.c_str(), name.ascii(), (void*)v);
295
296 if (lineno >= 0)
297 fprintf(stderr, ", line %d\n",lineno);
298 else
299 fprintf(stderr, "\n");
300 }
301}
302#endif
303
304}
Note: See TracBrowser for help on using the repository browser.