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

KJS-API

  • 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
34using namespace KJS;
35
36KJSObject::KJSObject()
37 : hnd(JSVALUE_HANDLE(new JSObject()))
38{
39 gcProtect(JSVALUE(this));
40}
41
42KJSObject::KJSObject(const KJSObject& o)
43 : hnd(o.hnd)
44{
45 gcProtectNullTolerant(JSVALUE(this));
46}
47
48KJSObject& 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
58KJSObject::~KJSObject()
59{
60 gcUnprotectNullTolerant(JSVALUE(this));
61}
62
63KJSNull::KJSNull()
64 : KJSObject(JSVALUE_HANDLE(jsNull()))
65{
66}
67
68KJSUndefined::KJSUndefined()
69 : KJSObject(JSVALUE_HANDLE(jsUndefined()))
70{
71}
72
73KJSBoolean::KJSBoolean(bool b)
74 : KJSObject(JSVALUE_HANDLE(jsBoolean(b)))
75{
76}
77
78KJSNumber::KJSNumber(double d)
79 : KJSObject(JSVALUE_HANDLE(jsNumber(d)))
80{
81 gcProtect(JSVALUE(this));
82}
83
84KJSString::KJSString(const QString& s)
85 : KJSObject(JSVALUE_HANDLE(jsString(toUString(s))))
86{
87 gcProtect(JSVALUE(this));
88}
89
90KJSString::KJSString(const char* s)
91 : KJSObject(JSVALUE_HANDLE(jsString(s)))
92{
93 gcProtect(JSVALUE(this));
94}
95
96static 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
105KJSArray::KJSArray(KJSContext* ctx, int len)
106 : KJSObject(JSVALUE_HANDLE(constructArrayHelper(EXECSTATE(ctx), len)))
107
108{
109 gcProtect(JSVALUE(this));
110}
111
112static 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
124KJSDate::KJSDate(KJSContext* ctx, const QDateTime& dt)
125 : KJSObject(JSVALUE_HANDLE(constructDateHelper(ctx, dt)))
126{
127 gcProtect(JSVALUE(this));
128}
129
130bool KJSObject::isUndefined() const
131{
132 return JSVALUE(this)->isUndefined();
133}
134
135bool KJSObject::isNull() const
136{
137 return JSVALUE(this)->isNull();
138}
139
140bool KJSObject::isBoolean() const
141{
142 return JSVALUE(this)->isBoolean();
143}
144
145bool KJSObject::isNumber() const
146{
147 return JSVALUE(this)->isNumber();
148}
149
150bool KJSObject::isString() const
151{
152 return JSVALUE(this)->isString();
153}
154
155bool KJSObject::isObject() const
156{
157 return JSVALUE(this)->isObject();
158}
159
160bool KJSObject::toBoolean(KJSContext* ctx)
161{
162 ExecState* exec = EXECSTATE(ctx);
163 assert(exec);
164 return JSVALUE(this)->toBoolean(exec);
165}
166
167double KJSObject::toNumber(KJSContext* ctx)
168{
169 ExecState* exec = EXECSTATE(ctx);
170 assert(exec);
171 return JSVALUE(this)->toNumber(exec);
172}
173
174int KJSObject::toInt32(KJSContext* ctx)
175{
176 ExecState* exec = EXECSTATE(ctx);
177 assert(exec);
178 return JSVALUE(this)->toInt32(exec);
179}
180
181QString KJSObject::toString(KJSContext* ctx)
182{
183 ExecState* exec = EXECSTATE(ctx);
184 assert(exec);
185 return toQString(JSVALUE(this)->toString(exec));
186}
187
188KJSObject 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
206void 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
217void KJSObject::setProperty(KJSContext* ctx, const QString& name, bool value)
218{
219 setProperty(ctx, name, KJSBoolean(value));
220}
221
222void KJSObject::setProperty(KJSContext* ctx, const QString& name,
223 double value)
224{
225 setProperty(ctx, name, KJSNumber(value));
226}
227
228void KJSObject::setProperty(KJSContext* ctx, const QString& name,
229 int value)
230{
231 setProperty(ctx, name, KJSNumber(double(value)));
232}
233
234void KJSObject::setProperty(KJSContext* ctx, const QString& name,
235 const QString &value)
236{
237 setProperty(ctx, name, KJSString(value));
238}
239
240void KJSObject::setProperty(KJSContext* ctx, const QString& name,
241 const char* value)
242{
243 setProperty(ctx, name, KJSString(value));
244}
245
246KJSGlobalObject::KJSGlobalObject(): KJSObject(JSVALUE_HANDLE(new JSGlobalObject()))
247{
248}
KJSContext
A class representing a JavaScript execution context.
Definition: kjscontext.h:40
KJSGlobalObject::KJSGlobalObject
KJSGlobalObject()
Constructs an empty global object.
Definition: kjsobject.cpp:246
KJSNull::KJSNull
KJSNull()
Constructs a null object.
Definition: kjsobject.cpp:63
KJSObject
A class representing a JavaScript value.
Definition: kjsobject.h:49
KJSObject::setProperty
void setProperty(KJSContext *ctx, const QString &name, const KJSObject &value)
Sets the specified property on this object.
Definition: kjsobject.cpp:206
KJSObject::KJSArray
friend class KJSArray
Definition: kjsobject.h:55
KJSObject::KJSUndefined
friend class KJSUndefined
Definition: kjsobject.h:51
KJSObject::isBoolean
bool isBoolean() const
Returns whether this object is a boolean.
Definition: kjsobject.cpp:140
KJSObject::KJSNumber
friend class KJSNumber
Definition: kjsobject.h:53
KJSObject::toBoolean
bool toBoolean(KJSContext *ctx)
Returns this value converted to a boolean.
Definition: kjsobject.cpp:160
KJSObject::property
KJSObject property(KJSContext *ctx, const QString &name)
Reads the specified property from this object.
Definition: kjsobject.cpp:188
KJSObject::KJSDate
friend class KJSDate
Definition: kjsobject.h:56
KJSObject::KJSObject
KJSObject()
Constructs a JavaScript object of the generic Object type.
Definition: kjsobject.cpp:36
KJSObject::toInt32
int toInt32(KJSContext *ctx)
Returns this value converted to a 32-bit integer number.
Definition: kjsobject.cpp:174
KJSObject::operator=
KJSObject & operator=(const KJSObject &o)
Assigns another JS object references to this one.
Definition: kjsobject.cpp:48
KJSObject::~KJSObject
~KJSObject()
Destructs this object freeing any references it might have held.
Definition: kjsobject.cpp:58
KJSObject::isObject
bool isObject() const
Returns whether this object is a full blown object.
Definition: kjsobject.cpp:155
KJSObject::isNull
bool isNull() const
Returns whether this object is null.
Definition: kjsobject.cpp:135
KJSObject::isUndefined
bool isUndefined() const
Returns whether this object is undefined.
Definition: kjsobject.cpp:130
KJSObject::toString
QString toString(KJSContext *ctx)
Returns this value converted to a string.
Definition: kjsobject.cpp:181
KJSObject::toNumber
double toNumber(KJSContext *ctx)
Returns this value converted to a number.
Definition: kjsobject.cpp:167
KJSObject::isNumber
bool isNumber() const
Returns whether this object is a number.
Definition: kjsobject.cpp:145
KJSObject::KJSString
friend class KJSString
Definition: kjsobject.h:54
KJSObject::KJSBoolean
friend class KJSBoolean
Definition: kjsobject.h:52
KJSObject::isString
bool isString() const
Returns whether this object is a string.
Definition: kjsobject.cpp:150
KJSUndefined::KJSUndefined
KJSUndefined()
Constructs an undefined object.
Definition: kjsobject.cpp:68
kjscontext.h
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.h
kjsprivate.h
toQString
static QString toQString(const KJS::UString &s)
Definition: kjsprivate.h:60
JSVALUE_HANDLE
#define JSVALUE_HANDLE(v)
Definition: kjsprivate.h:30
toIdentifier
static KJS::Identifier toIdentifier(const QString &s)
Definition: kjsprivate.h:53
toUString
static KJS::UString toUString(const QString &s)
Definition: kjsprivate.h:45
EXECSTATE
#define EXECSTATE(ctx)
Definition: kjsprivate.h:34
JSVALUE
#define JSVALUE(h)
Definition: kjsprivate.h:31
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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