Squirrel

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

_cloned metamethod

Last post 08-14-2008, 1:20 PM by cya. 3 replies.
Sort Posts: Previous Next
  •  08-13-2008, 7:11 AM 2698

    _cloned metamethod

    I need to clone datatypes in script that are created on c++ side, but I get segmentation faults (the same happens with squirrel-builtin types like blob).
    Even if I register a '_cloned'- metamethod, it doesn't get called if an instance is cloned.

    Any help would be appreciated (pure squirrel, no sqplus).
  •  08-14-2008, 12:51 PM 2702 in reply to 2698

    Re: _cloned metamethod

    The question is still open. I can provide a 'copy'
    method, but that won't help if I clone tables.

    This would work:

    local image = Image("test.png")

    local image2 = image.copy()

    image2.blit(x, y)

    while this would not:

    local image3 = clone image

    image3.blit(x,y) // <--crash

    Therefore something like this will fail:

    local a = {}

    a.a <- Image("test.png")

    local b = clone a

    b.a.blit(20, 30) // <--crash
  •  08-14-2008, 12:53 PM 2703 in reply to 2698

    Re: _cloned metamethod

    blobs cannot be cloned, as the implementation doesn't implement _cloned (my bad I'll fix that).

    this is what cloned for the blob should look like

    static SQInteger _blob__cloned(HSQUIRRELVM v)
    {
     SQBlob *other = NULL;
     {
      if(SQ_FAILED(sq_getinstanceup(v,2,(SQUserPointer*)&other,(SQUserPointer)SQSTD_BLOB_TYPE_TAG))) 
       return SQ_ERROR;
     }
     SQBlob *thisone = new SQBlob(other->Len());
     memcpy(thisone->GetBuf(),other->GetBuf(),thisone->Len());
     if(SQ_FAILED(sq_setinstanceup(v,1,thisone))) {
      delete thisone;
      return sq_throwerror(v, _SC("cannot clone blob"));
     }
     sq_setreleasehook(v,1,_blob_releasehook);
     return 0;
    }

    I hope this helps

    Alberto

     

  •  08-14-2008, 1:20 PM 2704 in reply to 2703

    Re: _cloned metamethod

    Thanks, that really helped.
    The documentation has room for improvement (you probably know that for yourself). I actually look more into the squirrel source code instead.
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems