From 30605e88ede9ad7e372dbfb2453559a673d3aa24 Mon Sep 17 00:00:00 2001 From: mertcandav Date: Fri, 30 Aug 2024 21:22:30 +0300 Subject: [PATCH] jule: completing implementation of new closures --- api/fn.hpp | 160 +++++------------------------------- src/julec/obj/cxx/type.jule | 2 +- 2 files changed, 21 insertions(+), 141 deletions(-) diff --git a/api/fn.hpp b/api/fn.hpp index 7ea5618f4..deff002e0 100644 --- a/api/fn.hpp +++ b/api/fn.hpp @@ -7,9 +7,15 @@ #include #include -#include #include +#ifdef OS_WINDOWS +#include +#else +#include +#include +#endif + #include "types.hpp" #include "error.hpp" #include "panic.hpp" @@ -20,134 +26,6 @@ #define __JULE_CO(EXPR) \ (__JULE_CO_SPAWN([=](void) mutable -> void { EXPR; }).detach()) -namespace jule -{ - - // std::function wrapper of JuleC. - template - struct Fn; - - template - jule::Uintptr addr_of_fn(std::function f) noexcept; - - template - struct Fn - { - public: - std::function buffer; - jule::Uintptr _addr; - - Fn(void) = default; - Fn(const Fn &fn) = default; - Fn(std::nullptr_t) : Fn() {} - - Fn(const std::function &function) noexcept - { - this->_addr = jule::addr_of_fn(function); - if (this->_addr == 0) - this->_addr = (jule::Uintptr)(&function); - this->buffer = function; - } - - Fn(const Function *function) noexcept - { - this->buffer = function; - this->_addr = jule::addr_of_fn(this->buffer); - if (this->_addr == 0) - this->_addr = (jule::Uintptr)(function); - } - - template - auto call( -#ifndef __JULE_ENABLE__PRODUCTION - const char *file, -#endif - Arguments... arguments) - { -#ifndef __JULE_DISABLE__SAFETY - if (this->buffer == nullptr) -#ifndef __JULE_ENABLE__PRODUCTION - jule::panic((std::string(__JULE_ERROR__INVALID_MEMORY) + "\nfile: ") + file); -#else - jule::panic(__JULE_ERROR__INVALID_MEMORY); -#endif // PRODUCTION -#endif // SAFETY - return this->buffer(arguments...); - } - - template - inline auto operator()(Arguments... arguments) - { -#ifndef __JULE_ENABLE__PRODUCTION - return this->call("/api/fn.hpp", arguments...); -#else - return this->call(arguments...); -#endif - } - - constexpr jule::Uintptr addr(void) const noexcept - { - return this->_addr; - } - - inline Fn &operator=(std::nullptr_t) noexcept - { - this->buffer = nullptr; - return *this; - } - - inline Fn &operator=(const std::function &function) - { - this->buffer = function; - return *this; - } - - inline Fn &operator=(const Function &function) - { - this->buffer = function; - return *this; - } - - constexpr jule::Bool operator==(std::nullptr_t) const noexcept - { - return this->buffer == nullptr; - } - - constexpr jule::Bool operator!=(std::nullptr_t) const noexcept - { - return !this->operator==(nullptr); - } - - friend std::ostream &operator<<(std::ostream &stream, - const Fn &src) noexcept - { - if (src == nullptr) - return (stream << ""); - return (stream << (void *)src._addr); - } - }; - - template - jule::Uintptr addr_of_fn(std::function f) noexcept - { - typedef T(FnType)(U...); - FnType **fn_ptr = f.template target(); - if (!fn_ptr) - return 0; - return (jule::Uintptr)(*fn_ptr); - } - -} // namespace jule - -#endif // ifndef __JULE_FN_HPP - -#ifdef OS_WINDOWS -#include -#else -#include -#include -#endif - #ifdef OS_WINDOWS #define __JULE_CLOSURE_MTX_INIT() InitializeSRWLock(&jule::__closure_mtx) #define __JULE_CLOSURE_MTX_LOCK() AcquireSRWLockExclusive(&jule::__closure_mtx) @@ -168,23 +46,23 @@ namespace jule { // std::function wrapper of JuleC. template - struct Fn2 + struct Fn { public: Ret (*f)(Args...); jule::Ptr ctx; // Closure ctx. void (*ctxHandler)(jule::Ptr &alloc) = nullptr; - Fn2(void) = default; - Fn2(const Fn2 &) = default; - Fn2(std::nullptr_t) : Fn2() {} + Fn(void) = default; + Fn(const Fn &) = default; + Fn(std::nullptr_t) : Fn() {} - Fn2(Ret (*f)(Args...)) noexcept + Fn(Ret (*f)(Args...)) noexcept { this->f = f; } - ~Fn2(void) noexcept + ~Fn(void) noexcept { this->f = nullptr; if (this->ctxHandler) @@ -223,7 +101,7 @@ namespace jule #endif } - inline Fn2 &operator=(std::nullptr_t) noexcept + inline Fn &operator=(std::nullptr_t) noexcept { this->f = nullptr; return *this; @@ -240,7 +118,7 @@ namespace jule } friend std::ostream &operator<<(std::ostream &stream, - const Fn2 &f) noexcept + const Fn &f) noexcept { if (f == nullptr) return (stream << ""); @@ -348,7 +226,7 @@ namespace jule } template - jule::Fn2 __new_closure(void *fn, jule::Ptr ctx, void (*ctxHandler)(jule::Ptr &)) noexcept + jule::Fn __new_closure(void *fn, jule::Ptr ctx, void (*ctxHandler)(jule::Ptr &)) noexcept { __JULE_CLOSURE_MTX_LOCK(); if (jule::__closure_cap < 1) @@ -361,7 +239,7 @@ namespace jule ptr[1] = fn; __JULE_CLOSURE_MTX_UNLOCK(); Ret (*static_closure)(Args...) = (Ret(*)(Args...))closure; - jule::Fn2 fn2(static_closure); + jule::Fn fn2(static_closure); fn2.ctx = std::move(ctx); fn2.ctxHandler = ctxHandler; ctx = nullptr; @@ -400,4 +278,6 @@ namespace jule jule::__closure_cap--; } #endif -} // namespace jule \ No newline at end of file +} // namespace jule + +#endif // ifndef __JULE_FN_HPP \ No newline at end of file diff --git a/src/julec/obj/cxx/type.jule b/src/julec/obj/cxx/type.jule index bbac740c3..6ff1c7b5e 100644 --- a/src/julec/obj/cxx/type.jule +++ b/src/julec/obj/cxx/type.jule @@ -57,7 +57,7 @@ impl typeCoder { const Slice = "jule::Slice" const Trait = "jule::Trait" const Array = "jule::Array" - const Fn = "jule::Fn2" + const Fn = "jule::Fn" const Bool = "jule::Bool" const Int = "jule::Int" const Uintptr = "jule::Uintptr"