Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind Engine::new_raw() #81

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/rhai/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ defmodule Rhai.Engine do
wrap_resource(Rhai.Native.engine_new())
end

@doc """
Create a new Engine with minimal built-in functions.
"""
@spec new_raw :: t()
def new_raw do
wrap_resource(Rhai.Native.engine_new_raw())
end

@doc """
Register a shared dylib Module into the global namespace of Engine.

Expand Down
1 change: 1 addition & 0 deletions lib/rhai/native.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule Rhai.Native do
def eval(_, _), do: err()
# engine
def engine_new, do: err()
def engine_new_raw, do: err()
def engine_register_global_module(_engine, _path), do: err()
def engine_register_static_module(_engine, _namespace, _path), do: err()
def engine_compile(_engine, _script), do: err()
Expand Down
9 changes: 9 additions & 0 deletions native/rhai_rustler/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ fn engine_new() -> ResourceArc<EngineResource> {
})
}

#[rustler::nif]
fn engine_new_raw() -> ResourceArc<EngineResource> {
let engine = Engine::new_raw();

ResourceArc::new(EngineResource {
engine: Mutex::new(engine),
})
}

#[rustler::nif]
fn engine_register_global_module(
resource: ResourceArc<EngineResource>,
Expand Down
1 change: 1 addition & 0 deletions native/rhai_rustler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ rustler::init!(
eval,
// engine
engine_new,
engine_new_raw,
engine_compile,
engine_compile_with_scope,
engine_compile_expression,
Expand Down
6 changes: 6 additions & 0 deletions test/rhai/engine_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ defmodule Rhai.EngineTest do
end
end

describe "new_raw/0" do
test "should create a new raw engine" do
assert %Engine{} = Engine.new_raw()
end
end

describe "module resolvers" do
test "should load a dylib module via the import directive" do
assert {:ok, [6, "inner", "value"]} =
Expand Down