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
// STARK, a system for computer augmented design.
// Copyright (C) 2021 Matthew Rothlisberger

// STARK is free software: you can redistribute it and / or modify it
// under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.

// STARK is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Affero General Public License for more details.

// You should have received a copy of the GNU Affero General Public
// License along with STARK (in the LICENSE file). If not, see
// <https://www.gnu.org/licenses/>.

// Find full copyright information in the top level COPYRIGHT file.

// <>

// src/lib.rs

// Top level lib: declares features and modules; contains the main
// Sail loop and some utility functions

// <>

#![feature(core_intrinsics)]
#![feature(const_fn_transmute)]

use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};

/// Graphical frame, provided by the desktop environment
type Frame = winit::window::Window;

pub mod context;
pub mod graphics;
pub mod sail;

use sail::SlHead;

/// Handle for a frame (to pass to rendering system)
pub struct FrameHandle(pub RawWindowHandle);

unsafe impl HasRawWindowHandle for FrameHandle {
    fn raw_window_handle(&self) -> RawWindowHandle {
        self.0
    }
}

unsafe impl Send for FrameHandle {}

/// Sail interpreter loop for the manager thread
pub fn manager_loop(frame: Frame, sl_reg: usize, sl_tbl: usize, sl_env: usize) {
    let (sl_tbl, sl_env) = (sl_tbl as *mut sail::SlHead, sl_env as *mut sail::SlHead);
    let sl_reg = sl_reg as *mut sail::memmgt::Region;

    frame.set_cursor_icon(winit::window::CursorIcon::Crosshair);

    let frm_obj = unsafe { sail::memmgt::alloc(sl_reg, 8, sail::Cfg::B8Other as u8) };
    unsafe { sail::write_field_unchecked(frm_obj, 0, (&frame as *const _) as u64) };
    sail::env_layer_ins_by_id(sl_reg, sl_env, sail::S_FRAME.0, frm_obj);

    sail_fn! {
        let mngr_fns;
        _reg _tbl _env;

        "cursor-vis" 2 [frm_ptr, vis] {
            assert_eq!(sail::get_cfg_spec(frm_ptr), sail::Cfg::B8Other);
            let frame = unsafe { &*(sail::read_field_unchecked::<u64>(frm_ptr, 0) as *const Frame) };

            frame.set_cursor_visible(sail::bool_get(vis));

            return sail::nil();
        }

        "cursor-pos" 5 [frm_ptr, w, h, x, y] {
            assert_eq!(sail::get_cfg_spec(frm_ptr), sail::Cfg::B8Other);
            let frame = unsafe { &*(sail::read_field_unchecked::<u64>(frm_ptr, 0) as *const Frame) };

            assert_eq!(sail::core_type(w), Some(sail::CoreType::U32));
            assert_eq!(sail::core_type(h), Some(sail::CoreType::U32));
            assert_eq!(sail::core_type(x), Some(sail::CoreType::F32));
            assert_eq!(sail::core_type(y), Some(sail::CoreType::F32));

            let (w, h, x, y) = (
                sail::u32_get(w),
                sail::u32_get(h),
                sail::f32_get(x),
                sail::f32_get(y),
            );

            frame
                .set_cursor_position(winit::dpi::Position::Physical(
                    winit::dpi::PhysicalPosition {
                        x: ((x + 1.0) * (w / 2) as f32) as i32,
                        y: ((y + 1.0) * (h / 2) as f32) as i32,
                    },
                ))
                .unwrap();

            return sail::nil();
        }
    }

    sail::insert_native_procs(sl_reg, sl_tbl, sl_env, mngr_fns);

    let prog_txt = &std::fs::read_to_string("scripts/main.sl").unwrap();
    let prog_expr = sail::parser::parse(sl_reg, sl_tbl, prog_txt).unwrap();

    let mut stack = sail::eval::EvalStack::new(10000);

    let sigil = 1 as *mut SlHead;

    let mut ret_slot = sigil;
    let ret_addr: *mut *mut SlHead = &mut ret_slot;

    stack.start(ret_addr, sl_env, prog_expr);

    while ret_slot == sigil {
        stack.iter_once(sl_reg, sl_tbl);
    }

    let main = sail::env_lookup_by_id(sl_env, sail::S_MAIN.0);

    stack.push_frame_head(ret_addr, sail::eval::Opcode::Apply, sl_env);
    stack.push(main);

    loop {
        stack.iter_once(sl_reg, sl_tbl);

        if stack.is_empty() {
            println!("manager thread ended");
            break;
        }
    }
}