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

feature: Add experimental support for constructing Kernel exprs #62

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
[alias]
xtask = "run --package xtask --"

# Avoid undefined symbol errors produced by the linker when calling functions
# declared in wolfram-library-link/src/kernel/sys.rs that aren't resolved until
# the built LibraryLink library is loaded into a running Kernel.
[build]
rustflags = ["-C", "link-args=-undefined dynamic_lookup"]
2 changes: 2 additions & 0 deletions wolfram-library-link/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ nightly = []
panic-failure-backtraces = ["backtrace"]
automate-function-loading-boilerplate = ["inventory", "process_path", "wolfram-library-link-macros/automate-function-loading-boilerplate"]

experimental-kernel-expr = []


#=======================================
# Examples
Expand Down
56 changes: 56 additions & 0 deletions wolfram-library-link/RustLink/Tests/KernelExpr.wlt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Needs["MUnit`"]

Test[
(
LibraryFunctionLoad[
"liblibrary_tests", "test_kernel_expr_create_string", {}, "Void"
][];
Global`$ReturnValue
),
{1, "two", 3.5}
]

Test[
(
LibraryFunctionLoad[
"liblibrary_tests", "test_kernel_expr_create_symbols", {}, "Void"
][];
Global`$ReturnValue
),
{Global`Example1, Global`Example2, Example3`Example4}
]

Test[
(
LibraryFunctionLoad[
"liblibrary_tests", "test_kernel_expr_create_heterogenous", {}, "Void"
][];
Global`$ReturnValue
),
{1, 2.01, "three", Four, {"a", "b", "c"}}
]

Test[
(
LibraryFunctionLoad[
"liblibrary_tests", "test_kernel_expr_evaluate", {}, "Void"
][];
Global`$ReturnValue
),
4
]

(*====================================*)
(* Custom DownCode *)
(*====================================*)

Test[
(
LibraryFunctionLoad[
"liblibrary_tests", "test_kernel_expr_custom_downcode", {}, "Void"
][];

Global`CustomDownCode[]
),
"CUSTOM DOWNCODE"
]
2 changes: 2 additions & 0 deletions wolfram-library-link/examples/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ mod test_data_store;
mod test_images;
mod test_numeric_array_conversions;
mod test_wstp;

mod test_kernel_expr;
77 changes: 77 additions & 0 deletions wolfram-library-link/examples/tests/test_kernel_expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use wolfram_library_link::{
export,
kernel::{self, Expr, MIntExpr, NormalExpr, SymbolExpr, UncountedExpr},
};

#[export]
fn test_kernel_expr_create_string() {
let list = NormalExpr::list_from_array([
Expr::mint(1),
Expr::string("two"),
Expr::mreal(3.5),
]);

// $ReturnValue = list
SymbolExpr::lookup("Global`$ReturnValue").set_to(&list.as_expr());
}

#[export]
fn test_kernel_expr_create_symbols() {
let list = NormalExpr::list_from_array([
SymbolExpr::lookup("Example1").into(),
SymbolExpr::lookup("`Example2").into(),
SymbolExpr::lookup("Example3`Example4").into(),
]);

// $ReturnValue = list
SymbolExpr::lookup("Global`$ReturnValue").set_to(&list.as_expr());
}

#[export]
fn test_kernel_expr_create_heterogenous() {
let result = NormalExpr::list_from_array([
Expr::mint(1),
Expr::mreal(2.01),
Expr::string("three"),
Expr::symbol("Global`Four"),
Expr::list_from_array([Expr::string("a"), Expr::string("b"), Expr::string("c")]),
]);

// $ReturnValue = list
SymbolExpr::lookup("Global`$ReturnValue").set_to(&result.as_expr());
}

#[export]
fn test_kernel_expr_evaluate() {
// Evaluate Plus[2, 2]
let result = kernel::eval(
&NormalExpr::from_slice(&Expr::symbol("System`Plus"), &[
Expr::mint(2),
Expr::mint(2),
])
.into(),
);

let result = MIntExpr::try_from_expr(result)
.expect("expected result of evaluating 2 + 2 to be MIntExpr");

// $ReturnValue = list
SymbolExpr::lookup("Global`$ReturnValue").set_to(&result.as_expr());
}

//======================================
// Custom DownCode
//======================================

extern "C" fn eval_downcode(expr: UncountedExpr) -> Expr {
let _normal = NormalExpr::try_from_expr_ref(expr.as_expr()).unwrap();

Expr::string("CUSTOM DOWNCODE")
}

#[export]
fn test_kernel_expr_custom_downcode() {
let symbol = SymbolExpr::lookup("Global`CustomDownCode");

symbol.set_downcode(Some(eval_downcode))
}
Loading