-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Small js refactor #2373
Small js refactor #2373
Changes from all commits
7e2ba38
b4764e8
54f1786
7377205
0752d37
c878391
8e9ecd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -252,22 +252,21 @@ func (b *Bundle) getExports(logger logrus.FieldLogger, rt *goja.Runtime, options | |
} | ||
|
||
// Instantiate creates a new runtime from this bundle. | ||
func (b *Bundle) Instantiate(logger logrus.FieldLogger, vuID uint64) (bi *BundleInstance, instErr error) { | ||
// TODO: actually use a real context here, so that the instantiation can be killed | ||
// Placeholder for a real context. | ||
ctxPtr := new(context.Context) | ||
|
||
func (b *Bundle) Instantiate( | ||
logger logrus.FieldLogger, vuID uint64, vuImpl *moduleVUImpl, | ||
) (bi *BundleInstance, instErr error) { | ||
// Instantiate the bundle into a new VM using a bound init context. This uses a context with a | ||
// runtime, but no state, to allow module-provided types to function within the init context. | ||
rt := goja.New() | ||
init := newBoundInitContext(b.BaseInitContext, ctxPtr, rt) | ||
if err := b.instantiate(logger, rt, init, vuID); err != nil { | ||
vuImpl.runtime = goja.New() | ||
Comment on lines
258
to
+260
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we now directly inject a vuImpl with an initialized runtime so we can maintain the initialization in one central place? (e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in a followign PR :P There are in practice a lot more changes, I just really needed moduleVUImpl to be part of VU and everything else was gravy |
||
init := newBoundInitContext(b.BaseInitContext, vuImpl) | ||
if err := b.instantiate(logger, vuImpl.runtime, init, vuID); err != nil { | ||
return nil, err | ||
} | ||
|
||
rt := vuImpl.runtime | ||
bi = &BundleInstance{ | ||
Runtime: rt, | ||
Context: ctxPtr, | ||
Context: vuImpl.ctxPtr, | ||
exports: make(map[string]goja.Callable), | ||
env: b.RuntimeOptions.Env, | ||
} | ||
|
@@ -315,7 +314,7 @@ func (b *Bundle) instantiate(logger logrus.FieldLogger, rt *goja.Runtime, init * | |
} | ||
rt.Set("__ENV", env) | ||
rt.Set("__VU", vuID) | ||
rt.Set("console", common.Bind(rt, newConsole(logger), init.ctxPtr)) | ||
_ = rt.Set("console", newConsole(logger)) | ||
|
||
if init.compatibilityMode == lib.CompatibilityModeExtended { | ||
rt.Set("global", rt.GlobalObject()) | ||
|
@@ -329,9 +328,13 @@ func (b *Bundle) instantiate(logger logrus.FieldLogger, rt *goja.Runtime, init * | |
CWD: init.pwd, | ||
Registry: b.registry, | ||
} | ||
init.moduleVUImpl.initEnv = initenv | ||
ctx := common.WithInitEnv(context.Background(), initenv) | ||
*init.ctxPtr = common.WithRuntime(ctx, rt) | ||
unbindInit := common.BindToGlobal(rt, common.Bind(rt, init, init.ctxPtr)) | ||
*init.moduleVUImpl.ctxPtr = common.WithRuntime(ctx, rt) | ||
unbindInit := common.BindToGlobal(rt, map[string]interface{}{ | ||
"require": init.Require, | ||
"open": init.Open, | ||
}) | ||
if _, err := rt.RunProgram(b.Program); err != nil { | ||
var exception *goja.Exception | ||
if errors.As(err, &exception) { | ||
|
@@ -340,7 +343,7 @@ func (b *Bundle) instantiate(logger logrus.FieldLogger, rt *goja.Runtime, init * | |
return err | ||
} | ||
unbindInit() | ||
*init.ctxPtr = nil | ||
*init.moduleVUImpl.ctxPtr = nil | ||
|
||
// If we've already initialized the original VU init context, forbid | ||
// any subsequent VUs to open new files | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could now we have
vuID
as part ofvuImpl
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably in another PR see #2373 (comment)