[angelscript] 1.9.0 WIP 3

  • From: "Andreas Jonsson" <andreas@xxxxxxxxxxxxx>
  • To: "angelscript" <angelscript@xxxxxxxxxxxxx>
  • Date: Sun, 29 Aug 2004 12:04:51 -0300

I've uploaded WIP 3 now.
This version adds dynamic binding of functions between modules, which allow 
functions to call into other modules.

The following code shows how the import can be done (can also be found in the 
test framework):


//
// Tests importing functions from other modules
//
// Test author: Andreas Jonsson
//

#include "angelscript.h"
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <string>

namespace TestImport
{

#define TESTNAME "TestImport"


static std::string output;

class COutStream : public asIOutputStream
{
public:
        void Write(const char *text) { printf(text); /*output += text;*/ }
};


static const char *script1 =
"import void Test() from \"DynamicModule\";   \n"
"void main()                                  \n"
"{                                            \n"
"  Test();                                    \n"
"}                                            \n";

static const char *script2 =
"void Test()            \n"
"{                      \n"
"  number = 1234567890; \n"
"}                      \n";

bool Test()
{
        bool fail = false;

        int number = 0;

        asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
        engine->RegisterGlobalProperty("int number", &number);

        COutStream out;
        engine->AddScriptSection(0, TESTNAME ":1", script1, strlen(script1));
        engine->Build(0, &out);

        engine->AddScriptSection("DynamicModule", TESTNAME ":2", script2, 
strlen(script2));
        engine->Build("DynamicModule", &out);

        int module1 = engine->GetModuleID(0);

        // Bind imported functions
        int c = engine->GetImportedFunctionCount(0);
        for( int n = 0; n < c; ++n )
        {
                char buffer[256];
                engine->GetImportedFunctionDeclaration(module1 + n, buffer, 
256);

                // Get module name from where the function should be imported
                const char *moduleName = 
engine->GetImportedFunctionSourceModule(module1 + n);

                int funcID = engine->GetFunctionIDByDecl(moduleName, buffer);
                engine->BindImportedFunction(module1 + n, funcID);
        }

        engine->ExecuteString(0, "main()", &out, 0);

        engine->Release();

        if( number != 1234567890 )
        {
                printf("%s: Failed to set the number as expected\n", TESTNAME);
                fail = true;
        }

        // Success
        return fail;
}

} // namespace



I'm however not satisfied with how the asIScriptEngine interface has become so 
bloated with the latest additions. I will therefore break out the module 
interface, which will make it easier to work with.

Regards,
Andreas Jönsson
Author of AngelScript


AngelScript - AngelCode Scripting Library
http://www.angelcode.com/angelscript/
http://www.angelcode.com/forums/

Other related posts:

  • » [angelscript] 1.9.0 WIP 3