File indexing completed on 2024-12-01 09:54:39

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
0003     Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
0004     Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
0005     Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #include "builtins.h"
0024 
0025 #include <QCoreApplication>
0026 #include <QFile>
0027 #include <QStandardPaths>
0028 #include <QMessageBox>
0029 #include <QTextStream>
0030 #include <QDebug>
0031 #include <QMetaType>
0032 
0033 #include "variant_binding.h"
0034 #include "object_binding.h"
0035 #include "static_binding.h"
0036 #include "kjsembed.h"
0037 
0038 using namespace KJSEmbed;
0039 
0040 KJS::JSValue *callExec(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
0041 {
0042     Q_UNUSED(exec);
0043     Q_UNUSED(self);
0044     Q_UNUSED(args);
0045     return KJS::jsBoolean(QCoreApplication::exec());
0046 }
0047 
0048 KJS::JSValue *callDump(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
0049 {
0050     Q_UNUSED(self);
0051     if (args.size() == 1) {
0052         KJS::JSObject *object = args[0]->toObject(exec);
0053         Q_UNUSED(object);
0054     }
0055     return KJS::jsNull();
0056 }
0057 
0058 KJS::JSValue *callInclude(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
0059 {
0060     Q_UNUSED(self);
0061     if (args.size() == 1) {
0062         KJS::UString filename = args[0]->toString(exec);
0063         qDebug() << "include: " << toQString(filename);
0064 
0065         KJS::Completion c = Engine::runFile(exec->dynamicInterpreter(), filename);
0066 
0067         if (c.complType() == KJS::Normal) {
0068             return KJS::jsNull();
0069         }
0070 
0071         if (c.complType() == KJS::ReturnValue) {
0072             if (c.isValueCompletion()) {
0073                 return c.value();
0074             }
0075 
0076             return KJS::jsNull();
0077         }
0078 
0079         if (c.complType() == KJS::Throw) {
0080             QString message = toQString(c.value()->toString(exec));
0081             int line = c.value()->toObject(exec)->get(exec, "line")->toUInt32(exec);
0082             return throwError(exec, KJS::EvalError,
0083                               toUString(i18n("Error encountered while processing include '%1' line %2: %3", toQString(filename), line, message)));
0084         }
0085     } else {
0086         return throwError(exec, KJS::URIError,
0087                           toUString(i18n("include only takes 1 argument, not %1.", args.size())));
0088     }
0089 
0090     return KJS::jsNull();
0091 }
0092 
0093 KJS::JSValue *callLibrary(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
0094 {
0095     Q_UNUSED(self);
0096     if (args.size() == 1) {
0097         KJS::UString filename = args[0]->toString(exec);
0098         QString qualifiedFilename = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "scripts/" + toQString(filename));
0099         if (!qualifiedFilename.isEmpty()) {
0100             KJS::Completion c = Engine::runFile(exec->dynamicInterpreter(), toUString(qualifiedFilename));
0101             if (c.complType() == KJS::Normal) {
0102                 return KJS::jsNull();
0103             }
0104 
0105             if (c.complType() == KJS::ReturnValue) {
0106                 if (c.isValueCompletion()) {
0107                     return c.value();
0108                 }
0109 
0110                 return KJS::jsNull();
0111             }
0112 
0113             if (c.complType() == KJS::Throw) {
0114                 QString message = toQString(c.value()->toString(exec));
0115                 int line = c.value()->toObject(exec)->get(exec, "line")->toUInt32(exec);
0116                 return throwError(exec, KJS::EvalError,
0117                                   toUString(i18n("Error encountered while processing include '%1' line %2: %3", toQString(filename), line, message)));
0118             }
0119         } else {
0120             QString msg = i18n("File %1 not found.", toQString(filename));
0121             return throwError(exec, KJS::URIError, toUString(msg));
0122         }
0123     } else {
0124         return throwError(exec, KJS::URIError,
0125                           toUString(i18n("library only takes 1 argument, not %1.", args.size())));
0126     }
0127 
0128     return KJS::jsNull();
0129 }
0130 
0131 KJS::JSValue *callAlert(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
0132 {
0133     Q_UNUSED(self)
0134     if (args.size() == 1) {
0135         (*KJSEmbed::conerr()) << "callAlert";
0136         QString message = toQString(args[0]->toString(exec));
0137         QMessageBox::warning(nullptr, i18n("Alert"), message, QMessageBox::Ok, QMessageBox::NoButton);
0138     }
0139     return KJS::jsNull();
0140 }
0141 
0142 KJS::JSValue *callConfirm(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
0143 {
0144     Q_UNUSED(self)
0145     if (args.size() == 1) {
0146         QString message = toQString(args[0]->toString(exec));
0147         int result = QMessageBox::question(nullptr, i18n("Confirm"), message, QMessageBox::Yes, QMessageBox::No);
0148         if (result == QMessageBox::Yes) {
0149             return KJS::jsBoolean(true);
0150         }
0151     }
0152     return KJS::jsBoolean(false);
0153 }
0154 
0155 KJS::JSValue *callIsVariantType(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
0156 {
0157     Q_UNUSED(self)
0158     if (args.size() == 1) {
0159         QString thetypename = toQString(args[0]->toString(exec));
0160         return KJS::jsBoolean(QMetaType::type(thetypename.toLatin1().data()));
0161     }
0162     return KJS::jsBoolean(false);
0163 }
0164 
0165 KJS::JSValue *callIsVariant(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
0166 {
0167     Q_UNUSED(self)
0168     if (args.size() == 1) {
0169         KJS::JSObject *obj = args[0]->toObject(exec);
0170         if (obj->inherits(&VariantBinding::info)) {
0171             return KJS::jsBoolean(true);
0172         }
0173     }
0174     return KJS::jsBoolean(false);
0175 }
0176 
0177 KJS::JSValue *callIsObject(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
0178 {
0179     Q_UNUSED(self)
0180     if (args.size() == 1) {
0181         KJS::JSObject *obj = args[0]->toObject(exec);
0182         if (obj->inherits(&ObjectBinding::info)) {
0183             return KJS::jsBoolean(true);
0184         }
0185     }
0186     return KJS::jsBoolean(false);
0187 }
0188 
0189 const Method BuiltinsFactory::BuiltinMethods[] = {
0190     {"exec", 0, KJS::DontDelete | KJS::ReadOnly, &callExec},
0191     {"dump", 1, KJS::DontDelete | KJS::ReadOnly, &callDump},
0192     {"include", 1, KJS::DontDelete | KJS::ReadOnly, &callInclude},
0193     {"library", 1, KJS::DontDelete | KJS::ReadOnly, &callLibrary},
0194     {"alert", 1, KJS::DontDelete | KJS::ReadOnly, &callAlert},
0195     {"confirm", 1, KJS::DontDelete | KJS::ReadOnly, &callConfirm},
0196     {"isVariantType", 1, KJS::DontDelete | KJS::ReadOnly, &callIsVariantType},
0197     {"isVariant", 1, KJS::DontDelete | KJS::ReadOnly, &callIsVariant},
0198     {"isObject", 1, KJS::DontDelete | KJS::ReadOnly, &callIsObject},
0199     {nullptr, 0, 0, nullptr }
0200 };