embedding/application-modules.md
- Repository
- guillermomolina/protos
- Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75- Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6- Blob
33d77bb188b51ac983f9d20aaa3f2673207970dd- Source
- View exact source on GitHub
Supplying application modules from Java (app:)
Status: Non-normative HOWTO
Normative owners:
spec/semantics/MODULES.md,spec/concurrency/ACTORS.md(§8Actor.spawn), andspec/io/PROCESS_IO.md(Standard Polyglot embedding bootstrap and authority)
Purpose
A Java host often needs to ship Protos modules of its own: code that the main evaluation imports, or that a spawned Actor runs. In a standard Polyglot Context there is no filesystem or classpath search for such modules. The host supplies them explicitly through:
com.guillermomolina.protos.execution.ProtosEmbeddedModules
.install(Context context, Map<String, String> modules)
Each entry maps an exact app: specifier to the Protos source characters of
that module. The Protos program then reaches it through ordinary
import("app:...") or Actor.spawn("app:...", ...).
This page extends Embedding Protos in a Java application; read that first for the Context lifecycle, class path, and authority model.
Availability
| Capability | State |
|---|---|
ProtosEmbeddedModules.install with import("app:...") | Implemented and accepted (I087-2 / PLAT055) on the portable JVM runtime |
Actor.spawn("app:...", ...) | Implemented and accepted (same slice) |
Filesystem/classpath lookup, relative app: imports, aliases | Not available by design |
| Native Image embedding hosts / Native CLI | Not supported |
app: in the CLI, Test Tool, or other Protos-owned drivers | Not available: installation is rejected on driver-owned Contexts |
applicationModules of ProtosRootActorServiceSession.open (D225-B1 RootActor service entry) | Implemented: the session composes the same catalog itself |
Verified scope: this repository's 0.3.312-SNAPSHOT development revision; no
release containing it is implied.
The two hosting APIs share one app: identity and module semantics but have
different installation boundaries. ProtosEmbeddedModules.install(...) applies
only to a standard Polyglot Context you build, before its first evaluation,
and is still rejected on driver-owned Contexts. The explicit RootActor service
session (Hosting a RootActor message service)
does not use install: you pass the catalog as the applicationModules
argument of ProtosRootActorServiceSession.open(...), and the session validates
it with the same rules before it opens anything. It then resolves the entry
file and its ./ modules first, then the catalog, then std:, for its whole
Process lifetime. HTTP Workers, which import their factory module inside their
own Actor, name it by its exact app: identity.
Example
import com.guillermomolina.protos.execution.ProtosEmbeddedModules;
import java.util.Map;
import org.graalvm.polyglot.Context;
public final class AppModules {
private static final String WORKER = """
greeting: "hello"
start: (initial) => {
count: initial
{
increment: () => {
count = count + 1
count
}
}
}
""";
public static void main(String[] args) {
try (Context context =
Context.newBuilder("protos").allowCreateThread(true).build()) {
// 1. Install once, after build and before the first evaluation.
ProtosEmbeddedModules.install(context, Map.of("app:worker", WORKER));
// 2. Ordinary import.
String greeting =
context.eval("protos", "import(\"app:worker\").greeting").asString();
// 3. The same module as the initial module of a new Actor.
int count = context.eval("protos",
"w: Actor.spawn(\"app:worker\", \"start\", 41)\n"
+ "w.request(\"increment\").value()")
.asInt();
System.out.println(greeting + " " + count);
}
}
}
Build and run it with the class path from the embedding page.
Expected output: hello 42.
This is the same scenario that the portable smoke runs from an external
application against the distributed JAR (application-modules mode of
dist/Plat054EmbeddingProbe.java).
allowCreateThread(true) is needed only for the Actor.spawn part; plain
imports work without it.
Rules of the catalog
Exact specifiers. A key must be app: followed by a non-empty name. The
key is used verbatim as the canonical ModuleKey: no normalization, no extension
inference, no case folding, no relative resolution. import("app:worker")
finds the entry; import("app:worker.protos"), import("worker"), and
import("APP:worker") do not.
Ordinary module semantics. Catalog modules follow chapter 6 unchanged:
- within one Actor, every
import("app:x")returns the same cached instance and the body runs once; - two entries with identical source are still two modules
(
import("app:a") !== import("app:b")); - cyclic imports observe the real, partially initialized module;
- a failed initialization is an ordinary Protos Error, the cache entry is removed, and a later import retries with a fresh instance;
- each Actor has its own instances: a spawned Actor initializes its own copy
of
app:worker, and the RootActor's copy is independent of it.
Lazy loading. A module is compiled only when first imported. An entry with a syntax error has no effect until something imports it.
Host evaluations never become catalog modules. Calling
context.eval(Source.newBuilder("protos", code, "app:worker").build()) does
not evaluate or replace the catalog module named app:worker: Source names,
URIs, and content never create a ModuleKey. Every host evaluation stays a
standalone entry.
Other domains are unaffected. std: modules still resolve from the
Standard Library alongside the catalog. A specifier that is not in the catalog,
including an unknown app: one, fails as an ordinary resolution Error.
Installation lifecycle
Install exactly once per Context, after building it and before its first evaluation:
Context.newBuilder("protos").build()
context.initialize("protos") allowed before install
context.getBindings("protos") allowed before install
context.parse("protos", ...) allowed before install
ProtosEmbeddedModules.install(...) once
context.eval(...) first evaluation starts the Process
- The
modulesmap is validated and copied. Later changes to your map have no effect, and an invalid entry rejects the whole installation: nothing is published. - An empty map is a valid installation (and still counts as the one installation).
- Installation does not create the Process.
- Installation and the first evaluation are atomic with respect to each other:
if they race on two threads, either the bootstrap sees the whole catalog or
installfails withIllegalStateException. - Each Context has its own catalog, even when several Contexts share one
Engine. A Context without a catalog cannot import another Context'sapp:modules.
Errors from install
| Exception | When |
|---|---|
NullPointerException | context, modules, a key, or a source is null |
IllegalArgumentException | A key is not app: + non-empty name (for example app:, std:..., java:x, app:x), or the Context does not support Protos |
IllegalStateException | The Context is closed, already has a catalog, has started (or failed) its Process bootstrap, or is owned by a Protos host driver |
Failures inside Protos (unknown specifier, failing module body) are ordinary
Protos Errors seen by the guest, not Java exceptions from install. If such an
Error escapes a host entry unhandled, it terminates the Process as described in
the embedding page.
Authority
Supplying code grants no authority. A catalog module:
- does not receive
process,filesystem,network, orresolverslots; only the initial module of the first host evaluation (or, in the RootActor service session, the entry module) receives the bootstrap-local slots; - cannot reach Filesystem, Network, host access, or thread creation beyond what the Context itself allows;
- receives capabilities only when Protos code passes them explicitly, for example as an argument to a function the module exports.
References
Implementation and evidence:
ProtosEmbeddedModules.java(public API and Javadoc);ProtosApplicationModuleResolver.java(exact catalog lookup in front of the Standard Library resolver);ProtosEmbeddedModulesTest.java(identity, cycles, retry, Actor-local instances, installation order, atomicity, shared Engines, authority);dist/Plat054EmbeddingProbe.java(external application,application-modulesmode).
Normative specification:
spec/semantics/MODULES.mdfor ModuleKey identity, Actor-local caching, cycles, failure, and host-initiated evaluations;spec/concurrency/ACTORS.md§8 forActor.spawnand the initial module of an Actor;spec/io/PROCESS_IO.mdfor embedding bootstrap and authority.
The app: spelling itself is host resolver policy of this embedding API, not a
Core language institution (see
chapter 6).