Raised This Month: $51 Target: $400
 12% 

Push Function to ArrayList..


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
client21
Senior Member
Join Date: Apr 2013
Old 12-04-2020 , 10:02   Push Function to ArrayList..
Reply With Quote #1

PHP Code:
public void OnPluginStart()
{
    
ArrayList a = new ArrayList();
    Function 
GetFunctionByName(GetMyHandle(), "OnPluginStart");

    
// error 130: cannot coerce functions to values
    
a.Push(f); 

    
// warning 237: coercing functions to and from primitives is unsupported
    // and will be removed in the future
    
a.Push(view_as<int>(f));

Are you seriously? Is that really all I can do? :

PHP Code:
public void OnPluginStart()
{
    
ArrayList a = new ArrayList();
    Function 
GetFunctionByName(GetMyHandle(), "OnPluginStart");
    
DataPack pack = new DataPack();
    
pack.WriteFunction(f);
    
a.Push(pack);

Very sad
client21 is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 12-04-2020 , 10:41   Re: Push Function to ArrayList..
Reply With Quote #2

You can also use PrivateForward with Call_StartForward.
Code:
public void OnPluginStart()
{
	ArrayList a = new ArrayList();
	PrivateForward hFwd = new PrivateForward( ET_Ignore );
	hFwd.AddFunction( null, GetFunctionByName( null, "OnPluginStart" ) );
	a.Push( hFwd );
}
__________________

Last edited by MAGNAT2645; 12-04-2020 at 10:54. Reason: Added code example
MAGNAT2645 is offline
client21
Senior Member
Join Date: Apr 2013
Old 12-04-2020 , 11:04   Re: Push Function to ArrayList..
Reply With Quote #3

Quote:
Originally Posted by MAGNAT2645 View Post
You can also use PrivateForward with Call_StartForward.
Yes, this is also a way, but it still looks silly.
It feels like PushFunction method was planned, but forgotten.
At the moment, there is still such a way (it works) (by Kailo) :

PHP Code:
int Extractor(int[] anchor)
{
    
// Stack structure
    // anchor[0] | 0 | old_frame | argn | func
    
return anchor[4];
}

int FuncToInt(Function func)
{
    if (
func) {} // symbol is never used
    
int anchor[1];
    return 
Extractor(anchor);
}

Function 
FuncExtractor(Function[] anchor)
{
    
// Stack structure
    // anchor[0] | 0 | old_frame | argn | func
    
return anchor[4];
}

Function 
IntToFunc(int func)
{
    if (
func) {} // symbol is never used
    
Function anchor[1];
    return 
FuncExtractor(anchor);
}

public 
void OnPluginStart()
{
    static 
int t 0;
    if (++
== 1)
    {
        
Handle plugin GetMyHandle();
        
        Function 
GetFunctionByName(plugin"OnPluginStart");
        
int i FuncToInt(f);
        
        
Call_StartFunction(pluginIntToFunc(i));
        
Call_Finish();
    }
    else {
        
PrintToServer("works"); // yes, works
    
}

client21 is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 12-04-2020 , 15:21   Re: Push Function to ArrayList..
Reply With Quote #4

As many have said you shouldn't convert functions to/from int since int is not that big so at some point it could break function addresses i think.
__________________
MAGNAT2645 is offline
client21
Senior Member
Join Date: Apr 2013
Old 12-04-2020 , 20:56   Re: Push Function to ArrayList..
Reply With Quote #5

Quote:
Originally Posted by MAGNAT2645 View Post
As many have said you shouldn't convert functions to/from int since int is not that big so at some point it could break function addresses i think.
I don't think so.
PHP Code:
void PackFunction(cell_t function); 
cell_t = 4 bytes (max value = unsigned int)
int i = StringToInt("4294967295"); // max value = unsigned int
PrintToServer("%u", i);

----

I can also add that the developers forgot to add another method like:
int FetchUInt(int field, DBResult &result=DBVal_Error)

Or:
int FetchInt(int field, DBResult &result=DBVal_Error, bool uint = false)

Because FetchInt can't return unsigned int and we must do: FetchString + StringToInt

Last edited by client21; 12-04-2020 at 21:10.
client21 is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 12-05-2020 , 06:35   Re: Push Function to ArrayList..
Reply With Quote #6

SourcePawn doesn't support uint as i know (at least most of functions work with signed integer). There's only int (which is signed, from −2147483648 to 2147483647)

EDIT: Maybe i was wrong, i don't really know about unsigned integers, but i know that SourcePawn supports only 32-bit integers.
__________________

Last edited by MAGNAT2645; 12-05-2020 at 06:52.
MAGNAT2645 is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 12-06-2020 , 05:21   Re: Push Function to ArrayList..
Reply With Quote #7

Quote:
Originally Posted by client21 View Post
I don't think so.
PHP Code:
void PackFunction(cell_t function); 
You need to decouple the API from the implementation in your head. The SourcePawn language rules were changed to consider Function as a separate type (like char[]) rather than a specialised int (like float) as future work to support closures will require “fat” function references that are larger than 4 bytes.

At the same time data structure that could safely contain these new function references in the future were updated with methods to get/set them — DataPack met the requirements as each entry is a) tagged with type info, so a round trip is type safe, and b) variable sized based on the implementation, so that when the work is done that requires fat references it is all internal changes. The work to implement closures has not been done yet, so obviously internally everything is still a cell_t at the moment.

ArrayList meets neither of those requirements, it has no internal typing and the block size is user-controlled.
__________________

Last edited by asherkin; 12-06-2020 at 05:22.
asherkin is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:19.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode