diff --git a/Compiler/include/Luau/Compiler.h b/Compiler/include/Luau/Compiler.h index f8d67158..f2b72377 100644 --- a/Compiler/include/Luau/Compiler.h +++ b/Compiler/include/Luau/Compiler.h @@ -36,6 +36,10 @@ struct CompileOptions // global builtin to construct vectors; disabled by default const char* vectorLib = nullptr; const char* vectorCtor = nullptr; + + // array of globals that are mutable; disables the import optimization for + // fields accessed through them. use NULL to end the array + const char** mutableGlobalNames; }; class CompileError : public std::exception diff --git a/Compiler/src/Compiler.cpp b/Compiler/src/Compiler.cpp index 75632c30..44084671 100644 --- a/Compiler/src/Compiler.cpp +++ b/Compiler/src/Compiler.cpp @@ -3703,7 +3703,7 @@ void compileOrThrow(BytecodeBuilder& bytecode, AstStatBlock* root, const AstName Compiler compiler(bytecode, options); - // since access to some global objects may result in values that change over time, we block table imports + // since access to some global objects may result in values that change over time, we block imports from non-readonly tables for (const char* global : kSpecialGlobals) { AstName name = names.get(global); @@ -3712,6 +3712,15 @@ void compileOrThrow(BytecodeBuilder& bytecode, AstStatBlock* root, const AstName compiler.globals[name].special = true; } + if (options.mutableGlobalNames) + for (const char** ptr = options.mutableGlobalNames; ptr; ptr += sizeof(const char*)) + { + AstName name = names.get(*ptr); + + if (name.value) + compiler.globals[name].special = true; + } + // this visitor traverses the AST to analyze mutability of locals/globals, filling Local::written and Global::written Compiler::AssignmentVisitor assignmentVisitor(&compiler); root->visit(&assignmentVisitor);