From 459c13313739f53f6718ca0407d9ecab5f2ea87c Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Fri, 10 Dec 2021 10:43:51 -0600 Subject: [PATCH] added VERY basic implementation of onyxrun --- build.bat | 15 +++++++++++++-- build.sh | 40 +++++++++++++++++++++++++--------------- src/onyxrun.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 17 deletions(-) create mode 100644 src/onyxrun.c diff --git a/build.bat b/build.bat index 6ade20ec..cf2dbe6e 100644 --- a/build.bat +++ b/build.bat @@ -1,5 +1,6 @@ @echo off +REM Compile the compiler set SOURCE_FILES=src/onyx.c src/astnodes.c src/builtins.c src/checker.c src/clone.c src/doc.c src/entities.c src/errors.c src/lex.c src/parser.c src/symres.c src/types.c src/utils.c src/wasm_emit.c src/wasm_runtime.c if "%1" == "1" ( @@ -13,11 +14,21 @@ for /f "delims=" %%i in ('cd') do set PWD=%%i set LINK_OPTIONS="%PWD%\lib\windows_x86_64\lib\wasmer.lib" ws2_32.lib Advapi32.lib userenv.lib bcrypt.lib /NODEFAULTLIB:msvcrt.lib /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:msvcrtd.lib set FLAGS=%FLAGS% "/I%PWD%\lib\common\include" /DENABLE_RUN_WITH_WASMER=1 +rc.exe misc/icon_resource.rc +cl.exe %FLAGS% /Iinclude /std:c17 /TC %SOURCE_FILES% /link /IGNORE:4217 %LINK_OPTIONS% /DEBUG /OUT:onyx.exe /incremental:no /opt:ref /subsystem:console misc\icon_resource.res + del *.pdb > NUL 2> NUL del *.ilk > NUL 2> NUL +del *.obj > NUL 2> NUL +del misc\icon_resource.res + +REM Compile the onyxrun tool +set SOURCE_FILES=src/onyxrun.c src/wasm_runtime.c rc.exe misc/icon_resource.rc -cl.exe %FLAGS% /Iinclude /std:c17 /TC %SOURCE_FILES% /link /IGNORE:4217 %LINK_OPTIONS% /DEBUG /OUT:onyx.exe /incremental:no /opt:ref /subsystem:console misc\icon_resource.res +cl.exe %FLAGS% /Iinclude /std:c17 /TC %SOURCE_FILES% /link /IGNORE:4217 %LINK_OPTIONS% /DEBUG /OUT:onyxrun.exe /incremental:no /opt:ref /subsystem:console misc\icon_resource.res +del *.pdb > NUL 2> NUL +del *.ilk > NUL 2> NUL del *.obj > NUL 2> NUL -del misc\icon_resource.res \ No newline at end of file +del misc\icon_resource.res diff --git a/build.sh b/build.sh index 5defd7c1..022579c2 100755 --- a/build.sh +++ b/build.sh @@ -50,23 +50,33 @@ INCLUDES="-I$WASMER_INCLUDE_DIR" mkdir -p "$BUILD_DIR" -for file in $C_FILES ; do - echo "Compiling $file.c" - $CC -o $BUILD_DIR/$file.o \ - $FLAGS \ - "-DCORE_INSTALLATION=\"$CORE_DIR\"" \ - -c src/$file.c \ - $INCLUDES $LIBS -done - -echo "Linking $TARGET" -$CC -o $TARGET $FLAGS $(for file in $C_FILES ; do printf "$BUILD_DIR/%s.o " $file ; done) $LIBS - -echo "Removing object files" -for file in $C_FILES ; do rm -f "$BUILD_DIR/$file".o 2>/dev/null ; done - +compile() { + for file in $C_FILES ; do + echo "Compiling $file.c" + $CC -o $BUILD_DIR/$file.o \ + $FLAGS \ + "-DCORE_INSTALLATION=\"$CORE_DIR\"" \ + -c src/$file.c \ + $INCLUDES $LIBS + done + + echo "Linking $TARGET" + $CC -o $TARGET $FLAGS $(for file in $C_FILES ; do printf "$BUILD_DIR/%s.o " $file ; done) $LIBS + + echo "Removing object files" + for file in $C_FILES ; do rm -f "$BUILD_DIR/$file".o 2>/dev/null ; done +} + +compile echo "Installing onyx executable" sudo cp ./bin/onyx "$BIN_DIR/onyx" +C_FILES="onyxrun wasm_runtime" +TARGET="./bin/onyxrun" + +compile +echo "Installing onyxrun executable" +sudo cp ./bin/onyxrun "$BIN_DIR/onyxrun" + # Otherwise the prompt ends on the same line printf "\n" diff --git a/src/onyxrun.c b/src/onyxrun.c new file mode 100644 index 00000000..edd2618b --- /dev/null +++ b/src/onyxrun.c @@ -0,0 +1,28 @@ +#define BH_DEFINE +#define BH_NO_TABLE +#define STB_DS_IMPLEMENTATION +#include "bh.h" + +#include "wasm_emit.h" + +int main(int argc, char *argv[]) { + if (argc < 2) { + fprintf(stderr, "Expected a WASM file to run.\n"); + return 1; + } + + bh_file wasm_file; + bh_file_error err = bh_file_open(&wasm_file, argv[1]); + if (err != BH_FILE_ERROR_NONE) { + fprintf(stderr, "Failed to open file %s", argv[1]); + return 1; + } + + bh_file_contents wasm_data = bh_file_read_contents(bh_heap_allocator(), &wasm_file); + bh_file_close(&wasm_file); + + bh_buffer data; + data.data = wasm_data.data; + data.length = wasm_data.length; + return onyx_run_wasm(data, argc - 1, argv + 1); +} \ No newline at end of file -- 2.25.1