Skip to content

Commit

Permalink
[lib/kaba] update 0.20.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
momentarylapse committed Sep 28, 2024
1 parent 046c696 commit d136b8b
Show file tree
Hide file tree
Showing 25 changed files with 258 additions and 137 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ add_executable(tsunami
src/lib/kaba/lib/dict.cpp
src/lib/kaba/lib/extern.cpp
src/lib/kaba/lib/lib.cpp
src/lib/kaba/lib/lib_async.cpp
src/lib/kaba/lib/lib_base.cpp
src/lib/kaba/lib/lib_doc.cpp
src/lib/kaba/lib/lib_gl.cpp
Expand Down
1 change: 1 addition & 0 deletions plugins/tsunami/other.kaba
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use common.*
use modules.*
use streams.*
use song.*
use async.*
use os
use kaba

Expand Down
1 change: 1 addition & 0 deletions plugins/tsunami/tsunami.kaba
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use processing.*

let EXPORT_IMPORTS = true

use async.*
use hui
use kaba
use os
Expand Down
40 changes: 20 additions & 20 deletions src/lib/base/future.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,34 +80,34 @@ struct xcallback<void> {
};

enum class PromiseState {
UNFINISHED,
SUCCEEDED,
FAILED
Unfinished,
Succeeded,
Failed
};

// internal/shared data structure
template<class T>
struct _promise_core_ : public Sharable<base::Empty> {
typename xcallback<T>::t cb_success;
Callback cb_fail;
PromiseState state = PromiseState::UNFINISHED;
PromiseState state = PromiseState::Unfinished;
mutable T result;

_promise_core_() {};
_promise_core_() = default;

void success(typename xparam<T>::t t) {
state = PromiseState::SUCCEEDED;
state = PromiseState::Succeeded;
result = t;
if (cb_success)
cb_success(t);
}
void fail() {
state = PromiseState::FAILED;
state = PromiseState::Failed;
if (cb_fail)
cb_fail();
}
void reset() {
state = PromiseState::UNFINISHED;
state = PromiseState::Unfinished;
cb_success = nullptr;
cb_fail = nullptr;
}
Expand Down Expand Up @@ -146,15 +146,15 @@ struct future {

shared<typename P::CoreType> core;

future(shared<typename P::CoreType> c) : core(c) {
explicit future(shared<typename P::CoreType> c) : core(c) {
}
future(const future<T>& f) : core(f.core) {
}

future<T>& then(typename xcallback<T>::t cb) {
core->cb_success = cb;
if (core->state == PromiseState::SUCCEEDED) {
if constexpr (std::is_same<T, void>::value)
if (core->state == PromiseState::Succeeded) {
if constexpr (std::is_same_v<T, void>)
cb();
else
cb(core->result);
Expand All @@ -164,7 +164,7 @@ struct future {

future<T>& on_fail(Callback cb) {
core->cb_fail = cb;
if (core->state == PromiseState::FAILED)
if (core->state == PromiseState::Failed)
cb();
return *this;
}
Expand All @@ -175,20 +175,20 @@ template<>
struct _promise_core_<void> : public Sharable<base::Empty> {
Callback cb_success;
Callback cb_fail;
PromiseState state = PromiseState::UNFINISHED;
PromiseState state = PromiseState::Unfinished;

void success() {
state = PromiseState::SUCCEEDED;
state = PromiseState::Succeeded;
if (cb_success)
cb_success();
}
void fail() {
state = PromiseState::FAILED;
state = PromiseState::Failed;
if (cb_fail)
cb_fail();
}
void reset() {
state = PromiseState::UNFINISHED;
state = PromiseState::Unfinished;
cb_success = nullptr;
cb_fail = nullptr;
}
Expand Down Expand Up @@ -219,7 +219,7 @@ struct promise<void> {
};

template<class T>
inline future<T> success(typename xparam<T>::t t) {
future<T> success(typename xparam<T>::t t) {
base::promise<T> promise;
promise(t);
return promise.get_future();
Expand All @@ -232,15 +232,15 @@ inline future<void> success() {
}

template<class T>
inline future<T> failed() {
future<T> failed() {
base::promise<T> promise;
promise.fail();
return promise.get_future();
}

template<class T>
inline void await(const future<T>& f) {
while (f.core->state == PromiseState::UNFINISHED) {}
void await(const future<T>& f) {
while (f.core->state == PromiseState::Unfinished) {}
}

}
Expand Down
21 changes: 21 additions & 0 deletions src/lib/kaba/dynamic/call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,27 @@ bool call_function_pointer(void *ff, void *ret, const Array<void*> &param, const
call2<int,CBR,CBR>(ff, ret, param);
return true;
}
} else if (return_type == TypeBool) {
if ((ptype[0] == TypeInt32) and (ptype[1] == TypeInt32)) {
call2<bool,int,int>(ff, ret, param);
return true;
}
if (ptype[0]->is_some_pointer() and ptype[1]->is_some_pointer()) {
call2<bool,void*,void*>(ff, ret, param);
return true;
}
/*if ((ptype[0]->uses_call_by_reference()) and (ptype[1] == TypeInt32)) {
call2<bool,CBR,int>(ff, ret, param);
return true;
}
if ((ptype[0]->uses_call_by_reference()) and (ptype[1] == TypeFloat32)) {
call2<bool,CBR,float>(ff, ret, param);
return true;
}
if ((ptype[0]->uses_call_by_reference()) and (ptype[1]->uses_call_by_reference())) {
call2<bool,CBR,CBR>(ff, ret, param);
return true;
}*/
} else if (return_type == TypeFloat32) {
if ((ptype[0] == TypeFloat32) and (ptype[1] == TypeFloat32)) {
call2<float,float,float>(ff, ret, param);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/kaba/kaba.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace kaba {

string Version = "0.20.6.3";
string Version = "0.20.7.0";

//#define ScriptDebug

Expand Down
85 changes: 61 additions & 24 deletions src/lib/kaba/lib/future.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,80 @@ namespace kaba {

extern Module *cur_package;
extern const Class *TypeFutureT;
extern const Class *TypePromiseT;
extern const Class* TypeCallback;

template<class T>
class ParamCallback : public Callable<void(typename base::xparam<T>::t)> {};
template<>
class ParamCallback<void> : public Callable<void()> {};

template<class T>
struct KabaFuture : public base::future<T> {
using CoreType = typename base::future<T>::P::CoreType;
KabaFuture() : base::future<T>(new CoreType) {
}
void __init__() {
// new(this) KabaFuture<T>();
new(this) KabaFuture();
}
void __delete__() {
this->~KabaFuture<T>();
}
void kaba_then(Callable<void(typename base::xparam<T>::t)> &c) {
this->then([&c] (typename base::xparam<T>::t p) { c(p); });
void assign(KabaFuture& other) {
*this = other;
}
void kaba_then_or_fail(Callable<void(typename base::xparam<T>::t)> &c, Callable<void()> &c_fail) {
this->then([&c] (typename base::xparam<T>::t p) { c(p); }).on_fail([&c_fail] { c_fail(); });
void kaba_then(ParamCallback<T> &c) {
if constexpr (std::is_same_v<T, void>)
this->then([&c] { c(); });
else
this->then([&c] (typename base::xparam<T>::t p) { c(p); });
}
void kaba_then_or_fail(ParamCallback<T> &c, Callable<void()> &c_fail) {
if constexpr (std::is_same_v<T, void>)
this->then([&c] { c(); }).on_fail([&c_fail] { c_fail(); });
else
this->then([&c] (typename base::xparam<T>::t p) { c(p); }).on_fail([&c_fail] { c_fail(); });
}
};

struct KabaVoidFuture : public base::future<void> {
template<class T>
struct KabaPromise : base::promise<T> {
void __init__() {
// new(this) KabaVoidFuture();
new(this) KabaPromise<T>();
}
void __delete__() {
this->~KabaVoidFuture();
this->~KabaPromise<T>();
}
void kaba_then(Callable<void()> &c) {
this->then([&c] { c(); });
void assign(KabaPromise& other) {
*this = other;
}
void kaba_then_or_fail(Callable<void()> &c, Callable<void()> &c_fail) {
this->then([&c] { c(); }).on_fail([&c_fail] { c_fail(); });
};

template<class T>
struct KabaPromiseX : KabaPromise<T> {
void call(typename base::xparam<T>::t value) {
(*this)(value);
}
};

template<>
struct KabaPromiseX<void> : KabaPromise<void> {
void call() {
(*this)();
}
};


template<class T>
inline void lib_create_future(const Class *tt, const Class *pp, const Class *t_cb) {
void lib_create_future(const Class *tt, const Class *pp, const Class *t_cb) {
auto t = const_cast<Class*>(tt);
t->param = {pp};

add_class(t);
//class_add_func(Identifier::func::INIT, TypeVoid, &KabaFuture<T>::__init__, Flags::MUTABLE);
class_add_func(Identifier::func::Init, TypeVoid, &KabaFuture<T>::__init__, Flags::Mutable);
class_add_func(Identifier::func::Delete, TypeVoid, &KabaFuture<T>::__delete__, Flags::Mutable);
class_add_func(Identifier::func::Assign, TypeVoid, &KabaFuture<T>::assign, Flags::Mutable);
func_add_param("other", tt);
class_add_func("then", TypeVoid, &KabaFuture<T>::kaba_then);
func_add_param("cb", t_cb);
class_add_func("then_or_fail", TypeVoid, &KabaFuture<T>::kaba_then_or_fail);
Expand All @@ -70,22 +102,27 @@ inline void lib_create_future(const Class *tt, const Class *pp, const Class *t_c
cur_package->tree.get(), tt, TypeFutureT, {pp}, 0);
}

template<>
inline void lib_create_future<void>(const Class *tt, const Class *pp, const Class *t_cb) {
template<class T>
void lib_create_promise(const Class *tt, const Class *pp, const Class *tfut) {
auto t = const_cast<Class*>(tt);
t->param = {pp};

add_class(t);
//class_add_func(Identifier::func::INIT, TypeVoid, &KabaVoidFuture::__init__, Flags::MUTABLE);
class_add_func(Identifier::func::Delete, TypeVoid, &KabaVoidFuture::__delete__, Flags::Mutable);
class_add_func("then", TypeVoid, &KabaVoidFuture::kaba_then);
func_add_param("cb", t_cb);
class_add_func("then_or_fail", TypeVoid, &KabaVoidFuture::kaba_then_or_fail);
func_add_param("cb", t_cb);
func_add_param("cb_fail", TypeCallback);
class_add_func(Identifier::func::Init, TypeVoid, &KabaPromise<T>::__init__, Flags::Mutable);
class_add_func(Identifier::func::Delete, TypeVoid, &KabaPromise<T>::__delete__, Flags::Mutable);
class_add_func(Identifier::func::Assign, TypeVoid, &KabaPromise<T>::assign, Flags::Mutable);
func_add_param("other", tt);
class_add_func("get_future", tfut, &KabaPromise<T>::get_future);
if (pp == TypeVoid) {
class_add_func(Identifier::func::Call, TypeVoid, &KabaPromiseX<T>::call);
} else {
class_add_func(Identifier::func::Call, TypeVoid, &KabaPromiseX<T>::call);
func_add_param("value", pp);
}
class_add_func("fail", TypeVoid, &KabaPromiseX<T>::fail);

cur_package->context->template_manager->add_explicit_class_instance(
cur_package->tree.get(), tt, TypeFutureT, {pp}, 0);
cur_package->tree.get(), tt, TypePromiseT, {pp}, 0);
}

}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/kaba/lib/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ void SIAddStatements();
void SIAddXCommands(Context *c);
void SIAddPackageBase(Context *c);
void SIAddPackageKaba(Context *c);
void SIAddPackageAsync(Context *c);
void SIAddPackageTime(Context *c);
void SIAddPackageOS(Context *c);
void SIAddPackageOSPath(Context *c);
Expand All @@ -732,6 +733,7 @@ void init_lib(Context *c) {


SIAddPackageBase(c);
SIAddPackageAsync(c);
SIAddPackageOSPath(c);
SIAddPackageKaba(c);
SIAddPackageMath(c);
Expand Down
31 changes: 31 additions & 0 deletions src/lib/kaba/lib/lib_async.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "../kaba.h"
#include "future.h"

namespace kaba {

const Class *TypeFutureT;
const Class *TypePromiseT;
const Class *TypeFutureCoreT;

const Class* TypeVoidFuture;
const Class* TypeVoidPromise;
const Class* TypeStringFuture;
const Class* TypeStringPromise;

void SIAddPackageAsync(Context *c) {
add_package(c, "async");

TypeFutureCoreT = add_class_template("@FutureCore", {"T"}, new TemplateClassInstantiatorFutureCore);
TypeFutureT = add_class_template("future", {"T"}, new TemplateClassInstantiatorFuture);
TypePromiseT = add_class_template("promise", {"T"}, new TemplateClassInstantiatorPromise);


// some pre-defined futures (more in hui)
TypeVoidFuture = add_type("future[void]", sizeof(base::future<void>));
TypeVoidPromise = add_type("promise[void]", sizeof(base::promise<void>));
TypeStringFuture = add_type("future[string]", sizeof(base::future<string>));
TypeStringPromise = add_type("promise[string]", sizeof(base::promise<string>));

}

}
Loading

0 comments on commit d136b8b

Please sign in to comment.