mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2025-12-06 08:46:53 +08:00
114 lines
4.7 KiB
Markdown
114 lines
4.7 KiB
Markdown
<a href="https://www.patreon.com/bePatron?u=2977989&redirect_uri=https%3A%2F%2Fwww.patreon.com%2Flefticus">
|
|
<img height="40" width="204" src="https://s3-us-west-1.amazonaws.com/widget-images/become-patron-widget-medium%402x.png">
|
|
</a>
|
|
|
|
|
|
Master Status: [](https://travis-ci.org/ChaiScript/ChaiScript) [](https://ci.appveyor.com/project/lefticus/chaiscript) [](http://codecov.io/github/ChaiScript/ChaiScript?branch=master)
|
|
|
|
Develop Status: [](https://travis-ci.org/ChaiScript/ChaiScript) [](https://ci.appveyor.com/project/lefticus/chaiscript/branch/develop) [](http://codecov.io/github/ChaiScript/ChaiScript?branch=develop)
|
|
|
|
ChaiScript
|
|
|
|
http://www.chaiscript.com
|
|
|
|
(c) 2009-2012 Jonathan Turner
|
|
(c) 2009-2017 Jason Turner
|
|
|
|
Release under the BSD license, see "license.txt" for details.
|
|
|
|
|
|
Introduction
|
|
============
|
|
|
|
[](https://gitter.im/ChaiScript/ChaiScript?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
|
|
|
ChaiScript is one of the only embedded scripting language designed from the
|
|
ground up to directly target C++ and take advantage of modern C++ development
|
|
techniques, working with the developer how they would expect it to work. Being a
|
|
native C++ application, it has some advantages over existing embedded scripting
|
|
languages:
|
|
|
|
1. It uses a header-only approach, which makes it easy to integrate with
|
|
existing projects.
|
|
2. It maintains type safety between your C++ application and the user scripts.
|
|
3. It supports a variety of C++ techniques including callbacks, overloaded
|
|
functions, class methods, and stl containers.
|
|
|
|
|
|
Requirements
|
|
============
|
|
|
|
ChaiScript requires a C++17 compiler to build with support for variadic
|
|
templates. It has been tested with gcc 7 and clang 6 (with libcxx).
|
|
|
|
Installation using vcpkg
|
|
========================
|
|
|
|
You can download and install ChaiScript using the [vcpkg](https://github.com/Microsoft/vcpkg/) dependency manager:
|
|
|
|
git clone https://github.com/Microsoft/vcpkg.git
|
|
cd vcpkg
|
|
./bootstrap-vcpkg.sh
|
|
./vcpkg integrate install
|
|
vcpkg install chaiscript
|
|
|
|
The ChaiScript port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.
|
|
|
|
Usage
|
|
=====
|
|
|
|
* Add the ChaiScript include directory to your project's header search path
|
|
* Add `#include <chaiscript/chaiscript.hpp>` to your source file
|
|
* Instantiate the ChaiScript engine in your application. For example, create a
|
|
new engine with the name `chai` like so: `chaiscript::ChaiScript chai`
|
|
* The default behavior is to load the ChaiScript standard library from a
|
|
loadable module. A second option is to compile the library into your code,
|
|
see below for an example.
|
|
|
|
Once instantiated, the engine is ready to start running ChaiScript source. You
|
|
have two main options for processing ChaiScript source: a line at a time using
|
|
`chai.eval(string)` and a file at a time using `chai.eval_file(fname)`
|
|
|
|
To make functions in your C++ code visible to scripts, they must be registered
|
|
with the scripting engine. To do so, call add:
|
|
|
|
chai.add(chaiscript::fun(&my_function), "my_function_name");
|
|
|
|
Once registered the function will be visible to scripts as "my_function_name"
|
|
|
|
Examples
|
|
========
|
|
|
|
ChaiScript is similar to ECMAScript (aka JavaScript(tm)), but with some
|
|
modifications to make it easier to use. For usage examples see the "samples"
|
|
directory, and for more in-depth look at the language, the unit tests in the
|
|
"unittests" directory cover the most ground.
|
|
|
|
For examples of how to register parts of your C++ application, see
|
|
"example.cpp" in the "samples" directory. Example.cpp is verbose and shows every
|
|
possible way of working with the library. For further documentation generate
|
|
the doxygen documentation in the build folder or see the website
|
|
http://www.chaiscript.com.
|
|
|
|
|
|
The shortest complete example possible follows:
|
|
|
|
```C++
|
|
/// main.cpp
|
|
|
|
#include <chaiscript/chaiscript.hpp>
|
|
|
|
double function(int i, double j)
|
|
{
|
|
return i * j;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
chaiscript::ChaiScript chai;
|
|
chai.add(chaiscript::fun(&function), "function");
|
|
|
|
double d = chai.eval<double>("function(3, 4.75);");
|
|
}
|
|
```
|