@mun-ui/ui 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +114 -2
- package/bin/compile.mjs +38 -0
- package/bin/desktop.mjs +143 -0
- package/bin/dev-protocol.mjs +94 -0
- package/bin/formatter.mjs +109 -0
- package/bin/hot-reload.mjs +38 -0
- package/bin/mun.mjs +538 -0
- package/bin/native.mjs +133 -0
- package/bin/pnpm-workspace.mjs +58 -0
- package/bin/program-patch.mjs +104 -0
- package/bin/project.mjs +322 -0
- package/bin/watch.mjs +316 -0
- package/bin/workflow.mjs +261 -0
- package/dist/astro.d.ts +3 -0
- package/dist/astro.d.ts.map +1 -0
- package/dist/astro.js +3 -0
- package/dist/astro.js.map +1 -0
- package/dist/closures.d.ts +2 -0
- package/dist/closures.d.ts.map +1 -0
- package/dist/closures.js +2 -0
- package/dist/closures.js.map +1 -0
- package/dist/compiler/index.d.ts +2 -0
- package/dist/compiler/index.d.ts.map +1 -0
- package/dist/compiler/index.js +2 -0
- package/dist/compiler/index.js.map +1 -0
- package/dist/core.d.ts +3 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +3 -0
- package/dist/core.js.map +1 -0
- package/dist/experimental.d.ts +2 -0
- package/dist/experimental.d.ts.map +1 -0
- package/dist/experimental.js +2 -0
- package/dist/experimental.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/jsx-dev-runtime.d.ts +2 -0
- package/dist/jsx-dev-runtime.d.ts.map +1 -0
- package/dist/jsx-dev-runtime.js +2 -0
- package/dist/jsx-dev-runtime.js.map +1 -0
- package/dist/jsx-runtime.d.ts +2 -0
- package/dist/jsx-runtime.d.ts.map +1 -0
- package/dist/jsx-runtime.js +2 -0
- package/dist/jsx-runtime.js.map +1 -0
- package/dist/legacy.d.ts +3 -0
- package/dist/legacy.d.ts.map +1 -0
- package/dist/legacy.js +3 -0
- package/dist/legacy.js.map +1 -0
- package/dist/mun.d.ts +5 -0
- package/dist/mun.d.ts.map +1 -0
- package/dist/mun.js +4 -0
- package/dist/mun.js.map +1 -0
- package/dist/progressive-blur/index.d.ts +8 -0
- package/dist/progressive-blur/index.d.ts.map +1 -0
- package/dist/progressive-blur/index.js +8 -0
- package/dist/progressive-blur/index.js.map +1 -0
- package/dist/progressive-blur/plan.d.ts +3 -0
- package/dist/progressive-blur/plan.d.ts.map +1 -0
- package/dist/progressive-blur/plan.js +3 -0
- package/dist/progressive-blur/plan.js.map +1 -0
- package/dist/react-types.d.ts +2 -0
- package/dist/react-types.d.ts.map +1 -0
- package/dist/react-types.js +2 -0
- package/dist/react-types.js.map +1 -0
- package/dist/rendering-quality.d.ts +4 -0
- package/dist/rendering-quality.d.ts.map +1 -0
- package/dist/rendering-quality.js +4 -0
- package/dist/rendering-quality.js.map +1 -0
- package/dist/state.d.ts +2 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +2 -0
- package/dist/state.js.map +1 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/vite.d.ts +2 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +2 -0
- package/dist/vite.js.map +1 -0
- package/editors/install.mjs +102 -0
- package/editors/lsp/mun-lsp.mjs +123 -0
- package/editors/lsp/service.mjs +306 -0
- package/editors/lsp/source.mjs +58 -0
- package/editors/vscode/client.mjs +40 -0
- package/editors/vscode/dev-client.mjs +83 -0
- package/editors/vscode/discovery.mjs +46 -0
- package/editors/vscode/export.mjs +42 -0
- package/editors/vscode/extension.cjs +226 -0
- package/editors/vscode/language-configuration.json +15 -0
- package/editors/vscode/package.json +119 -0
- package/editors/vscode/protocol.mjs +29 -0
- package/editors/vscode/syntaxes/mun.tmLanguage.json +130 -0
- package/native/Cargo.lock +3581 -0
- package/native/Cargo.toml +21 -0
- package/native/README.md +21 -0
- package/native/mun-native/Cargo.toml +23 -0
- package/native/mun-native/src/accessibility.rs +738 -0
- package/native/mun-native/src/dev.rs +604 -0
- package/native/mun-native/src/host_services.rs +202 -0
- package/native/mun-native/src/lib.rs +2843 -0
- package/native/mun-native/src/main.rs +204 -0
- package/native/mun-native/src/offscreen.rs +393 -0
- package/native/mun-native/src/rect.wgsl +63 -0
- package/native/mun-native/src/text.rs +401 -0
- package/native/mun-native/tests/offscreen_rendering.rs +161 -0
- package/native/mun-native/tests/stress.rs +408 -0
- package/native/mun-native/tests/window_lifecycle.rs +183 -0
- package/native/mun-runtime/Cargo.toml +13 -0
- package/native/mun-runtime/src/accessibility.rs +193 -0
- package/native/mun-runtime/src/collection.rs +1090 -0
- package/native/mun-runtime/src/gesture.rs +1070 -0
- package/native/mun-runtime/src/input.rs +325 -0
- package/native/mun-runtime/src/ir.rs +852 -0
- package/native/mun-runtime/src/kinetics.rs +500 -0
- package/native/mun-runtime/src/layout.rs +258 -0
- package/native/mun-runtime/src/layout_cache.rs +209 -0
- package/native/mun-runtime/src/lib.rs +52 -0
- package/native/mun-runtime/src/motion.rs +1162 -0
- package/native/mun-runtime/src/retained.rs +574 -0
- package/native/mun-runtime/src/runtime.rs +7449 -0
- package/native/mun-runtime/src/scene.rs +567 -0
- package/native/mun-runtime/src/scroll.rs +521 -0
- package/native/mun-runtime/src/scroll_view.rs +220 -0
- package/native/mun-runtime/src/text_edit.rs +670 -0
- package/native/mun-runtime/src/timeline.rs +1534 -0
- package/native/mun-runtime/src/validate.rs +677 -0
- package/native/mun-runtime/tests/accessibility_semantics.rs +168 -0
- package/native/mun-runtime/tests/compiler_contract.rs +110 -0
- package/native/mun-runtime/tests/flexible_frames.rs +47 -0
- package/native/mun-runtime/tests/host_services.rs +107 -0
- package/native/mun-runtime/tests/hot_update.rs +367 -0
- package/native/mun-runtime/tests/incremental_runtime.rs +323 -0
- package/native/mun-runtime/tests/ir_validation.rs +167 -0
- package/native/mun-runtime/tests/keyed_collections.rs +251 -0
- package/native/mun-runtime/tests/minimum_window_size.rs +80 -0
- package/native/mun-runtime/tests/native_controls_style.rs +491 -0
- package/native/mun-runtime/tests/native_demo.rs +285 -0
- package/native/mun-runtime/tests/native_layout_style.rs +175 -0
- package/native/mun-runtime/tests/native_settings_pane.rs +152 -0
- package/native/mun-runtime/tests/priority_proposals.rs +69 -0
- package/native/mun-runtime/tests/scroll_view.rs +403 -0
- package/native/mun-runtime/tests/text_editing.rs +491 -0
- package/native/mun-runtime/tests/text_proposals.rs +42 -0
- package/native/mun-runtime/tests/typography.rs +63 -0
- package/package.json +213 -3
- package/schemas/README.md +19 -0
- package/schemas/semantic-ui-ir-v1.schema.json +2061 -0
- package/templates/desktop/gitignore +5 -0
- package/templates/desktop/package.json +10 -0
- package/templates/desktop/src-tauri/Cargo.toml +14 -0
- package/templates/desktop/src-tauri/build.rs +1 -0
- package/templates/desktop/src-tauri/capabilities/main.json +12 -0
- package/templates/desktop/src-tauri/icons/icon.icns +0 -0
- package/templates/desktop/src-tauri/icons/icon.ico +0 -0
- package/templates/desktop/src-tauri/icons/icon.png +0 -0
- package/templates/desktop/src-tauri/src/main.rs +2 -0
- package/templates/desktop/src-tauri/tauri.conf.json +42 -0
- package/templates/desktop/vite.config.ts +10 -0
- package/templates/native/App.mun +15 -0
- package/templates/native/gitignore +3 -0
- package/templates/native/mun.toml +8 -0
- package/templates/project/gitignore +5 -0
- package/templates/project/index.html +13 -0
- package/templates/project/package.json +24 -0
- package/templates/project/src/App.mun +13 -0
- package/templates/project/src/index.css +30 -0
- package/templates/project/src/main.ts +5 -0
- package/templates/project/tsconfig.json +20 -0
- package/templates/project/vite.config.ts +6 -0
- package/templates/vite/src/App.css +134 -0
- package/templates/vite/src/App.tsx +91 -0
- package/templates/vite/src/index.css +23 -0
- package/templates/vite/vite.config.ts +10 -0
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
//! Development-only link between `mun dev` and a running native host.
|
|
2
|
+
//!
|
|
3
|
+
//! Transport: one loopback TCP connection the host opens to the toolchain,
|
|
4
|
+
//! framed as a 4-byte big-endian length followed by a UTF-8 JSON message.
|
|
5
|
+
//! Frames are bounded; the first message is a versioned, token-authenticated
|
|
6
|
+
//! hello. Production launches never construct a [`DevLink`]: the host enables
|
|
7
|
+
//! it only for the explicit `--dev` invocation.
|
|
8
|
+
|
|
9
|
+
use std::{
|
|
10
|
+
io::{self, Read, Write},
|
|
11
|
+
net::{SocketAddr, TcpStream},
|
|
12
|
+
sync::{Arc, Mutex},
|
|
13
|
+
thread,
|
|
14
|
+
time::Instant,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
use mun_runtime::{HotUpdateReport, HotUpdateTimings, Runtime};
|
|
18
|
+
use serde_json::{Value, json};
|
|
19
|
+
use winit::event_loop::EventLoopProxy;
|
|
20
|
+
|
|
21
|
+
use crate::accessibility::NativeEvent;
|
|
22
|
+
|
|
23
|
+
pub const DEV_PROTOCOL_VERSION: u64 = 2;
|
|
24
|
+
/// Largest accepted frame. Semantic programs are far smaller; anything larger
|
|
25
|
+
/// is a protocol error, not a reason to allocate.
|
|
26
|
+
pub const MAX_FRAME_BYTES: usize = 16 * 1024 * 1024;
|
|
27
|
+
|
|
28
|
+
/// When and at what cost a dev frame arrived (set by the reader thread).
|
|
29
|
+
#[derive(Clone, Copy, Debug)]
|
|
30
|
+
pub struct Received {
|
|
31
|
+
pub at: Instant,
|
|
32
|
+
/// JSON decode of the frame body, in microseconds.
|
|
33
|
+
pub decode_micros: u64,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
impl Received {
|
|
37
|
+
pub fn now() -> Self {
|
|
38
|
+
Self {
|
|
39
|
+
at: Instant::now(),
|
|
40
|
+
decode_micros: 0,
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/// A full program or a JSON path patch against the host's current program.
|
|
46
|
+
#[derive(Clone, Debug)]
|
|
47
|
+
pub enum UpdateBody {
|
|
48
|
+
Full(Value),
|
|
49
|
+
Patch(Vec<Value>),
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
#[derive(Clone, Debug)]
|
|
53
|
+
pub enum DevCommand {
|
|
54
|
+
Update {
|
|
55
|
+
id: u64,
|
|
56
|
+
base_revision: u64,
|
|
57
|
+
revision: u64,
|
|
58
|
+
body: UpdateBody,
|
|
59
|
+
preserve: Vec<String>,
|
|
60
|
+
received: Received,
|
|
61
|
+
},
|
|
62
|
+
Inspect {
|
|
63
|
+
id: u64,
|
|
64
|
+
include_values: bool,
|
|
65
|
+
},
|
|
66
|
+
Disconnected(Option<String>),
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
fn micros(started: Instant) -> u64 {
|
|
70
|
+
u64::try_from(started.elapsed().as_micros()).unwrap_or(u64::MAX)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/// Host-side cost of applying one dev update, in microseconds.
|
|
74
|
+
#[derive(Clone, Copy, Debug, Default)]
|
|
75
|
+
pub struct UpdateTimings {
|
|
76
|
+
pub decode_micros: u64,
|
|
77
|
+
/// From frame receipt to the event loop starting to apply it.
|
|
78
|
+
pub queue_micros: u64,
|
|
79
|
+
/// Reconstructing the full program from a patch.
|
|
80
|
+
pub patch_micros: u64,
|
|
81
|
+
pub runtime: HotUpdateTimings,
|
|
82
|
+
/// Patch reconstruction plus `Runtime::hot_update`.
|
|
83
|
+
pub apply_micros: u64,
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
impl UpdateTimings {
|
|
87
|
+
pub fn to_json(self) -> Value {
|
|
88
|
+
json!({
|
|
89
|
+
"decodeMicros": self.decode_micros,
|
|
90
|
+
"queueMicros": self.queue_micros,
|
|
91
|
+
"patchMicros": self.patch_micros,
|
|
92
|
+
"loadMicros": self.runtime.load_micros,
|
|
93
|
+
"materializeMicros": self.runtime.materialize_micros,
|
|
94
|
+
"reconcileMicros": self.runtime.reconcile_micros,
|
|
95
|
+
"applyMicros": self.apply_micros,
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/// Why an update was not applied; the host's program and revision are unchanged.
|
|
101
|
+
#[derive(Clone, Debug)]
|
|
102
|
+
pub struct UpdateRejection {
|
|
103
|
+
pub code: &'static str,
|
|
104
|
+
pub message: String,
|
|
105
|
+
pub current_revision: u64,
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/// The host's development view of its program: the JSON it last committed
|
|
109
|
+
/// and its revision. Both the windowed host and headless measurement apply
|
|
110
|
+
/// updates through [`DevProgram::apply`], so they share one code path.
|
|
111
|
+
pub struct DevProgram {
|
|
112
|
+
program: Value,
|
|
113
|
+
revision: u64,
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
impl DevProgram {
|
|
117
|
+
pub fn new(program: Value) -> Self {
|
|
118
|
+
Self {
|
|
119
|
+
program,
|
|
120
|
+
revision: 0,
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
pub fn revision(&self) -> u64 {
|
|
125
|
+
self.revision
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/// Check the revision, reconstruct the program and hot-update `runtime`.
|
|
129
|
+
/// Nothing is committed unless the runtime accepts the new program.
|
|
130
|
+
pub fn apply(
|
|
131
|
+
&mut self,
|
|
132
|
+
runtime: &mut Runtime,
|
|
133
|
+
base_revision: u64,
|
|
134
|
+
revision: u64,
|
|
135
|
+
body: UpdateBody,
|
|
136
|
+
preserve: &[String],
|
|
137
|
+
received: Received,
|
|
138
|
+
) -> Result<(HotUpdateReport, UpdateTimings), UpdateRejection> {
|
|
139
|
+
let mut timings = UpdateTimings {
|
|
140
|
+
decode_micros: received.decode_micros,
|
|
141
|
+
queue_micros: micros(received.at),
|
|
142
|
+
..UpdateTimings::default()
|
|
143
|
+
};
|
|
144
|
+
let reject = |code, message| UpdateRejection {
|
|
145
|
+
code,
|
|
146
|
+
message,
|
|
147
|
+
current_revision: self.revision,
|
|
148
|
+
};
|
|
149
|
+
if base_revision != self.revision || revision != base_revision.saturating_add(1) {
|
|
150
|
+
return Err(reject(
|
|
151
|
+
"revision-mismatch",
|
|
152
|
+
format!(
|
|
153
|
+
"dev revision mismatch: host is {}, update is {base_revision} -> {revision}",
|
|
154
|
+
self.revision
|
|
155
|
+
),
|
|
156
|
+
));
|
|
157
|
+
}
|
|
158
|
+
let started = Instant::now();
|
|
159
|
+
let program = match body {
|
|
160
|
+
UpdateBody::Full(program) => program,
|
|
161
|
+
UpdateBody::Patch(operations) => apply_program_patch(&self.program, &operations)
|
|
162
|
+
.map_err(|message| reject("patch-invalid", message))?,
|
|
163
|
+
};
|
|
164
|
+
timings.patch_micros = micros(started);
|
|
165
|
+
let report = runtime
|
|
166
|
+
.hot_update_value(&program, preserve)
|
|
167
|
+
.map_err(|error| reject("runtime-rejected", error.to_string()))?;
|
|
168
|
+
timings.runtime = report.timings;
|
|
169
|
+
timings.apply_micros = micros(started);
|
|
170
|
+
self.program = program;
|
|
171
|
+
self.revision = revision;
|
|
172
|
+
Ok((report, timings))
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/// [`read_frame`], stamped with arrival time and the JSON decode cost.
|
|
177
|
+
fn read_frame_timed(reader: &mut impl Read) -> io::Result<Option<(Value, Received)>> {
|
|
178
|
+
let mut length = [0; 4];
|
|
179
|
+
match reader.read_exact(&mut length) {
|
|
180
|
+
Ok(()) => {}
|
|
181
|
+
Err(error) if error.kind() == io::ErrorKind::UnexpectedEof => return Ok(None),
|
|
182
|
+
Err(error) => return Err(error),
|
|
183
|
+
}
|
|
184
|
+
let body = read_body(reader, u32::from_be_bytes(length) as usize)?;
|
|
185
|
+
let started = Instant::now();
|
|
186
|
+
let message = serde_json::from_slice(&body)
|
|
187
|
+
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
|
|
188
|
+
let received = Received {
|
|
189
|
+
decode_micros: micros(started),
|
|
190
|
+
at: Instant::now(),
|
|
191
|
+
};
|
|
192
|
+
Ok(Some((message, received)))
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
fn read_body(reader: &mut impl Read, length: usize) -> io::Result<Vec<u8>> {
|
|
196
|
+
if length > MAX_FRAME_BYTES {
|
|
197
|
+
return Err(io::Error::new(
|
|
198
|
+
io::ErrorKind::InvalidData,
|
|
199
|
+
format!("dev frame of {length} bytes exceeds {MAX_FRAME_BYTES}"),
|
|
200
|
+
));
|
|
201
|
+
}
|
|
202
|
+
let mut body = vec![0; length];
|
|
203
|
+
reader.read_exact(&mut body)?;
|
|
204
|
+
Ok(body)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
pub fn read_frame(reader: &mut impl Read) -> io::Result<Option<Value>> {
|
|
208
|
+
let mut length = [0; 4];
|
|
209
|
+
match reader.read_exact(&mut length) {
|
|
210
|
+
Ok(()) => {}
|
|
211
|
+
Err(error) if error.kind() == io::ErrorKind::UnexpectedEof => return Ok(None),
|
|
212
|
+
Err(error) => return Err(error),
|
|
213
|
+
}
|
|
214
|
+
let body = read_body(reader, u32::from_be_bytes(length) as usize)?;
|
|
215
|
+
serde_json::from_slice(&body)
|
|
216
|
+
.map(Some)
|
|
217
|
+
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
pub fn write_frame(writer: &mut impl Write, message: &Value) -> io::Result<()> {
|
|
221
|
+
let body = serde_json::to_vec(message)?;
|
|
222
|
+
if body.len() > MAX_FRAME_BYTES {
|
|
223
|
+
return Err(io::Error::new(
|
|
224
|
+
io::ErrorKind::InvalidData,
|
|
225
|
+
"dev frame too large",
|
|
226
|
+
));
|
|
227
|
+
}
|
|
228
|
+
writer.write_all(&(body.len() as u32).to_be_bytes())?;
|
|
229
|
+
writer.write_all(&body)?;
|
|
230
|
+
writer.flush()
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
fn revisions(message: &Value) -> Result<(u64, u64), String> {
|
|
234
|
+
let base_revision = message
|
|
235
|
+
.get("baseRevision")
|
|
236
|
+
.and_then(Value::as_u64)
|
|
237
|
+
.ok_or("dev update without baseRevision")?;
|
|
238
|
+
let revision = message
|
|
239
|
+
.get("revision")
|
|
240
|
+
.and_then(Value::as_u64)
|
|
241
|
+
.ok_or("dev update without revision")?;
|
|
242
|
+
if revision != base_revision.saturating_add(1) {
|
|
243
|
+
return Err(format!(
|
|
244
|
+
"dev revision must advance by one: {base_revision} -> {revision}"
|
|
245
|
+
));
|
|
246
|
+
}
|
|
247
|
+
Ok((base_revision, revision))
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
fn preserve_states(message: &Value) -> Vec<String> {
|
|
251
|
+
message
|
|
252
|
+
.get("preserve")
|
|
253
|
+
.and_then(Value::as_array)
|
|
254
|
+
.map(|items| {
|
|
255
|
+
items
|
|
256
|
+
.iter()
|
|
257
|
+
.filter_map(|item| item.as_str().map(str::to_owned))
|
|
258
|
+
.collect()
|
|
259
|
+
})
|
|
260
|
+
.unwrap_or_default()
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
fn patch_parent_mut<'a>(root: &'a mut Value, path: &[Value]) -> Result<&'a mut Value, String> {
|
|
264
|
+
let mut current = root;
|
|
265
|
+
for part in path {
|
|
266
|
+
current = match part {
|
|
267
|
+
Value::String(key) => current
|
|
268
|
+
.as_object_mut()
|
|
269
|
+
.and_then(|object| object.get_mut(key))
|
|
270
|
+
.ok_or_else(|| format!("patch object path is missing: {key}"))?,
|
|
271
|
+
Value::Number(index) => {
|
|
272
|
+
let index = index.as_u64().ok_or("patch array index must be unsigned")? as usize;
|
|
273
|
+
current
|
|
274
|
+
.as_array_mut()
|
|
275
|
+
.and_then(|array| array.get_mut(index))
|
|
276
|
+
.ok_or_else(|| format!("patch array index out of range: {index}"))?
|
|
277
|
+
}
|
|
278
|
+
_ => return Err("patch path segments must be strings or array indices".into()),
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
Ok(current)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/// Apply a dev-only JSON path patch to a clone. The caller commits it only
|
|
285
|
+
/// after `Runtime::hot_update` accepts the reconstructed full program.
|
|
286
|
+
pub fn apply_program_patch(program: &Value, operations: &[Value]) -> Result<Value, String> {
|
|
287
|
+
let mut result = program.clone();
|
|
288
|
+
for operation in operations {
|
|
289
|
+
let kind = operation
|
|
290
|
+
.get("op")
|
|
291
|
+
.and_then(Value::as_str)
|
|
292
|
+
.ok_or("patch operation without op")?;
|
|
293
|
+
let path = operation
|
|
294
|
+
.get("path")
|
|
295
|
+
.and_then(Value::as_array)
|
|
296
|
+
.ok_or("patch operation without path")?;
|
|
297
|
+
if path.is_empty() {
|
|
298
|
+
if kind != "set" {
|
|
299
|
+
return Err("cannot remove the program root".into());
|
|
300
|
+
}
|
|
301
|
+
result = operation
|
|
302
|
+
.get("value")
|
|
303
|
+
.cloned()
|
|
304
|
+
.ok_or("root set without value")?;
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
let (parent_path, tail) = path.split_at(path.len() - 1);
|
|
308
|
+
let parent = patch_parent_mut(&mut result, parent_path)?;
|
|
309
|
+
match (kind, &tail[0]) {
|
|
310
|
+
("set", Value::String(key)) => {
|
|
311
|
+
let value = operation
|
|
312
|
+
.get("value")
|
|
313
|
+
.cloned()
|
|
314
|
+
.ok_or("patch set without value")?;
|
|
315
|
+
parent
|
|
316
|
+
.as_object_mut()
|
|
317
|
+
.ok_or("patch object parent is invalid")?
|
|
318
|
+
.insert(key.clone(), value);
|
|
319
|
+
}
|
|
320
|
+
("set", Value::Number(index)) => {
|
|
321
|
+
let index = index.as_u64().ok_or("patch array index must be unsigned")? as usize;
|
|
322
|
+
let array = parent
|
|
323
|
+
.as_array_mut()
|
|
324
|
+
.ok_or("patch array parent is invalid")?;
|
|
325
|
+
if index >= array.len() {
|
|
326
|
+
return Err(format!("patch array index out of range: {index}"));
|
|
327
|
+
}
|
|
328
|
+
array[index] = operation
|
|
329
|
+
.get("value")
|
|
330
|
+
.cloned()
|
|
331
|
+
.ok_or("patch set without value")?;
|
|
332
|
+
}
|
|
333
|
+
("remove", Value::String(key)) => {
|
|
334
|
+
let object = parent
|
|
335
|
+
.as_object_mut()
|
|
336
|
+
.ok_or("patch object parent is invalid")?;
|
|
337
|
+
if object.remove(key).is_none() {
|
|
338
|
+
return Err(format!("patch remove path is missing: {key}"));
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
("remove", Value::Number(_)) => {
|
|
342
|
+
return Err(
|
|
343
|
+
"array element removal is unsupported; replace the array atomically".into(),
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
(_, _) if kind != "set" && kind != "remove" => {
|
|
347
|
+
return Err(format!("unsupported patch operation: {kind}"));
|
|
348
|
+
}
|
|
349
|
+
_ => return Err("patch path segments must be strings or array indices".into()),
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
Ok(result)
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
pub fn parse_command(message: Value, received: Received) -> Result<DevCommand, String> {
|
|
356
|
+
let id = message.get("id").and_then(Value::as_u64).unwrap_or(0);
|
|
357
|
+
let mut message = message;
|
|
358
|
+
match message.get("type").and_then(Value::as_str) {
|
|
359
|
+
Some("update") => {
|
|
360
|
+
let (base_revision, revision) = revisions(&message)?;
|
|
361
|
+
let preserve = preserve_states(&message);
|
|
362
|
+
let program = message
|
|
363
|
+
.get_mut("program")
|
|
364
|
+
.filter(|program| program.is_object())
|
|
365
|
+
.map(Value::take)
|
|
366
|
+
.ok_or("update without program")?;
|
|
367
|
+
Ok(DevCommand::Update {
|
|
368
|
+
id,
|
|
369
|
+
base_revision,
|
|
370
|
+
revision,
|
|
371
|
+
body: UpdateBody::Full(program),
|
|
372
|
+
preserve,
|
|
373
|
+
received,
|
|
374
|
+
})
|
|
375
|
+
}
|
|
376
|
+
Some("patch") => {
|
|
377
|
+
let (base_revision, revision) = revisions(&message)?;
|
|
378
|
+
let preserve = preserve_states(&message);
|
|
379
|
+
let operations = match message.get_mut("operations").map(Value::take) {
|
|
380
|
+
Some(Value::Array(operations)) => operations,
|
|
381
|
+
_ => return Err("patch without operations".into()),
|
|
382
|
+
};
|
|
383
|
+
Ok(DevCommand::Update {
|
|
384
|
+
id,
|
|
385
|
+
base_revision,
|
|
386
|
+
revision,
|
|
387
|
+
body: UpdateBody::Patch(operations),
|
|
388
|
+
preserve,
|
|
389
|
+
received,
|
|
390
|
+
})
|
|
391
|
+
}
|
|
392
|
+
Some("inspect") => Ok(DevCommand::Inspect {
|
|
393
|
+
id,
|
|
394
|
+
include_values: message
|
|
395
|
+
.get("includeValues")
|
|
396
|
+
.and_then(Value::as_bool)
|
|
397
|
+
.unwrap_or(false),
|
|
398
|
+
}),
|
|
399
|
+
other => Err(format!("unsupported dev message {other:?}")),
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
#[derive(Clone)]
|
|
404
|
+
pub struct DevLink {
|
|
405
|
+
writer: Arc<Mutex<TcpStream>>,
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
impl DevLink {
|
|
409
|
+
/// Connect to the toolchain. Only loopback endpoints are accepted.
|
|
410
|
+
pub fn connect(
|
|
411
|
+
endpoint: &str,
|
|
412
|
+
token: &str,
|
|
413
|
+
proxy: EventLoopProxy<NativeEvent>,
|
|
414
|
+
) -> io::Result<Self> {
|
|
415
|
+
let address: SocketAddr = endpoint
|
|
416
|
+
.parse()
|
|
417
|
+
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid dev endpoint"))?;
|
|
418
|
+
if !address.ip().is_loopback() {
|
|
419
|
+
return Err(io::Error::new(
|
|
420
|
+
io::ErrorKind::PermissionDenied,
|
|
421
|
+
"dev endpoint must be a loopback address",
|
|
422
|
+
));
|
|
423
|
+
}
|
|
424
|
+
let mut stream = TcpStream::connect(address)?;
|
|
425
|
+
stream.set_nodelay(true)?;
|
|
426
|
+
write_frame(
|
|
427
|
+
&mut stream,
|
|
428
|
+
&json!({
|
|
429
|
+
"type": "hello",
|
|
430
|
+
"protocol": DEV_PROTOCOL_VERSION,
|
|
431
|
+
"token": token,
|
|
432
|
+
"semanticUiIrVersion": mun_runtime::SEMANTIC_UI_IR_VERSION,
|
|
433
|
+
"pid": std::process::id(),
|
|
434
|
+
}),
|
|
435
|
+
)?;
|
|
436
|
+
let mut reader = stream.try_clone()?;
|
|
437
|
+
let link = Self {
|
|
438
|
+
writer: Arc::new(Mutex::new(stream)),
|
|
439
|
+
};
|
|
440
|
+
let replies = link.clone();
|
|
441
|
+
thread::Builder::new()
|
|
442
|
+
.name("mun-dev-link".into())
|
|
443
|
+
.spawn(move || {
|
|
444
|
+
let reason = loop {
|
|
445
|
+
match read_frame_timed(&mut reader) {
|
|
446
|
+
Ok(Some((message, received))) => match parse_command(message, received) {
|
|
447
|
+
Ok(command) => {
|
|
448
|
+
if proxy.send_event(NativeEvent::Dev(command)).is_err() {
|
|
449
|
+
break None;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
Err(error) => replies.send(&json!({
|
|
453
|
+
"type": "protocol-error",
|
|
454
|
+
"message": error,
|
|
455
|
+
})),
|
|
456
|
+
},
|
|
457
|
+
Ok(None) => break None,
|
|
458
|
+
Err(error) => break Some(error.to_string()),
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
let _ = proxy.send_event(NativeEvent::Dev(DevCommand::Disconnected(reason)));
|
|
462
|
+
})?;
|
|
463
|
+
Ok(link)
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/// Best effort: a closed toolchain is reported by the reader thread.
|
|
467
|
+
pub fn send(&self, message: &Value) {
|
|
468
|
+
if let Ok(mut stream) = self.writer.lock() {
|
|
469
|
+
let _ = write_frame(&mut *stream, message);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
#[cfg(test)]
|
|
475
|
+
mod tests {
|
|
476
|
+
use super::*;
|
|
477
|
+
|
|
478
|
+
#[test]
|
|
479
|
+
fn frames_round_trip_and_reject_oversized_lengths() {
|
|
480
|
+
let mut buffer = Vec::new();
|
|
481
|
+
write_frame(&mut buffer, &json!({"type": "inspect", "id": 3})).unwrap();
|
|
482
|
+
let mut reader = buffer.as_slice();
|
|
483
|
+
assert_eq!(
|
|
484
|
+
read_frame(&mut reader).unwrap(),
|
|
485
|
+
Some(json!({"type": "inspect", "id": 3}))
|
|
486
|
+
);
|
|
487
|
+
assert_eq!(read_frame(&mut reader).unwrap(), None);
|
|
488
|
+
|
|
489
|
+
let oversized = ((MAX_FRAME_BYTES + 1) as u32).to_be_bytes();
|
|
490
|
+
let error = read_frame(&mut oversized.as_slice()).unwrap_err();
|
|
491
|
+
assert_eq!(error.kind(), io::ErrorKind::InvalidData);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
#[test]
|
|
495
|
+
fn commands_parse_and_unknown_messages_are_rejected() {
|
|
496
|
+
let command = parse_command(
|
|
497
|
+
json!({
|
|
498
|
+
"type": "update", "id": 7, "baseRevision": 2, "revision": 3, "program": {"version": 1}, "preserve": ["a"]
|
|
499
|
+
}),
|
|
500
|
+
Received::now(),
|
|
501
|
+
)
|
|
502
|
+
.unwrap();
|
|
503
|
+
assert!(
|
|
504
|
+
matches!(command, DevCommand::Update { id: 7, ref preserve, .. } if preserve == &["a"])
|
|
505
|
+
);
|
|
506
|
+
assert!(parse_command(json!({"type": "eval"}), Received::now()).is_err());
|
|
507
|
+
assert!(parse_command(json!({"type": "update"}), Received::now()).is_err());
|
|
508
|
+
assert!(
|
|
509
|
+
parse_command(
|
|
510
|
+
json!({"type": "patch", "baseRevision": 2, "revision": 4, "operations": []}),
|
|
511
|
+
Received::now()
|
|
512
|
+
)
|
|
513
|
+
.is_err()
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
#[test]
|
|
518
|
+
fn dev_program_commits_only_accepted_updates_in_revision_order() {
|
|
519
|
+
let text = |label: &str| {
|
|
520
|
+
json!({
|
|
521
|
+
"version": 1, "sourceLanguage": "mun", "entry": "App",
|
|
522
|
+
"states": [{"name": "count", "initial": 0}],
|
|
523
|
+
"root": {"kind": "window", "id": "window-root", "title": "T", "child":
|
|
524
|
+
{"kind": "text", "id": "label", "value": {"kind": "literal", "value": label}}}
|
|
525
|
+
})
|
|
526
|
+
};
|
|
527
|
+
let mut runtime = Runtime::from_json(&text("a").to_string()).unwrap();
|
|
528
|
+
let mut dev = DevProgram::new(text("a"));
|
|
529
|
+
let preserve = ["count".to_owned()];
|
|
530
|
+
let set_label = |label: &str| {
|
|
531
|
+
UpdateBody::Patch(vec![json!({
|
|
532
|
+
"op": "set", "path": ["root", "child", "value", "value"], "value": label
|
|
533
|
+
})])
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
let (_, timings) = dev
|
|
537
|
+
.apply(
|
|
538
|
+
&mut runtime,
|
|
539
|
+
0,
|
|
540
|
+
1,
|
|
541
|
+
set_label("b"),
|
|
542
|
+
&preserve,
|
|
543
|
+
Received::now(),
|
|
544
|
+
)
|
|
545
|
+
.unwrap();
|
|
546
|
+
assert_eq!(dev.revision(), 1);
|
|
547
|
+
assert!(timings.apply_micros >= timings.patch_micros);
|
|
548
|
+
|
|
549
|
+
// Stale revision, malformed patch and a program the runtime rejects
|
|
550
|
+
// leave the committed program and revision untouched.
|
|
551
|
+
let stale = dev.apply(
|
|
552
|
+
&mut runtime,
|
|
553
|
+
0,
|
|
554
|
+
1,
|
|
555
|
+
set_label("c"),
|
|
556
|
+
&preserve,
|
|
557
|
+
Received::now(),
|
|
558
|
+
);
|
|
559
|
+
assert_eq!(stale.unwrap_err().code, "revision-mismatch");
|
|
560
|
+
let missing = UpdateBody::Patch(vec![
|
|
561
|
+
json!({"op": "set", "path": ["nope", "x"], "value": 1}),
|
|
562
|
+
]);
|
|
563
|
+
let invalid = dev.apply(&mut runtime, 1, 2, missing, &preserve, Received::now());
|
|
564
|
+
assert_eq!(invalid.unwrap_err().code, "patch-invalid");
|
|
565
|
+
let broken = UpdateBody::Full(json!({"version": 1, "states": 3}));
|
|
566
|
+
let rejected = dev.apply(&mut runtime, 1, 2, broken, &preserve, Received::now());
|
|
567
|
+
assert_eq!(rejected.unwrap_err().code, "runtime-rejected");
|
|
568
|
+
assert_eq!(dev.revision(), 1);
|
|
569
|
+
assert_eq!(dev.program["root"]["child"]["value"]["value"], "b");
|
|
570
|
+
|
|
571
|
+
dev.apply(
|
|
572
|
+
&mut runtime,
|
|
573
|
+
1,
|
|
574
|
+
2,
|
|
575
|
+
set_label("c"),
|
|
576
|
+
&preserve,
|
|
577
|
+
Received::now(),
|
|
578
|
+
)
|
|
579
|
+
.unwrap();
|
|
580
|
+
assert_eq!(dev.revision(), 2);
|
|
581
|
+
assert_eq!(dev.program["root"]["child"]["value"]["value"], "c");
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
#[test]
|
|
585
|
+
fn dev_patch_reconstructs_program_and_rejects_invalid_paths() {
|
|
586
|
+
let program = json!({"root": {"children": [{"text": "old"}]}, "states": []});
|
|
587
|
+
let patched = apply_program_patch(
|
|
588
|
+
&program,
|
|
589
|
+
&[json!({
|
|
590
|
+
"op": "set", "path": ["root", "children", 0, "text"], "value": "new"
|
|
591
|
+
})],
|
|
592
|
+
)
|
|
593
|
+
.unwrap();
|
|
594
|
+
assert_eq!(patched["root"]["children"][0]["text"], "new");
|
|
595
|
+
assert_eq!(program["root"]["children"][0]["text"], "old");
|
|
596
|
+
assert!(
|
|
597
|
+
apply_program_patch(
|
|
598
|
+
&program,
|
|
599
|
+
&[json!({"op": "set", "path": ["missing", "x"], "value": 1})]
|
|
600
|
+
)
|
|
601
|
+
.is_err()
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
}
|