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

kjs

  • kjs
error_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  * Copyright (C) 2003 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 Lesser 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  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  */
22 
23 #include "value.h"
24 #include "object.h"
25 #include "types.h"
26 #include "interpreter.h"
27 #include "operations.h"
28 #include "error_object.h"
29 //#include "debugger.h"
30 
31 using namespace KJS;
32 
33 // ------------------------------ ErrorInstanceImp ----------------------------
34 
35 const ClassInfo ErrorInstanceImp::info = {"Error", 0, 0, 0};
36 
37 ErrorInstanceImp::ErrorInstanceImp(ObjectImp *proto)
38  : ObjectImp(proto)
39 {
40 }
41 
42 // ------------------------------ ErrorPrototypeImp ----------------------------
43 
44 // ECMA 15.9.4
45 ErrorPrototypeImp::ErrorPrototypeImp(ExecState *exec,
46  ObjectPrototypeImp *objectProto,
47  FunctionPrototypeImp *funcProto)
48  : ObjectImp(objectProto)
49 {
50  Value protect(this);
51  setInternalValue(Undefined());
52  // The constructor will be added later in ErrorObjectImp's constructor
53 
54  put(exec, namePropertyName, String("Error"), DontEnum);
55  put(exec, messagePropertyName, String("Unknown error"), DontEnum);
56  putDirect(toStringPropertyName, new ErrorProtoFuncImp(exec,funcProto), DontEnum);
57 }
58 
59 // ------------------------------ ErrorProtoFuncImp ----------------------------
60 
61 ErrorProtoFuncImp::ErrorProtoFuncImp(ExecState * /*exec*/, FunctionPrototypeImp *funcProto)
62  : InternalFunctionImp(funcProto)
63 {
64  Value protect(this);
65  putDirect(lengthPropertyName, NumberImp::zero(), DontDelete|ReadOnly|DontEnum);
66  ident = "toString";
67 }
68 
69 bool ErrorProtoFuncImp::implementsCall() const
70 {
71  return true;
72 }
73 
74 Value ErrorProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &/*args*/)
75 {
76  // toString()
77  UString s = "Error";
78 
79  Value v = thisObj.get(exec, namePropertyName);
80  if (v.type() != UndefinedType) {
81  s = v.toString(exec);
82  }
83 
84  v = thisObj.get(exec, messagePropertyName);
85  if (v.type() != UndefinedType) {
86  s += ": " + v.toString(exec); // Mozilla compatible format
87  }
88 
89  return String(s);
90 }
91 
92 // ------------------------------ ErrorObjectImp -------------------------------
93 
94 ErrorObjectImp::ErrorObjectImp(ExecState * /*exec*/, FunctionPrototypeImp *funcProto,
95  ErrorPrototypeImp *errorProto)
96  : InternalFunctionImp(funcProto)
97 {
98  Value protect(this);
99  // ECMA 15.11.3.1 Error.prototype
100  putDirect(prototypePropertyName, errorProto, DontEnum|DontDelete|ReadOnly);
101  putDirect(lengthPropertyName, NumberImp::one(), DontDelete|ReadOnly|DontEnum);
102  //putDirect(namePropertyName, String(n));
103 }
104 
105 bool ErrorObjectImp::implementsConstruct() const
106 {
107  return true;
108 }
109 
110 // ECMA 15.9.3
111 Object ErrorObjectImp::construct(ExecState *exec, const List &args)
112 {
113  Object proto = Object::dynamicCast(exec->lexicalInterpreter()->builtinErrorPrototype());
114  ObjectImp *imp = new ErrorInstanceImp(proto.imp());
115  Object obj(imp);
116 
117  if (!args.isEmpty() && args[0].type() != UndefinedType) {
118  imp->putDirect(messagePropertyName, new StringImp(args[0].toString(exec)));
119  }
120 
121  return obj;
122 }
123 
124 bool ErrorObjectImp::implementsCall() const
125 {
126  return true;
127 }
128 
129 // ECMA 15.9.2
130 Value ErrorObjectImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
131 {
132  // "Error()" gives the sames result as "new Error()"
133  return construct(exec,args);
134 }
135 
136 // ------------------------------ NativeErrorPrototypeImp ----------------------
137 
138 NativeErrorPrototypeImp::NativeErrorPrototypeImp(ExecState * /*exec*/, ErrorPrototypeImp *errorProto,
139  ErrorType et, UString name, UString message)
140  : ObjectImp(errorProto)
141 {
142  Value protect(this);
143  errType = et;
144  putDirect(namePropertyName, new StringImp(name), 0);
145  putDirect(messagePropertyName, new StringImp(message), 0);
146 }
147 
148 // ------------------------------ NativeErrorImp -------------------------------
149 
150 const ClassInfo NativeErrorImp::info = {"Function", &InternalFunctionImp::info, 0, 0};
151 
152 NativeErrorImp::NativeErrorImp(ExecState * /*exec*/, FunctionPrototypeImp *funcProto,
153  const Object &prot)
154  : InternalFunctionImp(funcProto), proto(0)
155 {
156  Value protect(this);
157  proto = static_cast<ObjectImp*>(prot.imp());
158 
159  putDirect(lengthPropertyName, NumberImp::one(), DontDelete|ReadOnly|DontEnum); // ECMA 15.11.7.5
160  putDirect(prototypePropertyName, proto, DontDelete|ReadOnly|DontEnum);
161 }
162 
163 bool NativeErrorImp::implementsConstruct() const
164 {
165  return true;
166 }
167 
168 Object NativeErrorImp::construct(ExecState *exec, const List &args)
169 {
170  ObjectImp *imp = new ErrorInstanceImp(proto);
171  Object obj(imp);
172  if (args[0].type() != UndefinedType)
173  imp->putDirect(messagePropertyName, new StringImp(args[0].toString(exec)));
174  return obj;
175 }
176 
177 bool NativeErrorImp::implementsCall() const
178 {
179  return true;
180 }
181 
182 Value NativeErrorImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
183 {
184  return construct(exec,args);
185 }
186 
187 void NativeErrorImp::mark()
188 {
189  ObjectImp::mark();
190  if (proto && !proto->marked())
191  proto->mark();
192 }
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents...
Definition: value.h:168
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::InternalFunctionImp
Base class for all function objects.
Definition: function.h:41
KJS::Object::get
Value get(ExecState *exec, const Identifier &propertyName) const
Retrieves the specified property from the object.
Definition: object.h:664
KJS::Value::type
Type type() const
Returns the type of value.
Definition: value.h:196
KJS::ExecState::lexicalInterpreter
Interpreter * lexicalInterpreter() const
Returns the interpreter associated with the current scope&#39;s global object.
Definition: interpreter.cpp:395
KJS::FunctionPrototypeImp
The initial value of Function.prototype (and thus all objects created with the Function constructor) ...
Definition: function_object.h:35
KJS::Interpreter::builtinErrorPrototype
Object builtinErrorPrototype() const
Returns the builtin "Error.prototype" object.
Definition: interpreter.cpp:259
KJS::Object
Represents an Object.
Definition: object.h:82
KJS::Undefined
Represents an primitive Undefined value.
Definition: value.h:270
KJS::UString
Unicode string class.
Definition: ustring.h:190
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::List
Native list type.
Definition: list.h:48
KJS::ClassInfo
Class Information.
Definition: object.h:59
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:439
KJS::List::isEmpty
bool isEmpty() const
Definition: list.h:86

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.8.13
This website is maintained by Timothy Pearson.