Squirrel

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

[sqplus] Delegates for dummies

Last post 05-20-2008, 11:26 AM by fagiano. 3 replies.
Sort Posts: Previous Next
  •  05-19-2008, 7:58 PM 2521

    [sqplus] Delegates for dummies

    What is wrong with this code?

    int setter_func(HSQUIRRELVM v)
    {
       cout << "setter called" << endl;
       return 0;
    }

    int getter_func(HSQUIRRELVM v)
    {
       cout << "getter called" << endl;
       return 0;
    }

    ...

    SquirrelObject root = SquirrelVM::GetRootTable();
    SquirrelObject table = SquirrelVM::CreateTable();
    SquirrelObject delegate = SquirrelVM::CreateTable();
    SquirrelVM::CreateFunction(delegate, setter_func, "_set");
    SquirrelVM::CreateFunction(delegate, getter_func, "_get");
    table.SetDelegate(delegate);

    SquirrelObject tmp;

    table.SetValue("dog", "bark");
    delegate.SetValue("cat", "meow");

    tmp = table.GetValue("dog");
    if (tmp.GetType() == OT_STRING)
       cout << "The dog goes: " << tmp.ToString() << endl;

    tmp = table.GetValue("cat");
    if (tmp.GetType() == OT_STRING)
       cout << "The cat goes: " << tmp.ToString() << endl;

    tmp = table.GetValue("giraffe");
    if (tmp.GetType() == OT_STRING)
       cout << "The giraffe goes: " << tmp.ToString() << endl;

    tmp = delegate.GetValue("manatee");
    if (tmp.GetType() == OT_STRING)
       cout << "The manatee goes: " << tmp.ToString() << endl;


    Output is:

    The dog goes: bark
    The cat goes: meow

    The delegate functions are never called.
  •  05-19-2008, 8:46 PM 2522 in reply to 2521

    Re: [sqplus] Delegates for dummies

    table.GetValue()

    does this function use sq_get() or sq_rawget() ? sq_rawget() doesn't invoke delegates.

    Alberto

  •  05-19-2008, 9:23 PM 2523 in reply to 2522

    Re: [sqplus] Delegates for dummies

    GetValue() is a SqPlus function that calls another SqPlus function called GetSlot(). GetSlot() uses sq_get().
  •  05-20-2008, 11:26 AM 2524 in reply to 2523

    Re: [sqplus] Delegates for dummies

    ok, I downloaded sqplus and checked.

    SetValue() uses sq_rawset() that internally does a newslot

    the get fails because SquirrelVM::CreateFunction() seems to be buggy... at least the overload that you are calling. It creates _get & _set with a wrong typemask/paramcount(so _get fails). I'm not really sure what that function is trying to do and why sneaks in some additional typemask stuff.

    Anyway, other than squirrel does what is supposed to do.

    ciao

    Alberto

     

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