source: webkit/trunk/JavaScriptCore/kjs/object.h@ 1791

Last change on this file since 1791 was 1623, checked in by darin, 23 years ago

JavaScriptCore:

  • kjs/*: Roll KDE 3.0.2 changes in. Also switch to not using APPLE_CHANGES for some of the changes that we definitely want to contribute upstream.

WebCore:

  • khtml/*: Roll KDE 3.0.2 changes in. Also switch to not using APPLE_CHANGES for some of the changes that we definitely want to contribute upstream.
  • WebCore.pbproj/project.pbxproj: Add KWQStyle.mm, remove KWQStyle.h, moving contents into qstyle.h.
  • kwq/KWQApplication.mm: (QApplication::globalStrut): Remove _logNotYetImplemented().
  • kwq/KWQButton.mm: (QButton::QButton): Use plain release, not autorelease.
  • kwq/KWQComboBox.mm: (QComboBox::init): Use plain release, not autorelease.
  • kwq/KWQListBox.mm: (QListBox::QListBox): Use plain release, not autorelease.
  • kwq/KWQPainter.mm: (QPainter::drawArc): Use plain release, not autorelease.
  • kwq/KWQKHTMLPartBrowserExtension.mm: Remove import of KWQKHTMLPartImpl.h, now that it's always part of khtml_part.h.
  • kwq/KWQKHTMLPartImpl.cpp: Simplify.
  • kwq/KWQKHTMLPartImpl.h: Add wrapper to allow multiple inclusion. Don't include khtml_part.h any more, since that file now includes this one to minimize changes to KDE code that needs to get to functions in here.
  • kwq/KWQKHTMLPartImpl.mm: (KHTMLPart::onURL), (KHTMLPart::nodeActivated), (KHTMLPart::setStatusBarText): Moved here from khtml_part.cpp.
  • kwq/KWQLoaderImpl.mm: Include khtml_part.h instead of KWQKHTMLPartImpl.h.
  • kwq/KWQPushButton.mm: (buttonFontMetrics), (QPushButton::fontMetrics): Added. Used by the form code to size buttons.
  • kwq/KWQStyle.mm: Added. (QStyle::sizeFromContents): Added. Used by the form code to size buttons.
  • kwq/KWQStyle.h: Removed.
  • kwq/qt/qstyle.h: Moved contents of KWQStyle.h in here.
  • kwq/qt/qwidget.h: Include <qstyle.h> rather than KWQStyle.h.
  • kwq/WebCoreBridge.mm: (-[WebCoreBridge isFrameSet]): Call straight to impl.
  • kwq/kdeui/klineedit.h: Add KLineEdit::frameWidth().
  • kwq/qt/qnamespace.h: Remove GUIStyle, MacStyle, and WindowsStyle.
  • kwq/qt/qpaintdevice.h: Add QInternal, QInternal::Printer, and QPaintDevice::devType().
  • kwq/qt/qpainter.h: Add QPainter::device().
  • kwq/qt/qpushbutton.h: Add QPushButton::fontMetrics().
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.5 KB
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2001 Harri Porten ([email protected])
5 * Copyright (C) 2001 Peter Kelly ([email protected])
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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
22 */
23
24
25#ifndef _KJS_OBJECT_H_
26#define _KJS_OBJECT_H_
27
28// Objects
29
30// maximum global call stack size. Protects against accidental or
31// malicious infinite recursions. Define to -1 if you want no limit.
32#define KJS_MAX_STACK 1000
33
34#include "value.h"
35#include "types.h"
36
37namespace KJS {
38
39 class ObjectImpPrivate;
40 class PropertyMap;
41 class HashTable;
42 class HashEntry;
43 class ListImp;
44
45 // ECMA 262-3 8.6.1
46 // Attributes (only applicable to the Object type)
47 enum Attribute { None = 0,
48 ReadOnly = 1 << 1, // property can be only read, not written
49 DontEnum = 1 << 2, // property doesn't appear in (for .. in ..)
50 DontDelete = 1 << 3, // property can't be deleted
51 Internal = 1 << 4, // an internal property, set to by pass checks
52 Function = 1 << 5 }; // property is a function - only used by static hashtables
53
54 /**
55 * Class Information
56 */
57 struct ClassInfo {
58 /**
59 * A string denoting the class name. Example: "Window".
60 */
61 const char* className;
62 /**
63 * Pointer to the class information of the base class.
64 * 0L if there is none.
65 */
66 const ClassInfo *parentClass;
67 /**
68 * Static hash-table of properties.
69 */
70 const HashTable *propHashTable;
71 /**
72 * Reserved for future extension.
73 */
74 void *dummy;
75 };
76
77 /**
78 * Represents an Object. This is a wrapper for ObjectImp
79 */
80 class Object : public Value {
81 public:
82 Object();
83 explicit Object(ObjectImp *v);
84 Object(const Object &v);
85 virtual ~Object();
86
87 Object& operator=(const Object &v);
88
89 virtual const ClassInfo *classInfo() const;
90 bool inherits(const ClassInfo *cinfo) const;
91
92 /**
93 * Converts a Value into an Object. If the value's type is not ObjectType,
94 * a null object will be returned (i.e. one with it's internal pointer set
95 * to 0). If you do not know for sure whether the value is of type
96 * ObjectType, you should check the @ref isNull() methods afterwards before
97 * calling any methods on the Object.
98 *
99 * @return The value converted to an object
100 */
101 static Object dynamicCast(const Value &v);
102
103 /**
104 * Returns the prototype of this object. Note that this is not the same as
105 * the "prototype" property.
106 *
107 * See ECMA 8.6.2
108 *
109 * @return The object's prototype
110 */
111 Value prototype() const;
112
113 /**
114 * Returns the class name of the object
115 *
116 * See ECMA 8.6.2
117 *
118 * @return The object's class name
119 */
120 UString className() const;
121
122 /**
123 * Retrieves the specified property from the object. If neither the object
124 * or any other object in it's prototype chain have the property, this
125 * function will return Undefined.
126 *
127 * See ECMA 8.6.2.1
128 *
129 * @param exec The current execution state
130 * @param propertyName The name of the property to retrieve
131 *
132 * @return The specified property, or Undefined
133 */
134 Value get(ExecState *exec, const UString &propertyName) const;
135
136 /**
137 * Sets the specified property.
138 *
139 * See ECMA 8.6.2.2
140 *
141 * @param exec The current execution state
142 * @param propertyName The name of the property to set
143 * @param propertyValue The value to set
144 */
145 void put(ExecState *exec, const UString &propertyName,
146 const Value &value, int attr = None);
147
148 /**
149 * Used to check whether or not a particular property is allowed to be set
150 * on an object
151 *
152 * See ECMA 8.6.2.3
153 *
154 * @param exec The current execution state
155 * @param propertyName The name of the property
156 * @return true if the property can be set, otherwise false
157 */
158 bool canPut(ExecState *exec, const UString &propertyName) const;
159
160 /**
161 * Checks to see whether the object (or any object in it's prototype chain)
162 * has a property with the specified name.
163 *
164 * See ECMA 8.6.2.4
165 *
166 * @param exec The current execution state
167 * @param propertyName The name of the property to check for
168 * @return true if the object has the property, otherwise false
169 */
170 bool hasProperty(ExecState *exec, const UString &propertyName) const;
171
172 /**
173 * Removes the specified property from the object.
174 *
175 * See ECMA 8.6.2.5
176 *
177 * @param exec The current execution state
178 * @param propertyName The name of the property to delete
179 * @return true if the property was successfully deleted or did not
180 * exist on the object. false if deleting the specified property is not
181 * allowed.
182 */
183 bool deleteProperty(ExecState *exec, const UString &propertyName);
184
185 /**
186 * Converts the object into a primitive value. The value return may differ
187 * depending on the supplied hint
188 *
189 * See ECMA 8.6.2.6
190 *
191 * @param exec The current execution state
192 * @param hint The desired primitive type to convert to
193 * @return A primitive value converted from the objetc. Note that the
194 * type of primitive value returned may not be the same as the requested
195 * hint.
196 */
197 Value defaultValue(ExecState *exec, Type hint) const;
198
199 /**
200 * Whether or not the object implements the construct() method. If this
201 * returns false you should not call the construct() method on this
202 * object (typically, an assertion will fail to indicate this).
203 *
204 * @return true if this object implements the construct() method, otherwise
205 * false
206 */
207 bool implementsConstruct() const;
208
209 /**
210 * Creates a new object based on this object. Typically this means the
211 * following:
212 * 1. A new object is created
213 * 2. The prototype of the new object is set to the value of this object's
214 * "prototype" property
215 * 3. The call() method of this object is called, with the new object
216 * passed as the this value
217 * 4. The new object is returned
218 *
219 * In some cases, Host objects may differ from these semantics, although
220 * this is discouraged.
221 *
222 * If an error occurs during construction, the execution state's exception
223 * will be set. This can be tested for with @ref ExecState::hadException().
224 * Under some circumstances, the exception object may also be returned.
225 *
226 * Note: This function should not be called if implementsConstruct() returns
227 * false, in which case it will result in an assertion failure.
228 *
229 * @param exec The current execution state
230 * @param args The arguments to be passed to call() once the new object has
231 * been created
232 * @return The newly created &amp; initialized object
233 */
234 Object construct(ExecState *exec, const List &args);
235
236 /**
237 * Whether or not the object implements the call() method. If this returns
238 * false you should not call the call() method on this object (typically,
239 * an assertion will fail to indicate this).
240 *
241 * @return true if this object implements the call() method, otherwise
242 * false
243 */
244 bool implementsCall() const;
245
246
247 /**
248 * Calls this object as if it is a function.
249 *
250 * Note: This function should not be called if implementsCall() returns
251 * false, in which case it will result in an assertion failure.
252 *
253 * See ECMA 8.6.2.3
254 *
255 * @param exec The current execution state
256 * @param thisObj The obj to be used as "this" within function execution.
257 * Note that in most cases this will be different from the C++ "this"
258 * object. For example, if the ECMAScript code "window.location.toString()"
259 * is executed, call() will be invoked on the C++ object which implements
260 * the toString method, with the thisObj being window.location
261 * @param args List of arguments to be passed to the function
262 * @return The return value from the function
263 */
264 Value call(ExecState *exec, Object &thisObj, const List &args);
265
266 /**
267 * Whether or not the object implements the hasInstance() method. If this
268 * returns false you should not call the hasInstance() method on this
269 * object (typically, an assertion will fail to indicate this).
270 *
271 * @return true if this object implements the hasInstance() method,
272 * otherwise false
273 */
274 bool implementsHasInstance() const;
275
276 /**
277 * Checks whether value delegates behaviour to this object. Used by the
278 * instanceof operator.
279 *
280 * @param exec The current execution state
281 * @param value The value to check
282 * @return true if value delegates behaviour to this object, otherwise
283 * false
284 */
285 Boolean hasInstance(ExecState *exec, const Value &value);
286
287 /**
288 * Returns the scope of this object. This is used when execution declared
289 * functions - the execution context for the function is initialized with
290 * extra object in it's scope. An example of this is functions declared
291 * inside other functions:
292 *
293 * function f() {
294 *
295 * function b() {
296 * return prototype;
297 * }
298 *
299 * var x = 4;
300 * // do some stuff
301 * }
302 * f.prototype = new String();
303 *
304 * When the function f.b is executed, its scope will include properties of
305 * f. So in the example above the return value of f.b() would be the new
306 * String object that was assigned to f.prototype.
307 *
308 * @param exec The current execution state
309 * @return The function's scope
310 */
311 const List scope() const;
312 void setScope(const List &s);
313
314 /**
315 * Returns a List of References to all the properties of the object. Used
316 * in "for x in y" statements. The list is created new, so it can be freely
317 * modified without affecting the object's properties. It should be deleted
318 * by the caller.
319 *
320 * Subclasses can override this method in ObjectImpl to provide the
321 * appearance of
322 * having extra properties other than those set specifically with put().
323 *
324 * @param exec The current execution state
325 * @param recursive Whether or not properties in the object's prototype
326 * chain should be
327 * included in the list.
328 * @return A List of References to properties of the object.
329 **/
330 List propList(ExecState *exec, bool recursive = true);
331
332 /**
333 * Returns the internal value of the object. This is used for objects such
334 * as String and Boolean which are wrappers for native types. The interal
335 * value is the actual value represented by the wrapper objects.
336 *
337 * @see ECMA 8.6.2
338 * @return The internal value of the object
339 */
340 Value internalValue() const;
341
342 /**
343 * Sets the internal value of the object
344 *
345 * @see internalValue()
346 *
347 * @param v The new internal value
348 */
349 void setInternalValue(const Value &v);
350 };
351
352 class ObjectImp : public ValueImp {
353 public:
354 /**
355 * Creates a new ObjectImp with the specified prototype
356 *
357 * @param proto The prototype
358 */
359 ObjectImp(const Object &proto);
360
361 /**
362 * Creates a new ObjectImp with a prototype of Null()
363 * (that is, the ECMAScript "null" value, not a null Object).
364 *
365 */
366 ObjectImp();
367
368 virtual ~ObjectImp();
369
370 virtual void mark();
371
372 Type type() const;
373
374 /**
375 * A pointer to a ClassInfo struct for this class. This provides a basic
376 * facility for run-time type information, and can be used to check an
377 * object's class an inheritance (see @ref inherits()). This should
378 * always return a statically declared pointer, or 0 to indicate that
379 * there is no class information.
380 *
381 * This is primarily useful if you have application-defined classes that you
382 * wish to check against for casting purposes.
383 *
384 * For example, to specify the class info for classes FooImp and BarImp,
385 * where FooImp inherits from BarImp, you would add the following in your
386 * class declarations:
387 *
388 * class BarImp : public ObjectImp {
389 * virtual const ClassInfo *classInfo() const { return &info; }
390 * static const ClassInfo info;
391 * // ...
392 * };
393 *
394 * class FooImp : public ObjectImp {
395 * virtual const ClassInfo *classInfo() const { return &info; }
396 * static const ClassInfo info;
397 * // ...
398 * };
399 *
400 * And in your source file:
401 *
402 * const ClassInfo BarImp::info = {0, 0, 0}; // no parent class
403 * const ClassInfo FooImp::info = {&BarImp::info, 0, 0};
404 *
405 * @see inherits()
406 */
407 virtual const ClassInfo *classInfo() const;
408
409 /**
410 * Checks whether this object inherits from the class with the specified
411 * classInfo() pointer. This requires that both this class and the other
412 * class return a non-NULL pointer for their classInfo() methods (otherwise
413 * it will return false).
414 *
415 * For example, for two ObjectImp pointers obj1 and obj2, you can check
416 * if obj1's class inherits from obj2's class using the following:
417 *
418 * if (obj1->inherits(obj2->classInfo())) {
419 * // ...
420 * }
421 *
422 * If you have a handle to a statically declared ClassInfo, such as in the
423 * @ref classInfo() example, you can check for inheritance without needing
424 * an instance of the other class:
425 *
426 * if (obj1->inherits(FooImp::info)) {
427 * // ...
428 * }
429 *
430 * @param cinfo The ClassInfo pointer for the class you want to check
431 * inheritance against.
432 * @return true if this object's class inherits from class with the
433 * ClassInfo pointer specified in cinfo
434 */
435 bool inherits(const ClassInfo *cinfo) const;
436
437 // internal properties (ECMA 262-3 8.6.2)
438
439 /**
440 * Implementation of the [[Prototype]] internal property (implemented by
441 * all Objects)
442 *
443 * @see Object::prototype()
444 */
445 Value prototype() const;
446 void setPrototype(const Value &proto);
447
448 /**
449 * Implementation of the [[Class]] internal property (implemented by all
450 * Objects)
451 *
452 * The default implementation uses classInfo().
453 * You should either implement @ref classInfo(), or
454 * if you simply need a classname, you can reimplement @ref className()
455 * instead.
456 *
457 * @see Object::className()
458 */
459 virtual UString className() const;
460
461 /**
462 * Implementation of the [[Get]] internal property (implemented by all
463 * Objects)
464 *
465 * @see Object::get()
466 */
467 // [[Get]] - must be implemented by all Objects
468 virtual Value get(ExecState *exec, const UString &propertyName) const;
469
470 /**
471 * Implementation of the [[Put]] internal property (implemented by all
472 * Objects)
473 *
474 * @see Object::put()
475 */
476 virtual void put(ExecState *exec, const UString &propertyName,
477 const Value &value, int attr = None);
478
479 /**
480 * Implementation of the [[CanPut]] internal property (implemented by all
481 * Objects)
482 *
483 * @see Object::canPut()
484 */
485 virtual bool canPut(ExecState *exec, const UString &propertyName) const;
486
487 /**
488 * Implementation of the [[HasProperty]] internal property (implemented by
489 * all Objects)
490 *
491 * @see Object::hasProperty()
492 */
493 virtual bool hasProperty(ExecState *exec,
494 const UString &propertyName) const;
495
496 /**
497 * Implementation of the [[Delete]] internal property (implemented by all
498 * Objects)
499 *
500 * @see Object::deleteProperty()
501 */
502 virtual bool deleteProperty(ExecState *exec,
503 const UString &propertyName);
504
505 /**
506 * Remove all properties from this object.
507 * This doesn't take DontDelete into account, and isn't in the ECMA spec.
508 * It's simply a quick way to remove everything before destroying.
509 */
510 void deleteAllProperties( ExecState * );
511
512 /**
513 * Implementation of the [[DefaultValue]] internal property (implemented by
514 * all Objects)
515 *
516 * @see Object::defaultValue()
517 */
518 virtual Value defaultValue(ExecState *exec, Type hint) const;
519
520 virtual bool implementsConstruct() const;
521 /**
522 * Implementation of the [[Construct]] internal property
523 *
524 * @see Object::construct()
525 */
526 virtual Object construct(ExecState *exec, const List &args);
527
528 virtual bool implementsCall() const;
529 /**
530 * Implementation of the [[Call]] internal property
531 *
532 * @see Object::call()
533 */
534 virtual Value call(ExecState *exec, Object &thisObj,
535 const List &args);
536
537 virtual bool implementsHasInstance() const;
538 /**
539 * Implementation of the [[HasInstance]] internal property
540 *
541 * @see Object::hasInstance()
542 */
543 virtual Boolean hasInstance(ExecState *exec, const Value &value);
544
545 /**
546 * Implementation of the [[Scope]] internal property
547 *
548 * @see Object::scope()
549 */
550 const List scope() const;
551 void setScope(const List &s);
552
553 List propList(ExecState *exec, bool recursive = true);
554
555 Value internalValue() const;
556 void setInternalValue(const Value &v);
557
558 Value toPrimitive(ExecState *exec,
559 Type preferredType = UnspecifiedType) const;
560 bool toBoolean(ExecState *exec) const;
561 double toNumber(ExecState *exec) const;
562 int toInteger(ExecState *exec) const;
563 int toInt32(ExecState *exec) const;
564 unsigned int toUInt32(ExecState *exec) const;
565 unsigned short toUInt16(ExecState *exec) const;
566 UString toString(ExecState *exec) const;
567 Object toObject(ExecState *exec) const;
568
569 ValueImp* getDirect(const UString& propertyName) const;
570 private:
571 const HashEntry* findPropertyHashEntry( const UString& propertyName ) const;
572 ObjectImpPrivate *_od;
573 PropertyMap *_prop;
574 ValueImp *_proto;
575 ValueImp *_internalValue;
576 ListImp *_scope;
577 };
578
579 /**
580 * Types of Native Errors available. For custom errors, GeneralError
581 * should be used.
582 */
583 enum ErrorType { GeneralError = 0,
584 EvalError = 1,
585 RangeError = 2,
586 ReferenceError = 3,
587 SyntaxError = 4,
588 TypeError = 5,
589 URIError = 6};
590
591 /**
592 * @short Factory methods for error objects.
593 */
594 class Error {
595 public:
596 /**
597 * Factory method for error objects.
598 *
599 * @param exec The current execution state
600 * @param errtype Type of error.
601 * @param message Optional error message.
602 * @param lineno Optional line number.
603 * @param lineno Optional source id.
604 */
605 static Object create(ExecState *exec, ErrorType errtype = GeneralError,
606 const char *message = 0, int lineno = -1,
607 int sourceId = -1);
608
609 /**
610 * Array of error names corresponding to @ref ErrorType
611 */
612 static const char * const * const errorNames;
613 };
614
615}; // namespace
616
617#endif // _KJS_OBJECT_H_
Note: See TracBrowser for help on using the repository browser.