add facility for specifying custom mutable globals

This commit is contained in:
LoganDark 2021-11-08 13:14:49 -08:00
parent 7c5883fc60
commit 283a349fb9
No known key found for this signature in database
GPG key ID: B8C37CEDE1AC60EA
2 changed files with 14 additions and 1 deletions

View file

@ -36,6 +36,10 @@ struct CompileOptions
// global builtin to construct vectors; disabled by default // global builtin to construct vectors; disabled by default
const char* vectorLib = nullptr; const char* vectorLib = nullptr;
const char* vectorCtor = 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 class CompileError : public std::exception

View file

@ -3703,7 +3703,7 @@ void compileOrThrow(BytecodeBuilder& bytecode, AstStatBlock* root, const AstName
Compiler compiler(bytecode, options); 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) for (const char* global : kSpecialGlobals)
{ {
AstName name = names.get(global); AstName name = names.get(global);
@ -3712,6 +3712,15 @@ void compileOrThrow(BytecodeBuilder& bytecode, AstStatBlock* root, const AstName
compiler.globals[name].special = true; 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 // this visitor traverses the AST to analyze mutability of locals/globals, filling Local::written and Global::written
Compiler::AssignmentVisitor assignmentVisitor(&compiler); Compiler::AssignmentVisitor assignmentVisitor(&compiler);
root->visit(&assignmentVisitor); root->visit(&assignmentVisitor);