60 lines
1.8 KiB
C++
60 lines
1.8 KiB
C++
/* 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/>.
|
|
*/
|
|
|
|
#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";
|
|
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;
|
|
}
|