Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGrulich committed Mar 8, 2024
1 parent b77959d commit 2e9c8f4
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 28 deletions.
80 changes: 53 additions & 27 deletions nautilus-api/include/Interface/function.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <functional>
#include "DataTypes/Val.hpp"

#pragma once

namespace nautilus {



template<is_base_type R, is_base_type... FunctionArguments>
class CallableNautilusFunction {
public:
Expand Down Expand Up @@ -46,20 +46,26 @@ namespace nautilus {
template<typename R, typename... FunctionArguments>
class CallableRuntimeFunction {
public:
CallableRuntimeFunction(R (*fnptr)(FunctionArguments...)) : fnptr(fnptr) {

};
explicit CallableRuntimeFunction(R (*fnptr)(FunctionArguments...)) : fnptr(fnptr) {};

template<typename Arg>
auto transform(Arg argument) {
return details::getRawValue(argument);
}

template<typename... FunctionArgumentsRaw>
requires (!std::is_void_v<R>)
auto operator()(FunctionArgumentsRaw... args) {
// function is called from an external context.
auto result = fnptr(transform((args))...);
return make_value(result);
return make_value(result);
}

template<typename... FunctionArgumentsRaw>
requires std::is_void_v<R>
void operator()(FunctionArgumentsRaw... args) {
// function is called from an external context.
fnptr(transform((args))...);
}

template<is_integral... FunctionArgumentsRaw>
Expand All @@ -71,13 +77,54 @@ namespace nautilus {
R (*fnptr)(FunctionArguments...);
};

template<typename TP, typename _Class>
class MemberFunction {
public:
MemberFunction(TP _Class::* __pm) : __pm(__pm) {}

template<typename Arg>
auto transform(Arg argument) {
return details::getRawValue(argument);
}

template<typename ... FunctionArgumentsRaw>
val<int32_t> operator()(_Class *mem, FunctionArgumentsRaw... args) {
// we are in the nautilus context
// keep current tracing context and continue tracing
// TODO implement polymorphic inline cache
// TODO pass member as val
auto result = std::invoke(__pm, mem, transform((args))...);
return make_value(result);
}

TP _Class::* __pm;
};


template<is_integral R, is_integral... FunctionArguments, typename... ValueArguments>
template<is_fundamental R, is_fundamental... FunctionArguments, typename... ValueArguments>
auto
invoke(R (*fnptr)(FunctionArguments...), ValueArguments... args) {
return CallableRuntimeFunction<R, FunctionArguments...>(fnptr)(args...);
}

template<is_fundamental... FunctionArguments, typename... ValueArguments>
void invoke(void (*fnptr)(FunctionArguments...), ValueArguments... args) {
auto func = CallableRuntimeFunction<void, FunctionArguments...>(fnptr);
func(args...);
}

template<typename TP, typename _Class, typename... ValueArguments>
auto
invoke(TP _Class::* __pm, _Class *mem, ValueArguments... args) {
auto member = MemberFunction(__pm);
return member(mem, args...);
}

template<typename TP, typename _Class>
auto Function(TP _Class::* __pm) noexcept {
return MemberFunction(__pm);
}


template<class>
constexpr bool is_reference_wrapper_v = false;
Expand All @@ -95,27 +142,6 @@ namespace nautilus {
return CallableNautilusFunction<R, FunctionArguments...>(fnptr);
}

template<typename TP, typename _Class>
class MemberFunction {
public:
MemberFunction(TP _Class::* __pm) : __pm(__pm) {}

template<typename ... FunctionArgumentsRaw>
auto operator()(_Class *mem, FunctionArgumentsRaw... args) {
// we are in the nautilus context
// keep current tracing context and continue tracing
// TODO implement polymorphic inline cache
return std::invoke(__pm, mem, (args)...);
// return __pm(mem, (args)...);
}


TP _Class::* __pm;
};

template<typename TP, typename _Class>
auto Function(TP _Class::* __pm) noexcept {
return MemberFunction(__pm);
}

}
18 changes: 17 additions & 1 deletion nautilus-api/test/ExecutionTests/Executions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,27 @@ namespace nautilus::engine {
}

void functionCallExecutionTest(engine::NautilusEngine &engine) {
SECTION("sumLoop") {
SECTION("simpleDirectCall") {
auto f = engine.registerFunction(simpleDirectCall);
REQUIRE(f(10, 10) == 20);
REQUIRE(f(0, 1) == 1);
} SECTION("loopDirectCall") {
auto f = engine.registerFunction(loopDirectCall);
REQUIRE(f(10, 10) == 100);
REQUIRE(f(1, 1) == 1);
REQUIRE(f(0, 1) == 0);
}SECTION("voidCall") {
auto f = engine.registerFunction(voidFuncCall);
REQUIRE(f(10, 10) == 10);
REQUIRE(f(0, 1) == 0);
}SECTION("memberFuncCall") {
auto f = engine.registerFunction(memberFuncCall);
REQUIRE(f(10) == 52);
}SECTION("constMemberFuncCall") {
auto f = engine.registerFunction(constMemberFuncCall);
REQUIRE(f(10) == 52);
}

}

TEMPLATE_TEST_CASE("Engine Test", "[value][template]", int8_t) {
Expand Down
41 changes: 41 additions & 0 deletions nautilus-api/test/ExecutionTests/RunctimeCallFunctions.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#pragma once

#include <engine/engine.hpp>

namespace nautilus::engine {

void voidFunc(int32_t x, int32_t y) {
[[maybe_unused]]auto z = x + y;
}

int32_t add(int32_t x, int32_t y) {
return x + y;
}
Expand All @@ -10,5 +16,40 @@ namespace nautilus::engine {
return invoke<>(add, x, y);
}

val<int32_t> loopDirectCall(val<int32_t> c, val<int32_t> x) {
val<int32_t> sum = 0;
for (val<int32_t> i = 0; i < c; i++) {
sum = invoke<>(add, sum, x);
}
return sum;
}

val<int32_t> voidFuncCall(val<int32_t> x, val<int32_t> y) {
invoke<>(voidFunc, x, y);
return x;
}

class Clazz {
public:
int32_t add(int32_t x) {
return x + i;
}

int32_t addConst(int32_t x) const {
return x + i;
}

int i = 42;
};

static auto clazz = Clazz();

val<int32_t> memberFuncCall(val<int32_t> x) {
return invoke<>(&Clazz::add, &clazz, x);
}

val<int32_t> constMemberFuncCall(val<int32_t> x) {
return invoke<>(&Clazz::addConst, &clazz, x);
}

}

0 comments on commit 2e9c8f4

Please sign in to comment.