Skip to content

Commit

Permalink
Improve rs_port stage 3 (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
devraymondsh authored Apr 13, 2023
1 parent a87d68a commit 7684bd4
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 118 deletions.
13 changes: 0 additions & 13 deletions source/ports/rs_port/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-dockerfile
{
"name": "Existing Dockerfile",
"build": {
// Sets the run context to one level up instead of the .devcontainer folder.
"context": "..",
// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
"dockerfile": "../Dockerfile"
},
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Uncomment the next line to run commands after the container is created.
// "postCreateCommand": "cat /etc/os-release",
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root.
"remoteUser": "root"
}
1 change: 1 addition & 0 deletions source/ports/rs_port/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ RUN cd build \
-DOPTION_BUILD_LOADERS_C=On \
-DOPTION_BUILD_LOADERS_NODE=On \
-DOPTION_BUILD_LOADERS_PY=On \
-DOPTION_BUILD_LOADERS_TS=On \
-DOPTION_BUILD_SCRIPTS=Off \
-DOPTION_BUILD_SERIALS_RAPID_JSON=On \
-DOPTION_BUILD_TESTS=Off \
Expand Down
2 changes: 1 addition & 1 deletion source/ports/rs_port/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn main() {
// Metacall automatically shuts down when it goes out of scope
let _ = hooks::initialize().unwrap();

loaders::from_file("ts", ["sum.ts"]).unwrap();
loaders::from_file("ts", "sum.ts").unwrap();

let sum = metacall("sum", [Any::Double(1.0), Any::Double(2.0)]).unwrap();

Expand Down
54 changes: 18 additions & 36 deletions source/ports/rs_port/build.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
use bindgen::builder;
use std::{
env, fs,
path::{PathBuf},
};
use bindgen::{builder, CargoCallbacks};
use std::env;

use bindgen::CargoCallbacks;

fn get_bindings_dir() -> PathBuf {
let out_dir = PathBuf::from(env::current_dir().unwrap());
let bindings_dir = out_dir.join("target").join("bindings");

fs::create_dir_all(&bindings_dir).unwrap();

bindings_dir.canonicalize().unwrap()
}

fn generate_bindings(bindings_dir: &PathBuf, headers: &[&str]) {
fn generate_bindings(headers: &[&str]) {
let mut builder = builder();

builder = builder.clang_arg(format!("-I{}", env::current_dir().unwrap().join("include").to_str().unwrap()));
builder = builder.clang_arg(format!(
"-I{}",
env::current_dir()
.unwrap()
.join("include")
.to_str()
.unwrap()
));

for header in headers {
builder = builder.header(header.to_string());
Expand All @@ -35,13 +28,11 @@ fn generate_bindings(bindings_dir: &PathBuf, headers: &[&str]) {
let bindings = builder.generate().unwrap();

bindings
.write_to_file(bindings_dir.join("bindings.rs"))
.write_to_file(env::current_dir().unwrap().join("src/bindings.rs"))
.unwrap();
}

fn main() {
let bindings_dir = get_bindings_dir();

// When running from CMake
if let Ok(_) = env::var("CMAKE_BINDGEN") {
const HEADERS: [&str; 3] = [
Expand All @@ -50,20 +41,11 @@ fn main() {
"include/metacall/metacall_error.h",
];

generate_bindings(&bindings_dir, &HEADERS);

for header in HEADERS {
println!(
"{}",
format!(
"cargo:rerun-if-changed={}/{}",
bindings_dir.to_str().unwrap(),
header
)
);
}
generate_bindings(&HEADERS);
}

println!("cargo:rerun-if-changed=src/bindings.rs");

// Compile time assert for validating the minimum METACALL_VERSION
// TODO

Expand Down Expand Up @@ -98,13 +80,13 @@ fn main() {
match profile.as_str() {
"debug" => {
println!("cargo:rustc-link-lib=metacalld")
},
}
"release" => {
println!("cargo:rustc-link-lib=metacall")
},
}
_ => {
println!("cargo:rustc-link-lib=metacall")
},
}
}
}
}
21 changes: 0 additions & 21 deletions source/ports/rs_port/inline/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
use std::{
env,
path::{PathBuf},
};

use proc_macro::TokenStream;
use quote::quote;

Expand All @@ -25,19 +20,3 @@ macro_rules! gen_inline_macro {
}

gen_inline_macro!(py, node, ts, cs, rb, cob, rpc, java, wasm);


#[proc_macro]
pub fn include_bindings(_input: TokenStream) -> TokenStream {
let out_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let path_dir = out_dir.join("src").join("bindings.rs");
let path = path_dir.to_str();

let result = quote! {
#[path = #path]
#[allow(warnings)]
pub mod bindings;
};

result.into()
}
3 changes: 2 additions & 1 deletion source/ports/rs_port/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ pub mod inline {
pub use metacall_inline::*;
}

metacall_inline::include_bindings!();
#[allow(warnings)]
pub mod bindings;
5 changes: 4 additions & 1 deletion source/ports/rs_port/src/loaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use std::{
ptr,
};

pub fn from_file(
pub fn from_file(tag: impl ToString, script: impl AsRef<Path>) -> Result<(), MetacallLoaderError> {
from_files(tag, [script])
}
pub fn from_files(
tag: impl ToString,
scripts: impl IntoIterator<Item = impl AsRef<Path>>,
) -> Result<(), MetacallLoaderError> {
Expand Down
22 changes: 10 additions & 12 deletions source/ports/rs_port/tests/inline_test.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
use metacall::{
hooks,
inline::{node, ts},
inline::{node, py, ts},
};

#[test]
fn test_inline() {
let _d = hooks::initialize().unwrap();

// TODO
py! {
print("hello world")
}

// py! {
// print("hello world")
// }
node! {
console.log("hello world");
}

// node! {
// console.log("hello world");
// }

// ts! {
// console.log("hello world");
// }
ts! {
console.log("hello world");
}
}
31 changes: 24 additions & 7 deletions source/ports/rs_port/tests/invalid_loader_test.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
use metacall::{
hooks, loaders,
};
use std::{env};
use metacall::{hooks, loaders, prelude::MetacallLoaderError};
use std::env;

#[test]
fn invalid_loader_test() {
let _d = hooks::initialize().unwrap();

let tests_dir = env::current_dir().unwrap().join("tests/scripts");
let test_file = tests_dir.join("whatever.yeet");
let scripts_dir = env::current_dir().unwrap().join("tests/scripts");
let inavlid_file = scripts_dir.join("whatever.yeet");
let valid_file = scripts_dir.join("return_type_test.js");

loaders::from_file("random", [test_file]).unwrap();
if let Err(MetacallLoaderError::FileNotFound(_)) = loaders::from_file("random", inavlid_file) {
// Everything Ok
} else {
panic!("Expected the loader fail with `FileNotFound` error variant!");
}

if let Err(MetacallLoaderError::FromFileFailure) = loaders::from_file("random", valid_file) {
// Everything Ok
} else {
panic!("Expected the loader fail with `FromFileFailure` error variant!");
}

if let Err(MetacallLoaderError::FromMemoryFailure) =
loaders::from_memory("random", "Invalid code!")
{
// Everything Ok
} else {
panic!("Expected the loader fail with `FromMemoryFailure` error variant!");
}
}
6 changes: 3 additions & 3 deletions source/ports/rs_port/tests/loaders_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
path::PathBuf,
};

// Two different names to avoid conflicts when testing both load_from_memory and load_from_file
// Two different names to avoid conflicts when testing both load_from_memory and load_from_files
// in a single test.
const SCRIPT1: &str = "function greet1() { return 'hi there!' } \nmodule.exports = { greet1 };";
const SCRIPT2: &str = "function greet2() { return 'hi there!' } \nmodule.exports = { greet2 };";
Expand Down Expand Up @@ -40,7 +40,7 @@ fn load_from_file_test() {
temp_js.write_all(SCRIPT2.as_bytes()).unwrap();
temp_js.flush().unwrap();

loaders::from_file("node", [temp_js_path]).unwrap();
loaders::from_file("node", temp_js_path).unwrap();

call_greet("load_from_file", 2);

Expand All @@ -55,6 +55,6 @@ fn loaders_test() {
// Testing load_from_memory
load_from_memory_test();

// Testing load_from_file
// Testing load_from_files
load_from_file_test();
}
48 changes: 25 additions & 23 deletions source/ports/rs_port/tests/return_type_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,27 +309,29 @@ fn return_type_test() {
let c_test_file = tests_dir.join("return_type_test.c");
let py_test_file = tests_dir.join("return_type_test.py");

loaders::from_file("node", [js_test_file]).unwrap();
loaders::from_file("c", [c_test_file]).unwrap();
loaders::from_file("py", [py_test_file]).unwrap();

test_bool();
test_char();
test_short();
test_int();
test_long();
test_float();
test_double();
test_string();
test_buffer();
test_array();
test_map();
test_pointer();
// test_function();
test_null();
test_class();
test_object();
test_exception();
test_throwable();
test_future();
if let Ok(_) = loaders::from_file("c", c_test_file) {
test_char();
test_short();
test_int();
test_long();
test_float();
test_double();
}
if let Ok(_) = loaders::from_file("py", py_test_file) {
test_class();
test_object();
test_buffer();
test_pointer();
}
if let Ok(_) = loaders::from_file("node", js_test_file) {
test_bool();
test_string();
test_array();
test_map();
// test_function();
test_null();
test_exception();
test_throwable();
test_future();
}
}

0 comments on commit 7684bd4

Please sign in to comment.