Skip to content

Commit

Permalink
Merge pull request #111 from membraneframework/bugged-nil-unfix
Browse files Browse the repository at this point in the history
support for old bugged nil return functions as well as fixed ones
  • Loading branch information
bartkrak authored Mar 11, 2024
2 parents edc3ad1 + 20bf836 commit 6a9ae37
Show file tree
Hide file tree
Showing 15 changed files with 509 additions and 137 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Unifex uses [Bundlex](https://github.com/membraneframework/bundlex) to compile a

This tool is maintained by the [Membrane Framework](https://membraneframework.org/) team.

This version supports bugged version of functions returning nil, as well as fixed ones. This functionality should be removed in version 2.0.0
For more info see: [Issue](https://github.com/membraneframework/membrane_core/issues/758)

## Installation

To install, you need to configure Mix project as follows:
Expand All @@ -36,7 +39,7 @@ defmodule MyApp.Mixfile do

defp deps() do
[
{:unifex, "~>> 1.1.3"}
{:unifex, "~>> 1.1.2"}
]
end
end
Expand Down
70 changes: 68 additions & 2 deletions lib/unifex/code_generators/nif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,17 @@ defmodule Unifex.CodeGenerators.NIF do
* Functions that create the defined output from Nif.
* They are automatically generated and don't need to be implemented.
*/
#{Utils.generate_functions_declarations(specs.functions_results,
&generate_result_function_declaration/2,
ctx)}
/*
* Bugged version of functions returning nil, left for backwards compabiliy with older code using unifex
* Generating of these functions should be removed in unifex v2.0.0
* For more information check: https://github.com/membraneframework/membrane_core/issues/758
*/
#{generate_functions_declarations_bugged(specs.functions_results, ctx)}
/*
* Functions that send the defined messages from Nif.
* They are automatically generated and don't need to be implemented.
Expand All @@ -96,13 +102,18 @@ defmodule Unifex.CodeGenerators.NIF do

~g"""
#include "#{specs.name}.h"
#{Utils.generate_functions(specs.functions_results, &generate_result_function/2, ctx)}
#{Utils.generate_functions(specs.sends, &generate_send_function/2, ctx)}
#{generate_state_related_functions(specs)}
#{generate_nif_lifecycle_callbacks(specs)}
#{Utils.generate_functions(specs.functions_args, &generate_export_function/2, ctx)}
#{generate_erlang_boilerplate(specs)}
/*
* Bugged version of functions returning nil, generated for backwards compabiliy with older code using unifex
* Generating of these functions should be removed in unifex v2.0.0
* For more information check: https://github.com/membraneframework/membrane_core/issues/758
*/
#{generate_functions_bugged(specs.functions_results, ctx)}
"""
end

Expand Down Expand Up @@ -456,4 +467,59 @@ defmodule Unifex.CodeGenerators.NIF do
defp generate_struct_native_definition(struct_data, ctx) do
Unifex.CodeGenerators.Common.generate_struct_native_definition(struct_data, NIF, ctx)
end

# This function generates bugged version of functions declarations returning nil and should be removed in v2.0.0
defp generate_functions_declarations_bugged(config, ctx) do
config
|> Enum.flat_map(fn {name, result} ->
{_result, meta} = generate_serialization(result, ctx)
args = meta |> Keyword.get_values(:arg)

labels =
meta
|> Keyword.get_values(:label)

if Enum.member?(labels, "nil") do
args_declarations =
[~g<UnifexEnv* env> | generate_args_declarations(args, :const_unless_ptr_on_ptr, ctx)]
|> Enum.join(", ")

[~g<UNIFEX_TERM #{[name, :result, ""] |> Enum.join("_")}(#{args_declarations});>]
else
[]
end
end)
|> Enum.join("\n")
end

# This function generates bugged version of functions returning nil and should be removed in v2.0.0
defp generate_functions_bugged(config, ctx) do
config
|> Enum.flat_map(fn {name, result} ->
{result, meta} = generate_serialization(result, ctx)

labels =
meta
|> Keyword.get_values(:label)

if Enum.member?(labels, "nil") do
args = meta |> Keyword.get_values(:arg)

args_declarations =
[~g<UnifexEnv* env> | generate_args_declarations(args, :const_unless_ptr_on_ptr, ctx)]
|> Enum.join(", ")

[
~g"""
UNIFEX_TERM #{[name, :result, ""] |> Enum.join("_")}(#{args_declarations}) {
return #{result |> String.replace("\"nil\"", "\"\"")};
}
"""
]
else
[]
end
end)
|> Enum.join("\n")
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
defmodule Unifex.MixProject do
use Mix.Project

@version "1.1.3"
@version "1.1.2"

@github_url "https://github.com/membraneframework/unifex"

def project do
Expand Down
8 changes: 7 additions & 1 deletion test/fixtures/bundlex_exs_ref_generated/nif/example.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "example.h"

UNIFEX_TERM foo_result_ok(UnifexEnv *env, int answer) {
return ({
const ERL_NIF_TERM terms[] = {enif_make_atom(env, "ok"),
Expand Down Expand Up @@ -46,3 +45,10 @@ static ERL_NIF_TERM export_foo(ErlNifEnv *env, int argc,
static ErlNifFunc nif_funcs[] = {{"unifex_foo", 1, export_foo, 0}};

ERL_NIF_INIT(Elixir.Example.Nif, nif_funcs, unifex_load_nif, NULL, NULL, NULL)

/*
* Bugged version of functions returning nil, generated for backwards compabiliy
* with older code using unifex Generating of these functions should be removed
* in unifex v2.0.0 For more information check:
* https://github.com/membraneframework/membrane_core/issues/758
*/
8 changes: 7 additions & 1 deletion test/fixtures/bundlex_exs_ref_generated/nif/example.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "example.h"

UNIFEX_TERM foo_result_ok(UnifexEnv *env, int answer) {
return ({
const ERL_NIF_TERM terms[] = {enif_make_atom(env, "ok"),
Expand Down Expand Up @@ -46,3 +45,10 @@ static ERL_NIF_TERM export_foo(ErlNifEnv *env, int argc,
static ErlNifFunc nif_funcs[] = {{"unifex_foo", 1, export_foo, 0}};

ERL_NIF_INIT(Elixir.Example.Nif, nif_funcs, unifex_load_nif, NULL, NULL, NULL)

/*
* Bugged version of functions returning nil, generated for backwards compabiliy
* with older code using unifex Generating of these functions should be removed
* in unifex v2.0.0 For more information check:
* https://github.com/membraneframework/membrane_core/issues/758
*/
8 changes: 7 additions & 1 deletion test/fixtures/bundlex_exs_ref_generated/nif/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ UNIFEX_TERM foo(UnifexEnv *env, int num);
* Functions that create the defined output from Nif.
* They are automatically generated and don't need to be implemented.
*/

UNIFEX_TERM foo_result_ok(UnifexEnv *env, int answer);

/*
* Bugged version of functions returning nil, left for backwards compabiliy with
* older code using unifex Generating of these functions should be removed in
* unifex v2.0.0 For more information check:
* https://github.com/membraneframework/membrane_core/issues/758
*/

/*
* Functions that send the defined messages from Nif.
* They are automatically generated and don't need to be implemented.
Expand Down
Loading

0 comments on commit 6a9ae37

Please sign in to comment.