Sign in to follow this  
jflanglois

SqPlus Question and Comment

Recommended Posts

First the question: How does one get operator() to work on a squirrel object when it has parameters? Take the following code for example:
#include "sqplus.h"

#include <iostream>

struct Test {
  void operator()( int i ) {
    std::cout << "operator() " << i << std::endl;
  }
};

int main() {
  SquirrelVM::Init();
  SqPlus::SQClassDef< Test >( L"Test" ).
    func( &Test::operator (), L"_call" );

  SquirrelObject script = SquirrelVM::CompileBuffer(
    L"local test = Test();\n"
    L"test( 3 );" );

  SquirrelVM::RunScript( script );
  SquirrelVM::Shutdown();
}


I get an exeption with:
AN ERROR HAS OCCURED [attempt to call 'instance']

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

LOCALS
[test] INSTANCE
[this] TABLE
If I don't have any parameters for opertor(), then it works fine. Am I missing something about the _call metamethod? It can take parameters, can't it? Now the comment: You may have noticed that the code above uses wide character strings. If I define SQPLUS_SUPPORT_STD_STRING, then string works, but wstring doesn't. I propose that instead of:
inline void Push( HSQUIRRELVM v, const std::string & value ) { sq_pushstring( v, value.c_str(), -1 ); }
inline bool Match( TypeWrapper< const std::string & >, HSQUIRRELVM v, int idx ) { return sq_gettype( v, idx ) == OT_STRING; }
inline std::string Get( TypeWrapper< const std::string & >, HSQUIRRELVM v, int idx ) { const SQChar * s; SQPLUS_CHECK_GET( sq_getstring( v, idx, &s ) ); return std::string( s ); }

It be:
inline void Push( HSQUIRRELVM v, const std::basic_string< SQChar > & value ) { sq_pushstring( v, value.c_str(), -1 ); }
inline bool Match( TypeWrapper< const std::basic_string< SQChar > & >, HSQUIRRELVM v, int idx ) { return sq_gettype( v, idx ) == OT_STRING; }
inline std::basic_string< SQChar > Get( TypeWrapper< const std::basic_string< SQChar > & >, HSQUIRRELVM v, int idx ) { const SQChar * s; SQPLUS_CHECK_GET( sq_getstring( v, idx, &s ) ); return std::basic_string< SQChar >( s ); }

As far as I've tested it, it seems to work correctly. I suppose one could add the definitions for wstring instead, since I'm not entirely sure if SqChar will always be char or wchar_t, and that basic_string< SqChar > will be equivalent to string and wstring. jfl.

Share this post


Link to post
Share on other sites
Not clear why the metamethod _call does not work, perhaps trace the code to see if it's an SqPlus issue or language issue, or change the code slightly (register doCall() instead of _call):

SquirrelObject script = SquirrelVM::CompileBuffer(
L"local test = Test();\n"
L"test.doCall( 3 );" );



Also perhaps test the _call method in pure script code (and post question on Squirrel forum).

String change: please post on the Squirrel forum for feedback; if no objections, I'll make the changes to SqPlus.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this