<commit_before>#include "EGDetailType.h"
using namespace scene::enemyGuide;

const int EGDetailType::TARGET_INFORMATION_WIDTH = static_cast<const int>(0.5*W);

const double EGDetailType::TARGET_SCALE = 0.35;
const int EGDetailType::FONT_SIZE = 16;

const int EGDetailType::TEXT_SPEED = 1;

const Point EGDetailType::POS_NEXT_BUTTON = Point(W - 100, 0.5*H);
const Point EGDetailType::POS_PREVIOUS_BUTTON = Point(100, 0.5*H);
const Point EGDetailType::POS_LIST_BUTTON = Point(POS_HOME_BUTTON.x, POS_HOME_BUTTON.y - 150);
const Point EGDetailType::POS_TARGET = Point(0.2*W, 0.3*H);

const Point EGDetailType::POS_TARGET_NAME = Point(POS_PREVIOUS_BUTTON.x + 360, POS_PREVIOUS_BUTTON.y - 50);
const Point EGDetailType::POS_TARGET_NAME_CONTENT = Point(POS_TARGET_NAME.x - 60, POS_TARGET_NAME.y - 30);

const Point EGDetailType::POS_TARGET_MESSAGE = Point(POS_TARGET_NAME.x, POS_TARGET_NAME.y + 60);
const Point EGDetailType::POS_TARGET_MESSAGE_CONTENT = Point(POS_TARGET_MESSAGE.x - 60, POS_TARGET_MESSAGE.y - 30);

const Point EGDetailType::POS_COLLECT_ANSWER = Point(POS_TARGET_NAME.x, POS_TARGET_MESSAGE.y + 120);
const Point EGDetailType::POS_COLLECT_ANSWER_CONTENT = Point(POS_COLLECT_ANSWER.x - 60, POS_COLLECT_ANSWER.y - 30);

const Point EGDetailType::POS_DESCRIPTION = Point(POS_TARGET_NAME.x, POS_COLLECT_ANSWER.y + 60);
const Point EGDetailType::POS_DESCRIPTION_CONTENT = Point(POS_DESCRIPTION.x - 60, POS_DESCRIPTION.y - 30);

const Color EGDetailType::CONTENT_COLOR = Palette::Black;

int EGDetailType::cursorID_m = 1;

void EGDetailType::init()
{
	ButtonManager::clearAll();
	ButtonManager::update();

	targetNameContentFont_m = Font(FONT_SIZE, Typeface::Bold);
	targetMessageContentFont_m = Font(FONT_SIZE, Typeface::Bold);
	collectAnswerContentFont_m = Font(FONT_SIZE, Typeface::Bold);
	descriptionContentFont_m = Font(FONT_SIZE, Typeface::Bold);

	targetNameFont_m = Font(FONT_SIZE);
	targetMessageFont_m = Font(FONT_SIZE);
	collectAnswerFont_m = Font(FONT_SIZE);
	descriptionFont_m = Font(FONT_SIZE);

	backGround_m = std::make_shared<RollBackGround>(L"firstEnemiesBackGround", L"secondEnemiesBackGround");
	for (int i = 1; i <= KIND_OF_ENEMIES; ++i)
	{
		std::shared_ptr<DrawableAssetTexture> targetGraphic = std::make_shared<DrawableAssetTexture>(Format(L"Enemy", i), POS_TARGET, TARGET_SCALE);
		targetGraphics_m.add(targetGraphic, i);
	}

	goToNext_m = [this]() {this->nextTarget(); SoundAsset(L"enemies_move").play(); };
	backToPrevious_m = [this]() {this->previousTarget(); SoundAsset(L"enemies_move").play(); };
	backToList_m = [this]() {(this->*&Scene::changeScene)(L"EGListType", 500, false); SoundAsset(L"enemies_return").play(); };
	backToTitle_m = [this]() {(this->*&Scene::changeScene)(L"Title", 500, false); SoundAsset(L"enemies_bgm").stop(); };

	homeButton_m = std::make_shared<TextureAssetButton>(Vec2(POS_HOME_BUTTON.x, POS_HOME_BUTTON.y), L"title_button", backToTitle_m);
	nextButton_m = std::make_shared<TextureAssetButton>(Vec2(POS_NEXT_BUTTON.x, POS_NEXT_BUTTON.y), L"right_button", goToNext_m);
	previousButton_m = std::make_shared<TextureAssetButton>(Vec2(POS_PREVIOUS_BUTTON.x, POS_PREVIOUS_BUTTON.y), L"left_button", backToPrevious_m);
	backToListButton_m = std::make_shared<TextureAssetButton>(Vec2(POS_LIST_BUTTON.x, POS_LIST_BUTTON.y), L"back_button_resize", backToList_m);

	ButtonManager::add(homeButton_m);
	ButtonManager::add(nextButton_m);
	ButtonManager::add(previousButton_m);
	ButtonManager::add(backToListButton_m);

	nameTextView_m = std::make_shared<TextView>(L"", POS_TARGET_NAME, TARGET_INFORMATION_WIDTH, 2, targetNameFont_m, TEXT_SPEED, Palette::Black);
	messageTextView_m = std::make_shared<TextView>(L"", POS_TARGET_MESSAGE, TARGET_INFORMATION_WIDTH, 3, targetMessageFont_m, TEXT_SPEED, Palette::Black);
	answerTextView_m = std::make_shared<TextView>(L"", POS_COLLECT_ANSWER, TARGET_INFORMATION_WIDTH, 2, collectAnswerFont_m, TEXT_SPEED, Palette::Black);
	descriptionTextView_m = std::make_shared<TextView>(L"", POS_DESCRIPTION, TARGET_INFORMATION_WIDTH, 4, descriptionFont_m, TEXT_SPEED, Palette::Black);

	target_m = dataManager_m.getEnemy(cursorID_m);

	showData();
}
void EGDetailType::update()
{
	nameTextView_m->update();
	messageTextView_m->update();
	answerTextView_m->update();
	descriptionTextView_m->update();
	backGround_m->update();

}
void EGDetailType::draw() const
{
	backGround_m->draw();
	graphics_m.drawAll();
	title_m.draw(POS_HEADING);

	homeButton_m->draw();
	nextButton_m->draw();
	previousButton_m->draw();
	backToListButton_m->draw();

	targetGraphics_m.drawLayer(cursorID_m);

	targetNameContentFont_m(L"X^[").draw(POS_TARGET_NAME_CONTENT, CONTENT_COLOR);
	nameTextView_m->draw();
	targetMessageContentFont_m(L"|ꂽ̃bZ[W").draw(POS_TARGET_MESSAGE_CONTENT, CONTENT_COLOR);
	messageTextView_m->draw();
	collectAnswerContentFont_m(L"_").draw(POS_COLLECT_ANSWER_CONTENT, CONTENT_COLOR);
	answerTextView_m->draw();
	descriptionContentFont_m(L"").draw(POS_DESCRIPTION_CONTENT, CONTENT_COLOR);
	descriptionTextView_m->draw();
}

void EGDetailType::nextTarget()
{
	cursorID_m = (cursorID_m >= 30) ? 1 : cursorID_m+1;
	target_m = dataManager_m.getEnemy(cursorID_m);

	if (dataManager_m.getSaveData(cursorID_m).isDefeated_m)
	{
		showData();
	}
	else
	{
		nextTarget();
	}
}
void EGDetailType::previousTarget()
{
	cursorID_m = (cursorID_m <= 1) ? 30 : cursorID_m-1;
	target_m = dataManager_m.getEnemy(cursorID_m);

	if(dataManager_m.getSaveData(cursorID_m).isDefeated_m)
	{
		showData();
	}
	else
	{
		previousTarget();
	}
}

void EGDetailType::changeTarget(int ID)
{
	cursorID_m = ID;
}

void EGDetailType::showData()
{
	nameTextView_m->setNewText(target_m.name_m);
	messageTextView_m->setNewText(target_m.messages_m.onPlayerWon_m);
	answerTextView_m->setNewText(target_m.collectAnswer_m);
	descriptionTextView_m->setNewText(target_m.description_m);
}
<commit_msg>敵の位置を微調整した。<commit_after>#include "EGDetailType.h"
using namespace scene::enemyGuide;

const int EGDetailType::TARGET_INFORMATION_WIDTH = static_cast<const int>(0.5*W);

const double EGDetailType::TARGET_SCALE = 0.7;
const int EGDetailType::FONT_SIZE = 16;

const int EGDetailType::TEXT_SPEED = 1;

const Point EGDetailType::POS_NEXT_BUTTON = Point(W - 100, 0.5*H);
const Point EGDetailType::POS_PREVIOUS_BUTTON = Point(100, 0.5*H);
const Point EGDetailType::POS_LIST_BUTTON = Point(POS_HOME_BUTTON.x, POS_HOME_BUTTON.y - 150);
const Point EGDetailType::POS_TARGET = Point(0.15*W, 0.25*H);

const Point EGDetailType::POS_TARGET_NAME = Point(POS_PREVIOUS_BUTTON.x + 360, POS_PREVIOUS_BUTTON.y - 50);
const Point EGDetailType::POS_TARGET_NAME_CONTENT = Point(POS_TARGET_NAME.x - 60, POS_TARGET_NAME.y - 30);

const Point EGDetailType::POS_TARGET_MESSAGE = Point(POS_TARGET_NAME.x, POS_TARGET_NAME.y + 60);
const Point EGDetailType::POS_TARGET_MESSAGE_CONTENT = Point(POS_TARGET_MESSAGE.x - 60, POS_TARGET_MESSAGE.y - 30);

const Point EGDetailType::POS_COLLECT_ANSWER = Point(POS_TARGET_NAME.x, POS_TARGET_MESSAGE.y + 120);
const Point EGDetailType::POS_COLLECT_ANSWER_CONTENT = Point(POS_COLLECT_ANSWER.x - 60, POS_COLLECT_ANSWER.y - 30);

const Point EGDetailType::POS_DESCRIPTION = Point(POS_TARGET_NAME.x, POS_COLLECT_ANSWER.y + 60);
const Point EGDetailType::POS_DESCRIPTION_CONTENT = Point(POS_DESCRIPTION.x - 60, POS_DESCRIPTION.y - 30);

const Color EGDetailType::CONTENT_COLOR = Palette::Black;

int EGDetailType::cursorID_m = 1;

void EGDetailType::init()
{
	ButtonManager::clearAll();
	ButtonManager::update();

	targetNameContentFont_m = Font(FONT_SIZE, Typeface::Bold);
	targetMessageContentFont_m = Font(FONT_SIZE, Typeface::Bold);
	collectAnswerContentFont_m = Font(FONT_SIZE, Typeface::Bold);
	descriptionContentFont_m = Font(FONT_SIZE, Typeface::Bold);

	targetNameFont_m = Font(FONT_SIZE);
	targetMessageFont_m = Font(FONT_SIZE);
	collectAnswerFont_m = Font(FONT_SIZE);
	descriptionFont_m = Font(FONT_SIZE);

	backGround_m = std::make_shared<RollBackGround>(L"firstEnemiesBackGround", L"secondEnemiesBackGround");
	for (int i = 1; i <= KIND_OF_ENEMIES; ++i)
	{
		std::shared_ptr<DrawableAssetTexture> targetGraphic = std::make_shared<DrawableAssetTexture>(Format(L"Enemy", i), POS_TARGET, TARGET_SCALE);
		targetGraphics_m.add(targetGraphic, i);
	}

	goToNext_m = [this]() {this->nextTarget(); SoundAsset(L"enemies_move").play(); };
	backToPrevious_m = [this]() {this->previousTarget(); SoundAsset(L"enemies_move").play(); };
	backToList_m = [this]() {(this->*&Scene::changeScene)(L"EGListType", 500, false); SoundAsset(L"enemies_return").play(); };
	backToTitle_m = [this]() {(this->*&Scene::changeScene)(L"Title", 500, false); SoundAsset(L"enemies_bgm").stop(); };

	homeButton_m = std::make_shared<TextureAssetButton>(Vec2(POS_HOME_BUTTON.x, POS_HOME_BUTTON.y), L"title_button", backToTitle_m);
	nextButton_m = std::make_shared<TextureAssetButton>(Vec2(POS_NEXT_BUTTON.x, POS_NEXT_BUTTON.y), L"right_button", goToNext_m);
	previousButton_m = std::make_shared<TextureAssetButton>(Vec2(POS_PREVIOUS_BUTTON.x, POS_PREVIOUS_BUTTON.y), L"left_button", backToPrevious_m);
	backToListButton_m = std::make_shared<TextureAssetButton>(Vec2(POS_LIST_BUTTON.x, POS_LIST_BUTTON.y), L"back_button_resize", backToList_m);

	ButtonManager::add(homeButton_m);
	ButtonManager::add(nextButton_m);
	ButtonManager::add(previousButton_m);
	ButtonManager::add(backToListButton_m);

	nameTextView_m = std::make_shared<TextView>(L"", POS_TARGET_NAME, TARGET_INFORMATION_WIDTH, 2, targetNameFont_m, TEXT_SPEED, Palette::Black);
	messageTextView_m = std::make_shared<TextView>(L"", POS_TARGET_MESSAGE, TARGET_INFORMATION_WIDTH, 3, targetMessageFont_m, TEXT_SPEED, Palette::Black);
	answerTextView_m = std::make_shared<TextView>(L"", POS_COLLECT_ANSWER, TARGET_INFORMATION_WIDTH, 2, collectAnswerFont_m, TEXT_SPEED, Palette::Black);
	descriptionTextView_m = std::make_shared<TextView>(L"", POS_DESCRIPTION, TARGET_INFORMATION_WIDTH, 4, descriptionFont_m, TEXT_SPEED, Palette::Black);

	target_m = dataManager_m.getEnemy(cursorID_m);

	showData();
}
void EGDetailType::update()
{
	nameTextView_m->update();
	messageTextView_m->update();
	answerTextView_m->update();
	descriptionTextView_m->update();
	backGround_m->update();

}
void EGDetailType::draw() const
{
	backGround_m->draw();
	graphics_m.drawAll();
	title_m.draw(POS_HEADING);

	homeButton_m->draw();
	nextButton_m->draw();
	previousButton_m->draw();
	backToListButton_m->draw();

	targetGraphics_m.drawLayer(cursorID_m);

	targetNameContentFont_m(L"X^[").draw(POS_TARGET_NAME_CONTENT, CONTENT_COLOR);
	nameTextView_m->draw();
	targetMessageContentFont_m(L"|ꂽ̃bZ[W").draw(POS_TARGET_MESSAGE_CONTENT, CONTENT_COLOR);
	messageTextView_m->draw();
	collectAnswerContentFont_m(L"_").draw(POS_COLLECT_ANSWER_CONTENT, CONTENT_COLOR);
	answerTextView_m->draw();
	descriptionContentFont_m(L"").draw(POS_DESCRIPTION_CONTENT, CONTENT_COLOR);
	descriptionTextView_m->draw();
}

void EGDetailType::nextTarget()
{
	cursorID_m = (cursorID_m >= 30) ? 1 : cursorID_m+1;
	target_m = dataManager_m.getEnemy(cursorID_m);

	if (dataManager_m.getSaveData(cursorID_m).isDefeated_m)
	{
		showData();
	}
	else
	{
		nextTarget();
	}
}
void EGDetailType::previousTarget()
{
	cursorID_m = (cursorID_m <= 1) ? 30 : cursorID_m-1;
	target_m = dataManager_m.getEnemy(cursorID_m);

	if(dataManager_m.getSaveData(cursorID_m).isDefeated_m)
	{
		showData();
	}
	else
	{
		previousTarget();
	}
}

void EGDetailType::changeTarget(int ID)
{
	cursorID_m = ID;
}

void EGDetailType::showData()
{
	nameTextView_m->setNewText(target_m.name_m);
	messageTextView_m->setNewText(target_m.messages_m.onPlayerWon_m);
	answerTextView_m->setNewText(target_m.collectAnswer_m);
	descriptionTextView_m->setNewText(target_m.description_m);
}
<|endoftext|>


<commit_before>/***************************************************************************
**                                                                        **
**  Copyright (C) 2013 Jolla Ltd.                                         **
**  Contact: Petri M. Gerdt <petri.gerdt@jollamobile.com>                 **
**                                                                        **
**     This file is part of Timed                                         **
**                                                                        **
**     Timed is free software; you can redistribute it and/or modify      **
**     it under the terms of the GNU Lesser General Public License        **
**     version 2.1 as published by the Free Software Foundation.          **
**                                                                        **
**     Timed is distributed in the hope that it will be useful, but       **
**     WITHOUT ANY WARRANTY;  without even the implied warranty  of       **
**     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               **
**     See the GNU Lesser General Public License  for more details.       **
**                                                                        **
**   You should have received a copy of the GNU  Lesser General Public    **
**   License along with Timed. If not, see http://www.gnu.org/licenses/   **
**                                                                        **
***************************************************************************/

#include <QDBusInterface>
#include <QDBusReply>

#include "../common/log.h"

#include "ofonoconstants.h"
#include "modemwatcher.h"

ModemWatcher::ModemWatcher(const QString objectPath, const QString interface, QObject *parent) :
    QObject(parent), m_objectPath(objectPath), m_interface(interface)
{
    QDBusConnection::systemBus().connect(OfonoConstants::OFONO_SERVICE, m_objectPath,
                                         OfonoConstants::OFONO_MODEM_INTERFACE, "PropertyChanged",
                                         this, SLOT(onModemPropertyChanged(QString, QDBusVariant)));

    QDBusMessage request = QDBusMessage::createMethodCall(OfonoConstants::OFONO_SERVICE,
                                                          m_objectPath,
                                                          OfonoConstants::OFONO_MODEM_INTERFACE,
                                                          "GetProperties");

    QDBusReply<QVariantMap> reply = QDBusConnection::systemBus().call(request);
    if (reply.error().isValid()) {
        log_error("DBus call to interface %s function GetProperties of path %s failed: %s",
                  m_interface.toStdString().c_str(),
                  m_objectPath.toStdString().c_str(),
                  reply.error().message().toStdString().c_str());
    } else {
        QVariantMap properties = reply;
        checkInterfaceAvailability(properties.value("Interfaces"));
    }
}

ModemWatcher::~ModemWatcher()
{
    QDBusConnection::systemBus().disconnect(OfonoConstants::OFONO_SERVICE, m_objectPath,
                                            OfonoConstants::OFONO_MODEM_INTERFACE, "PropertyChanged",
                                            this, SLOT(onModemPropertyChanged(QString, QDBusVariant)));
}

bool ModemWatcher::interfaceAvailable() const
{
    return m_interfaceAvailable;
}

QString ModemWatcher::objectPath() const
{
    return m_objectPath;
}

QString ModemWatcher::interface() const
{
    return m_interface;
}

// Checks if the interface stored in m_interface is present in
// list of interfaces stored in parameter variant.
// Changes m_interfaceAvailable to reflect interface presence in
// the list: true indicates that the interface is in the list, false
// indicates that it is not. If m_interfaceAvailable was changed,
// then returns true, otherwise returns false.
bool ModemWatcher::checkInterfaceAvailability(QVariant variant)
{
    if (variant.type() != QVariant::StringList)
        return false;

    QStringList list = variant.toStringList();
    bool available = list.contains(m_interface);
    if (available != m_interfaceAvailable) {
        m_interfaceAvailable = available;
        return true;
    }

    return false;
}

void ModemWatcher::onModemPropertyChanged(QString name, QDBusVariant value)
{
    if (name.compare("Interfaces") != 0)
        return;

    if (checkInterfaceAvailability(value.variant())) {
        log_debug("interface %s availability changed: %s",
                  m_interface.toStdString().c_str(),
                  m_interfaceAvailable ? "available" : "not available");
        emit interfaceAvailableChanged(m_interfaceAvailable);
    }
}
<commit_msg>[timed] Initialize variable indicating modem presence<commit_after>/***************************************************************************
**                                                                        **
**  Copyright (C) 2013 Jolla Ltd.                                         **
**  Contact: Petri M. Gerdt <petri.gerdt@jollamobile.com>                 **
**                                                                        **
**     This file is part of Timed                                         **
**                                                                        **
**     Timed is free software; you can redistribute it and/or modify      **
**     it under the terms of the GNU Lesser General Public License        **
**     version 2.1 as published by the Free Software Foundation.          **
**                                                                        **
**     Timed is distributed in the hope that it will be useful, but       **
**     WITHOUT ANY WARRANTY;  without even the implied warranty  of       **
**     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               **
**     See the GNU Lesser General Public License  for more details.       **
**                                                                        **
**   You should have received a copy of the GNU  Lesser General Public    **
**   License along with Timed. If not, see http://www.gnu.org/licenses/   **
**                                                                        **
***************************************************************************/

#include <QDBusInterface>
#include <QDBusReply>

#include "../common/log.h"

#include "ofonoconstants.h"
#include "modemwatcher.h"

ModemWatcher::ModemWatcher(const QString objectPath, const QString interface, QObject *parent) :
    QObject(parent), m_objectPath(objectPath), m_interface(interface), m_interfaceAvailable(false)
{
    QDBusConnection::systemBus().connect(OfonoConstants::OFONO_SERVICE, m_objectPath,
                                         OfonoConstants::OFONO_MODEM_INTERFACE, "PropertyChanged",
                                         this, SLOT(onModemPropertyChanged(QString, QDBusVariant)));

    QDBusMessage request = QDBusMessage::createMethodCall(OfonoConstants::OFONO_SERVICE,
                                                          m_objectPath,
                                                          OfonoConstants::OFONO_MODEM_INTERFACE,
                                                          "GetProperties");

    QDBusReply<QVariantMap> reply = QDBusConnection::systemBus().call(request);
    if (reply.error().isValid()) {
        log_error("DBus call to interface %s function GetProperties of path %s failed: %s",
                  m_interface.toStdString().c_str(),
                  m_objectPath.toStdString().c_str(),
                  reply.error().message().toStdString().c_str());
    } else {
        QVariantMap properties = reply;
        checkInterfaceAvailability(properties.value("Interfaces"));
    }
}

