Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ NEW: Do you know C++ Script Libraries for extending C++ applications ?

Do you know C++ Script Libraries for extending C++ applications dynamically during runtime ?


In this article you will find a brief overview about existing scripting libraries embeddable in C++ applications:
Comparison of C++ Script Libraries.

The article is good for get a nice high-level overview as a starting point.
The reader may then dive deeper into a specific script language / library via the provided links.

(for the admins: please, delete the other old thread. It has a broken link. This one is correct.)
 
Interesting. However the only example I find here is running a script to generate Fibonacci numbers which seems rather too trivial. Nobody would ever execute a script for something you can easily do within the language. Can you give an example where executing a script is really essential, and why in that case kicking off something like system("sh -x myscript.sh") is not good enough ?
 
Interesting. However the only example I find here is running a script to generate Fibonacci numbers which seems rather too trivial. Nobody would ever execute a script for something you can easily do within the language. Can you give an example where executing a script is really essential, and why in that case kicking off something like system("sh -x myscript.sh") is not good enough ?
Where do you found "this only example"?

Take for example the "file io test" included in the download bundle and also readable on the download page:
Download page for TeaScript

If you read about the capabilities of TeaScript (all also observable via the comparison article above) then you can see what you could do with it already.
Most impressive highlights:
Overview and Highlights

Language Documentation:
Language Documentation

Examples of the C++ API usage is available in the download bundle, on the overview page as well as on github.
You only need to follow the links.
What you can do with TeaScript is up to the developer who integrate the Library in their application and / or who are programming TeaScript standalone files for execute arbitrary tasks. The actual limits of standalone TeaScript files are from the integrated core library which is not finished for file io support. network io will be added as well. Used in C++ you could nearly do everything already via arbitrary callback functions.

The Keypoint of TeaScript for C++ is that you don't need to change the compiled C++ application (once it is integrated). You can extend your application with arbitrary scripting tasks which might be different for each installation of the application.
I can do anything in C++. But only with compile and redistribute.
TeaScript is for customization points in already compiled and distributed applications.
 
Last edited:
Ah ok, sorry. Seems I had not really looked well past that Fibonacci example 😳
My question remains though - why not just spawn a bash/perl/python/node.js/powershell script ? Are there specific and essential things teascript can do that these interpreters cannot ?
 
Ah ok, sorry. Seems I had not really looked well past that Fibonacci example 😳
My question remains though - why not just spawn a bash/perl/python/node.js/powershell script ? Are there specific and essential things teascript can do that these interpreters cannot ?

No problem, maybe next time I will introduce the TeaScript C++ Library directly here with all information included :laugh: ;)

Yes, there is a lot of advantage instead of spawning bash or whatever script.
If you use the TeaScript C++ Library you can access and manipulate the variables before or after execution, or build an environment for the script exclusively for your application.
Also, you can register callback functions to your C++ code or access functions directly provided by the script.
Furthermore, you don't need to parse the output of the script, you get the result as C++ Object / C++ Type.

Here is a snippet from the teascript_demo.cpp.
You always must keep in mind, that the demo code is just for illustration. In real C++ apps you can do more complex and/or more dynamic things, e.g. not use hard coded strings but dynamic strings from file or elsewhere and also use dynamic set values.

C++:
// TeaScript includes
#include "teascript/version.h"
#include "teascript/Engine.hpp"
 
//(std includes missing...)
 
// This test code will add some variables (mutable and const) to the script context
// and then execute script code, which will use them.
void test_code1()
{
    // create the TeaScript default engine.
    teascript::Engine  engine;
 
    // add 2 integer variables a and b.
    engine.AddVar( "a", 2 );   // variable a is mutable with value 2
    engine.AddVar( "b", 3 );   // variable b is mutable with value 3
    engine.AddConst( "hello", "Hello, World!" );    // variable hello is a const string.
    // For Bool exists different named methods because many types could be implicit converted to bool by accident.
    // like char * for example.
    engine.AddBoolVar( "speak", true );   // variable speak is a Bool with value true.
    
 
    // execute the script code passed as string. it computes new variable c based on values a and b.
    // also it will print the content of the hello variable.
    // finally it returns the value of variable c.
    auto const res = engine.ExecuteCode(    "const c := a + b\n"
                                            "if( speak ) {\n"
                                            "    println( hello )\n"
                                            "}\n"
                                            "c\n"
                                        );
    // print the result.
    std::cout << "c is " << res.GetAsInteger() << std::endl;
}
 
 
// this is our simple callback function which we will call from TeaScript code.
// The callback function signature is always ValueObject (*)( Context & )
teascript::ValueObject user_callback( teascript::Context &rContext )
{
    // look up variable 'some_var'
    auto const val = rContext.FindValueObject( "some_var" );
 
    // print a message and the value of 'some_var'
    std::cout << "Hello from user_callback! some_var = " << val.PrintValue() << std::endl;
 
    // return 'nothing' aka NaV (Not A Value).
    return {};
}
 
// This test code will register a C++ callback function and then executes a script
// which will call it.
void test_code2()
{
    // create the TeaScript default engine.
    teascript::Engine  engine;
 
    // register a function as a callback. can use arbitrary names.
    engine.RegisterUserCallback( "call_me", user_callback );
 
    // execute the script which will create variable 'some_var' and then call our callback function.
    engine.ExecuteCode( "const some_var := \"Hello!\"\n"
                        "if( is_defined call_me and call_me is Function ) { // safety checks! \n"
                        "    call_me( )\n"
                        "}\n"               
                      );
}
 
 
// This is another callback function.
// It will create the sum of 2 passed parameters and return the result to the script.
teascript::ValueObject calc_sum( teascript::Context &rContext )
{
    if( rContext.CurrentParamCount() != 2 ) { // this check could be relaxed also...
        // in case of wrong parameter count, throw eval error with current source code location.
        throw teascript::exception::eval_error( rContext.GetCurrentSourceLocation(), "Calling calc_sum: Wrong amount of parameters! Expecting 2.");
    }
 
    // get the 2 operands for the calculation.
    auto const lhs = rContext.ConsumeParam();
    auto const rhs = rContext.ConsumeParam();
 
    // calculate the sum and return result as ValueObject.
    return teascript::ValueObject( lhs.GetAsInteger() + rhs.GetAsInteger() );
}
 
// This test code will register a callback function and call it from a script with parameters.
void test_code3()
{
    // create the TeaScript default engine.
    teascript::Engine  engine;
 
    // register a function as a callback. can use arbitrary names.
    engine.RegisterUserCallback( "sum", calc_sum );
 
    // execute the script which will call our function
    auto const res = engine.ExecuteCode( "sum( 1234, 4321 )\n" );
 
    // print the result.
    std::cout << "res is " << res.GetAsInteger() << std::endl;
}

Illustrating how to execute TeaScript files from disk:

C++:
// std includes
#include <iostream>
#include <filesystem>

// TeaScript includes
#include "teascript/Engine.hpp"

// somewhere in the code...
// create a TeaScript default engine (e.g. in the main() function).
teascript::Engine  engine;

// and execute the script file.
auto const res = engine.ExecuteScript( std::filesystem::path( "/path/to/some/script.tea" ) );

// print the result...
std::cout << "result: " << res.PrintValue() << std::endl;
 
Impressive ! As is the presentation and the comparison. Now that I've read a bit more I see that using scripting libraries from C++ is not as way out as I thought, so... sorry for appearing a bit skeptical to start with. Actually it seems a somewhat crowded niche already. I wish you all best in making this a commercial success.
 
Impressive ! As is the presentation and the comparison. Now that I've read a bit more I see that using scripting libraries from C++ is not as way out as I thought, so... sorry for appearing a bit skeptical to start with. Actually it seems a somewhat crowded niche already. I wish you all best in making this a commercial success.

Thank you very much for your wishes! :)

Just for clarifications to other readers: The TeaScript C++ Library is usable for "home brewed" (personal/family) things for free as well as for other things which are noted in the license.
The TeaScript Host Application can be even used for any commercial stuff if the conditions of the specific license are held.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom