[go: up one dir, main page]

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

Add additional usages #3951

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ func (bi *BundleInstance) manipulateOptions(options lib.Options) error {

func (b *Bundle) newCompiler(logger logrus.FieldLogger) *compiler.Compiler {
c := compiler.New(logger)
c.WithUsage(b.preInitState.Usage)
c.Options = compiler.Options{
CompatibilityMode: b.CompatibilityMode,
SourceMapLoader: generateSourceMapLoader(logger, b.filesystems),
Expand Down Expand Up @@ -386,7 +387,17 @@ func (b *Bundle) setupJSRuntime(rt *sobek.Runtime, vuID int64, logger logrus.Fie
}

if b.CompatibilityMode == lib.CompatibilityModeExtended {
err = rt.Set("global", rt.GlobalObject())
globalThis := rt.GlobalObject()
err = globalThis.DefineAccessorProperty("global",
rt.ToValue(func() sobek.Value {
if err := b.preInitState.Usage.Uint64("usage/global", 1); err != nil {
b.preInitState.Logger.WithError(err).Warn("couldn't report usage")
}
return globalThis
}), rt.ToValue(func(newGlobal *sobek.Object) { // probably not a thing that will happen but still
globalThis = newGlobal
}),
sobek.FLAG_TRUE, sobek.FLAG_TRUE)
if err != nil {
return err
}
Expand Down
26 changes: 25 additions & 1 deletion js/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,30 @@ import (
"github.com/sirupsen/logrus"

"go.k6.io/k6/lib"
"go.k6.io/k6/usage"
)

// A Compiler compiles JavaScript or TypeScript source code into a sobek.Program
type Compiler struct {
logger logrus.FieldLogger
Options Options
usage *usage.Usage
}

// New returns a new Compiler
func New(logger logrus.FieldLogger) *Compiler {
return &Compiler{logger: logger}
return &Compiler{
logger: logger,
// TODO(@mstoykov): unfortunately otherwise we need to do much bigger rewrite around experimental extensions
// and likely more extensions tests. This way tests don't need to care about this and the compiler code
// doesn't need to check if it has usage set or not.
usage: usage.New(),
}
}

// WithUsage allows the compiler to be given [usage.Usage] to use
func (c *Compiler) WithUsage(u *usage.Usage) {
c.usage = u
}

// Options are options to the compiler
Expand Down Expand Up @@ -64,7 +77,15 @@ func (c *Compiler) Parse(
return state.parseImpl(src, filename, commonJSWrap)
}

const (
usageParsedFilesKey = "usage/parsedFiles"
usageParsedTSFilesKey = "usage/parsedTSFiles"
)

func (ps *parsingState) parseImpl(src, filename string, commonJSWrap bool) (*ast.Program, string, error) {
if err := ps.compiler.usage.Uint64(usageParsedFilesKey, 1); err != nil {
ps.compiler.logger.WithError(err).Warn("couldn't report usage for " + usageParsedFilesKey)
}
code := src
if commonJSWrap { // the lines in the sourcemap (if available) will be fixed by increaseMappingsByOne
code = ps.wrap(code, filename)
Expand Down Expand Up @@ -97,6 +118,9 @@ func (ps *parsingState) parseImpl(src, filename string, commonJSWrap bool) (*ast
}

if ps.compatibilityMode == lib.CompatibilityModeExperimentalEnhanced {
if err := ps.compiler.usage.Uint64(usageParsedTSFilesKey, 1); err != nil {
ps.compiler.logger.WithError(err).Warn("couldn't report usage for " + usageParsedTSFilesKey)
}
code, ps.srcMap, err = esbuildTransform(src, filename)
if err != nil {
return nil, "", err
Expand Down
4 changes: 4 additions & 0 deletions js/modules/require_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (

// Require is the actual call that implements require
func (ms *ModuleSystem) Require(specifier string) (*sobek.Object, error) {
if err := ms.resolver.usage.Uint64("usage/require", 1); err != nil {
ms.resolver.logger.WithError(err).Warn("couldn't report usage")
}

if specifier == "" {
return nil, errors.New("require() can't be used with an empty specifier")
}
Expand Down