• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.11.5 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 
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 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Sep 23 2014 09:57:25 by doxygen 1.8.3.1 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.11.5 API Reference

Skip menu "kdelibs-4.11.5 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