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

kjs

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

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
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kjs by doxygen 1.8.11
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |