Add Fast Flags C API

This commit is contained in:
Alex Orlenko 2025-04-08 22:53:01 +01:00
parent ee1c6bf0db
commit 190b291bba
No known key found for this signature in database
GPG key ID: 4C150C250863B96D
7 changed files with 100 additions and 35 deletions

View file

@ -1,4 +1,6 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details // 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/Common.h"
#include "Luau/ExperimentalFlags.h" #include "Luau/ExperimentalFlags.h"
@ -9,16 +11,8 @@
static void setLuauFlag(std::string_view name, bool state) static void setLuauFlag(std::string_view name, bool state)
{ {
for (Luau::FValue<bool>* flag = Luau::FValue<bool>::list; flag; flag = flag->next) if (!luau_setfflag(name.data(), state))
{ fprintf(stderr, "Warning: unrecognized flag '%.*s'.\n", int(name.length()), name.data());
if (name == flag->name)
{
flag->value = state;
return;
}
}
fprintf(stderr, "Warning: unrecognized flag '%.*s'.\n", int(name.length()), name.data());
} }
static void setLuauFlags(bool state) static void setLuauFlags(bool state)

View file

@ -22,7 +22,7 @@ if(LUAU_STATIC_CRT)
endif() endif()
project(Luau LANGUAGES CXX C) project(Luau LANGUAGES CXX C)
add_library(Luau.Common INTERFACE) add_library(Luau.Common STATIC)
add_library(Luau.CLI.lib STATIC) add_library(Luau.CLI.lib STATIC)
add_library(Luau.Ast STATIC) add_library(Luau.Ast STATIC)
add_library(Luau.Compiler STATIC) add_library(Luau.Compiler STATIC)
@ -65,7 +65,9 @@ add_library(Luau.VM.Internals INTERFACE)
include(Sources.cmake) 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_compile_features(Luau.CLI.lib PUBLIC cxx_std_17)
target_include_directories(Luau.CLI.lib PUBLIC CLI/include) 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.VM PUBLIC LUA_API=extern\"C\")
target_compile_definitions(Luau.Compiler PUBLIC LUACODE_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.CodeGen PUBLIC LUACODEGEN_API=extern\"C\")
target_compile_definitions(Luau.Common PUBLIC LUACOMMON_API=extern\"C\")
endif() endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND MSVC_VERSION GREATER_EQUAL 1924) if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND MSVC_VERSION GREATER_EQUAL 1924)

View file

@ -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);

19
Common/src/lflags.cpp Normal file
View file

@ -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 <string.h>
int luau_setfflag(const char* name, int value)
{
for (Luau::FValue<bool>* flag = Luau::FValue<bool>::list; flag; flag = flag->next)
{
if (strcmp(flag->name, name) == 0)
{
flag->value = value;
return 1;
}
}
return 0;
}

View file

