Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize the static variables without main method. #237

Closed
ShawnYun opened this issue Apr 7, 2020 · 17 comments
Closed

Initialize the static variables without main method. #237

ShawnYun opened this issue Apr 7, 2020 · 17 comments

Comments

@ShawnYun
Copy link
Contributor

ShawnYun commented Apr 7, 2020

As #219 , we add abi offset for every method. But if there are static variables, we need to load them first.

@ShawnYun ShawnYun mentioned this issue Apr 7, 2020
@erikzhang
Copy link
Member

@lightszero What do you think?

@shargon
Copy link
Member

shargon commented Apr 7, 2020

We should call the static constructor first

@Tommo-L
Copy link
Contributor

Tommo-L commented Apr 8, 2020

Maybe we need a constructor method for each contract, like c# class?

@lightszero
Copy link
Member

简洁的修改方式 是 懒汉模式,在每个入口函数插入一个检查。

@shargon
Copy link
Member

shargon commented Apr 8, 2020

But it should be executed once, if we call a recursive method, it should be executed only once.

Exec(int a, int b, int counter)
{
 // STATIC INIT
 if (counter>10)return a+b;
 return Exec(a+b,b+1,counter+1);
}

@Tommo-L
Copy link
Contributor

Tommo-L commented Apr 8, 2020

简洁的修改方式 是 懒汉模式,在每个入口函数插入一个检查。

Translate to EN:

A simple way is to use lazy mode, insert a check method to init those static variables at the entry of each function.

@Tommo-L
Copy link
Contributor

Tommo-L commented Apr 8, 2020

But it should be executed once, if we call a recursive method, we should be executed only once.

If we use lazy mode, it's not a problem.

@Tommo-L
Copy link
Contributor

Tommo-L commented Apr 9, 2020

@erikzhang If we want to use lazy mode, could we add a new opcode SFCOUNT, which return the total amount of static field slot. Then we can use it to determine whether static variables have been loaded.

if static field slot is null return null
else return slot.count

@Tommo-L
Copy link
Contributor

Tommo-L commented Apr 9, 2020

Another way is that when we deploy a contract at the begining, we should call its constructor to complete its initialization(including static variables)

@erikzhang
Copy link
Member

My solution is that we allow the contract to have an optional special method: _initsslot().

When an contract is invoked, we check its ABI first. If it has the method _initsslot(), we call it first.

@shargon
Copy link
Member

shargon commented Apr 9, 2020

we can call it in other execution context, and after it, call the user method

@Tommo-L
Copy link
Contributor

Tommo-L commented Apr 10, 2020

allow the contract to have an optional special method: _initsslot().

Personally, I don't want to see that we have too many special functions, we already have one. I think we can adjust NEF format, make NEF like window's exe format, which have different segments.

@Tommo-L
Copy link
Contributor

Tommo-L commented Apr 15, 2020

I think we can implement the __initsslot now. I have read the windows PE format, it has a function address table and function name table for mapping. By using .ctor as object instance entry, .cctor as class static methods and variables initialization. It also has a .data sgement, but it's for user-define.

image

@shargon
Copy link
Member

shargon commented Apr 15, 2020

It's a good idea, if we process the static constructor, and after it, we save the static variables in the nef file, it could be easier to execute.

@erikzhang
Copy link
Member

neo-project/neo#1572

@lightszero
Copy link
Member

got it

@ShawnYun
Copy link
Contributor Author

close it via #219 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants