Commit bc5cd1df authored by kaniini's avatar kaniini

Merge branch 'feature/ei-porting' into 'master'

basically rewrite myhtml_worker from scratch to use EI

Closes #1

See merge request pleroma/myhtmlex!5
parents ad0097e2 fa87b8f8
Pipeline #19607 passed with stage
in 1 minute
MIX = mix
MYHTMLEX_CFLAGS = -g -O2 -std=c99 -pedantic -Wcomment -Wall
MYHTMLEX_CFLAGS = -g -O2 -std=c99 -pedantic -Wcomment -Wextra -Wno-old-style-declaration -Wall
# we need to compile position independent code
MYHTMLEX_CFLAGS += -fpic -DPIC
# For some reason __erl_errno is undefined unless _REENTRANT is defined
......@@ -38,7 +38,16 @@ ERL_INTERFACE = $(wildcard $(ERLANG_PATH)/../lib/erl_interface-*)
CNODE_CFLAGS = $(MYHTMLEX_CFLAGS)
CNODE_CFLAGS += -L$(ERL_INTERFACE)/lib
CNODE_CFLAGS += -I$(ERL_INTERFACE)/include
CNODE_CFLAGS += -lerl_interface -lei -pthread
CNODE_LDFLAGS =
ifeq ($(OTP22_DEF),YES)
CNODE_CFLAGS += -DOTP_22_OR_NEWER
else
CNODE_LDFLAGS += -lerl_interface
endif
CNODE_LDFLAGS += -lei -pthread
# enumerate docker build tests
BUILD_TESTS := $(patsubst %.dockerfile, %.dockerfile.PHONY, $(wildcard ./build-test/*.dockerfile))
......@@ -65,7 +74,7 @@ $(MYHTML_STATIC): $(MYHTML_PATH)
$(MAKE) -C $(MYHTML_PATH) library MyCORE_BUILD_WITHOUT_THREADS=YES
priv/myhtml_worker: c_src/myhtml_worker.c $(MYHTML_STATIC)
$(CC) -o $@ $< $(MYHTML_STATIC) $(CNODE_CFLAGS)
$(CC) -o $@ $< $(MYHTML_STATIC) $(CNODE_CFLAGS) $(CNODE_LDFLAGS)
clean: clean-myhtml
$(RM) -r priv/myhtmlex*
......
This diff is collapsed.
#ifndef TSTACK_H
#define TSTACK_H
#include "ei.h"
#define GROW_BY 30
typedef struct {
ETERM* *data;
myhtml_tree_node_t **data;
size_t used;
size_t size;
} tstack;
void tstack_init(tstack *stack, size_t initial_size) {
stack->data = (ETERM **) malloc(initial_size * sizeof(ETERM*));
stack->data = (myhtml_tree_node_t **) malloc(initial_size * sizeof(myhtml_tree_node_t *));
stack->used = 0;
stack->size = initial_size;
}
......@@ -21,18 +20,18 @@ void tstack_free(tstack *stack) {
}
void tstack_resize(tstack *stack, size_t new_size) {
stack->data = (ETERM **)realloc(stack->data, new_size * sizeof(ETERM*));
stack->data = (myhtml_tree_node_t **) realloc(stack->data, new_size * sizeof(myhtml_tree_node_t *));
stack->size = new_size;
}
void tstack_push(tstack *stack, ETERM* element) {
void tstack_push(tstack *stack, myhtml_tree_node_t * element) {
if(stack->used == stack->size) {
tstack_resize(stack, stack->size + GROW_BY);
}
stack->data[stack->used++] = element;
stack->data[stack->used++] = element;
}
ETERM* tstack_pop(tstack *stack) {
myhtml_tree_node_t* tstack_pop(tstack *stack) {
return stack->data[--(stack->used)];
}
......
......@@ -93,6 +93,16 @@ defmodule Mix.Tasks.Compile.MyhtmlexMake do
end
end
defp otp_version do
:erlang.system_info(:otp_release)
|> to_string()
|> String.to_integer()
end
defp otp_22_or_newer? do
otp_version() >= 22
end
def run(_) do
make_cmd = find_make()
......@@ -105,7 +115,10 @@ defmodule Mix.Tasks.Compile.MyhtmlexMake do
make_cmd,
@artifacts,
stderr_to_stdout: true,
env: [{"MIX_ENV", to_string(Mix.env())}]
env: [
{"MIX_ENV", to_string(Mix.env())},
{"OTP22_DEF", (otp_22_or_newer?() && "YES") || "NO"}
]
)
IO.binwrite(result)
......
defmodule Myhtmlex.SafeTest do
use MyhtmlexSharedTests, module: Myhtmlex.Safe
use ExUnit.Case
doctest Myhtmlex
test "doesn't segfault when <!----> is encountered" do
assert {"html", _attrs, _children} = Myhtmlex.decode("<div> <!----> </div>")
end
test "builds a tree, formatted like mochiweb by default" do
assert {"html", [],
[
{"head", [], []},
{"body", [],
[
{"br", [], []}
]}
]} = Myhtmlex.decode("<br>")
end
test "builds a tree, html tags as atoms" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{:br, [], []}
]}
]} = Myhtmlex.decode("<br>", format: [:html_atoms])
end
test "builds a tree, nil self closing" do
assert {"html", [],
[
{"head", [], []},
{"body", [],
[
{"br", [], nil},
{"esi:include", [], nil}
]}
]} = Myhtmlex.decode("<br><esi:include />", format: [:nil_self_closing])
end
test "builds a tree, multiple format options" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{:br, [], nil}
]}
]} = Myhtmlex.decode("<br>", format: [:html_atoms, :nil_self_closing])
end
test "attributes" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{:span, [{"id", "test"}, {"class", "foo garble"}], []}
]}
]} =
Myhtmlex.decode(~s'<span id="test" class="foo garble"></span>',
format: [:html_atoms]
)
end
test "single attributes" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{:button, [{"disabled", "disabled"}, {"class", "foo garble"}], []}
]}
]} =
Myhtmlex.decode(~s'<button disabled class="foo garble"></span>',
format: [:html_atoms]
)
end
test "text nodes" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
"text node"
]}
]} = Myhtmlex.decode(~s'<body>text node</body>', format: [:html_atoms])
end
test "broken input" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{:a, [{"<", "<"}], [" asdf"]}
]}
]} = Myhtmlex.decode(~s'<a <> asdf', format: [:html_atoms])
end
test "namespaced tags" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{"svg:svg", [],
[
{"svg:path", [], []},
{"svg:a", [], []}
]}
]}
]} = Myhtmlex.decode(~s'<svg><path></path><a></a></svg>', format: [:html_atoms])
end
test "custom namespaced tags" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{"esi:include", [], nil}
]}
]} = Myhtmlex.decode(~s'<esi:include />', format: [:html_atoms, :nil_self_closing])
end
test "html comments" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
comment: " a comment "
]}
]} = Myhtmlex.decode(~s'<body><!-- a comment --></body>', format: [:html_atoms])
end
end
defmodule MyhtmlexSharedTests do
defmacro __using__(opts) do
module = Keyword.fetch!(opts, :module)
quote do
use ExUnit.Case
doctest Myhtmlex
setup_all(_) do
Application.put_env(:myhtmlex, :mode, unquote(module))
:ok
end
test "builds a tree, formatted like mochiweb by default" do
assert {"html", [],
[
{"head", [], []},
{"body", [],
[
{"br", [], []}
]}
]} = Myhtmlex.decode("<br>")
end
test "builds a tree, html tags as atoms" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{:br, [], []}
]}
]} = Myhtmlex.decode("<br>", format: [:html_atoms])
end
test "builds a tree, nil self closing" do
assert {"html", [],
[
{"head", [], []},
{"body", [],
[
{"br", [], nil},
{"esi:include", [], nil}
]}
]} = Myhtmlex.decode("<br><esi:include />", format: [:nil_self_closing])
end
test "builds a tree, multiple format options" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{:br, [], nil}
]}
]} = Myhtmlex.decode("<br>", format: [:html_atoms, :nil_self_closing])
end
test "attributes" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{:span, [{"id", "test"}, {"class", "foo garble"}], []}
]}
]} =
Myhtmlex.decode(~s'<span id="test" class="foo garble"></span>',
format: [:html_atoms]
)
end
test "single attributes" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{:button, [{"disabled", "disabled"}, {"class", "foo garble"}], []}
]}
]} =
Myhtmlex.decode(~s'<button disabled class="foo garble"></span>',
format: [:html_atoms]
)
end
test "text nodes" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
"text node"
]}
]} = Myhtmlex.decode(~s'<body>text node</body>', format: [:html_atoms])
end
test "broken input" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{:a, [{"<", "<"}], [" asdf"]}
]}
]} = Myhtmlex.decode(~s'<a <> asdf', format: [:html_atoms])
end
test "namespaced tags" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{"svg:svg", [],
[
{"svg:path", [], []},
{"svg:a", [], []}
]}
]}
]} = Myhtmlex.decode(~s'<svg><path></path><a></a></svg>', format: [:html_atoms])
end
test "custom namespaced tags" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
{"esi:include", [], nil}
]}
]} =
Myhtmlex.decode(~s'<esi:include />', format: [:html_atoms, :nil_self_closing])
end
test "html comments" do
assert {:html, [],
[
{:head, [], []},
{:body, [],
[
comment: " a comment "
]}
]} = Myhtmlex.decode(~s'<body><!-- a comment --></body>', format: [:html_atoms])
end
end
# quote
end
# defmacro __using__
end
Code.require_file("myhtmlex_shared_tests.ex", "test")
ExUnit.start()
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