Skip to content

Commit

Permalink
web update
Browse files Browse the repository at this point in the history
Signed-off-by: Shaw Summa <[email protected]>
  • Loading branch information
ShawSumma committed Mar 26, 2024
1 parent b91c074 commit e1c4224
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 211 deletions.
66 changes: 34 additions & 32 deletions core.mak
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@

OPT ?= -Os -flto

EXE ?=
EXE ?=

CURRENT_DIR != pwd

BUILD_DIR ?= $(CURRENT_DIR)/build
BUILD_DIR ?= build
OBJ_DIR ?= $(BUILD_DIR)/obj
TMP_DIR ?= $(BUILD_DIR)/tmp
BIN_DIR ?= $(BUILD_DIR)/bin
RES_DIR ?= $(BUILD_DIR)/res

VENDOR_DIR ?= $(CURRENT_DIR)/vendor
VENDOR_DIR ?= vendor
CUIK_DIR ?= $(VENDOR_DIR)/cuik
ISOCLINE_DIR ?= $(VENDOR_DIR)/isocline
TCC_DIR ?= $(VENDOR_DIR)/tcc
Expand All @@ -20,13 +18,21 @@ XXHASH_DIR ?= $(VENDOR_DIR)/xxhash

UNAME_S != uname -s

OS_NAME_Windows_NT = WINDOWS
OS_NAME_Cygwin = WINDOWS
OS_NAME_Darwin = MACOS
OS_NAME_Linux = LINUX
OS_NAME_FreeBSD = FREEBSD

OS_NAME = $(OS_NAME_$(UNAME_S))

PROG_SRCS = main/minivm.c
PROG_OBJS = $(PROG_SRCS:%.c=$(OBJ_DIR)/%.o)

# GC_SRCS = vendor/bdwgc/alloc.c vendor/bdwgc/allchblk.c vendor/bdwgc/blacklst.c vendor/bdwgc/dbg_mlc.c vendor/bdwgc/dyn_load.c vendor/bdwgc/finalize.c vendor/bdwgc/headers.c vendor/bdwgc/malloc.c vendor/bdwgc/mallocx.c vendor/bdwgc/mark.c vendor/bdwgc/mach_dep.c vendor/bdwgc/mark_rts.c vendor/bdwgc/misc.c vendor/bdwgc/new_hblk.c vendor/bdwgc/obj_map.c vendor/bdwgc/os_dep.c vendor/bdwgc/ptr_chck.c vendor/bdwgc/reclaim.c
GC_OBJS = $(GC_SRCS:%.c=$(OBJ_DIR)/%.o)

TREES_SRCS = $(TREE_SITTER_DIR)/lib/src/alloc.c $(TREE_SITTER_DIR)/lib/src/get_changed_ranges.c $(TREE_SITTER_DIR)/lib/src/language.c $(TREE_SITTER_DIR)/lib/src/lexer.c $(TREE_SITTER_DIR)/lib/src/node.c $(TREE_SITTER_DIR)/lib/src/parser.c $(TREE_SITTER_DIR)/lib/src/query.c $(TREE_SITTER_DIR)/lib/src/stack.c $(TREE_SITTER_DIR)/lib/src/subtree.c $(TREE_SITTER_DIR)/lib/src/tree_cursor.c $(TREE_SITTER_DIR)/lib/src/tree.c $(TREE_SITTER_DIR)/lib/src/wasm_store.c
TREES_SRCS = $(TREE_SITTER_DIR)/lib/src/alloc.c $(TREE_SITTER_DIR)/lib/src/get_changed_ranges.c $(TREE_SITTER_DIR)/lib/src/language.c $(TREE_SITTER_DIR)/lib/src/lexer.c $(TREE_SITTER_DIR)/lib/src/node.c $(TREE_SITTER_DIR)/lib/src/parser.c $(TREE_SITTER_DIR)/lib/src/query.c $(TREE_SITTER_DIR)/lib/src/stack.c $(TREE_SITTER_DIR)/lib/src/subtree.c $(TREE_SITTER_DIR)/lib/src/tree_cursor.c $(TREE_SITTER_DIR)/lib/src/tree.c $(TREE_SITTER_DIR)/lib/src/wasm_store.c

STD_SRCS = vm/std/io.c vm/std/std.c
ISOCLINE_SRCS = $(ISOCLINE_DIR)/src/isocline.c
Expand All @@ -40,8 +46,8 @@ TCC_SRCS ?= $(TCC_DIR)/libtcc.c $(TCC_DIR)/lib/libtcc1.c
TCC_OBJS = $(TCC_SRCS:%.c=$(OBJ_DIR)/%.o)

TB_SRCS_BASE = $(CUIK_DIR)/common/common.c $(CUIK_DIR)/common/perf.c $(CUIK_DIR)/tb/src/libtb.c $(CUIK_DIR)/tb/src/x64/x64_target.c
TB_SRCS_FreeBSD = $(CUIK_DIR)/c11threads/threads_posix.c
TB_SRCS = $(TB_SRCS_BASE) $(TB_SRCS_$(UNAME_S))
TB_SRCS_FREEBSD = $(CUIK_DIR)/c11threads/threads_posix.c
TB_SRCS = $(TB_SRCS_BASE) $(TB_SRCS_$(OS_NAME))
TB_OBJS = $(TB_SRCS:%.c=$(OBJ_DIR)/%.o)

BASE_OBJS = $(ALL_OBJS) $(GC_OBJS) $(TB_OBJS) $(TCC_OBJS)
Expand All @@ -51,16 +57,23 @@ LDFLAGS += $(FLAGS)

OBJS = $(BASE_OBJS)

LDFLAGS_S_Darwin = -w -Wl,-pagezero_size,0x4000
LDFLAGS_S_Linux = -lm -ldl
LDFLAGS_O_Cygwin =
LDFLAGS_S_FreeBSD = -lm -ldl -lpthread
LDFLAGS_WINDOWS =
LDFLAGS_MACOS = -w -Wl,-pagezero_size,0x4000
LDFLAGS_LINUX = -lm -ldl
LDFLAGS_FREEBSD = -lm -ldl -lpthread

LDFLAGS := $(LDFLAGS_$(OS_NAME)) $(LDFLAGS)

LDFLAGS := $(LDFLAGS_S_$(UNAME_S)) $(LDFLAGS_O_$(UNAME_S)) $(LDFLAGS)
# CFLAGS_WINDOWS = -D_WIN32

CFLAGS_O_Cygwin = -D_WIN32
CFLAGS := $(CFLAGS_$(OS_NAME)) $(CFLAGS)

CFLAGS := $(CFLAGS_O_$(UNAME_S)) $(CFLAGS)
MKDIR_WINDOWS = mkdir -p
MKDIR_MACOS = mkdir -p
MKDIR_LINUX = mkdir -p
MKDIR_FREEBSD = mkdir -p

MKDIR = $(MKDIR_$(OS_NAME))

default: all

Expand All @@ -73,7 +86,7 @@ VM_LUA_GRAMMAR_DIR := $(TMP_DIR)/grammar
pre: $(VM_LUA_GRAMMAR_DIR)

$(VM_LUA_GRAMMAR_DIR): vm/lua/parser/grammar.js
mkdir -p $(VM_LUA_GRAMMAR_DIR)
$(MKDIR) $(VM_LUA_GRAMMAR_DIR)
cp vm/lua/parser/grammar.js $(VM_LUA_GRAMMAR_DIR)
cd $(VM_LUA_GRAMMAR_DIR) && cargo run --manifest-path $(TREE_SITTER_DIR)/Cargo.toml -- generate

Expand All @@ -83,43 +96,32 @@ vm/lua/parser/parser.c: $(VM_LUA_GRAMMAR_DIR) vm/lua/parser/tree_sitter
vm/lua/parser/tree_sitter: $(VM_LUA_GRAMMAR_DIR)
cp -r $(VM_LUA_GRAMMAR_DIR)/src/tree_sitter $(@)

# windows

clang-windows: .dummy
rm -rf build
$(MAKE) -Bj$(J) CC=clang EXE=.exe OPT="$(OPT)" CFLAGS="-Icuik/c11threads $(CFLAGS)" LDFLAGS="$(LDFLAGS)" EXTRA_SRCS="$(CUIK_DIR)/c11threads/threads_msvc.c"

gcc-windows: .dummy
rm -rf build
$(MAKE) -Bj$(J) CC=gcc EXE=.exe OPT="$(OPT)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS) -lSynchronization"

# binaries

bins: $(BIN_DIR)/minivm$(EXE)

minivm$(EXE) $(BIN_DIR)/minivm$(EXE): $(OBJ_DIR)/main/minivm.o $(OBJS)
@mkdir -p $$(dirname $(@))
$(MKDIR) $(dir $(@))
$(CC) $(OPT) $(OBJ_DIR)/main/minivm.o $(OBJS) -o $(@) $(LDFLAGS)

# intermediate files

$(TB_OBJS): $(@:$(OBJ_DIR)/%.o=%.c)
@mkdir -p $$(dirname $(@))
$(MKDIR) $(dir $(@))
$(CC) -Wno-unused-value -c $(OPT) $(@:$(OBJ_DIR)/%.o=%.c) -o $(@) $(CFLAGS) -I $(CUIK_DIR)/tb/include -I $(CUIK_DIR)/common -DCUIK_USE_TB -DLOG_SUPPRESS -DTB_HAS_X64

$(TCC_DIR)/config.h: $(TCC_DIR)/configure
cd $(TCC_DIR)/ && ./configure
cd $(TCC_DIR) && ./configure

$(TCC_DIR)/tccdefs_.h: $(TCC_DIR)/include/tccdefs.h $(TCC_DIR)/config.h
# $(MAKE) -C $(TCC_DIR) tccdefs_.h
echo '""' > $(TCC_DIR)/tccdefs_.h

$(TCC_OBJS): $(@:$(OBJ_DIR)/%.o=%.c) $(TCC_DIR) $(TCC_DIR)/tccdefs_.h $(TCC_DIR)/config.h
@mkdir -p $$(dirname $(@))
$(MKDIR) $(dir $(@))
$(CC) -c $(OPT) $(@:$(OBJ_DIR)/%.o=%.c) -o $(@) $(CFLAGS)

$(PROG_OBJS) $(ALL_OBJS) $(GC_OBJS): $(@:$(OBJ_DIR)/%.o=%.c)
@mkdir -p $$(dirname $(@))
$(MKDIR) $(dir $(@))
$(CC) -c $(OPT) $(@:$(OBJ_DIR)/%.o=%.c) -o $(@) $(CFLAGS)

# format
Expand Down
2 changes: 1 addition & 1 deletion vendor/cuik
16 changes: 16 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>MiniVM</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="icon" href="data://">
<link rel="stylesheet" href="src/app/global.css">
</head>

<body>
<script type="module" src="src/index.js"></script>
</body>

</html>
20 changes: 6 additions & 14 deletions web/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
{
"dependencies": {
"@babel/preset-env": "^7.23.9",
"babel-loader": "^9.1.3",
"compression-webpack-plugin": "^11.0.0",
"copy-webpack-plugin": "^12.0.2",
"css-loader": "^6.10.0",
"emception": "^1.0.11",
"mini-css-extract-plugin": "^2.8.0",
"svelte": "^4.2.10",
"svelte-loader": "^3.1.9",
"webpack": "^5.90.1",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1",
"webpack-node": "^0.0.0",
"worker-loader": "^3.0.8"
"@sveltejs/vite-plugin-svelte": "^3.0.2",
"emception": "^1.0.15",
"svelte": "^4.2.12",
"vite": "^5.2.6",
"vite-plugin-top-level-await": "^1.4.1",
"vite-plugin-wasm": "^3.3.0"
},
"scripts": {
"build": "npx webpack",
Expand Down
26 changes: 0 additions & 26 deletions web/src/index.html

This file was deleted.

4 changes: 0 additions & 4 deletions web/src/lib/lua.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@

import Module from '../../../build/bin/minivm.js';
import wasmBinary from '../../../build/bin/minivm.wasm';

const wasmBuffer = await (await fetch(wasmBinary)).arrayBuffer();

export const run = (args, opts) => {
const stdinFunc = () => {
Expand All @@ -27,7 +24,6 @@ export const run = (args, opts) => {

const mod = Module({
noInitialRun: true,
wasmBinary: wasmBuffer,
preRun: (mod) => {
mod.FS.init(stdinFunc, stdoutFunc, stderrFunc);
},
Expand Down
42 changes: 34 additions & 8 deletions web/src/lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let comp;
const thens = [];

const waitForComp = () => {
comp = new Worker(new URL(/* webpackChunkName: "wcomp" */ '../lib/wcomp.js', import.meta.url));
comp = new Worker(new URL(/* webpackChunkName: "wcomp" */ '../lib/wcomp.js', import.meta.url), {type: 'module'});
return new Promise((ok, err) => {
comp.onmessage = ({data}) => {
switch (data.type) {
Expand All @@ -31,24 +31,49 @@ const unmap = (c) => {
}
};

const wait = (ms) => new Promise((res, rej) => {
setTimeout(res, ms);
});

const waitAsync = async (array, index, value) => {
if (Atomics.waitAsync != null) {
await Atomics.waitAsync(array, index, value);
} else {
let time = 0;
while (true) {
const got = Atomics.wait(array, index, value, 0);
if (got !== 'timed-out') {
return got;
}
if (time < 20) {
time += 1;
}
await wait(time);
}
}
};

export const repl = ({putchar}) => {
const hasComp = waitForComp();
const obj = {};
obj.putchar = putchar;
const has = new SharedArrayBuffer(4);
const want = new SharedArrayBuffer(4);
const inbuf = new SharedArrayBuffer(4);
obj.input = async (str) => {
const i32buf = new Int32Array(inbuf);
const i32want = new Int32Array(want);
const inbuf32 = new Int32Array(inbuf);
const want32 = new Int32Array(want);
const has32 = new Int32Array(has);
for (const c of str) {
await Atomics.waitAsync(i32want, 0, 0).value;
i32want[0] = 0;
i32buf[0] = typeof c === 'number' ? c : c.charCodeAt(0);
Atomics.notify(i32buf, 0, 1);
await Atomics.waitAsync(want32, 0, 0).value;
want32[0] = 0;
inbuf32[0] = typeof c === 'string' ? c.charCodeAt(0) : c;
has32[0] = 0
Atomics.notify(has32, 0, 1);
}
};
obj.start = async() => {
const worker = new Worker(new URL(/* webpackChunkName: "wlua" */ '../lib/wlua.js', import.meta.url));
const worker = new Worker(new URL(/* webpackChunkName: "wlua" */ '../lib/wlua.js', import.meta.url), {type: 'module'});
const wait = new SharedArrayBuffer(4);
const ret = new SharedArrayBuffer(65536);
const number = thens.length;
Expand Down Expand Up @@ -93,6 +118,7 @@ export const repl = ({putchar}) => {
wait: wait,
inbuf: inbuf,
want: want,
has: has,
});
break;
}
Expand Down
24 changes: 13 additions & 11 deletions web/src/lib/wlua.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@

import {run} from './lua.js';

const n = (1 << 32) - 1;

let has;
let want;
let inbuf;
const stdin = () => {
const i32buf = new Int32Array(inbuf);
const i32want = new Int32Array(want);
i32want[0] = 1;
Atomics.notify(i32want, 0, 1);
Atomics.wait(i32buf, 0, i32buf[0]);
return i32buf[0];
const inbuf32 = new Int32Array(inbuf);
const want32 = new Int32Array(want);
const has32 = new Int32Array(has);
want32[0] = 1;
Atomics.notify(want32, 0, 1);
Atomics.wait(has32, 0, 0);
has32[0] = 0;
return inbuf32[0];
};

const stdout = (msg) => {
Expand Down Expand Up @@ -57,12 +62,9 @@ self.onmessage = ({data}) => {
self.postMessage({type: 'get-args'});
wait = data.wait;
ret = data.ret;
inbuf = data.inbuf;
has = data.has;
want = data.want;
break;
}
case 'stdin': {

inbuf = data.inbuf;
break;
}
case 'args': {
Expand Down
7 changes: 7 additions & 0 deletions web/svelte.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte";

export default {
// Consult https://svelte.dev/docs#compile-time-svelte-preprocess
// for more information about preprocessors
preprocess: vitePreprocess(),
};
28 changes: 28 additions & 0 deletions web/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { defineConfig } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import wasm from "vite-plugin-wasm";
import topLevelAwait from "vite-plugin-top-level-await";

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
svelte(),
wasm(),
topLevelAwait(),
],
base: './',
worker: {
format: 'es',
},
build: {
outDir: 'public',
rollupOptions: {
output: {
format: "es",
},
},
},
assetsInclude: [
'**/*.pack',
],
});
Loading

0 comments on commit e1c4224

Please sign in to comment.