maximecb / uvm

Fun, portable, minimalistic virtual machine.
Apache License 2.0
538 stars 19 forks source link

"sdl" as default feature #21

Closed markusmoenig closed 1 year ago

markusmoenig commented 1 year ago

I implemented "sdl" as a default feature, to compile the vm as headless use

cargo build --no-default-features

This is required as features in rust are supposed to add features (and not remove them).

Things are a bit ugly as the #[cfg] directive can only conditionally compile ranges of code inside a function, so for example in

vm/src/sys.mod

I can do

        #[cfg(feature = "sdl")]
        {
            self.reg_syscall(WINDOW_CREATE, SysCallFn::Fn4_1(window_create));
            self.reg_syscall(WINDOW_DRAW_FRAME, SysCallFn::Fn2_0(window_draw_frame));
            self.reg_syscall(WINDOW_ON_MOUSEMOVE, SysCallFn::Fn2_0(window_on_mousemove));
            self.reg_syscall(WINDOW_ON_MOUSEDOWN, SysCallFn::Fn2_0(window_on_mousedown));
            self.reg_syscall(WINDOW_ON_MOUSEUP, SysCallFn::Fn2_0(window_on_mouseup));
            self.reg_syscall(WINDOW_ON_KEYDOWN, SysCallFn::Fn2_0(window_on_keydown));
            self.reg_syscall(WINDOW_ON_KEYUP, SysCallFn::Fn2_0(window_on_keyup));
            self.reg_syscall(WINDOW_ON_TEXTINPUT, SysCallFn::Fn2_0(window_on_textinput));

            self.reg_syscall(AUDIO_OPEN_OUTPUT, SysCallFn::Fn4_1(audio_open_output));
        }

However as I cannot do this at the top level of files, so I needed to add a lot of #[cfg(feature = "sdl")] to sys/window.rs (and there is no way around this).

In headless mode I could compile and run the hello_world.c ncc example, while snake.c would obviously fail with an unknown syscall.

markusmoenig commented 1 year ago

btw; I still thing that making the current vm crate a library without any syscalls and an vmsdl app crate on top which provides the syscalls would be easier to maintain. But thats maybe a matter of taste.

I could implement this layout for you if you want to give it a try.