(rt *sobek.Runtime, vuID uint64, logger logrus.FieldLogger)
| 403 | } |
| 404 | |
| 405 | func (b *Bundle) setupJSRuntime(rt *sobek.Runtime, vuID uint64, logger logrus.FieldLogger) error { |
| 406 | rt.SetFieldNameMapper(common.FieldNameMapper{}) |
| 407 | rt.SetRandSource(common.NewRandSource()) |
| 408 | |
| 409 | env := make(map[string]string, len(b.preInitState.RuntimeOptions.Env)) |
| 410 | maps.Copy(env, b.preInitState.RuntimeOptions.Env) |
| 411 | err := rt.Set("__ENV", env) |
| 412 | if err != nil { |
| 413 | return err |
| 414 | } |
| 415 | err = rt.Set("__VU", vuID) |
| 416 | if err != nil { |
| 417 | return err |
| 418 | } |
| 419 | err = rt.Set("console", newConsole(logger)) |
| 420 | if err != nil { |
| 421 | return err |
| 422 | } |
| 423 | |
| 424 | if b.CompatibilityMode == lib.CompatibilityModeExtended { |
| 425 | globalThis := rt.GlobalObject() |
| 426 | err = globalThis.DefineAccessorProperty("global", |
| 427 | rt.ToValue(func() sobek.Value { |
| 428 | if err := b.preInitState.Usage.Uint64("usage/global", 1); err != nil { |
| 429 | b.preInitState.Logger.WithError(err).Warn("couldn't report usage") |
| 430 | } |
| 431 | return globalThis |
| 432 | }), rt.ToValue(func(newGlobal *sobek.Object) { // probably not a thing that will happen but still |
| 433 | globalThis = newGlobal |
| 434 | }), |
| 435 | sobek.FLAG_TRUE, sobek.FLAG_TRUE) |
| 436 | if err != nil { |
| 437 | return err |
| 438 | } |
| 439 | } |
| 440 | return nil |
| 441 | } |
| 442 | |
| 443 | // this exists only to make the check in the init context. |
| 444 | type requireImpl struct { |
no test coverage detected