Using .call or .acall to call member functions of an object

I’ve been successfully using .call and .acall to invoke methodsfor tables/instances/classes but am having trouble with the methods for default objects like strings. I’ve been trying to pass the object itself as the context. I get an error when that happens. Is there an alternate syntax that I can use? Using acall() is an essential part of my code, so I’m hoping I can still use it.

For example
`
testStr <- “hithere”;
server.log(“toupper=”+testStr.toupper());
server.log(“slice=”+testStr.slice(3));

server.log(“toupper.call=”+testStr.toupper.call(testStr)); // this fails
server.log(“toupper.acall=”+testStr.toupper.acall([testStr])); // this fails
`

The error I get is:
[Agent] toupper=HITHERE
[Agent] slice=here
[Agent] ERROR: parameter 1 has an invalid type ‘string’ ; expected: ‘table|userdata|class|instance|meta’
[Agent] ERROR: in unknown overrides:2
[Agent] ERROR: from main vimp:10

Squirrel call() and acall() don’t work on the built-in types. The best workaround is probably to wrap the strings in an object of your own which offers the methods that you need.

Peter

Thanks :frowning:
Is there any way for me to detect whether a method is from a delegate?

https://electricimp.com/docs/squirrel/table/rawin/ ?

Peter

Maybe I have my terminology wrong but isn’t tochar a number delegate? I can’t use table.rawin to determine whether it is, though.

//INTEGER DEFAULT DELEGATE////////////////////////// SQRegFunction SQSharedState::_number_default_delegate_funcz[]={ {_SC("tointeger"),default_delegate_tointeger,1, _SC("n|b")}, {_SC("tofloat"),default_delegate_tofloat,1, _SC("n|b")}, {_SC("tostring"),default_delegate_tostring,1, _SC(".")}, {_SC("tochar"),number_delegate_tochar,1, _SC("n|b")}, {_SC("weakref"),obj_delegate_weakref,1, NULL }, {0,0,0,0} };

myNumber <- 1;

(“tochar” in myNumber) returns true.

I’m looking for an easy way to determine whether I can still use obj.method.acall([params]) to invoke the method or (obj.method)(param1,param2…) solely for the built-in types.

So you’ve got a Squirrel variable of unknown type, and a string which is the name of a method you’d like to call on that variable, and an array of the parameters you’d like to pass?

One way would be to use typeof() to work out whether it’s a built-in type or not. But if you’re prepared to “manually” implement acall() in the built-in case, maybe you should do it that way in all cases?

Peter