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)

get function object from chaiscript to c++

Hi,i want to get function object in c++ and call later the function.

chaiscript code:

def fun(x)
{
    return x*x;    
}
var f = fun(x);
sendFun(f);

c++ code:

void sendFun(ChaiScript_Language::Function f)
{
    f.call();
}

Thanks for help.
PS: i try to include #include but i have some errors :
E:\chaiscript-3.1.0-Source\include\chaiscript\language\chaiscript_prelude_docs.hpp|27|error: 'Type_Info' does not name a type|
E:\chaiscript-3.1.0-Source\include\chaiscript\language\chaiscript_prelude_docs.hpp|30|error: 'string' has not been declared|
...

I'm trying to do this : attr

I'm trying to do this :

attr myclass::value;
def myclass::myclass()
{
    this.value = 5;
}
def myclass::function(x)
{
    this.value+=x;
}
var obj = myclass();
sendfun(obj.function());

What parameter must have sendfun ?

Second question :

dofun(obj.function);
def doFun(x)
{
   x(); // how call a function ? must have obj ref ?
}
 
dofun(myclass::function,obj);
def doFun2(f,o)
{
    o.f(); // ?
}

Thanks for help.

If you want a bound member

If you want a bound member function, your example might look like:

attr myclass::value;
def myclass::myclass()
{
    this.value = 5;
}
def myclass::function(x)
{
    this.value+=x;
}
var obj = myclass();
sendfun(bind(function, obj, _));

The result of "bind" in the above example is a function that takes one parameter and calls: obj.function(parameter).

Similarly, for your second example, you might use bind, or you might just pass both parameters into your function, depending on your need:

dofun(obj, function);
def doFun(obj, function)
{
   obj.function();
}
 

-Jason

Certainly, a complete example

Certainly, a complete example of what you want to do is in the unit test suite: https://github.com/ChaiScript/ChaiScript/blob/master/unittests/functor_c....

For completeness, I'll copy it here and simplify it for your case. Also, please note that "fun" is a reserved word in ChaiScript.

The best way to do what you want is to specify the exact parameters you want the function to take and return in C++ using a boost::function wrapper (or std::function in C++11).

#include <chaiscript/chaiscript.hpp>
 
double send_fun(const boost::function<double (double)> &f)
{
  // Call the ChaiScript function and return the result
  return f(12);
}
 
int main()
{
 
  chaiscript::ChaiScript chai;
 
  chai.add(chaiscript::fun(&send_fun), "send_fun");
 
  // Create the ChaiScript Function
  chai.eval("def func(i) { return i * i; };");
 
  // Pass the ChaiScript Function to our C++ Function and
  // automatically extract the result.
  double d = chai.eval<double>("send_fun(func)");
 
  // d should now be equal to 144
}

There are more generic methods but they are generally not recommended for end users. What do you think of this?

Thanks,this is exactly what i

Thanks,this is exactly what i wanted !

Support ChaiScript by sharing it with your friends: