These are chat archives for ChaiScript/ChaiScript

20th
Oct 2016
robotan0921
@robotan0921
Oct 20 2016 05:45
Hello, I have some questions.
Do ChaiScript apply the VM mechanism such as Java?
I wonder that a *.chai file is compiled to a bytecode, or not.
The other question is that "Do ChaiScript support coroutine?"
sinadogru
@sinadogru
Oct 20 2016 08:53
Hello all, I would like to use my C++ class from ChaiScript. I would like to be able to construct my C++ class and call member function of it. Is it possible? For example if I have a class called MyClass with a member function called foo, can I construct a MyClass instance and call it foo member function, var m = MyClass; m.foo();? Thank you
Jason Turner
@lefticus
Oct 20 2016 15:45
@robotan0921 it does not use a VM mechanism. And no, there are no coroutines, but coroutines are being considered.
@sinadogru yes, this is one of the most basic use cases of ChaiScript. Every internal type (string, vector, map) are direct mappings of the C++ equivalents. Here are some examples of a full class hierarchy with types, constructors, etc https://github.com/ChaiScript/ChaiScript/blob/3b2c82ba2c37d1b398bb92d53ed5f266d8f51666/src/test_module.cpp#L154-L189
sinadogru
@sinadogru
Oct 20 2016 19:08
@lefticus thank you for answering, I think I misunderstand something, can you please see the code below;
#include <iostream>
#include <chaiscript/chaiscript.hpp>

class MyClass {
public:
    MyClass() { std::cout << "MyClass::MyClass()" << std::endl; }
    void foo() { std::cout << "MyClass:foo()" << std::endl; }
};

int main()
{
    chaiscript::ChaiScript chai;
    chaiscript::ModulePtr m{new chaiscript::Module};

    m->add(chaiscript::user_type<MyClass>(), "MyClass");
    m->add(chaiscript::constructor<MyClass()>(), "MyClass");
    m->add(chaiscript::fun(&MyClass::foo), "foo");

    chai.add(m);

    try {
        chai(R"(
             var i = MyClass;
             i.foo();
             )");
    } catch (const chaiscript::exception::eval_error& e) {
        std::cout << "Error\n" << e.pretty_print() << '\n';
    }
}
And I got this output,
Error
Error: "Error with function dispatch for function 'foo'" With parameters: (Function).(string) during evaluation at (__EVAL__ 3, 14)

  2 overloads available:
      (Dynamic_Object).(const string)
      (const Dynamic_Object).(const string)

  __EVAL__ (3, 14) 'i foo   '
Jason Turner
@lefticus
Oct 20 2016 19:18
MyClass() you need to actually call the constructor. What you are doing is making i an alias for the constructor MyClass
sinadogru
@sinadogru
Oct 20 2016 19:47
@lefticus :+1: thank you so much. Btw I really enjoy to reading the source code of ChaiScript, it is a well written codebase. Cheers.
Jason Turner
@lefticus
Oct 20 2016 20:00
Thanks