3

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 ?

nounoursheureux
  • 167
  • 2
  • 11
  • 1
    I've never messed with this before, but I got chaisript installed. If you change your script to end with MyType().number and call it with auto i = chai.eval_file("script.chai"); it works as expected. I know that's not what you want, but it's just more information – xaxxon Dec 20 '15 at 09:08
  • @xaxxon yes, there is a problem when I try to access a member from the class I defined in the script. BTW, thanks for your help! – nounoursheureux Dec 20 '15 at 09:44
  • 1
    I'm wondering if it doesn't like types defined in scripts to be used outside the script. It's unfortunate the ChaiScript object doesn't let you query it to see what it knows about. You can add stuff, but I can't see any way to ask it about things. – xaxxon Dec 20 '15 at 09:50
  • @xaxxon I just discovered ChaiScript, so I don't know what you can and cannot do with it. Maybe you actually can't use a type defined in a script outside of the script – nounoursheureux Dec 20 '15 at 11:19
  • 1
    If you're still fighting with this, I'd suggest trying to build the class in c++ and then return it from your chaiscript and see if it knows the type then. Or maybe try another, more mature, language that has reasonable documentation on using and embedding it. Javascript might be a good choice. google's v8 is pretty nice. – xaxxon Dec 20 '15 at 18:21

2 Answers2

3

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.

lefticus
  • 3,346
  • 2
  • 24
  • 28
  • Based on your mention of needing to document this I'm assuming chaiscript is your project., I'd like to say that the overall documentation is pretty miserable to work through, having just tried to help solve @nounoursheureux question. doxygen docs are neat for developers but of limited use for normal users. The rest of the docs are a seemingly randomly organized hodge podge of very specific use cases that don't seem to cover much of the language. The coffeescript website is a great example of a language website: http://coffeescript.org/ I'm sure it's a ton of work, but might be worth. – xaxxon Dec 22 '15 at 01:02
1

Not an answer, but I added a bunch of debugging;

  chaiscript::Boxed_Value test_value = chai.eval_file("script.chai");
  auto info = test_value.get_type_info();
  printf("%d\n", info.is_const());
  printf("%d\n", info.is_reference());
  printf("%d\n", info.is_void());
  printf("%d\n", info.is_arithmetic());
  printf("%d\n", info.is_undef());
  printf("%d\n", info.is_pointer());
  printf("%s\n", info.name().c_str());
  printf("%s\n", info.bare_name().c_str());

and got:

0
0
0
0
0
0
N10chaiscript8dispatch14Dynamic_ObjectE
N10chaiscript8dispatch14Dynamic_ObjectE
xaxxon
  • 19,189
  • 5
  • 50
  • 80