wrenpp/src/main.cpp

44 lines
1.1 KiB
C++
Raw Normal View History

#include "wrenpp/vm_fun.hpp"
#include "wrenpp/def_configuration.hpp"
#include <iostream>
#include <chrono>
#include <thread>
namespace {
const constexpr char g_script[] = ""
R"(class Math {
construct new() {}
foreign add(a, b)
static sum_params(a,b) {
return a + b
}
}
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")
)";
} //unnamed namespace
int main() {
typedef wren::ModuleAndName MN;
using wren::variables;
std::cout << "hello world\n";
2020-04-25 18:45:59 +00:00
wren::DefConfiguration config;
wren::VM vm(&config);
vm.interpret("my_module", g_script);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
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";
return 0;
}