2 Copyright (C) 2012 Intel Corporation
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "bluemonkey.h"
22 #include <QJsonDocument>
32 #define foreach Q_FOREACH
34 typedef void create_bluemonkey_module_t(std::map<std::string, std::string> config, std::map<std::string, QObject*> &exports, QString &javascript, QObject* parent);
38 "console.log = function(msg)"
40 " bluemonkey.log(msg);"
45 Bluemonkey::Bluemonkey(std::map<std::string, std::string> config, QObject *parent)
46 :QObject(parent), engine(nullptr), configuration(config)
48 QTimer::singleShot(1,this,SLOT(reloadEngine()));
51 Bluemonkey::~Bluemonkey()
53 Q_FOREACH(void* module, modules)
58 engine->deleteLater();
61 void Bluemonkey::loadConfig(QString str)
64 if(!file.open(QIODevice::ReadOnly))
66 qDebug()<< "failed to open config file: "<< str;
70 QString script = file.readAll();
74 qDebug() << "evaluating script: "<< script;
76 QJSValue val = engine->evaluate(script);
78 qDebug()<< val.toString();
82 qDebug() << val.property("name").toString() << endl;
83 qDebug() << val.property("message").toString() << endl;
84 qDebug() << str << ":" << val.property("lineNumber").toString() << endl;
88 bool Bluemonkey::loadModule(QString path)
90 void* handle = dlopen(path.toUtf8().data(), RTLD_LAZY);
94 qDebug() << "bluemonkey load module failed: " << dlerror() << endl;
98 if(modules.contains(handle))
101 modules.push_back(handle);
103 void* c = dlsym(handle, "create");
107 qDebug() << "bluemonkey load module failed: " << path << " " << dlerror() << endl;
111 create_bluemonkey_module_t* create = reinterpret_cast<create_bluemonkey_module_t*>(c);
115 qCritical() << "Failed to call create() on module "<< path << ". Check signature.";
118 std::map<std::string, QObject*> exports;
120 create(configuration, exports, js, nullptr);
122 for(auto i : exports)
124 QObject* obj = i.second;
125 loadModule(i.first.c_str(), obj);
128 QJSValue val = engine->evaluate(js);
130 qDebug() << "evalutating module js result: " << val.toString();
134 qDebug() << "Script: " << js;
135 qCritical() << "Error in module javascript: " << val.toString();
141 void Bluemonkey::reloadEngine()
144 engine->deleteLater();
146 engine = new QJSEngine();
148 QJSValue value = engine->newQObject(this);
149 engine->globalObject().setProperty("bluemonkey", value);
151 if(engine->evaluate(bmJS).isError())
153 qCritical() << "Failed to load bluemonkey javascript extensions";
159 loadConfig(configuration["config"].c_str());
163 void Bluemonkey::writeProgram(QString program)
167 QJSValue result = temp.evaluate(program);
170 qDebug()<< "Syntax error in program: "<< result.toString();
174 QFile file(configuration["customPrograms"].c_str());
176 if(!file.open(QIODevice::ReadWrite | QIODevice::Append))
178 qDebug() << "failed to open file: " << file.fileName() << endl;
182 file.write(program.toUtf8());
188 void Bluemonkey::log(QJSValue str)
190 qDebug()<< str.toString();
193 QObject *Bluemonkey::createTimer()
195 return new QTimer(this);
198 QObject *Bluemonkey::createQObject()
200 return new QObject(this);
204 bool Bluemonkey::loadModule(const QString &name, QObject *module)
206 if(!engine->globalObject().hasProperty(name))
208 QJSValue val = engine->newQObject(module);
209 engine->globalObject().setProperty(name, val);
214 void Bluemonkey::assertIsTrue(bool isTrue, const QString & msg)