2020-04-30 21:33:54 +00:00
|
|
|
/* Copyright 2020, Michele Santullo
|
|
|
|
* This file is part of wrenpp.
|
|
|
|
*
|
|
|
|
* Wrenpp is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Wrenpp is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with wrenpp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-04-30 20:50:46 +00:00
|
|
|
#include "wrenpp/vm_fun.hpp"
|
|
|
|
#include "wrenpp/def_configuration.hpp"
|
2020-04-24 23:49:46 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
2020-04-30 23:39:11 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#if defined(__GNU_LIBRARY__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 17) || __GLIBC__ > 2)
|
|
|
|
# define HasSecureGetenv
|
|
|
|
#endif
|
2020-04-24 23:49:46 +00:00
|
|
|
|
|
|
|
namespace {
|
2020-04-26 15:19:02 +00:00
|
|
|
const constexpr char g_script[] = ""
|
2020-04-30 23:39:11 +00:00
|
|
|
R"script(class User {
|
|
|
|
construct new(nm) {
|
|
|
|
_name = nm
|
|
|
|
}
|
|
|
|
|
|
|
|
foreign static get_env(name)
|
2020-04-30 17:58:09 +00:00
|
|
|
|
2020-04-30 23:39:11 +00:00
|
|
|
greet() {
|
|
|
|
System.print("Hello %(_name), from wren script!")
|
2020-04-30 17:58:09 +00:00
|
|
|
}
|
2020-04-26 15:19:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 23:39:11 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-30 23:53:53 +00:00
|
|
|
void user_get_env (wren::VM& vm) {
|
|
|
|
set(vm, 0, raw_fetch_env(wren::get<const char*>(vm, 1)));
|
2020-04-30 23:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
2020-04-24 23:49:46 +00:00
|
|
|
} //unnamed namespace
|
|
|
|
|
|
|
|
int main() {
|
2020-04-30 23:39:11 +00:00
|
|
|
//typedef wren::ModuleAndName MN;
|
2020-04-30 17:58:09 +00:00
|
|
|
|
2020-04-30 23:39:11 +00:00
|
|
|
MyConfig config;
|
2020-05-01 15:44:09 +00:00
|
|
|
wren::VM vm(&config, nullptr);
|
2020-04-30 23:39:11 +00:00
|
|
|
vm.interpret("main", g_script);
|
2020-04-24 23:49:46 +00:00
|
|
|
|
2020-04-30 23:39:11 +00:00
|
|
|
wren::call<void>(vm, {"main", "the_user"}, "greet");
|
2020-04-30 17:58:09 +00:00
|
|
|
|
2020-04-30 23:39:11 +00:00
|
|
|
std::cout << "Quitting in 1 sec" << std::endl;
|
2020-04-30 22:14:55 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
2020-04-24 23:49:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|