robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
0abc9ccc19
master
1#include <emscripten.h> 2#include <emscripten/threading.h> 3#include <math.h> 4#include <poll.h> 5#include <pthread.h> 6#include <stdatomic.h> 7#include <stddef.h> 8#include <stdint.h> 9#include <stdio.h> 10#include <stdlib.h> 11#include <string.h> 12#include <sys/time.h> 13#include <time.h> 14#include <uv.h> 15 16void uv__io_cb (uv_loop_t * loop ,uv__io_t * w ,unsigned events ); 17 18// System info stubs 19 20uint64_t uv_get_free_memory (void ) 21{ 22return 8 * 1024 * 1024 ; 23} 24uint64_t uv_get_total_memory (void ) 25{ 26return 256 * 1024 * 1024 ; 27} 28uint64_t uv_get_available_memory (void ) 29{ 30return uv_get_free_memory (); 31} 32uint64_t uv_get_constrained_memory (void ) 33{ 34return uv_get_total_memory (); 35} 36 37void uv_loadavg (double avg [3 ]) 38{ 39avg [0 ]= 0.0 ;avg [1 ]= 0.0 ;avg [2 ]= 0.0 ; 40} 41 42int uv_uptime (double * uptime ) 43{ 44if (!uptime ) { 45return UV_EINVAL ; 46 } 47* uptime = emscripten_get_now () /1000.0 ; 48return 0 ; 49} 50 51int uv_resident_set_memory (size_t * rss ) 52{ 53if (!rss ) { 54return UV_EINVAL ; 55 } 56* rss = uv_get_total_memory () /2 ;// fake value 57return 0 ; 58} 59 60int uv_exepath (char * buffer ,size_t * size ) 61{ 62if (!buffer || !size ) { 63return UV_EINVAL ; 64 } 65const char * exepath = "/nvim.wasm" ; 66size_t len = strlen (exepath ); 67if (* size <=len ) { 68* size = len + 1 ; 69return UV_ENOBUFS ; 70 } 71memcpy (buffer ,exepath ,len ); 72buffer [len ]= '\0' ; 73* size = len ; 74return 0 ; 75} 76 77// Browsers have no native CPU information. So return a single virtual CPU 78int uv_cpu_info (uv_cpu_info_t * * cpu_infos ,int * count ) 79{ 80if (!cpu_infos || !count ) { 81return UV_EINVAL ; 82 } 83* cpu_infos = (uv_cpu_info_t * )malloc (sizeof (uv_cpu_info_t )); 84if (!* cpu_infos ) { 85return UV_ENOMEM ; 86 } 87 88uv_cpu_info_t * cpu = * cpu_infos ; 89cpu -> model = "WebAssembly Virtual CPU" ; 90cpu -> speed = 0 ; 91cpu -> cpu_times .user = 0 ;cpu -> cpu_times .nice = 0 ;cpu -> cpu_times .sys = 0 ; 92cpu -> cpu_times .idle = 0 ;cpu -> cpu_times .irq = 0 ; 93* count = 1 ; 94return 0 ; 95} 96 97// Browsers do not expose host network interfaces. Report only loopback. 98int uv_interface_addresses (uv_interface_address_t * * addresses ,int * count ) 99{ 100if (!addresses || !count ) { 101return UV_EINVAL ; 102 } 103* addresses = (uv_interface_address_t * )malloc (sizeof (uv_interface_address_t )); 104if (!* addresses ) { 105return UV_ENOMEM ; 106 } 107 108uv_interface_address_t * addr = * addresses ; 109memset (addr ,0 ,sizeof (uv_interface_address_t )); 110addr -> name = strdup ("lo" ); 111if (!addr -> name ) { 112free (addr );return UV_ENOMEM ; 113 } 114addr -> is_internal = 1 ; 115uv_ip4_addr ("127.0.0.1" ,0 , (struct sockaddr_in * )& addr -> address ); 116uv_ip4_addr ("255.0.0.0" ,0 , (struct sockaddr_in * )& addr -> netmask ); 117* count = 1 ; 118return 0 ; 119} 120 121#define UV_BROWSER_MAX_FD 256 122 123typedef struct { 124 _Atomicuint32_t generation ;// woken on any fd state change 125 _Atomicint32_t readable [UV_BROWSER_MAX_FD ]; 126 _Atomicint32_t writable [UV_BROWSER_MAX_FD ]; 127}uv_browser_shared_state_t ; 128 129static uv_browser_shared_state_t g_shared_state ; 130 131EMSCRIPTEN_KEEPALIVE 132void * uv_browser_get_shared_state_ptr (void ) 133{ 134return (void * )& g_shared_state ; 135} 136 137EMSCRIPTEN_KEEPALIVE 138void uv_browser_set_readable (int fd ,int32_t nbytes ) 139{ 140if (fd < 0 || fd >=UV_BROWSER_MAX_FD ) { 141return ; 142 } 143atomic_store (& g_shared_state .readable [fd ],nbytes ); 144atomic_fetch_add (& g_shared_state .generation ,1 ); 145emscripten_futex_wake (& g_shared_state .generation ,1 ); 146} 147 148EMSCRIPTEN_KEEPALIVE 149void uv_browser_set_writable (int fd ,int32_t nbytes ) 150{ 151if (fd < 0 || fd >=UV_BROWSER_MAX_FD ) { 152return ; 153 } 154atomic_store (& g_shared_state .writable [fd ],nbytes ); 155atomic_fetch_add (& g_shared_state .generation ,1 ); 156emscripten_futex_wake (& g_shared_state .generation ,1 ); 157} 158 159static int uv_browser_scan_ready (uv_loop_t * loop ,unsigned * out_idx ,int * out_revents ) 160{ 161unsigned i ; 162uv__io_t * w ; 163int revents ; 164 165for (i = 0 ;i < loop -> nwatchers && i < UV_BROWSER_MAX_FD ;i ++ ) { 166w = loop -> watchers [i ]; 167if (w == NULL || w -> pevents == 0 ) { 168continue ; 169 } 170 171revents = 0 ; 172if ((w -> pevents & POLLIN )&& atomic_load (& g_shared_state .readable [i ])> 0 ) { 173revents |=POLLIN ; 174 } 175if ((w -> pevents & POLLOUT )&& atomic_load (& g_shared_state .writable [i ])> 0 ) { 176revents |=POLLOUT ; 177 } 178 179if (revents ) { 180* out_idx = i ; 181* out_revents = revents ; 182return 1 ; 183 } 184 } 185return 0 ; 186} 187 188void uv__io_poll (uv_loop_t * loop ,int timeout ) 189{ 190double deadline ; 191double remaining ; 192uint32_t g0 ; 193unsigned idx ; 194int revents ; 195int have_signal ; 196uv__io_t * w ; 197 198deadline = (timeout < 0 ) ?-1.0 : (emscripten_get_now ()+ (double )timeout ); 199have_signal = 0 ; 200 201while (true) { 202/* Any state change that lands between this load and the scan below is still caught by the scan 203itself. Any change that lands after the scan will bump generation past g0, so the futex_wait call 204falls through immediately instead of sleeping through it. This closes the narrow window where a notify 205could otherwise land between "nothing is ready" and "waiting". */ 206 207g0 = atomic_load (& g_shared_state .generation ); 208 209if (uv_browser_scan_ready (loop ,& idx ,& revents )) { 210have_signal = 1 ; 211break ; 212 } 213 214if (deadline >=0.0 ) { 215remaining = deadline - emscripten_get_now (); 216if (remaining <=0.0 ) { 217break ; 218 } 219 }else { 220remaining = 60000.0 ; 221 } 222 223int rc = emscripten_futex_wait (& g_shared_state .generation ,g0 ,remaining ); 224 } 225 226uv_update_time (loop ); 227 228if (!have_signal ) { 229return ; 230 } 231 232for (idx = 0 ;idx < loop -> nwatchers && idx < UV_BROWSER_MAX_FD ;idx ++ ) { 233w = loop -> watchers [idx ]; 234if (w == NULL || w -> pevents == 0 ) { 235continue ; 236 } 237 238revents = 0 ; 239if ((w -> pevents & POLLIN )&& atomic_load (& g_shared_state .readable [idx ])> 0 ) { 240revents |=POLLIN ; 241 } 242if ((w -> pevents & POLLOUT )&& atomic_load (& g_shared_state .writable [idx ])> 0 ) { 243revents |=POLLOUT ; 244 } 245 246revents &=w -> pevents |POLLERR |POLLHUP ; 247 248if (revents == 0 ) { 249continue ; 250 } 251 252uv__io_cb (loop ,w ,revents ); 253 } 254} 255 256int uv__platform_loop_init (uv_loop_t * loop ) 257{ 258if (!loop ) { 259return UV_EINVAL ; 260 } 261loop -> backend_fd = -1 ; 262return 0 ; 263} 264 265void uv__platform_loop_delete (uv_loop_t * loop ) 266{ 267 (void )loop ; 268} 269 270void uv__platform_invalidate_fd (uv_loop_t * loop ,int fd ) 271{ 272if (fd < 0 || fd >=UV_BROWSER_MAX_FD ) { 273return ; 274 } 275atomic_store (& g_shared_state .readable [fd ],0 ); 276atomic_store (& g_shared_state .writable [fd ],0 ); 277} 278 279int uv__io_check_fd (uv_loop_t * loop ,int fd ) 280{ 281 (void )loop ; (void )fd ; 282return 0 ; 283} 284 285int uv__io_fork (uv_loop_t * loop ) 286{ 287 (void )loop ; 288return 0 ; 289} 290 291// pthread shims 292 293int pthread_getname_np (pthread_t thread ,char * name ,size_t len ) 294{ 295 (void )thread ; 296if (!name || len < 1 ) { 297return EINVAL ; 298 } 299strncpy (name ,"nvim-main" ,len - 1 ); 300name [len - 1 ]= '\0' ; 301return 0 ; 302} 303 304int pthread_setname_np (pthread_t thread ,const char * name ) 305{ 306 (void )thread ; (void )name ; 307return 0 ; 308} 309 310int pthread_setschedparam (pthread_t thread ,int policy ,const struct sched_param * param ) 311{ 312 (void )thread ; (void )policy ; (void )param ; 313return 0 ; 314}