Adds support for setting engine options from code settings.

This commit is contained in:
2021-10-24 12:35:26 +02:00
parent b94ed45d9e
commit ac841d81db
4 changed files with 195 additions and 23 deletions

View File

@@ -116,6 +116,17 @@ connection.onInitialized(() => {
}
});
connection.workspace.getConfiguration('angelscript-languageserver.engine-settings').then((config) => {
for (const key in config) {
if (Object.prototype.hasOwnProperty.call(config, key)) {
const element = config[key];
let keyValue : NativeWrapper.ASEngineProperty = NativeWrapper.ASEngineProperty[key as keyof typeof NativeWrapper.ASEngineProperty];
database.setEngineProperty(keyValue, element);
}
}
});
connection.workspace.getWorkspaceFolders().then((ws) => {
ws?.forEach(element => {
registerFiles(element.uri.substr(7))

View File

@@ -6,13 +6,12 @@
#include "ASTypeDefParser/Parser.hpp"
Napi::Object Database::Init(Napi::Env env, Napi::Object exports) {
Napi::Function func =
DefineClass(env, "Database",
{InstanceMethod("reset", &Database::Reset), InstanceMethod("loadTypeDef", &Database::LoadTypeDef),
InstanceMethod("loadScript", &Database::LoadScript), InstanceMethod("build", &Database::Build),
InstanceMethod("messages", &Database::GetMessages),
InstanceMethod("addDefine", &Database::AddDefine),
InstanceMethod("setEngineProperty", &Database::SetEngineProperty)});
Napi::Function func = DefineClass(
env, "Database",
{InstanceMethod("reset", &Database::Reset), InstanceMethod("loadTypeDef", &Database::LoadTypeDef),
InstanceMethod("loadScript", &Database::LoadScript), InstanceMethod("build", &Database::Build),
InstanceMethod("messages", &Database::GetMessages), InstanceMethod("addDefine", &Database::AddDefine),
InstanceMethod("setEngineProperty", &Database::SetEngineProperty)});
auto* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
@@ -30,14 +29,9 @@ void Database::SetupEngine() {
_engine = asCreateScriptEngine();
_builder = {};
_engine->SetMessageCallback(asMETHOD(Database, MessageCallback), this, asCALL_THISCALL);
_engine->SetEngineProperty(asEP_DISALLOW_EMPTY_LIST_ELEMENTS, true);
_engine->SetEngineProperty(asEP_DISALLOW_VALUE_ASSIGN_FOR_REF_TYPE, false);
_engine->SetEngineProperty(asEP_ALLOW_UNSAFE_REFERENCES, true);
_engine->SetEngineProperty(asEP_ALWAYS_IMPL_DEFAULT_CONSTRUCT, true);
_engine->SetEngineProperty(asEP_AUTO_GARBAGE_COLLECT, false);
_engine->SetEngineProperty(asEP_REQUIRE_ENUM_SCOPE, true);
_engine->SetEngineProperty(asEP_PROPERTY_ACCESSOR_MODE, 2);
_engine->SetEngineProperty(asEP_COMPILER_WARNINGS, 2);
for (auto& prop : _engineProperties) {
_engine->SetEngineProperty(prop.first, prop.second);
}
for (auto& flag : _defines) {
_builder.DefineWord(flag.c_str());
}
@@ -46,20 +40,22 @@ void Database::SetupEngine() {
_builder.StartNewModule(_engine, "Module");
}
Database::Database(const Napi::CallbackInfo& info) : ObjectWrap(info) {
Reset(info);
}
Database::Database(const Napi::CallbackInfo& info) : ObjectWrap(info) { Reset(info); }
void Database::SetEngineProperty(const Napi::CallbackInfo& info) {
auto property = info[0].As<Napi::Number>().Int32Value();
auto value = info[1].As<Napi::Number>().Int32Value();
_engine->SetEngineProperty(static_cast<asEEngineProp>(property), value);
if (info[1].IsBoolean()) {
auto value = info[1].As<Napi::Boolean>().Value();
_engineProperties[(asEEngineProp)property] = value;
} else {
auto value = info[1].As<Napi::Number>().Int32Value();
_engineProperties[(asEEngineProp)property] = value;
}
}
void Database::Reset(const Napi::CallbackInfo&) {
std::lock_guard<std::mutex> lck(_lock);
if (_engine != nullptr){
if (_engine != nullptr) {
_engine->DiscardModule("Module");
_engine->Release();
}

View File

@@ -13,6 +13,7 @@ private:
CScriptBuilder _builder;
ASTypeDefParser::TypeDefResult _result;
std::vector<std::string> _defines;
std::unordered_map<asEEngineProp, asPWORD> _engineProperties;
std::vector<const Diagnostic*> _messages;
std::mutex _lock;