MCPcopy
hub / github.com/grafana/k6 / setupJSRuntime

Method setupJSRuntime

internal/js/bundle.go:405–441  ·  view source on GitHub ↗
(rt *sobek.Runtime, vuID uint64, logger logrus.FieldLogger)

Source from the content-addressed store, hash-verified

403}
404
405func (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.
444type requireImpl struct {

Callers 1

instantiateMethod · 0.95

Calls 5

NewRandSourceFunction · 0.92
newConsoleFunction · 0.85
Uint64Method · 0.80
WarnMethod · 0.80
SetMethod · 0.45

Tested by

no test coverage detected