hexicon-host 0.1.0

The interface between the Hexicon VM and the system it runs on
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! The interface between the Hexicon VM and the system it runs on.
//!
//! The VM has no direct access to terminals, files, clocks, or randomness.
//! Everything outside the VM goes through a [`Host`], which keeps the VM
//! portable to WebAssembly and makes runs reproducible under test.

pub mod entities;
pub mod pane;
pub mod rich;

use std::cell::RefCell;
use std::collections::{HashMap, VecDeque};
use std::rc::Rc;

/// Why a file could not be read or written.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileError {
    /// There is no file by that name.
    NotFound,
    /// The host does not let the program use that file.
    Denied,
    /// The host has no files.
    Unsupported,
    /// Anything else, such as a full disk.
    Failed,
}

/// What kind of thing a file name refers to. The values are the ones
/// programs see, and can be combined.
pub mod file_type {
    /// An ordinary file.
    pub const FILE: u32 = 0x01;
    /// A directory.
    pub const DIR: u32 = 0x02;
    /// A character device.
    pub const CHAR: u32 = 0x04;
    /// A block device.
    pub const BLOCK: u32 = 0x08;
    /// A pipe.
    pub const PIPE: u32 = 0x10;
    /// A socket.
    pub const SOCKET: u32 = 0x20;
    /// A symbolic link.
    pub const LINK: u32 = 0x40;
}

/// Attributes of a file. The values are the ones programs see.
pub mod file_attr {
    /// Not normally listed.
    pub const HIDDEN: u32 = 0x01;
    /// Belongs to the system.
    pub const SYSTEM: u32 = 0x02;
    /// Can be read.
    pub const READ: u32 = 0x04;
    /// Can be written.
    pub const WRITE: u32 = 0x08;
}

/// What the host knows about a file.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FileInfo {
    /// A combination of [`file_type`] values.
    pub kind: u32,
    /// A combination of [`file_attr`] values.
    pub attrs: u32,
    /// The size in bytes.
    pub size: u64,
    /// When the file was last read, in seconds since the start of 1970.
    pub accessed: Option<i64>,
    /// When the file was last changed.
    pub modified: Option<i64>,
    /// When the file was created.
    pub created: Option<i64>,
    /// Where a symbolic link leads.
    pub link_target: Option<String>,
}

/// Where a file is in relation to the folder a program's file access is
/// confined to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Sandbox {
    /// In the folder, or in a folder within it.
    Inside,
    /// The folder itself.
    Itself,
    /// A folder the sandbox is in.
    Contains,
    /// Anywhere else.
    Outside,
}

/// What a host has when it is asked for input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Input {
    /// A line the player entered, or a key.
    Text(String),
    /// There is no more input and never will be.
    End,
    /// There is none yet. The VM stops where it is and hands control
    /// back, and asks again when it is resumed. A host that cannot wait
    /// for the player without freezing, as in a browser, answers this
    /// way; one that can wait never needs to.
    Pending,
}

/// Which file dialog a program asks for.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileDialog {
    /// A file to read: one that is there.
    Open,
    /// A file to write: one that may not be there yet.
    Save,
}

/// What kind of file a program asks for, so that a dialog can show files
/// of that kind and name a new one as they are named.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileKind {
    /// A saved game: `.hxs`.
    SavedGame,
    /// A transcript, or other text: `.txt`.
    Text,
    /// Anything else.
    Other,
}

impl FileKind {
    /// The kind a program's file type code (`FileTypeSave`, ...) asks for.
    pub fn from_code(code: i64) -> Self {
        match code {
            15 => FileKind::SavedGame,
            2 | 7 => FileKind::Text,
            _ => FileKind::Other,
        }
    }
}

/// What the player did with a file dialog.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FileChoice {
    /// They chose a file: its name, as the host's file functions take it.
    Chosen(String),
    /// They chose none.
    Cancelled,
    /// They have not chosen yet. As with [`Input::Pending`], the VM stops
    /// and asks again when it is resumed.
    Pending,
}

/// The time on the host's clock.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Time {
    /// Milliseconds since the start of 1970, UTC.
    pub unix_millis: i64,
    /// How far local time is ahead of UTC, in seconds.
    pub utc_offset_seconds: i32,
}

/// The services a VM needs from the system it runs on.
pub trait Host {
    /// Display text on the main console.
    fn print(&mut self, text: &str);

    /// What the display can do beyond plain text, if it is a rich
    /// display; `None` for a display of text, which gets its text through
    /// [`Host::print`], broken into lines that fit.
    fn rich_display(&self) -> Option<rich::RichDisplay> {
        None
    }

    /// On a rich display: what the program wrote, as it wrote it, markup
    /// and all, in order. The host checks it with a [`rich::Checker`].
    fn print_markup(&mut self, markup: &str) {
        let _ = markup;
    }

    /// On a rich display: a line the player typed, from a script, shown
    /// as the player's own words.
    fn print_input(&mut self, text: &str) {
        let _ = text;
    }

    /// Fill a buffer with unpredictable bytes, for seeding the random
    /// number generator. A host that leaves the buffer alone gives every
    /// run the same seed, which is what tests want.
    fn random_bytes(&mut self, buffer: &mut [u8]) {
        let _ = buffer;
    }

    /// The whole contents of a file. Names are the host's to interpret:
    /// a path on a system with a file system, a key in a browser's
    /// storage.
    fn read_file(&mut self, name: &str) -> Result<Vec<u8>, FileError> {
        let _ = name;
        Err(FileError::Unsupported)
    }

    /// Create a file with the given contents, replacing any there was.
    fn write_file(&mut self, name: &str, data: &[u8]) -> Result<(), FileError> {
        let _ = (name, data);
        Err(FileError::Unsupported)
    }

    /// What a name refers to, or `None` if nothing. With `follow_links`,
    /// a symbolic link is described by what it leads to.
    ///
    /// File names are in one syntax on every host: parts separated by
    /// `/`, with `.` for the folder itself and `..` for the one above. A
    /// name that does not start at a root is relative to wherever the
    /// host keeps the program's files.
    fn file_info(&mut self, name: &str, follow_links: bool) -> Option<FileInfo> {
        let _ = (name, follow_links);
        None
    }

    /// Delete a file.
    fn delete_file(&mut self, name: &str) -> Result<(), FileError> {
        let _ = name;
        Err(FileError::Unsupported)
    }

    /// Give a file a new name, which must not be in use.
    fn rename_file(&mut self, from: &str, to: &str) -> Result<(), FileError> {
        let _ = (from, to);
        Err(FileError::Unsupported)
    }

    /// Create a directory, and with `parents` any directories above it
    /// that are missing.
    fn create_dir(&mut self, name: &str, parents: bool) -> Result<(), FileError> {
        let _ = (name, parents);
        Err(FileError::Unsupported)
    }

    /// Remove a directory, which has to be empty unless `contents` says
    /// to delete what is in it too.
    fn remove_dir(&mut self, name: &str, contents: bool) -> Result<(), FileError> {
        let _ = (name, contents);
        Err(FileError::Unsupported)
    }

    /// The names in a directory, without the path to it. A host whose
    /// directories have entries for themselves and their parents includes
    /// `.` and `..`.
    fn list_dir(&mut self, name: &str) -> Option<Vec<String>> {
        let _ = name;
        None
    }

    /// The full name of a file, from a root.
    fn absolute_path(&mut self, name: &str) -> Option<String> {
        let _ = name;
        None
    }

    /// The roots of the file system: `/` on some systems, a list of
    /// drives on others.
    fn root_dirs(&mut self) -> Vec<String> {
        Vec::new()
    }

