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

kjs

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