Skip to content

Commit

Permalink
Merge pull request #14 from Clemapfel/cmake_rework
Browse files Browse the repository at this point in the history
merged, c.f. #15 #16
  • Loading branch information
Clemapfel authored Mar 18, 2022
2 parents 2672041 + 65d4081 commit d7292dd
Show file tree
Hide file tree
Showing 40 changed files with 1,975 additions and 1,536 deletions.
15 changes: 7 additions & 8 deletions .src/array_iterator.inl
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,15 @@ namespace jluna
}

template<Boxable V, size_t R>
template<Unboxable T>
T Array<V, R>::ConstIterator::operator*() const
auto Array<V, R>::ConstIterator::operator*()
{
static jl_function_t* getindex = jl_get_function(jl_base_module, "getindex");
return unbox<T>(jluna::safe_call(getindex, _owner->operator const jl_value_t *(), box(_index + 1)));
return Iterator(_index, const_cast<Array<V, R>*>(_owner));
}

template<Boxable V, size_t R>
auto Array<V, R>::ConstIterator::operator*()
auto Array<V, R>::ConstIterator::operator*() const
{
return Iterator(_index, const_cast<Array<V, R>*>(_owner));
return *this;
}

template<Boxable V, size_t R>
Expand All @@ -88,10 +86,11 @@ namespace jluna
}

template<Boxable V, size_t R>
template<Unboxable T, std::enable_if_t<not std::is_same_v<T, Proxy>, bool>>
template<Unboxable T, std::enable_if_t<not Is<Proxy, T>, bool>>
Array<V, R>::ConstIterator::operator T() const
{
return operator*<T>();
static jl_function_t* getindex = jl_get_function(jl_base_module, "getindex");
return unbox<T>(jluna::safe_call(getindex, _owner->operator const jl_value_t *(), box(_index + 1)));
}

template<Boxable V, size_t R>
Expand Down
2 changes: 1 addition & 1 deletion .src/c_adapter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifdef __cplusplus

#include <julia.h>
#include <include/julia_wrapper.hpp>

#include <iostream>

Expand Down
2 changes: 1 addition & 1 deletion .src/c_adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#ifdef __cplusplus

#include <map>
#include <julia.h>
#include <include/julia_wrapper.hpp>
#include <functional>
#include <string>

Expand Down
36 changes: 32 additions & 4 deletions .src/include_julia.inl.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,40 @@

#pragma once

#cmakedefine RESOURCE_PATH "@RESOURCE_PATH@"
#include <string>

namespace jluna::detail
{
///@brief allow julia to load jluna by using C++ #import statement
static inline const char* include = R"(
include("@RESOURCE_PATH@/include/jluna.jl")
static inline const char* c_adapter_path = "@C_ADAPTER_NAME@";
static inline std::string c_adapter_path_override = "";

// source split like this because of: https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/compiler-error-c2026?view=msvc-160

static inline const char* module_start = "module jluna";

static inline const char* include_01 = R"(
@01_COMMON@
)";

static inline const char* include_02 = R"(
@02_COMMON@
)";

static inline const char* include_03 = R"(
@03_EXCEPTION_HANDLER@
)";

static inline const char* include_04 = R"(
@04_MEMORY_HANDLER@
)";

static inline const char* include_05 = R"(
@05_CPPCALL@
)";

static inline const char* module_end = "end";

static inline const char* include_06 = R"(
@06_PUBLIC@
)";
}
39 changes: 31 additions & 8 deletions .src/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Created on 31.01.22 by clem ([email protected])
//

#include <julia.h>
#include <include/julia_wrapper.hpp>

#include <sstream>
#include <iostream>
Expand All @@ -13,10 +13,9 @@
#include <include/exceptions.hpp>
#include <include/julia_extension.hpp>
#include <include/proxy.hpp>
#include <.src/include_julia.inl>
#include <include/module.hpp>
#include <include/type.hpp>

#include <.src/include_julia.inl>

namespace jluna::detail
{
Expand All @@ -30,6 +29,11 @@ namespace jluna::detail

namespace jluna::State
{
void set_c_adapter_path(const std::string& path)
{
jluna::detail::c_adapter_path_override = path;
}

void initialize()
{
initialize("");
Expand All @@ -42,9 +46,23 @@ namespace jluna::State
else
jl_init_with_image(path.c_str(), NULL);

jl_eval_string(jluna::detail::include);
forward_last_exception();
{ // execute jluna julia code in pieces
using namespace jluna::detail;
std::stringstream str;
str << module_start;
str << include_01;
str << include_02;
str << include_03;
str << include_04;
str << include_05;
str << module_end;

str << include_06;

jl_eval_string(str.str().c_str());
}

forward_last_exception();

jl_eval_string(R"(
begin
Expand All @@ -57,7 +75,12 @@ namespace jluna::State
)");
forward_last_exception();

jl_eval_string(("jluna._cppcall.eval(:(_library_name = \"" + std::string(RESOURCE_PATH) + "/libjluna_c_adapter.so\"))").c_str());
std::stringstream str;
str << "jluna._cppcall.eval(:(_library_name = \"";
str << (jluna::detail::c_adapter_path_override.empty() ? jluna::detail::c_adapter_path : jluna::detail::c_adapter_path_override);
str << "\"))";

jl_eval_string(str.str().c_str());
forward_last_exception();

jl_eval_string(R"(
Expand Down Expand Up @@ -214,7 +237,7 @@ namespace jluna::State::detail
Any * get_reference(size_t key)
{
static Function* get_reference = jl_find_function("jluna.memory_handler", "get_reference");
return jluna::safe_call(get_reference, jl_box_uint64(reinterpret_cast<size_t>(key)));
return jluna::safe_call(get_reference, jl_box_uint64(static_cast<size_t>(key)));
}

void free_reference(size_t key)
Expand All @@ -226,7 +249,7 @@ namespace jluna::State::detail
static Function* free_reference = jl_find_function("jluna.memory_handler", "free_reference");

jl_gc_pause;
jluna::safe_call(free_reference, jl_box_uint64(reinterpret_cast<size_t>(key)));
jluna::safe_call(free_reference, jl_box_uint64(static_cast<size_t>(key)));
jl_gc_unpause;
}

Expand Down
2 changes: 1 addition & 1 deletion .test/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <iostream>
#include <julia.h>
#include <include/julia_wrapper.hpp>
#include <ptrhash.h>
#include <jluna.hpp>
#include <.test/test.hpp>
Expand Down
Loading

0 comments on commit d7292dd

Please sign in to comment.