Welcome Guest Search | Active Topics | Log In | Register

Embedded Squirrel - Running a function
#1 Posted : Thursday, October 31, 2019 1:50:36 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 10/31/2019(UTC)
Posts: 1

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Hey Squirrel-Forum,

I think I'm losing my mind - currently I'm investigating Squirrel for use in one of our projects. However, I fail with the most basic feature: calling a function from C/C++ into Squirrel.

My squirrel code looks like this:

function tuna()


I would love to execute the function tuna via the Squirrel API. I followed the examples from the Documentation and on the C-Side I wrote:
SQInteger readFile(SQUserPointer file)
    char buffer;

    if(fread(&buffer, sizeof(char), 1, (FILE*)file) > 0)
        return buffer;
    return 0;

bool compileScript(HSQUIRRELVM vm, const char *fileName)
    FILE *file = nullptr;    
    if(fopen_s(&file, fileName, "r") == 0)
        const bool success = SQ_SUCCEEDED(sq_compile(vm, readFile, file, "Leviathan", true));
        return success;

    return false;

void printfunc(HSQUIRRELVM v, const SQChar *s, ...)
    va_list arglist;
    va_start(arglist, s);
    vprintf(s, arglist);

In my main I execute "compileScript" with my freshly setup VM and the path to my "hello.nut" file.


HSQUIRRELVM vm = sq_open(1024);

printf("Compiling %s...[%s]\n", argv[1], (compileScript(vm, argv[1])) ? "OK" : "FAILED");
sq_setprintfunc(vm, printfunc, printfunc); //sets the print function

From there it depends! If I do:
sq_call(vm, 1, true, true)

Then I will get my output! But If I try to find my function "tuna" with:
sq_pushroottable(vm); //pushes the global table    
sq_pushstring(vm, "tuna", -1);

printf("-> %i\n", sq_get(vm, -2));

Then I get -1 and I will not be able to call it. I feel like I'm doing something obviously wrong - but I can't figure it out...
#2 Posted : Tuesday, June 9, 2020 10:33:16 AM(UTC)
Rank: Advanced Member

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

Thanks: 2 times
Was thanked: 10 time(s) in 10 post(s)
you need to understand that when you compile, you compile - but you do not yet run.

Think of your compiled file to be enclosed by "function() { [your file here }" and when you call it, you execute that function.

What exactly happens is this:


//Compilation does this:
local myfile= function() {
  this<-function foobar() { print("hello world") ; } // this<-function ... is exactly what "function foobar() {...}" does - create a function named "foobar"
  foobar() ; // will print hello world when myfile is executed

//then you try:
foobar() ; //will fail, foobar does not yet exist here
myfile.bindenv(this) ; //this is your "sq_pushroottable(), the "this" object
myfile() ; //sq_call. this executes myfile and creates foobar in the this object
foobar() ; //will now work

After compilation there is function on the stack, but not "foobar" in any table so you won't find foobar.
But if you execute myfile, it creates foobar.
So what you do is push the root table to the stack and then execute "myfile", which THEN creates foobar in the root table. What you push on the stack is the "this" object, in above example kind the function "myfile" (However, the compiled script does not have a formal name in reality). You could push any other table first and execution would create foobar in the given table, btw. So before you execute your compiled script, no objects and functions it creates do exist yet.

(Which by the way means, you COULD delete your script object after execution to free memory. It won't EVER be referenced again. Unless you want to execute it again to recreate objects, or to create clones in a different table)
Users browsing this topic
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.056 seconds.