KJS-API
kjsobject.cpp
Go to the documentation of this file.
00001 /* 00002 * This file is part of the KDE libraries 00003 * Copyright (C) 2008 Harri Porten (porten@kde.org) 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 * 00020 */ 00021 00022 #include "kjsobject.h" 00023 #include "kjsprivate.h" 00024 #include "kjscontext.h" 00025 #include "kjs/value.h" 00026 #include "kjs/object.h" 00027 #include "kjs/protect.h" 00028 #include "kjs/ExecState.h" 00029 #include "kjs/JSVariableObject.h" 00030 00031 #include <kdebug.h> 00032 #include <QDateTime> 00033 00034 using namespace KJS; 00035 00036 KJSObject::KJSObject() 00037 : hnd(JSVALUE_HANDLE(new JSObject())) 00038 { 00039 gcProtect(JSVALUE(this)); 00040 } 00041 00042 KJSObject::KJSObject(const KJSObject& o) 00043 : hnd(o.hnd) 00044 { 00045 gcProtectNullTolerant(JSVALUE(this)); 00046 } 00047 00048 KJSObject& KJSObject::operator=(const KJSObject& o) 00049 { 00050 gcUnprotectNullTolerant(JSVALUE(this)); 00051 00052 hnd = o.hnd; 00053 gcProtectNullTolerant(JSVALUE(this)); 00054 00055 return *this; 00056 } 00057 00058 KJSObject::~KJSObject() 00059 { 00060 gcUnprotectNullTolerant(JSVALUE(this)); 00061 } 00062 00063 KJSNull::KJSNull() 00064 : KJSObject(JSVALUE_HANDLE(jsNull())) 00065 { 00066 } 00067 00068 KJSUndefined::KJSUndefined() 00069 : KJSObject(JSVALUE_HANDLE(jsUndefined())) 00070 { 00071 } 00072 00073 KJSBoolean::KJSBoolean(bool b) 00074 : KJSObject(JSVALUE_HANDLE(jsBoolean(b))) 00075 { 00076 } 00077 00078 KJSNumber::KJSNumber(double d) 00079 : KJSObject(JSVALUE_HANDLE(jsNumber(d))) 00080 { 00081 gcProtect(JSVALUE(this)); 00082 } 00083 00084 KJSString::KJSString(const QString& s) 00085 : KJSObject(JSVALUE_HANDLE(jsString(toUString(s)))) 00086 { 00087 gcProtect(JSVALUE(this)); 00088 } 00089 00090 KJSString::KJSString(const char* s) 00091 : KJSObject(JSVALUE_HANDLE(jsString(s))) 00092 { 00093 gcProtect(JSVALUE(this)); 00094 } 00095 00096 static JSValue* constructArrayHelper(ExecState* exec, int len) 00097 { 00098 JSObject* builtinArray = exec->lexicalInterpreter()->builtinArray(); 00099 JSObject* newArray = builtinArray->construct(exec, List()); 00100 newArray->put(exec, exec->propertyNames().length, jsNumber(len), 00101 DontDelete|ReadOnly|DontEnum); 00102 return newArray; 00103 } 00104 00105 KJSArray::KJSArray(KJSContext* ctx, int len) 00106 : KJSObject(JSVALUE_HANDLE(constructArrayHelper(EXECSTATE(ctx), len))) 00107 00108 { 00109 gcProtect(JSVALUE(this)); 00110 } 00111 00112 static JSValue* constructDateHelper(KJSContext* ctx, const QDateTime& dt) 00113 { 00114 Q_UNUSED(ctx); 00115 Q_UNUSED(dt); 00116 kWarning() << "converDateTimeHelper() not implemented, yet"; 00117 00118 // ### make call into data_object.cpp 00119 00120 return jsNumber(42.0); 00121 } 00122 00123 00124 KJSDate::KJSDate(KJSContext* ctx, const QDateTime& dt) 00125 : KJSObject(JSVALUE_HANDLE(constructDateHelper(ctx, dt))) 00126 { 00127 gcProtect(JSVALUE(this)); 00128 } 00129 00130 bool KJSObject::isUndefined() const 00131 { 00132 return JSVALUE(this)->isUndefined(); 00133 } 00134 00135 bool KJSObject::isNull() const 00136 { 00137 return JSVALUE(this)->isNull(); 00138 } 00139 00140 bool KJSObject::isBoolean() const 00141 { 00142 return JSVALUE(this)->isBoolean(); 00143 } 00144 00145 bool KJSObject::isNumber() const 00146 { 00147 return JSVALUE(this)->isNumber(); 00148 } 00149 00150 bool KJSObject::isString() const 00151 { 00152 return JSVALUE(this)->isString(); 00153 } 00154 00155 bool KJSObject::isObject() const 00156 { 00157 return JSVALUE(this)->isObject(); 00158 } 00159 00160 bool KJSObject::toBoolean(KJSContext* ctx) 00161 { 00162 ExecState* exec = EXECSTATE(ctx); 00163 assert(exec); 00164 return JSVALUE(this)->toBoolean(exec); 00165 } 00166 00167 double KJSObject::toNumber(KJSContext* ctx) 00168 { 00169 ExecState* exec = EXECSTATE(ctx); 00170 assert(exec); 00171 return JSVALUE(this)->toNumber(exec); 00172 } 00173 00174 int KJSObject::toInt32(KJSContext* ctx) 00175 { 00176 ExecState* exec = EXECSTATE(ctx); 00177 assert(exec); 00178 return JSVALUE(this)->toInt32(exec); 00179 } 00180 00181 QString KJSObject::toString(KJSContext* ctx) 00182 { 00183 ExecState* exec = EXECSTATE(ctx); 00184 assert(exec); 00185 return toQString(JSVALUE(this)->toString(exec)); 00186 } 00187 00188 KJSObject KJSObject::property(KJSContext* ctx, const QString& name) 00189 { 00190 JSValue* v = JSVALUE(this); 00191 assert(v); 00192 00193 #if 0 00194 // would normally throw an exception 00195 if (v == jsUndefined || v == jsNull()) 00196 return KJSUndefined(); 00197 #endif 00198 00199 ExecState* exec = EXECSTATE(ctx); 00200 JSObject* o = v->toObject(exec); 00201 JSValue* res = o->get(exec, toIdentifier(name)); 00202 00203 return KJSObject(JSVALUE_HANDLE(res)); 00204 } 00205 00206 void KJSObject::setProperty(KJSContext* ctx, const QString& name, 00207 const KJSObject& value) 00208 { 00209 JSValue* v = JSVALUE(this); 00210 assert(v); 00211 00212 ExecState* exec = EXECSTATE(ctx); 00213 JSObject* o = v->toObject(exec); 00214 o->put(exec, toIdentifier(name), JSVALUE(&value)); 00215 } 00216 00217 void KJSObject::setProperty(KJSContext* ctx, const QString& name, bool value) 00218 { 00219 setProperty(ctx, name, KJSBoolean(value)); 00220 } 00221 00222 void KJSObject::setProperty(KJSContext* ctx, const QString& name, 00223 double value) 00224 { 00225 setProperty(ctx, name, KJSNumber(value)); 00226 } 00227 00228 void KJSObject::setProperty(KJSContext* ctx, const QString& name, 00229 int value) 00230 { 00231 setProperty(ctx, name, KJSNumber(double(value))); 00232 } 00233 00234 void KJSObject::setProperty(KJSContext* ctx, const QString& name, 00235 const QString &value) 00236 { 00237 setProperty(ctx, name, KJSString(value)); 00238 } 00239 00240 void KJSObject::setProperty(KJSContext* ctx, const QString& name, 00241 const char* value) 00242 { 00243 setProperty(ctx, name, KJSString(value)); 00244 } 00245 00246 KJSGlobalObject::KJSGlobalObject(): KJSObject(JSVALUE_HANDLE(new JSGlobalObject())) 00247 { 00248 }
KDE 4.6 API Reference