WurstScript icon indicating copy to clipboard operation
WurstScript copied to clipboard

hashmap data can't migrate from compiletime to runtime

Open taoria opened this issue 5 years ago • 8 comments

package testcom
import HashMap
HashMap<int,int> test_map= compiletime(new HashMap<int,int>())
@compiletime function initMap()
    test_map.put(1,10)
init
    Log.info(test_map.size().toString())
    Log.info(test_map.get(1).toString())

should get

1
10

but get

1
0

is there something i did wrong?

taoria avatar Dec 28 '19 07:12 taoria

Hmm, I think this happens because HashMap uses a static field for the hashtable and since this is not marked as compiletime it will have different values at runtime and compiletime.

@Frotty Maybe this field should be initialized with a compiletime expression in StdLib?

peq avatar Dec 28 '19 17:12 peq

I suppose it could be done. If this is no longer a super-experimental feature we should probably also document it.

Frotty avatar Dec 28 '19 18:12 Frotty

fixed by https://github.com/wurstscript/WurstStdlib2/commit/0cd75387f2b50bb89057fbf78f61d84cf4885beb

Frotty avatar Dec 29 '19 19:12 Frotty

@peq That didn't solve the issue. This simple example still doesn't work.

Frotty avatar Jan 05 '20 22:01 Frotty

Emmm,I find expression ‘sth castTo int’ has different result on runtime and compiletime.this is the reason why compiletime migration failed on hashtable-based data structure.

import HashList
HashList<int> hashList = compiletime(new HashList<int>())
int i = compiletime(initMap())
function initMap() returns int
    hashList.add(10)
    return hashList castTo int
init
//force hashlist.ht to be public and make the initializing compiletime 
    Log.info(HashList.ht.loadInt(i, 0).toString())

get 10.(:

taoria avatar Jan 07 '20 23:01 taoria

Hmm, I don't think it makes sense to keep the object ids the same in compiletime and runtime. If there were 1000 HashList items created at compiletime but only id 1 and 1000 migrated to runtime, then we would have to initialize the free-list for the remaining 998 items at map loadtime.

It's probably better to just work around this limitation and don't rely on casting to int.

peq avatar Jan 15 '20 19:01 peq

looks like it's not a wurstscript issue anymore?and yes it's easy to make a compiletime compatible and hashtable based container.🙂

taoria avatar Jan 16 '20 08:01 taoria

It's still open for discussion.

On the one hand the feature would be easier to use if object ids stayed the same.

On the other hand it makes the compiler more complicated and the generated map script bigger.

peq avatar Jan 16 '20 08:01 peq