fix logic
[personal-kdelibs.git] / kjs / api / kjsobject.cpp
blob45fe0a55a78ec2bc99b8dd08908258b5796e074e
1 /*
2 * This file is part of the KDE libraries
3 * Copyright (C) 2008 Harri Porten (porten@kde.org)
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.
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.
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.
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"
31 #include <kdebug.h>
32 #include <QDateTime>
34 using namespace KJS;
36 KJSObject::KJSObject()
37 : hnd(JSVALUE_HANDLE(new JSObject()))
39 gcProtect(JSVALUE(this));
42 KJSObject::KJSObject(const KJSObject& o)
43 : hnd(o.hnd)
45 gcProtectNullTolerant(JSVALUE(this));
48 KJSObject& KJSObject::operator=(const KJSObject& o)
50 gcUnprotectNullTolerant(JSVALUE(this));
52 hnd = o.hnd;
53 gcProtectNullTolerant(JSVALUE(this));
55 return *this;
58 KJSObject::~KJSObject()
60 gcUnprotectNullTolerant(JSVALUE(this));
63 KJSNull::KJSNull()
64 : KJSObject(JSVALUE_HANDLE(jsNull()))
68 KJSUndefined::KJSUndefined()
69 : KJSObject(JSVALUE_HANDLE(jsUndefined()))
73 KJSBoolean::KJSBoolean(bool b)
74 : KJSObject(JSVALUE_HANDLE(jsBoolean(b)))
78 KJSNumber::KJSNumber(double d)
79 : KJSObject(JSVALUE_HANDLE(jsNumber(d)))
81 gcProtect(JSVALUE(this));
84 KJSString::KJSString(const QString& s)
85 : KJSObject(JSVALUE_HANDLE(jsString(toUString(s))))
87 gcProtect(JSVALUE(this));
90 KJSString::KJSString(const char* s)
91 : KJSObject(JSVALUE_HANDLE(jsString(s)))
93 gcProtect(JSVALUE(this));
96 static JSValue* constructArrayHelper(ExecState* exec, int len)
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;
105 KJSArray::KJSArray(KJSContext* ctx, int len)
106 : KJSObject(JSVALUE_HANDLE(constructArrayHelper(EXECSTATE(ctx), len)))
109 gcProtect(JSVALUE(this));
112 static JSValue* constructDateHelper(KJSContext* ctx, const QDateTime& dt)
114 kWarning() << "converDateTimeHelper() not implemented, yet";
116 // ### make call into data_object.cpp
118 return jsNumber(42.0);
122 KJSDate::KJSDate(KJSContext* ctx, const QDateTime& dt)
123 : KJSObject(JSVALUE_HANDLE(constructDateHelper(ctx, dt)))
125 gcProtect(JSVALUE(this));
128 bool KJSObject::isUndefined() const
130 return JSVALUE(this)->isUndefined();
133 bool KJSObject::isNull() const
135 return JSVALUE(this)->isNull();
138 bool KJSObject::isBoolean() const
140 return JSVALUE(this)->isBoolean();
143 bool KJSObject::isNumber() const
145 return JSVALUE(this)->isNumber();
148 bool KJSObject::isString() const
150 return JSVALUE(this)->isString();
153 bool KJSObject::isObject() const
155 return JSVALUE(this)->isObject();
158 bool KJSObject::toBoolean(KJSContext* ctx)
160 ExecState* exec = EXECSTATE(ctx);
161 assert(exec);
162 return JSVALUE(this)->toBoolean(exec);
165 double KJSObject::toNumber(KJSContext* ctx)
167 ExecState* exec = EXECSTATE(ctx);
168 assert(exec);
169 return JSVALUE(this)->toNumber(exec);
172 int KJSObject::toInt32(KJSContext* ctx)
174 ExecState* exec = EXECSTATE(ctx);
175 assert(exec);
176 return JSVALUE(this)->toInt32(exec);
179 QString KJSObject::toString(KJSContext* ctx)
181 ExecState* exec = EXECSTATE(ctx);
182 assert(exec);
183 return toQString(JSVALUE(this)->toString(exec));
186 KJSObject KJSObject::property(KJSContext* ctx, const QString& name)
188 JSValue* v = JSVALUE(this);
189 assert(v);
191 #if 0
192 // would normally throw an exception
193 if (v == jsUndefined || v == jsNull())
194 return KJSUndefined();
195 #endif
197 ExecState* exec = EXECSTATE(ctx);
198 JSObject* o = v->toObject(exec);
199 JSValue* res = o->get(exec, toIdentifier(name));
201 return KJSObject(JSVALUE_HANDLE(res));
204 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
205 const KJSObject& value)
207 JSValue* v = JSVALUE(this);
208 assert(v);
210 ExecState* exec = EXECSTATE(ctx);
211 JSObject* o = v->toObject(exec);
212 o->put(exec, toIdentifier(name), JSVALUE(&value));
215 void KJSObject::setProperty(KJSContext* ctx, const QString& name, bool value)
217 setProperty(ctx, name, KJSBoolean(value));
220 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
221 double value)
223 setProperty(ctx, name, KJSNumber(value));
226 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
227 int value)
229 setProperty(ctx, name, KJSNumber(double(value)));
232 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
233 const QString &value)
235 setProperty(ctx, name, KJSString(value));
238 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
239 const char* value)
241 setProperty(ctx, name, KJSString(value));
244 KJSGlobalObject::KJSGlobalObject(): KJSObject(JSVALUE_HANDLE(new JSGlobalObject()))