From 7684bd41b2a1ba2f0a3419bba789184f048b5ce1 Mon Sep 17 00:00:00 2001 From: Raymond Date: Thu, 13 Apr 2023 10:02:56 +0330 Subject: [PATCH] Improve rs_port stage 3 (#421) --- .../rs_port/.devcontainer/devcontainer.json | 13 ----- source/ports/rs_port/Dockerfile | 1 + source/ports/rs_port/README.md | 2 +- source/ports/rs_port/build.rs | 54 +++++++------------ source/ports/rs_port/inline/src/lib.rs | 21 -------- source/ports/rs_port/src/lib.rs | 3 +- source/ports/rs_port/src/loaders.rs | 5 +- source/ports/rs_port/tests/inline_test.rs | 22 ++++---- .../rs_port/tests/invalid_loader_test.rs | 31 ++++++++--- source/ports/rs_port/tests/loaders_test.rs | 6 +-- .../ports/rs_port/tests/return_type_test.rs | 48 +++++++++-------- 11 files changed, 88 insertions(+), 118 deletions(-) diff --git a/source/ports/rs_port/.devcontainer/devcontainer.json b/source/ports/rs_port/.devcontainer/devcontainer.json index d78d1042b..7745f883a 100644 --- a/source/ports/rs_port/.devcontainer/devcontainer.json +++ b/source/ports/rs_port/.devcontainer/devcontainer.json @@ -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" } diff --git a/source/ports/rs_port/Dockerfile b/source/ports/rs_port/Dockerfile index d783261b9..7b1485629 100644 --- a/source/ports/rs_port/Dockerfile +++ b/source/ports/rs_port/Dockerfile @@ -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 \ diff --git a/source/ports/rs_port/README.md b/source/ports/rs_port/README.md index 3150bdb50..b51fce5a5 100644 --- a/source/ports/rs_port/README.md +++ b/source/ports/rs_port/README.md @@ -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(); diff --git a/source/ports/rs_port/build.rs b/source/ports/rs_port/build.rs index fbc7e8b12..e516cf361 100644 --- a/source/ports/rs_port/build.rs +++ b/source/ports/rs_port/build.rs @@ -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()); @@ -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] = [ @@ -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 @@ -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") - }, + } } } } diff --git a/source/ports/rs_port/inline/src/lib.rs b/source/ports/rs_port/inline/src/lib.rs index a7480f376..2e9fd1a5e 100644 --- a/source/ports/rs_port/inline/src/lib.rs +++ b/source/ports/rs_port/inline/src/lib.rs @@ -1,8 +1,3 @@ -use std::{ - env, - path::{PathBuf}, -}; - use proc_macro::TokenStream; use quote::quote; @@ -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() -} diff --git a/source/ports/rs_port/src/lib.rs b/source/ports/rs_port/src/lib.rs index 72053716e..af405766e 100644 --- a/source/ports/rs_port/src/lib.rs +++ b/source/ports/rs_port/src/lib.rs @@ -34,4 +34,5 @@ pub mod inline { pub use metacall_inline::*; } -metacall_inline::include_bindings!(); +#[allow(warnings)] +pub mod bindings; diff --git a/source/ports/rs_port/src/loaders.rs b/source/ports/rs_port/src/loaders.rs index b2a9671d4..8ac9db490 100644 --- a/source/ports/rs_port/src/loaders.rs +++ b/source/ports/rs_port/src/loaders.rs @@ -9,7 +9,10 @@ use std::{ ptr, }; -pub fn from_file( +pub fn from_file(tag: impl ToString, script: impl AsRef) -> Result<(), MetacallLoaderError> { + from_files(tag, [script]) +} +pub fn from_files( tag: impl ToString, scripts: impl IntoIterator>, ) -> Result<(), MetacallLoaderError> { diff --git a/source/ports/rs_port/tests/inline_test.rs b/source/ports/rs_port/tests/inline_test.rs index 015885039..93d543492 100644 --- a/source/ports/rs_port/tests/inline_test.rs +++ b/source/ports/rs_port/tests/inline_test.rs @@ -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"); + } } diff --git a/source/ports/rs_port/tests/invalid_loader_test.rs b/source/ports/rs_port/tests/invalid_loader_test.rs index dd9883e54..a9b990ed5 100644 --- a/source/ports/rs_port/tests/invalid_loader_test.rs +++ b/source/ports/rs_port/tests/invalid_loader_test.rs @@ -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!"); + } } diff --git a/source/ports/rs_port/tests/loaders_test.rs b/source/ports/rs_port/tests/loaders_test.rs index 51245c0f8..3b7953596 100644 --- a/source/ports/rs_port/tests/loaders_test.rs +++ b/source/ports/rs_port/tests/loaders_test.rs @@ -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 };"; @@ -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); @@ -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(); } diff --git a/source/ports/rs_port/tests/return_type_test.rs b/source/ports/rs_port/tests/return_type_test.rs index 9f2d753f8..1711eda3b 100644 --- a/source/ports/rs_port/tests/return_type_test.rs +++ b/source/ports/rs_port/tests/return_type_test.rs @@ -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(); + } }