static std::string chai_message = "";
static chaiscript::ChaiScript::State default_state;
void ros_print(const std::string &s) { chai_message = s; }
std::string helloWorld(const std::string &t_name) { return "Hello " + t_name + "!"; }
ChaiSystem::ChaiSystem() : chai(nullptr) {}
void ChaiSystem::initialize() {
chai = new chaiscript::ChaiScript();
chai->add(chaiscript::fun(&helloWorld), "helloWorld");
chai->add(chaiscript::fun(&ros_print), "ros_print");
default_state = chai->get_state();
}
void ChaiSystem::execute() {
static char script[16 * 1024] = "var x= 1\nros_print(helloWorld(\" Bob \"));";
try {
chai->set_state(default_state);
chai->eval(script);
} catch (chaiscript::exception::eval_error &ee) {
chai_message = "Error: " + ee.reason;
throw;
} catch (...) {
chai_message = "Unknown Error: ";
}
}
chai->set_state(default_state)
doesn't seem to reset my global state
Hi, I'm trying to do a double map like this:
this.anims = [
"stand": [
"left": Animation(love.graphics.newImage("assets/bomberman_stand_left.png"), 24, 24, 0.2f),
"right": Animation(love.graphics.newImage("assets/bomberman_stand_right.png"), 24, 24, 0.2f),
"up": Animation(love.graphics.newImage("assets/bomberman_stand_up.png"), 24, 24, 0.2f),
"down": Animation(love.graphics.newImage("assets/bomberman_stand_down.png"), 24, 24, 0.2f)
],
"walk": [
"left": Animation(love.graphics.newImage("assets/bomberman_walk_left.png"), 24, 24, 0.2f),
"right": Animation(love.graphics.newImage("assets/bomberman_walk_right.png"), 24, 24, 0.2f),
"up": Animation(love.graphics.newImage("assets/bomberman_walk_up.png"), 24, 24, 0.2f),
"down": Animation(love.graphics.newImage("assets/bomberman_walk_down.png"), 24, 24, 0.2f)
]
]
this.anim = this.anims[this.stance][this.direction]
But I'm getting this error: Error: "Missing clone or copy constructor for right hand side of equation" With parameters: (Image)
var status
is some times an int, and some times a string, and it should always be an int
run()
is a C++ function that always returns an int, and internally does an eval()
on a script
var running = true;
while(running)
{
printRaw("/");
var line = readLine();
if(line == "exit")
{
running = false;
print("Good bye.");
}
else
{
var status = run(line);
print(status);
if(status == 0)
{
print("Success");
}
else
{
print("Program returned error");
}
}
}
int ChaiOs::run(const string& path)
{
if(fileExists(path))
{
try
{
auto script = loadScript(path);
int r = chai.eval<int>(script);
return r;
}
catch(chaiscript::exception::eval_error& e)
{
cout << "eval_error: " << endl << e.pretty_print();
return 1;
}
catch(chaiscript::exception::bad_boxed_cast& e)
{
cout << "bad_boxed_cast" << endl << e.what();
return 1;
}
}
else
{
cout << "Program not found" << endl;
return 1;
}
}
printRaw("Test Program, type something:");
var input = readLine();
print("You entered: " + input);
return 0;
Hi all!
I have a question regarding the chai embedded interpreter in C++. Is there any kind of environment/isolation/sandbox (milion of names) while doing eval
so that the objects/variables/functions declared inside the script reside only in the environment?
Example would be
auto script = R("
hp = 10";
def attack() { hp -= 5; }
");
chai.eval(script, env1);
chai.eval(script, env2);
chai.eval("attack()", env1);
assert.true(env1["hp"], 5);
assert.true(env2["hp"], 10);