Skip to content

Commit

Permalink
s/elixir/beam/g
Browse files Browse the repository at this point in the history
  • Loading branch information
ityonemo committed Oct 9, 2019
1 parent eacb111 commit aea8238
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 40 deletions.
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -64,18 +64,18 @@ defmodule Allocations do
use Zigler, app: :my_app
~Z"""
@nif("double_atom")
fn double_atom(env: elixir.env, string: []u8) elixir.atom {
var double_string = elixir.allocator.alloc(u8, string.len * 2)
catch elixir.enomem(env);
fn double_atom(env: beam.env, string: []u8) beam.atom {
var double_string = beam.allocator.alloc(u8, string.len * 2)
catch beam.enomem(env);
defer elixir.allocator.free(double_string);
defer beam.allocator.free(double_string);
for (string) | char, i | {
double_string[i] = char;
double_string[i + string.len] = char;
}
return elixir.make_atom(env, double_string);
return beam.make_atom(env, double_string);
}
"""
end
Expand Down
2 changes: 1 addition & 1 deletion assets/nif_header.zig
@@ -1,3 +1,3 @@
// import a header containing all of the NIF ABI forwards.
const e = @import("erl_nif.zig").c;
const elixir = @import("elixir.zig");
const beam = @import("beam.zig");
8 changes: 4 additions & 4 deletions lib/zigler/compiler.ex
Expand Up @@ -5,7 +5,7 @@ defmodule Zigler.Compiler do

@zig_dir_path Path.expand("../../../zig", __ENV__.file)
@erl_nif_zig_h Path.join(@zig_dir_path, "include/erl_nif_zig.h")
@erl_nif_zig_eex File.read!("zig/elixir/erl_nif.zig")
@erl_nif_zig_eex File.read!("zig/beam/erl_nif.zig")

def basename(version) do
os = case :os.type do
Expand Down Expand Up @@ -58,9 +58,9 @@ defmodule Zigler.Compiler do
# put the erl_nif.zig adapter into the path.
erl_nif_zig_path = Path.join(tmp_dir, "erl_nif.zig")
File.write!(erl_nif_zig_path, EEx.eval_string(@erl_nif_zig_eex, erl_nif_zig_h: @erl_nif_zig_h))
# now put the elixir.zig file into the temporary directory too.
elixir_zig_src = Path.join(@zig_dir_path, "elixir/elixir.zig")
File.cp!(elixir_zig_src, Path.join(tmp_dir, "elixir.zig"))
# now put the beam.zig file into the temporary directory too.
beam_zig_src = Path.join(@zig_dir_path, "beam/beam.zig")
File.cp!(beam_zig_src, Path.join(tmp_dir, "beam.zig"))
# now put the erl_nif.h file into the temporary directory.
erl_nif_zig_h_path = Path.join(@zig_dir_path, "include/erl_nif_zig.h")
File.cp!(erl_nif_zig_h_path, Path.join(tmp_dir, "erl_nif_zig.h"))
Expand Down
32 changes: 16 additions & 16 deletions lib/zigler/zig.ex
Expand Up @@ -92,7 +92,7 @@ defmodule Zigler.Zig do
def params([_, ":", "[", "]", "f64"]), do: [:"[]f64"]
def params([_, ":", type, "," | rest]), do: [String.to_atom(type) | params(rest)]
def params([_, ":", "?", "*", "e.ErlNifEnv", "," | rest]), do: [:"?*e.ErlNifEnv" | params(rest)]
def params([_, ":", "elixir.env", "," | rest]), do: [:"elixir.env" | params(rest)]
def params([_, ":", "beam.env", "," | rest]), do: [:"beam.env" | params(rest)]
def params([_, ":", "[", "*", "c", "]", "u8", "," | rest]), do: [:"[*c]u8" | params(rest)]
def params([_, ":", "[", "]", "u8", "," | rest]), do: [:"[]u8" | params(rest)]
def params([_, ":", "[", "]", "i64", "," | rest]), do: [:"[]i64" | params(rest)]
Expand All @@ -103,12 +103,12 @@ defmodule Zigler.Zig do

@spec nif_adapter({atom, {[atom], atom}}) :: iodata
def nif_adapter({func, {params, type}}) do
has_env = match?([:"?*e.ErlNifEnv" | _], params) || match?([:"elixir.env" | _], params)
has_env = match?([:"?*e.ErlNifEnv" | _], params) || match?([:"beam.env" | _], params)
EEx.eval_string(@nif_adapter, func: func, params: adjust_params(params), type: type, has_env: has_env)
end

def adjust_params(params) do
Enum.reject(params, &(&1 in [:"?*e.ErlNifEnv" , :"elixir.env"]))
Enum.reject(params, &(&1 in [:"?*e.ErlNifEnv" , :"beam.env"]))
end

# TODO: move these to an "ASSEMBLER" module.
Expand Down Expand Up @@ -161,8 +161,8 @@ defmodule Zigler.Zig do
if (res != 0) {
// but first we have to allocate memory.
arg#{idx} = elixir.allocator.alloc(i64, @intCast(usize, length#{idx}))
catch elixir.enomem(env);
arg#{idx} = beam.allocator.alloc(i64, @intCast(usize, length#{idx}))
catch beam.enomem(env);
while (idx#{idx} < length#{idx}) {
res = e.enif_get_list_cell(env, list#{idx}, &head#{idx}, &list#{idx});
Expand All @@ -176,7 +176,7 @@ defmodule Zigler.Zig do
}
// free it after we're done with the entire function.
defer elixir.allocator.free(arg#{idx});
defer beam.allocator.free(arg#{idx});
"""
def getfor(:"[]f64", idx), do: """
var length#{idx}: c_uint = undefined;
Expand All @@ -191,8 +191,8 @@ defmodule Zigler.Zig do
if (res != 0) {
// but first we have to allocate memory.
arg#{idx} = elixir.allocator.alloc(f64, @intCast(usize, length#{idx}))
catch elixir.enomem(env);
arg#{idx} = beam.allocator.alloc(f64, @intCast(usize, length#{idx}))
catch beam.enomem(env);
while (idx#{idx} < length#{idx}) {
res = e.enif_get_list_cell(env, list#{idx}, &head#{idx}, &list#{idx});
Expand All @@ -206,14 +206,14 @@ defmodule Zigler.Zig do
}
// free it after we're done with the entire function.
defer elixir.allocator.free(arg#{idx});
defer beam.allocator.free(arg#{idx});
"""
def getfor(:"e.ErlNifTerm", idx), do: "arg#{idx} = argv[#{idx}];"
def getfor(:"e.ErlNifPid", idx), do: """
res = e.enif_get_local_pid(env, argv[#{idx}], &arg#{idx});
"""

def makefor(:"elixir.atom"), do: "return result;"
def makefor(:"beam.atom"), do: "return result;"
def makefor(:c_int), do: "return e.enif_make_int(env, result);"
def makefor(:i64), do: "return e.enif_make_int(env, @intCast(c_int, result));"
def makefor(:f64), do: "return e.enif_make_double(env, result);"
Expand Down Expand Up @@ -245,9 +245,9 @@ defmodule Zigler.Zig do
return result_term;
"""
def makefor(:"[]i64"), do: """
var term_slice = elixir.allocator.alloc(e.ErlNifTerm, result.len)
catch elixir.enomem(env);
defer elixir.allocator.free(term_slice);
var term_slice = beam.allocator.alloc(e.ErlNifTerm, result.len)
catch beam.enomem(env);
defer beam.allocator.free(term_slice);
for (term_slice) | _term, i | {
term_slice[i] = e.enif_make_int(env, @intCast(c_int, result[i]));
Expand All @@ -258,9 +258,9 @@ defmodule Zigler.Zig do
return result_term;
"""
def makefor(:"[]f64"), do: """
var term_slice = elixir.allocator.alloc(e.ErlNifTerm, result.len)
catch elixir.enomem(env);
defer elixir.allocator.free(term_slice);
var term_slice = beam.allocator.alloc(e.ErlNifTerm, result.len)
catch beam.enomem(env);
defer beam.allocator.free(term_slice);
for (term_slice) | _term, i | {
term_slice[i] = e.enif_make_double(env, result[i]);
Expand Down
14 changes: 7 additions & 7 deletions test/allocators_test.exs
Expand Up @@ -9,8 +9,8 @@ defmodule ZigTest.AllocatorsTest do
fn alloctest(env: ?*e.ErlNifEnv, length: i64) e.ErlNifTerm {
var usize_length = @intCast(usize, length);
var slice = elixir.allocator.alloc(u8, usize_length) catch elixir.enomem(env);
defer elixir.allocator.free(slice);
var slice = beam.allocator.alloc(u8, usize_length) catch beam.enomem(env);
defer beam.allocator.free(slice);
// fill the slice with letters
for (slice) | _char, i | {
Expand All @@ -24,10 +24,10 @@ defmodule ZigTest.AllocatorsTest do
fn realloctest(env: ?*e.ErlNifEnv, length: i64) e.ErlNifTerm {
var usize_length = @intCast(usize, length);
var slice = elixir.allocator.alloc(u8, usize_length) catch elixir.enomem(env);
defer elixir.allocator.free(slice);
var slice = beam.allocator.alloc(u8, usize_length) catch beam.enomem(env);
defer beam.allocator.free(slice);
var slice2 = elixir.allocator.realloc(slice, usize_length * 2) catch elixir.enomem(env);
var slice2 = beam.allocator.realloc(slice, usize_length * 2) catch beam.enomem(env);
// fill the slice with letters
for (slice2) | _char, i | {
Expand Down Expand Up @@ -62,7 +62,7 @@ defmodule ZigTest.AllocatorsTest do
var usize_length = @intCast(usize, length);
global_slice = elixir.allocator.alloc(u8, usize_length) catch elixir.enomem(env);
global_slice = beam.allocator.alloc(u8, usize_length) catch beam.enomem(env);
// don't defer a free here (don't do this in real life!!!)
Expand Down Expand Up @@ -101,7 +101,7 @@ defmodule ZigTest.AllocatorsTest do
var usize_length = @intCast(usize, length);
global_slice = elixir.allocator.alloc(u8, usize_length) catch elixir.enomem(env);
global_slice = beam.allocator.alloc(u8, usize_length) catch beam.enomem(env);
// don't defer a free here (don't do this in real life!!!)
// fill the slice with letters
Expand Down
10 changes: 5 additions & 5 deletions test/readme_test.exs
Expand Up @@ -45,19 +45,19 @@ defmodule ZiglerTest.ReadmeTest do
use Zigler, app: :zigler
~Z"""
@nif("double_atom")
fn double_atom(env: elixir.env, string: []u8) elixir.atom {
fn double_atom(env: beam.env, string: []u8) beam.atom {
var double_string = elixir.allocator.alloc(u8, string.len * 2)
catch elixir.enomem(env);
var double_string = beam.allocator.alloc(u8, string.len * 2)
catch beam.enomem(env);
defer elixir.allocator.free(double_string);
defer beam.allocator.free(double_string);
for (string) | char, i | {
double_string[i] = char;
double_string[i + string.len] = char;
}
return elixir.make_atom(env, double_string);
return beam.make_atom(env, double_string);
}
"""
end
Expand Down
2 changes: 1 addition & 1 deletion test/zig_adlib_test.exs
Expand Up @@ -24,7 +24,7 @@ defmodule ZiglerTest.ZigAdlibTest do
assert """
// import a header containing all of the NIF ABI forwards.
const e = @import("erl_nif.zig").c;
const elixir = @import("elixir.zig");
const beam = @import("beam.zig");
""" == Zig.nif_header()
end

Expand Down
2 changes: 1 addition & 1 deletion test/zigler_types_test.exs
Expand Up @@ -11,7 +11,7 @@ defmodule ZiglerTest.ZiglerTypesTest do
}
@nif("zeroarity2")
fn zeroarity2(env: elixir.env) i64 {
fn zeroarity2(env: beam.env) i64 {
return 47;
}
"""
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit aea8238

Please sign in to comment.