Page 1 of 2

Chaiscript, anyone?

Posted: Tue Oct 06, 2009 11:28 pm
by jacmoe
I stumbled over something called ChaiScript:
ChaiScript is the first and only scripting language designed from the ground up with C++ compatibility in mind. It is an ECMAScript-inspired, embedded functional-like language.
I am not really sure if it's the 'first and only scripting language designed from the ground up with C++ compability in mind', but it looks interesting.
Even though it requires Boost. :)

-> http://www.chaiscript.com/

Have any of you used it with Ogre?

Seems to be fairly easy to integrate as it's only one header.

I'll try it out with some Ogre code later. :wink:

Re: Chaiscript, anyone?

Posted: Tue Oct 06, 2009 11:51 pm
by haffax
Looks interesting enough, waiting for your feedback.

Re: Chaiscript, anyone?

Posted: Wed Oct 07, 2009 12:29 am
by nullsquared
jacmoe wrote: Even though it requires Boost. :)
A good thing.

Re: Chaiscript, anyone?

Posted: Wed Oct 07, 2009 5:09 am
by Kojack
Interesting.
I like their example code:

Code: Select all

#include <chaiscript/chaiscript.hpp>
int add(int x, int y) {
  return x+y;
}
 
int main() {
  using namespace chaiscript;
 
  ChaiScript chai;
  chai.add(fun(&add), "add");  //register our 'add' function under the name "add"
  chai.eval("print(add(3, 4))");
 
  return 0;
}
That's all it takes to create a Chaiscript interpreter, export a c++ function to it and then execute some script code.

Although I'm not really much of a ECMAScript fan (javascript/actionscript/etc).

Re: Chaiscript, anyone?

Posted: Wed Oct 07, 2009 2:19 pm
by jacmoe
What I like is that you are able to do some really hefty binding in just a couple of lines of code:
http://code.google.com/p/chaidraw/sourc ... aidraw.cpp
For instance, here's how to expose some Cairo drawing functions to ChaiScript:

Code: Select all

    using namespace chaiscript;
    chaiscript::ChaiScript chai;

    chai.add(fun(&fabs), "abs");

    chai.add(chaiscript::var(this), "drawingarea");
    chai.add(chaiscript::var(m_cairo_context.operator->()), "context");

    chai.add(fun(&DrawArea::get_width), "get_width");
    chai.add(fun(&DrawArea::get_height), "get_height");

    chai.add(fun(&Cairo::Context::arc), "arc");
    chai.add(fun(&Cairo::Context::arc_negative), "arc_negative");
    chai.add(fun(&Cairo::Context::begin_new_path), "begin_new_path");
    chai.add(fun(&Cairo::Context::begin_new_sub_path), "begin_new_sub_path");
    chai.add(fun(&Cairo::Context::clip), "clip");
    chai.add(fun(&Cairo::Context::clip_preserve), "clip_preserve");
    chai.add(fun(&Cairo::Context::close_path), "close_path");
    chai.add(fun(&Cairo::Context::curve_to), "curve_to");
    chai.add(fun(&Cairo::Context::fill), "fill");
    chai.add(fun(&Cairo::Context::fill_preserve), "fill_preserve");
    chai.add(fun(&Cairo::Context::line_to), "line_to");
    chai.add(fun(&Cairo::Context::move_to), "move_to");
    chai.add(fun(&Cairo::Context::paint), "paint");
    chai.add(fun(&Cairo::Context::rectangle), "rectangle");
    chai.add(fun(&Cairo::Context::restore), "restore");
    chai.add(fun(&Cairo::Context::save), "save");
    chai.add(fun(&Cairo::Context::stroke), "stroke");
    chai.add(fun(&Cairo::Context::stroke_preserve), "stroke_preserve");
    chai.add(fun(&Cairo::Context::set_source_rgb), "set_source_rgb");
    chai.add(fun(&Cairo::Context::set_source_rgba), "set_source_rgba");

    chai(script);
I hope I get the time to do some Ogre testing soon. :)

Re: Chaiscript, anyone?

Posted: Wed Oct 07, 2009 3:43 pm
by nikki
Wow, jacmoe, nice find! :D I'm already knee-deep in Python (which I totally love now), but ChaiScript looks real good. I should play with it some time.

Re: Chaiscript, anyone?

Posted: Thu Oct 08, 2009 4:31 am
by jacobb
Wow, this looks pretty slick, I'm gonna have to play with this. Nice find!

Re: Chaiscript, anyone?

