0% found this document useful (0 votes)
15 views12 pages

Crazy Easy Scripting With ChaiScript - Jason Turner - CppCon 2015

Uploaded by

alan88w
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views12 pages

Crazy Easy Scripting With ChaiScript - Jason Turner - CppCon 2015

Uploaded by

alan88w
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Crazy Easy Scripting with ChaiScript

Copyright 2015 Jason Turner @lefticus 1 / 12


Jason Turner
http://chaiscript.com
http://cppcast.com
http://cppbestpractices.com
http://github.com/lefticus
Independent Contractor

Copyright 2015 Jason Turner @lefticus 2 / 12


ChaiScript
Header only
0 external dependencies
Mature: 6 years old
Stable: every release is tested against
clang / MSVC / g++
MacOS / Windows / Linux
64bit / 32bit
AddressSanitizer / ThreadSanitizer
MSVC's Static Analyzer / cppcheck
Thread safe by default (20% perf boost if disabled)
Crazy easy to use

Copyright 2015 Jason Turner @lefticus 3 / 12


ChaiScript History
Developed with SWIG but the build process overhead was frustrating for small projects
At the same time I started wondering what it would take to do multimethod dispatch
with C++
And my cousin, a language geek, was interested in working on a language with me.
Then ChaiScript was born. First as a toolkit to build your own scripting language with
ChaiScript being just one reference implementation.
You can still see some of this in the file layout with the dispatchkit and language sub folders.

Copyright 2015 Jason Turner @lefticus 4 / 12


ChaiScript Goals
Syntax feel natural to C++ developers
Seemless integration with C++
Not get in the way
No pre-processor or complex build process
Be "fast enough" (~1.5M C++ callbacks / second possible currently)

Copyright 2015 Jason Turner @lefticus 5 / 12


ChaiScript is Not as Fast as Lua
But it doesn't need to be
If you need performance, simply call into C++

Copyright 2015 Jason Turner @lefticus 6 / 12


Basics
#include <chaiscript/chaiscript.hpp>

int main()
{
chaiscript::ChaiScript chai;

chai.eval(R"(
print("Hello ChaiScript")
)");
}

Copyright 2015 Jason Turner @lefticus 7 / 12


Adding a Function
#include <chaiscript/chaiscript.hpp>
#include <iostream>

void say_hi()
{
std::cout << "Hello C++\n";
}

int main()
{
chaiscript::ChaiScript chai;

chai.add(chaiscript::fun(&say_hi), "say_hi");

chai.eval(R"(
say_hi();
)");
}

Copyright 2015 Jason Turner @lefticus 8 / 12


Sharing a Value
#include <chaiscript/chaiscript.hpp>

int main()
{
chaiscript::ChaiScript chai;

int &i = chai.eval<int &>(R"(


var i = 5;
i;
)");

i = 20;

chai.eval(R"(
print(i); // prints 20
)");
}

Copyright 2015 Jason Turner @lefticus 9 / 12


Passing a Function
#include <chaiscript/chaiscript.hpp>

void do_something(const std::function<double (double, double)> &f)


{
std::cout << "Calculated: " << f(2.6, 3.5) << '\n';
}

int main()
{
chaiscript::ChaiScript chai;
chai.add(chaiscript::fun(&do_something), "do_something");

chai.eval(R"(
do_something(`+`); // prints "Calculated 6.1"
do_something(`-`); // prints "Calculated -.9"
do_something(`*`); // prints "Calculated 9.1"
do_something(fun(x, y) { x + y / 3 + y} ); // prints "Calculated 7.2666667"
)");
}

Copyright 2015 Jason Turner @lefticus 10 / 12


ChaiScript as a Configuration
Language
Why use an INI?

Copyright 2015 Jason Turner @lefticus 11 / 12


Jason Turner
http://chaiscript.com
http://cppcast.com
http://cppbestpractices.com
http://github.com/lefticus
@chaiscript
@lefticus
Independent Contractor - Always interested in meeting new clients

Copyright 2015 Jason Turner @lefticus 12 / 12

You might also like