refactor(app): cut liboakengine, link module rlibs directly (M14 R3)

- real.rs rewritten over module Rust APIs (Arc<Mutex<Project>> +
  NodeId; the addref handle dance and renderer boxes are gone);
  AppEngine trait and all panels untouched
- new app assembly layers: graphops (project/timeline/edit
  primitives), effectchain (chain composition with undo groups),
  renderops (montage build + ticket render + ExportTask export),
  library via oakstorage directly
- module-side safe API additions: oakundo global value-semantic
  push/undo/redo + from_closures, oakstorage project_arc_of
- deleted: src/oakui/ffi.rs, src/oakui/host_syms.rs, the dylib link
  config in build.rs (only the gpui IOSurface framework link remains)
- the binary carries zero liboakengine references (otool/nm verified);
  101 app tests green incl. the real-render and full-res e2e tests
- behavior improvements for free: sequences land in the project graph
  (the facade scratch-project deviation is gone), footage drops take
  one undo record, effect remove/reorder undo restores edges
This commit is contained in:
2026-08-16 23:36:43 +08:00
parent a45a7af2ac
commit 022c0a7a5a
22 changed files with 4345 additions and 4784 deletions
+3 -1
View File
@@ -103,7 +103,9 @@ pub unsafe extern "C" fn oakengine_undo_push(command: *mut c_void, name: *const
/// `oakengine_undo_group_begin` — start collecting commands into a group.
#[no_mangle]
pub extern "C" fn oakengine_undo_group_begin(name: *const c_char) -> c_int {
guard(|| oakundo::global::group_begin(name).map_err(map_group_err))
guard(|| unsafe {
oakundo::global::group_begin(&crate::handle::read_cstr(name)).map_err(map_group_err)
})
}
/// `oakengine_undo_group_end` — close the group and push it as one entry.
+26
View File
@@ -35,6 +35,32 @@ pub fn make_project_owned(project: ProjectArc) -> CHandle {
oaknode::handle::make_owned(project)
}
/// Release one owned reference of a project handle produced by
/// [`make_project_owned`] (or the database backend's load). The handle is
/// dead afterwards; the write-through binding keeps its own reference, so
/// releasing the caller's copy never tears a bound project down.
pub fn release_project(mut h: CHandle) {
if let Some(release) = h.release {
// SAFETY: `h` is an owned handle from this module; the release runs
// the box's own destructor once per owned reference.
unsafe { release(h.ctx) };
}
h.ctx = std::ptr::null_mut();
}
/// The boxed project of a handle produced by [`make_project_owned`] or the
/// database backend's load (both box a `ProjectArc`). `None` for an empty
/// handle.
///
/// The handle ABI carries no type tag, so the caller must only pass handles
/// from those two producers; the unsafe downcast is contained here instead
/// of spread across every direct-rlib consumer.
pub fn project_arc_of(h: &CHandle) -> Option<ProjectArc> {
// SAFETY: the documented precondition — handles from this module's
// producers box a `ProjectArc`.
unsafe { oaknode::handle::get::<ProjectArc>(h) }.cloned()
}
/// Read the boxed project of a project handle.
///
/// # Safety
+111 -19
View File
@@ -31,8 +31,9 @@ use std::sync::{Mutex, OnceLock};
use crate::error::{Error, Result};
use crate::handle::CHandle;
use crate::undocommand::{
command_free, command_init_multi, command_multi_add_child, command_multi_child,
command_multi_child_count, command_redo_now, command_undo_now,
command_free, command_from_owned, command_init_multi, command_multi_add_child,
command_multi_child, command_multi_child_count, command_redo_now, command_undo_now,
UndoCommand,
};
use crate::undostack::{
undostack_can_redo, undostack_can_undo, undostack_clear, undostack_command_is_done,
@@ -60,18 +61,6 @@ fn stack() -> CHandle {
*global_stack()
}
/// Read a NUL-terminated C string; `NULL` yields an empty string.
fn read_name(name: *const c_char) -> String {
if name.is_null() {
String::new()
} else {
// SAFETY: the caller guarantees a valid NUL-terminated string.
unsafe { std::ffi::CStr::from_ptr(name) }
.to_string_lossy()
.into_owned()
}
}
// ---------------------------------------------------------------------------
// Command-success observers
// ---------------------------------------------------------------------------
@@ -127,7 +116,7 @@ fn group_lock() -> std::sync::MutexGuard<'static, Option<OpenGroup>> {
/// Start collecting commands into a group. [`Error::State`] when a group
/// is already open.
pub fn group_begin(name: *const c_char) -> Result<()> {
pub fn group_begin(name: &str) -> Result<()> {
let mut g = group_lock();
if g.is_some() {
return Err(Error::State);
@@ -138,7 +127,7 @@ pub fn group_begin(name: *const c_char) -> Result<()> {
}
*g = Some(OpenGroup {
multi,
name: read_name(name),
name: name.to_string(),
});
Ok(())
}
@@ -259,6 +248,61 @@ pub fn push_or_run(command: CHandle, name: &str) -> c_int {
rc
}
// ---------------------------------------------------------------------------
// Safe value-typed surface (M14 R3)
//
// The direct-rlib frontends (the app) hold module [`UndoCommand`] values,
// not CHandles. The functions below are the safe twins of the handle-based
// API above; the handle marshalling they perform stays inside this crate.
// ---------------------------------------------------------------------------
/// Push `command` onto the process-wide stack (redo then record), or into
/// the open group. On a stack record the command observers fire (the
/// oakstorage write-through persists the edit).
pub fn push(command: UndoCommand, name: &str) -> Result<()> {
// SAFETY: `command_from_owned` boxes the value; `push_or_run` takes the
// value out of the handle (stack push or group child), and
// `command_free` releases the remaining non-owning shell (dropping the
// command itself when nobody took it).
let mut handle = unsafe { command_from_owned(command) };
if handle.is_null() {
return Err(Error::NoMem);
}
let rc = push_or_run(handle, name);
command_free(&mut handle);
if rc == 0 {
Ok(())
} else {
Err(Error::from_code(rc))
}
}
/// Step the process-wide stack back one entry (no-op at the bottom). On
/// success the command observers fire, persisting the reverted state.
pub fn undo() -> Result<()> {
let i = index()?;
jump(i - 1)
}
/// Step the process-wide stack forward one entry (no-op at the top). On
/// success the command observers fire.
pub fn redo() -> Result<()> {
let i = index()?;
jump(i + 1)
}
/// Whether the process-wide stack has an entry to undo.
pub fn undoable() -> bool {
let mut v: c_int = 0;
can_undo(&mut v).is_ok() && v != 0
}
/// Whether the process-wide stack has an entry to redo.
pub fn redoable() -> bool {
let mut v: c_int = 0;
can_redo(&mut v).is_ok() && v != 0
}
// ---------------------------------------------------------------------------
// Stack queries and mutations
// ---------------------------------------------------------------------------
@@ -358,6 +402,13 @@ mod tests {
COUNT.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
}
/// A second counting observer (the observers are process-lifetime, so a
/// test must not share `COUNT` with the lifecycle test above).
static COUNT2: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);
fn counter2() {
COUNT2.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
}
/// The stack/group/observer state is process-wide: every test here runs
/// serially under this lock, mirroring the facade's GLOBAL_STACK_LOCK
/// pattern.
@@ -395,8 +446,8 @@ mod tests {
assert_eq!(COUNT.load(std::sync::atomic::Ordering::SeqCst), 1);
// Group begin/end fires the observer once at end.
assert!(group_begin(c"grouped".as_ptr()).is_ok());
assert!(group_begin(c"again".as_ptr()).is_err()); // State
assert!(group_begin("grouped").is_ok());
assert!(group_begin("again").is_err()); // State
let c1 = vtable_command();
let c2 = vtable_command();
assert_eq!(push_or_run(c1, "c1"), 0);
@@ -409,7 +460,7 @@ mod tests {
assert_eq!(COUNT.load(std::sync::atomic::Ordering::SeqCst), 2);
// Abort fires nothing.
assert!(group_begin(c"abort".as_ptr()).is_ok());
assert!(group_begin("abort").is_ok());
let c3 = vtable_command();
assert_eq!(push_or_run(c3, "c3"), 0);
assert!(group_abort().is_ok());
@@ -422,4 +473,45 @@ mod tests {
assert!(clear().is_ok());
}
/// The safe value-typed surface (M14 R3): `push` redoes and records a
/// closure command, `undo`/`redo` step the stack, and the observers
/// fire on every recorded mutation.
#[test]
fn value_push_and_undo_redo() {
let _g = LOCK.lock().unwrap_or_else(|e| e.into_inner());
COUNT2.store(0, std::sync::atomic::Ordering::SeqCst);
add_observer(counter2);
assert!(clear().is_ok());
assert!(!undoable());
assert!(!redoable());
let state = std::sync::Arc::new(std::sync::atomic::AtomicI32::new(0));
let (r, u) = (state.clone(), state.clone());
let cmd = UndoCommand::from_closures(
move || {
r.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
},
move || {
u.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
},
);
push(cmd, "bump").unwrap();
assert_eq!(state.load(std::sync::atomic::Ordering::SeqCst), 1);
assert!(undoable());
assert_eq!(COUNT2.load(std::sync::atomic::Ordering::SeqCst), 1);
undo().unwrap();
assert_eq!(state.load(std::sync::atomic::Ordering::SeqCst), 0);
assert!(!undoable());
assert!(redoable());
redo().unwrap();
assert_eq!(state.load(std::sync::atomic::Ordering::SeqCst), 1);
assert!(undoable());
assert!(!redoable());
assert!(clear().is_ok());
}
}
+69
View File
@@ -134,6 +134,28 @@ impl UndoCommand {
}
}
/// New closure-backed command (M14 R3): `redo`/`undo` are plain Rust
/// closures, boxed behind the vtable machinery. This is the safe way
/// for direct-rlib consumers (the app) to build composite edit
/// commands without writing their own `extern "C"` trampolines.
pub fn from_closures(
redo: impl FnMut() + Send + 'static,
undo: impl FnMut() + Send + 'static,
) -> Self {
let state = Box::into_raw(Box::new(ClosureCommand {
redo: Box::new(redo),
undo: Box::new(undo),
}));
UndoCommand::from_vtable(
OakUndoCommandVtable {
redo: Some(closure_redo),
undo: Some(closure_undo),
free_fn: Some(closure_free),
},
state as *mut c_void,
)
}
/// The invariant bottom-of-stack command ("New/Open Project"); all
/// callbacks are no-ops and it is never undoable.
pub(crate) fn empty() -> Self {
@@ -283,6 +305,53 @@ impl Drop for UndoCommand {
}
}
/// Closure-backed command state (see [`UndoCommand::from_closures`]).
struct ClosureCommand {
/// The redo closure.
redo: Box<dyn FnMut() + Send>,
/// The undo closure.
undo: Box<dyn FnMut() + Send>,
}
/// `redo` trampoline for closure commands.
///
/// # Safety
/// `userdata` must be the `Box<ClosureCommand>` produced by
/// [`UndoCommand::from_closures`]; the owning command keeps it alive.
unsafe extern "C" fn closure_redo(userdata: *mut c_void) {
if userdata.is_null() {
return;
}
// SAFETY: per the from_closures contract; the command owns the box.
let state = unsafe { &mut *(userdata as *mut ClosureCommand) };
(state.redo)();
}
/// `undo` trampoline for closure commands.
///
/// # Safety
/// As [`closure_redo`].
unsafe extern "C" fn closure_undo(userdata: *mut c_void) {
if userdata.is_null() {
return;
}
// SAFETY: per the from_closures contract; the command owns the box.
let state = unsafe { &mut *(userdata as *mut ClosureCommand) };
(state.undo)();
}
/// `free` trampoline for closure commands: drops the boxed closures.
///
/// # Safety
/// As [`closure_redo`]; called at most once (the command's final release).
unsafe extern "C" fn closure_free(userdata: *mut c_void) {
if userdata.is_null() {
return;
}
// SAFETY: per the from_closures contract; this is the final release.
unsafe { drop(Box::from_raw(userdata as *mut ClosureCommand)) };
}
/// `olive::MultiUndoCommand` — a composite of child commands.
///
/// `redo` runs children in order; `undo` runs them in reverse. Owns one