YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Returning instance error, when function has arguments
matty
#1 Posted : Friday, June 23, 2017 3:26:29 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 6/12/2017(UTC)
Posts: 8

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
I have these two functions that do the exact same thing, they both return instances of an exposed C/C++ class.
However, one of the functions takes arguments and fails to return an instance.

Code:

static SQRegFunction reg[] = {
    { "getData", wrap_getData, 2, _SC("ts") }, // this function fails
    { "getDataTest", wrap_getDataTest, NULL, NULL },
    { 0, 0 }
};


I suspect this is due to "wrap_getData" requiring arguments. When looking at DXSquirrel (specifically DXSquirrel_Device.cpp, as it is a similar case) but nothing special is done about it.

Code:

// this function throws a runtime error
int wrap_getData(SQVM* v) {
    const char* arg0;
    sq_getstring(v, -1, &arg0);

    auto instance = new Data;

    sq_pushstring(v, "Data", -1);
    if (SQ_FAILED(sq_get(v, -2))) {
        // failure here.
        return sq_throwerror(v, "Couldn't resolve squirrel type 'Data'");
    }
        
    if (SQ_FAILED(sq_createinstance(v, 1)) || SQ_FAILED(sq_setinstanceup(v, 1, instance))) {
        return sq_throwerror(v, "Couldn't setup squirrel instance for object type 'Data'");
    }
   
    sq_setreleasehook(v, 1, release_hook<Data>);
    sq_remove(v, -2); // remove object name
   
    return 1;
}


// This function works
int wrap_getDataTest(SQVM* v) {
    sq_pushstring(v, "Data", -1);
    if (SQ_FAILED(sq_get(v, -2))) {
        throw sq_throwerror(v, "Couldn't resolve squirrel type 'Data'");
    }

    auto instance = new Data;
    if (SQ_FAILED(sq_createinstance(v, -1)) || SQ_FAILED(sq_setinstanceup(v, -1, instance))) {
        throw sq_throwerror(v, "Couldn't setup squirrel instance for object type 'Data'");
    }

    sq_setreleasehook(v, -1, release_hook<Data>);
    sq_remove(v, -2); // remove object name

    return 1;
}
fagiano
#2 Posted : Saturday, June 24, 2017 6:10:59 PM(UTC)
Rank: Advanced Member

Groups: Registered, Administrators
Joined: 6/11/2005(UTC)
Posts: 1,059

Thanks: 0 times
Was thanked: 79 time(s) in 61 post(s)
I think sq_setinstanceup(v, 1, instance) should be sq_setinstanceup(v, -1, instance) the newly created instance is at the "top" of the stack after sq_createinstance()

ciao
Alberto
Follow me on Twitter @squirrellang
Users browsing this topic
Guest (2)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.095 seconds.