-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
5b1373e
commit c3aa26f
Showing
8 changed files
with
171 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,5 @@ rhai_rustler-*.tar | |
/priv/native/* | ||
|
||
checksum-Elixir.Rhai.Native.exs | ||
|
||
.elixir-ls |
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,59 @@ | ||
defmodule Rhai.Engine do | ||
@moduledoc """ | ||
Rhai main scripting engine. | ||
""" | ||
|
||
defstruct [ | ||
# The actual NIF Resource. | ||
resource: nil, | ||
# Normally the compiler will happily do stuff like inlining the | ||
# resource in attributes. This will convert the resource into an | ||
# empty binary with no warning. This will make that harder to | ||
# accidentaly do. | ||
# It also serves as a handy way to tell file handles apart. | ||
reference: nil | ||
] | ||
|
||
@type t :: %__MODULE__{} | ||
|
||
@doc """ | ||
Create a new Engine | ||
""" | ||
@spec new :: t() | ||
def new do | ||
wrap_resource(Rhai.Native.engine_new()) | ||
end | ||
|
||
@doc """ | ||
Evaluate a string as a script, returning the result value or an error. | ||
""" | ||
@spec eval(t(), String.t()) :: {:ok, Rhai.rhai_any()} | {:error, Rhai.rhai_error()} | ||
def eval(%__MODULE__{resource: resource}, script) do | ||
Rhai.Native.engine_eval(resource, script) | ||
end | ||
|
||
@doc """ | ||
Set whether to raise error if an object map property does not exist. | ||
""" | ||
@spec set_fail_on_invalid_map_property(t(), boolean) :: t() | ||
def set_fail_on_invalid_map_property(%__MODULE__{resource: resource} = engine, enable) do | ||
Rhai.Native.engine_set_fail_on_invalid_map_property(resource, enable) | ||
|
||
engine | ||
end | ||
|
||
@doc """ | ||
Set whether to raise error if an object map property does not exist. | ||
""" | ||
@spec fail_on_invalid_map_property?(t()) :: boolean | ||
def fail_on_invalid_map_property?(%__MODULE__{resource: resource}) do | ||
Rhai.Native.engine_fail_on_invalid_map_property(resource) | ||
end | ||
|
||
defp wrap_resource(resource) do | ||
%__MODULE__{ | ||
resource: resource, | ||
reference: make_ref() | ||
} | ||
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
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,45 @@ | ||
use std::sync::Mutex; | ||
|
||
use rhai::{Dynamic, Engine}; | ||
use rustler::{Env, ResourceArc, Term}; | ||
|
||
use crate::{errors::to_error, types::from_dynamic}; | ||
|
||
pub struct EngineResource { | ||
pub engine: Mutex<Engine>, | ||
} | ||
|
||
#[rustler::nif] | ||
fn engine_new() -> ResourceArc<EngineResource> { | ||
ResourceArc::new(EngineResource { | ||
engine: Mutex::new(Engine::new()), | ||
}) | ||
} | ||
|
||
#[rustler::nif] | ||
fn engine_eval<'a>( | ||
env: Env<'a>, | ||
resource: ResourceArc<EngineResource>, | ||
script: &str, | ||
) -> Result<Term<'a>, Term<'a>> { | ||
let engine = resource.engine.try_lock().unwrap(); | ||
|
||
match engine.eval::<Dynamic>(script) { | ||
Ok(result) => Ok(from_dynamic(env, result)), | ||
Err(e) => Err(to_error(env, *e)), | ||
} | ||
} | ||
|
||
#[rustler::nif] | ||
fn engine_set_fail_on_invalid_map_property(resource: ResourceArc<EngineResource>, enable: bool) { | ||
let mut engine = resource.engine.try_lock().unwrap(); | ||
|
||
engine.set_fail_on_invalid_map_property(enable); | ||
} | ||
|
||
#[rustler::nif] | ||
fn engine_fail_on_invalid_map_property(resource: ResourceArc<EngineResource>) -> bool { | ||
let engine = resource.engine.try_lock().unwrap(); | ||
|
||
engine.fail_on_invalid_map_property() | ||
} |
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,33 @@ | ||
defmodule Rhai.EngineTest do | ||
use ExUnit.Case | ||
|
||
alias Rhai.Engine | ||
|
||
describe "new/0" do | ||
test "should create a new engine" do | ||
assert %Engine{} = Engine.new() | ||
end | ||
end | ||
|
||
describe "eval/1" do | ||
test "should eval a script" do | ||
engine = Engine.new() | ||
|
||
assert {:ok, 2} = Engine.eval(engine, "1 + 1") | ||
end | ||
end | ||
|
||
describe "set_fail_on_invalid_map_property/2, fail_on_invalid_map_property?/0" do | ||
test "should return false by default" do | ||
engine = Engine.new() | ||
|
||
refute Engine.fail_on_invalid_map_property?(engine) | ||
end | ||
|
||
test "should set fail on invalid map property to enabled" do | ||
assert Engine.new() | ||
|> Engine.set_fail_on_invalid_map_property(true) | ||
|> Engine.fail_on_invalid_map_property?() | ||
end | ||
end | ||
end |