ModemWatcher::~ModemWatcher()
{
    QDBusConnection::systemBus().disconnect(OfonoConstants::OFONO_SERVICE, m_objectPath,
                                            OfonoConstants::OFONO_MODEM_INTERFACE, "PropertyChanged",
                                            this, SLOT(onModemPropertyChanged(QString, QDBusVariant)));
}

bool ModemWatcher::interfaceAvailable() const
{
    return m_interfaceAvailable;
}

QString ModemWatcher::objectPath() const
{
    return m_objectPath;
}

QString ModemWatcher::interface() const
{
    return m_interface;
}

// Checks if the interface stored in m_interface is present in
// list of interfaces stored in parameter variant.
// Changes m_interfaceAvailable to reflect interface presence in
// the list: true indicates that the interface is in the list, false
// indicates that it is not. If m_interfaceAvailable was changed,
// then returns true, otherwise returns false.
bool ModemWatcher::checkInterfaceAvailability(QVariant variant)
{
    if (variant.type() != QVariant::StringList)
        return false;

    QStringList list = variant.toStringList();
    bool available = list.contains(m_interface);
    if (available != m_interfaceAvailable) {
        m_interfaceAvailable = available;
        return true;
    }

    return false;
}

void ModemWatcher::onModemPropertyChanged(QString name, QDBusVariant value)
{
    if (name.compare("Interfaces") != 0)
        return;

    if (checkInterfaceAvailability(value.variant())) {
        log_debug("interface %s availability changed: %s",
                  m_interface.toStdString().c_str(),
                  m_interfaceAvailable ? "available" : "not available");
        emit interfaceAvailableChanged(m_interfaceAvailable);
    }
}
<|endoftext|>


<commit_before>// Copyright (c) 2013 Stanislas Polu.
// See the LICENSE file.

#include "breach/browser/node/node_thread.h"

#include "breach/browser/node/api/api_bindings.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/node/src/node.h"
#include "third_party/node/src/node_internals.h"
#include "base/command_line.h"
#include "base/time/time.h"

using v8::Isolate;
using v8::HandleScope;
using v8::Local;
using v8::Locker;
using v8::Context;
using v8::Object;
using v8::V8;
using v8::Value;
using v8::Script;
using v8::String;
using v8::RegisterExtension;

using namespace content;

namespace breach {

static NodeThread* s_thread = NULL;

NodeThread*
NodeThread::Get()
{
  if(s_thread == NULL) {
    s_thread = new NodeThread();
  }
  return s_thread;
}

NodeThread::NodeThread()
: Thread("node_wrapper_thread")
{
}

NodeThread::~NodeThread()
{
  /* All Thread subclasses must call Stop() in the destructor */
  Stop();
}

void
NodeThread::Init()
{
  message_loop()->PostTask(FROM_HERE,
                           base::Bind(&NodeThread::RunUvLoop,
                                      base::Unretained(this)));
}

void
NodeThread::Run(
    base::MessageLoop* message_loop)
{
  /* TODO(spolu): fork execution depending on kBreachRawInit */
  /* If not set, launch the default version of the Browser.  */
  /* If set, pass argc/argv to Node                          */

  /* Extract argc, argv to pass it directly to Node */
  const CommandLine* command_line = CommandLine::ForCurrentProcess();
  int argc = command_line->argv().size();
  char **argv = (char**)malloc(argc * sizeof(char*));
  for(int i = 0; i < argc; i ++) {
    unsigned len = strlen(command_line->argv()[i].c_str()) + 1;
    argv[i] = (char*) malloc(len * sizeof(char));
    memcpy(argv[i], command_line->argv()[i].c_str(), len);
  }

  node::InitSetup(argc, argv);
  Isolate* node_isolate = Isolate::GetCurrent();

  V8::Initialize();
  {
    Locker locker(node_isolate);
    HandleScope handle_scope(node_isolate);

    api_bindings_.reset(new ApiBindings());
    RegisterExtension(api_bindings_.get());
    const char* names[] = { "api_bindings.js" };
    v8::ExtensionConfiguration extensions(1, names);

    /* Create the one and only Context. */
    context_ = Context::New(node_isolate, &extensions);
    Context::Scope context_scope(context_);

    node::SetupBindingCache();
    process_ = node::SetupProcessObject(argc, argv);

    /* Create all the objects, load modules, do everything. */
    /* so your next reading stop should be node::Load()!    */
    node::Load(process_);

    Thread::Run(message_loop);

    node::EmitExit(process_);
    node::RunAtExit();
  }

  /* Cleanup */
  for(int i = 0; i < argc; i++) { free(argv[i]); }
  free(argv);
}

void
NodeThread::CleanUp()
{
  /* Clean up. Not strictly necessary. */
  V8::Dispose();
}

void
NodeThread::RunUvLoop()
{
  /* int ret = */ uv_run(uv_default_loop(), UV_RUN_NOWAIT);

  /* TODO(spolu): FixMe [/!\ Bad Code Ahead]                                 */
  /* If we call with UV_RUN_ONCE then it will hang the thread while there is */
  /* no `uv` related events, blocking all Chromium/Breach message delivery.  */
  /* We therefore post a delayed task to avoid hogging the CPU but not too   */
  /* far away to avoid any delay in the execution of nodeJS code. This is no */
  /* perfect, but it works!                                                  */
  /* Eventual best solution will be to implement a message_loop based on uv  */
  /* as it has already been done for node-webkit                             */
  message_loop()->PostDelayedTask(FROM_HERE,
                                  base::Bind(&NodeThread::RunUvLoop,
                                             base::Unretained(this)),
                                  base::TimeDelta::FromMicroseconds(100));
}

} // namespace breach
<commit_msg>comments on NodeThread<commit_after>// Copyright (c) 2013 Stanislas Polu.
// See the LICENSE file.

#include "breach/browser/node/node_thread.h"

#include "breach/browser/node/api/api_bindings.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/node/src/node.h"
#include "third_party/node/src/node_internals.h"
#include "base/command_line.h"
#include "base/time/time.h"

using v8::Isolate;
using v8::HandleScope;
using v8::Local;
using v8::Locker;
using v8::Context;
using v8::Object;
using v8::V8;
using v8::Value;
using v8::Script;
using v8::String;
using v8::RegisterExtension;

using namespace content;

namespace breach {

static NodeThread* s_thread = NULL;

NodeThread*
NodeThread::Get()
{
  if(s_thread == NULL) {
    s_thread = new NodeThread();
  }
  return s_thread;
}

NodeThread::NodeThread()
: Thread("node_wrapper_thread")
{
}

NodeThread::~NodeThread()
{
  /* All Thread subclasses must call Stop() in the destructor */
  Stop();
}

void
NodeThread::Init()
{
  message_loop()->PostTask(FROM_HERE,
                           base::Bind(&NodeThread::RunUvLoop,
                                      base::Unretained(this)));
}

void
NodeThread::Run(
    base::MessageLoop* message_loop)
{
  /* TODO(spolu): fork execution depending on kBreachRawInit */
  /* If not set, launch the default version of the Browser.  */
  /* If set, pass argc/argv to Node                          */

  /* Extract argc, argv to pass it directly to Node */
  const CommandLine* command_line = CommandLine::ForCurrentProcess();
  int argc = command_line->argv().size();
  char **argv = (char**)malloc(argc * sizeof(char*));
  for(int i = 0; i < argc; i ++) {
    unsigned len = strlen(command_line->argv()[i].c_str()) + 1;
    argv[i] = (char*) malloc(len * sizeof(char));
    memcpy(argv[i], command_line->argv()[i].c_str(), len);
  }

  node::InitSetup(argc, argv);
  Isolate* node_isolate = Isolate::GetCurrent();

  V8::Initialize();
  {
    Locker locker(node_isolate);
    HandleScope handle_scope(node_isolate);

    api_bindings_.reset(new ApiBindings());
    RegisterExtension(api_bindings_.get());
    const char* names[] = { "api_bindings.js" };
    v8::ExtensionConfiguration extensions(1, names);

    /* Create the one and only Context. */
    context_ = Context::New(node_isolate, &extensions);
    Context::Scope context_scope(context_);

    node::SetupBindingCache();
    process_ = node::SetupProcessObject(argc, argv);

    /* Create all the objects, load modules, do everything. */
    /* so your next reading stop should be node::Load()!    */
    node::Load(process_);

    Thread::Run(message_loop);

    node::EmitExit(process_);
    node::RunAtExit();
  }

  /* Cleanup */
  for(int i = 0; i < argc; i++) { free(argv[i]); }
  free(argv);
}

void
NodeThread::CleanUp()
{
  /* Clean up. Not strictly necessary. */
  V8::Dispose();
}

void
NodeThread::RunUvLoop()
{
  /* int ret = */ uv_run(uv_default_loop(), UV_RUN_NOWAIT);

  /* TODO(spolu): FixMe [/!\ Bad Code Ahead]                                 */
  /* If we call with UV_RUN_ONCE then it will hang the thread while there is */
  /* no `uv` related events, blocking all Chromium/Breach message delivery.  */
  /* We therefore post a delayed task to avoid hogging the CPU but not too   */
  /* far away to avoid any delay in the execution of nodeJS code. This is no */
  /* perfect, but it works!                                                  */
  /* Eventual best solution will be to implement a message_loop based on uv  */
  /* as it has already been done for node-webkit                             */
  /* Another acceptable solution would be to run the uv run_loop direclty    */
  /* from here and expose a mechanism for existing content threads to post a */
  /* task on that thread.                                                    */
  message_loop()->PostDelayedTask(FROM_HERE,
                                  base::Bind(&NodeThread::RunUvLoop,
                                             base::Unretained(this)),
                                  base::TimeDelta::FromMicroseconds(100));
}

} // namespace breach
<|endoftext|>


<commit_before>#include "text/table.h"
#include "text/cmdline.h"
#include "cortex/batch.h"
#include "cortex/cortex.h"
#include "cortex/logger.h"
#include "thread/thread.h"
#include "cortex/measure.hpp"
#include "cortex/minibatch.h"
#include "cortex/stochastic.h"
#include "cortex/accumulator.h"
#include "text/concatenate.hpp"
#include "cortex/tasks/task_charset.h"
#include "cortex/layers/make_layers.h"

using namespace nano;

template
<
        typename tvalue
>
static string_t stats_to_string(const nano::stats_t<tvalue>& stats)
{
        return  nano::to_string(static_cast<tvalue>(stats.avg()))
                + "+/-" + nano::to_string(static_cast<tvalue>(stats.stdev()))
                + " [" + nano::to_string(stats.min())
                + ", " + nano::to_string(stats.max())
                + "]";
}

template
<
        typename ttrainer
