KJsEmbed

builtins.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2005, 2006 Ian Reinhart Geiser <[email protected]>
3  Copyright (C) 2005, 2006 Matt Broadstone <[email protected]>
4  Copyright (C) 2005, 2006 Richard J. Moore <[email protected]>
5  Copyright (C) 2005, 2006 Erik L. Bunce <[email protected]>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include "builtins.h"
24 
25 #include <QCoreApplication>
26 #include <QFile>
27 #include <QStandardPaths>
28 #include <QMessageBox>
29 #include <QTextStream>
30 #include <QDebug>
31 #include <QMetaType>
32 
33 #include "variant_binding.h"
34 #include "object_binding.h"
35 #include "static_binding.h"
36 #include "kjsembed.h"
37 
38 using namespace KJSEmbed;
39 
40 KJS::JSValue *callExec(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
41 {
42  Q_UNUSED(exec);
43  Q_UNUSED(self);
44  Q_UNUSED(args);
45  return KJS::jsBoolean(QCoreApplication::exec());
46 }
47 
48 KJS::JSValue *callDump(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
49 {
50  Q_UNUSED(self);
51  if (args.size() == 1) {
52  KJS::JSObject *object = args[0]->toObject(exec);
53  Q_UNUSED(object);
54  }
55  return KJS::jsNull();
56 }
57 
58 KJS::JSValue *callInclude(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
59 {
60  Q_UNUSED(self);
61  if (args.size() == 1) {
62  KJS::UString filename = args[0]->toString(exec);
63  qDebug() << "include: " << toQString(filename);
64 
65  KJS::Completion c = Engine::runFile(exec->dynamicInterpreter(), filename);
66 
67  if (c.complType() == KJS::Normal) {
68  return KJS::jsNull();
69  }
70 
71  if (c.complType() == KJS::ReturnValue) {
72  if (c.isValueCompletion()) {
73  return c.value();
74  }
75 
76  return KJS::jsNull();
77  }
78 
79  if (c.complType() == KJS::Throw) {
80  QString message = toQString(c.value()->toString(exec));
81  int line = c.value()->toObject(exec)->get(exec, "line")->toUInt32(exec);
82  return throwError(exec, KJS::EvalError,
83  toUString(i18n("Error encountered while processing include '%1' line %2: %3", toQString(filename), line, message)));
84  }
85  } else {
86  return throwError(exec, KJS::URIError,
87  toUString(i18n("include only takes 1 argument, not %1.", args.size())));
88  }
89 
90  return KJS::jsNull();
91 }
92 
93 KJS::JSValue *callLibrary(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
94 {
95  Q_UNUSED(self);
96  if (args.size() == 1) {
97  KJS::UString filename = args[0]->toString(exec);
98  QString qualifiedFilename = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "scripts/" + toQString(filename));
99  if (!qualifiedFilename.isEmpty()) {
100  KJS::Completion c = Engine::runFile(exec->dynamicInterpreter(), toUString(qualifiedFilename));
101  if (c.complType() == KJS::Normal) {
102  return KJS::jsNull();
103  }
104 
105  if (c.complType() == KJS::ReturnValue) {
106  if (c.isValueCompletion()) {
107  return c.value();
108  }
109 
110  return KJS::jsNull();
111  }
112 
113  if (c.complType() == KJS::Throw) {
114  QString message = toQString(c.value()->toString(exec));
115  int line = c.value()->toObject(exec)->get(exec, "line")->toUInt32(exec);
116  return throwError(exec, KJS::EvalError,
117  toUString(i18n("Error encountered while processing include '%1' line %2: %3", toQString(filename), line, message)));
118  }
119  } else {
120  QString msg = i18n("File %1 not found.", toQString(filename));
121  return throwError(exec, KJS::URIError, toUString(msg));
122  }
123  } else {
124  return throwError(exec, KJS::URIError,
125  toUString(i18n("library only takes 1 argument, not %1.", args.size())));
126  }
127 
128  return KJS::jsNull();
129 }
130 
131 KJS::JSValue *callAlert(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
132 {
133  Q_UNUSED(self)
134  if (args.size() == 1) {
135  (*KJSEmbed::conerr()) << "callAlert";
136  QString message = toQString(args[0]->toString(exec));
138  }
139  return KJS::jsNull();
140 }
141 
142 KJS::JSValue *callConfirm(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
143 {
144  Q_UNUSED(self)
145  if (args.size() == 1) {
146  QString message = toQString(args[0]->toString(exec));
147  int result = QMessageBox::question(nullptr, i18n("Confirm"), message, QMessageBox::Yes, QMessageBox::No);
148  if (result == QMessageBox::Yes) {
149  return KJS::jsBoolean(true);
150  }
151  }
152  return KJS::jsBoolean(false);
153 }
154 
155 KJS::JSValue *callIsVariantType(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
156 {
157  Q_UNUSED(self)
158  if (args.size() == 1) {
159  QString thetypename = toQString(args[0]->toString(exec));
160  return KJS::jsBoolean(QMetaType::type(thetypename.toLatin1().data()));
161  }
162  return KJS::jsBoolean(false);
163 }
164 
165 KJS::JSValue *callIsVariant(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
166 {
167  Q_UNUSED(self)
168  if (args.size() == 1) {
169  KJS::JSObject *obj = args[0]->toObject(exec);
170  if (obj->inherits(&VariantBinding::info)) {
171  return KJS::jsBoolean(true);
172  }
173  }
174  return KJS::jsBoolean(false);
175 }
176 
177 KJS::JSValue *callIsObject(KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args)
178 {
179  Q_UNUSED(self)
180  if (args.size() == 1) {
181  KJS::JSObject *obj = args[0]->toObject(exec);
182  if (obj->inherits(&ObjectBinding::info)) {
183  return KJS::jsBoolean(true);
184  }
185  }
186  return KJS::jsBoolean(false);
187 }
188 
189 const Method BuiltinsFactory::BuiltinMethods[] = {
190  {"exec", 0, KJS::DontDelete | KJS::ReadOnly, &callExec},
191  {"dump", 1, KJS::DontDelete | KJS::ReadOnly, &callDump},
192  {"include", 1, KJS::DontDelete | KJS::ReadOnly, &callInclude},
193  {"library", 1, KJS::DontDelete | KJS::ReadOnly, &callLibrary},
194  {"alert", 1, KJS::DontDelete | KJS::ReadOnly, &callAlert},
195  {"confirm", 1, KJS::DontDelete | KJS::ReadOnly, &callConfirm},
196  {"isVariantType", 1, KJS::DontDelete | KJS::ReadOnly, &callIsVariantType},
197  {"isVariant", 1, KJS::DontDelete | KJS::ReadOnly, &callIsVariant},
198  {"isObject", 1, KJS::DontDelete | KJS::ReadOnly, &callIsObject},
199  {nullptr, 0, 0, nullptr }
200 };
int type(const char *typeName)
QByteArray toLatin1() const const
int size() const
QMessageBox::StandardButton warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
ComplType complType() const
QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
QMessageBox::StandardButton question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
ExitStatus runFile(const KJS::UString &file)
Execute the file with the specified name using the current interpreter.
Definition: kjsembed.cpp:233
QString i18n(const char *text, const TYPE &arg...)
bool isEmpty() const const
Method structure.
bool isValueCompletion() const
Interpreter * dynamicInterpreter() const
JSValue * value() const
QString message
char * data()
char * toString(const EngineQuery &query)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Dec 10 2023 03:59:19 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.