Skip to content

Commit

Permalink
fix: js eventloop data race
Browse files Browse the repository at this point in the history
  • Loading branch information
shiroyk committed Dec 3, 2024
1 parent 2949197 commit 3e2fa12
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
7 changes: 4 additions & 3 deletions js/eventloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions js/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 3e2fa12

Please sign in to comment.