8000 fix: crashes caused by creating empty components by jimmy54 · Pull Request #1003 · rime/librime · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: crashes caused by creating empty components #1003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 48 additions & 68 deletions src/rime/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,38 @@ void ConcreteEngine::ApplySchema(Schema* schema) {
message_sink_("schema", schema_->sche 8000 ma_id() + "/" + schema_->schema_name());
}

// Helper template function to create components
template <typename T>
inline void CreateComponentsFromList(Engine* engine,
Config* config,
const string& config_key,
const string& component_type,
vector<an<T>>& target_collection) {
if (auto component_list = config->GetList(config_key)) {
size_t n = component_list->size();
for (size_t i = 0; i < n; ++i) {
auto prescription = As<ConfigValue>(component_list->GetAt(i));
if (!prescription)
continue;
Ticket ticket{engine, component_type, prescription->str()};
auto c = T::Require(ticket.klass);
if (!c) {
LOG(ERROR) << "error creating " << component_type << ": '"
<< ticket.klass << "'";
continue;
}
auto component = c->Create(ticket);
if (!component) {
LOG(ERROR) << "error creating " << component_type << " from ticket: '"
<< ticket.klass << "'";
continue;
}
an<T> instance(component);
target_collection.push_back(instance);
}
}
}

void ConcreteEngine::InitializeComponents() {
processors_.clear();
segmentors_.clear();
Expand All @@ -311,80 +343,28 @@ void ConcreteEngine::InitializeComponents() {
Config* config = schema_->config();
if (!config)
return;
// create processors
if (auto processor_list = config->GetList("engine/processors")) {
size_t n = processor_list->size();
for (size_t i = 0; i < n; ++i) {
auto prescription = As<ConfigValue>(processor_list->GetAt(i));
if (!prescription)
continue;
Ticket ticket{this, "processor", prescription->str()};
if (auto c = Processor::Require(ticket.klass)) {
an<Processor> p(c->Create(ticket));
processors_.push_back(p);
} else {
LOG(ERROR) << "error creating processor: '" << ticket.klass << "'";
}
}
}
// create segmentors
if (auto segmentor_list = config->GetList("engine/segmentors")) {
size_t n = segmentor_list->size();
for (size_t i = 0; i < n; ++i) {
auto prescription = As<ConfigValue>(segmentor_list->GetAt(i));
if (!prescription)
continue;
Ticket ticket{this, "segmentor", prescription->str()};
if (auto c = Segmentor::Require(ticket.klass)) {
an<Segmentor> s(c->Create(ticket));
segmentors_.push_back(s);
} else {
LOG(ERROR) << "error creating segmentor: '" << ticket.klass << "'";
}
}
}
// create translators
if (auto translator_list = config->GetList("engine/translators")) {
size_t n = translator_list->size();
for (size_t i = 0; i < n; ++i) {
auto prescription = As<ConfigValue>(translator_list->GetAt(i));
if (!prescription)
continue;
Ticket ticket{this, "translator", prescription->str()};
if (auto c = Translator::Require(ticket.klass)) {
an<Translator> t(c->Create(ticket));
translators_.push_back(t);
} else {
LOG(ERROR) << "error creating translator: '" << ticket.klass << "'";
}
}
}
// create filters
if (auto filter_list = config->GetList("engine/filters")) {
size_t n = filter_list->size();
for (size_t i = 0; i < n; ++i) {
auto prescription = As<ConfigValue>(filter_list->GetAt(i));
if (!prescription)
continue;
Ticket ticket{this, "filter", prescription->str()};
if (auto c = Filter::Require(ticket.klass)) {
an<Filter> f(c->Create(ticket));
filters_.push_back(f);
} else {
LOG(ERROR) << "error creating filter: '" << ticket.klass << "'";
}
}
}

// Create components using inline template function
CreateComponentsFromList<Processor>(this, config, "engine/processors",
"processor", processors_);
CreateComponentsFromList<Segmentor>(this, config, "engine/segmentors",
"segmentor", segmentors_);
CreateComponentsFromList<Translator>(this, config, "engine/translators",
"translator", translators_);
CreateComponentsFromList<Filter>(this, config, "engine/filters", "filter",
filters_);
// create formatters
if (auto c = Formatter::Require("shape_formatter")) {
an<Formatter> f(c->Create(Ticket(this)));
auto c_formatter = Formatter::Require("shape_formatter");
if (c_formatter) {
an<Formatter> f(c_formatter->Create(Ticket(this)));
formatters_.push_back(f);
} else {
LOG(WARNING) << "shape_formatter not available.";
}
// create post-processors
if (auto c = Processor::Require("shape_processor")) {
an<Processor> p(c->Create(Ticket(this)));
auto c_processor = Processor::Require("shape_processor");
if (c_processor) {
an<Processor> p(c_processor->Create(Ticket(this)));
post_processors_.push_back(p);
} else {
LOG(WARNING) << "shape_processor not available.";
Expand Down
0