@ -14,6 +14,10 @@ AST_SOURCES=$(wildcard Ast/src/*.cpp)
AST_OBJECTS=$(AST_SOURCES:%=$(BUILD)/%.o) AST_OBJECTS=$(AST_SOURCES:%=$(BUILD)/%.o)
AST_TARGET=$(BUILD)/libluauast.a 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_SOURCES=$(wildcard Compiler/src/*.cpp)
COMPILER_OBJECTS=$(COMPILER_SOURCES:%=$(BUILD)/%.o) COMPILER_OBJECTS=$(COMPILER_SOURCES:%=$(BUILD)/%.o)
COMPILER_TARGET=$(BUILD)/libluaucompiler.a COMPILER_TARGET=$(BUILD)/libluaucompiler.a
@ -73,7 +77,7 @@ ifneq ($(opt),)
TESTS_ARGS+=-O$(opt) TESTS_ARGS+=-O$(opt)
endif 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 EXECUTABLE_ALIASES = luau luau-analyze luau-compile luau-bytecode luau-tests
# common flags # common flags
@ -142,6 +146,7 @@ endif
# target-specific flags # target-specific flags
$(AST_OBJECTS): CXXFLAGS+=-std=c++17 -ICommon/include -IAst/include $(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 $(COMPILER_OBJECTS): CXXFLAGS+=-std=c++17 -ICompiler/include -ICommon/include -IAst/include
$(CONFIG_OBJECTS): CXXFLAGS+=-std=c++17 -IConfig/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 $(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 $^ $@ ln -fs $^ $@
# executable targets # executable targets
$(TESTS_TARGET): $(TESTS_OBJECTS) $(ANALYSIS_TARGET) $(EQSAT_TARGET) $(COMPILER_TARGET) $(CONFIG_TARGET) $(AST_TARGET) $(CODEGEN_TARGET) $(VM_TARGET) $(ISOCLINE_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) $(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) $(COMPILER_TARGET) $(VM_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) $(COMPILER_TARGET) $(AST_TARGET) $(CODEGEN_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) $(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): $(TESTS_TARGET) $(REPL_CLI_TARGET) $(ANALYZE_CLI_TARGET) $(COMPILE_CLI_TARGET) $(BYTECODE_CLI_TARGET):
$(CXX) $^ $(LDFLAGS) -o $@ $(CXX) $^ $(LDFLAGS) -o $@
# executable targets for fuzzing # 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 $@ $(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-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) $(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 # static library targets
$(AST_TARGET): $(AST_OBJECTS) $(AST_TARGET): $(AST_OBJECTS)
$(COMMON_TARGET): $(COMMON_OBJECTS)
$(COMPILER_TARGET): $(COMPILER_OBJECTS) $(COMPILER_TARGET): $(COMPILER_OBJECTS)
$(CONFIG_TARGET): $(CONFIG_OBJECTS) $(CONFIG_TARGET): $(CONFIG_OBJECTS)
$(ANALYSIS_TARGET): $(ANALYSIS_OBJECTS) $(ANALYSIS_TARGET): $(ANALYSIS_OBJECTS)
@ -253,7 +259,7 @@ $(CODEGEN_TARGET): $(CODEGEN_OBJECTS)
$(VM_TARGET): $(VM_OBJECTS) $(VM_TARGET): $(VM_OBJECTS)
$(ISOCLINE_TARGET): $(ISOCLINE_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 $@ $^ ar rcs $@ $^
# object file targets # object file targets

View file

@ -1,16 +1,16 @@
# Luau.Common Sources # Luau.Common Sources
# Note: Until 3.19, INTERFACE targets couldn't have SOURCES property set target_sources(Luau.Common PRIVATE
if(NOT ${CMAKE_VERSION} VERSION_LESS "3.19") Common/include/Luau/Common.h
target_sources(Luau.Common PRIVATE Common/include/Luau/Bytecode.h
Common/include/Luau/Common.h Common/include/Luau/BytecodeUtils.h
Common/include/Luau/Bytecode.h Common/include/Luau/DenseHash.h
Common/include/Luau/BytecodeUtils.h Common/include/Luau/ExperimentalFlags.h
Common/include/Luau/DenseHash.h Common/include/Luau/Variant.h
Common/include/Luau/ExperimentalFlags.h Common/include/Luau/VecDeque.h
Common/include/Luau/Variant.h Common/include/luacommon.h
Common/include/Luau/VecDeque.h
) Common/src/lflags.cpp
endif() )
# Luau.Ast Sources # Luau.Ast Sources
target_sources(Luau.Ast PRIVATE target_sources(Luau.Ast PRIVATE
@ -459,9 +459,10 @@ if(TARGET Luau.UnitTest)
tests/EqSat.slice.test.cpp tests/EqSat.slice.test.cpp
tests/EqSatSimplification.test.cpp tests/EqSatSimplification.test.cpp
tests/Error.test.cpp tests/Error.test.cpp
tests/FastFlags.test.cpp
tests/Fixture.cpp tests/Fixture.cpp
tests/Fixture.h tests/Fixture.h
tests/FragmentAutocomplete.test.cpp tests/FragmentAutocomplete.test.cpp
tests/Frontend.test.cpp tests/Frontend.test.cpp
tests/Generalization.test.cpp tests/Generalization.test.cpp
tests/InsertionOrderedMap.test.cpp tests/InsertionOrderedMap.test.cpp

32
tests/FastFlags.test.cpp Normal file
View file

@ -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();