added VERY basic implementation of onyxrun
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 10 Dec 2021 16:43:51 +0000 (10:43 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 10 Dec 2021 16:43:51 +0000 (10:43 -0600)
build.bat
build.sh
src/onyxrun.c [new file with mode: 0644]

index 6ade20ec8508ab427d5eca6964cecc6f0de1a191..cf2dbe6eb6820d638f156f7687ed2d6cca5293cd 100644 (file)
--- 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
index 5defd7c16e1189a3e2621cdb6468f4306cb9cf27..022579c2333929c9e414182e2ec895d4751e37c5 100755 (executable)
--- 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 (file)
index 0000000..edd2618
--- /dev/null
@@ -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