Skip to content

Commit 512332d

Browse files
committed
Initial commit
0 parents  commit 512332d

27 files changed

+959
-0
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Source code for the webvm.io webpage.
2+
3+
Please use github.com/webvm/issues to signal any bug.
4+
5+
6+
This project depends on xterm.js (https://xtermjs.org/) and on its add-on xterm-addon-fit
7+
8+
To update the xterm-related files do:
9+
```
10+
mkdir build
11+
cd build
12+
npm install --save xterm
13+
npm install --save xterm-addon-fit
14+
cd ../xterm
15+
cp ../build/node_modules/xterm/lib/xterm.js .
16+
cp ../build/node_modules/xterm/css/xterm.css .
17+
cp ../build/node_modules/xterm-addon-fit/lib/xterm-addon-fit.js .
18+
cd ..
19+
rm -r build
20+
```

assets/webvm.jpeg

102 KB
Loading

examples/c/Makefile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
SRCS = $(wildcard *.c)
2+
3+
PROGS = $(patsubst %.c,%,$(SRCS))
4+
5+
all: $(PROGS)
6+
7+
%: %.c
8+
$(CC) $(CFLAGS) -o $@ $<
9+
10+
clean:
11+
rm -f $(PROGS)
12+
13+
.PHONY: all clean

examples/c/env.c

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdio.h>
2+
3+
// Most of the C compilers support a third parameter to main which
4+
// store all envorinment variables
5+
int main(int argc, char *argv[], char * envp[])
6+
{
7+
int i;
8+
for (i = 0; envp[i] != NULL; i++)
9+
printf("\n%s", envp[i]);
10+
getchar();
11+
return 0;
12+
}

examples/c/helloworld.c

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <stdio.h>
2+
3+
int main()
4+
{
5+
printf("Hello, World!\n");
6+
}

examples/c/link.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <unistd.h>
2+
3+
int main()
4+
{
5+
link("env", "env3");
6+
return 0;
7+
}

examples/c/openat.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <fcntl.h>
2+
#include <stdio.h>
3+
#include <errno.h>
4+
5+
int main()
6+
{
7+
int ret = openat(AT_FDCWD, "/dev/tty", 0x88102, 0);
8+
printf("return value is %d and errno is %d\n", ret, errno);
9+
}
10+

examples/c/waitpid.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <sys/wait.h>
2+
#include <unistd.h>
3+
#include <errno.h>
4+
#include <stdio.h>
5+
6+
int main()
7+
{
8+
int status;
9+
10+
pid_t p = getpid();
11+
// waitpid takes a children's pid, not the current process one
12+
// if the pid is not a children of the current process, it returns -ECHILD
13+
pid_t res = waitpid(1001, &status, WNOHANG);
14+
15+
printf("res is %d, p is %d and errno is %d\n", res, p, errno);
16+
17+
}

examples/lua/fizzbuzz.lua

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env luajit
2+
cfizz,cbuzz=0,0
3+
for i=1,20 do
4+
cfizz=cfizz+1
5+
cbuzz=cbuzz+1
6+
io.write(i .. ": ")
7+
if cfizz~=3 and cbuzz~=5 then
8+
io.write(i)
9+
else
10+
if cfizz==3 then
11+
io.write("Fizz")
12+
cfizz=0
13+
end
14+
if cbuzz==5 then
15+
io.write("Buzz")
16+
cbuzz=0
17+
end
18+
end
19+
io.write("\n")
20+
end

examples/lua/sorting.lua

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fruits = {"banana","orange","apple","grapes"}
2+
3+
for k,v in ipairs(fruits) do
4+
print(k,v)
5+
end
6+
7+
table.sort(fruits)
8+
print("sorted table")
9+
10+
for k,v in ipairs(fruits) do
11+
print(k,v)
12+
end

examples/lua/symmetric_difference.lua

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
A = { ["John"] = true, ["Bob"] = true, ["Mary"] = true, ["Elena"] = true }
2+
B = { ["Jim"] = true, ["Mary"] = true, ["John"] = true, ["Bob"] = true }
3+
4+
A_B = {}
5+
for a in pairs(A) do
6+
if not B[a] then A_B[a] = true end
7+
end
8+
9+
B_A = {}
10+
for b in pairs(B) do
11+
if not A[b] then B_A[b] = true end
12+
end
13+
14+
for a_b in pairs(A_B) do
15+
print( a_b )
16+
end
17+
for b_a in pairs(B_A) do
18+
print( b_a )
19+
end

examples/nodejs/environment.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
console.log("process.uptime = ", global.process.uptime());
2+
console.log("process.title = ", global.process.title);
3+
console.log("process version = ", global.process.version);
4+
console.log("process.platform = ", global.process.platform);
5+
console.log("process.cwd = ", global.process.cwd());
6+
console.log("process.uptime = ", global.process.uptime());

examples/nodejs/nbody.js

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
const PI = Math.PI;
2+
const SOLAR_MASS = 4 * PI * PI;
3+
const DAYS_PER_YEAR = 365.24;
4+
5+
function Body(x, y, z, vx, vy, vz, mass) {
6+
this.x = x;
7+
this.y = y;
8+
this.z = z;
9+
this.vx = vx;
10+
this.vy = vy;
11+
this.vz = vz;
12+
this.mass = mass;
13+
}
14+
15+
function Jupiter() {
16+
return new Body(
17+
4.84143144246472090e+00,
18+
-1.16032004402742839e+00,
19+
-1.03622044471123109e-01,
20+
1.66007664274403694e-03 * DAYS_PER_YEAR,
21+
7.69901118419740425e-03 * DAYS_PER_YEAR,
22+
-6.90460016972063023e-05 * DAYS_PER_YEAR,
23+
9.54791938424326609e-04 * SOLAR_MASS
24+
);
25+
}
26+
27+
function Saturn() {
28+
return new Body(
29+
8.34336671824457987e+00,
30+
4.12479856412430479e+00,
31+
-4.03523417114321381e-01,
32+
-2.76742510726862411e-03 * DAYS_PER_YEAR,
33+
4.99852801234917238e-03 * DAYS_PER_YEAR,
34+
2.30417297573763929e-05 * DAYS_PER_YEAR,
35+
2.85885980666130812e-04 * SOLAR_MASS
36+
);
37+
}
38+
39+
function Uranus() {
40+
return new Body(
41+
1.28943695621391310e+01,
42+
-1.51111514016986312e+01,
43+
-2.23307578892655734e-01,
44+
2.96460137564761618e-03 * DAYS_PER_YEAR,
45+
2.37847173959480950e-03 * DAYS_PER_YEAR,
46+
-2.96589568540237556e-05 * DAYS_PER_YEAR,
47+
4.36624404335156298e-05 * SOLAR_MASS
48+
);
49+
}
50+
51+
function Neptune() {
52+
return new Body(
53+
1.53796971148509165e+01,
54+
-2.59193146099879641e+01,
55+
1.79258772950371181e-01,
56+
2.68067772490389322e-03 * DAYS_PER_YEAR,
57+
1.62824170038242295e-03 * DAYS_PER_YEAR,
58+
-9.51592254519715870e-05 * DAYS_PER_YEAR,
59+
5.15138902046611451e-05 * SOLAR_MASS
60+
);
61+
}
62+
63+
function Sun() {
64+
return new Body(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, SOLAR_MASS);
65+
}
66+
67+
const bodies = Array(Sun(), Jupiter(), Saturn(), Uranus(), Neptune());
68+
69+
function offsetMomentum() {
70+
let px = 0;
71+
let py = 0;
72+
let pz = 0;
73+
const size = bodies.length;
74+
for (let i = 0; i < size; i++) {
75+
const body = bodies[i];
76+
const mass = body.mass;
77+
px += body.vx * mass;
78+
py += body.vy * mass;
79+
pz += body.vz * mass;
80+
}
81+
82+
const body = bodies[0];
83+
body.vx = -px / SOLAR_MASS;
84+
body.vy = -py / SOLAR_MASS;
85+
body.vz = -pz / SOLAR_MASS;
86+
}
87+
88+
function advance(dt) {
89+
const size = bodies.length;
90+
91+
for (let i = 0; i < size; i++) {
92+
const bodyi = bodies[i];
93+
let vxi = bodyi.vx;
94+
let vyi = bodyi.vy;
95+
let vzi = bodyi.vz;
96+
for (let j = i + 1; j < size; j++) {
97+
const bodyj = bodies[j];
98+
const dx = bodyi.x - bodyj.x;
99+
const dy = bodyi.y - bodyj.y;
100+
const dz = bodyi.z - bodyj.z;
101+
102+
const d2 = dx * dx + dy * dy + dz * dz;
103+
const mag = dt / (d2 * Math.sqrt(d2));
104+
105+
const massj = bodyj.mass;
106+
vxi -= dx * massj * mag;
107+
vyi -= dy * massj * mag;
108+
vzi -= dz * massj * mag;
109+
110+
const massi = bodyi.mass;
111+
bodyj.vx += dx * massi * mag;
112+
bodyj.vy += dy * massi * mag;
113+
bodyj.vz += dz * massi * mag;
114+
}
115+
bodyi.vx = vxi;
116+
bodyi.vy = vyi;
117+
bodyi.vz = vzi;
118+
}
119+
120+
for (let i = 0; i < size; i++) {
121+
const body = bodies[i];
122+
body.x += dt * body.vx;
123+
body.y += dt * body.vy;
124+
body.z += dt * body.vz;
125+
}
126+
}
127+
128+
function energy() {
129+
let e = 0;
130+
const size = bodies.length;
131+
132+
for (let i = 0; i < size; i++) {
133+
const bodyi = bodies[i];
134+
135+
e += 0.5 * bodyi.mass * ( bodyi.vx * bodyi.vx + bodyi.vy * bodyi.vy + bodyi.vz * bodyi.vz );
136+
137+
for (let j = i + 1; j < size; j++) {
138+
const bodyj = bodies[j];
139+
const dx = bodyi.x - bodyj.x;
140+
const dy = bodyi.y - bodyj.y;
141+
const dz = bodyi.z - bodyj.z;
142+
143+
const distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
144+
e -= (bodyi.mass * bodyj.mass) / distance;
145+
}
146+
}
147+
return e;
148+
}
149+
150+
const n = +50000000;
151+
152+
offsetMomentum();
153+
154+
console.log(energy().toFixed(9));
155+
const start = Date.now();
156+
for (let i = 0; i < n; i++) {
157+
advance(0.01);
158+
}
159+
const end = Date.now();
160+
console.log(energy().toFixed(9));
161+
console.log("elapsed:",end-start);

examples/nodejs/primes.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(function () {
2+
3+
function isPrime(p) {
4+
const upper = Math.sqrt(p);
5+
for(let i = 2; i <= upper; i++) {
6+
if (p % i === 0 ) {
7+
return false;
8+
}
9+
}
10+
return true;
11+
}
12+
13+
// Return n-th prime
14+
function prime(n) {
15+
if (n < 1) {
16+
throw Error("n too small: " + n);
17+
}
18+
let count = 0;
19+
let result = 1;
20+
while(count < n) {
21+
result++;
22+
if (isPrime(result)) {
23+
count++;
24+
}
25+
}
26+
return result;
27+
}
28+
29+
console.log("your prime is ", prime(100000));
30+
31+
}());

examples/nodejs/wasm.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(function (){
2+
let bytes = new Uint8Array([
3+
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
4+
0x01, 0x07, 0x01, 0x60, 0x02, 0x7f, 0x7f, 0x01,
5+
0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01,
6+
0x03, 0x73, 0x75, 0x6d, 0x00, 0x00, 0x0a, 0x0a,
7+
0x01, 0x08, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a,
8+
0x0f, 0x0b
9+
]);
10+
11+
console.log(bytes);
12+
let mod = new WebAssembly.Module(bytes);
13+
let instance = new WebAssembly.Instance(mod, {});
14+
console.log(instance.exports);
15+
return instance.exports.sum(2020, 1);
16+
}());

examples/python3/factorial.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def factorial():
2+
f, n = 1, 1
3+
while True: # First iteration:
4+
yield f # yield 1 to start with and then
5+
f, n = f * n, n+1 # f will now be 1, and n will be 2, ...
6+
7+
for index, factorial_number in zip(range(51), factorial()):
8+
print('{i:3}!= {f:65}'.format(i=index, f=factorial_number))
9+

examples/python3/fibonacci.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def fib():
2+
a, b = 0, 1
3+
while True: # First iteration:
4+
yield a # yield 0 to start with and then
5+
a, b = b, a + b # a will now be 1, and b will also be 1, (0 + 1)
6+
7+
for index, fibonacci_number in zip(range(100), fib()):
8+
print('{i:3}: {f:3}'.format(i=index, f=fibonacci_number))
9+

examples/python3/pi.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from decimal import Decimal, getcontext
2+
getcontext().prec=60
3+
summation = 0
4+
for k in range(50):
5+
summation = summation + 1/Decimal(16)**k * (
6+
Decimal(4)/(8*k+1)
7+
- Decimal(2)/(8*k+4)
8+
- Decimal(1)/(8*k+5)
9+
- Decimal(1)/(8*k+6)
10+
)
11+
print(summation)
12+

0 commit comments

Comments
 (0)