source: webkit/trunk/JavaScriptCore/kjs/reference.cpp@ 9781

Last change on this file since 9781 was 9768, checked in by ggaren, 20 years ago

-rolled in patches for http://bugzilla.opendarwin.org/show_bug.cgi?id=3945
[PATCH] Safe merges of comments and other trivialities from KDE's kjs

-patch by Martijn Klingens <[email protected]>

  • kjs/array_instance.h:
  • kjs/array_object.cpp:
  • kjs/array_object.h:
  • kjs/bool_object.cpp:
  • kjs/bool_object.h:
  • kjs/collector.cpp:
  • kjs/collector.h:
  • kjs/completion.h:
  • kjs/context.h:
  • kjs/date_object.cpp:
  • kjs/date_object.h:
  • kjs/debugger.cpp:
  • kjs/debugger.h:
  • kjs/dtoa.h:
  • kjs/error_object.cpp:
  • kjs/error_object.h:
  • kjs/function.cpp:
  • kjs/function.h:
  • kjs/function_object.cpp:
  • kjs/function_object.h:
  • kjs/grammar.y:
  • kjs/identifier.cpp:
  • kjs/identifier.h:
  • kjs/internal.cpp:
  • kjs/internal.h:
  • kjs/interpreter.cpp:
  • kjs/interpreter.h:
  • kjs/interpreter_map.cpp:
  • kjs/interpreter_map.h:
  • kjs/lexer.cpp:
  • kjs/lexer.h:
  • kjs/list.cpp:
  • kjs/list.h:
  • kjs/lookup.cpp:
  • kjs/lookup.h:
  • kjs/math_object.cpp:
  • kjs/math_object.h:
  • kjs/nodes.cpp:
  • kjs/nodes.h:
  • kjs/nodes2string.cpp:
  • kjs/number_object.cpp:
  • kjs/number_object.h:
  • kjs/object.cpp:
  • kjs/object.h:
  • kjs/object_object.cpp:
  • kjs/object_object.h:
  • kjs/operations.cpp:
  • kjs/operations.h:
  • kjs/property_map.cpp:
  • kjs/property_map.h:
  • kjs/reference.cpp:
  • kjs/reference.h:
  • kjs/reference_list.cpp:
  • kjs/reference_list.h:
  • kjs/regexp.cpp:
  • kjs/regexp.h:
  • kjs/regexp_object.cpp:
  • kjs/regexp_object.h:
  • kjs/scope_chain.cpp:
  • kjs/scope_chain.h:
  • kjs/simple_number.h:
  • kjs/string_object.cpp:
  • kjs/string_object.h:
  • kjs/testkjs.cpp:
  • kjs/types.h:
  • kjs/ustring.cpp:
  • kjs/ustring.h:
  • kjs/value.cpp:
  • kjs/value.h:
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 2004 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 Steet, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "reference.h"
24#include "internal.h"
25
26namespace KJS {
27
28// ------------------------------ Reference ------------------------------------
29
30Reference::Reference(const Object& b, const Identifier& p)
31 : base(b),
32 baseIsValue(false),
33 propertyNameIsNumber(false),
34 prop(p)
35{
36}
37
38Reference::Reference(const Object& b, unsigned p)
39 : base(b),
40 propertyNameAsNumber(p),
41 baseIsValue(false),
42 propertyNameIsNumber(true)
43{
44}
45
46Reference::Reference(ObjectImp *b, const Identifier& p)
47 : base(b),
48 baseIsValue(false),
49 propertyNameIsNumber(false),
50 prop(p)
51{
52}
53
54Reference::Reference(ObjectImp *b, unsigned p)
55 : base(b),
56 propertyNameAsNumber(p),
57 baseIsValue(false),
58 propertyNameIsNumber(true)
59{
60}
61
62Reference::Reference(const Null& b, const Identifier& p)
63 : base(b),
64 baseIsValue(false),
65 propertyNameIsNumber(false),
66 prop(p)
67{
68}
69
70Reference::Reference(const Null& b, unsigned p)
71 : base(b),
72 propertyNameAsNumber(p),
73 baseIsValue(false),
74 propertyNameIsNumber(true)
75{
76}
77
78Reference Reference::makeValueReference(const Value& v)
79{
80 Reference valueRef;
81 valueRef.base = v;
82 valueRef.baseIsValue = true;
83 return valueRef;
84}
85
86Value Reference::getBase(ExecState *exec) const
87{
88 if (baseIsValue) {
89 Object err = Error::create(exec, ReferenceError, I18N_NOOP("Invalid reference base"));
90 exec->setException(err);
91 return err;
92 }
93
94 return base;
95}
96
97Identifier Reference::getPropertyName(ExecState *exec) const
98{
99 if (baseIsValue) {
100 // the spec wants a runtime error here. But getValue() and putValue()
101 // will catch this case on their own earlier. When returning a Null
102 // string we should be on the safe side.
103 return Identifier();
104 }
105
106 if (propertyNameIsNumber && prop.isNull())
107 prop = Identifier::from(propertyNameAsNumber);
108 return prop;
109}
110
111Value Reference::getValue(ExecState *exec) const
112{
113 if (baseIsValue) {
114 return base;
115 }
116
117 ValueImp *o = base.imp();
118 Type t = o ? o->dispatchType() : NullType;
119
120 if (t == NullType) {
121 UString m = I18N_NOOP("Can't find variable: ") + getPropertyName(exec).ustring();
122 Object err = Error::create(exec, ReferenceError, m.ascii());
123 exec->setException(err);
124 return err;
125 }
126
127 if (t != ObjectType) {
128 UString m = I18N_NOOP("Base is not an object");
129 Object err = Error::create(exec, ReferenceError, m.ascii());
130 exec->setException(err);
131 return err;
132 }
133
134 if (propertyNameIsNumber)
135 return static_cast<ObjectImp*>(o)->get(exec, propertyNameAsNumber);
136 return static_cast<ObjectImp*>(o)->get(exec, prop);
137}
138
139void Reference::putValue(ExecState *exec, const Value &w)
140{
141 if (baseIsValue) {
142 Object err = Error::create(exec, ReferenceError);
143 exec->setException(err);
144 return;
145 }
146
147#ifdef KJS_VERBOSE
148 printInfo(exec,(UString("setting property ")+getPropertyName(exec).ustring()).cstring().c_str(),w);
149#endif
150
151 ValueImp *o = base.imp();
152 Type t = o ? o->dispatchType() : NullType;
153
154 if (t == NullType)
155 o = exec->lexicalInterpreter()->globalObject().imp();
156
157 if (propertyNameIsNumber)
158 return static_cast<ObjectImp*>(o)->put(exec, propertyNameAsNumber, w);
159 return static_cast<ObjectImp*>(o)->put(exec, prop, w);
160}
161
162bool Reference::deleteValue(ExecState *exec)
163{
164 if (baseIsValue) {
165 Object err = Error::create(exec,ReferenceError);
166 exec->setException(err);
167 return false;
168 }
169
170 ValueImp *o = base.imp();
171 Type t = o ? o->dispatchType() : NullType;
172
173 // The spec doesn't mention what to do if the base is null... just return true
174 if (t != ObjectType) {
175 assert(t == NullType);
176 return true;
177 }
178
179 if (propertyNameIsNumber)
180 return static_cast<ObjectImp*>(o)->deleteProperty(exec,propertyNameAsNumber);
181 return static_cast<ObjectImp*>(o)->deleteProperty(exec,prop);
182}
183
184}
Note: See TracBrowser for help on using the repository browser.