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

Can't return valie from script in C++
Fati
#1 Posted : Thursday, April 13, 2017 8:29:16 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 4/10/2017(UTC)
Posts: 1
Location: Tag

Thanks: 1 times
Was thanked: 0 time(s) in 0 post(s)
Hello, I am successful calling squirrel function from C++, but I can't get the value that function return.
Here is my code
Code:

#include <QCoreApplication>

#include "squirrel.h"
#include <sqstdio.h>
#include <sqstdaux.h>

//in that header file containt the squirrel bytecode
#include "call_cpp.h"
#include <iostream>
using namespace std;

#ifdef SQUNICODE
#define scvprintf vwprintf
#else
#define scvprintf vprintf
#endif

int sum=0;
int ptr=0;

void printfunc(HSQUIRRELVM v, const SQChar *s, ...)
{
        va_list arglist;
        va_start(arglist, s);
        scvprintf(s, arglist);
        va_end(arglist);
}

void call_sort(HSQUIRRELVM v)
{
    SQInteger *retval;
    int top = sq_gettop(v); //saves the stack size before the call
    sq_pushroottable(v); //pushes the global table
    sq_pushstring(v,_SC("sort"),-1);
    if(SQ_SUCCEEDED(sq_get(v,-2))) { //gets the field 'foo' from the global table
            sq_pushroottable(v); //push the 'this' (in this case is the global table)
            sq_call(v,1,1,0); //calls the function
//Here I am trying to read return value!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            sq_getinteger(v,sq_gettop(v),retval);
            cout<<"retval="<<*retval<<endl;

    }
    sq_settop(v,top); //restores the original stack size
}



SQInteger register_global_func(HSQUIRRELVM v,SQFUNCTION f,const char *fname)
{
        sq_pushroottable(v);
        sq_pushstring(v,fname,-1);
        sq_newclosure(v,f,0); //create a new function
        sq_createslot(v,-3);
        sq_pop(v,1); //pops the root table
        return 0;
}

SQInteger mem_read(SQUserPointer file,SQUserPointer buf,SQInteger size)
{
    for(int i=ptr;i<ptr+size;i++)
    {
        ((char*)buf)[i-ptr]=call_cpp[i];
    }
    ptr+=size;
    return size;
}

int main(int argc, char *argv[])
{
    SQInteger *retval;
    SQRESULT sqRES;
    SQUserPointer pUser;
    HSQUIRRELVM v;
    pUser=temporary;
    v = sq_open(1024); // creates a VM with initial stack size 1024
    sqstd_seterrorhandlers(v);

    sq_setprintfunc(v, printfunc, NULL); //sets the print function
    sq_readclosure(v,mem_read,0);
    sq_pushroottable(v);

    if(SQ_SUCCEEDED(sq_call(v,1,0,0))) {
        call_sort(v);
    }else{
        cout<<"error"<<endl;
    }

    sq_pop(v,1); //pops the root table
    sq_close(v);

    return 0;
}


Script
Code:

function sort()
{
local arr = [  5,  12, 7,  23,  0,  2,  14,  82,  85,
     80,  92,  6,  9,  40,  62,  45,  26,  80,  70,
     20,  80,  02,  07,  00,  05,  80,  71,  73];
local k=0;
for(local i=0;i<arr.len();i+=1)
    {
        for(local j=0;j<arr.len()-1;j+=1)
        {
            if(arr[j]>arr[j+1])
            {
                k=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=k;
            }
        }
    }
for(local a=0;a<arr.len();a+=1)
    {
        print(arr[a]+"\n");
    }
return 1;
}

absence
#2 Posted : Tuesday, April 18, 2017 3:47:58 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 109
Man
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 1 times
Was thanked: 10 time(s) in 10 post(s)
Wrong:
SQInteger *retval ;
[...]
sq_getinteger(v,sq_gettop(v),retval);

-> retval is uninitialized. This is prone to crashes. In debug mode, where unused variables are always 0 / NULL, sq_getinteger will simply dont't do anything

Correct:
SQInteger retval ;
[...}
sq_getinteger(v,-1,&retval);

(note there's no sq_gettop here, but a negative stack index...)

PS: also you should do sq_call(v,1,SQTrue,SQFalse) and not use integer values where the prototype uses SQBool...

And most important, CHECK WETHER THE CALL WAS SUCCESSFUL AT ALL:

[...]
if (SQ_SUCCEEDED(sq_call(v,1,SQTrue,SQTrue)) {
sq_getinteger(v,-1,&retval) ;
cout<<"retval="<<*retval<<endl;
} else {
//nothing to do. rely on SQRaiseError to report any errors via Squirrels normal print mechanisms)
}
sq_settop(v,top) ; //restore stack by all means
1 user thanked absence for this useful post.
Fati on 4/20/2017(UTC)
Users browsing this topic
Guest
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.099 seconds.