Skip to content

Commit

Permalink
Fix implementation of scripts in renaming/adding Emoji
Browse files Browse the repository at this point in the history
  • Loading branch information
tindy2013 committed Jul 11, 2020
1 parent 081b5ad commit 447f680
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
16 changes: 9 additions & 7 deletions src/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,6 @@ int duktape_call_function(duk_context *ctx, const std::string &name, size_t narg
return duk_pcall(ctx, nargs);
}

int duktape_get_res_int(duk_context *ctx)
{
int retval = duk_to_int(ctx, -1);
duk_pop(ctx);
return retval;
}

int duktape_push_nodeinfo(duk_context *ctx, const nodeInfo &node)
{
duk_push_object(ctx);
Expand Down Expand Up @@ -237,8 +230,17 @@ int duktape_push_nodeinfo_arr(duk_context *ctx, const nodeInfo &node, duk_idx_t
return 0;
}

int duktape_get_res_int(duk_context *ctx)
{
int retval = duk_to_int(ctx, -1);
duk_pop(ctx);
return retval;
}

std::string duktape_get_res_str(duk_context *ctx)
{
if(duk_is_null_or_undefined(ctx, -1))
return std::string();
std::string retstr = duk_safe_to_string(ctx, -1);
duk_pop(ctx);
return retstr;
Expand Down
24 changes: 10 additions & 14 deletions src/subexport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,11 @@ bool applyMatcher(const std::string &rule, std::string &real_rule, const nodeInf
return true;
}

std::string nodeRename(const nodeInfo &node, const string_array &rename_array)
void nodeRename(nodeInfo &node, const string_array &rename_array)
{
string_size pos;
std::string match, rep;
std::string remark = node.remarks, real_rule;
std::string &remark = node.remarks, original_remark = node.remarks, returned_remark, real_rule;
duk_context *ctx = NULL;
defer(duk_destroy_heap(ctx);)

Expand All @@ -531,8 +531,8 @@ std::string nodeRename(const nodeInfo &node, const string_array &rename_array)
{
duk_get_global_string(ctx, "rename");
duktape_push_nodeinfo(ctx, node);
if(duk_pcall(ctx, 1) == 0)
remark = duktape_get_res_str(ctx);
if(duk_pcall(ctx, 1) == 0 && !(returned_remark = duktape_get_res_str(ctx)).empty())
remark = returned_remark;
}
else
{
Expand All @@ -552,8 +552,8 @@ std::string nodeRename(const nodeInfo &node, const string_array &rename_array)
remark = regReplace(remark, real_rule, rep);
}
if(remark.empty())
return node.remarks;
return remark;
remark = original_remark;
return;
}

std::string removeEmoji(const std::string &orig_remark)
Expand All @@ -574,7 +574,7 @@ std::string removeEmoji(const std::string &orig_remark)

std::string addEmoji(const nodeInfo &node, const string_array &emoji_array)
{
std::string real_rule;
std::string real_rule, ret;
string_size pos;
duk_context *ctx = NULL;
defer(duk_destroy_heap(ctx);)
Expand All @@ -594,12 +594,8 @@ std::string addEmoji(const nodeInfo &node, const string_array &emoji_array)
{
duk_get_global_string(ctx, "getEmoji");
duktape_push_nodeinfo(ctx, node);
if(duk_pcall(ctx, 1) == 0)
{
std::string ret = duktape_get_res_str(ctx);
if(ret.size())
return ret + " " + node.remarks;
}
if(duk_pcall(ctx, 1) == 0 && !(ret = duktape_get_res_str(ctx)).empty())
return ret + " " + node.remarks;
}
else
{
Expand Down Expand Up @@ -1080,7 +1076,7 @@ void preprocessNodes(std::vector<nodeInfo> &nodes, const extra_settings &ext)
if(ext.remove_emoji)
x.remarks = trim(removeEmoji(x.remarks));

x.remarks = nodeRename(x, ext.rename_array);
nodeRename(x, ext.rename_array);

if(ext.add_emoji)
x.remarks = addEmoji(x, ext.emoji_array);
Expand Down

0 comments on commit 447f680

Please sign in to comment.