    /// Where a file is in relation to the folder the program's file
    /// access is confined to. The default confines nothing.
    fn sandbox(&mut self, name: &str) -> Sandbox {
        let _ = name;
        Sandbox::Inside
    }

    /// A name for a new temporary file, which does not exist yet.
    fn temp_file_name(&mut self) -> Option<String> {
        None
    }

    /// The name of one of the files that programs ask for by number
    /// because where they are kept is up to the system: 1 is the file of
    /// settings shared by every game that uses the standard library, and
    /// 2 the same for the web interface.
    fn special_file(&mut self, id: i32) -> Option<String> {
        let _ = id;
        None
    }

    /// Add to the end of a file, creating it if it is not there.
    fn append_file(&mut self, name: &str, data: &[u8]) -> Result<(), FileError> {
        let _ = (name, data);
        Err(FileError::Unsupported)
    }

    /// Read a line of input from the player, without the line ending.
    fn read_line(&mut self) -> Input {
        Input::End
    }

    /// Read one keystroke. A key that types a character is that character;
    /// Enter is a newline; any other key goes by its name in square
    /// brackets, such as `[up]` or `[f1]`.
    fn read_key(&mut self) -> Input {
        Input::End
    }

    /// Ask the player for a file in a dialog of the system's own, with
    /// the program's prompt as its title, or `None` for a host that has
    /// no such dialog: the VM then asks for the file's name as a line of
    /// input, after the prompt, as a console of text does.
    fn choose_file(
        &mut self,
        prompt: &str,
        dialog: FileDialog,
        kind: FileKind,
    ) -> Option<FileChoice> {
        let _ = (prompt, dialog, kind);
        None
    }

    /// Clear the main console.
    fn clear_screen(&mut self) {}

    /// The width of the main console in columns, if the host has one of
    /// its own, such as a terminal window's. Text is broken into lines
    /// that fit. Without one the width is 80 columns.
    fn console_width(&mut self) -> Option<usize> {
        None
    }

    /// Input has just come from a script instead of from the player. A
    /// host that pauses when a screenful of text has gone by since the
    /// player last typed starts counting again from here.
    fn script_input(&mut self) {}

    /// Pause for a number of milliseconds. A host that can wait does,
    /// and returns true. One that cannot returns false, which stops the
    /// VM where it is; when the time has passed, the host resumes the VM,
    /// which asks again, and this time the answer is true.
    fn sleep(&mut self, milliseconds: u32) -> bool {
        let _ = milliseconds;
        true
    }

    /// Show text in the status line: the left part, which is usually the
    /// name of the player's location.
    fn status_line(&mut self, text: &str) {
        let _ = text;
    }

    /// Show text at the right of the status line, usually the score.
    fn status_right(&mut self, text: &str) {
        let _ = text;
    }

    /// The time now. A host without a clock reports the start of 1970,
    /// which also keeps tests reproducible.
    fn time(&mut self) -> Time {
        Time::default()
    }

    /// The name of the host's time zone in the zoneinfo database, such as
    /// `America/Chicago`, if the host knows it. Without one, local time
    /// is UTC shifted by the offset that [`Host::time`] reports.
    fn local_time_zone(&mut self) -> Option<String> {
        None
    }

    /// A name for the system the host runs on.
    fn system_name(&self) -> String {
        std::env::consts::OS.to_owned()
    }

    /// Whether the host can show panes. Without them, a program
    /// that asks for one is told it cannot have it, as on a plain
    /// console.
    fn has_panes(&self) -> bool {
        false
    }

    /// Create a pane inside another, or inside the main window
    /// if `parent` is `None`: its handle, or `None` if the host cannot.
    fn pane_create(
        &mut self,
        parent: Option<u32>,
        placement: pane::Placement,
        spec: pane::PaneSpec,
    ) -> Option<u32> {
        let _ = (parent, placement, spec);
        None
    }

