Skip to content

Commit 44b0aa6

Browse files
committed
extract module compile and cache helpers
1 parent 0694100 commit 44b0aa6

2 files changed

Lines changed: 49 additions & 43 deletions

File tree

vibes/modules.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -109,46 +109,3 @@ func (e *Engine) loadSearchPathModule(request moduleRequest) (moduleEntry, error
109109

110110
return moduleEntry{}, fmt.Errorf("require: module %q not found", request.raw)
111111
}
112-
113-
func (e *Engine) compileAndCacheModule(key, root, relative, fullPath string, content []byte) (moduleEntry, error) {
114-
if err := e.enforceModulePolicy(relative); err != nil {
115-
return moduleEntry{}, err
116-
}
117-
118-
script, err := e.Compile(string(content))
119-
if err != nil {
120-
return moduleEntry{}, fmt.Errorf("require: compiling %s failed: %w", fullPath, err)
121-
}
122-
123-
entry := moduleEntry{
124-
key: key,
125-
name: filepath.Clean(relative),
126-
path: filepath.Clean(fullPath),
127-
script: script,
128-
}
129-
script.moduleKey = key
130-
script.modulePath = entry.path
131-
script.moduleRoot = filepath.Clean(root)
132-
133-
e.modMu.Lock()
134-
if cached, ok := e.modules[key]; ok {
135-
e.modMu.Unlock()
136-
return cached, nil
137-
}
138-
if len(e.modules) >= e.config.MaxCachedModules {
139-
e.modMu.Unlock()
140-
return moduleEntry{}, fmt.Errorf("require: module cache limit reached (%d modules)", e.config.MaxCachedModules)
141-
}
142-
e.modules[key] = entry
143-
e.modMu.Unlock()
144-
145-
return entry, nil
146-
}
147-
148-
// cloneFunctionForEnv creates a shallow copy of a ScriptFunction with a different environment.
149-
// This is safe because ScriptFunction fields are immutable except for Env which we explicitly override.
150-
func cloneFunctionForEnv(fn *ScriptFunction, env *Env) *ScriptFunction {
151-
clone := *fn
152-
clone.Env = env
153-
return &clone
154-
}

vibes/modules_compile.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package vibes
2+
3+
import (
4+
"fmt"
5+
"path/filepath"
6+
)
7+
8+
func (e *Engine) compileAndCacheModule(key, root, relative, fullPath string, content []byte) (moduleEntry, error) {
9+
if err := e.enforceModulePolicy(relative); err != nil {
10+
return moduleEntry{}, err
11+
}
12+
13+
script, err := e.Compile(string(content))
14+
if err != nil {
15+
return moduleEntry{}, fmt.Errorf("require: compiling %s failed: %w", fullPath, err)
16+
}
17+
18+
entry := moduleEntry{
19+
key: key,
20+
name: filepath.Clean(relative),
21+
path: filepath.Clean(fullPath),
22+
script: script,
23+
}
24+
script.moduleKey = key
25+
script.modulePath = entry.path
26+
script.moduleRoot = filepath.Clean(root)
27+
28+
e.modMu.Lock()
29+
if cached, ok := e.modules[key]; ok {
30+
e.modMu.Unlock()
31+
return cached, nil
32+
}
33+
if len(e.modules) >= e.config.MaxCachedModules {
34+
e.modMu.Unlock()
35+
return moduleEntry{}, fmt.Errorf("require: module cache limit reached (%d modules)", e.config.MaxCachedModules)
36+
}
37+
e.modules[key] = entry
38+
e.modMu.Unlock()
39+
40+
return entry, nil
41+
}
42+
43+
// cloneFunctionForEnv creates a shallow copy of a ScriptFunction with a different environment.
44+
// This is safe because ScriptFunction fields are immutable except for Env which we explicitly override.
45+
func cloneFunctionForEnv(fn *ScriptFunction, env *Env) *ScriptFunction {
46+
clone := *fn
47+
clone.Env = env
48+
return &clone
49+
}

0 commit comments

Comments
 (0)