-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
if System.otp_release() >= "26" do | ||
defmodule ResourceDyncall do | ||
use Rustler, | ||
otp_app: :rustler_test, | ||
crate: :resource_dyncall | ||
|
||
def new(_), do: err() | ||
|
||
defp err(), do: :erlang.nif_error(:nif_not_loaded) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "resource_dyncall" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
name = "resource_dyncall" | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
rustler = { path = "../../../rustler", features = ["allocator", "nif_version_2_16"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use rustler::{Env, LocalPid, Resource, ResourceArc}; | ||
|
||
#[repr(C)] | ||
struct Params { | ||
a: i64, | ||
b: i64, | ||
c: i64, | ||
sent_to: Option<LocalPid>, | ||
} | ||
|
||
struct ResourceWithDyncall { | ||
pid: LocalPid, | ||
} | ||
|
||
#[rustler::resource_impl(name = "resource_with_dyncall")] | ||
impl Resource for ResourceWithDyncall { | ||
unsafe fn dyncall<'a>(&'a self, env: Env<'a>, call_data: *mut rustler::sys::c_void) { | ||
let p = &mut *(call_data as *mut Params); | ||
if let Ok(()) = env.send(&self.pid, (p.a, p.b, p.c)) { | ||
p.sent_to = Some(self.pid); | ||
} else { | ||
p.sent_to = None | ||
} | ||
} | ||
} | ||
|
||
#[rustler::nif] | ||
fn new(pid: LocalPid) -> ResourceArc<ResourceWithDyncall> { | ||
ResourceWithDyncall { pid }.into() | ||
} | ||
|
||
rustler::init!("Elixir.ResourceDyncall"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use rustler::{Env, LocalPid, Term}; | ||
|
||
rustler::atoms! { | ||
module = "Elixir.ResourceDyncall", | ||
resource_name = "resource_with_dyncall", | ||
} | ||
|
||
#[repr(C)] | ||
struct Params { | ||
a: i64, | ||
b: i64, | ||
c: i64, | ||
sent_to: Option<LocalPid>, | ||
} | ||
|
||
#[rustler::nif] | ||
pub fn perform_dyncall<'a>( | ||
env: Env<'a>, | ||
resource: Term<'a>, | ||
a: i64, | ||
b: i64, | ||
c: i64, | ||
) -> Option<LocalPid> { | ||
let mut params = Params { | ||
a, | ||
b, | ||
c, | ||
sent_to: None, | ||
}; | ||
|
||
unsafe { | ||
let params_ptr = std::ptr::addr_of_mut!(params) as *mut rustler::sys::c_void; | ||
let _ = env.dynamic_resource_call(module(), resource_name(), resource, params_ptr); | ||
} | ||
|
||
params.sent_to | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
if System.otp_release() >= "26" do | ||
defmodule RustlerTest.ResourceDyncallTest do | ||
use ExUnit.Case, async: true | ||
|
||
test "perform dyncall" do | ||
pid = self() | ||
|
||
res = ResourceDyncall.new(pid) | ||
|
||
call_res = RustlerTest.perform_dyncall(res, 1, 2, 3) | ||
|
||
assert call_res == pid | ||
|
||
receive do | ||
{1, 2, 3} -> true | ||
after | ||
50 -> | ||
raise "fail" | ||
end | ||
end | ||
end | ||
end |