diff --git a/include/wrenpp/def_configuration.hpp b/include/wrenpp/def_configuration.hpp index 9c29420..dd90b58 100644 --- a/include/wrenpp/def_configuration.hpp +++ b/include/wrenpp/def_configuration.hpp @@ -29,6 +29,5 @@ namespace wren { public: static void write_fn (VM*, const char* text); static void error_fn (VM*, ErrorType, const char* module, int line, const char* msg); - foreign_method_t foreign_method_fn (VM* vm, const char* module, const char* class_name, bool is_static, const char* signature); }; } //namespace wren diff --git a/include/wrenpp/vm_fun.hpp b/include/wrenpp/vm_fun.hpp index 4931585..432a77a 100644 --- a/include/wrenpp/vm_fun.hpp +++ b/include/wrenpp/vm_fun.hpp @@ -189,7 +189,7 @@ namespace wren { const constexpr char dummy_name_buff[N] = {0}; const constexpr auto params = dhandy::bt::string(dummy_name_buff) + dhandy::bt::make_string("(") + - ((static_cast(args), dhandy::bt::make_string(",_")) + ...).template substr<1>() + + ((static_cast(args), dhandy::bt::make_string(",_")) + ... + dhandy::bt::make_string("")).template substr<1>() + dhandy::bt::make_string(")"); ; char cat_buff[params.size() + 1]; diff --git a/meson.build b/meson.build index ca17ba0..a450196 100644 --- a/meson.build +++ b/meson.build @@ -63,7 +63,7 @@ wrenpp_dep = declare_dependency( include_directories: public_incl, ) -executable(meson.project_name(), +executable(meson.project_name() + 'sample', 'src/main.cpp', dependencies: wrenpp_dep, install: false, diff --git a/src/main.cpp b/src/main.cpp index 2365315..84931c4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,44 +17,79 @@ #include "wrenpp/vm_fun.hpp" #include "wrenpp/def_configuration.hpp" -#include #include #include +#include + +#if defined(__GNU_LIBRARY__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 17) || __GLIBC__ > 2) +# define HasSecureGetenv +#endif namespace { const constexpr char g_script[] = "" -R"(class Math { - construct new() {} - foreign add(a, b) +R"script(class User { + construct new(nm) { + _name = nm + } - static sum_params(a,b) { - return a + b + foreign static get_env(name) + + greet() { + System.print("Hello %(_name), from wren script!") } } -System.print("I am running in a VM!") -var myvar = Math.new() -var name = "some_test" -var number = 123 -myvar.add(5, 6) -System.print("Script done") -)"; +var the_user = User.new(User.get_env("USER")) +)script"; + +const char* raw_fetch_env (const char* name) noexcept { + if (not name) + return nullptr; + +#if defined(HasSecureGetenv) + const char* const val_ptr = secure_getenv(name); +#else + const char* const val_ptr = getenv(name); +#endif + + return val_ptr; +} + +void user_get_env (wren::VM* vm) { + set(*vm, 0, raw_fetch_env(wren::get(*vm, 1))); +} + +class MyConfig : public wren::DefConfiguration { +public: + wren::foreign_method_t foreign_method_fn ( + wren::VM* vm, + const char* module_ptr, + const char* class_name_ptr, + bool is_static, + const char* signature_ptr + ) { + //std::cout << "VM " << vm << " requested foreign method " << class_name << "::" << signature << " in module " << module << "\n"; + std::string_view module(module_ptr); + std::string_view class_name(class_name_ptr); + std::string_view signature(signature_ptr); + if ("User" == class_name and "main" == module and "get_env(_)" == signature) + return &user_get_env; + + return nullptr; + } +}; } //unnamed namespace int main() { - typedef wren::ModuleAndName MN; - using wren::variables; + //typedef wren::ModuleAndName MN; - std::cout << "hello world\n"; - wren::DefConfiguration config; + MyConfig config; wren::VM vm(&config); - vm.interpret("my_module", g_script); + vm.interpret("main", g_script); - auto vars = variables(vm, MN{"my_module", "name"}, MN{"my_module","number"}); - std::cout << "name = \"" << std::get<0>(vars) << "\", number = " << std::get<1>(vars) << '\n'; - const int sum = wren::call(vm, {"my_module", "Math"}, "sum_params", MN{"my_module", "number"}, 90); - std::cout << "wren method returned " << sum << " (expected " << std::get<1>(vars) + 90 << ")\n"; + wren::call(vm, {"main", "the_user"}, "greet"); + std::cout << "Quitting in 1 sec" << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(1000)); return 0; } diff --git a/src/wren/def_configuration.cpp b/src/wren/def_configuration.cpp index 67cc1db..1f060d4 100644 --- a/src/wren/def_configuration.cpp +++ b/src/wren/def_configuration.cpp @@ -19,10 +19,6 @@ #include namespace wren { - void test (VM* vm) { - std::cout << "VM " << vm << " invoked foreign method!\n"; - } - void DefConfiguration::write_fn (VM*, const char* text) { std::cout << text; } @@ -30,9 +26,4 @@ namespace wren { void DefConfiguration::error_fn (VM*, ErrorType type, const char* module, int line, const char* message) { std::cerr << "Wren error: " << message << " in " << module << ':' << line << '\n'; } - - foreign_method_t DefConfiguration::foreign_method_fn (VM* vm, const char* module, const char* class_name, bool is_static, const char* signature) { - std::cout << "VM " << vm << " requested foreign method " << class_name << "::" << signature << " in module " << module << "\n"; - return &test; - } } //namespace wren diff --git a/src/wren/vm_fun.cpp b/src/wren/vm_fun.cpp index 0777168..f5f77cd 100644 --- a/src/wren/vm_fun.cpp +++ b/src/wren/vm_fun.cpp @@ -34,8 +34,8 @@ namespace wren { return {ptr.first, static_cast(ptr.second)}; } - template<> std::string get (VM& vm, int slot_num) { - return {vm.slot_string(slot_num)}; + template<> const char* get (VM& vm, int slot_num) { + return vm.slot_string(slot_num); } template <> double get (VM& vm, int slot_num) { @@ -54,6 +54,10 @@ namespace wren { return static_cast(vm.slot_double(slot_num)); } + template<> std::string get (VM& vm, int slot_num) { + return {vm.slot_string(slot_num)}; + } + template <> std::string_view get (VM& vm, int slot_num) { auto arr = get>(vm, slot_num); return std::string_view{arr.first, static_cast(arr.second)};