Skip to content

Commit 06b9861

Browse files
committed
usage: report usage of global
As part of #3864 it will be nice to know how much this is used if at all.
1 parent bef21cf commit 06b9861

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

js/bundle.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,17 @@ func (b *Bundle) setupJSRuntime(rt *sobek.Runtime, vuID int64, logger logrus.Fie
387387
}
388388

389389
if b.CompatibilityMode == lib.CompatibilityModeExtended {
390-
err = rt.Set("global", rt.GlobalObject())
390+
globalThis := rt.GlobalObject()
391+
err = globalThis.DefineAccessorProperty("global",
392+
rt.ToValue(func() sobek.Value {
393+
if err := b.preInitState.Usage.Uint64("usage/global", 1); err != nil {
394+
b.preInitState.Logger.WithError(err).Warn("couldn't report usage")
395+
}
396+
return globalThis
397+
}), rt.ToValue(func(newGlobal *sobek.Object) { // probably not a thing that will happen but still
398+
globalThis = newGlobal
399+
}),
400+
sobek.FLAG_TRUE, sobek.FLAG_TRUE)
391401
if err != nil {
392402
return err
393403
}

0 commit comments

Comments
 (0)