Squirrel

The programming language
Welcome to Squirrel Sign in | Join | Help
in Search

Attach user data to table

Last post 06-22-2009, 10:26 PM by fagiano. 5 replies.
Sort Posts: Previous Next
  •  06-09-2009, 3:45 AM 3299

    Attach user data to table

    Hello! Is it posible to attach my data to Squirrel table?
    Here is the C++ code for piece creation.

    class Piece
    {
    public:
        HSQOBJECT mScriptObject;
    };

    int createPiece(HSQUIRRELVM v)
    {
        Piece* piece = new Piece();

        sq_newtable(v); //create table
    //set the table's data to piece
        sq_pushstring(v, "ID", -1);
        sq_pushinteger(v, 2);
        sq_newslot(v, -3, SQFalse);

        sq_resetobject(&piece->mScriptObject); //initialize the handle
        sq_getstackobj(v,-1,&piece->mScriptObject); //retrieve an object handle from the pos –2
        sq_addref(v,&piece->mScriptObject); //adds a reference to the object

        return 1;
    }

    And to delete piece, I use this:

    int bgwDestroyPiece(HSQUIRRELVM v)
    {
        Piece* piece;
    //get user data of table

        sq_release(v,&piece->mScriptObject); //relese the object
        delete piece;

        return 0;
    }

    Here is how it looks in Squirrel scripts:

    local piece = createPiece();
    piece.posX <- 10;
    destroyPiece(piece);

    I don't want user to be able to change the user data of the table and I need it to be table, so that script can add slots to table like this: piece.posX <- 10. Is it possible with Squirrel?
    Thanks in advance!

  •  06-09-2009, 7:27 AM 3300 in reply to 3299

    Re: Attach user data to table

    If it is not possible to attach user data to table, I can create a slot like this:

    sq_pushstring(v, "userData", -1);
    sq_pushuserpointer(v, piece);
    sq_newslot(v, -3, SQFalse);

    But if user calls this in script, it crashes the program:
    piece.userData = 0;

    Is it possible to protect table's slots somehow, so that they are not accessible from script?
  •  06-10-2009, 6:29 AM 3301 in reply to 3300

    Re: Attach user data to table

    Ok, I see, that squirrel supports constants (like this: const variable = value). Is it possible to create constant from C++? I think I could save the user data in constant, so that script wouldn't be able to change it.
  •  06-18-2009, 9:07 AM 3316 in reply to 3301

    Re: Attach user data to table

    It is possible to set foreign pointer for Virtual Machine with sq_setforeignptr. Is there something similar for HSQOBJECT?
    Can somebody please answer me?
  •  06-19-2009, 8:32 AM 3318 in reply to 3316

    Re: Attach user data to table

    Is it possible for me tu suggest a patch?
    I would like to add void *_foreignptr; to HSQOBJECT (like in SQVM).
    And add these functions to sqapi.h:

    void sq_setforeignptr(HSQOBJECT o,SQUserPointer p)
    {
        o->_foreignptr = p;
    }

    SQUserPointer sq_getforeignptr(HSQOBJECT o)
    {
        return o->_foreignptr;
    }

  •  06-22-2009, 10:26 PM 3325 in reply to 3318

    Re: Attach user data to table

    have you considered using a class instead?
    class instances can have userdata attached.

    Alberto
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems