-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibwebfuck.js
107 lines (99 loc) · 3.35 KB
/
libwebfuck.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const WFCOMPLIANCE = "WebFuckV3";
const WFIMPL = "libwebfuck.js";
// 512K of memory should be enough for anyone.
const wfmem = new Uint8Array(524288);
let wfaltptrstack = [];
let wfptr = 0;
let wfoutbuf = "";
class WebFuckModule {
/** @type string */
name;
/** @type string */
src;
constructor(name, src) {
this.name = name;
this.src = src;
}
/**
* Call this WebFuck module.
*/
call() {
let loopstart = -1;
for (let i = 0; i < this.src.length; i++) {
let c = this.src[i];
switch (c) {
case '<': // SINCE: BrainFuck
wfptr--;
break;
case '>': // SINCE: BrainFuck
wfptr++;
break;
case '+': // SINCE: BrainFuck
wfmem[wfptr]++;
break;
case '-': // SINCE: BrainFuck
wfmem[wfptr]--;
break;
case '.': // SINCE: BrainFuck
wfoutbuf += String.fromCharCode(wfmem[wfptr]);
break;
case '[': // SINCE: BrainFuck
loopstart = i;
break;
case ']': // SINCE: BrainFuck
if (wfmem[wfptr] != 0) {
i = loopstart;
}
break;
case '0': // SINCE: WebFuckV2
wfptr = 0;
loopstart = -1;
break;
case '@': // SINCE: WebFuckV2, DEPRECATED: WebFuckV2
console.warn("Even though the @ command was added in this version of WebFuck (WebFuckV2), it is deprecated. Don't use it.");
wfmem.fill(0, 0, 524288);
break;
case '$': // SINCE: WebFuckV1
eval(wfoutbuf);
break;
case '&': // SINCE: WebFuckV3
wfaltptrstack.push(wfptr);
break;
case '*': // SINCE: WebFuckV3
wfptr = wfaltptrstack.pop();
break;
case '?': // SINCE: WebFuckV3
console.log(`===== Begin WebFuck State Dump =====`);
console.log(`WFCOMPLIANCE: ${WFCOMPLIANCE}`);
console.log(`WFIMPL: ${WFIMPL}`);
console.log(`WFPTR: ${wfptr}`);
console.log(`WFALTPTRSTACK: ${wfaltptrstack}`);
console.log(`WFOUTBUF: ${wfoutbuf}`);
console.log(`WFMEM: ${wfmem}`);
console.log(`===== End WebFuck State Dump =====`);
break;
case '_': // SINCE: WebFuckV1
wfoutbuf = "";
break;
}
}
}
}
/**
* Load a WebFuck module with the given file name.
*/
async function loadWebFuck(webfuck) {
let res = await fetch(
`/${webfuck}`, {
headers: {
"WebFuck-Version": WFCOMPLIANCE,
"WFCompliance": WFCOMPLIANCE,
"WFImplementation": WFIMPL,
}
});
if (res.status != 200) {
throw "the response is web-fucked up";
}
let text = await res.text();
return new WebFuckModule(webfuck, text);
}