diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 029641f2507..33a6beea581 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -4,6 +4,7 @@ import ( "fmt" "math/big" "reflect" + "sync/atomic" "github.com/gnolang/gno/tm2/pkg/errors" ) @@ -95,7 +96,8 @@ func PredefineFileSet(store Store, pn *PackageNode, fset *FileSet) { // (like ConvertUntypedTo() for bigints and strings) // are only called during the preprocessing stage. // It is a counter because Preprocess() is recursive. -var preprocessing int +// As a global counter, use lockless atomic to support concurrency. +var preprocessing atomic.Int32 // Preprocess n whose parent block node is ctx. If any names // are defined in another file, generally you must call @@ -116,12 +118,8 @@ var preprocessing int // - TODO document what it does. func Preprocess(store Store, ctx BlockNode, n Node) Node { // Increment preprocessing counter while preprocessing. - { - preprocessing += 1 - defer func() { - preprocessing -= 1 - }() - } + preprocessing.Add(1) + defer preprocessing.Add(-1) if ctx == nil { // Generally a ctx is required, but if not, it's ok to pass in nil. diff --git a/gnovm/pkg/gnolang/values_conversions.go b/gnovm/pkg/gnolang/values_conversions.go index 66d8bcbf233..b4888878c7a 100644 --- a/gnovm/pkg/gnolang/values_conversions.go +++ b/gnovm/pkg/gnolang/values_conversions.go @@ -939,17 +939,17 @@ func ConvertUntypedTo(tv *TypedValue, t Type) { case UntypedRuneType: ConvertUntypedRuneTo(tv, t) case UntypedBigintType: - if preprocessing == 0 { + if preprocessing.Load() == 0 { panic("untyped Bigint conversion should not happen during interpretation") } ConvertUntypedBigintTo(tv, tv.V.(BigintValue), t) case UntypedBigdecType: - if preprocessing == 0 { + if preprocessing.Load() == 0 { panic("untyped Bigdec conversion should not happen during interpretation") } ConvertUntypedBigdecTo(tv, tv.V.(BigdecValue), t) case UntypedStringType: - if preprocessing == 0 { + if preprocessing.Load() == 0 { panic("untyped String conversion should not happen during interpretation") } if t.Kind() == StringKind {