Update sample code in main(), fix the rest as required
This commit is contained in:
parent
8ba01de416
commit
274a2fea11
6 changed files with 65 additions and 36 deletions
|
@ -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
|
||||
|
|
|
@ -189,7 +189,7 @@ namespace wren {
|
|||
const constexpr char dummy_name_buff[N] = {0};
|
||||
const constexpr auto params = dhandy::bt::string<N, char>(dummy_name_buff) +
|
||||
dhandy::bt::make_string("(") +
|
||||
((static_cast<void>(args), dhandy::bt::make_string(",_")) + ...).template substr<1>() +
|
||||
((static_cast<void>(args), dhandy::bt::make_string(",_")) + ... + dhandy::bt::make_string("")).template substr<1>() +
|
||||
dhandy::bt::make_string(")");
|
||||
;
|
||||
char cat_buff[params.size() + 1];
|
||||
|
|
|
@ -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,
|
||||
|
|
79
src/main.cpp
79
src/main.cpp
|
@ -17,44 +17,79 @@
|
|||
|
||||
#include "wrenpp/vm_fun.hpp"
|
||||
#include "wrenpp/def_configuration.hpp"
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
|
||||
#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<const char*>(*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<std::string, int>(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<int>(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<void>(vm, {"main", "the_user"}, "greet");
|
||||
|
||||
std::cout << "Quitting in 1 sec" << std::endl;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -19,10 +19,6 @@
|
|||
#include <iostream>
|
||||
|
||||
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
|
||||
|
|
|
@ -34,8 +34,8 @@ namespace wren {
|
|||
return {ptr.first, static_cast<std::size_t>(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<int>(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<std::pair<const char*, int>>(vm, slot_num);
|
||||
return std::string_view{arr.first, static_cast<std::size_t>(arr.second)};
|
||||
|
|
Loading…
Reference in a new issue