tinygo
tinygo copied to clipboard
Lazy loading always failing for the first time when compiling Go to WebAssembly
tinygo version tinygo version 0.38.0 windows/amd64 (using go version go1.24.4 and LLVM version 19.1.2)
compile command tinygo build -o o2o_drug_shop_assistant.wasm -target wasm -no-debug -scheduler=none -gc=leaking .
error the code in getMemoryPool does not seem to be executed on the first call wasm method: allocate_size_bytes
the code in getMemoryPool does not seem to be executed on the first call.
var _memoryPool *MemoryPool
func forceInitMemoryPool() *MemoryPool {
if _memoryPool == nil {
pool := &MemoryPool{
pool: make(map[uint32][]byte),
maxSize: 1024,
curSize: 0,
}
if pool.pool != nil {
testKey := uint32(12345)
testValue := make([]byte, 8)
pool.pool[testKey] = testValue
if _, exists := pool.pool[testKey]; exists {
delete(pool.pool, testKey)
_memoryPool = pool
}
}
}
return _memoryPool
}
func getMemoryPool() *MemoryPool {
if _memoryPool == nil {
return forceInitMemoryPool()
}
return _memoryPool
}
//
//export allocate_size_bytes
func AllocateSizeBytes(size uint32) uint32 {
.......
memoryPool := getMemoryPool()
// Commenting out the following section will definitely fail; the code in getMemoryPool does not seem to be executed on the first call.
if memoryPool == nil || memoryPool.pool == nil {
// 如果获取失败,尝试强制初始化
memoryPool = forceInitMemoryPool()
if memoryPool == nil || memoryPool.pool == nil {
return 0
}
}
.......
return ptr
}```