Skip to content

Commit

Permalink
Merge pull request #2187 from CortexFoundation/dev
Browse files Browse the repository at this point in the history
avoid compiling js bigint when not needed
  • Loading branch information
ucwong authored Oct 31, 2024
2 parents 4943737 + cf9713a commit ac276b3
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions ctxc/tracers/js/goja.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"math/big"
"slices"
"sync"

"github.com/dop251/goja"

Expand All @@ -45,9 +46,17 @@ func init() {
tracers.RegisterLookup(true, newJsTracer)
}

// bigIntProgram is compiled once and the exported function mostly invoked to convert
// hex strings into big ints.
var bigIntProgram = goja.MustCompile("bigInt", bigIntegerJS, false)
var compiledBigInt *goja.Program
var compileOnce sync.Once

// getBigIntProgram compiles the bigint library, if needed, and returns the compiled
// goja program.
func getBigIntProgram() *goja.Program {
compileOnce.Do(func() {
compiledBigInt = goja.MustCompile("bigInt", bigIntegerJS, false)
})
return compiledBigInt
}

type toBigFn = func(vm *goja.Runtime, val string) (goja.Value, error)
type toBufFn = func(vm *goja.Runtime, val []byte) (goja.Value, error)
Expand Down Expand Up @@ -511,7 +520,7 @@ func (t *jsTracer) setBuiltinFunctions() {
func (t *jsTracer) setTypeConverters() error {
// Inject bigint logic.
// TODO: To be replaced after goja adds support for native JS bigint.
toBigCode, err := t.vm.RunProgram(bigIntProgram)
toBigCode, err := t.vm.RunProgram(getBigIntProgram())
if err != nil {
return err
}
Expand Down

0 comments on commit ac276b3

Please sign in to comment.