Commit d4115381 authored by kaniini's avatar kaniini

cnode: remove prefab entirely

there is no notable performance advantage with using prebaked erlang terms,
but the memory management has to be done carefully.

thusly, handle things in a safe way instead...
parent 1acba499
......@@ -25,12 +25,6 @@ typedef struct _state_t {
myhtml_t* myhtml;
} state_t;
typedef struct _prefab_t {
ETERM* atom_nil;
ETERM* atom_comment;
ETERM* empty_list;
} prefab_t;
void
handle_emsg(state_t* state, ErlMessage* emsg);
void
......@@ -38,7 +32,7 @@ handle_send(state_t* state, ErlMessage* emsg);
ETERM*
decode(state_t* state, ErlMessage* emsg, ETERM* bin, ETERM* args);
ETERM*
build_tree(prefab_t* prefab, myhtml_tree_t* tree, myhtml_tree_node_t* node, unsigned char* parse_flags);
build_tree(myhtml_tree_t* tree, myhtml_tree_node_t* node, unsigned char* parse_flags);
ETERM*
build_node_attrs(myhtml_tree_t* tree, myhtml_tree_node_t* node);
ETERM*
......@@ -208,13 +202,6 @@ ETERM*
decode(state_t* state, ErlMessage* emsg, ETERM* bin, ETERM* args)
{
unsigned char parse_flags = 0;
prefab_t prefab;
// prepare reusable prefab terms
prefab.atom_nil = erl_mk_atom("nil");
prefab.atom_comment = erl_mk_atom("comment");
prefab.empty_list = erl_mk_empty_list();
if (!ERL_IS_BINARY(bin) || !ERL_IS_LIST(args))
{
......@@ -240,12 +227,9 @@ decode(state_t* state, ErlMessage* emsg, ETERM* bin, ETERM* args)
// build tree
myhtml_tree_node_t *root = myhtml_tree_get_document(tree);
ETERM* result = build_tree(&prefab, tree, myhtml_node_last_child(root), &parse_flags);
ETERM* result = build_tree(tree, myhtml_node_last_child(root), &parse_flags);
myhtml_tree_destroy(tree);
erl_free_term(prefab.atom_nil);
erl_free_term(prefab.atom_comment);
return result;
}
......@@ -280,9 +264,12 @@ read_parse_flags(ETERM* list)
return parse_flags;
}
ETERM* build_tree(prefab_t* prefab, myhtml_tree_t* tree, myhtml_tree_node_t* node, unsigned char* parse_flags)
ETERM* build_tree(myhtml_tree_t* tree, myhtml_tree_node_t* node, unsigned char* parse_flags)
{
ETERM* result;
ETERM* atom_nil = erl_mk_atom("nil");
ETERM* empty_list = erl_mk_empty_list();
myhtml_tree_node_t* prev_node = NULL;
tstack stack;
......@@ -298,15 +285,15 @@ ETERM* build_tree(prefab_t* prefab, myhtml_tree_t* tree, myhtml_tree_node_t* nod
tstack_push(&stack, erl_mk_empty_list());
prev_node = current_node;
current_node=current_node->last_child;
current_node = current_node->last_child;
continue;
} else {
if ((myhtml_node_is_close_self(current_node) || myhtml_node_is_void_element(current_node))
&& (*parse_flags & FLAG_NIL_SELF_CLOSING)) {
children = prefab->atom_nil;
children = atom_nil;
} else {
children = prefab->empty_list;
children = empty_list;
}
}
......@@ -373,7 +360,6 @@ ETERM* build_tree(prefab_t* prefab, myhtml_tree_t* tree, myhtml_tree_node_t* nod
// attributes
attrs = build_node_attrs(tree, current_node);
if (!(*parse_flags & FLAG_HTML_ATOMS) || (tag_id == MyHTML_TAG__UNDEF || tag_id == MyHTML_TAG_LAST_ENTRY || tag_ns != MyHTML_NAMESPACE_HTML))
tag = erl_mk_binary(tag_string, tag_string_len);
else
......@@ -381,15 +367,20 @@ ETERM* build_tree(prefab_t* prefab, myhtml_tree_t* tree, myhtml_tree_node_t* nod
result = erl_format("{~w, ~w, ~w}", tag, attrs, children);
}
if (stack.used == 0) {
tstack_free(&stack);
return result;
break;
} else {
tstack_push(&stack, erl_cons(result, tstack_pop(&stack)));
prev_node = current_node;
current_node=current_node->prev ? current_node->prev : current_node->parent;
current_node = current_node->prev ? current_node->prev : current_node->parent;
}
}
erl_free_term(atom_nil);
return result;
}
ETERM*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment