About 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.

ChaiScript is licensed under the BSD license.

Download

Version: 2.3.3 Released: 5/15/2010

Source
Windows
Linux

Recent comments

  • ChaiScript Questions   3 days 3 hours ago

    I was thinking of using a type map in a little different way. How ever this still requires the user to specify the types in this way which is a little cumbersome and it has another problem still.
    The main problem is that in this way the language and engine are not used to enforce any kind of value assignment correction. This is not a major problem since I can reject a variable using the type when it is used by the db engine if the values of attributes don't fit the specification or if there isn't a full map declared but it's less elegant and also it's a late check and it's always best to capture errors early. Also the user can later change the map without a problem and accidentally or deliberately force wrong value insertion (This can be avoided by an error check not just against the type but also against the type definition in the DB but that takes the error checking even further down the pipeline)

    If you would add the option to specify the type of an attribute I was thinking in using it in one as follows:

    Supply from C++ to Chaiscript a function called CreateType that will accept a type name and a map in the form you specified. This function will then create a Chaiscript block to define this type with the strong typing and register it with the db engine. This way the language and engine can be used to enforce the safety of assignments. And every time the db engine is loaded the type will be reloaded.

    Another option will be to supply a RegisterType class that will work with the option you suggested but in that way we will not benefit from early error detection.
    If you can support strong typing then at least the next time the db engine is loaded the type safety will be enforced.

    So I see it's not a must have but for me it's a really really nice to have.

    Is it hard to implement?

    Thanks
    Sebastian

  • ChaiScript Questions   4 days 1 hour ago

    We designed ChaiScript foremost as a tool for tying together your C++ objects, and did not initially consider that you would want to define types in ChaiScript.

    Then, as we got further along and decided to give types a try, we thought they should be in the spirit of the rest of ChaiScript and very free form. If you want something rigid, you should define it in C++, not in ChaiScript.

    However, I see your point about wanting to create types that are passed back from ChaiScript land into C++ land.

    Your proposal is interesting, but I don't think it would ultimately solve your problem, because the dynamically typed attr would still exist. The user could ignore that option and still make things dynamically typed.

    Perhaps requiring the user to provide a typemap would work for you? This is something you could validate at runtime and perform any conversions that you wanted to based on what the user specified.

    attr Rectangle::type_map
    attr Rectangle::height
    attr Rectangle::width
    fun Rectangle::Rectangle() { this.type_map = ["height":int_type, "width":int_type]; }

    This would make you do a little bit more heavy lifting, but by requiring the user to provide a type_map attribute, you could reject an object simply on its being missing. Then, you could further use the type_map as a driver to determine which attributes should get stored and how they should get stored.

    This would allow the user even to use internal attributes for whatever purposes he has in mind, but will never get stored by your database tool.

    I'm sure there are unanswered questions about the best way to extract/access/convert the stored data that we could help you with.

    It's at least an idea, anyhow.

    Just out of curiosity, is this a closed or open project? Is it something we would like to add to the list of projects using ChaiScript?

  • ChaiScript Questions   4 days 5 hours ago

    I looked into Dynamic_Object and it can satisfy most of my needs.

    I'm looking to use chaiscript as a scripting language for a kind of a database engine.
    And it would be useful to allow the user to create types using the standard mechanism and for me to be able to store data using those types. The Dynamic_Object will allow me to store the object and it's associated data.
    However I have still to decide whether weak typing is good enough for me. If not this method will not allow for creating a schema that can be reused (How can you compare objects if every instance can store different type of data under the same attribute name?). It would have been best if during the creation of the type there was the option to decide which type each attribute will have before instantiating the object.
    Like (formal):

    Attribute Definition ::= "attr" class_name "::" attribute_name
                           | "attr" class_name "::" attribute_name type_name

    And a sample:

    attr Rectangle::Height int

    If the user uses this notation then values assigned to Rectangle::Height will be converted to int if possible or an exception is thrown (or any other predictable behavior).

    Unless you are willing to add this behavior I think I will still use chaiscript but I will produce a different mechanism for creating user defined types that can be stored in that database.

    Thanks
    Sebastian

  • ChaiScript Questions   5 days 3 hours ago

    There are a variety of techniques that can be used to share data between chaiscript defined objects and C++. I've just checked in a unit test to verify that it all worked, and provide some examples.

    Regarding forcing a variable to be a certain type or not, let's start by clarifying terminology. Hopefully I have this right, but ChaiScript is a strongly typed dynamic language. The variable type is determined at runtime, but once it is set, it is set permanently. There are very few automatic type conversions. Conversions between pointer, reference, const, boost::shared_ptr happen automatically, whenever they safely can. Pointers that would require modifying the underlying type (ie, int->float) do not happen automatically.

    There is exactly one type of variable declaration in ChaiScript, the "var" that has its type set on the first assignment operation.

    However, you can force something to be a certain type during a function call by using function guards, like:

    fun test(x) : x.is_type(int_type) {
     
    /* do something */
     
    }

    Hope that helps.

    -Jason

  • ChaiScript Questions   5 days 17 hours ago

    Hello again.

    I want my users to be able to create types with chaiscript and for me to be able to retrieve the type information - mainly the attribute information.

    Is it in any way possible?

    Also is it at all possible to force a var or attribute to be of a certain type (strong typing)?

    Thanks
    Sebastian

  • ChaiScript Questions   5 days 18 hours ago

    I understand the explanation and I agree it should not pose a problem. After looking through the docs again I can see that there is no implicit access to members I don't explicitly register so it's OK. I was really worried about users gaining access to protected or private members but I see it can't be so.

  • ChaiScript Questions   6 days 1 hour ago

    No, this is not possible. In ChaiScript, technically, methods do not exist; only functions exist.

    So, a method call like:

    obj.somefunc();

    Is translated to:

    somefunc(obj);

    So, fundamentally, the user will always be allowed to add functions that can operate on your class. This means that the method sugaring we perform essentially means that all classes are always extendable, inside of script.

    Unless I miss your meaning. What affect are you going for? What is your use case?

  • ChaiScript Questions   6 days 1 hour ago

    Thanks for the bug report. I've fixed the problem in SVN. You were correct that the stack was not being properly initialized in the case of adding objects to the stack from C++ during multithreading.

  • ChaiScript Questions   6 days 5 hours ago

    First let me say thanks and Hello.

    My question is whether there is a way of registering a class with chaiscript that cannot be further extended by a user script?

    Thanks
    Sebastian

  • ChaiScript Questions   1 week 1 day ago

    It'd help if you could post all of your source files as a bug report to chaiscript.googlecode.com. This way I can try to reproduce the problem and see where the error is. I'll do my best to debug it this weekend, or maybe tomorrow afternoon.

    Thanks!

  • ChaiScript Questions   1 week 1 day ago

    any idea why is this happening ?
    and whay can i do to workaround it ?

  • ChaiScript Questions   2 weeks 1 day ago

    Im thinking that both my errors are caused by the same thing.
    (like some portion of the stack is not being initialize)

  • ChaiScript Questions   2 weeks 1 day ago

    OK having that previous problem with variables I tryed to implement my function via functor call.
    (so whole script is 1 function)

    fun(meta,dicom)
    {
      print(meta.srcAeTitle);
    }

    ProcessDES now looks like this

    	boost::call_once(&g_chaiInit,g_chaiInitOnce);
     
    	g_chai.functor<void(DcDirMetaInfo_ptr,DcElemList_ptr)>(des)(pMetaInfo,pElemList);

    however I'm getting an exception/error when running this (at least its not a crash)

    Unable to pop global stack

    any idea why ??

  • ChaiScript Questions   2 weeks 1 day ago

    I'm getting crash when trying to add a shared_ptr<> variable (first variable)
    to the chaiscript.

    it seems like its accessing non exisiting index or element.

          void add_object(const std::string &name, const Boxed_Value &obj)
          {
            StackData &stack = get_stack_data();
            validate_object_name(name);
            stack.get<0>().erase(name);
            stack.get<1>().back()[name] = obj;
          }

    it happens on last line of this method.

    this is the setup (processDES can be called from multiple threads )

    static chaiscript::ChaiScript g_chai;
    static boost::once_flag g_chaiInitOnce = BOOST_ONCE_INIT;
     
    void g_chaiInit()
    {
    	g_chai.add(chaiscript::fun(&parseHex),"hex");
    	g_chai.add(chaiscript::fun(&getTagValue),"getTag");
    	g_chai.add(chaiscript::fun(&setTagValue),"setTag");
    }
     
    void processDES(DcDirMetaInfo_ptr pMetaInfo,DcElemList_ptr pElemList,const string& des)
    {	
    	boost::call_once(&g_chaiInit,g_chaiInitOnce);
     
    	g_chai.add(chaiscript::var(pElemList),"dicom"); // This is the call that crashes in add_object
    	g_chai.add(chaiscript::var(pMetaInfo),"meta");
     
    	g_chai.eval(des);	
    }

    any ideas ?

  • ChaiScript Questions   2 weeks 4 days ago

    To my memory, there aren't yet ways to do number literals like you describe, though it shouldn't be too difficult to support them.

  • ChaiScript Questions   2 weeks 4 days ago

    In ChaiScript, we intentionally focused on flexibility over performance. Why? Let's think about how you might use a scripting language in a C++ application:

    You build up your C++ app, but realize parts should be runtime configurable. These types of things might be settings, business logic, or game scripts.

    These call back into the engine to fire off the events or set the values for you. The script itself is merely a set of calls back to the main application.

    In ChaiScript, we went one step further. The entire script is calls back to the ChaiScript engine and your application. Every addition, every step of the loop, is all calling back to ChaiScript. This makes it generally less performant, but in return you get the ability to augment what each script does to the nth degree.

    We chose to put less restrictions on the programmer. We could have tightened things down and prevented that level of modification, and for our efforts we might have gotten something of a speed increase, but in our opinion, if you're using ChaiScript for time critical things, you're using it wrong. Put the time critical sections in C++ where they will always work best, and fire them off from your script.

    Hope that helps.

  • ChaiScript Questions   2 weeks 5 days ago

    yet another question. Is there a standard math library ready to be included? For time to time functions like sin, cos, exp .... just come handy.
    And a related question, is there any way to specify 1.0e-6 as a double (without typing 0.000001) ?

  • ChaiScript Questions   2 weeks 6 days ago

    Hi there , I love ChaiScript! But, will it see some performance boosts in the future?
    Especially for-loops (C-style) seems to be very slow.

  • ChaiScript Questions   2 weeks 6 days ago

    Keep in mind that the above example is only necessary if you need to worry about choosing the specific version of an inherited member. If you are in that situation, you might do something like:

    Chai.add(fun<void (Class2 *, int)>(&Class2::Print1), "Print1");

    To solve that problem.

    If the member is not inherited, you could just do (the recommended / ideal form):

    Chai.add(fun(&Class2::Print1), "Print1");
  • ChaiScript Questions   2 weeks 6 days ago

    Hi,

    How does the above example work with the modification?

    	void Print1(int value){std::cout << value<<" Print from Class1" << std::endl;}

    It does not compile anymore after adding the parameter, showing the following error (MacOSX 10.6.4, gcc4.2 and gcc4.5):

    /opt/local/include/boost/function/function_template.hpp: In static member function ‘static void boost::detail::function::function_void_mem_invoker1<MemberPtr, R, T0>::invoke(boost::detail::function::function_buffer&, T0) [with MemberPtr = void (Class1::*)(int), R = void, T0 = Class2*]’:
    /opt/local/include/boost/function/function_template.hpp:913:   instantiated from ‘void boost::function1<R, T1>::assign_to(Functor) [with Functor = void (Class1::*)(int), R = void, T0 = Class2*]/opt/local/include/boost/function/function_template.hpp:722:   instantiated from ‘boost::function1<R, T1>::function1(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = void (Class1::*)(int), R = void, T0 = Class2*]/opt/local/include/boost/function/function_template.hpp:1064:   instantiated from ‘boost::function<R ()(T0)>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = void (Class1::*)(int), R = void, T0 = Class2*]’
    test_inherit.cpp:31:   instantiated from here
    /opt/local/include/boost/function/function_template.hpp:225: error: no match for call to ‘(boost::_mfi::mf1<void, Class1, int>) (Class2*&)/opt/local/include/boost/bind/mem_fn_template.hpp:163: note: candidates are: R boost::_mfi::mf1<R, T, A1>::operator()(T*, A1) const [with R = void, T = Class1, A1 = int]
    /opt/local/include/boost/bind/mem_fn_template.hpp:184: note:                 R boost::_mfi::mf1<R, T, A1>::operator()(T&, A1) const [with R = void, T = Class1, A1 = int]
  • ChaiScript Questions   3 weeks 1 day ago

    oh, such a stupid typo -_- facepalm.jpg. Thanks.

  • ChaiScript Questions   3 weeks 2 days ago

    It would appear that you have a typo in your main.chai:

    var x = LoadAnimMesh("C:\man.b3d")

    should be:

    var x = LoadAnimMesh("C:\\man.b3d")

    Normal C++ escaping rules apply.

  • ChaiScript Questions   3 weeks 4 days ago

    Hi there! I like ChaiScript - it's really comfortable way to bind functions in scripts. But I have trouble with binding:
    I'm trying to bind function (Irrlicht engine):

    In code:

    IAnimatedMesh* LoadAnimMesh(io::path path)
    {
    return smgr->getMesh(path);
    }

    In main():

    ChaiScript chai;
    chai.add(fun(&LoadAnimMesh),"LoadAnimMesh");
    chai.eval_file("C:\\main.chai");

    In main.chai:

    var x = LoadAnimMesh("C:\man.b3d")

    But, when I tried to run, I'm received unknown exception. Please, tell me, where the problem can be.
    Thanks in advance.

  • ChaiScript Questions   12 weeks 10 hours ago

    We don't have any support for operator() in chaiscript, which is an interesting point that I will make a note of.

    You would want to do something like:

    chai.add(fun(&PSignal<void ()>::operator(), "emit"));.

    Then in your chaiscript you could emit the signal explicitly:

    mysignal.emit()

    I'll look more closely at your example as soon as I get more time and see if I cannot make any more suggestions.

    I've been considering a set of optional libraries that ship with chaiscript, like the stl extra that is currently there. boost::regex and filesystem were also considerations. boost::signal might be a good one too. You're showing that it could be a great way of hooking together c++ and chaiscript.

    Regarding compile time, just wait :).

    In all seriousness, 95% of what we need to do cannot really be done in compiled library code, but there are some techniques for making recompiles of your application faster. I should publish some docs on that. The gist of it is that you want to include ChaiScript in your main.cpp, then have a mylib.cpp that creates and returns a chaiscript::module object. Your this way, main.cpp (and the core of chaiscript) only needs to be recompiled rarely, and you make all of your changes inside your mylib.cpp.

    -Jason

  • ChaiScript Questions   12 weeks 1 day ago

    ok i cleaned it up a bit.
    http://zoadian.pastebin.com/TX6MaPYt

    any idea how to call the signal from within chaiscript?

    in c++ it would be

      PSignal<void ()> sigUpdate;
      sigUpdate(); //call all connected slots. how to call it in chai?

    post it as a example whereever you like ;)

    my wish for next chai version: reduce compiletime (sourcefiles/lib)

    thx, suicide

Syndicate content