Squirrel

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

Typesafe scripting (SqPlus)?

Last post 05-10-2008, 6:15 AM by neimod. 8 replies.
Sort Posts: Previous Next
  •  05-03-2008, 9:18 PM 2478

    Typesafe scripting (SqPlus)?

    Hello,

    I've got a class Vector3 bound to squirrel, and it has a function Vector3::add(Vector3* vector).
    Now I also have another class, lets say Player, bound to squirrel.

    The odd thing is, when I try to do this in a squirrel script:

    local v = Vector3(1.0, 2.0, 3.0);
    local p = Player();

    local v2 = v + p;

    SqPlus seems to simply cast the Player* into a Vector3* when the function Vector3::add is called?
    Should there not be a runtime exception from SqPlus?

    Is this normal SqPlus behavior or am I doing something wrong?

    Thanks in advance.

  •  05-03-2008, 9:51 PM 2479 in reply to 2478

    Re: Typesafe scripting (SqPlus)?

    Ok, I seem to have nailed it down to line 520 in sqplus.h:

    // Get an instance of type T from the stack at idx (for function calls).
    template<typename T,bool ExceptionOnError>
    T * GetInstance(HSQUIRRELVM v,SQInteger idx) {
      SQUserPointer up=0;
      sq_getinstanceup(v,idx,&up,ClassType<T>::type());        if (ExceptionOnError) { // This code block should be compiled out when ExceptionOnError is false. In any case, the compiler should not generate a test condition (include or exclude the enclosed code block).
        if (!up) throw SquirrelError(_T("GetInstance: Invalid argument type"));
      } // if
      return (T *)up;
    } // GetInstance


    In my case, the sq_getinstanceup returns an error, and this error is not checked. I'm not sure if this is intended?
  •  05-03-2008, 10:10 PM 2480 in reply to 2479

    Re: Typesafe scripting (SqPlus)?

    Ok changing that to:

    template<typename T,bool ExceptionOnError>
    T * GetInstance(HSQUIRRELVM v,SQInteger idx) {
      SQUserPointer up=0;
      bool failed = SQ_FAILED(sq_getinstanceup(v,idx,&up,ClassType<T>::type()));

      if (ExceptionOnError) { // This code block should be compiled out when ExceptionOnError is false. In any case, the compiler should not generate a test condition (include or exclude the enclosed code block).
        if (!up || failed) throw SquirrelError(_T("GetInstance: Invalid argument type"));
      } // if
      return (T *)up;
    } // GetInstance

    Seems to fix my problems.
    Could any of the SqPlus devs comment on this?
  •  05-04-2008, 6:01 AM 2481 in reply to 2480

    Re: Typesafe scripting (SqPlus)?

    Hi, Thank you for your contribution.
    I've made a branch for this change and added a test code.
    Please check SVN at https://sqplus.svn.sourceforge.net/svnroot/sqplus/branches/neimod-typesafe-r152

    I will merge it into SVN trunk if it's OK with you.

    Regards,
    --
    K. Kawachi
  •  05-04-2008, 3:25 PM 2482 in reply to 2481

    Re: Typesafe scripting (SqPlus)?

    Sure, no problem.

    Happy to contribute :)
  •  05-05-2008, 6:09 AM 2485 in reply to 2481

    Re: Typesafe scripting (SqPlus)?

    Hi Katsuaki,

    I've found a bug with my replacement code!

    The SqPlus Match() for example requires proper return values from the GetInstance function, and in my replacement code, if the exceptionOnError flag is set to false, it will still return a pointer to the UP, even if it was invalid. This is not right, it should return NULL in this case.

    Here my new version of the code:

    template<typename T,bool ExceptionOnError>
    T * GetInstance(HSQUIRRELVM v,SQInteger idx) {
      SQUserPointer up=0;
      const bool failed = SQ_FAILED(sq_getinstanceup(v,idx,&up,ClassType<T>::type()));

      if (failed)
          up = 0;

      if (ExceptionOnError) { // This code block should be compiled out when ExceptionOnError is false. In any case, the compiler should not generate a test condition (include or exclude the enclosed code block).
        if (!up) throw SquirrelError(_T("GetInstance: Invalid argument type"));
      } // if
      return (T *)up;
    } // GetInstance
  •  05-06-2008, 7:16 AM 2495 in reply to 2485

    Re: Typesafe scripting (SqPlus)?

    Hi,

    The SVN branch is updated.  Thank you.

    I'd completely forgotten what I did around "GetInstance" two years ago.
     http://squirrel-lang.org/forums/thread/962.aspx
    :-D
    --
    K. Kawachi

  •  05-10-2008, 3:34 AM 2502 in reply to 2485

    Re: Typesafe scripting (SqPlus)?

    The svn branch merged into trunk.
    --
    K. Kawachi
  •  05-10-2008, 6:15 AM 2503 in reply to 2502

    Re: Typesafe scripting (SqPlus)?

    Hi Katsuaki,

    SqPlus is now only typesafe when compiling in Debug mode, for Release mode it will sometimes allow invalid type conversions.
    Please see http://squirrel-lang.org/forums/thread/2486.aspx for a solution.

    Thank you.
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems