About ChaiScript

ChaiScript is the first and only scripting language designed from the ground up with C++ compatibility and modern design in mind. It is an ECMAScript-inspired, embedded functional-like language.

ChaiScript is licensed under the BSD license.

Download

Due to an increased release schedule, and the nature of ChaiScript being a header-only library, binary releases will be made less often. Previous releases can be found on github.

Version: 4.2.0 Released: 12/1/2012
(Requires Boost)
Source (tar.bz2)
Source (zip)

Version: 5.2.0 Released: 12/1/2012
(Requires C++11 Compiler)
Source (tar.bz2)
Source (zip)

Recent comments

  • Chaiscript performance   32 weeks 23 hours ago

    This is a rather crude test and they do not claim to be an absolute measure of performance. If you are somebody give an example - I tried to do more tests.

    In turn, I would say that is a really nice Chaisript syntax, and it is easy to register functions and classes (although in python and lua it done about as well), but the question of productivity is still open.

  • Passing by value vs. passing by reference?   32 weeks 1 day ago

    So everyone else knows, I did add a handful of new functions for inserting into maps. Please see https://github.com/ChaiScript/ChaiScript/blob/master/unittests/map.chai for examples. It's also important to remember that map is a direct mapping of std::map.

    I'll get a search function added as soon as I can.

    -Jason

  • How to explicitly set a var to null from inside Chaiscript?   32 weeks 4 days ago

    Object lifetime works like it does in C++:

    { // create some scope for some useage
      var obj = Object() // alloc 200 mb
    } 
    // scope exits and 200 mb is freed
  • Chaiscript performance   32 weeks 4 days ago

    ChaiScript has never been intended for doing calculations with fast performance. If you truly need to sum 10,000 digits, make a C++ function that does it and export the function to ChaiScript. There is ChaiScript's main advantage: you can expose a C++ to ChaiScript with a simple 1 line of code, far less work than any other scripting language available.

    Do you have a more real world example of the code that is slowing you down / making it so you cannot use ChaiScript in your project? Please share and I will work out optimizing for such cases.

    Thanks,
    Jason

  • Chaiscript performance   32 weeks 4 days ago

    Are you initializing the ChaiScript engine with each frame? That will definitely slow you down. If you are doing a few simple calculations and simply setting some variables, that does surprise me.

    Also, have you compared release vs debug builds? ChaiScript performance is heavily influenced by compiler optimization settings.

    Can you paste what your per-frame code is like and I can run some tests to see if I can optimize it further.

    Thanks,
    Jason

  • How to explicitly set a var to null from inside Chaiscript?   32 weeks 6 days ago

    var obj := Object() // alloc 200 Mb memory
    var obj2 := obj.work() // alloc 100 Mb memory
    var obj = 0 // free 200 Mb memory

  • Chaiscript performance   33 weeks 20 hours ago

    I also care about this issue, and therefore I decided to do a little test. I compared the python and chaisript:
    python:

    for i in range(0, 10000):
        print "Hello"

    chaiscript:

    for (var i = 0; i < 10000; ++i)
    {
        print("Hello");
    }

    the result:
    Python time: ~10ms
    Chaiscript time: ~1600ms

    !!!!!!

    But, this is not the best example. Let me know if you want more tests.
    But as long as the ChaiScript seems not so fast. Especially given the fact that Python is quite slow embedded language and I find it very frustrating. =(

    UPD:
    python:

    for i in range(0, 10000):
        pass;

    chaiscript:

    for (var i = 0; i < 10000; ++i) {}

    result:

    Python time: 2ms
    Chaiscript time: 45ms

    UPD2:
    python:

    a = 0;
    b = 0;
    c = 0;
    for i in range(0, 10000):
        a = 999;
        b = 1;
        c = a + b;

    chaiscript:

    var a;
    var b;
    var c;
    for (var i = 0; i < 10000; ++i)
    {
        a = 999;
        b = 1;
        c = a + b;
    }

    lua:

    a = 0
    b = 0
    c = 0
    for i=0,10000
    do
        a = 999
        b = 1
        c = a + b
    end

    result:

    Python time: 4ms
    Chaiscript time: 244ms !!!
    Lua time: 4ms

  • Passing by value vs. passing by reference?   35 weeks 1 day ago

    I am trying to create a map of references.

    (These are references to C++ objects, which were created in the C++ and then passed into ChaiScript. I do NOT want to copy the objects themselves into the map, but just store a map of references.)

    When I do this: sub_map = [strSellStatus: offerData]

    I get this error: ---------------
    OTScriptChai::ExecuteScript:
    Caught chaiscript::exception::eval_error:
    Can not find appropriate copy constructor or 'clone' while inserting into Map..
    File:
    Start position, line: 0 column: 0
    End position, line: 0 column: 0

    Error: "Can not find appropriate copy constructor or 'clone' while inserting into Map." With parameters: (OTDB_OfferDataNym) during evaluation at (3596, 28)

    ===> HOW can I insert my objects into this map BY REFERENCE and without having to clone the objects?

    ===> Cannot find anything about this in the documentation, nor in the unittests.

    ===> (In fact the unittests don't show at all how to add objects to an existing map -- only how to construct one. I'm using += as a guess.)

    ===> I also tried to search the forum but couldn't find the search functionality. Educate me?

    ===> Perhaps there is some reference wrapper?
    Like this: sub_map = [strSellStatus: ref offerData]
    Or this: sub_map = [strSellStatus: Ref_Wrapper(offerData)]

    ===> Or is that the solution? (That I should just write my own "reference wrapper" class.)

  • Chaiscript performance   36 weeks 14 hours ago

    PS: I use ChaiScript 4.0.0.

  • CHAISCRIPT_NO_THREADS flag error   38 weeks 16 hours ago

    Unfortunately, I still had problems. I will try to describe them.
    1. I download zip archive from GitHub with latest version of ChaiScript
    2. Extract the archive in "$PROJECT_FOLDER/chaiscript"
    3. Do "cd chaiscript", "cmake ." and "make"

    After that, in chaiscript folder appears binary "chai". It works fine(!).

    Then I write some code in $PROJECT_FOLDER/main.cpp :

    #include <chaiscript/chaiscript.hpp>
     
    using namespace chaiscript;
     
    int main()
    {
        ChaiScript context;
        context.eval("print(\"hello\")");
        return 0;
    }

    and build it. I use QtCreator to build. On output:

    g++ -c -pipe -std=c++0x -march=i686 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 -Wall -W  -I/usr/share/qt/mkspecs/linux-g++ -I../dummy -Ilib/chaiscript/include -I../dummy -I. -o main.o ../dummy/main.cpp
    g++ -Wl,-O1,--sort-common,--as-needed,-z,relro -Wl,-O1 -o dummy main.o     -ldl -lpthread 

    it build programm called "dummy" in $PROJECT_FOLDER/../.dummy-release

    next if i try to exec binary i have error like in first message of this topic.
    I tried to run from different locations:
    $PROJECT_FOLDER
    $PROJECT_FOLDER/chaiscript
    $PROJECT_FOLDER/../.dummy-release
    But the problem did not disappear...

    Then i check chaiscript folder. I found "libreflection.so", "libstl_extra.so", "libtest_module.so", but no "libchaiscript_stdlib.so" or some other .so files.
    In CMakeList.txt i found references to flection and stl_extra libraries, but nothing about chaiscript_stdlib or something similar.

    Please, help me. How to build chaiscript_stdlib library?

    By the way: Why not distribute everything in a library with headers like others? =)

  • Using unsigned int   38 weeks 1 day ago

    I'll wait for the implementation of this feature =)

    By the way, can I also override the class method?

    And do you have a plan of work for which you are moving? =)
    Can we see something like what to expect in the new versions? =)

  • Problem exposing derived classes   40 weeks 1 day ago

    I'm glad you found a solution that works for you. The problem is a "feature" of the C++ language. A class does not have a vtable if it has no virtual functions. If it doesn't have a vtable, dynamic_cast cannot be used. If dynamic_cast cannot be used, I cannot support base_class.

    -Jason

  • Abort script execution   40 weeks 1 day ago

    Considering C++ functions are so easy to add, the best bet would be to throw from inside of a C++ callback.

    //main.cpp
     
    void should_I_keep_running()
    {
      if (somecondition)
      {
        throw std::runtime_error("Running aborted");
      }
    }
     
    int main()
    {
      chaiscript::ChaiScript chai;
      chai.add(chaiscript::fun(&should_I_keep_running), "should_I_keep_running");
     
      chai.eval("while (true) { /*do something*/; should_I_keep_running(); }");
    }

    This is on par with what other scripting engines allow you to do.

    A possible other option would be to insert points into the chaiscript evaluation code that allows you to interrupt loops internally. Options like that would slow down the interpreter, however.

    What are your thoughts?

    -Jason

  • forward output of print to stringstream instead of stdout   40 weeks 1 day ago

    You can do it on a temporary basis in a local context:

    var print = fun(x) : { puts("My String: " + to_string(x) + "\n"); }

    but that's not really what you want.

    I've made a feature request to add it.

    https://github.com/ChaiScript/ChaiScript/issues/67

    -Jason

  • Using unsigned int   40 weeks 1 day ago

    On a side note, if you would like to support just one function to make it more accessible to your users (or yourself) you could add a ChaiScript overload:

    def mainFunc(value)
    {
      mainFunc(unsigned_int(value));
    }

    This will work with any value that can be converted to an unsigned_int. It will take lower priority over the strongly typed C++ version you registered so will only add overhead in the case where you call it with anything other than an unsigned_int.

    -Jason

  • Using unsigned int   40 weeks 1 day ago

    It is cumbersome, I agree, but ChaiScript has a few unique challenges, trying to straddle the line between C++ and Scripting Land.

    If we support automatic conversions, it makes the choice as to which version of the function to call much more complicated (say, if you had both signed and unsigned versions of a method). It also adds a fair bit of overhead to make that determination at runtime.

    It might happen in the future, but it is pretty low priority right now.

    -Jason

  • CHAISCRIPT_NO_THREADS flag error   40 weeks 1 day ago

    Glad to know it's working!

    Jason

  • Problem exposing derived classes   40 weeks 3 days ago

    i had success binding it as std::function

    chai.add( fun(std::function<float(const LocalPlayer&)>(&LocalPlayer::getHealth)), "getHealth" );
  • CHAISCRIPT_NO_THREADS flag error   40 weeks 5 days ago

    Sorry for the long absence. I can not remember which version I used, but I decided to also check everything again.
    I downloaded version 5.0.0 and try to compile test program (interpreter) with and without multithreaded support option and run it. In both cases, no errors =) I'm sorry for your time. I'll let you know if the error is repeated =)

  • Using unsigned int   40 weeks 5 days ago

    Do you plan to support automatic conversions? Or is it one of the feature of ChaiScript?
    The first thing I thought, it's situation like this:
    c++:

    int intFunc() { ... };
    unsigned int uintFunc() { ... };
    void mainFunc(unsigned int value) { ... };

    If i export this functions, i can do like this:

    mainFunc(uintFunc());

    but can't do:

    mainFunc(intFunc());

    and i must do:

    mainFunc(unsigned_int(intFunc()));

    I think it's a little uncomfortable. But this is my personal opinion. How do you look at this problem?

    P.S. Sorry for bad English

  • ChaiScript as library (static or dynamic)   43 weeks 3 days ago

    What you are asking for is virtually impossible. We could build a library, but it could not be used in the way you ask. The problem is that most of ChaiScript is necessarily template based. Templates are required for automatically determining the types of objects and functions that are wrapped. Any change to the ChaiScript internals would invalidate the code you have wrapped and swapping out a DLL would do no good.

    Your best bet would be to just keep using it as a header only library and swap out your shared code as a DLL.

    -Jason

  • get/set state not getting rid of variables ?   43 weeks 3 days ago

    Please see this task for more info and a solution to what you need: https://github.com/ChaiScript/ChaiScript/issues/25

    -Jason

  • CHAISCRIPT_NO_THREADS flag error   43 weeks 4 days ago

    Great, I'm glad you got it working. Sorry I had not yet had time to set up a mingw dev environment myself.

    It's unfortunate you cannot make a debug build, just in case you needed it later.

  • CHAISCRIPT_NO_THREADS flag error   43 weeks 5 days ago

    I have no cmake available (basically all I can work with is make). I use the official MinGW distribution http://www.mingw.org/ 32 Bit latest install from repositories.

    As for the gcc usage, in your documentation: http://chaiscript.com/doxygen/index.html
    The command line for compiling the first sample is

    gcc main.cpp -I/path/to/chaiscript/headers -ldl -lboost_threads

    EDIT:
    I managed to compile the code with

    g++ -std=c++11 Main.cpp src\chaiscript_stdlib.cpp -ldl -L. -Iinclude -DCHAISCRIPT_NO_THREADS -O3
    #include <chaiscript/chaiscript_stdlib.hpp>
    #include <chaiscript/chaiscript.hpp>
     
    int main()
    {
    	chaiscript::ChaiScript cscr(chaiscript::Std_Lib::library());
    	cscr.eval("print(\"Test\")");
     
    	return 0;
    }

    (I had to download libdl.a for mingw, for some reason its not packed with all the other libraries)
    The -O3 option seems to do the trick (source: http://permalink.gmane.org/gmane.comp.parsers.spirit.general/24423)
    I guess this is really a problem with gas

    And now if you excuse me, I have a new script language to mess around with

  • get/set state not getting rid of variables ?   44 weeks 1 day ago

    Another user brought up the same question. There are some technical details that I have to look into, but it might be possible to make the state switching also swap out the local variables.

    -Jason

Syndicate content
Support ChaiScript by sharing it with your friends: