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

kjs

  • kjs
reference.cpp
1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 2003 Apple Computer, Inc
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library 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 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#include "reference.h"
23#include "internal.h"
24#include "context.h"
25
26#include <assert.h>
27
28using namespace KJS;
29
30// ------------------------------ Reference ------------------------------------
31
32Reference::Reference(const Object& b, const Identifier& p)
33 : base(b),
34 baseIsValue(false),
35 propertyNameIsNumber(false),
36 prop(p)
37{
38}
39
40Reference::Reference(const Object& b, unsigned p)
41 : base(b),
42 propertyNameAsNumber(p),
43 baseIsValue(false),
44 propertyNameIsNumber(true)
45{
46}
47
48Reference::Reference(ObjectImp *b, const Identifier& p)
49 : base(b),
50 baseIsValue(false),
51 propertyNameIsNumber(false),
52 prop(p)
53{
54}
55
56Reference::Reference(ObjectImp *b, unsigned p)
57 : base(b),
58 propertyNameAsNumber(p),
59 baseIsValue(false),
60 propertyNameIsNumber(true)
61{
62}
63
64Reference::Reference(const Null& b, const Identifier& p)
65 : base(b),
66 baseIsValue(false),
67 propertyNameIsNumber(false),
68 prop(p)
69{
70}
71
72Reference::Reference(const Null& b, unsigned p)
73 : base(b),
74 propertyNameAsNumber(p),
75 baseIsValue(false),
76 propertyNameIsNumber(true)
77{
78}
79
80Reference Reference::makeValueReference(const Value& v)
81{
82 Reference valueRef;
83 valueRef.base = v;
84 valueRef.baseIsValue = true;
85 return valueRef;
86}
87
88Reference::Reference()
89{
90}
91
92Value Reference::getBase(ExecState *exec) const
93{
94 if (baseIsValue) {
95 Object err = Error::create(exec, ReferenceError, I18N_NOOP("Invalid reference base"));
96 exec->setException(err);
97 return err;
98 }
99
100 return base;
101}
102
103Identifier Reference::getPropertyName(ExecState * /*exec*/) const
104{
105 if (baseIsValue) {
106 // the spec wants a runtime error here. But getValue() and putValue()
107 // will catch this case on their own earlier. When returning a Null
108 // string we should be on the safe side.
109 return Identifier();
110 }
111
112 if (propertyNameIsNumber && prop.isNull())
113 prop = Identifier::from(propertyNameAsNumber);
114 return prop;
115}
116
117Value Reference::getValue(ExecState *exec) const
118{
119 if (baseIsValue) {
120 return base;
121 }
122
123 Value o = getBase(exec);
124
125 if (!o.isValid() || o.type() == NullType) {
126 UString m = I18N_NOOP("Can't find variable: ") + getPropertyName(exec).ustring();
127 Object err = Error::create(exec, ReferenceError, m.ascii());
128 exec->setException(err);
129 return err;
130 }
131
132 if (o.type() != ObjectType) {
133 UString m = I18N_NOOP("Base is not an object");
134 Object err = Error::create(exec, ReferenceError, m.ascii());
135 exec->setException(err);
136 return err;
137 }
138
139 ObjectImp *oimp = static_cast<ObjectImp*>(o.imp());
140 if (propertyNameIsNumber)
141 return oimp->getPropertyByIndex(exec, propertyNameAsNumber);
142 return oimp->get(exec, prop);
143}
144
145void Reference::putValue(ExecState *exec, const Value &w)
146{
147 if (baseIsValue) {
148 Object err = Error::create(exec,ReferenceError);
149 exec->setException(err);
150 return;
151 }
152
153#ifdef KJS_VERBOSE
154 printInfo(exec,(UString("setting property ")+getPropertyName(exec).ustring()).cstring().c_str(),w);
155#endif
156 Value o = getBase(exec);
157 if (o.type() == NullType)
158 o = Value(exec->context().imp()->scopeChain().bottom());
159
160 ObjectImp *oimp = static_cast<ObjectImp*>(o.imp());
161 if (propertyNameIsNumber)
162 oimp->putPropertyByIndex(exec, propertyNameAsNumber, w);
163 else
164 oimp->put(exec, prop, w);
165}
166
167bool Reference::deleteValue(ExecState *exec)
168{
169 if (baseIsValue) {
170 Object err = Error::create(exec,ReferenceError);
171 exec->setException(err);
172 return false;
173 }
174
175 Value b = getBase(exec);
176
177 // The spec doesn't mention what to do if the base is null... just return true
178 if (b.type() != ObjectType) {
179 assert(b.type() == NullType);
180 return true;
181 }
182
183 ObjectImp *bimp = static_cast<ObjectImp*>(b.imp());
184 if (propertyNameIsNumber)
185 return bimp->deletePropertyByIndex(exec, propertyNameAsNumber);
186 return bimp->deleteProperty(exec, prop);
187}
188
189bool Reference::isMutable()
190{
191 return !baseIsValue;
192}
KJS::Error::create
static Object create(ExecState *exec, ErrorType errtype=GeneralError, const char *message=0, int lineno=-1, int sourceId=-1)
Factory method for error objects.
Definition: object.cpp:503
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:438
KJS::ExecState::context
Context context() const
Returns the execution context associated with this execution state.
Definition: interpreter.h:470
KJS::Identifier
Represents an Identifier for a Javascript object.
Definition: identifier.h:32
KJS::Identifier::isNull
bool isNull() const
Returns the identfiers state of being unset.
Definition: identifier.h:78
KJS::Identifier::ustring
const UString & ustring() const
returns a UString of the identifier
Definition: identifier.h:52
KJS::Null
Represents an primitive Null value.
Definition: value.h:294
KJS::Object
Represents an Object.
Definition: object.h:81
KJS::Object::get
Value get(ExecState *exec, const Identifier &propertyName) const
Retrieves the specified property from the object.
Definition: object.h:663
KJS::Reference
Defines a Javascript reference.
Definition: reference.h:34
KJS::Reference::getPropertyName
Identifier getPropertyName(ExecState *exec) const
Performs the GetPropertyName type conversion operation on this value (ECMA 8.7)
Definition: reference.cpp:103
KJS::Reference::getBase
Value getBase(ExecState *exec) const
Performs the GetBase type conversion operation on this value (ECMA 8.7)
Definition: reference.cpp:92
KJS::Reference::putValue
void putValue(ExecState *exec, const Value &w)
Performs the PutValue type conversion operation on this value (ECMA 8.7.1)
Definition: reference.cpp:145
KJS::Reference::getValue
Value getValue(ExecState *exec) const
Performs the GetValue type conversion operation on this value (ECMA 8.7.1)
Definition: reference.cpp:117
KJS::UString
Unicode string class.
Definition: ustring.h:189
KJS::UString::ascii
char * ascii() const
Convert the Unicode string to plain ASCII chars chopping of any higher bytes.
Definition: ustring.cpp:485
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents.
Definition: value.h:167
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

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