Sq Plus Return Copy In Thread Problem
Last changed: -66.249.2.51

.

coorrelrelac

#include <stdio.h>
#include <stdarg.h>
#include <sqplus.h>

class Vector3
{
public:
  float x, y, z;
};


class A 
{
public:
  Vector3  v;
  Vector3  get() {return v;}
  Vector3& getRef() {return v;}
  Vector3* getPtr() {return &v;}
};

static A a;

A* get_a()
{
  return &a;
}


DECLARE_INSTANCE_TYPE(Vector3);
DECLARE_INSTANCE_TYPE(A);

static int getV(HSQUIRRELVM v)
{
  Vector3 vec = {1,2,3};
  return SqPlus::ReturnCopy(v, vec);
}



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


const char *code = "\
  print(\"main flow\\n\")\n\
  local a = get_a()\n\
  ::v <- null\n\
  v = a.getPtr()\n\
  v.x = 234\n\
  v = a.getRef()\n\
  v.x = 234\n\
  v = a.get()\n\
  v.x = 234\n\
  print(\"main flow end\\n\")\n\
  function thread_func() {\n\
    print(\"thread start\\n\")\n\
    local a = get_a()\n\
    print(\"trying getPtr\\n\")\n\
    v = a.getPtr()\n\
    v.x = 234\n\
    print(\"trying getRef\\n\")\n\
    v = a.getRef()\n\
    v.x = 234\n\
    print(\"trying get\\n\")\n\
    v = a.get()\n\
    v.x = 234\n\
    print(\"thread end\\n\")\n\
  }\n\
  local coro = ::newthread(thread_func)\n\
  coro.call()\n\
";


int main()
{
  SquirrelVM::Init();

  sq_setprintfunc(SquirrelVM::GetVMPtr(), printfunc);

  SqPlus::SQClassDef<Vector3>("Vector3")
    .var(&Vector3::x, "x")
    .var(&Vector3::y, "y")
    .var(&Vector3::z, "z")
  ;
  SqPlus::SQClassDef<A>("A")
    .var(&A::v, "v")
    .func(&A::get,    "get")
    .func(&A::getRef, "getRef")
    .func(&A::getPtr, "getPtr")
  ;


  SqPlus::RegisterGlobal(get_a, "get_a");

  SquirrelObject inst = SquirrelVM::CompileBuffer(code);
  SquirrelVM::RunScript(inst);


  SquirrelVM::Shutdown();

  return 0;
}

When program is run it produces the following output:

main flow
main flow end
thread start
trying getPtr
trying getRef

AN ERROR HAS OCCURED [the index doesn't exist]

CALLSTACK
*FUNCTION [main()] console_buffer line [26]

LOCALS
[coro] THREAD
[a] INSTANCE
[this] TABLE

This means that fields of variable returned by SqPlus::ReturnCopy is inaccessible when this variable is passed to script from thread. Variables returned by pointer or outside of thread work ok.

It seems that there is a problem in ReturnCopy() or SqPlus::Push() because of SqPlus global VM handle while threads are also HSQUIRRELVMs.