robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

bfredlMerge pull request #40772 from Rawan10101/wasm-browser0abc9ccc19

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