Posted: Thu Oct 08, 2009 7:49 am
by stealth977
@jacmoe:

Jacob, what about the performance? How fast is it? Also can it multi-thread? Can the script variables be exposed as Properties? (Basicly would it be feasible to use it in OGF?)

ismail,

Re: Chaiscript, anyone?

Posted: Thu Oct 08, 2009 9:23 am
by xadhoom
stealth977 wrote:Also can it multi-thread?
Multi threading seems to work:
Chai Forum wrote: The latest SVN version now has thread safety. You can register any functions in one thread and they will be visible to all threads. Context and state are handled for you automatically.

There is a minor caveat, however. If a function is added in one thread, no other currently executing script on a different thread will see that new function. However, any new evaluation started after the function is added will see the new function.

Also, file includes are supported as of the last release in the form of "use." The new threading support works with "use" as well, so that you can safely include the same file in multiple executing threads by calling "use" from each one.

The latest SVN does add library requirements that did not exist before, namely: libboost_thread.

An example of the multithreaded support with "use" is available in svn.
----
While you can't share the same ChaiScript engine instance, you can run multiple ChaiScript engine instances and share objects between them using one of the Boost methods, like:

boost::shared_ptr obj(new Class());

Then sharing 'obj' between two (or more) ChaiScript engine instances.
----
I just wanted to comment that the next version of ChaiScript (2.0) will have shared ChaiScript engine thread safety available. We expect to release it very soon.
xad

Re: Chaiscript, anyone?

Posted: Thu Oct 08, 2009 12:56 pm
by aguru
Is it possible to save/load the script state, including the context stack? Like pythons pickle module - I also seem to remember this is possible with lua. Otherwise using the scripts for anything more than simple triggers can prove difficult. Or am I missing something?

Re: Chaiscript, anyone?

Posted: Thu Oct 08, 2009 3:50 pm
by jacmoe
stealth977 wrote:@jacmoe:

Jacob, what about the performance? How fast is it? Also can it multi-thread? Can the script variables be exposed as Properties? (Basicly would it be feasible to use it in OGF?)

ismail,
It's a bit early, but from what I've heard, it performs slightly slower than Boost Python.
I am not sure if that's before or after the multi-threading bit.
I see that they are actively hacking on the library, so I guess that will change.

Using it would require Boost, so that means that switching to TBB would be impractical for us.

ChaiScript and C++ talks to each other natively, so it is possible to extend C++ classes from within the scripts.

I am pretty sure the developers are willing to bend to specific needs, though.

I seems to be too good to be true? :)

Re: Chaiscript, anyone?

Posted: Thu Oct 08, 2009 8:18 pm
by Klaim
Isn't it a bit like Falcon (the c++ base oriented) ?

Re: Chaiscript, anyone?

Posted: Thu Oct 08, 2009 11:14 pm
by madmarx
Good find, Jacmoe!
I suppose it is possible to create it's own 'object oriented scripted class' ?

EDIT:

Code: Select all

attr Rectangle::height
attr Rectangle::width
def Rectangle::Rectangle() { this.height = 10; this.width = 20 }
def Rectangle::area() { this.height * this.width }
 
var rect = Rectangle()
rect.height = 30
print(rect.area())
pretty simple!

Re: Chaiscript, anyone?

Posted: Fri Oct 09, 2009 2:39 am
by jacmoe
Klaim wrote:Isn't it a bit like Falcon (the c++ base oriented) ?
I haven't been able to find anything useful from browsing the Falcon docs - it's horrible. :o
Maybe a search through this forum will prove more useful..

Re: Chaiscript, anyone?

Posted: Fri Oct 09, 2009 6:52 am
by efeX
From the looks of the tutorial and documentation, this looks promising!

Re: Chaiscript, anyone?

Posted: Fri Oct 09, 2009 10:39 am
by Klaim
jacmoe> We talked about it on a specific thread here : http://www.ogre3d.org/forums/viewtopic. ... lit=falcon

The doc is available there for embedding : http://falconpl.org/index.ftd?page_id=s ... ders+guide

