kjsembed
builtins.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org> 00003 Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com> 00004 Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org> 00005 Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us> 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 00021 */ 00022 00023 #include "builtins.h" 00024 00025 #include <QtCore/QCoreApplication> 00026 #include <QtCore/QFile> 00027 #include <QtGui/QMessageBox> 00028 #include <QtCore/QTextStream> 00029 #include <QtCore/QDebug> 00030 #include <QtCore/QMetaType> 00031 00032 #ifndef QT_ONLY 00033 #include <kstandarddirs.h> 00034 #endif // QT_ONLY 00035 00036 00037 #include "variant_binding.h" 00038 #include "object_binding.h" 00039 #include "static_binding.h" 00040 #include "kjsembed.h" 00041 00042 using namespace KJSEmbed; 00043 00044 KJS::JSValue *callExec( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args ) 00045 { 00046 Q_UNUSED(exec); 00047 Q_UNUSED(self); 00048 Q_UNUSED(args); 00049 return KJS::jsBoolean( QCoreApplication::exec() ); 00050 } 00051 00052 KJS::JSValue *callDump( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args ) 00053 { 00054 Q_UNUSED(self); 00055 if( args.size() == 1) 00056 { 00057 KJS::JSObject *object = args[0]->toObject(exec); 00058 Q_UNUSED(object); 00059 } 00060 return KJS::jsNull(); 00061 } 00062 00063 KJS::JSValue *callInclude( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args ) 00064 { 00065 Q_UNUSED(self); 00066 if( args.size() == 1) 00067 { 00068 KJS::UString filename = args[0]->toString(exec); 00069 qDebug() << "include: " << toQString(filename); 00070 00071 KJS::Completion c = Engine::runFile( exec->dynamicInterpreter(), filename ); 00072 00073 if ( c.complType() == KJS::Normal ) 00074 return KJS::jsNull(); 00075 00076 if (c.complType() == KJS::ReturnValue) 00077 { 00078 if (c.isValueCompletion()) 00079 return c.value(); 00080 00081 return KJS::jsNull(); 00082 } 00083 00084 if (c.complType() == KJS::Throw) 00085 { 00086 QString message = toQString(c.value()->toString(exec)); 00087 int line = c.value()->toObject(exec)->get(exec, "line")->toUInt32(exec); 00088 return throwError(exec, KJS::EvalError, 00089 toUString(i18n("Error encountered while processing include '%1' line %2: %3", toQString(filename), line, message))); 00090 } 00091 } 00092 else 00093 { 00094 return throwError(exec, KJS::URIError, 00095 toUString(i18n("include only takes 1 argument, not %1.", args.size()))); 00096 } 00097 00098 return KJS::jsNull(); 00099 } 00100 00101 #ifndef QT_ONLY 00102 00103 KJS::JSValue *callLibrary( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args ) 00104 { 00105 Q_UNUSED(self); 00106 if( args.size() == 1) 00107 { 00108 KJS::UString filename = args[0]->toString(exec); 00109 QString qualifiedFilename = KStandardDirs::locate( "scripts", toQString(filename) ); 00110 if ( !qualifiedFilename.isEmpty() ) 00111 { 00112 KJS::Completion c = Engine::runFile( exec->dynamicInterpreter(), toUString(qualifiedFilename) ); 00113 if ( c.complType() == KJS::Normal ) 00114 return KJS::jsNull(); 00115 00116 if (c.complType() == KJS::ReturnValue) 00117 { 00118 if (c.isValueCompletion()) 00119 return c.value(); 00120 00121 return KJS::jsNull(); 00122 } 00123 00124 if (c.complType() == KJS::Throw) 00125 { 00126 QString message = toQString(c.value()->toString(exec)); 00127 int line = c.value()->toObject(exec)->get(exec, "line")->toUInt32(exec); 00128 return throwError(exec, KJS::EvalError, 00129 toUString(i18n("Error encountered while processing include '%1' line %2: %3", toQString(filename), line, message))); 00130 } 00131 } 00132 else { 00133 QString msg = i18n( "File %1 not found.", toQString(filename) ); 00134 return throwError( exec, KJS::URIError, toUString(msg) ); 00135 } 00136 } 00137 else { 00138 return throwError(exec, KJS::URIError, 00139 toUString(i18n("library only takes 1 argument, not %1.", args.size()))); 00140 } 00141 00142 return KJS::jsNull(); 00143 } 00144 00145 #endif // QT_ONLY 00146 00147 KJS::JSValue *callAlert( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args ) 00148 { 00149 Q_UNUSED(self) 00150 if (args.size() == 1) 00151 { 00152 (*KJSEmbed::conerr()) << "callAlert"; 00153 QString message = toQString(args[0]->toString(exec)); 00154 QMessageBox::warning(0, i18n("Alert"), message, QMessageBox::Ok, QMessageBox::NoButton); 00155 } 00156 return KJS::jsNull(); 00157 } 00158 00159 KJS::JSValue *callConfirm( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args ) 00160 { 00161 Q_UNUSED(self) 00162 if (args.size() == 1) 00163 { 00164 QString message = toQString(args[0]->toString(exec)); 00165 int result = QMessageBox::question (0, i18n("Confirm"), message, QMessageBox::Yes, QMessageBox::No); 00166 if (result == QMessageBox::Yes) 00167 return KJS::jsBoolean(true); 00168 } 00169 return KJS::jsBoolean(false); 00170 } 00171 00172 KJS::JSValue *callIsVariantType( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args ) 00173 { 00174 Q_UNUSED(self) 00175 if (args.size() == 1) 00176 { 00177 QString thetypename = toQString(args[0]->toString(exec)); 00178 return KJS::jsBoolean( QMetaType::type( thetypename.toLatin1().data() ) ); 00179 } 00180 return KJS::jsBoolean(false); 00181 } 00182 00183 KJS::JSValue *callIsVariant( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args ) 00184 { 00185 Q_UNUSED(self) 00186 if (args.size() == 1) 00187 { 00188 KJS::JSObject *obj = args[0]->toObject(exec); 00189 if (obj->inherits(&VariantBinding::info)) 00190 { 00191 return KJS::jsBoolean(true); 00192 } 00193 } 00194 return KJS::jsBoolean(false); 00195 } 00196 00197 KJS::JSValue *callIsObject( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args ) 00198 { 00199 Q_UNUSED(self) 00200 if (args.size() == 1) 00201 { 00202 KJS::JSObject *obj = args[0]->toObject(exec); 00203 if (obj->inherits(&ObjectBinding::info)) 00204 { 00205 return KJS::jsBoolean(true); 00206 } 00207 } 00208 return KJS::jsBoolean(false); 00209 } 00210 00211 const Method BuiltinsFactory::BuiltinMethods[] = 00212 { 00213 {"exec", 0, KJS::DontDelete|KJS::ReadOnly, &callExec}, 00214 {"dump", 1, KJS::DontDelete|KJS::ReadOnly, &callDump}, 00215 {"include", 1, KJS::DontDelete|KJS::ReadOnly, &callInclude}, 00216 #ifndef QT_ONLY 00217 {"library", 1, KJS::DontDelete|KJS::ReadOnly, &callLibrary}, 00218 #endif // QT_ONLY 00219 {"alert", 1, KJS::DontDelete|KJS::ReadOnly, &callAlert}, 00220 {"confirm", 1, KJS::DontDelete|KJS::ReadOnly, &callConfirm}, 00221 {"isVariantType", 1, KJS::DontDelete|KJS::ReadOnly, &callIsVariantType}, 00222 {"isVariant", 1, KJS::DontDelete|KJS::ReadOnly, &callIsVariant}, 00223 {"isObject", 1, KJS::DontDelete|KJS::ReadOnly, &callIsObject}, 00224 {0, 0, 0, 0 } 00225 }; 00226 //kate: indent-spaces on; indent-width 4; replace-tabs on; indent-mode cstyle;
KDE 4.6 API Reference