diff --git a/CLI/src/Flags.cpp b/CLI/src/Flags.cpp index 4bdad341..290d147b 100644 --- a/CLI/src/Flags.cpp +++ b/CLI/src/Flags.cpp @@ -1,4 +1,6 @@ // This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details +#include "luacommon.h" + #include "Luau/Common.h" #include "Luau/ExperimentalFlags.h" @@ -9,16 +11,8 @@ static void setLuauFlag(std::string_view name, bool state) { - for (Luau::FValue* flag = Luau::FValue::list; flag; flag = flag->next) - { - if (name == flag->name) - { - flag->value = state; - return; - } - } - - fprintf(stderr, "Warning: unrecognized flag '%.*s'.\n", int(name.length()), name.data()); + if (!luau_setfflag(name.data(), state)) + fprintf(stderr, "Warning: unrecognized flag '%.*s'.\n", int(name.length()), name.data()); } static void setLuauFlags(bool state) diff --git a/CMakeLists.txt b/CMakeLists.txt index 220031e2..6860c410 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,7 @@ if(LUAU_STATIC_CRT) endif() project(Luau LANGUAGES CXX C) -add_library(Luau.Common INTERFACE) +add_library(Luau.Common STATIC) add_library(Luau.CLI.lib STATIC) add_library(Luau.Ast STATIC) add_library(Luau.Compiler STATIC) @@ -65,7 +65,9 @@ add_library(Luau.VM.Internals INTERFACE) include(Sources.cmake) -target_include_directories(Luau.Common INTERFACE Common/include) +target_compile_features(Luau.Common PUBLIC cxx_std_17) +target_include_directories(Luau.Common PUBLIC Common/include) +target_link_libraries(Luau.Common) target_compile_features(Luau.CLI.lib PUBLIC cxx_std_17) target_include_directories(Luau.CLI.lib PUBLIC CLI/include) @@ -164,6 +166,7 @@ if(LUAU_EXTERN_C) target_compile_definitions(Luau.VM PUBLIC LUA_API=extern\"C\") target_compile_definitions(Luau.Compiler PUBLIC LUACODE_API=extern\"C\") target_compile_definitions(Luau.CodeGen PUBLIC LUACODEGEN_API=extern\"C\") + target_compile_definitions(Luau.Common PUBLIC LUACOMMON_API=extern\"C\") endif() if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND MSVC_VERSION GREATER_EQUAL 1924) diff --git a/Common/include/luacommon.h b/Common/include/luacommon.h new file mode 100644 index 00000000..49d55f3e --- /dev/null +++ b/Common/include/luacommon.h @@ -0,0 +1,10 @@ +// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details +#pragma once + +// Can be used to reconfigure visibility/exports for public APIs +#ifndef LUACOMMON_API +#define LUACOMMON_API extern +#endif + +// sets a bool fast flag value (returns 1 if the flag was found, 0 otherwise) +LUACOMMON_API int luau_setfflag(const char* name, int value); diff --git a/Common/src/lflags.cpp b/Common/src/lflags.cpp new file mode 100644 index 00000000..8f7cade9 --- /dev/null +++ b/Common/src/lflags.cpp @@ -0,0 +1,19 @@ +// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details +#include "luacommon.h" + +#include "Luau/Common.h" + +#include + +int luau_setfflag(const char* name, int value) +{ + for (Luau::FValue* flag = Luau::FValue::list; flag; flag = flag->next) + { + if (strcmp(flag->name, name) == 0) + { + flag->value = value; + return 1; + } + } + return 0; +} diff --git a/Makefile b/Makefile index 2ad0fc00..75935db7 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,10 @@ AST_SOURCES=$(wildcard Ast/src/*.cpp) AST_OBJECTS=$(AST_SOURCES:%=$(BUILD)/%.o) AST_TARGET=$(BUILD)/libluauast.a +COMMON_SOURCES=$(wildcard Common/src/*.cpp) +COMMON_OBJECTS=$(COMMON_SOURCES:%=$(BUILD)/%.o) +COMMON_TARGET=$(BUILD)/libluaucommon.a + COMPILER_SOURCES=$(wildcard Compiler/src/*.cpp) COMPILER_OBJECTS=$(COMPILER_SOURCES:%=$(BUILD)/%.o) COMPILER_TARGET=$(BUILD)/libluaucompiler.a @@ -73,7 +77,7 @@ ifneq ($(opt),) TESTS_ARGS+=-O$(opt) endif -OBJECTS=$(AST_OBJECTS) $(COMPILER_OBJECTS) $(CONFIG_OBJECTS) $(ANALYSIS_OBJECTS) $(EQSAT_OBJECTS) $(CODEGEN_OBJECTS) $(VM_OBJECTS) $(ISOCLINE_OBJECTS) $(TESTS_OBJECTS) $(REPL_CLI_OBJECTS) $(ANALYZE_CLI_OBJECTS) $(COMPILE_CLI_OBJECTS) $(BYTECODE_CLI_OBJECTS) $(FUZZ_OBJECTS) +OBJECTS=$(AST_OBJECTS) $(COMMON_OBJECTS) $(COMPILER_OBJECTS) $(CONFIG_OBJECTS) $(ANALYSIS_OBJECTS) $(EQSAT_OBJECTS) $(CODEGEN_OBJECTS) $(VM_OBJECTS) $(ISOCLINE_OBJECTS) $(TESTS_OBJECTS) $(REPL_CLI_OBJECTS) $(ANALYZE_CLI_OBJECTS) $(COMPILE_CLI_OBJECTS) $(BYTECODE_CLI_OBJECTS) $(FUZZ_OBJECTS) EXECUTABLE_ALIASES = luau luau-analyze luau-compile luau-bytecode luau-tests # common flags @@ -142,6 +146,7 @@ endif # target-specific flags $(AST_OBJECTS): CXXFLAGS+=-std=c++17 -ICommon/include -IAst/include +$(COMMON_OBJECTS): CXXFLAGS+=-std=c++17 -ICommon/include $(COMPILER_OBJECTS): CXXFLAGS+=-std=c++17 -ICompiler/include -ICommon/include -IAst/include $(CONFIG_OBJECTS): CXXFLAGS+=-std=c++17 -IConfig/include -ICommon/include -IAst/include $(ANALYSIS_OBJECTS): CXXFLAGS+=-std=c++17 -ICommon/include -IAst/include -IAnalysis/include -IEqSat/include -IConfig/include -ICompiler/include -IVM/include @@ -227,24 +232,25 @@ luau-tests: $(TESTS_TARGET) ln -fs $^ $@ # executable targets -$(TESTS_TARGET): $(TESTS_OBJECTS) $(ANALYSIS_TARGET) $(EQSAT_TARGET) $(COMPILER_TARGET) $(CONFIG_TARGET) $(AST_TARGET) $(CODEGEN_TARGET) $(VM_TARGET) $(ISOCLINE_TARGET) -$(REPL_CLI_TARGET): $(REPL_CLI_OBJECTS) $(COMPILER_TARGET) $(CONFIG_TARGET) $(AST_TARGET) $(CODEGEN_TARGET) $(VM_TARGET) $(ISOCLINE_TARGET) -$(ANALYZE_CLI_TARGET): $(ANALYZE_CLI_OBJECTS) $(ANALYSIS_TARGET) $(EQSAT_TARGET) $(AST_TARGET) $(CONFIG_TARGET) $(COMPILER_TARGET) $(VM_TARGET) -$(COMPILE_CLI_TARGET): $(COMPILE_CLI_OBJECTS) $(COMPILER_TARGET) $(AST_TARGET) $(CODEGEN_TARGET) $(VM_TARGET) -$(BYTECODE_CLI_TARGET): $(BYTECODE_CLI_OBJECTS) $(COMPILER_TARGET) $(AST_TARGET) $(CODEGEN_TARGET) $(VM_TARGET) +$(TESTS_TARGET): $(TESTS_OBJECTS) $(ANALYSIS_TARGET) $(EQSAT_TARGET) $(COMMON_TARGET) $(COMPILER_TARGET) $(CONFIG_TARGET) $(AST_TARGET) $(CODEGEN_TARGET) $(VM_TARGET) $(ISOCLINE_TARGET) +$(REPL_CLI_TARGET): $(REPL_CLI_OBJECTS) $(COMMON_TARGET) $(COMPILER_TARGET) $(CONFIG_TARGET) $(AST_TARGET) $(CODEGEN_TARGET) $(VM_TARGET) $(ISOCLINE_TARGET) +$(ANALYZE_CLI_TARGET): $(ANALYZE_CLI_OBJECTS) $(ANALYSIS_TARGET) $(EQSAT_TARGET) $(AST_TARGET) $(CONFIG_TARGET) $(COMMON_TARGET) $(COMPILER_TARGET) $(VM_TARGET) +$(COMPILE_CLI_TARGET): $(COMPILE_CLI_OBJECTS) $(COMMON_TARGET) $(COMPILER_TARGET) $(AST_TARGET) $(CODEGEN_TARGET) $(VM_TARGET) +$(BYTECODE_CLI_TARGET): $(BYTECODE_CLI_OBJECTS) $(COMMON_TARGET) $(COMPILER_TARGET) $(AST_TARGET) $(CODEGEN_TARGET) $(VM_TARGET) $(TESTS_TARGET) $(REPL_CLI_TARGET) $(ANALYZE_CLI_TARGET) $(COMPILE_CLI_TARGET) $(BYTECODE_CLI_TARGET): $(CXX) $^ $(LDFLAGS) -o $@ # executable targets for fuzzing -fuzz-%: $(BUILD)/fuzz/%.cpp.o $(ANALYSIS_TARGET) $(EQSAT_TARGET) $(COMPILER_TARGET) $(AST_TARGET) $(CONFIG_TARGET) $(CODEGEN_TARGET) $(VM_TARGET) +fuzz-%: $(BUILD)/fuzz/%.cpp.o $(ANALYSIS_TARGET) $(EQSAT_TARGET) $(COMMON_TARGET) $(COMPILER_TARGET) $(AST_TARGET) $(CONFIG_TARGET) $(CODEGEN_TARGET) $(VM_TARGET) $(CXX) $^ $(LDFLAGS) -o $@ -fuzz-proto: $(BUILD)/fuzz/proto.cpp.o $(BUILD)/fuzz/protoprint.cpp.o $(BUILD)/fuzz/luau.pb.cpp.o $(ANALYSIS_TARGET) $(EQSAT_TARGET) $(COMPILER_TARGET) $(AST_TARGET) $(CONFIG_TARGET) $(VM_TARGET) | build/libprotobuf-mutator -fuzz-prototest: $(BUILD)/fuzz/prototest.cpp.o $(BUILD)/fuzz/protoprint.cpp.o $(BUILD)/fuzz/luau.pb.cpp.o $(ANALYSIS_TARGET) $(EQSAT_TARGET) $(COMPILER_TARGET) $(AST_TARGET) $(CONFIG_TARGET) $(VM_TARGET) | build/libprotobuf-mutator +fuzz-proto: $(BUILD)/fuzz/proto.cpp.o $(BUILD)/fuzz/protoprint.cpp.o $(BUILD)/fuzz/luau.pb.cpp.o $(ANALYSIS_TARGET) $(EQSAT_TARGET) $(COMMON_TARGET) $(COMPILER_TARGET) $(AST_TARGET) $(CONFIG_TARGET) $(VM_TARGET) | build/libprotobuf-mutator +fuzz-prototest: $(BUILD)/fuzz/prototest.cpp.o $(BUILD)/fuzz/protoprint.cpp.o $(BUILD)/fuzz/luau.pb.cpp.o $(ANALYSIS_TARGET) $(EQSAT_TARGET) $(COMMON_TARGET) $(COMPILER_TARGET) $(AST_TARGET) $(CONFIG_TARGET) $(VM_TARGET) | build/libprotobuf-mutator # static library targets $(AST_TARGET): $(AST_OBJECTS) +$(COMMON_TARGET): $(COMMON_OBJECTS) $(COMPILER_TARGET): $(COMPILER_OBJECTS) $(CONFIG_TARGET): $(CONFIG_OBJECTS) $(ANALYSIS_TARGET): $(ANALYSIS_OBJECTS) @@ -253,7 +259,7 @@ $(CODEGEN_TARGET): $(CODEGEN_OBJECTS) $(VM_TARGET): $(VM_OBJECTS) $(ISOCLINE_TARGET): $(ISOCLINE_OBJECTS) -$(AST_TARGET) $(COMPILER_TARGET) $(CONFIG_TARGET) $(ANALYSIS_TARGET) $(EQSAT_TARGET) $(CODEGEN_TARGET) $(VM_TARGET) $(ISOCLINE_TARGET): +$(AST_TARGET) $(COMMON_TARGET) $(COMPILER_TARGET) $(CONFIG_TARGET) $(ANALYSIS_TARGET) $(EQSAT_TARGET) $(CODEGEN_TARGET) $(VM_TARGET) $(ISOCLINE_TARGET): ar rcs $@ $^ # object file targets diff --git a/Sources.cmake b/Sources.cmake index a40505df..bd47e86d 100644 --- a/Sources.cmake +++ b/Sources.cmake @@ -1,16 +1,16 @@ # Luau.Common Sources -# Note: Until 3.19, INTERFACE targets couldn't have SOURCES property set -if(NOT ${CMAKE_VERSION} VERSION_LESS "3.19") - target_sources(Luau.Common PRIVATE - Common/include/Luau/Common.h - Common/include/Luau/Bytecode.h - Common/include/Luau/BytecodeUtils.h - Common/include/Luau/DenseHash.h - Common/include/Luau/ExperimentalFlags.h - Common/include/Luau/Variant.h - Common/include/Luau/VecDeque.h - ) -endif() +target_sources(Luau.Common PRIVATE + Common/include/Luau/Common.h + Common/include/Luau/Bytecode.h + Common/include/Luau/BytecodeUtils.h + Common/include/Luau/DenseHash.h + Common/include/Luau/ExperimentalFlags.h + Common/include/Luau/Variant.h + Common/include/Luau/VecDeque.h + Common/include/luacommon.h + + Common/src/lflags.cpp +) # Luau.Ast Sources target_sources(Luau.Ast PRIVATE @@ -459,9 +459,10 @@ if(TARGET Luau.UnitTest) tests/EqSat.slice.test.cpp tests/EqSatSimplification.test.cpp tests/Error.test.cpp + tests/FastFlags.test.cpp tests/Fixture.cpp tests/Fixture.h - tests/FragmentAutocomplete.test.cpp + tests/FragmentAutocomplete.test.cpp tests/Frontend.test.cpp tests/Generalization.test.cpp tests/InsertionOrderedMap.test.cpp diff --git a/tests/FastFlags.test.cpp b/tests/FastFlags.test.cpp new file mode 100644 index 00000000..b72850c6 --- /dev/null +++ b/tests/FastFlags.test.cpp @@ -0,0 +1,32 @@ +// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details +#include "Luau/Common.h" + +#include "luacommon.h" + +#include "doctest.h" +#include "ScopedFlags.h" + +LUAU_FASTFLAGVARIABLE(LuauTestingBoolFlag); +LUAU_FASTINTVARIABLE(LuauTestingIntFlag, 123); + +using namespace Luau; + +TEST_SUITE_BEGIN("FastFlagsTest"); + +TEST_CASE("set_fflag") +{ + // Set a boolean flag + luau_setfflag("LuauTestingBoolFlag", true); + CHECK_EQ(FFlag::LuauTestingBoolFlag, true); + + // Set a non-existent flag + int result = luau_setfflag("NonExistentFlag", 1); + CHECK_EQ(result, 0); // Expect 0 for non-existent flag + + // Bool and int flags are not mixed + result = luau_setfflag("LuauTestingIntFlag", 0); + CHECK_EQ(result, 0); // Expect 0 for non-existent (boolean) flag + CHECK_EQ(FInt::LuauTestingIntFlag, 123); // Should be unchanged +} + +TEST_SUITE_END();