    /// Remove a pane.
    fn pane_delete(&mut self, id: u32) {
        let _ = id;
    }

    /// Remove the text in a pane.
    fn pane_clear(&mut self, id: u32) {
        let _ = id;
    }

    /// Add text to a pane. The text is already broken into lines
    /// that fit.
    fn pane_write(&mut self, id: u32, text: &str) {
        let _ = (id, text);
    }

    /// On a rich display: add what the program wrote to a text pane, as
    /// it wrote it, markup and all, as [`Host::print_markup`] does for the
    /// main window. A text grid's text still comes through
    /// [`Host::pane_write`].
    fn pane_write_markup(&mut self, id: u32, markup: &str) {
        let _ = (id, markup);
    }

    /// Show what has been written to a pane.
    fn pane_flush(&mut self, id: u32) {
        let _ = id;
    }

    /// Change the size of a pane. See [`pane::Screen::set_size`].
    fn pane_set_size(&mut self, id: u32, size: i32, units: i32, advisory: bool) {
        let _ = (id, size, units, advisory);
    }

    /// Make a pane as big as what it holds.
    fn pane_size_to_contents(&mut self, id: u32) {
        let _ = id;
    }

    /// Move where text goes next in a text grid, counting from zero.
    fn pane_go_to(&mut self, id: u32, row: usize, column: usize) {
        let _ = (id, row, column);
    }

    /// What a program can learn about a pane.
    fn pane_info(&mut self, id: u32) -> Option<pane::PaneInfo> {
        let _ = id;
        None
    }

    /// Give a pane a background color: one of the colors the
    /// host chooses, such as [`pane::color::STATUS_BG`], or an RGB value.
    fn pane_set_screen_color(&mut self, id: u32, color: u32) {
        let _ = (id, color);
    }
}

/// A host that collects console output in memory.
///
/// Clones share one buffer, so a caller can keep a clone, hand the other to
/// a VM, and read the output afterward.
#[derive(Debug, Default, Clone)]
pub struct BufferHost {
    output: Rc<RefCell<String>>,
    files: Rc<RefCell<HashMap<String, Vec<u8>>>>,
    input: Rc<RefCell<VecDeque<String>>>,
    temp_counter: Rc<RefCell<u32>>,
    interactive: Rc<RefCell<bool>>,
    screen: Rc<RefCell<Option<pane::Screen>>>,
}

impl BufferHost {
    /// A host with an empty buffer.
    pub fn new() -> Self {
        Self::default()
    }

    /// Everything displayed so far.
    pub fn output(&self) -> String {
        self.output.borrow().clone()
    }

    /// Queue a line for the program to read. A read of one key takes a
    /// whole line too, as the name of the key.
    pub fn push_input(&self, line: &str) {
        self.input.borrow_mut().push_back(line.to_owned());
    }

    /// Say that more input may be queued later, so that running out is
    /// not the end of input: the program is to stop and wait.
    pub fn set_interactive(&self, interactive: bool) {
        *self.interactive.borrow_mut() = interactive;
    }

    fn next_input(&self) -> Input {
        match self.input.borrow_mut().pop_front() {
            Some(line) => Input::Text(line),
            None if *self.interactive.borrow() => Input::Pending,
            None => Input::End,
        }
    }

    /// The contents of a file the program wrote.
    pub fn file(&self, name: &str) -> Option<Vec<u8>> {
        self.files.borrow().get(name).cloned()
    }

    /// Show panes, on a screen of the given size.
    pub fn enable_panes(&self, rows: usize, columns: usize) {
        *self.screen.borrow_mut() = Some(pane::Screen::new(rows, columns, true));
    }

    /// The panes, if they are shown.
    pub fn screen(&self) -> std::cell::Ref<'_, Option<pane::Screen>> {
        self.screen.borrow()
    }

    fn with_screen<T>(&self, f: impl FnOnce(&mut pane::Screen) -> T) -> Option<T> {
        self.screen.borrow_mut().as_mut().map(f)
    }

    /// Supply a file for the program to read.
    pub fn set_file(&self, name: &str, data: &[u8]) {
        self.files
            .borrow_mut()
            .insert(name.to_owned(), data.to_vec());
    }
}

