2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** binding-util.cpp
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
|
|
|
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
|
|
|
|
**
|
|
|
|
** mkxp is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 2 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** mkxp 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 General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "binding-util.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "exception.h"
|
|
|
|
|
2014-08-09 16:35:01 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
#define SYMD(symbol) { CS##symbol, #symbol }
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
CommonSymbol ind;
|
|
|
|
const char *str;
|
|
|
|
} static const symData[] =
|
|
|
|
{
|
|
|
|
SYMD(font),
|
|
|
|
SYMD(viewport),
|
|
|
|
SYMD(bitmap),
|
|
|
|
SYMD(color),
|
2014-09-05 20:53:19 +00:00
|
|
|
SYMD(out_color),
|
2013-09-01 14:27:21 +00:00
|
|
|
SYMD(tone),
|
|
|
|
SYMD(rect),
|
|
|
|
SYMD(src_rect),
|
|
|
|
SYMD(tileset),
|
|
|
|
SYMD(autotiles),
|
|
|
|
SYMD(map_data),
|
|
|
|
SYMD(flash_data),
|
|
|
|
SYMD(priorities),
|
|
|
|
SYMD(windowskin),
|
|
|
|
SYMD(contents),
|
|
|
|
SYMD(cursor_rect),
|
|
|
|
SYMD(path),
|
|
|
|
SYMD(array),
|
2014-08-09 16:35:01 +00:00
|
|
|
SYMD(default_color),
|
2014-09-05 20:53:19 +00:00
|
|
|
SYMD(default_out_color),
|
2014-08-09 16:35:01 +00:00
|
|
|
SYMD(children),
|
|
|
|
SYMD(dispose)
|
2013-09-01 14:27:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static elementsN(symData);
|
|
|
|
|
|
|
|
struct MrbExcData
|
|
|
|
{
|
|
|
|
MrbException ind;
|
|
|
|
const char *str;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const MrbExcData excData[] =
|
|
|
|
{
|
|
|
|
{ Shutdown, "SystemExit" },
|
|
|
|
{ RGSS, "RGSSError" },
|
|
|
|
{ PHYSFS, "PHYSFSError" },
|
|
|
|
{ SDL, "SDLError" },
|
2013-09-03 13:22:00 +00:00
|
|
|
{ MKXP, "MKXPError" },
|
|
|
|
{ IO, "IOError" }
|
2013-09-01 14:27:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static elementsN(excData);
|
|
|
|
|
|
|
|
#define ENO(id) { Errno##id, #id }
|
|
|
|
|
|
|
|
static const MrbExcData enoExcData[] =
|
|
|
|
{
|
2013-09-03 13:22:00 +00:00
|
|
|
ENO(E2BIG),
|
|
|
|
ENO(EACCES),
|
|
|
|
ENO(EAGAIN),
|
|
|
|
ENO(EBADF),
|
|
|
|
ENO(ECHILD),
|
|
|
|
ENO(EDEADLOCK),
|
|
|
|
ENO(EDOM),
|
|
|
|
ENO(EEXIST),
|
|
|
|
ENO(EINVAL),
|
|
|
|
ENO(EMFILE),
|
|
|
|
ENO(ENOENT),
|
|
|
|
ENO(ENOEXEC),
|
|
|
|
ENO(ENOMEM),
|
|
|
|
ENO(ENOSPC),
|
|
|
|
ENO(ERANGE),
|
|
|
|
ENO(EXDEV)
|
2013-09-01 14:27:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static elementsN(enoExcData);
|
|
|
|
|
|
|
|
MrbData::MrbData(mrb_state *mrb)
|
|
|
|
{
|
|
|
|
int arena = mrb_gc_arena_save(mrb);
|
|
|
|
|
2014-07-18 22:49:25 +00:00
|
|
|
for (size_t i = 0; i < excDataN; ++i)
|
2013-09-01 14:27:21 +00:00
|
|
|
exc[excData[i].ind] = mrb_define_class(mrb, excData[i].str, mrb->eException_class);
|
|
|
|
|
|
|
|
RClass *errnoMod = mrb_define_module(mrb, "Errno");
|
|
|
|
|
2014-07-18 22:49:25 +00:00
|
|
|
for (size_t i = 0; i < enoExcDataN; ++i)
|
2013-09-01 14:27:21 +00:00
|
|
|
exc[enoExcData[i].ind] =
|
|
|
|
mrb_define_class_under(mrb, errnoMod, enoExcData[i].str, mrb->eStandardError_class);
|
|
|
|
|
|
|
|
exc[TypeError] = mrb_class_get(mrb, "TypeError");
|
|
|
|
exc[ArgumentError] = mrb_class_get(mrb, "ArgumentError");
|
|
|
|
|
2014-07-18 22:49:25 +00:00
|
|
|
for (size_t i = 0; i < symDataN; ++i)
|
2013-12-27 03:36:24 +00:00
|
|
|
symbols[symData[i].ind] = mrb_intern_cstr(mrb, symData[i].str);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
mrb_gc_arena_restore(mrb, arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Indexed with Exception::Type */
|
|
|
|
static const MrbException excToMrbExc[] =
|
|
|
|
{
|
2013-09-03 13:22:00 +00:00
|
|
|
RGSS, /* RGSSError */
|
|
|
|
ErrnoENOENT, /* NoFileError */
|
|
|
|
IO,
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-09-03 13:22:00 +00:00
|
|
|
TypeError,
|
|
|
|
ArgumentError,
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-09-03 13:22:00 +00:00
|
|
|
PHYSFS, /* PHYSFSError */
|
|
|
|
SDL, /* SDLError */
|
|
|
|
MKXP /* MKXPError */
|
2013-09-01 14:27:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void raiseMrbExc(mrb_state *mrb, const Exception &exc)
|
|
|
|
{
|
|
|
|
MrbData *data = getMrbData(mrb);
|
|
|
|
RClass *excClass = data->exc[excToMrbExc[exc.type]];
|
|
|
|
|
2013-12-29 17:05:11 +00:00
|
|
|
mrb_raise(mrb, excClass, exc.msg.c_str());
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2014-08-09 16:35:01 +00:00
|
|
|
void
|
|
|
|
raiseDisposedAccess(mrb_state *mrb, mrb_value self)
|
|
|
|
{
|
|
|
|
const char *klassName = DATA_TYPE(self)->struct_name;
|
|
|
|
char buf[32];
|
|
|
|
|
|
|
|
strncpy(buf, klassName, sizeof(buf));
|
|
|
|
buf[0] = tolower(buf[0]);
|
|
|
|
|
|
|
|
mrb_raisef(mrb, getMrbData(mrb)->exc[RGSS],
|
|
|
|
"disposed %S", mrb_str_new_cstr(mrb, buf));
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
MRB_METHOD_PUB(inspectObject)
|
|
|
|
{
|
|
|
|
static char buffer[64];
|
2014-08-09 08:38:42 +00:00
|
|
|
snprintf(buffer, sizeof(buffer), "#<%s:%p>", DATA_TYPE(self)->struct_name, DATA_PTR(self));
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
return mrb_str_new_cstr(mrb, buffer);
|
|
|
|
}
|