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 #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 kWarning() << "converDateTimeHelper() not implemented, yet";
00115
00116
00117
00118 return jsNumber(42.0);
00119 }
00120
00121
00122 KJSDate::KJSDate(KJSContext* ctx, const QDateTime& dt)
00123 : KJSObject(JSVALUE_HANDLE(constructDateHelper(ctx, dt)))
00124 {
00125 gcProtect(JSVALUE(this));
00126 }
00127
00128 bool KJSObject::isUndefined() const
00129 {
00130 return JSVALUE(this)->isUndefined();
00131 }
00132
00133 bool KJSObject::isNull() const
00134 {
00135 return JSVALUE(this)->isNull();
00136 }
00137
00138 bool KJSObject::isBoolean() const
00139 {
00140 return JSVALUE(this)->isBoolean();
00141 }
00142
00143 bool KJSObject::isNumber() const
00144 {
00145 return JSVALUE(this)->isNumber();
00146 }
00147
00148 bool KJSObject::isString() const
00149 {
00150 return JSVALUE(this)->isString();
00151 }
00152
00153 bool KJSObject::isObject() const
00154 {
00155 return JSVALUE(this)->isObject();
00156 }
00157
00158 bool KJSObject::toBoolean(KJSContext* ctx)
00159 {
00160 ExecState* exec = EXECSTATE(ctx);
00161 assert(exec);
00162 return JSVALUE(this)->toBoolean(exec);
00163 }
00164
00165 double KJSObject::toNumber(KJSContext* ctx)
00166 {
00167 ExecState* exec = EXECSTATE(ctx);
00168 assert(exec);
00169 return JSVALUE(this)->toNumber(exec);
00170 }
00171
00172 int KJSObject::toInt32(KJSContext* ctx)
00173 {
00174 ExecState* exec = EXECSTATE(ctx);
00175 assert(exec);
00176 return JSVALUE(this)->toInt32(exec);
00177 }
00178
00179 QString KJSObject::toString(KJSContext* ctx)
00180 {
00181 ExecState* exec = EXECSTATE(ctx);
00182 assert(exec);
00183 return toQString(JSVALUE(this)->toString(exec));
00184 }
00185
00186 KJSObject KJSObject::property(KJSContext* ctx, const QString& name)
00187 {
00188 JSValue* v = JSVALUE(this);
00189 assert(v);
00190
00191 #if 0
00192
00193 if (v == jsUndefined || v == jsNull())
00194 return KJSUndefined();
00195 #endif
00196
00197 ExecState* exec = EXECSTATE(ctx);
00198 JSObject* o = v->toObject(exec);
00199 JSValue* res = o->get(exec, toIdentifier(name));
00200
00201 return KJSObject(JSVALUE_HANDLE(res));
00202 }
00203
00204 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
00205 const KJSObject& value)
00206 {
00207 JSValue* v = JSVALUE(this);
00208 assert(v);
00209
00210 ExecState* exec = EXECSTATE(ctx);
00211 JSObject* o = v->toObject(exec);
00212 o->put(exec, toIdentifier(name), JSVALUE(&value));
00213 }
00214
00215 void KJSObject::setProperty(KJSContext* ctx, const QString& name, bool value)
00216 {
00217 setProperty(ctx, name, KJSBoolean(value));
00218 }
00219
00220 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
00221 double value)
00222 {
00223 setProperty(ctx, name, KJSNumber(value));
00224 }
00225
00226 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
00227 int value)
00228 {
00229 setProperty(ctx, name, KJSNumber(double(value)));
00230 }
00231
00232 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
00233 const QString &value)
00234 {
00235 setProperty(ctx, name, KJSString(value));
00236 }
00237
00238 void KJSObject::setProperty(KJSContext* ctx, const QString& name,
00239 const char* value)
00240 {
00241 setProperty(ctx, name, KJSString(value));
00242 }
00243
00244 KJSGlobalObject::KJSGlobalObject(): KJSObject(JSVALUE_HANDLE(new JSGlobalObject()))
00245 {
00246 }