These are chat archives for ChaiScript/ChaiScript

Oct 2016
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?"
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;;? Thank you
Jason Turner
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
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 {
    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");


    try {
             var i = MyClass;
    } catch (const chaiscript::exception::eval_error& e) {
        std::cout << "Error\n" << e.pretty_print() << '\n';
And I got this output,
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
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
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
Oct 20 2016 20:00