standalone: Windows support

This commit is contained in:
Remko Tronçon 2022-10-08 09:16:30 +02:00
parent 344e926531
commit ca337981ae
10 changed files with 119 additions and 42 deletions

View file

@ -15,9 +15,5 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/setup
- run: make -C src/standalone install-deps all
# TODO: Windows support
if: runner.os != 'Windows'
- run: make -C src/standalone install-deps package
- run: make -C src/standalone check
# TODO: Windows support
if: runner.os != 'Windows'

View file

@ -12,7 +12,7 @@ jobs:
needs: build
strategy:
matrix:
os: [macos-latest, ubuntu-18.04]
os: [macos-latest, ubuntu-18.04, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2

View file

@ -39,9 +39,9 @@ document](doc/Design.md).
<figcaption><em><a href="https://mko.re/waforth/">WAForth console</a></em></figcaption>
</div>
<div align="center" style="margin-top: 2em">
<div align="center">
<div>
<a href="https://mko.re/thurtle/"><img style="width: 550px" src="https://raw.githubusercontent.com/remko/waforth/master/doc/thurtle.png" alt="Thurtle program"></a>
<a href="https://mko.re/thurtle/"><img style="width: 550px; margin-top: 1.5em;" src="https://raw.githubusercontent.com/remko/waforth/master/doc/thurtle.png" alt="Thurtle program"></a>
</div>
<figcaption><em>WAForth integrated in <a href="https://mko.re/thurtle/">Thurtle</a>, a <a href="https://en.wikipedia.org/wiki/Turtle_graphics">turtle graphics</a> programming environment using Forth</em></figcaption>
</div>
@ -59,6 +59,12 @@ WebAssembly engine that supports the
[WebAssembly C API](https://github.com/WebAssembly/wasm-c-api) (although some
engines have [known issues](https://github.com/remko/waforth/issues/6#issue-326830993)).
<div align="center">
<div>
<a href="https://mko.re/thurtle/"><img style="width: 550px;" src="https://raw.githubusercontent.com/remko/waforth/master/doc/standalone.png" alt="Thurtle program"></a>
</div>
<figcaption><em>Standalone WAForth shell executable</em></figcaption>
</div>
## Using WAForth in a JavaScript application

BIN
doc/standalone.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

View file

@ -2,4 +2,4 @@
/waforth_core.h
/wasm-micro-runtime
/wasmtime-*
/*.tgz
/*.tgz

View file

@ -1,15 +1,7 @@
UNAME_S=$(shell uname -s)
UNAME_P=$(shell uname -p)
ifeq (, $(shell which gtar))
TAR := tar
else
# bsd-tar corrupts files on GitHub: https://github.com/actions/virtual-environments/issues/2619
TAR := gtar
endif
.DEFAULT_GOAL := all
VERSION?=$(shell cat ../../package.json | grep '"version"' | sed -e 's/.*:.*"\(.*\)".*/\1/')
################################################
# WebAssembly engine configuration
################################################
@ -23,37 +15,68 @@ LIBS:=$(WASMER_DIR)/lib/libwasmer.a
else
# Wasmtime (https://wasmtime.dev)
ifeq ($(UNAME_S),Darwin)
WASMTIME_DIR=wasmtime-v0.37.0-x86_64-macos-c-api
WASMTIME_RELEASE_URL=https://github.com/bytecodealliance/wasmtime/releases/download/v0.37.0/wasmtime-v0.37.0-x86_64-macos-c-api.tar.xz
ifeq ($(OS),Windows_NT)
WASMTIME_DIR=wasmtime-v1.0.0-x86_64-mingw-c-api
WASMTIME_RELEASE_URL=https://github.com/bytecodealliance/wasmtime/releases/download/v1.0.0/wasmtime-v1.0.0-x86_64-mingw-c-api.zip
CC=gcc
CFLAGS=-DWASM_API_EXTERN=
LIBS=-luserenv -lole32 -lntdll -lws2_32 -lkernel32 -lbcrypt
RESOURCE_OBJECTS=waforth.res
install-deps:
-rm -rf wasmtime-*
curl -o $(WASMTIME_DIR).zip -L -s $(WASMTIME_RELEASE_URL)
unzip $(WASMTIME_DIR).zip
package: waforth
7z a -tzip waforth-v$(VERSION)-windows.zip waforth.exe
comma:=,
%.res: %.rc
windres --use-temp-file $< -DVERSION_STRING='"$(VERSION)"' -DVERSION_TUPLE='$(subst .,$(comma),$(VERSION)),0' -O coff -o $@
else
WASMTIME_DIR=wasmtime-v0.37.0-x86_64-linux-c-api
WASMTIME_RELEASE_URL=https://github.com/bytecodealliance/wasmtime/releases/download/v0.37.0/wasmtime-v0.37.0-x86_64-linux-c-api.tar.xz
LIBS=-lpthread -lm -ldl
ifeq (, $(shell which gtar))
TAR := tar
else
# bsd-tar corrupts files on GitHub: https://github.com/actions/virtual-environments/issues/2619
TAR := gtar
endif
UNAME_S=$(shell uname -s)
UNAME_P=$(shell uname -p)
ifeq ($(UNAME_S),Darwin)
WASMTIME_DIR=wasmtime-v1.0.1-x86_64-macos-c-api
WASMTIME_RELEASE_URL=https://github.com/bytecodealliance/wasmtime/releases/download/v1.0.1/wasmtime-v1.0.1-x86_64-macos-c-api.tar.xz
PACKAGE_SUFFIX=x86_64-macos
else
WASMTIME_DIR=wasmtime-v1.0.1-x86_64-linux-c-api
WASMTIME_RELEASE_URL=https://github.com/bytecodealliance/wasmtime/releases/download/v1.0.1/wasmtime-v1.0.1-x86_64-linux-c-api.tar.xz
LIBS=-lpthread -lm -ldl
PACKAGE_SUFFIX=x86_64-linux
endif
CFLAGS=-I$(WASMTIME_DIR)/include
LIBS:=$(WASMTIME_DIR)/lib/libwasmtime.a $(LIBS)
install-deps:
-rm -rf wasmtime-*
curl -L -s $(WASMTIME_RELEASE_URL) | tar xJv
package: waforth
$(TAR) czf waforth-v$(VERSION)-$(PACKAGE_SUFFIX).tgz waforth
endif
endif
CFLAGS:=-I$(WASMTIME_DIR)/include $(CFLAGS)
LIBS:=$(WASMTIME_DIR)/lib/libwasmtime.a $(LIBS)
################################################
ifeq ($(UNAME_S),Darwin)
PACKAGE_SUFFIX=x86_64-macos
else
PACKAGE_SUFFIX=x86_64-linux
endif
VERSION?=$(shell cat ../../package.json | grep '"version"' | sed -e 's/.*:.*"\(.*\)".*/\1/')
BIN2H=../../scripts/bin2h
WAT2WASM=wat2wasm
WAT2WASM_FLAGS=--debug-names
CFLAGS:=-DVERSION='"$(VERSION)"' $(CFLAGS)
OBJECTS=main.o
OBJECTS=main.o $(RESOURCE_OBJECTS)
all: waforth
@ -62,17 +85,16 @@ waforth: $(OBJECTS)
main.o: waforth_core.h
icon.ico: ../../doc/logo.svg
convert -background transparent $< -define icon:auto-resize=16,32,48,64,256 $@
waforth_core.wasm: ../waforth.wat
$(WAT2WASM) $(WAT2WASM_FLAGS) -o $@ $<
waforth_core.h: waforth_core.wasm
$(BIN2H) $< $@
.PHONY: install-deps
.PHONY: package
package: waforth
$(TAR) czf waforth-v$(VERSION)-$(PACKAGE_SUFFIX).tgz waforth
.PHONY: install-deps package
.PHONY: check
check:

View file

@ -1,4 +1,4 @@
# Standalone native WAForth binary
# Standalone native WAForth executable
This directory contains a small C program to run the WAForth WebAssembly core
in a native WebAssembly engine.
@ -9,6 +9,13 @@ WebAssembly engine that supports the
[WebAssembly C API](https://github.com/WebAssembly/wasm-c-api) (although some
engines have [known issues](https://github.com/remko/waforth/issues/6#issue-326830993)).
<div align="center">
<div>
<a href="https://mko.re/thurtle/"><img style="width: 550px;" src="https://raw.githubusercontent.com/remko/waforth/master/doc/standalone.png" alt="Thurtle program"></a>
</div>
<figcaption><em>Standalone WAForth shell executable</em></figcaption>
</div>
## Download
You can download a pre-built binary of the standalone shell from

BIN
src/standalone/icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View file

@ -1,8 +1,12 @@
#include <stdio.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#include <windows.h>
#else
#include <termios.h>
#endif
#include "waforth_core.h"
#include "wasm.h"
#include <stdio.h>
#ifndef VERSION
#define VERSION "dev"
@ -63,6 +67,19 @@ wasm_trap_t *read_cb(const wasm_val_vec_t *args, wasm_val_vec_t *results) {
}
wasm_trap_t *key_cb(const wasm_val_vec_t *args, wasm_val_vec_t *results) {
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
if (h == NULL) {
return trap_from_string("no console");
}
DWORD mode;
GetConsoleMode(h, &mode);
SetConsoleMode(h, mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT));
TCHAR ch = 0;
DWORD cc;
ReadConsole(h, &ch, 1, &cc, NULL);
SetConsoleMode(h, mode);
#else
struct termios old, current;
tcgetattr(0, &old);
current = old;
@ -71,6 +88,7 @@ wasm_trap_t *key_cb(const wasm_val_vec_t *args, wasm_val_vec_t *results) {
tcsetattr(0, TCSANOW, &current);
char ch = getchar();
tcsetattr(0, TCSANOW, &old);
#endif
results->data[0].kind = WASM_I32;
results->data[0].of.i32 = ch;
return NULL;

28
src/standalone/waforth.rc Normal file
View file

@ -0,0 +1,28 @@
#pragma code_page(65001)
id ICON "icon.ico"
// See https://learn.microsoft.com/en-us/windows/win32/menurc/versioninfo-resource
1 VERSIONINFO
FILEVERSION VERSION_TUPLE
PRODUCTVERSION VERSION_TUPLE
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "0409FDE9"
BEGIN
VALUE "CompanyName", "Remko Tronçon"
VALUE "FileDescription", "WAForth"
VALUE "FileVersion", VERSION_STRING
VALUE "InternalName", "waforth"
VALUE "LegalCopyright", "© 2022 Remko Tronçon"
VALUE "OriginalFilename", "waforth.exe"
VALUE "ProductName", "WAForth"
VALUE "ProductVersion", VERSION_STRING
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 65001
END
END