impl Host for BufferHost {
    fn print(&mut self, text: &str) {
        self.output.borrow_mut().push_str(text);
    }

    // Files are kept in memory, shared between clones like the output.
    fn read_file(&mut self, name: &str) -> Result<Vec<u8>, FileError> {
        self.files
            .borrow()
            .get(name)
            .cloned()
            .ok_or(FileError::NotFound)
    }

    fn write_file(&mut self, name: &str, data: &[u8]) -> Result<(), FileError> {
        self.files
            .borrow_mut()
            .insert(name.to_owned(), data.to_vec());
        Ok(())
    }

    fn append_file(&mut self, name: &str, data: &[u8]) -> Result<(), FileError> {
        self.files
            .borrow_mut()
            .entry(name.to_owned())
            .or_default()
            .extend_from_slice(data);
        Ok(())
    }

    // The files have no directories: a name is only a key.
    fn file_info(&mut self, name: &str, _follow_links: bool) -> Option<FileInfo> {
        self.files.borrow().get(name).map(|data| FileInfo {
            kind: file_type::FILE,
            attrs: file_attr::READ | file_attr::WRITE,
            size: data.len() as u64,
            ..FileInfo::default()
        })
    }

    fn delete_file(&mut self, name: &str) -> Result<(), FileError> {
        match self.files.borrow_mut().remove(name) {
            Some(_) => Ok(()),
            None => Err(FileError::NotFound),
        }
    }

    fn rename_file(&mut self, from: &str, to: &str) -> Result<(), FileError> {
        let mut files = self.files.borrow_mut();
        if files.contains_key(to) {
            return Err(FileError::Failed);
        }
        let data = files.remove(from).ok_or(FileError::NotFound)?;
        files.insert(to.to_owned(), data);
        Ok(())
    }

    fn temp_file_name(&mut self) -> Option<String> {
        let mut counter = self.temp_counter.borrow_mut();
        *counter += 1;
        Some(format!("/tmp/temp{counter}"))
    }

    fn read_line(&mut self) -> Input {
        self.next_input()
    }

    fn read_key(&mut self) -> Input {
        self.next_input()
    }

    fn has_panes(&self) -> bool {
        self.screen.borrow().is_some()
    }

    fn pane_create(
        &mut self,
        parent: Option<u32>,
        placement: pane::Placement,
        spec: pane::PaneSpec,
    ) -> Option<u32> {
        self.with_screen(|screen| screen.create(parent, placement, spec))
            .flatten()
    }

    fn pane_delete(&mut self, id: u32) {
        self.with_screen(|screen| screen.delete(id));
    }

    fn pane_clear(&mut self, id: u32) {
        self.with_screen(|screen| screen.clear(id));
    }

    fn pane_write(&mut self, id: u32, text: &str) {
        self.with_screen(|screen| screen.write(id, text));
    }

    fn pane_set_size(&mut self, id: u32, size: i32, units: i32, advisory: bool) {
        self.with_screen(|screen| screen.set_size(id, size, units, advisory));
    }

    fn pane_size_to_contents(&mut self, id: u32) {
        self.with_screen(|screen| screen.size_to_contents(id));
    }

    fn pane_go_to(&mut self, id: u32, row: usize, column: usize) {
        self.with_screen(|screen| screen.go_to(id, row, column));
    }

    fn pane_info(&mut self, id: u32) -> Option<pane::PaneInfo> {
        self.with_screen(|screen| screen.info(id)).flatten()
    }

    fn pane_set_screen_color(&mut self, id: u32, color: u32) {
        self.with_screen(|screen| screen.set_screen_color(id, color));
    }
}