Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 9 of 21 clojure.string vars #201

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions compiler+runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ add_library(

# Native module sources.
src/cpp/clojure/core_native.cpp
src/cpp/clojure/string_native.cpp
src/cpp/jank/compiler_native.cpp
src/cpp/jank/perf_native.cpp
)
Expand Down
5 changes: 5 additions & 0 deletions compiler+runtime/include/cpp/clojure/string_native.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <jank/c_api.h>

jank_object_ptr jank_load_clojure_string_native();
91 changes: 91 additions & 0 deletions compiler+runtime/src/cpp/clojure/string_native.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <boost/algorithm/string.hpp>

#include <jank/native_persistent_string/fmt.hpp>
#include <clojure/core_native.hpp>
#include <jank/runtime/convert.hpp>
#include <jank/runtime/core.hpp>
#include <jank/runtime/context.hpp>
#include <jank/runtime/visit.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're not using any visting fns, while this is an incredibly expensive include. Please audit all of the includes here to ensure they're needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still working on this.


namespace clojure::string_native
{
using namespace jank;
using namespace jank::runtime;

static object_ptr blank(object_ptr const s)
{
if(runtime::is_nil(s))
{
return obj::boolean::true_const();
}
auto const s_str(runtime::to_string(s));
return make_box(s_str.is_blank());
}

static object_ptr reverse(object_ptr const s)
{
auto const s_str(runtime::to_string(s));
return make_box<obj::persistent_string>(
native_persistent_string{ s_str.rbegin(), s_str.rend() });
}

static object_ptr lower_case(object_ptr const s)
{
auto const s_str(runtime::to_string(s));
return make_box(boost::to_lower_copy(s_str));
}

static object_ptr starts_with(object_ptr const s, object_ptr const substr)
{
auto const s_str(runtime::to_string(s));
auto const substr_str(runtime::to_string(substr));
return make_box(s_str.starts_with(substr_str));
}

static object_ptr ends_with(object_ptr const s, object_ptr const substr)
{
auto const s_str(runtime::to_string(s));
auto const substr_str(runtime::to_string(substr));
return make_box(s_str.ends_with(substr_str));
}

static object_ptr includes(object_ptr const s, object_ptr const substr)
{
auto const s_str(runtime::to_string(s));
auto const substr_str(runtime::to_string(substr));
return make_box(s_str.contains(substr_str));
}

static object_ptr upper_case(object_ptr const s)
{
auto const s_str(runtime::to_string(s));
return make_box(boost::to_upper_copy(s_str));
}
}

jank_object_ptr jank_load_clojure_string_native()
{
using namespace jank;
using namespace jank::runtime;
using namespace clojure;

auto const ns(__rt_ctx->intern_ns("clojure.string-native"));

auto const intern_fn([=](native_persistent_string const &name, auto const fn) {
ns->intern_var(name)->bind_root(
make_box<obj::native_function_wrapper>(convert_function(fn))
->with_meta(obj::persistent_hash_map::create_unique(std::make_pair(
__rt_ctx->intern_keyword("name").expect_ok(),
make_box(obj::symbol{ __rt_ctx->current_ns()->to_string(), name }.to_string())))));
});

intern_fn("blank?", &string_native::blank);
intern_fn("ends-with?", &string_native::ends_with);
intern_fn("includes?", &string_native::includes);
intern_fn("lower-case", &string_native::lower_case);
intern_fn("reverse", &string_native::reverse);
intern_fn("starts-with?", &string_native::starts_with);
intern_fn("upper-case", &string_native::upper_case);

return erase(obj::nil::nil_const());
}
2 changes: 2 additions & 0 deletions compiler+runtime/src/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <jank/compiler_native.hpp>
#include <jank/perf_native.hpp>
#include <clojure/core_native.hpp>
#include <clojure/string_native.hpp>

namespace jank
{
Expand Down Expand Up @@ -295,6 +296,7 @@ try
__rt_ctx = new(GC) runtime::context{ opts };

jank_load_clojure_core_native();
jank_load_clojure_string_native();
frenchy64 marked this conversation as resolved.
Show resolved Hide resolved
jank_load_jank_compiler_native();
jank_load_jank_perf_native();

Expand Down
Loading