At the time I implemented it I had some problem that I managed to solve to get it right (details there : http://www.klaimsden.net/blog/2009/firs ... s-scripts/ )

I'm looking at chairscript too as it seem to fit to what I need. Will have to try that...

Re: Chaiscript, anyone?

Posted: Fri Oct 09, 2009 11:49 am
by Klaim
http://www.chaiscript.com/node/28 > *eye brow up*

If that's really as easy at it seems, it is f*cking good! O__O!

Re: Chaiscript, anyone?

Posted: Mon Oct 12, 2009 2:40 pm
by stoneCold
Kojack wrote:...Although I'm not really much of a ECMAScript fan (javascript/actionscript/etc).
You should give ActionScript 3 a chance though, it's far more geared towards object oriented scripting than it's predecessors or javascript, etc.

About ChaiScript, it's definately looking interesting, too good to be true almost.
I'll definately have to play with it someday soon, does someone know which parts of boost this does rely on ?

Re: Chaiscript, anyone?

Posted: Mon Oct 12, 2009 2:46 pm
by xadhoom
chaiscript.hpp wrote:#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
boxed_value.hpp wrote:#include <boost/any.hpp>
#include <boost/ref.hpp>
#include <boost/cstdint.hpp>
#include <boost/type_traits/add_const.hpp>
type_info.hpp wrote:#include <boost/type_traits/is_const.hpp>
#include <boost/type_traits/is_void.hpp>
#include <boost/type_traits/is_reference.hpp>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/type_traits/remove_pointer.hpp>
And for threading:
chaiscript_threading.hpp wrote:#include <boost/thread.hpp>
Its quiet a bit. Maybe I´ve missed some...

Re: Chaiscript, anyone?

Posted: Mon Oct 12, 2009 2:54 pm
by stoneCold
But except boost::thread it looks like it's all header only things, right ?
Thanks for the fast clarification

Re: Chaiscript, anyone?

Posted: Mon Oct 12, 2009 10:34 pm
by xadhoom
Yes, it seems so.
Exception is only one chaiscript example using the regex lib...

xad

Re: Chaiscript, anyone?

Posted: Tue Oct 13, 2009 4:49 am
by lefticus
Hi,

I'm one of the two main ChaiScript developers. I've been following the discussion here and thought I would chime in.

First of all, yes, it really is that easy to use, that's the entire point of the tool. I had personally gotten frustrated with other scripting tools and scripting preprocessors like SWIG and got the idea that led to ChaiScript.

We are performance minded, but the performance is not spectacular. As a rule of thumb you should keep the API's you expose to ChaiScript as high-level as possible. Put another way, "Can you write a raytracer in ChaiScript?" "yes, but if you do, you are using it wrong."

We are totally willing to work with people who have reasonable enhancements they would like to see. I think we've managed to address every request that has come up so far.

The library is implemented as header only. The only library dependency we have is boost::thread. If you do not need threading support you can eliminate that requirement and increase performance by compiling with -DCHAISCRIPT_NO_THREADS.

Let me know if you have any other questions.

-Jason

Re: Chaiscript, anyone?

Posted: Tue Oct 13, 2009 9:25 am
by xadhoom
I´m curious.
When creating a function object from a chai script and executing it several time will the script be parsed and interpeted on every exectution again? Or is there some precomputation happening? (I try to a avoid the word compilation here...)

xad

Re: Chaiscript, anyone?

Posted: Tue Oct 13, 2009 9:35 am
by Berserker
lefticus wrote:Let me know if you have any other questions.
Two questions :)

1) Why did you implement a custom api instead of the "common" boost one (boost::python or luabind)?
2) Are you going to support an "autowrapper"? I mean something like Py++. In my experience of embedding scripting engines (spydermonkey, python, lua ecc..), the most tedious part is when I have to expose my C/C++ library: at the moment I'm trying to automate this process using Py++ but with a lot of problems (mostly related to the CPython's GIL :twisted:, you can read some posts of mine in the Py++ mailing list) .

I'm always interested in looking at new scripting engines because I didn't found the "perfect" solution for me yet :lol:

Re: Chaiscript, anyone?

Posted: Tue Oct 13, 2009 2:20 pm
by lefticus
xadhoom wrote:I´m curious.
When creating a function object from a chai script and executing it several time will the script be parsed and interpeted on every exectution again? Or is there some precomputation happening? (I try to a avoid the word compilation here...)

xad
Script strings are parsed once, at the time of the "eval" call.

So, assuming you do not intentionally thwart that with code like:

chai.eval("something");
chai.eval("something");

But instead do something like:

boost::function<void ()> chaifun = chai.functor<void ()>("print(\"hello world\")");
chaifun();
chaifun();

The print command is only parsed once.

The interpretation does happen each time, mostly because ChaiScript is very free about how you define functions/objects/functionobjects so it's possible that "print" could actually change meaning between calls if you really wanted it to.

-Jason