Squirrel as alt scripting language
#11
Geeze.. finally!!

main.cpp
Code:
#include "cMCLogger.h"

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <sqplus/sqplus.h>
#include <sqplus/SquirrelObject.h>


class MyCppClass
{
public:
    static MyCppClass* s_Singleton;
    int classVal;
    MyCppClass()
        : classVal(123)
    {
        LOGINFO("Created new class");
        s_Singleton = this;
    }
    virtual bool process(int iVal,const SQChar * sVal)
    {
        LOGINFO("classVal: %d, iVal: %d, sVal %s\n",classVal,iVal,sVal);
        classVal += iVal;
        return iVal > 1;
    }
    virtual void lulwut() = 0;
};
MyCppClass* MyCppClass::s_Singleton = 0;

extern bool IsTopClosure( HSQUIRRELVM v );

class MyCppClass__Squirrel : public MyCppClass
{
public:
    MyCppClass__Squirrel()
    {
    }
    virtual void lulwut()
    {
        if( !IsTopClosure(vm) ) // Avoid recursion
        {    //Called from C++
            SqPlus::SquirrelFunction<void> startFunc = SqPlus::SquirrelFunction<void>(obj, "lulwut");
            startFunc();
        }
        else // Called from Squirrel
        {
            //MyCppClass::lulwut(); // Can't do this, it's a pure virtual
        }
    }
    virtual bool process(int iVal,const SQChar * sVal)
    {
        return MyCppClass::process(iVal, sVal);
    }

    static int constructor(HSQUIRRELVM v)
    {
        LOG("construct__MyCppClass__Squirrel()");

        MyCppClass__Squirrel* newClass = new MyCppClass__Squirrel();

        StackHandler sa(v);
        HSQOBJECT ho = sa.GetObjectHandle(1); // OT_INSTANCE
        SquirrelObject instance(ho);
        SqPlus::PopulateAncestry(v, instance, newClass);
        newClass->SetInstance( instance, v );

        sq_setinstanceup(v, 1, newClass);
        sq_setreleasehook(v, 1, SqPlus::ReleaseClassPtr<MyCppClass__Squirrel>::release);
        return TRUE;
    };

    void SetInstance( SquirrelObject & a_Inst, HSQUIRRELVM & a_VM ) { obj = a_Inst; vm = a_VM; }

    HSQUIRRELVM vm;
    SquirrelObject obj;
};

static void printFunc(HSQUIRRELVM v,const SQChar * s,...)
{
    va_list vl;
    va_start(vl,s);
    cMCLogger::GetInstance()->Log( s, vl );
    va_end(vl);
}

int main( int argc, char **argv )
{
    new cMCLogger();

    SquirrelVM::Init();
    sq_setprintfunc(SquirrelVM::GetVMPtr(),printFunc,printFunc);

    SqPlus::SQClassDef<MyCppClass__Squirrel>( "MyCppClass" ).
        staticFunc(&MyCppClass__Squirrel::constructor, "constructor").
        func(&MyCppClass__Squirrel::process, "process").
        func(&MyCppClass__Squirrel::lulwut, "lulwut").
        var(&MyCppClass__Squirrel::classVal, "classVal");

    try
    {
        SquirrelObject helloSqPlus = SquirrelVM::CompileScript( "squirreltest.nut" );
        try
        {
            SquirrelVM::RunScript(helloSqPlus);
            LOGWARN("About to call function from C++");
            MyCppClass::s_Singleton->lulwut();
        }
        catch (SquirrelError & e)
        {
            printf("Error: %s, %s\n", e.desc, "Squirrel::helloSqPlus");
        }
    }
    catch (SquirrelError & e)
    {
        printf("Error: %s, %s\n", e.desc, "Compiling");
    }

    getchar();

    SquirrelVM::Shutdown();
    return 0;
}

squirreltest.nut
Code:
class TestClass extends MyCppClass
{
    function print(msg)
    {
        ::print(msg);
    }
    function process(iVal, sVal)
    {
        print("Printed from Squirrel");
        base.process(iVal, sVal);
    }

    function lulwut()
    {
        print("Lulwut from Squirrel!");
    }
}

myClass <- TestClass();

local rVal = myClass.process(1, "MyClass1");

print("Returned: " + ( rVal ? "true" : "false") );

rVal = myClass.process(2, "MyClass2");

print("Returned: " + (rVal ? "true" : "false") );
print("classVal: " + myClass.classVal );

FINALLY produces this:
[Image: 30801124583-orig.png]


bedtime now ...
Reply
Thanks given by:


Messages In This Thread
Squirrel as alt scripting language - by Kwen - 11-05-2011, 02:21 AM
RE: Squirrel as alt scripting language - by rs2k - 11-05-2011, 02:26 AM
RE: Squirrel as alt scripting language - by Kwen - 11-05-2011, 09:49 AM
RE: Squirrel as alt scripting language - by rs2k - 11-05-2011, 09:49 AM
RE: Squirrel as alt scripting language - by rs2k - 11-05-2011, 09:59 AM
RE: Squirrel as alt scripting language - by FakeTruth - 11-05-2011, 03:38 PM
RE: Squirrel as alt scripting language - by Kwen - 11-05-2011, 04:01 PM
RE: Squirrel as alt scripting language - by rs2k - 11-07-2011, 10:59 AM



Users browsing this thread: 1 Guest(s)