>
static void test_optimizer(model_t& model, const string_t& name, const string_t& basepath,
        nano::table_t& table, const vectors_t& x0s, const ttrainer& trainer)
{
        nano::stats_t<scalar_t> errors;
        nano::stats_t<scalar_t> speeds;
        nano::stats_t<scalar_t> timings;

        log_info() << "<<< running " << name << " ...";

        for (size_t i = 0; i < x0s.size(); ++ i)
        {
                const nano::timer_t timer;

                model.load_params(x0s[i]);

                const auto result = trainer();
                const auto opt_state = result.optimum_state();
                const auto opt_speed = nano::convergence_speed(result.optimum_states());

                errors(opt_state.m_test.m_error_avg);
                speeds(opt_speed);
                timings(static_cast<scalar_t>(timer.seconds().count()));

                log_info() << "<<< " << name << ": " << result << ", speed = " << opt_speed << "/s.";

                const auto path = basepath + "-trial" + nano::to_string(i) + ".state";

                const auto opt_states = result.optimum_states();
                nano::save(path, opt_states);
        }

        table.append(name)
                << stats_to_string(errors)
                << stats_to_string(speeds)
                << stats_to_string(timings);
}

static void evaluate(model_t& model,
        const task_t& task, const size_t fold,
        const loss_t& loss, const criterion_t& criterion, const vectors_t& x0s, const size_t iterations,
        const std::vector<batch_optimizer>& batch_optimizers,
        const std::vector<batch_optimizer>& minibatch_optimizers,
        const std::vector<stoch_optimizer>& stochastic_optimizers,
        const string_t& basename, const string_t& basepath, nano::table_t& table)
{
        const scalar_t epsilon = 1e-6;
        const size_t n_threads = thread::concurrency();
        const bool verbose = true;

        for (auto optimizer : batch_optimizers)
        {
                const auto optname = "batch-" + nano::to_string(optimizer);
                test_optimizer(model, basename + optname, basepath + optname, table, x0s, [&] ()
                {
                        return  nano::batch_train(model, task, fold,
                                n_threads, loss, criterion, optimizer, iterations, epsilon, verbose);
                });
        }

        for (auto optimizer : minibatch_optimizers)
        {
                const auto optname = "minibatch-" + nano::to_string(optimizer);
                test_optimizer(model, basename + optname, basepath + optname, table, x0s, [&] ()
                {
                        return  nano::minibatch_train(model, task, fold,
                                n_threads, loss, criterion, optimizer, iterations, epsilon, verbose);
                });
        }

        for (auto optimizer : stochastic_optimizers)
        {
                const auto optname = "stochastic-" + nano::to_string(optimizer);
                test_optimizer(model, basename + optname, basepath + optname, table, x0s, [&] ()
                {
                        return  nano::stochastic_train(model, task, fold,
                                n_threads, loss, criterion, optimizer, iterations, verbose);
                });
        }
}

int main(int argc, const char* argv[])
{
        using namespace nano;

        // parse the command line
        nano::cmdline_t cmdline("benchmark trainers");
        cmdline.add("", "mlps",                 "use MLPs with varying number of hidden layers");
        cmdline.add("", "convnets",             "use convolution networks with varying number of convolution layers");
        cmdline.add("", "batch",                "evaluate batch optimizers");
        cmdline.add("", "batch-gd",             "evaluate batch optimizer GD (gradient descent)");
        cmdline.add("", "batch-cgd",            "evaluate batch optimizer CGD (conjugate gradient descent)");
        cmdline.add("", "batch-lbfgs",          "evaluate batch optimizer LBFGS");
        cmdline.add("", "minibatch",            "evaluate mini-batch optimizers");
        cmdline.add("", "minibatch-gd",         "evaluate mini-batch optimizer GD (gradient descent)");
        cmdline.add("", "minibatch-cgd",        "evaluate mini-batch optimizer CGD (conjugate gradient descent)");
        cmdline.add("", "minibatch-lbfgs",      "evaluate mini-batch optimizer LBFGS");
        cmdline.add("", "stochastic",           "evaluate stochastic optimizers");
        cmdline.add("", "stochastic-sg",        "evaluate stochastic optimizer SG (stochastic gradient)");
        cmdline.add("", "stochastic-sgm",       "evaluate stochastic optimizer SGM (stochastic gradient with momentum)");
        cmdline.add("", "stochastic-ag",        "evaluate stochastic optimizer AG (Nesterov's accelerated gradient)");
        cmdline.add("", "stochastic-agfr",      "evaluate stochastic optimizer AG (AG + function value restarts)");
        cmdline.add("", "stochastic-aggr",      "evaluate stochastic optimizer AG (AG + gradient restarts)");
        cmdline.add("", "stochastic-adam",      "evaluate stochastic optimizer ADAM");
        cmdline.add("", "stochastic-adagrad",   "evaluate stochastic optimizer ADAGRAD");
        cmdline.add("", "stochastic-adadelta",  "evaluate stochastic optimizer ADADELTA");
        cmdline.add("", "l2n-reg",              "also evaluate the l2-norm-based regularizer");
        cmdline.add("", "var-reg",              "also evaluate the variance-based regularizer");
        cmdline.add("", "trials",               "number of models to train & evaluate", "10");
        cmdline.add("", "iterations",           "number of iterations/epochs", "64");

        cmdline.process(argc, argv);

        // check arguments and options
        const bool use_mlps = cmdline.has("mlps");
        const bool use_convnets = cmdline.has("convnets");
        const bool use_reg_l2n = cmdline.has("l2n-reg");
        const bool use_reg_var = cmdline.has("var-reg");
        const auto trials = cmdline.get<size_t>("trials");
        const auto iterations = cmdline.get<size_t>("iterations");

        if (    !use_mlps &&
                !use_convnets)
        {
                cmdline.usage();
        }

        std::vector<batch_optimizer> batch_optimizers;
        if (cmdline.has("batch") || cmdline.has("batch-gd")) batch_optimizers.push_back(batch_optimizer::GD);
        if (cmdline.has("batch") || cmdline.has("batch-cgd")) batch_optimizers.push_back(batch_optimizer::CGD);
        if (cmdline.has("batch") || cmdline.has("batch-lbfgs")) batch_optimizers.push_back(batch_optimizer::LBFGS);

        std::vector<batch_optimizer> minibatch_optimizers;
        if (cmdline.has("minibatch") || cmdline.has("minibatch-gd")) minibatch_optimizers.push_back(batch_optimizer::GD);
        if (cmdline.has("minibatch") || cmdline.has("minibatch-cgd")) minibatch_optimizers.push_back(batch_optimizer::CGD);
        if (cmdline.has("minibatch") || cmdline.has("minibatch-lbfgs")) minibatch_optimizers.push_back(batch_optimizer::LBFGS);

        std::vector<stoch_optimizer> stochastic_optimizers;
        if (cmdline.has("stochastic") || cmdline.has("stochastic-sg")) stochastic_optimizers.push_back(stoch_optimizer::SG);
        if (cmdline.has("stochastic") || cmdline.has("stochastic-sgm")) stochastic_optimizers.push_back(stoch_optimizer::SGM);
        if (cmdline.has("stochastic") || cmdline.has("stochastic-ag")) stochastic_optimizers.push_back(stoch_optimizer::AG);
        if (cmdline.has("stochastic") || cmdline.has("stochastic-agfr")) stochastic_optimizers.push_back(stoch_optimizer::AGFR);
        if (cmdline.has("stochastic") || cmdline.has("stochastic-aggr")) stochastic_optimizers.push_back(stoch_optimizer::AGGR);
        if (cmdline.has("stochastic") || cmdline.has("stochastic-adam")) stochastic_optimizers.push_back(stoch_optimizer::ADAM);
        if (cmdline.has("stochastic") || cmdline.has("stochastic-adagrad")) stochastic_optimizers.push_back(stoch_optimizer::ADAGRAD);
        if (cmdline.has("stochastic") || cmdline.has("stochastic-adadelta")) stochastic_optimizers.push_back(stoch_optimizer::ADADELTA);

        if (    batch_optimizers.empty() &&
                minibatch_optimizers.empty() &&
                stochastic_optimizers.empty())
        {
                cmdline.usage();
        }

        // create task
        const size_t rows = 16;
        const size_t cols = 16;
        const size_t count = thread::concurrency() * 32 * 20;
        const color_mode color = color_mode::rgb;

        charset_task_t task(charset::digit, color, rows, cols, count);
        task.load();

        const size_t fold = 0;
        const auto outputs = task.osize();

        // construct models
        const auto activation = "act-snorm";
        const auto pooling = "pool-full";

        const auto mlp0 = string_t();
        const auto mlp1 = mlp0 + make_affine_layer(16, activation);
        const auto mlp2 = mlp1 + make_affine_layer(16, activation);
        const auto mlp3 = mlp2 + make_affine_layer(16, activation);

        const auto convnet1 =
                make_conv_pool_layer(16, 7, 7, 1, activation, pooling);

        const auto convnet2 =
                make_conv_pool_layer(16, 7, 7, 1, activation, pooling) +
                make_conv_layer(32, 5, 5, 2);

        const auto convnet3 =
                make_conv_layer(16, 7, 7, 1, activation) +
                make_conv_layer(32, 5, 5, 2, activation) +
                make_conv_layer(64, 3, 3, 4, activation);

        const string_t outlayer = make_output_layer(outputs);

        std::vector<std::pair<string_t, string_t>> networks;
        if (use_mlps)
        {
                networks.emplace_back(mlp0 + outlayer, "mlp0");
                networks.emplace_back(mlp1 + outlayer, "mlp1");
                networks.emplace_back(mlp2 + outlayer, "mlp2");
                networks.emplace_back(mlp3 + outlayer, "mlp3");
        }
        if (use_convnets)
        {
                networks.emplace_back(convnet1 + outlayer, "convnet1");
                networks.emplace_back(convnet2 + outlayer, "convnet2");
                networks.emplace_back(convnet3 + outlayer, "convnet3");
        }

        const strings_t losses = { "classnll" }; //nano::get_losses().ids();

        strings_t criteria;
        criteria.push_back("avg"); //nano::get_criteria().ids();
        if (use_reg_l2n) { criteria.push_back("l2n-reg"); }
        if (use_reg_var) { criteria.push_back("var-reg"); }

        // vary the model
        for (const auto& net : networks)
        {
                const auto& network = net.first;
                const auto& netname = net.second;

                log_info() << "<<< running network [" << network << "] ...";

                const auto model = nano::get_models().get("forward-network", network);
                model->resize(task, true);

                // generate fixed random starting points
                vectors_t x0s(trials);
                for (vector_t& x0 : x0s)
                {
                        model->random_params();
                        model->save_params(x0);
                }

                // vary the loss
                for (const string_t& iloss : losses)
                {
                        log_info() << "<<< running loss [" << iloss << "] ...";

                        const auto loss = nano::get_losses().get(iloss);

                        nano::table_t table("optimizer");
                        table.header() << "test error"
                                       << "convergence speed"
                                       << "time [sec]";

                        // vary the criteria
                        for (const string_t& icriterion : criteria)
                        {
                                const auto criterion = nano::get_criteria().get(icriterion);
                                const auto basename = "[" + icriterion + "] ";
                                const auto basepath = netname + "-" + iloss + "-" + icriterion + "-";

                                evaluate(*model, task, fold, *loss, *criterion, x0s, iterations,
                                         batch_optimizers, minibatch_optimizers, stochastic_optimizers,
                                         basename, basepath, table);
                        }

                        // show results
                        table.print(std::cout);
                }

                log_info();
        }

        // OK
        log_info() << done;
        return EXIT_SUCCESS;
}
<commit_msg>print model name when outputting optimization results<commit_after>#include "text/table.h"
#include "text/cmdline.h"
#include "cortex/batch.h"
#include "cortex/cortex.h"
#include "cortex/logger.h"
#include "thread/thread.h"
#include "cortex/measure.hpp"
#include "cortex/minibatch.h"
#include "cortex/stochastic.h"
#include "cortex/accumulator.h"
#include "text/concatenate.hpp"
#include "cortex/tasks/task_charset.h"
#include "cortex/layers/make_layers.h"

using namespace nano;

template
<
        typename tvalue
>
static string_t stats_to_string(const nano::stats_t<tvalue>& stats)
{
        return  nano::to_string(static_cast<tvalue>(stats.avg()))
                + "+/-" + nano::to_string(static_cast<tvalue>(stats.stdev()))
                + " [" + nano::to_string(stats.min())
                + ", " + nano::to_string(stats.max())
                + "]";
}

template
<
        typename ttrainer
>
static void test_optimizer(model_t& model, const string_t& name, const string_t& basepath,
        nano::table_t& table, const vectors_t& x0s, const ttrainer& trainer)
{
        nano::stats_t<scalar_t> errors;
        nano::stats_t<scalar_t> speeds;
        nano::stats_t<scalar_t> timings;

        log_info() << "<<< running " << name << " ...";

        for (size_t i = 0; i < x0s.size(); ++ i)
        {
                const nano::timer_t timer;

                model.load_params(x0s[i]);

                const auto result = trainer();
                const auto opt_state = result.optimum_state();
                const auto opt_speed = nano::convergence_speed(result.optimum_states());

                errors(opt_state.m_test.m_error_avg);
                speeds(opt_speed);
                timings(static_cast<scalar_t>(timer.seconds().count()));

                log_info() << "<<< " << name << ": " << result << ", speed = " << opt_speed << "/s.";

                const auto path = basepath + "-trial" + nano::to_string(i) + ".state";

                const auto opt_states = result.optimum_states();
                nano::save(path, opt_states);
        }

        table.append(name)
                << stats_to_string(errors)
                << stats_to_string(speeds)
                << stats_to_string(timings);
}

static void evaluate(model_t& model,
        const task_t& task, const size_t fold,
        const loss_t& loss, const criterion_t& criterion, const vectors_t& x0s, const size_t iterations,
        const std::vector<batch_optimizer>& batch_optimizers,
        const std::vector<batch_optimizer>& minibatch_optimizers,
        const std::vector<stoch_optimizer>& stochastic_optimizers,
        const string_t& basename, const string_t& basepath, nano::table_t& table)
{
        const scalar_t epsilon = 1e-6;
        const size_t n_threads = thread::concurrency();
        const bool verbose = true;

        for (auto optimizer : batch_optimizers)
        {
                const auto optname = "batch-" + nano::to_string(optimizer);
                test_optimizer(model, basename + optname, basepath + optname, table, x0s, [&] ()
                {
                        return  nano::batch_train(model, task, fold,
                                n_threads, loss, criterion, optimizer, iterations, epsilon, verbose);
                });
        }

        for (auto optimizer : minibatch_optimizers)
        {
                const auto optname = "minibatch-" + nano::to_string(optimizer);
                test_optimizer(model, basename + optname, basepath + optname, table, x0s, [&] ()
                {
                        return  nano::minibatch_train(model, task, fold,
                                n_threads, loss, criterion, optimizer, iterations, epsilon, verbose);
                });
        }

        for (auto optimizer : stochastic_optimizers)
        {
                const auto optname = "stochastic-" + nano::to_string(optimizer);
                test_optimizer(model, basename + optname, basepath + optname, table, x0s, [&] ()
                {
                        return  nano::stochastic_train(model, task, fold,
                                n_threads, loss, criterion, optimizer, iterations, verbose);
                });
        }
}

int main(int argc, const char* argv[])
{
        using namespace nano;

        // parse the command line
        nano::cmdline_t cmdline("benchmark trainers");
        cmdline.add("", "mlps",                 "use MLPs with varying number of hidden layers");
        cmdline.add("", "convnets",             "use convolution networks with varying number of convolution layers");
        cmdline.add("", "batch",                "evaluate batch optimizers");
        cmdline.add("", "batch-gd",             "evaluate batch optimizer GD (gradient descent)");
        cmdline.add("", "batch-cgd",            "evaluate batch optimizer CGD (conjugate gradient descent)");
        cmdline.add("", "batch-lbfgs",          "evaluate batch optimizer LBFGS");
        cmdline.add("", "minibatch",            "evaluate mini-batch optimizers");
        cmdline.add("", "minibatch-gd",         "evaluate mini-batch optimizer GD (gradient descent)");
        cmdline.add("", "minibatch-cgd",        "evaluate mini-batch optimizer CGD (conjugate gradient descent)");
        cmdline.add("", "minibatch-lbfgs",      "evaluate mini-batch optimizer LBFGS");
        cmdline.add("", "stochastic",           "evaluate stochastic optimizers");
        cmdline.add("", "stochastic-sg",        "evaluate stochastic optimizer SG (stochastic gradient)");
        cmdline.add("", "stochastic-sgm",       "evaluate stochastic optimizer SGM (stochastic gradient with momentum)");
        cmdline.add("", "stochastic-ag",        "evaluate stochastic optimizer AG (Nesterov's accelerated gradient)");
        cmdline.add("", "stochastic-agfr",      "evaluate stochastic optimizer AG (AG + function value restarts)");
        cmdline.add("", "stochastic-aggr",      "evaluate stochastic optimizer AG (AG + gradient restarts)");
        cmdline.add("", "stochastic-adam",      "evaluate stochastic optimizer ADAM");
        cmdline.add("", "stochastic-adagrad",   "evaluate stochastic optimizer ADAGRAD");
        cmdline.add("", "stochastic-adadelta",  "evaluate stochastic optimizer ADADELTA");
        cmdline.add("", "l2n-reg",              "also evaluate the l2-norm-based regularizer");
        cmdline.add("", "var-reg",              "also evaluate the variance-based regularizer");
        cmdline.add("", "trials",               "number of models to train & evaluate", "10");
        cmdline.add("", "iterations",           "number of iterations/epochs", "64");

        cmdline.process(argc, argv);

        // check arguments and options
        const bool use_mlps = cmdline.has("mlps");
        const bool use_convnets = cmdline.has("convnets");
        const bool use_reg_l2n = cmdline.has("l2n-reg");
        const bool use_reg_var = cmdline.has("var-reg");
        const auto trials = cmdline.get<size_t>("trials");
        const auto iterations = cmdline.get<size_t>("iterations");

        if (    !use_mlps &&
                !use_convnets)
        {
                cmdline.usage();
        }

        std::vector<batch_optimizer> batch_optimizers;
        if (cmdline.has("batch") || cmdline.has("batch-gd")) batch_optimizers.push_back(batch_optimizer::GD);
        if (cmdline.has("batch") || cmdline.has("batch-cgd")) batch_optimizers.push_back(batch_optimizer::CGD);
        if (cmdline.has("batch") || cmdline.has("batch-lbfgs")) batch_optimizers.push_back(batch_optimizer::LBFGS);

        std::vector<batch_optimizer> minibatch_optimizers;
        if (cmdline.has("minibatch") || cmdline.has("minibatch-gd")) minibatch_optimizers.push_back(batch_optimizer::GD);
        if (cmdline.has("minibatch") || cmdline.has("minibatch-cgd")) minibatch_optimizers.push_back(batch_optimizer::CGD);
        if (cmdline.has("minibatch") || cmdline.has("minibatch-lbfgs")) minibatch_optimizers.push_back(batch_optimizer::LBFGS);

        std::vector<stoch_optimizer> stochastic_optimizers;
        if (cmdline.has("stochastic") || cmdline.has("stochastic-sg")) stochastic_optimizers.push_back(stoch_optimizer::SG);
        if (cmdline.has("stochastic") || cmdline.has("stochastic-sgm")) stochastic_optimizers.push_back(stoch_optimizer::SGM);
        if (cmdline.has("stochastic") || cmdline.has("stochastic-ag")) stochastic_optimizers.push_back(stoch_optimizer::AG);
        if (cmdline.has("stochastic") || cmdline.has("stochastic-agfr")) stochastic_optimizers.push_back(stoch_optimizer::AGFR);
        if (cmdline.has("stochastic") || cmdline.has("stochastic-aggr")) stochastic_optimizers.push_back(stoch_optimizer::AGGR);
        if (cmdline.has("stochastic") || cmdline.has("stochastic-adam")) stochastic_optimizers.push_back(stoch_optimizer::ADAM);
        if (cmdline.has("stochastic") || cmdline.has("stochastic-adagrad")) stochastic_optimizers.push_back(stoch_optimizer::ADAGRAD);
        if (cmdline.has("stochastic") || cmdline.has("stochastic-adadelta")) stochastic_optimizers.push_back(stoch_optimizer::ADADELTA);

        if (    batch_optimizers.empty() &&
                minibatch_optimizers.empty() &&
                stochastic_optimizers.empty())
        {
                cmdline.usage();
        }

        // create task
        const size_t rows = 16;
        const size_t cols = 16;
        const size_t count = thread::concurrency() * 32 * 20;
        const color_mode color = color_mode::rgb;

        charset_task_t task(charset::digit, color, rows, cols, count);
        task.load();

        const size_t fold = 0;
        const auto outputs = task.osize();

        // construct models
        const auto activation = "act-snorm";
        const auto pooling = "pool-full";

        const auto mlp0 = string_t();
        const auto mlp1 = mlp0 + make_affine_layer(16, activation);
        const auto mlp2 = mlp1 + make_affine_layer(16, activation);
        const auto mlp3 = mlp2 + make_affine_layer(16, activation);

        const auto convnet1 =
                make_conv_pool_layer(16, 7, 7, 1, activation, pooling);

        const auto convnet2 =
                make_conv_pool_layer(16, 7, 7, 1, activation, pooling) +
                make_conv_layer(32, 5, 5, 2);

        const auto convnet3 =
                make_conv_layer(16, 7, 7, 1, activation) +
                make_conv_layer(32, 5, 5, 2, activation) +
                make_conv_layer(64, 3, 3, 4, activation);

        const string_t outlayer = make_output_layer(outputs);

        std::vector<std::pair<string_t, string_t>> networks;
        if (use_mlps)
        {
                networks.emplace_back(mlp0 + outlayer, "mlp0");
                networks.emplace_back(mlp1 + outlayer, "mlp1");
                networks.emplace_back(mlp2 + outlayer, "mlp2");
                networks.emplace_back(mlp3 + outlayer, "mlp3");
        }
        if (use_convnets)
        {
                networks.emplace_back(convnet1 + outlayer, "convnet1");
                networks.emplace_back(convnet2 + outlayer, "convnet2");
                networks.emplace_back(convnet3 + outlayer, "convnet3");
        }

        const strings_t losses = { "classnll" }; //nano::get_losses().ids();

        strings_t criteria;
        criteria.push_back("avg"); //nano::get_criteria().ids();
        if (use_reg_l2n) { criteria.push_back("l2n-reg"); }
        if (use_reg_var) { criteria.push_back("var-reg"); }

        // vary the model
        for (const auto& net : networks)
        {
                const auto& network = net.first;
                const auto& netname = net.second;

                log_info() << "<<< running network [" << network << "] ...";

                const auto model = nano::get_models().get("forward-network", network);
                model->resize(task, true);

                // generate fixed random starting points
                vectors_t x0s(trials);
                for (vector_t& x0 : x0s)
                {
                        model->random_params();
                        model->save_params(x0);
                }

                // vary the loss
                for (const string_t& iloss : losses)
                {
                        log_info() << "<<< running loss [" << iloss << "] ...";

                        const auto loss = nano::get_losses().get(iloss);

                        nano::table_t table(netname +"-" + iloss);
                        table.header() << "test error"
                                       << "convergence speed"
                                       << "time [sec]";

                        // vary the criteria
                        for (const string_t& icriterion : criteria)
                        {
                                const auto criterion = nano::get_criteria().get(icriterion);
                                const auto basename = "[" + icriterion + "] ";
                                const auto basepath = netname + "-" + iloss + "-" + icriterion + "-";

                                evaluate(*model, task, fold, *loss, *criterion, x0s, iterations,
                                         batch_optimizers, minibatch_optimizers, stochastic_optimizers,
                                         basename, basepath, table);
                        }

                        // show results
                        table.print(std::cout);
                }

                log_info();
        }

        // OK
        log_info() << done;
        return EXIT_SUCCESS;
}
<|endoftext|>


<commit_before>#include <aleph/topology/Simplex.hh>
#include <aleph/topology/SimplicialComplex.hh>

#include <aleph/topology/io/SparseAdjacencyMatrix.hh>

#include <iostream>
#include <string>
#include <vector>

int main( int argc, char** argv )
{
  using DataType          = unsigned;
  using VertexType        = std::size_t;
  using Simplex           = aleph::topology::Simplex<DataType, VertexType>;
  using SimplicialComplex = aleph::topology::SimplicialComplex<Simplex>;

  if( argc <= 1 )
    return -1;

  std::string filename = argv[1];

  std::vector<SimplicialComplex> simplicialComplexes;
  std::vector<std::string> labels;

  aleph::topology::io::SparseAdjacencyMatrixReader reader;
  reader.setReadGraphLabels();

  std::cerr << "* Reading '" << filename << "'...";

  reader( filename, simplicialComplexes );

  std::cerr << "finished\n";
}
<commit_msg>Added degree-based filtration<commit_after>#include <aleph/geometry/RipsExpander.hh>

#include <aleph/topology/Simplex.hh>
#include <aleph/topology/SimplicialComplex.hh>

#include <aleph/topology/filtrations/Data.hh>
#include <aleph/topology/filtrations/Degree.hh>

#include <aleph/topology/io/SparseAdjacencyMatrix.hh>

#include <iostream>
#include <string>
#include <vector>

int main( int argc, char** argv )
{
  using DataType          = unsigned;
  using VertexType        = std::size_t;
  using Simplex           = aleph::topology::Simplex<DataType, VertexType>;
  using SimplicialComplex = aleph::topology::SimplicialComplex<Simplex>;

  if( argc <= 1 )
    return -1;

  std::string filename = argv[1];

  std::vector<SimplicialComplex> simplicialComplexes;
  std::vector<std::string> labels;

  aleph::topology::io::SparseAdjacencyMatrixReader reader;
  reader.setReadGraphLabels();

  std::cerr << "* Reading '" << filename << "'...";

  reader( filename, simplicialComplexes );

  std::cerr << "finished\n"
            << "* Read " << simplicialComplexes.size() << " simplicial complexes\n";

  // Expand simplicial complexes ---------------------------------------

  aleph::geometry::RipsExpander<SimplicialComplex> expander;

  // TODO: make expansion configurable; does it make sense to expand the
  // complexes at all?

  // Calculate degrees -------------------------------------------------

  std::cerr << "* Calculating degree-based filtration...";

  for( auto&& K : simplicialComplexes )
  {
    std::vector<DataType> degrees;
    aleph::topology::filtrations::degrees( K, std::back_inserter( degrees ) );

    K = expander.assignMaximumData( K, degrees.begin(), degrees.end() );

    K.sort( aleph::topology::filtrations::Data<Simplex>() );
  }

  std::cerr << "finished\n";
}
<|endoftext|>


