KJS-API
kjsobject.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00030 #include <kdebug.h>
00031 #include <QDateTime>
00032
00033 using namespace KJS;
00034
00035 KJSObject::KJSObject()
00036 : hnd(JSVALUE_HANDLE(new JSObject()))
00037 {
00038 gcProtect(JSVALUE(this));
00039 }
00040
00041 KJSObject::KJSObject(const KJSObject& o)
00042 : hnd(o.hnd)
00043 {
00044 gcProtectNullTolerant(JSVALUE(this));
00045 }
00046
00047 KJSObject& KJSObject::operator=(const KJSObject& o)
00048 {
00049 gcUnprotectNullTolerant(JSVALUE(this));
00050
00051 hnd = o.hnd;
00052 gcProtectNullTolerant(JSVALUE(this));
00053
00054 return *this;
00055 }
00056
00057 KJSObject::~KJSObject()
00058 {
00059 gcUnprotectNullTolerant(JSVALUE(this));
00060 }
00061
00062 KJSNull::KJSNull()
00063 : KJSObject(JSVALUE_HANDLE(jsNull()))
00064 {
00065 }
00066
00067 KJSUndefined::KJSUndefined()
00068 : KJSObject(JSVALUE_HANDLE(jsUndefined()))
00069 {
00070 }
00071
00072 KJSBoolean::KJSBoolean(bool b)
00073 : KJSObject(JSVALUE_HANDLE(jsBoolean(b)))
00074 {
00075 }
00076
00077 KJSNumber::KJSNumber(double d)
00078 : KJSObject(JSVALUE_HANDLE(jsNumber(d)))
00079 {
00080 gcProtect(JSVALUE(this));
00081 }
00082
00083 KJSString::KJSString(const QString& s)
00084 : KJSObject(JSVALUE_HANDLE(jsString(toUString(s))))
00085 {
00086 gcProtect(JSVALUE(this));
00087 }
00088
00089 KJSString::KJSString(const char* s)
00090 : KJSObject(JSVALUE_HANDLE(jsString(s)))
00091 {
00092 gcProtect(JSVALUE(this));
00093 }
00094
00095 static JSValue* constructArrayHelper(ExecState* exec, int len)
00096 {
00097 JSObject* builtinArray = exec->lexicalInterpreter()->builtinArray();
00098 JSObject* newArray = builtinArray->construct(exec, List());
00099 newArray->put(exec, exec->propertyNames().length, jsNumber(len),
00100 DontDelete|ReadOnly|DontEnum);
00101 return newArray;
00102 }
00103
00104 KJSArray::KJSArray(KJSContext* ctx, int len)
00105 : KJSObject(JSVALUE_HANDLE(constructArrayHelper(EXECSTATE(ctx), len)))
00106
00107 {
00108 gcProtect(JSVALUE(this));
00109 }
00110
00111 static JSValue* constructDateHelper(KJSContext* ctx, const QDateTime& dt)
00112 {
00113 kWarning() << "converDateTimeHelper() not implemented, yet";
00114
00115
00116
00117 return jsNumber(42.0);
00118 }
00119
00120
00121 KJSDate::KJSDate(KJSContext* ctx, const QDateTime& dt)
00122 : KJSObject(JSVALUE_HANDLE(constructDateHelper(ctx, dt)))
00123 {
00124 gcProtect(JSVALUE(this));
00125 }
00126
00127 bool KJSObject::isUndefined() const
00128 {
00129 return JSVALUE(this)->isUndefined();
00130 }
00131
00132 bool KJSObject::isNull() const
00133 {
00134 return JSVALUE(this)->isNull();
00135 }
00136
00137 bool KJSObject::isBoolean() const
00138 {
00139 return JSVALUE(this)->isBoolean();
00140 }
00141
00142 bool KJSObject::isNumber() const
00143 {
00144 return JSVALUE(this)->isNumber();
00145 }
00146
00147 bool KJSObject::isString() const
00148 {
00149 return JSVALUE(this)->isString();
00150 }
00151
00152 bool KJSObject::isObject() const
00153 {
00154 return JSVALUE(this)->isObject();
00155 }
00156
00157 bool KJSObject::toBoolean(KJSContext* ctx)
00158 {
00159 ExecState* exec = EXECSTATE(ctx);
00160 assert(exec);
00161 return JSVALUE(this)->toBoolean(exec);
00162 }
00163
00164 double KJSObject::toNumber(KJSContext* ctx)
00165 {
00166 ExecState* exec = EXECSTATE(ctx);
00167 assert(exec);
00168 return JSVALUE(this)->toNumber(exec);
00169 }
00170
00171 int KJSObject::toInt32(KJSContext* ctx)
00172 {
00173 ExecState* exec = EXECSTATE(ctx);
00174 assert(exec);
00175 return JSVALUE(this)->toInt32(exec);
00176 }
00177
00178 QString KJSObject::toString(KJSContext* ctx)
00179 {
00180 ExecState* exec = EXECSTATE(ctx);
00181 assert(exec);
00182 return toQString(JSVALUE(this)->toString(exec));
00183 }
00184
00185 KJSObject KJSObject::property(KJSContext* ctx, const QString& name)
00186 {
00187 JSValue* v = JSVALUE(this);
00188 assert(v);
00189
00190 #if 0
00191
00192 if (v == jsUndefined || v == jsNull())
00193 return KJSUndefined();
00194 #endif
00195
00196 ExecState* exec = EXECSTATE(ctx);
00197 JSObject* o = v->toObject(exec);
00198 JSValue* res = o->get(exec, toIdentifier(name));
00199
00200 return KJSObject(JSVALUE_HANDLE(res));
00201 }
00202
00203 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
00204 const KJSObject& value)
00205 {
00206 JSValue* v = JSVALUE(this);
00207 assert(v);
00208
00209 ExecState* exec = EXECSTATE(ctx);
00210 JSObject* o = v->toObject(exec);
00211 o->put(exec, toIdentifier(name), JSVALUE(&value));
00212 }
00213
00214 void KJSObject::setProperty(KJSContext* ctx, const QString& name, bool value)
00215 {
00216 setProperty(ctx, name, KJSBoolean(value));
00217 }
00218
00219 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
00220 double value)
00221 {
00222 setProperty(ctx, name, KJSNumber(value));
00223 }
00224
00225 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
00226 int value)
00227 {
00228 setProperty(ctx, name, KJSNumber(double(value)));
00229 }
00230
00231 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
00232 const QString &value)
00233 {
00234 setProperty(ctx, name, KJSString(value));
00235 }
00236
00237 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
00238 const char* value)
00239 {
00240 setProperty(ctx, name, KJSString(value));
00241 }
00242
00243 KJSGlobalObject::KJSGlobalObject()
00244 {
00245 }