feat(module): pluggable module providers and a host module prelude - #87
Merged
Conversation
The module system assumed a filesystem, so embedders without one (WASM in a browser) could not use [use ...] at all. Add a ModuleProvider trait consulted ahead of path resolution — declining with Ok(None) falls through, so package/manifest/lockfile resolution is unchanged — plus a MapProvider over an in-memory name -> source map. Provider modules share the eval path with filesystem ones via eval_module, so cycle detection, nested [use], and pub exports behave identically whatever the origin. set_prelude lets an embedder evaluate a host stdlib into every module's env (vcad needs its CAD vocabulary there). Names defined before the module body are now excluded from the implicit "export everything" fallback, so a module without pub exports only its own definitions rather than the prelude's as well.
|
Bugbot is not enabled for your account, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
Choji has used up today's reviewsYour team has reached the free plan's limit of 5 reviews per day, so Choji stepped aside on this pull request. That's a plan limit, not a reflection of the change itself. Your free reviews reset tomorrow. To have Choji review every push without waiting, upgrade to Pro for unlimited reviews. You're on the free plan · 5 reviews/day. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
The module system assumed a filesystem, so an embedder without one — WASM in a browser — could not use
[use ...]at all. vcad hit this building a two-repo CAD project where a shared interface must have a single source of truth: its browser kernel and its MCP server both evaluate loon, and neither could reach the module system, so multi-file projects needed an external concatenation step that defeatspub, cannot alias, and has no real cycle detection.Threading a
base_dirfurther would only fix the native path. The resolver, not the path, is the thing to abstract.What changed
trait ModuleProvider { fetch(module_path, from_dir) -> Result<Option<String>, String> }, consulted ahead of path resolution. ReturningOk(None)declines and resolution falls through, so path deps, manifests, lockfiles and remote deps are untouched — a provider is purely additive.MapProvider, a provider over an in-memoryname -> sourcemap (also accepts<name>.loon/<name>.ookeys so a caller can key by filename).ModuleCache::{with_provider, set_provider, set_prelude}, andinterp::eval_program_with_modules(exprs, base_dir, provider, module_prelude);eval_program_with_base_dirdelegates to it, so existing callers are unchanged.eval_module, keyed by a<provider>/<name>sentinel. Cycle detection (Loading/Loaded), nested[use], aliasing, selective import andpubbehave identically whatever the origin.set_preludeevaluates a host stdlib into every module's env — vcad needs its CAD vocabulary visible inside imported modules.Behavior change worth a look
The implicit "no
pubdeclared → export everything" fallback previously excluded only builtins, so it also exported prelude names (Option,Result, …) undermod.Name. It now excludes everything defined before the module body — the prelude and any host prelude — so such a module exports only its own definitions.loon-lang's 415 tests pass unchanged.Consumer
Required by ecto/vcad#715, which supplies an in-memory map on the WASM side and the vcad stdlib as the module prelude. That PR's Rust jobs fail until this lands on
main, since vcad's CI clones loon at its default branch.🤖 Generated with Claude Code