• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kjs
 

kjs

  • kjs
object_object.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser 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  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  */
20 
21 #include "value.h"
22 #include "object.h"
23 #include "types.h"
24 #include "interpreter.h"
25 #include "operations.h"
26 #include "object_object.h"
27 #include "function_object.h"
28 #include "lookup.h"
29 #include <stdio.h>
30 #include <assert.h>
31 
32 using namespace KJS;
33 
34 // ------------------------------ ObjectPrototypeImp --------------------------------
35 
36 ObjectPrototypeImp::ObjectPrototypeImp(ExecState *exec,
37  FunctionPrototypeImp *funcProto)
38  : ObjectImp() // [[Prototype]] is Null()
39 {
40  Value protect(this);
41  putDirect(toStringPropertyName, new ObjectProtoFuncImp(exec,funcProto,ObjectProtoFuncImp::ToString,
42  0, toStringPropertyName), DontEnum);
43  putDirect(toLocaleStringPropertyName, new ObjectProtoFuncImp(exec,funcProto,ObjectProtoFuncImp::ToLocaleString,
44  0, toLocaleStringPropertyName), DontEnum);
45  putDirect(valueOfPropertyName, new ObjectProtoFuncImp(exec,funcProto,ObjectProtoFuncImp::ValueOf,
46  0, valueOfPropertyName), DontEnum);
47  putDirect("hasOwnProperty", new ObjectProtoFuncImp(exec,funcProto,ObjectProtoFuncImp::HasOwnProperty,
48  1,"hasOwnProperty"),DontEnum);
49  putDirect("isPrototypeOf", new ObjectProtoFuncImp(exec,funcProto,ObjectProtoFuncImp::IsPrototypeOf,
50  1,"isPrototypeOf"),DontEnum);
51  putDirect("propertyIsEnumerable", new ObjectProtoFuncImp(exec,funcProto,ObjectProtoFuncImp::PropertyIsEnumerable,
52  1,"propertyIsEnumerable"),DontEnum);
53 
54 #ifndef KJS_PURE_ECMA // standard compliance location is the Global object
55  // see http://www.devguru.com/Technologies/ecmascript/quickref/object.html
56  put(exec, "eval",
57  Object(new GlobalFuncImp(exec, funcProto,GlobalFuncImp::Eval, 1, "eval")),
58  DontEnum);
59 #endif
60 }
61 
62 // ------------------------------ ObjectProtoFuncImp --------------------------------
63 
64 ObjectProtoFuncImp::ObjectProtoFuncImp(ExecState * /*exec*/,
65  FunctionPrototypeImp *funcProto,
66  int i, int len, const Identifier &_ident)
67  : InternalFunctionImp(funcProto), id(i)
68 {
69  Value protect(this);
70  putDirect(lengthPropertyName, len, DontDelete|ReadOnly|DontEnum);
71  ident = _ident;
72 }
73 
74 
75 bool ObjectProtoFuncImp::implementsCall() const
76 {
77  return true;
78 }
79 
80 // ECMA 15.2.4.2 + 15.2.4.3
81 
82 Value ObjectProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &args)
83 {
84  switch (id) {
85  case ToString:
86  // fall through
87  case ToLocaleString:
88  return String("[object "+thisObj.className()+"]");
89  case ValueOf:
90  return thisObj;
91  case HasOwnProperty: {
92  // Same as hasProperty() but without checking the prototype
93  Identifier propertyName(args[0].toString(exec));
94  Value tempProto(thisObj.imp()->prototype());
95  thisObj.imp()->setPrototype(Value());
96  bool exists = thisObj.hasProperty(exec,propertyName);
97  thisObj.imp()->setPrototype(tempProto);
98  return Value(exists ? BooleanImp::staticTrue : BooleanImp::staticFalse);
99  }
100  case IsPrototypeOf: {
101  Value v = args[0];
102  for (; v.isValid() && v.isA(ObjectType); v = Object::dynamicCast(v).prototype()) {
103  if (v.imp() == thisObj.imp())
104  return Value(BooleanImp::staticTrue);
105  }
106  return Value(BooleanImp::staticFalse);
107  }
108  case PropertyIsEnumerable: {
109  Identifier propertyName(args[0].toString(exec));
110  ObjectImp *obj = static_cast<ObjectImp*>(thisObj.imp());
111 
112  int attributes;
113  ValueImp *v = obj->_prop.get(propertyName,attributes);
114  if (v)
115  return Value((attributes & DontEnum) ?
116  BooleanImp::staticFalse : BooleanImp::staticTrue);
117 
118  if (propertyName == specialPrototypePropertyName)
119  return Value(BooleanImp::staticFalse);
120 
121  const HashEntry *entry = obj->findPropertyHashEntry(propertyName);
122  return Value((entry && !(entry->attr & DontEnum)) ?
123  BooleanImp::staticTrue : BooleanImp::staticFalse);
124  }
125  }
126 
127  return Undefined();
128 }
129 
130 // ------------------------------ ObjectObjectImp --------------------------------
131 
132 ObjectObjectImp::ObjectObjectImp(ExecState * /*exec*/,
133  ObjectPrototypeImp *objProto,
134  FunctionPrototypeImp *funcProto)
135  : InternalFunctionImp(funcProto)
136 {
137  Value protect(this);
138  // ECMA 15.2.3.1
139  putDirect(prototypePropertyName, objProto, DontEnum|DontDelete|ReadOnly);
140 
141  // no. of arguments for constructor
142  putDirect(lengthPropertyName, NumberImp::one(), ReadOnly|DontDelete|DontEnum);
143 }
144 
145 
146 bool ObjectObjectImp::implementsConstruct() const
147 {
148  return true;
149 }
150 
151 // ECMA 15.2.2
152 Object ObjectObjectImp::construct(ExecState *exec, const List &args)
153 {
154  // if no arguments have been passed ...
155  if (args.isEmpty()) {
156  Object proto = exec->lexicalInterpreter()->builtinObjectPrototype();
157  Object result(new ObjectImp(proto));
158  return result;
159  }
160 
161  Value arg = *(args.begin());
162  Object obj = Object::dynamicCast(arg);
163  if (obj.isValid())
164  return obj;
165 
166  switch (arg.type()) {
167  case StringType:
168  case BooleanType:
169  case NumberType:
170  return arg.toObject(exec);
171  default:
172  assert(!"unhandled switch case in ObjectConstructor");
173  case NullType:
174  case UndefinedType:
175  Object proto = exec->lexicalInterpreter()->builtinObjectPrototype();
176  return Object(new ObjectImp(proto));
177  }
178 }
179 
180 bool ObjectObjectImp::implementsCall() const
181 {
182  return true;
183 }
184 
185 Value ObjectObjectImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
186 {
187  Value result;
188 
189  List argList;
190  // Construct a new Object
191  if (args.isEmpty()) {
192  result = construct(exec,argList);
193  } else {
194  Value arg = args[0];
195  if (arg.type() == NullType || arg.type() == UndefinedType) {
196  argList.append(arg);
197  result = construct(exec,argList);
198  } else
199  result = arg.toObject(exec);
200  }
201  return result;
202 }
203 
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:438
KJS::ExecState::lexicalInterpreter
Interpreter * lexicalInterpreter() const
Returns the interpreter associated with the current scope's global object.
Definition: interpreter.cpp:394
KJS::FunctionPrototypeImp
The initial value of Function.prototype (and thus all objects created with the Function constructor)
Definition: function_object.h:34
KJS::Identifier
Represents an Identifier for a Javascript object.
Definition: identifier.h:32
KJS::InternalFunctionImp
Base class for all function objects.
Definition: function.h:40
KJS::Interpreter::builtinObjectPrototype
Object builtinObjectPrototype() const
Returns the builtin "Object.prototype" object.
Definition: interpreter.cpp:218
KJS::List
Native list type.
Definition: list.h:48
KJS::List::append
void append(const Value &val)
Append an object to the end of the list.
Definition: list.h:66
KJS::List::isEmpty
bool isEmpty() const
Definition: list.h:86
KJS::List::begin
ListIterator begin() const
Definition: list.h:186
KJS::Object
Represents an Object.
Definition: object.h:81
KJS::Object::hasProperty
bool hasProperty(ExecState *exec, const Identifier &propertyName) const
Checks to see whether the object (or any object in it's prototype chain) has a property with the spec...
Definition: object.h:678
KJS::Object::className
UString className() const
Returns the class name of the object.
Definition: object.h:660
KJS::String
Represents an primitive String value.
Definition: value.h:340
KJS::Undefined
Represents an primitive Undefined value.
Definition: value.h:269
KJS::ValueImp
ValueImp is the base type for all primitives (Undefined, Null, Boolean, String, Number) and objects i...
Definition: value.h:78
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents.
Definition: value.h:167
KJS::Value::isA
bool isA(Type t) const
Checks whether or not the value is of a particular tpye.
Definition: value.h:203
KJS::Value::toObject
Object toObject(ExecState *exec) const
Performs the ToObject type conversion operation on this value (ECMA 9.9)
Definition: object.h:358
KJS::Value::isValid
bool isValid() const
Returns whether or not this is a valid value.
Definition: value.h:181
KJS::Value::type
Type type() const
Returns the type of value.
Definition: value.h:195
KJS::HashEntry
An entry in a hash table.
Definition: lookup.h:36
KJS::HashEntry::attr
unsigned char attr
attr is a set for flags (e.g.
Definition: lookup.h:48

kjs

Skip menu "kjs"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kjs

Skip menu "kjs"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kjs by doxygen 1.9.1
This website is maintained by Timothy Pearson.