2022-04-28 20:56:11 +00:00
|
|
|
/* Copyright 2020-2022, Michele Santullo
|
2020-04-30 21:33:54 +00:00
|
|
|
* 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"
|
2022-04-29 16:16:57 +00:00
|
|
|
#include "wrenpp/callback_manager.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;
|
|
|
|
}
|
|
|
|
|
2022-05-16 23:00:00 +00:00
|
|
|
void user_get_env (wren::VM& vm, wren::ModuleAndName) {
|
2020-04-30 23:53:53 +00:00
|
|
|
set(vm, 0, raw_fetch_env(wren::get<const char*>(vm, 1)));
|
2020-04-30 23:39:11 +00:00
|
|
|
}
|
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
|
|
|
|
2022-04-29 16:16:57 +00:00
|
|
|
wren::DefConfiguration config;
|
2020-05-01 15:44:09 +00:00
|
|
|
wren::VM vm(&config, nullptr);
|
2022-04-29 16:16:57 +00:00
|
|
|
vm.callback_manager().add_callback(true, "main", "User", "get_env(_)", &user_get_env);
|
|
|
|
|
2020-04-30 23:39:11 +00:00
|
|
|
vm.interpret("main", g_script);
|
2020-04-24 23:49:46 +00:00
|
|
|
|
2022-05-16 23:00:00 +00:00
|
|
|
wren::call<void>(vm, wren::MN<"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;
|
|
|
|
}
|