I am trying to access from C++ file members from a type I defined in a script. The problem is that Boxed_Value::get_attr
always return a null value.
Here is my C++ file:
#include <chaiscript/chaiscript.hpp>
#include <iostream>
int main()
{
chaiscript::ChaiScript chai;
chaiscript::Boxed_Value test_value = chai.eval_file("script.chai");
chaiscript::Boxed_Value number = test_value.get_attr("number");
std::cout << chaiscript::boxed_cast<int>(number) << std::endl;
}
And script.chai:
class MyType
{
attr number
def MyType
{
this.number = 30
}
}
MyType()
I expected it to print 30, but instead it throwed a bad_boxed_cast
exception. During my investingation I found that number.is_null()
is true.
I obviously did something wrong, but I can't find my mistake.
Or maybe it is not intended to be used this way ?
Boxed_Value::get_attr
is meant for internal use (I really need to document it. Making a note of that now.) It can be generically used to apply attributes to any type of object. These are not attributes which can be looked up by name in ChaiScript with .name
notation.
The function you want is chaiscript::dispatch::Dynamic_Object::get_attr()
. Dynamic_Object
is the C++ type that implements ChaiScript defined objects.
To get access to it you want to:
int main()
{
chaiscript::ChaiScript chai;
const chaiscript::dispatch::Dynamic_Object &test_value = chai.eval_file<const chaiscript::dispatch::Dynamic_Object &>("script.chai");
chaiscript::Boxed_Value number = test_value.get_attr("number");
std::cout << chaiscript::boxed_cast<int>(number) << std::endl;
}
You can also call test_value.get_attrs()
to get the full set of named attributes on the object.