diff --git a/Cargo.lock b/Cargo.lock index f7c85551..357743e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,18 +17,6 @@ version = "1.0.51" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b26702f315f53b6071259e15dd9d64528213b44d61de1ec926eca7715d62203" -[[package]] -name = "arrayref" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" - -[[package]] -name = "arrayvec" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" - [[package]] name = "atty" version = "0.2.14" @@ -40,47 +28,12 @@ dependencies = [ "winapi", ] -[[package]] -name = "base64" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" - [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" -[[package]] -name = "blake2b_simd" -version = "0.5.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afa748e348ad3be8263be728124b24a24f268266f6f5d58af9d75f6a40b5c587" -dependencies = [ - "arrayref", - "arrayvec", - "constant_time_eq", -] - -[[package]] -name = "cc" -version = "1.0.72" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" - -[[package]] -name = "cfg-if" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - [[package]] name = "clap" version = "2.34.0" @@ -107,88 +60,12 @@ dependencies = [ "winapi", ] -[[package]] -name = "constant_time_eq" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" - -[[package]] -name = "crossbeam-utils" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d82cfc11ce7f2c3faef78d8a684447b40d503d9681acebed6cb728d45940c4db" -dependencies = [ - "cfg-if 1.0.0", - "lazy_static", -] - -[[package]] -name = "dirs" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901" -dependencies = [ - "libc", - "redox_users 0.3.5", - "winapi", -] - -[[package]] -name = "dirs" -version = "2.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3" -dependencies = [ - "cfg-if 0.1.10", - "dirs-sys", -] - -[[package]] -name = "dirs-sys" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780" -dependencies = [ - "libc", - "redox_users 0.4.0", - "winapi", -] - [[package]] name = "downcast-rs" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" -[[package]] -name = "fnv" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" - -[[package]] -name = "getrandom" -version = "0.1.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" -dependencies = [ - "cfg-if 1.0.0", - "libc", - "wasi 0.9.0+wasi-snapshot-preview1", -] - -[[package]] -name = "getrandom" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" -dependencies = [ - "cfg-if 1.0.0", - "libc", - "wasi 0.10.2+wasi-snapshot-preview1", -] - [[package]] name = "heck" version = "0.3.3" @@ -216,9 +93,7 @@ dependencies = [ "downcast-rs", "lazy_static", "libc", - "libloading", - "linefeed", - "nom 7.1.0", + "nom", "structopt", ] @@ -234,27 +109,6 @@ version = "0.2.108" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119" -[[package]] -name = "libloading" -version = "0.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afe203d669ec979b7128619bae5a63b7b42e9203c1b29146079ee05e2f604b52" -dependencies = [ - "cfg-if 1.0.0", - "winapi", -] - -[[package]] -name = "linefeed" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28715d08e35c6c074f9ae6b2e6a2420bac75d050c66ecd669d7d5b98e2caa036" -dependencies = [ - "dirs 1.0.5", - "mortal", - "winapi", -] - [[package]] name = "memchr" version = "2.4.1" @@ -267,45 +121,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" -[[package]] -name = "mortal" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "998fd6a991497275567703b6f435e27958b633878ec991f5734b96dd46675e9f" -dependencies = [ - "bitflags", - "libc", - "nix", - "smallstr", - "terminfo", - "unicode-normalization", - "unicode-width", - "winapi", -] - -[[package]] -name = "nix" -version = "0.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50e4785f2c3b7589a0d0c1dd60285e1188adac4006e8abd6dd578e1567027363" -dependencies = [ - "bitflags", - "cc", - "cfg-if 0.1.10", - "libc", - "void", -] - -[[package]] -name = "nom" -version = "5.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af" -dependencies = [ - "memchr", - "version_check", -] - [[package]] name = "nom" version = "7.1.0" @@ -317,50 +132,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "phf" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12" -dependencies = [ - "phf_shared", -] - -[[package]] -name = "phf_codegen" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbffee61585b0411840d3ece935cce9cb6321f01c45477d30066498cd5e1a815" -dependencies = [ - "phf_generator", - "phf_shared", -] - -[[package]] -name = "phf_generator" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17367f0cc86f2d25802b2c26ee58a7b23faeccf78a396094c13dced0d0182526" -dependencies = [ - "phf_shared", - "rand", -] - -[[package]] -name = "phf_shared" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c00cf8b9eafe68dde5e9eaa2cef8ee84a9336a47d566ec55ca16589633b65af7" -dependencies = [ - "siphasher", -] - -[[package]] -name = "ppv-lite86" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba" - [[package]] name = "proc-macro-error" version = "1.0.4" @@ -403,126 +174,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rand" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" -dependencies = [ - "getrandom 0.1.16", - "libc", - "rand_chacha", - "rand_core", - "rand_hc", - "rand_pcg", -] - -[[package]] -name = "rand_chacha" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" -dependencies = [ - "ppv-lite86", - "rand_core", -] - -[[package]] -name = "rand_core" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" -dependencies = [ - "getrandom 0.1.16", -] - -[[package]] -name = "rand_hc" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" -dependencies = [ - "rand_core", -] - -[[package]] -name = "rand_pcg" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429" -dependencies = [ - "rand_core", -] - -[[package]] -name = "redox_syscall" -version = "0.1.57" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" - -[[package]] -name = "redox_syscall" -version = "0.2.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" -dependencies = [ - "bitflags", -] - -[[package]] -name = "redox_users" -version = "0.3.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d" -dependencies = [ - "getrandom 0.1.16", - "redox_syscall 0.1.57", - "rust-argon2", -] - -[[package]] -name = "redox_users" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64" -dependencies = [ - "getrandom 0.2.3", - "redox_syscall 0.2.10", -] - -[[package]] -name = "rust-argon2" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b18820d944b33caa75a71378964ac46f58517c92b6ae5f762636247c09e78fb" -dependencies = [ - "base64", - "blake2b_simd", - "constant_time_eq", - "crossbeam-utils", -] - -[[package]] -name = "siphasher" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "533494a8f9b724d33625ab53c6c4800f7cc445895924a8ef649222dcb76e938b" - -[[package]] -name = "smallstr" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e922794d168678729ffc7e07182721a14219c65814e66e91b839a272fe5ae4f" -dependencies = [ - "smallvec", -] - -[[package]] -name = "smallvec" -version = "1.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ecab6c735a6bb4139c0caafd0cc3635748bbb3acf4550e8138122099251f309" - [[package]] name = "strsim" version = "0.8.0" @@ -564,19 +215,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "terminfo" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76971977e6121664ec1b960d1313aacfa75642adc93b9d4d53b247bd4cb1747e" -dependencies = [ - "dirs 2.0.2", - "fnv", - "nom 5.1.2", - "phf", - "phf_codegen", -] - [[package]] name = "textwrap" version = "0.11.0" @@ -586,30 +224,6 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "tinyvec" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c1c1d5a42b6245520c249549ec267180beaffcc0615401ac8e31853d4b6d8d2" -dependencies = [ - "tinyvec_macros", -] - -[[package]] -name = "tinyvec_macros" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" - -[[package]] -name = "unicode-normalization" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d54590932941a9e9266f0832deed84ebe1bf2e4c9e4a3554d393d18f5e854bf9" -dependencies = [ - "tinyvec", -] - [[package]] name = "unicode-segmentation" version = "1.8.0" @@ -640,24 +254,6 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" -[[package]] -name = "void" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" - -[[package]] -name = "wasi" -version = "0.9.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" - -[[package]] -name = "wasi" -version = "0.10.2+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" - [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index bf8cd038..7cd97fe4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,16 +10,22 @@ repository = "https://github.com/cohenarthur/jinko" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + [dependencies] structopt = "0.3" colored = "2.0" nom = "7.0" -linefeed = "0.6" +linefeed = { version = "0.6", optional = true } downcast-rs = "1.2" anyhow = "1.0" -libloading = "0.7" +libloading = { version = "0.7", optional = true } lazy_static = "1.4" +[features] +default = ["repl", "ffi"] +repl = ["linefeed"] +ffi = ["libloading"] + [dev-dependencies] libc = "0.2" diff --git a/interpreter/jinko.rs b/interpreter/jinko.rs index 2c497ab0..fe34fe73 100644 --- a/interpreter/jinko.rs +++ b/interpreter/jinko.rs @@ -1,6 +1,7 @@ // FIXME: Add #![warn(missing_docs)] mod args; +#[cfg(feature = "repl")] mod repl; use colored::Colorize; @@ -10,6 +11,7 @@ use jinko::{ }; use args::Args; +#[cfg(feature = "repl")] use repl::Repl; use std::{fs, path::Path}; @@ -105,7 +107,10 @@ fn handle_input(args: &Args, file: &Path) -> InteractResult { match args.test() { false => match args.interactive() { + #[cfg(feature = "repl")] true => Repl::new()?.with_context(ctx).launch(), + #[cfg(not(feature = "repl"))] + true => panic!("Jinko is not compiled with repl support"), false => { let res = ctx.execute()?; ctx.emit_errors(); @@ -132,10 +137,17 @@ fn main() -> anyhow::Result<()> { jinko::log::enable(); } + #[cfg(feature = "repl")] let result = args.input().map_or_else( || Repl::new()?.launch(), |filename| handle_input(&args, filename), )?; + #[cfg(not(feature = "repl"))] + let result = args + .input() + .map(|filename| handle_input(&args, filename)) + .unwrap()?; + handle_exit_code(result.0) } diff --git a/src/builtins.rs b/src/builtins.rs index 66e8c60d..e1781947 100644 --- a/src/builtins.rs +++ b/src/builtins.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use std::path::PathBuf; +#[cfg(feature = "ffi")] use crate::ffi; use crate::instance::{FromObjectInstance, ToObjectInstance}; use crate::{Context, Instruction, JkBool, JkInt, JkString, ObjectInstance}; @@ -72,6 +73,7 @@ fn string_equals(ctx: &mut Context, args: Args) -> Option { } /// Link with a given library at runtime +#[cfg(feature = "ffi")] fn ffi_link_with(ctx: &mut Context, args: Args) -> Option { let lib_path = JkString::from_instance(&args[0].execute(ctx).unwrap()).0; @@ -131,6 +133,7 @@ impl Builtins { builtins.add("__builtin_string_display_err", string_display_err); builtins.add("__builtin_string_is_empty", string_is_empty); builtins.add("__builtin_string_equals", string_equals); + #[cfg(feature = "ffi")] builtins.add("__builtin_ffi_link_with", ffi_link_with); builtins.add("__builtin_arg_get", arg_get); builtins.add("__builtin_arg_amount", arg_amount); @@ -156,7 +159,7 @@ impl Default for Builtins { #[cfg(test)] mod tests { - use crate::jinko; + use crate::{jinko, jinko_fail}; #[test] fn t_string_builtins_are_valid() { @@ -171,12 +174,21 @@ mod tests { } #[test] + #[cfg(feature = "ffi")] fn t_ffi_builtins_are_valid() { jinko! { __builtin_ffi_link_with("tests/fixtures/clib/lib.so"); }; } + #[test] + #[cfg(not(feature = "ffi"))] + fn t_ffi_builtins_are_valid_no_ffi() { + jinko_fail! { + __builtin_ffi_link_with("tests/fixtures/clib/lib.so"); + }; + } + #[test] fn t_args_builtins_are_valid() { jinko! { diff --git a/src/context.rs b/src/context.rs index bd44766d..85494e54 100644 --- a/src/context.rs +++ b/src/context.rs @@ -58,6 +58,7 @@ pub struct Context { included: HashSet, /// External libraries to use via FFI + #[cfg(feature = "ffi")] external_libs: Vec, /// Errors being kept by the context @@ -91,6 +92,7 @@ impl Context { builtins: Builtins::new(), tests: HashMap::new(), included: HashSet::new(), + #[cfg(feature = "ffi")] external_libs: Vec::new(), error_handler: ErrorHandler::default(), }; @@ -324,11 +326,13 @@ impl Context { } /// Add a library to the interpreter + #[cfg(feature = "ffi")] pub fn add_lib(&mut self, lib: libloading::Library) { self.external_libs.push(lib) } /// Get a reference on all shared libraries loaded in the interpreter + #[cfg(feature = "ffi")] pub fn libs(&self) -> &Vec { &self.external_libs } diff --git a/src/error/mod.rs b/src/error/mod.rs index 3668124f..dd6d26dc 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -193,6 +193,7 @@ impl Display for Error { } } +#[cfg(feature = "ffi")] impl std::convert::From for Error { fn from(e: libloading::Error) -> Self { Error::new(ErrKind::ExternFunc).with_msg(e.to_string()) diff --git a/src/instruction/function_call.rs b/src/instruction/function_call.rs index 44b0e947..250c00c0 100644 --- a/src/instruction/function_call.rs +++ b/src/instruction/function_call.rs @@ -133,6 +133,7 @@ impl FunctionCall { } } } else { + #[cfg(feature = "ffi")] match crate::ffi::execute(dec, self, ctx) { Ok(value) => value, Err(e) => { @@ -140,6 +141,15 @@ impl FunctionCall { None } } + + #[cfg(not(feature = "ffi"))] + { + ctx.error(Error::new(ErrKind::Context).with_msg(format!( + "Jinko is not compiled with FFI support. Cannot call `{}` external function", + dec.name() + ))); + None + } } } } diff --git a/src/lib.rs b/src/lib.rs index 3cdeb85b..05a33091 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ pub mod builtins; mod context; mod error; +#[cfg(feature = "ffi")] mod ffi; mod indent; pub mod instance;