Skip to content

Commit 3ff7841

Browse files
committed
Make more emscripten compatibility improvements
See #8
1 parent 8c2d419 commit 3ff7841

11 files changed

+40
-71
lines changed

blink/blink.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19-
#include <setjmp.h>
2019
#include <signal.h>
2120
#include <stdio.h>
2221
#include <stdlib.h>
2322
#include <string.h>
23+
#include "blink/web.h"
2424
#include <unistd.h>
2525

2626
#include "blink/assert.h"
@@ -156,11 +156,12 @@ static void HandleSigs(void) {
156156
#endif
157157
}
158158

159-
int main(int argc, char *argv[], char **envp) {
159+
int main(int argc, char *argv[]) {
160+
SetupWeb();
160161
g_blink_path = argc > 0 ? argv[0] : 0;
161162
GetOpts(argc, argv);
162163
if (optind_ == argc) PrintUsage(argc, argv, 48, 2);
163164
WriteErrorInit();
164165
HandleSigs();
165-
return Exec(argv[optind_], argv + optind_, envp);
166+
return Exec(argv[optind_], argv + optind_, environ);
166167
}

blink/blink.mk

-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ ifneq ($(MODE), prof)
1414
o/$(MODE)/blink/uop.o: private CFLAGS += -fomit-frame-pointer
1515
endif
1616

17-
# x86.c doesn't understand VEX encoding right now
18-
ifeq ($(HOST_ARCH), x86_64)
19-
o/$(MODE)/blink/uop.o: private TARGET_ARCH = -march=k8
20-
endif
21-
2217
# vectorization makes code smaller
2318
o/$(MODE)/blink/sse2.o: private CFLAGS += -O3
2419
o/$(MODE)/x86_64/blink/sse2.o: private CFLAGS += -O3

blink/blinkenlights.c

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <time.h>
3535
#include <unistd.h>
3636
#include <wchar.h>
37+
#include "blink/web.h"
3738
#include <wctype.h>
3839

3940
#include "blink/assert.h"
@@ -3659,6 +3660,7 @@ int main(int argc, char *argv[]) {
36593660
int rc;
36603661
struct System *s;
36613662
static struct sigaction sa;
3663+
SetupWeb();
36623664
g_blink_path = argc > 0 ? argv[0] : 0;
36633665
react = true;
36643666
tuimode = true;

blink/close.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static int CloseFd(struct System *s, struct Fd *fd) {
3838
if (fd->dirstream) {
3939
rc = closedir(fd->dirstream);
4040
} else {
41-
rc = IB(fd->cb->close)(sf);
41+
rc = fd->cb->close(sf);
4242
}
4343
// EINTR shouldn't be possible since we don't support SO_LINGER
4444
unassert(!(rc == -1 && errno == EINTR));

blink/debug.h

-11
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@
66
#include "blink/machine.h"
77
#include "blink/types.h"
88

9-
#if !defined(NDEBUG) && defined(__GNUC__)
10-
#define IB(x) \
11-
__extension__({ \
12-
__typeof__(x) x_ = (x); \
13-
unassert((intptr_t)x_ > 4096); \
14-
x_; \
15-
})
16-
#else
17-
#define IB(x) (x)
18-
#endif
19-
209
void DumpHex(u8 *, size_t);
2110
void PrintFds(struct Fds *);
2211
void LogCpu(struct Machine *);

blink/end.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "blink/jit.h"
44
#include "blink/mop.h"
55

6-
#ifdef __APPLE__
6+
#if defined(__APPLE__) || defined(__EMSCRIPTEN__)
77
#define IMAGE_END ((u8 *)&StartJit)
88
#else
99
extern u8 end[];

blink/ioports.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static int OpE9Read(struct Machine *m) {
2929
struct Fd *fd;
3030
struct iovec t = {&b, 1};
3131
if (!(fd = GetAndLockFd(m, 0))) return -1;
32-
if (IB(fd->cb->readv)(fd->systemfd, &t, 1) == 1) {
32+
if (fd->cb->readv(fd->systemfd, &t, 1) == 1) {
3333
return b;
3434
} else {
3535
return -1;
@@ -41,7 +41,7 @@ static int OpE9Write(struct Machine *m, u8 b) {
4141
struct Fd *fd;
4242
struct iovec t = {&b, 1};
4343
if (!(fd = GetAndLockFd(m, 1))) return -1;
44-
rc = IB(fd->cb->writev)(fd->systemfd, &t, 1);
44+
rc = fd->cb->writev(fd->systemfd, &t, 1);
4545
UnlockFd(fd);
4646
return rc;
4747
}
@@ -53,7 +53,7 @@ static int OpE9Poll(struct Machine *m) {
5353
if (!(fd = GetAndLockFd(m, 0))) return -1;
5454
pf.fd = fd->systemfd;
5555
pf.events = POLLIN | POLLOUT;
56-
rc = IB(fd->cb->poll)(&pf, 1, 20);
56+
rc = fd->cb->poll(&pf, 1, 20);
5757
if (rc > 0) rc = pf.revents;
5858
UnlockFd(fd);
5959
return rc;

blink/memorymalloc.c

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ void FreeBig(void *p, size_t n) {
6464

6565
void *AllocateBig(size_t n) {
6666
void *p;
67+
#if defined(__EMSCRIPTEN__)
68+
p = Mmap(NULL, n, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0, "big");
69+
return p != MAP_FAILED ? p : 0;
70+
#else
6771
u8 *brk;
6872
if (!(brk = atomic_load_explicit(&g_allocator.brk, memory_order_relaxed))) {
6973
// we're going to politely ask the kernel for addresses starting
@@ -87,6 +91,7 @@ void *AllocateBig(size_t n) {
8791
MAP_DEMAND | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0, "big");
8892
} while (p == MAP_FAILED && errno == MAP_DENIED);
8993
return p != MAP_FAILED ? p : 0;
94+
#endif
9095
}
9196

9297
static void FreeHostPages(struct System *s) {

blink/oneoff.c

-41
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,6 @@
2929
#include "blink/types.h"
3030
#include "blink/util.h"
3131

32-
static u64 Vigna(u64 s[1]) {
33-
u64 z = (s[0] += 0x9e3779b97f4a7c15);
34-
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
35-
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
36-
return z ^ (z >> 31);
37-
}
38-
39-
_Noreturn void TerminateSignal(struct Machine *m, int sig) {
40-
abort();
41-
}
42-
43-
u16 add16(u16 x, u16 y, bool *cf, bool *zf) {
44-
u16 z = x + y;
45-
*cf = z < y;
46-
*zf = !z;
47-
return z;
48-
}
49-
50-
u8 add8(u8 x, u8 y, bool *cf, bool *zf) {
51-
u8 z = x + y;
52-
*cf = z < y;
53-
*zf = !z;
54-
return z;
55-
}
56-
5732
int main(int argc, char *argv[]) {
58-
int x, y;
59-
for (x = -128; x < 127; ++x) {
60-
for (y = -128; y < 127; ++y) {
61-
u8 z1, z2;
62-
bool cf1, cf2;
63-
bool zf1, zf2;
64-
z1 = add8((i8)x, (i8)y, &cf1, &zf1);
65-
z2 = add16((i8)x, (i8)y, &cf2, &zf2);
66-
unassert(z1 == z2);
67-
unassert(cf1 == cf2);
68-
if (zf1 != zf2) {
69-
LOGF("%d %d", x, y);
70-
}
71-
unassert(zf1 == zf2);
72-
}
73-
}
7433
return 0;
7534
}

blink/syscall.c

+11-6
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ static i64 SysReadImpl(struct Machine *m, struct Fd *fd, i64 addr, u64 size) {
11191119
unassert(fd->cb);
11201120
InitIovs(&iv);
11211121
if ((rc = AppendIovsReal(m, &iv, addr, size)) != -1) {
1122-
INTERRUPTIBLE(rc = IB(fd->cb->readv)(fd->systemfd, iv.p, iv.i));
1122+
INTERRUPTIBLE(rc = fd->cb->readv(fd->systemfd, iv.p, iv.i));
11231123
if (rc != -1) SetWriteAddr(m, addr, rc);
11241124
}
11251125
FreeIovs(&iv);
@@ -1132,7 +1132,7 @@ static i64 SysWriteImpl(struct Machine *m, struct Fd *fd, i64 addr, u64 size) {
11321132
unassert(fd->cb);
11331133
InitIovs(&iv);
11341134
if ((rc = AppendIovsReal(m, &iv, addr, size)) != -1) {
1135-
INTERRUPTIBLE(rc = IB(fd->cb->writev)(fd->systemfd, iv.p, iv.i));
1135+
INTERRUPTIBLE(rc = fd->cb->writev(fd->systemfd, iv.p, iv.i));
11361136
if (rc != -1) SetReadAddr(m, addr, rc);
11371137
}
11381138
FreeIovs(&iv);
@@ -1213,7 +1213,7 @@ static i64 SysReadv(struct Machine *m, i32 fildes, i64 iovaddr, i32 iovlen) {
12131213
unassert(fd->cb);
12141214
InitIovs(&iv);
12151215
if ((rc = AppendIovsGuest(m, &iv, iovaddr, iovlen)) != -1) {
1216-
INTERRUPTIBLE(rc = IB(fd->cb->readv)(fd->systemfd, iv.p, iv.i));
1216+
INTERRUPTIBLE(rc = fd->cb->readv(fd->systemfd, iv.p, iv.i));
12171217
}
12181218
UnlockFd(fd);
12191219
FreeIovs(&iv);
@@ -1228,7 +1228,7 @@ static i64 SysWritev(struct Machine *m, i32 fildes, i64 iovaddr, i32 iovlen) {
12281228
unassert(fd->cb);
12291229
InitIovs(&iv);
12301230
if ((rc = AppendIovsGuest(m, &iv, iovaddr, iovlen)) != -1) {
1231-
INTERRUPTIBLE(rc = IB(fd->cb->writev)(fd->systemfd, iv.p, iv.i));
1231+
INTERRUPTIBLE(rc = fd->cb->writev(fd->systemfd, iv.p, iv.i));
12321232
}
12331233
UnlockFd(fd);
12341234
FreeIovs(&iv);
@@ -1412,7 +1412,7 @@ static int SysIoctl(struct Machine *m, int fildes, u64 request, i64 addr) {
14121412
int (*func)(int, unsigned long, ...);
14131413
if (!(fd = GetAndLockFd(m, fildes))) return -1;
14141414
unassert(fd->cb);
1415-
func = IB(fd->cb->ioctl);
1415+
func = fd->cb->ioctl;
14161416
systemfd = atomic_load_explicit(&fd->systemfd, memory_order_relaxed);
14171417
switch (request) {
14181418
case TIOCGWINSZ_LINUX:
@@ -1761,7 +1761,12 @@ static int SysWait4(struct Machine *m, int pid, i64 opt_out_wstatus_addr,
17611761
struct rusage hrusage;
17621762
struct rusage_linux grusage;
17631763
if ((options = XlatWait(options)) == -1) return -1;
1764+
#if !defined(__EMSCRIPTEN__)
17641765
INTERRUPTIBLE(rc = wait4(pid, &wstatus, options, &hrusage));
1766+
#else
1767+
memset(&hrusage, 0, sizeof(hrusage));
1768+
INTERRUPTIBLE(rc = waitpid(pid, &wstatus, options));
1769+
#endif
17651770
if (rc != -1) {
17661771
if (opt_out_wstatus_addr) {
17671772
CopyToUserWrite(m, opt_out_wstatus_addr, &wstatus, sizeof(wstatus));
@@ -2191,7 +2196,7 @@ static int SysPoll(struct Machine *m, i64 fdsaddr, u64 nfds, i32 timeout_ms) {
21912196
hfds[0].events = (((ev & POLLIN_LINUX) ? POLLIN : 0) |
21922197
((ev & POLLOUT_LINUX) ? POLLOUT : 0) |
21932198
((ev & POLLPRI_LINUX) ? POLLPRI : 0));
2194-
switch (IB(fd->cb->poll)(hfds, 1, 0)) {
2199+
switch (fd->cb->poll(hfds, 1, 0)) {
21952200
case 0:
21962201
Write16(gfds[i].revents, 0);
21972202
break;

blink/web.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef BLINK_WEB_H_
2+
#define BLINK_WEB_H_
3+
#ifdef __EMSCRIPTEN__
4+
#include <emscripten.h>
5+
6+
static void SetupWeb(void) {
7+
// EM_ASM({FS.mkdir("/cwd"); FS.mount(NODEFS, {root : "."}, "/cwd");});
8+
}
9+
10+
#else
11+
static void SetupWeb(void) {}
12+
#endif /* __EMSCRIPTEN__ */
13+
#endif /* BLINK_WEB_H_ */

0 commit comments

Comments
 (0)