KJS

kjsobject.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 2008 Harri Porten ([email protected])
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 "kjsobject.h"
23 #include "kjsprivate.h"
24 #include "kjscontext.h"
25 #include "kjs/value.h"
26 #include "kjs/object.h"
27 #include "kjs/protect.h"
28 #include "kjs/ExecState.h"
29 #include "kjs/JSVariableObject.h"
30 
31 #include <QDebug>
32 #include <QDateTime>
33 
34 using namespace KJS;
35 
37  : hnd(JSVALUE_HANDLE(new JSObject()))
38 {
39  gcProtect(JSVALUE(this));
40 }
41 
43  : hnd(o.hnd)
44 {
45  gcProtectNullTolerant(JSVALUE(this));
46 }
47 
49 {
50  gcUnprotectNullTolerant(JSVALUE(this));
51 
52  hnd = o.hnd;
53  gcProtectNullTolerant(JSVALUE(this));
54 
55  return *this;
56 }
57 
59 {
60  gcUnprotectNullTolerant(JSVALUE(this));
61 }
62 
64  : KJSObject(JSVALUE_HANDLE(jsNull()))
65 {
66 }
67 
69  : KJSObject(JSVALUE_HANDLE(jsUndefined()))
70 {
71 }
72 
74  : KJSObject(JSVALUE_HANDLE(jsBoolean(b)))
75 {
76 }
77 
79  : KJSObject(JSVALUE_HANDLE(jsNumber(d)))
80 {
81  gcProtect(JSVALUE(this));
82 }
83 
85  : KJSObject(JSVALUE_HANDLE(jsString(toUString(s))))
86 {
87  gcProtect(JSVALUE(this));
88 }
89 
90 KJSString::KJSString(const char *s)
91  : KJSObject(JSVALUE_HANDLE(jsString(s)))
92 {
93  gcProtect(JSVALUE(this));
94 }
95 
96 static JSValue *constructArrayHelper(ExecState *exec, int len)
97 {
98  JSObject *builtinArray = exec->lexicalInterpreter()->builtinArray();
99  JSObject *newArray = builtinArray->construct(exec, List());
100  newArray->put(exec, exec->propertyNames().length, jsNumber(len),
101  DontDelete | ReadOnly | DontEnum);
102  return newArray;
103 }
104 
106  : KJSObject(JSVALUE_HANDLE(constructArrayHelper(EXECSTATE(ctx), len)))
107 
108 {
109  gcProtect(JSVALUE(this));
110 }
111 
112 static JSValue *constructDateHelper(KJSContext *ctx, const QDateTime &dt)
113 {
114  Q_UNUSED(ctx);
115  Q_UNUSED(dt);
116  qWarning() << "converDateTimeHelper() not implemented, yet";
117 
118  // ### make call into data_object.cpp
119 
120  return jsNumber(42.0);
121 }
122 
124  : KJSObject(JSVALUE_HANDLE(constructDateHelper(ctx, dt)))
125 {
126  gcProtect(JSVALUE(this));
127 }
128 
130 {
131  return JSVALUE(this)->isUndefined();
132 }
133 
134 bool KJSObject::isNull() const
135 {
136  return JSVALUE(this)->isNull();
137 }
138 
140 {
141  return JSVALUE(this)->isBoolean();
142 }
143 
145 {
146  return JSVALUE(this)->isNumber();
147 }
148 
150 {
151  return JSVALUE(this)->isString();
152 }
153 
155 {
156  return JSVALUE(this)->isObject();
157 }
158 
160 {
161  ExecState *exec = EXECSTATE(ctx);
162  assert(exec);
163  return JSVALUE(this)->toBoolean(exec);
164 }
165 
167 {
168  ExecState *exec = EXECSTATE(ctx);
169  assert(exec);
170  return JSVALUE(this)->toNumber(exec);
171 }
172 
174 {
175  ExecState *exec = EXECSTATE(ctx);
176  assert(exec);
177  return JSVALUE(this)->toInt32(exec);
178 }
179 
181 {
182  ExecState *exec = EXECSTATE(ctx);
183  assert(exec);
184  return toQString(JSVALUE(this)->toString(exec));
185 }
186 
188 {
189  JSValue *v = JSVALUE(this);
190  assert(v);
191 
192 #if 0
193  // would normally throw an exception
194  if (v == jsUndefined || v == jsNull()) {
195  return KJSUndefined();
196  }
197 #endif
198 
199  ExecState *exec = EXECSTATE(ctx);
200  JSObject *o = v->toObject(exec);
201  JSValue *res = o->get(exec, toIdentifier(name));
202 
203  return KJSObject(JSVALUE_HANDLE(res));
204 }
205 
207  const KJSObject &value)
208 {
209  JSValue *v = JSVALUE(this);
210  assert(v);
211 
212  ExecState *exec = EXECSTATE(ctx);
213  JSObject *o = v->toObject(exec);
214  o->put(exec, toIdentifier(name), JSVALUE(&value));
215 }
216 
217 void KJSObject::setProperty(KJSContext *ctx, const QString &name, bool value)
218 {
219  setProperty(ctx, name, KJSBoolean(value));
220 }
221 
223  double value)
224 {
225  setProperty(ctx, name, KJSNumber(value));
226 }
227 
229  int value)
230 {
231  setProperty(ctx, name, KJSNumber(double(value)));
232 }
233 
235  const QString &value)
236 {
237  setProperty(ctx, name, KJSString(value));
238 }
239 
241  const char *value)
242 {
243  setProperty(ctx, name, KJSString(value));
244 }
245 
246 KJSGlobalObject::KJSGlobalObject(): KJSObject(JSVALUE_HANDLE(new JSGlobalObject()))
247 {
248 }
bool isBoolean() const
Returns whether this object is a boolean.
Definition: kjsobject.cpp:139
bool isString() const
Returns whether this object is a string.
Definition: kjsobject.cpp:149
KJSObject()
Constructs a JavaScript object of the generic Object type.
Definition: kjsobject.cpp:36
KJSArray(KJSContext *ctx, int len=0)
Constructs an array object with the specified length.
Definition: kjsobject.cpp:105
A class representing an undefined JavaScript value.
Definition: kjsobject.h:187
JSObject * builtinArray() const
Returns the builtin "Array" object.
KJSNull()
Constructs a null object.
Definition: kjsobject.cpp:63
bool isObject() const
Returns whether this object is a full blown object.
Definition: kjsobject.cpp:154
A class representing a JavaScript execution context.
Definition: kjscontext.h:41
bool toBoolean(KJSContext *ctx)
Returns this value converted to a boolean.
Definition: kjsobject.cpp:159
void setProperty(KJSContext *ctx, const QString &name, const KJSObject &value)
Sets the specified property on this object.
Definition: kjsobject.cpp:206
int toInt32(KJSContext *ctx)
Returns this value converted to a 32-bit integer number.
Definition: kjsobject.cpp:173
double toNumber(KJSContext *ctx)
Returns this value converted to a number.
Definition: kjsobject.cpp:166
KJSObject & operator=(const KJSObject &o)
Assigns another JS object references to this one.
Definition: kjsobject.cpp:48
A class representing a JavaScript number value.
Definition: kjsobject.h:215
bool isUndefined() const
Returns whether this object is undefined.
Definition: kjsobject.cpp:129
bool isNumber() const
Returns whether this object is a number.
Definition: kjsobject.cpp:144
QString toString(KJSContext *ctx)
Returns this value converted to a string.
Definition: kjsobject.cpp:180
JSValue is the base type for all primitives (Undefined, Null, Boolean, String, Number) and objects in...
Definition: value.h:58
A class representing a JavaScript value.
Definition: kjsobject.h:48
KJSGlobalObject()
Constructs an empty global object.
Definition: kjsobject.cpp:246
KJSDate(KJSContext *ctx, const QDateTime &dt)
Constructs a date object from the specified date and time.
Definition: kjsobject.cpp:123
KJSUndefined()
Constructs an undefined object.
Definition: kjsobject.cpp:68
KJSBoolean(bool b)
Constructs a boolean object.
Definition: kjsobject.cpp:73
A class representing a JavaScript string value.
Definition: kjsobject.h:229
Native list type.
Definition: list.h:52
KJSString(const QString &s)
Constructs a string object.
Definition: kjsobject.cpp:84
KJSNumber(double d)
Constructs a number object.
Definition: kjsobject.cpp:78
KJSObject property(KJSContext *ctx, const QString &name)
Reads the specified property from this object.
Definition: kjsobject.cpp:187
Interpreter * lexicalInterpreter() const
Returns the interpreter associated with the current scope&#39;s global object.
Definition: ExecState.cpp:35
A class representing a boolean JavaScript value.
Definition: kjsobject.h:201
Represents the current state of script execution.
Definition: ExecState.h:53
bool isNull() const
Returns whether this object is null.
Definition: kjsobject.cpp:134
~KJSObject()
Destructs this object freeing any references it might have held.
Definition: kjsobject.cpp:58
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Sep 23 2020 23:00:47 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.