From 3e2fa129343ae695825f36e38049b0d5044ee6ba Mon Sep 17 00:00:00 2001 From: shiroyk Date: Tue, 3 Dec 2024 19:16:44 +0800 Subject: [PATCH] fix: js eventloop data race --- js/eventloop.go | 7 ++++--- js/utils.go | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/js/eventloop.go b/js/eventloop.go index bb3da6f..cb5ff5d 100644 --- a/js/eventloop.go +++ b/js/eventloop.go @@ -45,13 +45,14 @@ func (e *EventLoop) Start(f func()) { continue } + done := e.doneJobs + e.doneJobs = e.doneJobs[:0] e.cond.L.Unlock() - if len(e.doneJobs) > 0 { - for _, job := range e.doneJobs { + if len(done) > 0 { + for _, job := range done { job() } - e.doneJobs = e.doneJobs[:0] } return diff --git a/js/utils.go b/js/utils.go index 8e5af22..f572706 100644 --- a/js/utils.go +++ b/js/utils.go @@ -56,8 +56,8 @@ func Unwrap(value sobek.Value) (any, error) { } } -// ModuleCallable return the sobek.CyclicModuleRecord default export as sobek.Callable. -func ModuleCallable(rt *sobek.Runtime, resolve sobek.HostResolveImportedModuleFunc, module sobek.CyclicModuleRecord) (sobek.Callable, error) { +// ModuleInstance return the sobek.ModuleInstance. +func ModuleInstance(rt *sobek.Runtime, resolve sobek.HostResolveImportedModuleFunc, module sobek.CyclicModuleRecord) (sobek.ModuleInstance, error) { instance := rt.GetModuleInstance(module) if instance == nil { if err := module.Link(); err != nil { @@ -70,7 +70,16 @@ func ModuleCallable(rt *sobek.Runtime, resolve sobek.HostResolveImportedModuleFu case sobek.PromiseStateFulfilled: default: } - instance = rt.GetModuleInstance(module) + return rt.GetModuleInstance(module), nil + } + return instance, nil +} + +// ModuleCallable return the sobek.CyclicModuleRecord default export as sobek.Callable. +func ModuleCallable(rt *sobek.Runtime, resolve sobek.HostResolveImportedModuleFunc, module sobek.CyclicModuleRecord) (sobek.Callable, error) { + instance, err := ModuleInstance(rt, resolve, module) + if err != nil { + return nil, err } value := instance.GetBindingValue("default") call, ok := sobek.AssertFunction(value)