Chaiscript, anyone?

A place for Ogre users to discuss non-Ogre subjects with friends from the community.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Chaiscript, anyone?

Post 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:
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
haffax
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4823
Joined: Fri Jun 18, 2004 1:40 pm
Location: Berlin, Germany
x 8
Contact:

Re: Chaiscript, anyone?

Post by haffax »

Looks interesting enough, waiting for your feedback.
team-pantheon programmer
creators of Rastullahs Lockenpracht
User avatar
nullsquared
Old One
Posts: 3245
Joined: Tue Apr 24, 2007 8:23 pm
Location: NY, NY, USA
x 11

Re: Chaiscript, anyone?

Post by nullsquared »

jacmoe wrote: Even though it requires Boost. :)
A good thing.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Re: Chaiscript, anyone?

Post 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).
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: Chaiscript, anyone?

Post 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. :)
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

Re: Chaiscript, anyone?

Post 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.
User avatar
jacobb
Greenskin
Posts: 127
Joined: Tue Feb 14, 2006 1:34 am
Location: Orlando, FL
x 1

Re: Chaiscript, anyone?

Post by jacobb »

Wow, this looks pretty slick, I'm gonna have to play with this. Nice find!
find / -name base -exec chown -R us {} ';'
Check the API | Read the Manual | Browse the Wiki
User avatar
stealth977
Gnoll
Posts: 638
Joined: Mon Dec 15, 2008 6:14 pm
Location: Istanbul, Turkey
x 42

Re: Chaiscript, anyone?

Post 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,
Ismail TARIM
Ogitor - Ogre Scene Editor
WWW:http://www.ogitor.org
Repository: https://bitbucket.org/ogitor
User avatar
xadhoom
Minaton
Posts: 973
Joined: Fri Dec 28, 2007 4:35 pm
Location: Germany
x 1

Re: Chaiscript, anyone?

Post 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
User avatar
aguru
Goblin
Posts: 236
Joined: Tue Feb 26, 2008 5:48 pm
x 3

Re: Chaiscript, anyone?

Post 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?
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: Chaiscript, anyone?

Post 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? :)
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Re: Chaiscript, anyone?

Post by Klaim »

Isn't it a bit like Falcon (the c++ base oriented) ?
User avatar
madmarx
OGRE Expert User
OGRE Expert User
Posts: 1671
Joined: Mon Jan 21, 2008 10:26 pm
x 51

Re: Chaiscript, anyone?

Post 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!
Tutorials + Ogre searchable API + more for Ogre1.7 : http://sourceforge.net/projects/so3dtools/
Corresponding thread : http://www.ogre3d.org/forums/viewtopic. ... 93&start=0
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: Chaiscript, anyone?

Post 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..
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
efeX
Gnoblar
Posts: 6
Joined: Thu Jul 09, 2009 8:44 pm

Re: Chaiscript, anyone?

Post by efeX »

From the looks of the tutorial and documentation, this looks promising!
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Re: Chaiscript, anyone?

Post 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...
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Re: Chaiscript, anyone?

Post 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!
User avatar
stoneCold
OGRE Expert User
OGRE Expert User
Posts: 867
Joined: Fri Oct 01, 2004 9:13 pm
Location: Carinthia, Austria
x 1

Re: Chaiscript, anyone?

Post 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 ?
User avatar
xadhoom
Minaton
Posts: 973
Joined: Fri Dec 28, 2007 4:35 pm
Location: Germany
x 1

Re: Chaiscript, anyone?

Post 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...
User avatar
stoneCold
OGRE Expert User
OGRE Expert User
Posts: 867
Joined: Fri Oct 01, 2004 9:13 pm
Location: Carinthia, Austria
x 1

Re: Chaiscript, anyone?

Post by stoneCold »

But except boost::thread it looks like it's all header only things, right ?
Thanks for the fast clarification
User avatar
xadhoom
Minaton
Posts: 973
Joined: Fri Dec 28, 2007 4:35 pm
Location: Germany
x 1

Re: Chaiscript, anyone?

Post by xadhoom »

Yes, it seems so.
Exception is only one chaiscript example using the regex lib...

xad
lefticus
Gnoblar
Posts: 5
Joined: Mon Oct 12, 2009 10:05 pm

Re: Chaiscript, anyone?

Post 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
User avatar
xadhoom
Minaton
Posts: 973
Joined: Fri Dec 28, 2007 4:35 pm
Location: Germany
x 1

Re: Chaiscript, anyone?

Post 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
User avatar
Berserker
Kobold
Posts: 25
Joined: Sat May 01, 2004 3:41 pm

Re: Chaiscript, anyone?

Post 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:
lefticus
Gnoblar
Posts: 5
Joined: Mon Oct 12, 2009 10:05 pm

Re: Chaiscript, anyone?

Post 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
Post Reply