<commit_before>/*
 * Copyright 2017 WebAssembly Community Group participants
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//
// wasm-emscripten-finalize console tool
// Performs Emscripten-specific transforms on .wasm files
//

#include <exception>

#include "abi/js.h"
#include "ir/trapping.h"
#include "support/colors.h"
#include "support/debug.h"
#include "support/file.h"
#include "tool-options.h"
#include "wasm-binary.h"
#include "wasm-emscripten.h"
#include "wasm-io.h"
#include "wasm-printing.h"
#include "wasm-validator.h"

#define DEBUG_TYPE "emscripten"

using namespace cashew;
using namespace wasm;

int main(int argc, const char* argv[]) {
  const uint64_t INVALID_BASE = -1;

  std::string infile;
  std::string outfile;
  std::string inputSourceMapFilename;
  std::string outputSourceMapFilename;
  std::string outputSourceMapUrl;
  std::string dataSegmentFile;
  bool emitBinary = true;
  bool debugInfo = false;
  bool DWARF = false;
  bool sideModule = false;
  bool legacyPIC = true;
  bool legalizeJavaScriptFFI = true;
  bool bigInt = false;
  bool checkStackOverflow = false;
  uint64_t globalBase = INVALID_BASE;
  bool standaloneWasm = false;
  // TODO: remove after https://github.com/WebAssembly/binaryen/issues/3043
  bool minimizeWasmChanges = false;
  bool noDynCalls = false;
  bool onlyI64DynCalls = false;

  ToolOptions options("wasm-emscripten-finalize",
                      "Performs Emscripten-specific transforms on .wasm files");
  options
    .add("--output",
         "-o",
         "Output file",
         Options::Arguments::One,
         [&outfile](Options*, const std::string& argument) {
           outfile = argument;
           Colors::setEnabled(false);
         })
    .add("--debuginfo",
         "-g",
         "Emit names section in wasm binary (or full debuginfo in wast)",
         Options::Arguments::Zero,
         [&debugInfo](Options*, const std::string&) { debugInfo = true; })
    .add("--dwarf",
         "",
         "Update DWARF debug info",
         Options::Arguments::Zero,
         [&DWARF](Options*, const std::string&) { DWARF = true; })
    .add("--emit-text",
         "-S",
         "Emit text instead of binary for the output file. "
         "In this mode if no output file is specified, we write to stdout.",
         Options::Arguments::Zero,
         [&emitBinary](Options*, const std::string&) { emitBinary = false; })
    .add("--global-base",
         "",
         "The address at which static globals were placed",
         Options::Arguments::One,
         [&globalBase](Options*, const std::string& argument) {
           globalBase = std::stoull(argument);
         })
    // TODO(sbc): Remove this one this argument is no longer passed by
    // emscripten. See https://github.com/emscripten-core/emscripten/issues/8905
    .add("--initial-stack-pointer",
         "",
         "ignored - will be removed in a future release",
         Options::Arguments::One,
         [](Options*, const std::string& argument) {})
    .add("--side-module",
         "",
         "Input is an emscripten side module",
         Options::Arguments::Zero,
         [&sideModule](Options* o, const std::string& argument) {
           sideModule = true;
         })
    .add("--new-pic-abi",
         "",
         "Use new/llvm PIC abi",
         Options::Arguments::Zero,
         [&legacyPIC](Options* o, const std::string& argument) {
           legacyPIC = false;
         })
    .add("--input-source-map",
         "-ism",
         "Consume source map from the specified file",
         Options::Arguments::One,
         [&inputSourceMapFilename](Options* o, const std::string& argument) {
           inputSourceMapFilename = argument;
         })
    .add("--no-legalize-javascript-ffi",
         "-nj",
         "Do not fully legalize (i64->i32, "
         "f32->f64) the imports and exports for interfacing with JS",
         Options::Arguments::Zero,
         [&legalizeJavaScriptFFI](Options* o, const std::string&) {
           legalizeJavaScriptFFI = false;
         })
    .add("--bigint",
         "-bi",
         "Assume JS will use wasm/JS BigInt integration, so wasm i64s will "
         "turn into JS BigInts, and there is no need for any legalization at "
         "all (not even minimal legalization of dynCalls)",
         Options::Arguments::Zero,
         [&bigInt](Options* o, const std::string&) { bigInt = true; })
    .add("--output-source-map",
         "-osm",
         "Emit source map to the specified file",
         Options::Arguments::One,
         [&outputSourceMapFilename](Options* o, const std::string& argument) {
           outputSourceMapFilename = argument;
         })
    .add("--output-source-map-url",
         "-osu",
         "Emit specified string as source map URL",
         Options::Arguments::One,
         [&outputSourceMapUrl](Options* o, const std::string& argument) {
           outputSourceMapUrl = argument;
         })
    .add("--separate-data-segments",
         "",
         "Separate data segments to a file",
         Options::Arguments::One,
         [&dataSegmentFile](Options* o, const std::string& argument) {
           dataSegmentFile = argument;
         })
    .add("--check-stack-overflow",
         "",
         "Check for stack overflows every time the stack is extended",
         Options::Arguments::Zero,
         [&checkStackOverflow](Options* o, const std::string&) {
           checkStackOverflow = true;
         })
    .add("--standalone-wasm",
         "",
         "Emit a wasm file that does not depend on JS, as much as possible,"
         " using wasi and other standard conventions etc. where possible",
         Options::Arguments::Zero,
         [&standaloneWasm](Options* o, const std::string&) {
           standaloneWasm = true;
         })
    .add("--minimize-wasm-changes",
         "",
         "Modify the wasm as little as possible. This is useful during "
         "development as we reduce the number of changes to the wasm, as it "
         "lets emscripten control how much modifications to do.",
         Options::Arguments::Zero,
         [&minimizeWasmChanges](Options* o, const std::string&) {
           minimizeWasmChanges = true;
         })
    .add("--no-dyncalls",
         "",
         "",
         Options::Arguments::Zero,
         [&noDynCalls](Options* o, const std::string&) { noDynCalls = true; })
    .add("--dyncalls-i64",
         "",
         "",
         Options::Arguments::Zero,
         [&onlyI64DynCalls](Options* o, const std::string&) {
           onlyI64DynCalls = true;
         })
    .add_positional("INFILE",
                    Options::Arguments::One,
                    [&infile](Options* o, const std::string& argument) {
                      infile = argument;
                    });
  options.parse(argc, argv);

  if (infile == "") {
    Fatal() << "Need to specify an infile\n";
  }

  Module wasm;
  ModuleReader reader;
  reader.setDWARF(DWARF);
  try {
    reader.read(infile, wasm, inputSourceMapFilename);
  } catch (ParseException& p) {
    p.dump(std::cerr);
    std::cerr << '\n';
    Fatal() << "error in parsing input";
  } catch (MapParseException& p) {
    p.dump(std::cerr);
    std::cerr << '\n';
    Fatal() << "error in parsing wasm source map";
  }

  options.applyFeatures(wasm);

  BYN_TRACE_WITH_TYPE("emscripten-dump", "Module before:\n");
  BYN_DEBUG_WITH_TYPE("emscripten-dump",
                      WasmPrinter::printModule(&wasm, std::cerr));

  uint32_t dataSize = 0;

  if (!sideModule) {
    if (globalBase == INVALID_BASE) {
      Fatal() << "globalBase must be set";
    }
    Export* dataEndExport = wasm.getExport("__data_end");
    if (dataEndExport == nullptr) {
      Fatal() << "__data_end export not found";
    }
    Global* dataEnd = wasm.getGlobal(dataEndExport->value);
    if (dataEnd == nullptr) {
      Fatal() << "__data_end global not found";
    }
    if (dataEnd->type != Type::i32) {
      Fatal() << "__data_end global has wrong type";
    }
    if (dataEnd->imported()) {
      Fatal() << "__data_end must not be an imported global";
    }
    Const* dataEndConst = dataEnd->init->cast<Const>();
    dataSize = dataEndConst->value.geti32() - globalBase;
  }

  EmscriptenGlueGenerator generator(wasm);
  generator.standalone = standaloneWasm;
  generator.sideModule = sideModule;
  generator.minimizeWasmChanges = minimizeWasmChanges;
  generator.onlyI64DynCalls = onlyI64DynCalls;
  generator.noDynCalls = noDynCalls;

  std::vector<Name> initializerFunctions;

  // The wasm backend emits "__indirect_function_table" as the import name for
  // the table, while older emscripten expects "table"
  if (wasm.table.imported() && !minimizeWasmChanges) {
    wasm.table.base = Name("table");
  }
  wasm.updateMaps();

  if (!standaloneWasm) {
    // This is also not needed in standalone mode since standalone mode uses
    // crt1.c to invoke the main and is aware of __main_argc_argv mangling.
    generator.renameMainArgcArgv();
  }

  PassRunner passRunner(&wasm, options.passOptions);
  passRunner.setDebug(options.debug);
  passRunner.setDebugInfo(debugInfo);

  if (checkStackOverflow && !sideModule) {
    if (!standaloneWasm) {
      // In standalone mode we don't set a handler at all.. which means
      // just trap on overflow.
      passRunner.options.arguments["stack-check-handler"] =
        "__handle_stack_overflow";
    }
    passRunner.add("stack-check");
  }

  if (sideModule) {
    passRunner.add("replace-stack-pointer");
  }

  if (legacyPIC) {
    if (sideModule) {
      passRunner.add("emscripten-pic");
    } else {
      passRunner.add("emscripten-pic-main-module");
    }
  }

  if (!noDynCalls && !standaloneWasm) {
    // If not standalone wasm then JS is relevant and we need dynCalls.
    if (onlyI64DynCalls) {
      passRunner.add("generate-i64-dyncalls");
    } else {
      passRunner.add("generate-dyncalls");
    }
  }

  // Legalize the wasm, if BigInts don't make that moot.
  if (!bigInt) {
    passRunner.add(ABI::getLegalizationPass(
      legalizeJavaScriptFFI ? ABI::LegalizationLevel::Full
                            : ABI::LegalizationLevel::Minimal));
  }

  // Strip target features section (its information is in the metadata)
  passRunner.add("strip-target-features");

  // If DWARF is unused, strip it out. This avoids us keeping it alive
  // until wasm-opt strips it later.
  if (!DWARF) {
    passRunner.add("strip-dwarf");
  }

  passRunner.run();

  if (sideModule) {
    BYN_TRACE("finalizing as side module\n");
    generator.generatePostInstantiateFunction();
  } else {
    BYN_TRACE("finalizing as regular module\n");
    generator.internalizeStackPointerGlobal();
    // For side modules these gets called via __post_instantiate
    if (Function* F = wasm.getFunctionOrNull(ASSIGN_GOT_ENTRIES)) {
      auto* ex = new Export();
      ex->value = F->name;
      ex->name = F->name;
      ex->kind = ExternalKind::Function;
      wasm.addExport(ex);
      initializerFunctions.push_back(F->name);
    }
    // Costructors get called from crt1 in wasm standalone mode.
    // Unless there is no entry point.
    if (!standaloneWasm || !wasm.getExportOrNull("_start")) {
      if (auto* e = wasm.getExportOrNull(WASM_CALL_CTORS)) {
        initializerFunctions.push_back(e->name);
      }
    }
  }

  BYN_TRACE("generated metadata\n");
  // Substantial changes to the wasm are done, enough to create the metadata.
  std::string metadata =
    generator.generateEmscriptenMetadata(dataSize, initializerFunctions);

  // Finally, separate out data segments if relevant (they may have been needed
  // for metadata).
  if (!dataSegmentFile.empty()) {
    Output memInitFile(dataSegmentFile, Flags::Binary);
    if (globalBase == INVALID_BASE) {
      Fatal() << "globalBase must be set";
    }
    generator.separateDataSegments(&memInitFile, globalBase);
  }

  BYN_TRACE_WITH_TYPE("emscripten-dump", "Module after:\n");
  BYN_DEBUG_WITH_TYPE("emscripten-dump",
                      WasmPrinter::printModule(&wasm, std::cerr));

  // Write the modified wasm if the user asked us to, either by specifying an
  // output file, or requesting text output (which goes to stdout by default).
  if (outfile.size() > 0 || !emitBinary) {
    Output output(outfile, emitBinary ? Flags::Binary : Flags::Text);
    ModuleWriter writer;
    writer.setDebugInfo(debugInfo);
    // writer.setSymbolMap(symbolMap);
    writer.setBinary(emitBinary);
    if (outputSourceMapFilename.size()) {
      writer.setSourceMapFilename(outputSourceMapFilename);
      writer.setSourceMapUrl(outputSourceMapUrl);
    }
    writer.write(wasm, output);
    if (!emitBinary) {
      output << "(;\n";
      output << "--BEGIN METADATA --\n" << metadata << "-- END METADATA --\n";
      output << ";)\n";
    }
  }
  // If we emit text then we emitted the metadata together with that text
  // earlier. Otherwise emit it to stdout.
  if (emitBinary) {
    std::cout << metadata;
  }
  return 0;
}
<commit_msg>finalize: move more functionality behind legacyPIC (#3248)<commit_after>/*
 * Copyright 2017 WebAssembly Community Group participants
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//
// wasm-emscripten-finalize console tool
// Performs Emscripten-specific transforms on .wasm files
//

#include <exception>

#include "abi/js.h"
#include "ir/trapping.h"
#include "support/colors.h"
#include "support/debug.h"
#include "support/file.h"
#include "tool-options.h"
#include "wasm-binary.h"
#include "wasm-emscripten.h"
#include "wasm-io.h"
#include "wasm-printing.h"
#include "wasm-validator.h"

#define DEBUG_TYPE "emscripten"

using namespace cashew;
using namespace wasm;

int main(int argc, const char* argv[]) {
  const uint64_t INVALID_BASE = -1;

  std::string infile;
  std::string outfile;
  std::string inputSourceMapFilename;
  std::string outputSourceMapFilename;
  std::string outputSourceMapUrl;
  std::string dataSegmentFile;
  bool emitBinary = true;
  bool debugInfo = false;
  bool DWARF = false;
  bool sideModule = false;
  bool legacyPIC = true;
  bool legalizeJavaScriptFFI = true;
  bool bigInt = false;
  bool checkStackOverflow = false;
  uint64_t globalBase = INVALID_BASE;
  bool standaloneWasm = false;
  // TODO: remove after https://github.com/WebAssembly/binaryen/issues/3043
  bool minimizeWasmChanges = false;
  bool noDynCalls = false;
  bool onlyI64DynCalls = false;

  ToolOptions options("wasm-emscripten-finalize",
                      "Performs Emscripten-specific transforms on .wasm files");
  options
    .add("--output",
         "-o",
         "Output file",
         Options::Arguments::One,
         [&outfile](Options*, const std::string& argument) {
           outfile = argument;
           Colors::setEnabled(false);
         })
    .add("--debuginfo",
         "-g",
         "Emit names section in wasm binary (or full debuginfo in wast)",
         Options::Arguments::Zero,
         [&debugInfo](Options*, const std::string&) { debugInfo = true; })
    .add("--dwarf",
         "",
         "Update DWARF debug info",
         Options::Arguments::Zero,
         [&DWARF](Options*, const std::string&) { DWARF = true; })
    .add("--emit-text",
         "-S",
         "Emit text instead of binary for the output file. "
         "In this mode if no output file is specified, we write to stdout.",
         Options::Arguments::Zero,
         [&emitBinary](Options*, const std::string&) { emitBinary = false; })
    .add("--global-base",
         "",
         "The address at which static globals were placed",
         Options::Arguments::One,
         [&globalBase](Options*, const std::string& argument) {
           globalBase = std::stoull(argument);
         })
    // TODO(sbc): Remove this one this argument is no longer passed by
    // emscripten. See https://github.com/emscripten-core/emscripten/issues/8905
    .add("--initial-stack-pointer",
         "",
         "ignored - will be removed in a future release",
         Options::Arguments::One,
         [](Options*, const std::string& argument) {})
    .add("--side-module",
         "",
         "Input is an emscripten side module",
         Options::Arguments::Zero,
         [&sideModule](Options* o, const std::string& argument) {
           sideModule = true;
         })
    .add("--new-pic-abi",
         "",
         "Use new/llvm PIC abi",
         Options::Arguments::Zero,
         [&legacyPIC](Options* o, const std::string& argument) {
           legacyPIC = false;
         })
    .add("--input-source-map",
         "-ism",
         "Consume source map from the specified file",
         Options::Arguments::One,
         [&inputSourceMapFilename](Options* o, const std::string& argument) {
           inputSourceMapFilename = argument;
         })
    .add("--no-legalize-javascript-ffi",
         "-nj",
         "Do not fully legalize (i64->i32, "
         "f32->f64) the imports and exports for interfacing with JS",
         Options::Arguments::Zero,
         [&legalizeJavaScriptFFI](Options* o, const std::string&) {
           legalizeJavaScriptFFI = false;
         })
    .add("--bigint",
         "-bi",
         "Assume JS will use wasm/JS BigInt integration, so wasm i64s will "
         "turn into JS BigInts, and there is no need for any legalization at "
         "all (not even minimal legalization of dynCalls)",
         Options::Arguments::Zero,
         [&bigInt](Options* o, const std::string&) { bigInt = true; })
    .add("--output-source-map",
         "-osm",
         "Emit source map to the specified file",
         Options::Arguments::One,
         [&outputSourceMapFilename](Options* o, const std::string& argument) {
           outputSourceMapFilename = argument;
         })
    .add("--output-source-map-url",
         "-osu",
         "Emit specified string as source map URL",
         Options::Arguments::One,
         [&outputSourceMapUrl](Options* o, const std::string& argument) {
           outputSourceMapUrl = argument;
         })
    .add("--separate-data-segments",
         "",
         "Separate data segments to a file",
         Options::Arguments::One,
         [&dataSegmentFile](Options* o, const std::string& argument) {
           dataSegmentFile = argument;
         })
    .add("--check-stack-overflow",
         "",
         "Check for stack overflows every time the stack is extended",
         Options::Arguments::Zero,
         [&checkStackOverflow](Options* o, const std::string&) {
           checkStackOverflow = true;
         })
    .add("--standalone-wasm",
         "",
         "Emit a wasm file that does not depend on JS, as much as possible,"
         " using wasi and other standard conventions etc. where possible",
         Options::Arguments::Zero,
         [&standaloneWasm](Options* o, const std::string&) {
           standaloneWasm = true;
         })
    .add("--minimize-wasm-changes",
         "",
         "Modify the wasm as little as possible. This is useful during "
         "development as we reduce the number of changes to the wasm, as it "
         "lets emscripten control how much modifications to do.",
         Options::Arguments::Zero,
         [&minimizeWasmChanges](Options* o, const std::string&) {
           minimizeWasmChanges = true;
         })
    .add("--no-dyncalls",
         "",
         "",
         Options::Arguments::Zero,
         [&noDynCalls](Options* o, const std::string&) { noDynCalls = true; })
    .add("--dyncalls-i64",
         "",
         "",
         Options::Arguments::Zero,
         [&onlyI64DynCalls](Options* o, const std::string&) {
           onlyI64DynCalls = true;
         })
    .add_positional("INFILE",
                    Options::Arguments::One,
                    [&infile](Options* o, const std::string& argument) {
                      infile = argument;
                    });
  options.parse(argc, argv);

  if (infile == "") {
    Fatal() << "Need to specify an infile\n";
  }

  Module wasm;
  ModuleReader reader;
  reader.setDWARF(DWARF);
  try {
    reader.read(infile, wasm, inputSourceMapFilename);
  } catch (ParseException& p) {
    p.dump(std::cerr);
    std::cerr << '\n';
    Fatal() << "error in parsing input";
  } catch (MapParseException& p) {
    p.dump(std::cerr);
    std::cerr << '\n';
    Fatal() << "error in parsing wasm source map";
  }

  options.applyFeatures(wasm);

  BYN_TRACE_WITH_TYPE("emscripten-dump", "Module before:\n");
  BYN_DEBUG_WITH_TYPE("emscripten-dump",
                      WasmPrinter::printModule(&wasm, std::cerr));

  uint32_t dataSize = 0;

  if (!sideModule) {
    if (globalBase == INVALID_BASE) {
      Fatal() << "globalBase must be set";
    }
    Export* dataEndExport = wasm.getExport("__data_end");
    if (dataEndExport == nullptr) {
      Fatal() << "__data_end export not found";
    }
    Global* dataEnd = wasm.getGlobal(dataEndExport->value);
    if (dataEnd == nullptr) {
      Fatal() << "__data_end global not found";
    }
    if (dataEnd->type != Type::i32) {
      Fatal() << "__data_end global has wrong type";
    }
    if (dataEnd->imported()) {
      Fatal() << "__data_end must not be an imported global";
    }
    Const* dataEndConst = dataEnd->init->cast<Const>();
    dataSize = dataEndConst->value.geti32() - globalBase;
  }

  EmscriptenGlueGenerator generator(wasm);
  generator.standalone = standaloneWasm;
  generator.sideModule = sideModule;
  generator.minimizeWasmChanges = minimizeWasmChanges;
  generator.onlyI64DynCalls = onlyI64DynCalls;
  generator.noDynCalls = noDynCalls;

  std::vector<Name> initializerFunctions;

  // The wasm backend emits "__indirect_function_table" as the import name for
  // the table, while older emscripten expects "table"
  if (wasm.table.imported() && !minimizeWasmChanges) {
    wasm.table.base = Name("table");
  }
  wasm.updateMaps();

  if (!standaloneWasm) {
    // This is also not needed in standalone mode since standalone mode uses
    // crt1.c to invoke the main and is aware of __main_argc_argv mangling.
    generator.renameMainArgcArgv();
  }

  PassRunner passRunner(&wasm, options.passOptions);
  passRunner.setDebug(options.debug);
  passRunner.setDebugInfo(debugInfo);

  if (checkStackOverflow && !sideModule) {
    if (!standaloneWasm) {
      // In standalone mode we don't set a handler at all.. which means
      // just trap on overflow.
      passRunner.options.arguments["stack-check-handler"] =
        "__handle_stack_overflow";
    }
    passRunner.add("stack-check");
  }

  if (sideModule) {
    passRunner.add("replace-stack-pointer");
  }

  if (legacyPIC) {
    if (sideModule) {
      passRunner.add("emscripten-pic");
    } else {
      passRunner.add("emscripten-pic-main-module");
    }
  }

  if (!noDynCalls && !standaloneWasm) {
    // If not standalone wasm then JS is relevant and we need dynCalls.
    if (onlyI64DynCalls) {
      passRunner.add("generate-i64-dyncalls");
    } else {
      passRunner.add("generate-dyncalls");
    }
  }

  // Legalize the wasm, if BigInts don't make that moot.
  if (!bigInt) {
    passRunner.add(ABI::getLegalizationPass(
      legalizeJavaScriptFFI ? ABI::LegalizationLevel::Full
                            : ABI::LegalizationLevel::Minimal));
  }

  // Strip target features section (its information is in the metadata)
  passRunner.add("strip-target-features");

  // If DWARF is unused, strip it out. This avoids us keeping it alive
  // until wasm-opt strips it later.
  if (!DWARF) {
    passRunner.add("strip-dwarf");
  }

  passRunner.run();

  if (sideModule) {
    BYN_TRACE("finalizing as side module\n");
    generator.generatePostInstantiateFunction();
  } else {
    BYN_TRACE("finalizing as regular module\n");
    if (legacyPIC) {
      generator.internalizeStackPointerGlobal();
      // For side modules these gets called via __post_instantiate
      if (Function* F = wasm.getFunctionOrNull(ASSIGN_GOT_ENTRIES)) {
        auto* ex = new Export();
        ex->value = F->name;
        ex->name = F->name;
        ex->kind = ExternalKind::Function;
        wasm.addExport(ex);
        initializerFunctions.push_back(F->name);
      }
    }
    // Costructors get called from crt1 in wasm standalone mode.
    // Unless there is no entry point.
    if (!standaloneWasm || !wasm.getExportOrNull("_start")) {
      if (auto* e = wasm.getExportOrNull(WASM_CALL_CTORS)) {
        initializerFunctions.push_back(e->name);
      }
    }
  }

  BYN_TRACE("generated metadata\n");
  // Substantial changes to the wasm are done, enough to create the metadata.
  std::string metadata =
    generator.generateEmscriptenMetadata(dataSize, initializerFunctions);

  // Finally, separate out data segments if relevant (they may have been needed
  // for metadata).
  if (!dataSegmentFile.empty()) {
    Output memInitFile(dataSegmentFile, Flags::Binary);
    if (globalBase == INVALID_BASE) {
      Fatal() << "globalBase must be set";
    }
    generator.separateDataSegments(&memInitFile, globalBase);
  }

  BYN_TRACE_WITH_TYPE("emscripten-dump", "Module after:\n");
  BYN_DEBUG_WITH_TYPE("emscripten-dump",
                      WasmPrinter::printModule(&wasm, std::cerr));

  // Write the modified wasm if the user asked us to, either by specifying an
  // output file, or requesting text output (which goes to stdout by default).
  if (outfile.size() > 0 || !emitBinary) {
    Output output(outfile, emitBinary ? Flags::Binary : Flags::Text);
    ModuleWriter writer;
    writer.setDebugInfo(debugInfo);
    // writer.setSymbolMap(symbolMap);
    writer.setBinary(emitBinary);
    if (outputSourceMapFilename.size()) {
      writer.setSourceMapFilename(outputSourceMapFilename);
      writer.setSourceMapUrl(outputSourceMapUrl);
    }
    writer.write(wasm, output);
    if (!emitBinary) {
      output << "(;\n";
      output << "--BEGIN METADATA --\n" << metadata << "-- END METADATA --\n";
      output << ";)\n";
    }
  }
  // If we emit text then we emitted the metadata together with that text
  // earlier. Otherwise emit it to stdout.
  if (emitBinary) {
    std::cout << metadata;
  }
  return 0;
}
<|endoftext|>


<commit_before>/* Generated from orogen/lib/orogen/templates/typekit/corba/Convertions.hpp */

