Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

term.ui: fix in -prod mode (stop abusing ub) #19529

Merged
merged 4 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions vlib/term/ui/input_nix.c.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2020-2021 Raúl Hernández. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
[has_globals]
module ui

struct ExtraContext {
Expand All @@ -11,7 +12,7 @@ mut:
read_all_bytes bool = true
}

const ctx_ptr = &Context(unsafe { nil })
__global ctx_ptr = &Context(unsafe { nil })
Wertzui123 marked this conversation as resolved.
Show resolved Hide resolved

// init initializes the terminal console with Config `cfg`.
pub fn init(cfg Config) &Context {
Expand All @@ -20,12 +21,7 @@ pub fn init(cfg Config) &Context {
}
ctx.read_buf = []u8{cap: cfg.buffer_size}

// lmao
unsafe {
x := &ui.ctx_ptr
*x = ctx
_ = x
}
ctx_ptr = ctx
return ctx
}

Expand Down
22 changes: 10 additions & 12 deletions vlib/term/ui/input_windows.c.v
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// Copyright (c) 2020-2021 Raúl Hernández. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
[has_globals]
module ui

import os
import time

const buf_size = 64

const ctx_ptr = &Context(unsafe { nil })
__global ctx_ptr = &Context(unsafe { nil })

const stdin_at_startup = u32(0)
__global stdin_at_startup = u32(0)
Wertzui123 marked this conversation as resolved.
Show resolved Hide resolved

struct ExtraContext {
mut:
Expand All @@ -21,14 +22,14 @@ mut:
}

fn restore_terminal_state() {
if unsafe { ui.ctx_ptr != 0 } {
if ui.ctx_ptr.cfg.use_alternate_buffer {
if unsafe { ctx_ptr != 0 } {
if ctx_ptr.cfg.use_alternate_buffer {
// clear the terminal and set the cursor to the origin
print('\x1b[2J\x1b[3J')
print('\x1b[?1049l')
flush_stdout()
}
C.SetConsoleMode(ui.ctx_ptr.stdin_handle, ui.stdin_at_startup)
C.SetConsoleMode(ctx_ptr.stdin_handle, stdin_at_startup)
}
load_title()
os.flush()
Expand All @@ -46,7 +47,7 @@ pub fn init(cfg Config) &Context {
panic('could not get stdin handle')
}
// save the current input mode, to be restored on exit
if !C.GetConsoleMode(stdin_handle, &ui.stdin_at_startup) {
if !C.GetConsoleMode(stdin_handle, &stdin_at_startup) {
panic('could not get stdin console mode')
}

Expand Down Expand Up @@ -80,14 +81,11 @@ pub fn init(cfg Config) &Context {
flush_stdout()
}

unsafe {
x := &ui.ctx_ptr
*x = ctx
}
ctx_ptr = ctx
C.atexit(restore_terminal_state)
for code in ctx.cfg.reset {
os.signal_opt(code, fn (_ os.Signal) {
mut c := unsafe { ui.ctx_ptr }
mut c := ctx_ptr
if unsafe { c != 0 } {
c.cleanup()
}
Expand Down Expand Up @@ -116,7 +114,7 @@ pub fn (mut ctx Context) run() ! {
}
if !ctx.paused {
sw.restart()
if ctx.cfg.event_fn != unsafe { nil } {
if ctx.cfg.event_fn != none {
ctx.parse_events()
}
ctx.frame()
Expand Down
8 changes: 4 additions & 4 deletions vlib/term/ui/termios_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn restore_terminal_state_signal(_ os.Signal) {

fn restore_terminal_state() {
termios_reset()
mut c := unsafe { ctx_ptr }
mut c := ctx_ptr
if unsafe { c != 0 } {
c.paused = true
load_title()
Expand Down Expand Up @@ -118,7 +118,7 @@ fn (mut ctx Context) termios_setup() ! {
C.atexit(restore_terminal_state)
os.signal_opt(.tstp, restore_terminal_state_signal) or {}
os.signal_opt(.cont, fn (_ os.Signal) {
mut c := unsafe { ctx_ptr }
mut c := ctx_ptr
if unsafe { c != 0 } {
c.termios_setup() or { panic(err) }
c.window_height, c.window_width = get_terminal_size()
Expand All @@ -133,7 +133,7 @@ fn (mut ctx Context) termios_setup() ! {
}) or {}
for code in ctx.cfg.reset {
os.signal_opt(code, fn (_ os.Signal) {
mut c := unsafe { ctx_ptr }
mut c := ctx_ptr
if unsafe { c != 0 } {
c.cleanup()
}
Expand All @@ -142,7 +142,7 @@ fn (mut ctx Context) termios_setup() ! {
}

os.signal_opt(.winch, fn (_ os.Signal) {
mut c := unsafe { ctx_ptr }
mut c := ctx_ptr
if unsafe { c != 0 } {
c.window_height, c.window_width = get_terminal_size()

Expand Down