Skip to content

Commit

Permalink
fix: integer float conversion and native deps bump (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziosestito authored Nov 1, 2023
1 parent fc2f5b7 commit 59a94ab
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .dialyzer_ignore.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
# something is wrong with the RustlerPrecompiled use macro
# this fixes it by ignoring the Rhai.Native module completely
{"lib/rhai/native.ex"},
]
8 changes: 4 additions & 4 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ jobs:
fail-fast: false
matrix:
include:
- otp: 24.1
elixir: 1.13
- otp: 26
elixir: 1.15.4
lint: true
- otp: 23.0
elixir: 1.11.2
- otp: 25
elixir: 1.14.3

env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
Expand Down
2 changes: 0 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,3 @@ defmodule Rhai.MixProject do
]
end
end


9 changes: 4 additions & 5 deletions native/rhai_rustler/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ pub fn from_dynamic(env: Env, value: Dynamic) -> Term {

pub fn to_dynamic<'a>(env: Env<'a>, term: &Term<'a>) -> Dynamic {
match Term::get_type(*term) {
rustler::TermType::Binary => term
TermType::Binary => term
.decode::<String>()
.map(Dynamic::from)
.expect("get_type() returned Binary but could not decode as string."),

rustler::TermType::Atom => term
TermType::Atom => term
.decode::<bool>()
.map(Dynamic::from)
.or_else(|_| {
Expand All @@ -46,8 +46,6 @@ pub fn to_dynamic<'a>(env: Env<'a>, term: &Term<'a>) -> Dynamic {
}
})
.expect("get_type() returned Atom but could not decode as string, boolean or empty."),
TermType::EmptyList => Dynamic::from(Vec::<Dynamic>::new()),
TermType::Exception => Dynamic::from(()),
TermType::Fun => Dynamic::from(()),
TermType::List => {
let items: Vec<Dynamic> = term
Expand All @@ -70,7 +68,7 @@ pub fn to_dynamic<'a>(env: Env<'a>, term: &Term<'a>) -> Dynamic {
}
Dynamic::from(object_map)
}
TermType::Number => term
TermType::Float => term
.decode::<i64>()
.map(Dynamic::from)
.or_else(|_| term.decode::<f64>().map(Dynamic::from))
Expand All @@ -90,5 +88,6 @@ pub fn to_dynamic<'a>(env: Env<'a>, term: &Term<'a>) -> Dynamic {
}

TermType::Unknown => Dynamic::from(()),
TermType::Integer => unreachable!(),
}
}
4 changes: 2 additions & 2 deletions native/test_dylib_module/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion native/test_dylib_module/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ path = "src/lib.rs"
crate-type = ["cdylib"]

[dependencies]
rhai-dylib = { version = "=0.1.11", features = ["sync"] }
rhai-dylib = { version = "=0.1.12", features = ["sync"] }
rustler = "0.30.0"
29 changes: 19 additions & 10 deletions test/rhai/property.exs → test/rhai/property_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ defmodule Rhai.PropertyTest do
use ExUnit.Case
use ExUnitProperties

alias Rhai.Engine
alias Rhai.{Engine, Scope}

describe "type conversion" do
property "should convert integer() to rhai integer type and vice-versa" do
engine = Engine.new()

check all int <- integer() do
assert {:ok, result} = Engine.eval(engine, "a", %{"a" => int})
scope = Scope.new() |> Scope.push("a", int)
assert {:ok, result} = Engine.eval_with_scope(engine, scope, "a")
assert int == result
assert is_integer(result)
end
Expand All @@ -19,7 +20,8 @@ defmodule Rhai.PropertyTest do
engine = Engine.new()

check all float <- float() do
assert {:ok, result} = Engine.eval(engine, "a", %{"a" => float})
scope = Scope.new() |> Scope.push("a", float)
assert {:ok, result} = Engine.eval_with_scope(engine, scope, "a")
assert float == result
assert is_float(result)
end
Expand All @@ -29,7 +31,8 @@ defmodule Rhai.PropertyTest do
engine = Engine.new()

check all bool <- boolean() do
assert {:ok, result} = Engine.eval(engine, "a", %{"a" => bool})
scope = Scope.new() |> Scope.push("a", bool)
assert {:ok, result} = Engine.eval_with_scope(engine, scope, "a")
assert bool == result
assert is_boolean(result)
end
Expand All @@ -39,7 +42,8 @@ defmodule Rhai.PropertyTest do
engine = Engine.new()

check all tuple <- tuple({integer(), string(:ascii)}) do
assert {:ok, result} = Engine.eval(engine, "a", %{"a" => tuple})
scope = Scope.new() |> Scope.push("a", tuple)
assert {:ok, result} = Engine.eval_with_scope(engine, scope, "a")
assert Tuple.to_list(tuple) == result
assert is_list(result)
end
Expand All @@ -49,7 +53,8 @@ defmodule Rhai.PropertyTest do
engine = Engine.new()

check all list <- list_of(string(:ascii)) do
assert {:ok, result} = Engine.eval(engine, "a", %{"a" => list})
scope = Scope.new() |> Scope.push("a", list)
assert {:ok, result} = Engine.eval_with_scope(engine, scope, "a")
assert list == result
assert is_list(list)
end
Expand All @@ -60,7 +65,8 @@ defmodule Rhai.PropertyTest do

check all str1 <- string(:ascii),
str2 <- string(:printable) do
assert {:ok, result} = Engine.eval(engine, "a + b", %{"a" => str1, "b" => str2})
scope = Scope.new() |> Scope.push("a", str1) |> Scope.push("b", str2)
assert {:ok, result} = Engine.eval_with_scope(engine, scope, "a + b")
assert str1 <> str2 == result
assert is_binary(result)
end
Expand All @@ -72,13 +78,16 @@ defmodule Rhai.PropertyTest do
check all map1 <- map_of(string(:ascii), string(:ascii)),
map2 <- map_of(string(:ascii), integer()),
map3 <- map_of(string(:ascii), map_of(string(:ascii), integer())) do
assert {:ok, result} = Engine.eval(engine, "a", %{"a" => map1})
scope =
Scope.new() |> Scope.push("a", map1) |> Scope.push("b", map2) |> Scope.push("c", map3)

assert {:ok, result} = Engine.eval_with_scope(engine, scope, "a")
assert map1 == result

assert {:ok, result} = Engine.eval(engine, "a", %{"a" => map2})
assert {:ok, result} = Engine.eval_with_scope(engine, scope, "b")
assert map2 == result

assert {:ok, result} = Engine.eval(engine, "a", %{"a" => map3})
assert {:ok, result} = Engine.eval_with_scope(engine, scope, "c")
assert map3 == result
end
end
Expand Down

0 comments on commit 59a94ab

Please sign in to comment.