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 |
|
---|
26 | namespace KJS {
|
---|
27 |
|
---|
28 | // ------------------------------ Reference ------------------------------------
|
---|
29 |
|
---|
30 | Reference::Reference(const Object& b, const Identifier& p)
|
---|
31 | : base(b),
|
---|
32 | baseIsValue(false),
|
---|
33 | propertyNameIsNumber(false),
|
---|
34 | prop(p)
|
---|
35 | {
|
---|
36 | }
|
---|
37 |
|
---|
38 | Reference::Reference(const Object& b, unsigned p)
|
---|
39 | : base(b),
|
---|
40 | propertyNameAsNumber(p),
|
---|
41 | baseIsValue(false),
|
---|
42 | propertyNameIsNumber(true)
|
---|
43 | {
|
---|
44 | }
|
---|
45 |
|
---|
46 | Reference::Reference(ObjectImp *b, const Identifier& p)
|
---|
47 | : base(b),
|
---|
48 | baseIsValue(false),
|
---|
49 | propertyNameIsNumber(false),
|
---|
50 | prop(p)
|
---|
51 | {
|
---|
52 | }
|
---|
53 |
|
---|
54 | Reference::Reference(ObjectImp *b, unsigned p)
|
---|
55 | : base(b),
|
---|
56 | propertyNameAsNumber(p),
|
---|
57 | baseIsValue(false),
|
---|
58 | propertyNameIsNumber(true)
|
---|
59 | {
|
---|
60 | }
|
---|
61 |
|
---|
62 | Reference::Reference(const Null& b, const Identifier& p)
|
---|
63 | : base(b),
|
---|
64 | baseIsValue(false),
|
---|
65 | propertyNameIsNumber(false),
|
---|
66 | prop(p)
|
---|
67 | {
|
---|
68 | }
|
---|
69 |
|
---|
70 | Reference::Reference(const Null& b, unsigned p)
|
---|
71 | : base(b),
|
---|
72 | propertyNameAsNumber(p),
|
---|
73 | baseIsValue(false),
|
---|
74 | propertyNameIsNumber(true)
|
---|
75 | {
|
---|
76 | }
|
---|
77 |
|
---|
78 | Reference Reference::makeValueReference(const Value& v)
|
---|
79 | {
|
---|
80 | Reference valueRef;
|
---|
81 | valueRef.base = v;
|
---|
82 | valueRef.baseIsValue = true;
|
---|
83 | return valueRef;
|
---|
84 | }
|
---|
85 |
|
---|
86 | Value 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 |
|
---|
97 | Identifier 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 |
|
---|
111 | Value 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 |
|
---|
139 | void 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 |
|
---|
162 | bool 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 | }
|
---|