#ifndef OROCOS_<%= typekit.name.upcase %>_CORBA_CONVERTIONS_HPP
#define OROCOS_<%= typekit.name.upcase %>_CORBA_CONVERTIONS_HPP

#include "Types.hpp"
#include "<%= typekit.name %>/transports/corba/<%= typekit.name %>TypesC.h"
#include <boost/cstdint.hpp>

namespace orogen_typekits {
    <% typesets.converted_types.each do |type| %>
    bool toCORBA( <%= type.corba_ref_type %> corba, <%= type.arg_type %> value );
    bool fromCORBA( <%= type.ref_type %> value, <%= type.corba_arg_type %> corba );
    <% end %>
    <% typesets.array_types.each do |type| %>
    bool toCORBA( <%= type.corba_ref_type %> corba, <%= type.arg_type %> value, int length );
    bool fromCORBA( <%= type.ref_type %> value, int length, <%= type.corba_arg_type %> corba );
    <% end %>
    <% typesets.opaque_types.each do |opdef|
        type = opdef.type
        intermediate_type = typekit.find_type(opdef.intermediate)
        %>
    bool toCORBA( <%= intermediate_type.corba_ref_type %> corba, <%= type.arg_type %> value );
    bool fromCORBA( <%= type.ref_type %> value, <%= intermediate_type.corba_arg_type %> corba );
    <% end %>
}

#endif

<commit_msg>corba: document in generated code which conversion is generated.<commit_after>/* Generated from orogen/lib/orogen/templates/typekit/corba/Convertions.hpp */

#ifndef OROCOS_<%= typekit.name.upcase %>_CORBA_CONVERTIONS_HPP
#define OROCOS_<%= typekit.name.upcase %>_CORBA_CONVERTIONS_HPP

#include "Types.hpp"
#include "<%= typekit.name %>/transports/corba/<%= typekit.name %>TypesC.h"
#include <boost/cstdint.hpp>

namespace orogen_typekits {
    /** Converted types: */
    <% typesets.converted_types.each do |type| %>
    bool toCORBA( <%= type.corba_ref_type %> corba, <%= type.arg_type %> value );
    bool fromCORBA( <%= type.ref_type %> value, <%= type.corba_arg_type %> corba );
    <% end %>
    /** Array types: */
    <% typesets.array_types.each do |type| %>
    bool toCORBA( <%= type.corba_ref_type %> corba, <%= type.arg_type %> value, int length );
    bool fromCORBA( <%= type.ref_type %> value, int length, <%= type.corba_arg_type %> corba );
    <% end %>
    /** Opaque types: */
    <% typesets.opaque_types.each do |opdef|
        type = opdef.type
        intermediate_type = typekit.find_type(opdef.intermediate)
        %>
    bool toCORBA( <%= intermediate_type.corba_ref_type %> corba, <%= type.arg_type %> value );
    bool fromCORBA( <%= type.ref_type %> value, <%= intermediate_type.corba_arg_type %> corba );
    <% end %>
}

#endif

<|endoftext|>