diff --git a/implementations/duktape/.gitignore b/implementations/duktape/.gitignore index 7d20b7a..15e3353 100644 --- a/implementations/duktape/.gitignore +++ b/implementations/duktape/.gitignore @@ -4,3 +4,4 @@ app app.zip app-tiny prefix +/target/ diff --git a/implementations/duktape/Cargo.lock b/implementations/duktape/Cargo.lock new file mode 100644 index 0000000..f8e0e4b --- /dev/null +++ b/implementations/duktape/Cargo.lock @@ -0,0 +1,12 @@ +[root] +name = "nucleus" +version = "0.1.0" +dependencies = [ + "c-path 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "c-path" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + diff --git a/implementations/duktape/Cargo.toml b/implementations/duktape/Cargo.toml new file mode 100644 index 0000000..af7be49 --- /dev/null +++ b/implementations/duktape/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "nucleus" +version = "0.1.0" +authors = [ "Tim Caswell ", + "Jeremiah Senkpiel ", + "Steve Klabnik ", + "Ashley Williams " ] + +[dependencies] +c-path = "0.1.1" + +[lib] +crate-type = ["staticlib"] diff --git a/implementations/duktape/Makefile b/implementations/duktape/Makefile index ba63060..0f73f96 100644 --- a/implementations/duktape/Makefile +++ b/implementations/duktape/Makefile @@ -8,11 +8,11 @@ CC= cc -g # Uncomment the following to make a static musl binary on linux # CC= musl-gcc -Os -static -nucleus: main.o miniz.o duktape.o duv/duv.a ${LIBUV}/.libs/libuv.a env.o rust_path/target/release/libc_path.a - ${CC} $^ -lm -lpthread -o $@ rust_path/target/release/libc_path.a -ldl +nucleus: main.o miniz.o duktape.o duv/duv.a ${LIBUV}/.libs/libuv.a env.o target/release/libnucleus.a + ${CC} $^ -lm -lpthread -o $@ target/release/libnucleus.a -rust_path/target/release/libc_path.a: rust_path/src/lib.rs rust_path/src/helpers.rs - cd rust_path && cargo build --release +target/release/libnucleus.a: + cargo build --release install: nucleus install nucleus /usr/local/bin/ @@ -20,7 +20,7 @@ install: nucleus test: test-dir test-zip test-app test-app-tiny test-dir: nucleus - ./nucleus ../../test/manual -- 1 2 3 + ./nucleus ../../tests -- 1 2 3 test-zip: nucleus app.zip ./nucleus app.zip -- 4 5 6 @@ -42,14 +42,14 @@ app-tiny: app.zip prefix prefix: nucleus echo "#!$(shell pwd)/nucleus --" > prefix -app.zip: ../../test/manual/* ../../test/manual/deps/* +app.zip: ../../tests/* ../../tests/deps/* rm -f app.zip - cd ../../test/manual; zip -9 -r ../../implementations/duktape/app.zip .; cd - + cd ../../tests; zip -9 -r ../implementations/duktape/app.zip .; cd - env.o: env.c env.h ${CC} -std=gnu99 -Wall -Wextra -pedantic -Werror -c $< -o $@ -main.o: main.c *.h +main.o: main.c ${CC} -std=gnu99 -Wall -Wextra -pedantic -Werror -c $< -o $@ @@ -72,7 +72,7 @@ miniz.o: ../../deps/miniz.c ${CC} -std=gnu99 -c $< -o $@ clean: - rm -rf nucleus *.o app.zip app prefix app-tiny rust_path/target + rm -f nucleus *.o app.zip app prefix app-tiny ${MAKE} -C duv clean distclean: clean diff --git a/implementations/duktape/rust_path/.gitignore b/implementations/duktape/rust_path/.gitignore deleted file mode 100644 index a9d37c5..0000000 --- a/implementations/duktape/rust_path/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -target -Cargo.lock diff --git a/implementations/duktape/rust_path/Cargo.toml b/implementations/duktape/rust_path/Cargo.toml deleted file mode 100644 index 4ca0384..0000000 --- a/implementations/duktape/rust_path/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "c-path" -version = "0.1.0" -authors = ["Ashley Williams "] - -[dependencies] - -[lib] -crate-type = ["staticlib"] diff --git a/implementations/duktape/rust_path/src/helpers.rs b/implementations/duktape/rust_path/src/helpers.rs deleted file mode 100644 index 128e8cf..0000000 --- a/implementations/duktape/rust_path/src/helpers.rs +++ /dev/null @@ -1,24 +0,0 @@ -use std::ffi::CStr; -use std::os::raw::c_char; -use std::ffi::OsString; -use std::ptr; -use std::panic; - -pub fn build_str_from_c(c_path_string: *const c_char) -> String { - unsafe { CStr::from_ptr(c_path_string).to_string_lossy().into_owned() } -} - -pub fn path_to_str(path: OsString) -> String { - path.to_string_lossy().into_owned() -} - -pub fn protect_against_panic(code: F) -> *const c_char - where F: Fn() -> *const c_char + panic::UnwindSafe -{ - let result = panic::catch_unwind(code); - - match result { - Ok(value) => value, - Err(_) => ptr::null(), - } -} diff --git a/implementations/duktape/rust_path/src/lib.rs b/implementations/duktape/rust_path/src/lib.rs deleted file mode 100644 index e74a38c..0000000 --- a/implementations/duktape/rust_path/src/lib.rs +++ /dev/null @@ -1,54 +0,0 @@ -use std::ffi::CString; -use std::os::raw::c_char; -use std::path::Path; - -mod helpers; - -#[no_mangle] -pub extern fn rust_join(c_path_string1: *const c_char, - c_path_string2: *const c_char) - -> *const c_char { - helpers::protect_against_panic(|| { - let path_string1 = helpers::build_str_from_c(c_path_string1); - let path_string2 = helpers::build_str_from_c(c_path_string2); - let path1 = Path::new(&path_string1); - let joined_path = path1.join(path_string2); - - let joined_string = helpers::path_to_str(joined_path.into_os_string()); - let c_joined = CString::new(joined_string).unwrap(); - c_joined.into_raw() - }) -} - -#[no_mangle] -pub extern fn rust_dirname(c_path_string: *const c_char) -> *const c_char { - helpers::protect_against_panic(|| { - let path_string = helpers::build_str_from_c(c_path_string); - let path = Path::new(&path_string); - let dirname_string = - helpers::path_to_str(path.parent().unwrap().to_owned().into_os_string()); - let c_dirname = CString::new(dirname_string).unwrap(); - c_dirname.into_raw() - }) -} - -#[no_mangle] -pub extern fn rust_extension(c_path_string: *const c_char) -> *const c_char { - helpers::protect_against_panic(|| { - let path_string = helpers::build_str_from_c(c_path_string); - let path = Path::new(&path_string); - let ext = path.extension().unwrap(); - CString::new(helpers::path_to_str(ext.to_owned())).unwrap().into_raw() - }) -} - -#[no_mangle] -pub extern fn rust_filename(c_path_string: *const c_char) -> *const c_char { - helpers::protect_against_panic(|| { - let path_string = helpers::build_str_from_c(c_path_string); - let path = Path::new(&path_string); - let filename_string = helpers::path_to_str(path.file_name().unwrap().to_owned()); - let c_filename = CString::new(filename_string).unwrap(); - c_filename.into_raw() - }) -} diff --git a/implementations/duktape/src/lib.rs b/implementations/duktape/src/lib.rs new file mode 100644 index 0000000..3ef29a9 --- /dev/null +++ b/implementations/duktape/src/lib.rs @@ -0,0 +1 @@ +extern crate c_path;