Plug-ins: a module with a manifest section that lists events must export vc_event

Changes on main needed the events code and its tests to follow them.

- A module that carries its manifest in the `vc_manifest`
  custom section is loaded without being instantiated, and its exports
  are checked by `check_exports`. That check now also requires
  `vc_event` with the type (param i32 i32) (result i32) when the
  manifest lists `events`, as the instantiated path already did. The
  `an_event_plugin_exports_vc_event` test covers a sectioned module
  without `vc_event`, with the wrong type and with the right one.
- JavaScript plug-ins have no `vc_event`, so their manifest may not
  list the permission `events`, next to `settings`, `cache` and
  `commands`. The JavaScript bundle test and docs/plugins.md say so.
- The craft UI framework port removed
  `VectorcraftApp::begin_host_iteration`, so the two UI-pass event
  tests no longer call it.
- On the framework, the whole window draws only on a context with the
  framework's runtime installed. `a_change_made_in_a_ui_pass_is_delivered_in_that_pass`
  drew it on a plain `egui::Context`, where the runtime panics ("the
  runtime plugin's hooks did not run for the previous pass") and the
  test did not finish. It now draws its passes with
  `testing::ui_context` and `testing::part_frame`, as the window tests
  on main do.
This commit is contained in:
dotCooCoo committed 2026-11-28 00:19:30 -08:00
1 parent 156fdce3f4
commit ab75ac0c13
7 files changed
+26 -13

No files matched your search

+2
View File
@@ -460,6 +460,8 @@ mod tests {
assert!(matches!(&e, Error::Manifest(why) if why.contains("`runtime` \"js\" is for an object filter")), "{e:?}");
let e = refused(bundle("test.js.settings", ok, json!({"api": 2, "permissions": ["settings"]})));
assert!(matches!(&e, Error::Manifest(why) if why.contains("`settings`")), "{e:?}");
let e = refused(bundle("test.js.events", ok, json!({"api": 2, "permissions": ["events"], "events": ["selection.changed"]})));
assert!(matches!(&e, Error::Manifest(why) if why.contains("can't list the permission `events`")), "{e:?}");
let e = refused(bundle("test.js.name", ok, json!({"script": "../x.js"})));
assert!(matches!(&e, Error::Manifest(why) if why.starts_with("`script`")), "{e:?}");
let e = refused(bundle("test.js.py", ok, json!({"runtime": "py"})));
+2 -2
View File
@@ -704,7 +704,7 @@ impl Manifest {
}
/// A JavaScript plug-in (`"runtime": "js"`) is an object filter, and of the host's permissions
/// it lists only `progress`: its realm has no settings, cache or command calls.
/// it lists only `progress`: its realm has no settings, cache, command calls or `vc_event`.
fn validate_runtime(&self) -> Result<()> {
if self.runtime == Runtime::Wasm {
return Ok(());
@@ -714,7 +714,7 @@ impl Manifest {
return Err(Error::Manifest(format!("`runtime` \"js\" is for an object filter (`kind` \"filter\"), not `kind` \"{kind}\"")));
}
let listed = self.permissions.as_deref().unwrap_or_default();
for p in [Permission::Settings, Permission::Cache, Permission::Commands] {
for p in [Permission::Settings, Permission::Cache, Permission::Commands, Permission::Events] {
if listed.iter().any(|n| n == p.name()) {
let name = p.name();
return Err(Error::Manifest(format!(
+7 -3
View File
@@ -322,8 +322,9 @@ fn check_tables(bytes: &[u8]) -> Result<()> {
}
/// Checks the exports a run needs without instantiating `module`: its memory as `memory`, whose
/// initial size fits in `max_memory_bytes`, and `vc_alloc` and `vc_run` with the ABI's types.
fn check_exports(module: &Module, max_memory_bytes: usize) -> Result<()> {
/// initial size fits in `max_memory_bytes`, and `vc_alloc` and `vc_run` with the ABI's types, and
/// `vc_event` with its type when `events` is set (the manifest lists events).
fn check_exports(module: &Module, max_memory_bytes: usize, events: bool) -> Result<()> {
use ValType::{I32, I64};
let export = |name: &str| module.exports().find(|e| e.name() == name).map(|e| e.ty().clone());
let Some(ExternType::Memory(memory)) = export("memory") else {
@@ -341,6 +342,9 @@ fn check_exports(module: &Module, max_memory_bytes: usize) -> Result<()> {
return Err(Error::Abi(format!("the module must export `{name}` with the type {signature}")));
}
}
if events && !matches!(export("vc_event"), Some(ExternType::Func(f)) if f.params() == [I32, I32] && f.results() == [I32]) {
return Err(Error::Abi("the module lists `events`, so it must export `vc_event` with the type (param i32 i32) (result i32)".into()));
}
Ok(())
}
@@ -428,7 +432,7 @@ impl Plugin {
}
}
None => {
check_exports(&module, p.limits.max_memory_bytes)?;
check_exports(&module, p.limits.max_memory_bytes, !p.manifest.events.is_empty())?;
check_tables(bytes)?;
}
}
+10
View File
@@ -1264,6 +1264,16 @@ fn an_event_plugin_exports_vc_event() {
assert!(e.to_string().contains("vc_event"), "{e}");
assert!(Plugin::load(&build(&tpl::reacting(&manifest, tpl::HOST_IMPORTS, tpl::LOG_EVENT)), Limits::default()).is_ok());
assert!(Plugin::load(&ok_module(tpl::ECHO), Limits::default()).is_ok(), "no events, no vc_event");
// A module whose manifest is in its custom section is checked without being instantiated.
let section =
r#"{"id":"test.events.section","name":"T","kind":"filter","api":2,"permissions":["events"],"events":["selection.changed"],"abi":1}"#;
let e = Plugin::load(&build(&tpl::sectioned(section, tpl::ECHO, "", 1)), Limits::default()).unwrap_err();
assert!(e.to_string().contains("vc_event"), "{e}");
let one_param = r#"(func (export "vc_event") (param i32) (result i32) (i32.const 0))"#;
let e = Plugin::load(&build(&tpl::sectioned(section, tpl::ECHO, one_param, 1)), Limits::default()).unwrap_err();
assert!(e.to_string().contains("vc_event"), "{e}");
let handler = r#"(func (export "vc_event") (param i32 i32) (result i32) (i32.const 0))"#;
assert!(Plugin::load(&build(&tpl::sectioned(section, tpl::ECHO, handler, 1)), Limits::default()).is_ok());
}
/// `vc_event` is given the event JSON in its own memory and returns 0; anything else, a trap and a
+3 -5
View File
@@ -838,20 +838,18 @@ fn a_change_made_in_a_ui_pass_is_delivered_in_that_pass() {
app.run("plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&wasm), "grant": grant, "replace": true})).unwrap();
let r = app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap()["id"].clone();
app.run("select.none", json!({})).unwrap();
let ctx = egui::Context::default();
let ctx = crate::testing::ui_context();
let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1200.0, 800.0));
let pass = |app: &mut VectorcraftApp, select: bool| {
app.begin_host_iteration();
let mut out = ctx.run_ui(egui::RawInput { screen_rect: Some(screen), ..Default::default() }, |ui| {
crate::testing::part_frame(app, &ctx, egui::RawInput { screen_rect: Some(screen), ..Default::default() }, |app, ui| {
app.tick(ui.ctx());
if select {
app.session.execute("select.set", &json!({"ids": [r]})).unwrap();
}
app.ui(ui);
});
out.textures_delta.clear();
};
// Fonts install on the first pass and are ready on the second.
// The window lays itself out in the first two passes.
pass(&mut app, false);
pass(&mut app, false);
pass(&mut app, true);
-1
View File
@@ -343,7 +343,6 @@ fn a_finished_run_delivers_its_plugin_events_in_the_frame_it_ends() {
if !app.session.script_running() {
break;
}
app.begin_host_iteration();
let mut out = ctx.run_ui(egui::RawInput::default(), |ui| app.tick(ui.ctx()));
out.textures_delta.clear();
}
+2 -2
View File
@@ -1308,8 +1308,8 @@ function run(input, params) {
The manifest has the keys of a module's [manifest](#manifest), with `"runtime": "js"` and `script`, a file name of 1 to
128 bytes ending in `.js`, without a folder or `:`, and not starting with `.`. Left out, `runtime` is WebAssembly, so
existing modules load as before; a module's own manifest may not give `"js"`. A JavaScript plug-in is a filter (`kind`
`"filter"`), and of the host's [permissions](#permissions) it may list `progress` but not `settings`, `cache` or
`commands`, so it has no `commandAllowlist`.
`"filter"`), and of the host's [permissions](#permissions) it may list `progress` but not `settings`, `cache`,
`commands` or `events`, so it has no `commandAllowlist` and no `events`.
`plugin.install {path}` and `plugin.inspect {path}` take the `.vcplugin` file, and File › Open and `plugin.reload`
install it like a `.wasm` file. The host refuses a manifest without `script`, with `source`, or with a `runtime` other