<commit_before>/// Synchronization mechanism used to signal one or more other threads.
// Usually, one thread signals an event, while one or more other threads wait for an event to become signaled.
// Note: event userdata are copyable/sharable between threads.
// @module event

#include "Event.h"
#include "Poco/Exception.h"

LUA_API int luaopen_poco_event(lua_State* L)
{
    return LuaPoco::loadConstructor(L, LuaPoco::EventUserdata::Event);
}

namespace LuaPoco
{

const char* POCO_EVENT_METATABLE_NAME = "Poco.Event.metatable";

EventUserdata::EventUserdata(bool autoReset) :
    mEvent(new Poco::Event(autoReset))
{
}

EventUserdata::EventUserdata(const Poco::SharedPtr<Poco::Event>& fm) :
    mEvent(fm)
{
}

EventUserdata::~EventUserdata()
{
}

bool EventUserdata::copyToState(lua_State *L)
{
    EventUserdata* eud = new(lua_newuserdata(L, sizeof *eud)) EventUserdata(mEvent);
    setupPocoUserdata(L, eud, POCO_EVENT_METATABLE_NAME);
    return true;
}

// register metatable for this class
bool EventUserdata::registerEvent(lua_State* L)
{
    struct UserdataMethod methods[] = 
    {
        { "__gc", metamethod__gc },
        { "__tostring", metamethod__tostring },
        { "set", set },
        { "tryWait", tryWait },
        { "wait", wait },
        { "reset", reset },
        { NULL, NULL}
    };
    
    setupUserdataMetatable(L, POCO_EVENT_METATABLE_NAME, methods);
    return true;
}

/// create a new event userdata.
// @bool[opt] autoReset optional boolean indicating if the event should auto reset after wait() succesfully returns.
// [default = true]
// @return userdata or nil. (error)
// @return error message.
// @function new
int EventUserdata::Event(lua_State* L)
{
    int rv = 0;
    int firstArg = lua_istable(L, 1) ? 2 : 1;
    bool autoReset = true;
    if (lua_gettop(L) > 0)
    {
        luaL_checktype(L, firstArg, LUA_TBOOLEAN);
        autoReset = lua_toboolean(L, firstArg);
    }
    
    try
    {
        EventUserdata* eud = new(lua_newuserdata(L, sizeof *eud)) EventUserdata(autoReset);
        setupPocoUserdata(L, eud, POCO_EVENT_METATABLE_NAME);
        rv = 1;
    }
    catch (const Poco::Exception& e)
    {
        rv = pushPocoException(L, e);
    }
    catch (...)
    {
        rv = pushUnknownException(L);
    }
    return rv;
}

///
// @type event

// metamethod infrastructure
int EventUserdata::metamethod__tostring(lua_State* L)<commit_msg>bug fix for Event.<commit_after>/// Synchronization mechanism used to signal one or more other threads.
// Usually, one thread signals an event, while one or more other threads wait for an event to become signaled.
// Note: event userdata are copyable/sharable between threads.
// @module event

#include "Event.h"
#include "Poco/Exception.h"

LUA_API int luaopen_poco_event(lua_State* L)
{
    return LuaPoco::loadConstructor(L, LuaPoco::EventUserdata::Event);
}

namespace LuaPoco
{

const char* POCO_EVENT_METATABLE_NAME = "Poco.Event.metatable";

EventUserdata::EventUserdata(bool autoReset) :
    mEvent(new Poco::Event(autoReset))
{
}

EventUserdata::EventUserdata(const Poco::SharedPtr<Poco::Event>& fm) :
    mEvent(fm)
{
}

EventUserdata::~EventUserdata()
{
}

bool EventUserdata::copyToState(lua_State *L)
{
    EventUserdata* eud = new(lua_newuserdata(L, sizeof *eud)) EventUserdata(mEvent);
    setupPocoUserdata(L, eud, POCO_EVENT_METATABLE_NAME);
    return true;
}

// register metatable for this class
bool EventUserdata::registerEvent(lua_State* L)
{
    struct UserdataMethod methods[] = 
    {
        { "__gc", metamethod__gc },
        { "__tostring", metamethod__tostring },
        { "set", set },
        { "tryWait", tryWait },
        { "wait", wait },
        { "reset", reset },
        { NULL, NULL}
    };
    
    setupUserdataMetatable(L, POCO_EVENT_METATABLE_NAME, methods);
    return true;
}

/// create a new event userdata.
// @bool[opt] autoReset optional boolean indicating if the event should auto reset after wait() succesfully returns.
// [default = true]
// @return userdata or nil. (error)
// @return error message.
// @function new
int EventUserdata::Event(lua_State* L)
{
    int rv = 0;
    int firstArg = lua_istable(L, 1) ? 2 : 1;
    bool autoReset = true;
    if (lua_gettop(L) > firstArg)
    {
        luaL_checktype(L, firstArg, LUA_TBOOLEAN);
        autoReset = lua_toboolean(L, firstArg);
    }
    
    try
    {
        EventUserdata* eud = new(lua_newuserdata(L, sizeof *eud)) EventUserdata(autoReset);
        setupPocoUserdata(L, eud, POCO_EVENT_METATABLE_NAME);
        rv = 1;
    }
    catch (const Poco::Exception& e)
    {
        rv = pushPocoException(L, e);
    }
    catch (...)
    {
        rv = pushUnknownException(L);
    }
    return rv;
}

///
// @type event

// metamethod infrastructure
int EventUserdata::metamethod__tostring(lua_State* L)
{'fraction_non_alphanumeric': 0.121996685998343, 'fraction_numerical': 0.0022783761391880694, 'mean_word_length': 3.338724168912848, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '739184', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1647, 'n_words': 394}