• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KJS-API

  • sources
  • kde-4.12
  • kdelibs
  • kjs
  • api
kjsobject.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 2008 Harri Porten (porten@kde.org)
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 <kdebug.h>
32 #include <QDateTime>
33 
34 using namespace KJS;
35 
36 KJSObject::KJSObject()
37  : hnd(JSVALUE_HANDLE(new JSObject()))
38 {
39  gcProtect(JSVALUE(this));
40 }
41 
42 KJSObject::KJSObject(const KJSObject& o)
43  : hnd(o.hnd)
44 {
45  gcProtectNullTolerant(JSVALUE(this));
46 }
47 
48 KJSObject& KJSObject::operator=(const KJSObject& o)
49 {
50  gcUnprotectNullTolerant(JSVALUE(this));
51 
52  hnd = o.hnd;
53  gcProtectNullTolerant(JSVALUE(this));
54 
55  return *this;
56 }
57 
58 KJSObject::~KJSObject()
59 {
60  gcUnprotectNullTolerant(JSVALUE(this));
61 }
62 
63 KJSNull::KJSNull()
64  : KJSObject(JSVALUE_HANDLE(jsNull()))
65 {
66 }
67 
68 KJSUndefined::KJSUndefined()
69  : KJSObject(JSVALUE_HANDLE(jsUndefined()))
70 {
71 }
72 
73 KJSBoolean::KJSBoolean(bool b)
74  : KJSObject(JSVALUE_HANDLE(jsBoolean(b)))
75 {
76 }
77 
78 KJSNumber::KJSNumber(double d)
79  : KJSObject(JSVALUE_HANDLE(jsNumber(d)))
80 {
81  gcProtect(JSVALUE(this));
82 }
83 
84 KJSString::KJSString(const QString& s)
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 
105 KJSArray::KJSArray(KJSContext* ctx, int len)
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  kWarning() << "converDateTimeHelper() not implemented, yet";
117 
118  // ### make call into data_object.cpp
119 
120  return jsNumber(42.0);
121 }
122 
123 
124 KJSDate::KJSDate(KJSContext* ctx, const QDateTime& dt)
125  : KJSObject(JSVALUE_HANDLE(constructDateHelper(ctx, dt)))
126 {
127  gcProtect(JSVALUE(this));
128 }
129 
130 bool KJSObject::isUndefined() const
131 {
132  return JSVALUE(this)->isUndefined();
133 }
134 
135 bool KJSObject::isNull() const
136 {
137  return JSVALUE(this)->isNull();
138 }
139 
140 bool KJSObject::isBoolean() const
141 {
142  return JSVALUE(this)->isBoolean();
143 }
144 
145 bool KJSObject::isNumber() const
146 {
147  return JSVALUE(this)->isNumber();
148 }
149 
150 bool KJSObject::isString() const
151 {
152  return JSVALUE(this)->isString();
153 }
154 
155 bool KJSObject::isObject() const
156 {
157  return JSVALUE(this)->isObject();
158 }
159 
160 bool KJSObject::toBoolean(KJSContext* ctx)
161 {
162  ExecState* exec = EXECSTATE(ctx);
163  assert(exec);
164  return JSVALUE(this)->toBoolean(exec);
165 }
166 
167 double KJSObject::toNumber(KJSContext* ctx)
168 {
169  ExecState* exec = EXECSTATE(ctx);
170  assert(exec);
171  return JSVALUE(this)->toNumber(exec);
172 }
173 
174 int KJSObject::toInt32(KJSContext* ctx)
175 {
176  ExecState* exec = EXECSTATE(ctx);
177  assert(exec);
178  return JSVALUE(this)->toInt32(exec);
179 }
180 
181 QString KJSObject::toString(KJSContext* ctx)
182 {
183  ExecState* exec = EXECSTATE(ctx);
184  assert(exec);
185  return toQString(JSVALUE(this)->toString(exec));
186 }
187 
188 KJSObject KJSObject::property(KJSContext* ctx, const QString& name)
189 {
190  JSValue* v = JSVALUE(this);
191  assert(v);
192 
193 #if 0
194  // would normally throw an exception
195  if (v == jsUndefined || v == jsNull())
196  return KJSUndefined();
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 
206 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
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 
222 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
223  double value)
224 {
225  setProperty(ctx, name, KJSNumber(value));
226 }
227 
228 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
229  int value)
230 {
231  setProperty(ctx, name, KJSNumber(double(value)));
232 }
233 
234 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
235  const QString &value)
236 {
237  setProperty(ctx, name, KJSString(value));
238 }
239 
240 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
241  const char* value)
242 {
243  setProperty(ctx, name, KJSString(value));
244 }
245 
246 KJSGlobalObject::KJSGlobalObject(): KJSObject(JSVALUE_HANDLE(new JSGlobalObject()))
247 {
248 }
KJSObject::isBoolean
bool isBoolean() const
Returns whether this object is a boolean.
Definition: kjsobject.cpp:140
KJSObject::isString
bool isString() const
Returns whether this object is a string.
Definition: kjsobject.cpp:150
toQString
static QString toQString(const KJS::UString &s)
Definition: kjsprivate.h:60
kjsobject.h
KJSObject::KJSObject
KJSObject()
Constructs a JavaScript object of the generic Object type.
Definition: kjsobject.cpp:36
KJSObject::KJSUndefined
friend class KJSUndefined
Definition: kjsobject.h:51
JSVALUE
#define JSVALUE(h)
Definition: kjsprivate.h:31
KJSArray::KJSArray
KJSArray(KJSContext *ctx, int len=0)
Constructs an array object with the specified length.
Definition: kjsobject.cpp:105
toUString
static KJS::UString toUString(const QString &s)
Definition: kjsprivate.h:45
KJSObject::KJSNumber
friend class KJSNumber
Definition: kjsobject.h:53
KJSNull::KJSNull
KJSNull()
Constructs a null object.
Definition: kjsobject.cpp:63
EXECSTATE
#define EXECSTATE(ctx)
Definition: kjsprivate.h:34
KJSObject::isObject
bool isObject() const
Returns whether this object is a full blown object.
Definition: kjsobject.cpp:155
KJSContext
A class representing a JavaScript execution context.
Definition: kjscontext.h:39
KJSObject::toBoolean
bool toBoolean(KJSContext *ctx)
Returns this value converted to a boolean.
Definition: kjsobject.cpp:160
KJSObject::setProperty
void setProperty(KJSContext *ctx, const QString &name, const KJSObject &value)
Sets the specified property on this object.
Definition: kjsobject.cpp:206
KJSObject::toInt32
int toInt32(KJSContext *ctx)
Returns this value converted to a 32-bit integer number.
Definition: kjsobject.cpp:174
KJSObject::toNumber
double toNumber(KJSContext *ctx)
Returns this value converted to a number.
Definition: kjsobject.cpp:167
KJSObject::operator=
KJSObject & operator=(const KJSObject &o)
Assigns another JS object references to this one.
Definition: kjsobject.cpp:48
constructArrayHelper
static JSValue * constructArrayHelper(ExecState *exec, int len)
Definition: kjsobject.cpp:96
constructDateHelper
static JSValue * constructDateHelper(KJSContext *ctx, const QDateTime &dt)
Definition: kjsobject.cpp:112
KJSObject::isUndefined
bool isUndefined() const
Returns whether this object is undefined.
Definition: kjsobject.cpp:130
KJSObject::isNumber
bool isNumber() const
Returns whether this object is a number.
Definition: kjsobject.cpp:145
KJSObject::toString
QString toString(KJSContext *ctx)
Returns this value converted to a string.
Definition: kjsobject.cpp:181
toIdentifier
static KJS::Identifier toIdentifier(const QString &s)
Definition: kjsprivate.h:53
KJSObject
A class representing a JavaScript value.
Definition: kjsobject.h:48
KJSGlobalObject::KJSGlobalObject
KJSGlobalObject()
Constructs an empty global object.
Definition: kjsobject.cpp:246
KJSDate::KJSDate
KJSDate(KJSContext *ctx, const QDateTime &dt)
Constructs a date object from the specified date and time.
Definition: kjsobject.cpp:124
KJSObject::KJSBoolean
friend class KJSBoolean
Definition: kjsobject.h:52
KJSUndefined::KJSUndefined
KJSUndefined()
Constructs an undefined object.
Definition: kjsobject.cpp:68
kjscontext.h
KJSBoolean::KJSBoolean
KJSBoolean(bool b)
Constructs a boolean object.
Definition: kjsobject.cpp:73
KJSObject::KJSString
friend class KJSString
Definition: kjsobject.h:54
KJSString::KJSString
KJSString(const QString &s)
Constructs a string object.
Definition: kjsobject.cpp:84
KJSNumber::KJSNumber
KJSNumber(double d)
Constructs a number object.
Definition: kjsobject.cpp:78
KJSObject::property
KJSObject property(KJSContext *ctx, const QString &name)
Reads the specified property from this object.
Definition: kjsobject.cpp:188
JSVALUE_HANDLE
#define JSVALUE_HANDLE(v)
Definition: kjsprivate.h:30
kjsprivate.h
KJSObject::isNull
bool isNull() const
Returns whether this object is null.
Definition: kjsobject.cpp:135
KJSObject::~KJSObject
~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-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KJS-API

Skip menu "KJS-API"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal