Embedded luajit in hate-no-d

This commit is contained in:
pedro-bento 2026-10-21 18:37:55 +01:00
parent 9bc51f1dd3
commit 3bfd7e0920
6 changed files with 292 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
hate-no-d
quick-start.lua

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "submodules/luajit"]
path = submodules/luajit
url = https://luajit.org/git/luajit.git

200
Makefile Normal file
View file

@ -0,0 +1,200 @@
# hate-no-d
#
# Native platforms are auto-detected from uname. Cross targets are selected explicitly:
# make
# make PLATFORM=Android ANDROID_NDK=/opt/android-ndk
# make PLATFORM=Android ANDROID_NDK=... ANDROID_ABI=armeabi-v7a ANDROID_API=21
# make PLATFORM=iOS IOS_ARCH=arm64 IOS_MIN=12.0
# luajit requires GNU Make (use `gmake` on FreeBSD/OpenBSD/NetBSD).
ifndef .FEATURES
$(error This build requires GNU Make -- on BSD use 'gmake')
endif
.DEFAULT_GOAL := all
LUAJIT_DIR := submodules/luajit
LUAJIT_SRC := $(LUAJIT_DIR)/src
LUAJIT_AR := $(LUAJIT_SRC)/libluajit.a
BIN := hate-no-d
LUAJIT_XCFLAGS := -DLUAJIT_ENABLE_LUA52COMPAT
LUAJIT_EXTRA :=
# Platform detection. PLATFORM= always wins over uname.
PLATFORM ?= $(shell uname -s)
ifeq ($(PLATFORM),Linux)
OS_KIND := linux
else ifeq ($(PLATFORM),Android)
OS_KIND := android
else ifeq ($(PLATFORM),Darwin)
OS_KIND := macos
else ifeq ($(PLATFORM),iOS)
OS_KIND := ios
else ifneq (,$(filter $(PLATFORM),FreeBSD OpenBSD NetBSD DragonFly GNU/kFreeBSD))
OS_KIND := bsd
else
OS_KIND := posix
endif
# Linux (ELF)
ifeq ($(OS_KIND),linux)
EXPORT_LDFLAGS := -Wl,-E
PLAT_LIBS := -lm -ldl
LINK_TARGET := $(BIN)
endif
# BSD (ELF or Mach-O, dlopen lives in libc)
ifeq ($(OS_KIND),bsd)
EXPORT_LDFLAGS := -Wl,-E
PLAT_LIBS := -lm
LINK_TARGET := $(BIN)
endif
# Generic POSIX fallback (Solaris/illumos, AIX, Haiku, ...).
# Note: Solaris ld's -E is NOT --export-dynamic, so it is deliberately off.
ifeq ($(OS_KIND),posix)
EXPORT_LDFLAGS :=
PLAT_LIBS := -lm
LINK_TARGET := $(BIN)
endif
# macOS (Mach-O)
#
# ld64 exports main-executable globals by default; -export_dynamic only stops
# -dead_strip / LTO from discarding the Lua API. GC64 is mandatory on arm64.
# JIT under the hardened runtime needs MAP_JIT, which is compiled in only when
# the deployment target is >= 11.0.
ifeq ($(OS_KIND),macos)
ifeq ($(origin CC),default)
CC := clang
endif
MACOSX_DEPLOYMENT_TARGET ?= 11.0
export MACOSX_DEPLOYMENT_TARGET
EXPORT_LDFLAGS := -Wl,-export_dynamic
PLAT_LIBS := -lm
LINK_TARGET := $(BIN)
# MAP_JIT path: needed for the hardened runtime (and therefore notarization).
# Requires MACOSX_DEPLOYMENT_TARGET >= 11.0 or LuaJIT compiles it back out.
MACOSX_HRT ?= 1
ifeq ($(MACOSX_HRT),1)
LUAJIT_XCFLAGS += -DLUAJIT_ENABLE_OSX_HRT
endif
endif
# Android (ELF, cross-compiled with the NDK)
#
# 64-bit ABIs build on any 64-bit host. armeabi-v7a additionally needs a
# 32-bit host compiler: make ... HOST_CC="gcc -m32"
ifeq ($(OS_KIND),android)
ANDROID_NDK ?= $(ANDROID_NDK_HOME)
ANDROID_API ?= 21
ANDROID_ABI ?= arm64-v8a
ifeq ($(strip $(ANDROID_NDK)),)
$(error PLATFORM=Android requires ANDROID_NDK (or ANDROID_NDK_HOME) to be set)
endif
ifeq ($(ANDROID_ABI),arm64-v8a)
ANDROID_TRIPLET := aarch64-linux-android
else ifeq ($(ANDROID_ABI),armeabi-v7a)
ANDROID_TRIPLET := armv7a-linux-androideabi
else ifeq ($(ANDROID_ABI),x86_64)
ANDROID_TRIPLET := x86_64-linux-android
else ifeq ($(ANDROID_ABI),x86)
ANDROID_TRIPLET := i686-linux-android
else
$(error ANDROID_ABI must be one of: arm64-v8a, armeabi-v7a, x86_64, x86)
endif
ifeq ($(shell uname -s),Darwin)
ANDROID_HOST_TAG ?= darwin-x86_64
else
ANDROID_HOST_TAG ?= linux-x86_64
endif
ANDROID_BIN := $(ANDROID_NDK)/toolchains/llvm/prebuilt/$(ANDROID_HOST_TAG)/bin
ANDROID_CC := $(ANDROID_BIN)/$(ANDROID_TRIPLET)$(ANDROID_API)-clang
CC := $(ANDROID_CC)
EXPORT_LDFLAGS := -Wl,-E
PLAT_LIBS := -lm
LINK_TARGET := $(BIN)
LUAJIT_EXTRA := CROSS=$(ANDROID_BIN)/$(ANDROID_TRIPLET)- STATIC_CC=$(ANDROID_CC) TARGET_LD=$(ANDROID_CC) "TARGET_AR=$(ANDROID_BIN)/llvm-ar rcus" TARGET_STRIP=$(ANDROID_BIN)/llvm-strip
endif
# iOS (Mach-O, ARM64 only)
#
# There is no runnable binary: link libluajit.a into your Xcode app target.
# The JIT is compiled out by LuaJIT itself (no runtime code generation is
# allowed on iOS), so you get the fast interpreter only.
ifeq ($(OS_KIND),ios)
IOS_SDK ?= $(shell xcrun --sdk iphoneos --show-sdk-path 2>/dev/null)
IOS_CC ?= $(shell xcrun --sdk iphoneos --find clang 2>/dev/null)
IOS_ARCH ?= arm64
IOS_MIN ?= 12.0
ifeq ($(strip $(IOS_SDK)),)
$(error PLATFORM=iOS requires Xcode/xcrun on the build host)
endif
IOS_FLAGS := -arch $(IOS_ARCH) -isysroot $(IOS_SDK) -mios-version-min=$(IOS_MIN)
CC := $(IOS_CC)
EXPORT_LDFLAGS :=
PLAT_LIBS :=
LINK_TARGET := $(LUAJIT_AR)
# DEFAULT_CC keeps the host toolchain (minilua/buildvm) native; CROSS
# switches only the target objects to the iOS SDK.
LUAJIT_EXTRA := DEFAULT_CC=clang CROSS=$(dir $(IOS_CC)) TARGET_SYS=iOS "TARGET_FLAGS=$(IOS_FLAGS)"
endif
ifndef LINK_TARGET
$(error Unsupported platform '$(PLATFORM)')
endif
# Compiler / linker flags
CFLAGS ?= -O2 -Wall
CPPFLAGS += -I$(LUAJIT_SRC)
# LuaJIT: static archive only, built through the upstream GNU Makefile.
LUAJIT_MAKE_ARGS := BUILDMODE=static XCFLAGS="$(LUAJIT_XCFLAGS)" $(LUAJIT_EXTRA)
$(LUAJIT_AR):
$(MAKE) -C $(LUAJIT_SRC) $(LUAJIT_MAKE_ARGS) "LJCORE_O=ljamalg.o" libluajit.a
# Targets
all: $(LINK_TARGET)
$(BIN): hate-no-d.c $(LUAJIT_AR)
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ hate-no-d.c \
$(LUAJIT_AR) $(EXPORT_LDFLAGS) $(LDFLAGS) $(PLAT_LIBS)
luajit: $(LUAJIT_AR)
# Show the resolved configuration for the current (or selected) platform.
# Useful for checking a cross build before committing to it.
print-config:
@echo "PLATFORM = $(PLATFORM)"
@echo "OS_KIND = $(OS_KIND)"
@echo "CC = $(CC)"
@echo "CFLAGS = $(CFLAGS)"
@echo "CPPFLAGS = $(CPPFLAGS)"
@echo "EXPORT_LDFLAGS = $(EXPORT_LDFLAGS)"
@echo "PLAT_LIBS = $(PLAT_LIBS)"
@echo "LINK_TARGET = $(LINK_TARGET)"
@echo "LUAJIT_MAKE_ARGS = $(LUAJIT_MAKE_ARGS)"
@echo "MACOSX_DEPLOYMENT_TARGET = $(MACOSX_DEPLOYMENT_TARGET) (HRT=$(MACOSX_HRT))"
@echo "ANDROID_ABI / API = $(ANDROID_ABI) / $(ANDROID_API)"
@echo "IOS_ARCH / MIN = $(IOS_ARCH) / $(IOS_MIN)"
clean:
$(MAKE) -C $(LUAJIT_DIR) clean
rm -f $(BIN)
.PHONY: all clean luajit print-config

View file

@ -1,2 +1,11 @@
# hate-no-d
A [Love2D](https://www.love2d.org/) antagonist.
## Quick start
```sh
make
echo -e 'print("Hey! I am hate-no-d!")' > quick-start.lua
./hate-no-d quick-start.lua
```

1
compile_flags.txt Normal file
View file

@ -0,0 +1 @@
-I./submodules/luajit/src

77
hate-no-d.c Normal file
View file

@ -0,0 +1,77 @@
#include <stdio.h>
#include <stdlib.h>
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
static int traceback(lua_State *L) {
if (!lua_isstring(L, 1)) {
if (lua_isnoneornil(L, 1) || !luaL_callmeta(L, 1, "__tostring") || !lua_isstring(L, -1)) {
return 1;
}
lua_remove(L, 1);
}
luaL_traceback(L, L, lua_tostring(L, 1), 1);
return 1;
}
static void report(lua_State *L) {
const char *msg = lua_tostring(L, -1);
if (!msg) {
msg = "(error object is not a string)";
}
fprintf(stderr, "[ERROR] hate-no-d: %s\n", msg);
fflush(stderr);
lua_pop(L, 1);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <file-path> [args...]\n", argv[0]);
return EXIT_FAILURE;
}
const char *file_path = argv[1];
lua_State *L = luaL_newstate();
if (!L) {
fprintf(stderr, "[ERROR] hate-no-d: cannot create Lua state\n");
return EXIT_FAILURE;
}
luaL_openlibs(L);
lua_createtable(L, argc - 1, 1);
for (int i = 0; i < argc; i++) {
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i - 1);
}
lua_setglobal(L, "arg");
int status = luaL_loadfile(L, file_path);
if (status != LUA_OK) {
report(L);
lua_close(L);
return EXIT_FAILURE;
}
lua_pushcfunction(L, traceback);
lua_insert(L, -2);
status = lua_pcall(L, 0, 0, -2);
if (status != LUA_OK) {
report(L);
lua_close(L);
return EXIT_FAILURE;
}
lua_pop(L, 1);
lua_close(L);
return EXIT_SUCCESS;
}