From 04329df17f4be61d2d0d43a8e3ad6d7a4d0bcec9 Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Fri, 15 Dec 2023 07:42:54 +0300 Subject: [PATCH 01/32] First jruby ver, without any rust methods --- .gitignore | 4 + Cargo.lock | 300 ++++++++++++++++++++++++++++++- Rakefile | 34 +++- ext/Cargo.toml | 16 +- ext/build.rs | 6 +- ext/extconf.rb | 6 +- ext/oxi/test/OxiTest.java | 20 +++ ext/oxi/test/OxiTestService.java | 18 ++ ext/src/lib.rs | 7 + lib/oxi/test.rb | 3 + lib/oxi/test/jruby.rb | 15 ++ oxi-test.gemspec | 6 +- rake_ext.rb | 33 ++++ 13 files changed, 447 insertions(+), 21 deletions(-) create mode 100644 ext/oxi/test/OxiTest.java create mode 100644 ext/oxi/test/OxiTestService.java create mode 100644 lib/oxi/test/jruby.rb create mode 100644 rake_ext.rb diff --git a/.gitignore b/.gitignore index 766c291..29d2564 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,7 @@ mkmf.log target/ Gemfile.lock +*.jar +.rake_tasks~ +.idea/ +*.iml diff --git a/Cargo.lock b/Cargo.lock index 9c0a4ae..6b8690e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,16 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "Inflector" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" +dependencies = [ + "lazy_static", + "regex", +] + [[package]] name = "aho-corasick" version = "1.1.1" @@ -28,7 +38,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn", + "syn 2.0.37", ] [[package]] @@ -37,6 +47,18 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" +[[package]] +name = "bytes" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" + +[[package]] +name = "cesu8" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" + [[package]] name = "cexpr" version = "0.6.0" @@ -63,12 +85,100 @@ dependencies = [ "libloading", ] +[[package]] +name = "combine" +version = "4.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35ed6e9d84f0b51a7f52daf1c7d71dd136fd7a3f41a8462b8cdb8c78d920fad4" +dependencies = [ + "bytes", + "memchr", +] + +[[package]] +name = "darling" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 1.0.109", +] + +[[package]] +name = "darling_macro" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e" +dependencies = [ + "darling_core", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "getrandom" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "glob" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "jni" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6df18c2e3db7e453d3c6ac5b3e9d5182664d28788126d39b91f2d1e22b017ec" +dependencies = [ + "cesu8", + "combine", + "jni-sys", + "log", + "thiserror", + "walkdir", +] + +[[package]] +name = "jni-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" + [[package]] name = "lazy_static" version = "1.4.0" @@ -83,9 +193,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] name = "libc" -version = "0.2.148" +version = "0.2.151" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b" +checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" [[package]] name = "libloading" @@ -97,6 +207,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "log" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" + [[package]] name = "memchr" version = "2.6.3" @@ -125,14 +241,50 @@ version = "0.1.0" dependencies = [ "rb-sys", "rb-sys-env", + "robusta_jni", ] +[[package]] +name = "paste" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" + [[package]] name = "peeking_take_while" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.67" @@ -151,6 +303,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "rb-sys" version = "0.9.82" @@ -172,7 +354,7 @@ dependencies = [ "quote", "regex", "shell-words", - "syn", + "syn 2.0.37", ] [[package]] @@ -210,12 +392,48 @@ version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" +[[package]] +name = "robusta-codegen" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffb512b451472948a204452dfad582bdc48d69caacdd3b1b4571d5e3f11707f3" +dependencies = [ + "Inflector", + "darling", + "proc-macro-error", + "proc-macro2", + "quote", + "rand", + "syn 1.0.109", +] + +[[package]] +name = "robusta_jni" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c080146e0cc733697fe500413871142af91bd879641205c2febbe5f982f304e3" +dependencies = [ + "jni", + "paste", + "robusta-codegen", + "static_assertions", +] + [[package]] name = "rustc-hash" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "shell-words" version = "1.1.0" @@ -228,6 +446,29 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7cee0529a6d40f580e7a5e6c495c8fbfe21b7b52795ed4bb5e62cdf92bc6380" +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.37" @@ -239,12 +480,54 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.37", +] + [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "walkdir" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "winapi" version = "0.3.9" @@ -261,6 +544,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Rakefile b/Rakefile index e2108c3..082c4d0 100644 --- a/Rakefile +++ b/Rakefile @@ -2,12 +2,38 @@ require "bundler/gem_tasks" require "rake/testtask" -require "rb_sys/extensiontask" GEMSPEC = Gem::Specification.load("oxi-test.gemspec") || abort("Could not load oxi-test.gemspec") -RbSys::ExtensionTask.new("oxi-test", GEMSPEC) do |ext| - ext.lib_dir = "lib/oxi/test" +java_p = RUBY_PLATFORM.include?("java") + +if java_p + require_relative "rake_ext" + require "rake/javaextensiontask" + + # Note: not "oxi-test" + Rake::JavaExtensionTask.new("oxi_test", GEMSPEC) do |ext| + ext.lib_dir = "lib/oxi/test" + ext.ext_dir = "ext" + ext.source_version = "11" # or "8" + ext.target_version = "11" # or "8" + end + + Rake::Task[:compile].rename(:java_compile) + + require "rb_sys/extensiontask" + + RbSys::ExtensionTask.new("oxi-test", GEMSPEC) do |ext| + ext.lib_dir = "lib/oxi/test" + end + + task compile: :java_compile +else + require "rb_sys/extensiontask" + + RbSys::ExtensionTask.new("oxi-test", GEMSPEC) do |ext| + ext.lib_dir = "lib/oxi/test" + end end task :fmt do @@ -26,7 +52,7 @@ task :native, [:platform] do |_t, platform:| end task :cargo_test do - sh "cargo test" + sh "cargo test --features #{java_p ? "jruby" : "mri_dev"}" end task test: [:ruby_test, :cargo_test] diff --git a/ext/Cargo.toml b/ext/Cargo.toml index 61596ae..9827e5e 100644 --- a/ext/Cargo.toml +++ b/ext/Cargo.toml @@ -14,15 +14,13 @@ rb-sys = { version = "0.9", features = [ "bindgen-rbimpls", "bindgen-deprecated-types", "stable-api-compiled-fallback", -] } - -[dev-dependencies] -rb-sys = { version = "0.9", features = [ - "link-ruby", - "bindgen-rbimpls", - "bindgen-deprecated-types", - "stable-api-compiled-fallback", -] } +], optional = true } +robusta_jni = { version = "0.2.2", optional = true } [build-dependencies] rb-sys-env = { version = "0.1" } + +[features] +mri = ["dep:rb-sys"] +mri_dev = ["mri", "rb-sys/link-ruby"] +jruby = ["dep:robusta_jni"] diff --git a/ext/build.rs b/ext/build.rs index 03ec1cd..f8fc5d2 100644 --- a/ext/build.rs +++ b/ext/build.rs @@ -6,7 +6,9 @@ fn main() -> Result<(), Box> { // // This is not a requirement, but it is a convenient if you want to use // `cargo test`, etc. - rb_sys_env::activate()?; - + #[cfg(feature = "mri")] + { + rb_sys_env::activate()?; + } Ok(()) } diff --git a/ext/extconf.rb b/ext/extconf.rb index 9da5bfd..a0b2fd8 100644 --- a/ext/extconf.rb +++ b/ext/extconf.rb @@ -3,4 +3,8 @@ require "mkmf" require "rb_sys/mkmf" -create_rust_makefile("oxi/test/oxi_test") +java_p = RUBY_PLATFORM.include?("java") + +create_rust_makefile("oxi/test/oxi_test") do |r| + r.features = java_p ? %w[jruby] : %w[mri] +end \ No newline at end of file diff --git a/ext/oxi/test/OxiTest.java b/ext/oxi/test/OxiTest.java new file mode 100644 index 0000000..d32cbdb --- /dev/null +++ b/ext/oxi/test/OxiTest.java @@ -0,0 +1,20 @@ +package oxi.test; + +import org.jruby.*; +import org.jruby.anno.JRubyMethod; +import org.jruby.anno.JRubyModule; +import org.jruby.runtime.ThreadContext; +import org.jruby.runtime.builtin.IRubyObject; + +@SuppressWarnings("serial") +@JRubyModule(name = "Xml") +public class OxiTest { + +// private static native String buildNative(String input); + + // https://github.com/jruby/jruby/wiki/JRubyMethod_Signatures + @JRubyMethod(name = "hello", module = true, meta = true) + public static IRubyObject hello(ThreadContext context, IRubyObject self, RubyString name) { + return RubyString.newString(context.getRuntime(), "Hello, " + name.asJavaString()); + } +} \ No newline at end of file diff --git a/ext/oxi/test/OxiTestService.java b/ext/oxi/test/OxiTestService.java new file mode 100644 index 0000000..3f068f7 --- /dev/null +++ b/ext/oxi/test/OxiTestService.java @@ -0,0 +1,18 @@ +package oxi.test; + +import org.jruby.*; +import org.jruby.runtime.load.BasicLibraryService; + +public class OxiTestService implements BasicLibraryService { + public static void systemLoad(String libPath) { + System.load(libPath); + } + + @Override + public boolean basicLoad(final Ruby ruby) { + RubyModule oxi_module = ruby.defineModule("Oxi"); + RubyModule oxi_test_module = ruby.defineModuleUnder("Test", oxi_module); + oxi_test_module.defineAnnotatedMethods(OxiTest.class); + return true; + } +} diff --git a/ext/src/lib.rs b/ext/src/lib.rs index 758ac00..391800f 100644 --- a/ext/src/lib.rs +++ b/ext/src/lib.rs @@ -6,16 +6,20 @@ //! If you do need to drop down into raw libruby, you can enable the //! `rb-sys-interop` feature and add `rb-sys` to you Cargo dependencies. +#[cfg(feature = "mri")] use rb_sys::{ rb_define_module, rb_define_module_under, rb_define_singleton_method, rb_str_buf_append, rb_utf8_str_new_cstr, VALUE, }; +#[cfg(feature = "mri")] use std::{ffi::CString, intrinsics::transmute, os::raw::c_char}; +#[cfg(feature = "mri")] trait AsCStr { fn to_cstring(&self) -> *const c_char; } +#[cfg(feature = "mri")] impl AsCStr for str { /// Convert a Rust string to a C string. fn to_cstring(&self) -> *const c_char { @@ -23,10 +27,12 @@ impl AsCStr for str { } } +#[cfg(feature = "mri")] unsafe extern "C" fn hello(_: VALUE, name: VALUE) -> VALUE { rb_str_buf_append(rb_utf8_str_new_cstr("Hello, ".to_cstring()), name) } +#[cfg(feature = "mri")] #[no_mangle] unsafe extern "C" fn Init_oxi_test() { let oxi_module = rb_define_module("Oxi".to_cstring()); @@ -42,6 +48,7 @@ unsafe extern "C" fn Init_oxi_test() { ); } +#[cfg(feature = "mri")] #[cfg(test)] mod tests { use crate::{AsCStr, Init_oxi_test}; diff --git a/lib/oxi/test.rb b/lib/oxi/test.rb index fa2fd94..dab5227 100644 --- a/lib/oxi/test.rb +++ b/lib/oxi/test.rb @@ -9,6 +9,9 @@ require "oxi/test/oxi_test" end +require "oxi/test/jruby" if RUBY_PLATFORM.include?("java") + + module Oxi module Test class Error < StandardError; end diff --git a/lib/oxi/test/jruby.rb b/lib/oxi/test/jruby.rb new file mode 100644 index 0000000..0bb2966 --- /dev/null +++ b/lib/oxi/test/jruby.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require "jruby" + +def oxi + Java::Oxi +end + +# like require_relative +lib_path = File.join( + File.absolute_path(File.dirname(__FILE__)), + "oxi_test.#{RbConfig::MAKEFILE_CONFIG["DLEXT"]}" +) +oxi.test.OxiTestService.systemLoad(lib_path) +oxi.test.OxiTestService.new.basicLoad(JRuby.runtime) diff --git a/oxi-test.gemspec b/oxi-test.gemspec index 805291e..9dc54a8 100644 --- a/oxi-test.gemspec +++ b/oxi-test.gemspec @@ -2,6 +2,8 @@ require_relative "lib/oxi/test/version" +java_p = RUBY_PLATFORM.include?("java") + Gem::Specification.new do |spec| spec.name = "oxi-test" spec.version = Oxi::Test::VERSION @@ -17,7 +19,9 @@ Gem::Specification.new do |spec| spec.metadata["source_code_uri"] = spec.homepage spec.required_ruby_version = ">= 2.3.0" - spec.files = Dir["lib/**/*.rb", "ext/**/*.{rs,rb}", "**/Cargo.*", "LICENSE.txt", "README.md"] + ext_files = %w[rs rb] + ext_files << "java" if java_p + spec.files = Dir["lib/**/*.rb", "ext/**/*.{#{ext_files.join(",")}}", "**/Cargo.*", "LICENSE.txt", "README.md"] spec.bindir = "exe" spec.executables = [] spec.require_paths = ["lib"] diff --git a/rake_ext.rb b/rake_ext.rb new file mode 100644 index 0000000..4f57392 --- /dev/null +++ b/rake_ext.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +# rubocop:disable all + +# add rename_task method to Rake::Application +# it has an internal hash with name -> Rake::Task mapping +module Rake + class Application + def rename_task(task, oldname, newname) + @tasks = {} if @tasks.nil? + @tasks[newname.to_s] = task + + @tasks.delete(oldname) if @tasks.has_key?(oldname) + end + end +end + +# add new rename method to Rake::Task class +# to rename a task +class Rake::Task + def rename(new_name) + unless new_name.nil? + old_name = @name + + return if old_name == new_name + + @name = new_name.to_s + application.rename_task(self, old_name, new_name) + end + end +end + +# rubocop:enable all From 7e14654df468dbb387e22f0c3778067ceb0fc58c Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Fri, 15 Dec 2023 08:10:05 +0300 Subject: [PATCH 02/32] add native impl for java --- ext/oxi/test/OxiTest.java | 4 +-- ext/src/lib.rs | 58 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/ext/oxi/test/OxiTest.java b/ext/oxi/test/OxiTest.java index d32cbdb..fc3a631 100644 --- a/ext/oxi/test/OxiTest.java +++ b/ext/oxi/test/OxiTest.java @@ -10,11 +10,11 @@ @JRubyModule(name = "Xml") public class OxiTest { -// private static native String buildNative(String input); + private static native String helloNative(String name); // https://github.com/jruby/jruby/wiki/JRubyMethod_Signatures @JRubyMethod(name = "hello", module = true, meta = true) public static IRubyObject hello(ThreadContext context, IRubyObject self, RubyString name) { - return RubyString.newString(context.getRuntime(), "Hello, " + name.asJavaString()); + return RubyString.newString(context.getRuntime(), OxiTest.helloNative(name.asJavaString())); } } \ No newline at end of file diff --git a/ext/src/lib.rs b/ext/src/lib.rs index 391800f..3125fbe 100644 --- a/ext/src/lib.rs +++ b/ext/src/lib.rs @@ -80,3 +80,61 @@ mod tests { } } } + +#[cfg(feature = "jruby")] +use std::os::raw::c_void; +#[cfg(feature = "jruby")] +use robusta_jni::convert::{Signature, TryFromJavaValue, TryIntoJavaValue}; +#[cfg(feature = "jruby")] +use robusta_jni::jni::{ + JavaVM, JNIEnv, NativeMethod, objects::{JClass, JString}, strings::JNIString, + sys::{jint, JNI_ERR, JNI_VERSION_1_4}, +}; + +#[cfg(feature = "jruby")] +extern "system" fn hello<'local>(env: JNIEnv<'local>, + _class: JClass<'local>, + name: >::Source, +) -> >::Target { + let name_res: robusta_jni::jni::errors::Result = TryFromJavaValue::try_from(name, &env); + match name_res { + Ok(name_conv) => { + let res = format!("Hello, {}", name_conv); + let res_res: robusta_jni::jni::errors::Result<::Target> = TryIntoJavaValue::try_into(res, &env); + match res_res { + Ok(conv_res) => { return conv_res; } + Err(err) => { + // No need to handle err, ClassNotFoundException will be thrown implicitly + let _ = env.throw_new("java/lang/RuntimeException", format!("{:?}", err)); + } + } + } + Err(err) => { + // No need to handle err, ClassNotFoundException will be thrown implicitly + let _ = env.throw_new("java/lang/RuntimeException", format!("{:?}", err)); + } + } + JString::from(std::ptr::null_mut()) +} + +/// This function is executed on loading native library by JVM. +/// It initializes the cache of method and class references. +#[allow(non_snake_case)] +#[no_mangle] +pub extern "system" fn JNI_OnLoad<'local>(vm: JavaVM, _: *mut c_void) -> jint { + let Ok(env) = vm.get_env() else { return JNI_ERR; }; + let Ok(clazz) = env.find_class( + "oxi/test/OxiTest" + ) else { return JNI_ERR; }; + let hello_func = hello as unsafe extern "system" fn(env: JNIEnv<'local>, _class: JClass<'local>, name: JString<'local>) -> JString<'local>; + let hello_ptr = hello_func as *mut c_void; + let build_xml_method = NativeMethod { + name: JNIString::from("helloNative"), + sig: JNIString::from(format!("({}){}", + ::SIG_TYPE, + ::SIG_TYPE)), + fn_ptr: hello_ptr, + }; + let Ok(_) = env.register_native_methods(clazz, &[build_xml_method]) else { return JNI_ERR; }; + JNI_VERSION_1_4 +} From e1fb188c3c5f519c48328e5f031716f40f2b55ce Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Fri, 15 Dec 2023 10:56:57 +0300 Subject: [PATCH 03/32] Add simple test --- Cargo.lock | 1 + Gemfile | 2 +- Rakefile | 12 +++++++++++- ext/Cargo.toml | 2 ++ ext/src/lib.rs | 27 +++++++++++++++++++++++++++ 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6b8690e..1200764 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -239,6 +239,7 @@ dependencies = [ name = "oxi-test" version = "0.1.0" dependencies = [ + "jni", "rb-sys", "rb-sys-env", "robusta_jni", diff --git a/Gemfile b/Gemfile index a96d6fa..a862504 100644 --- a/Gemfile +++ b/Gemfile @@ -6,5 +6,5 @@ gemspec gem "rake", "~> 13.0" gem "rake-compiler", "~> 1.2" -gem "rb_sys", "~> 0.9.65" +gem "rb_sys", git: "https://github.com/uvlad7/rb-sys", ref: "4d5bd86" gem "minitest", "~> 5.16" diff --git a/Rakefile b/Rakefile index 082c4d0..420d381 100644 --- a/Rakefile +++ b/Rakefile @@ -52,7 +52,17 @@ task :native, [:platform] do |_t, platform:| end task :cargo_test do - sh "cargo test --features #{java_p ? "jruby" : "mri_dev"}" + if java_p + env = { + # For libjvm.so + "LD_LIBRARY_PATH" => File.join( + java.lang.System.getProperty("java.home"), "lib", "server" + ) + } + sh env, "cargo test --features jruby_dev" + else + sh "cargo test --features mri_dev" + end end task test: [:ruby_test, :cargo_test] diff --git a/ext/Cargo.toml b/ext/Cargo.toml index 9827e5e..5b541f1 100644 --- a/ext/Cargo.toml +++ b/ext/Cargo.toml @@ -15,6 +15,7 @@ rb-sys = { version = "0.9", features = [ "bindgen-deprecated-types", "stable-api-compiled-fallback", ], optional = true } +jni = { version = "*", optional = true } robusta_jni = { version = "0.2.2", optional = true } [build-dependencies] @@ -24,3 +25,4 @@ rb-sys-env = { version = "0.1" } mri = ["dep:rb-sys"] mri_dev = ["mri", "rb-sys/link-ruby"] jruby = ["dep:robusta_jni"] +jruby_dev = ["jruby", "jni/invocation"] diff --git a/ext/src/lib.rs b/ext/src/lib.rs index 3125fbe..f982f77 100644 --- a/ext/src/lib.rs +++ b/ext/src/lib.rs @@ -138,3 +138,30 @@ pub extern "system" fn JNI_OnLoad<'local>(vm: JavaVM, _: *mut c_void) -> jint { let Ok(_) = env.register_native_methods(clazz, &[build_xml_method]) else { return JNI_ERR; }; JNI_VERSION_1_4 } + +#[cfg(feature = "jruby")] +#[cfg(test)] +mod tests { + use robusta_jni::jni::{Executor, InitArgsBuilder, JavaVM}; + use std::sync::Arc; + use jni::objects::{JClass, JString}; + use robusta_jni::convert::{TryFromJavaValue}; + use crate::hello; + + #[test] + fn test_simple_hello() { + let jvm_args = InitArgsBuilder::new().build().unwrap(); + let jvm = Arc::new(JavaVM::new(jvm_args).unwrap()); + let exec = Executor::new(jvm); + + let val: String = exec.with_attached(|env| { + let name = env.new_string("world")?; + // Too hard to load a real class and call env.call_static_method() + let raw_res: JString = hello(*(env), JClass::from(std::ptr::null_mut()), name); + let res: jni::errors::Result = TryFromJavaValue::try_from(JString::from(raw_res), &env); + res + }).unwrap(); + + assert_eq!(val, "Hello, world"); + } +} From 738a34a1896b14d2e9fc3e4ed21c5df766a3fd30 Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Fri, 15 Dec 2023 12:37:48 +0300 Subject: [PATCH 04/32] Fix --- .github/workflows/ci.yml | 1 + ext/src/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e3e47be..18d4165 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,6 +29,7 @@ jobs: include: - os: windows-latest ruby: mswin + - ruby: jruby-9.4.3.0 steps: - uses: actions/checkout@v3 diff --git a/ext/src/lib.rs b/ext/src/lib.rs index f982f77..61535b6 100644 --- a/ext/src/lib.rs +++ b/ext/src/lib.rs @@ -117,6 +117,7 @@ extern "system" fn hello<'local>(env: JNIEnv<'local>, JString::from(std::ptr::null_mut()) } +#[cfg(feature = "jruby")] /// This function is executed on loading native library by JVM. /// It initializes the cache of method and class references. #[allow(non_snake_case)] From 2c8e5a7ccd35aae8a3a897af7d9c40f0e7a84b14 Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Fri, 15 Dec 2023 12:48:48 +0300 Subject: [PATCH 05/32] Fix --- .github/workflows/ci.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 18d4165..7e853bd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,6 +30,11 @@ jobs: - os: windows-latest ruby: mswin - ruby: jruby-9.4.3.0 + os: "ubuntu-latest" + - ruby: jruby-9.4.3.0 + os: "macos-latest" + - ruby: jruby-9.4.3.0 + os: "windows-latest" steps: - uses: actions/checkout@v3 From c14296c505287783c7544e645ed2bf2ade5e1705 Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Fri, 15 Dec 2023 12:57:01 +0300 Subject: [PATCH 06/32] Get rid of recursive Cargo files copying in the tmp dir --- oxi-test.gemspec | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/oxi-test.gemspec b/oxi-test.gemspec index 9dc54a8..d18d478 100644 --- a/oxi-test.gemspec +++ b/oxi-test.gemspec @@ -21,7 +21,8 @@ Gem::Specification.new do |spec| ext_files = %w[rs rb] ext_files << "java" if java_p - spec.files = Dir["lib/**/*.rb", "ext/**/*.{#{ext_files.join(",")}}", "**/Cargo.*", "LICENSE.txt", "README.md"] + spec.files = Dir["lib/**/*.rb", "ext/**/*.{#{ext_files.join(",")}}", + "Cargo.lock", "Cargo.toml", "ext/Cargo.toml", "LICENSE.txt", "README.md"] spec.bindir = "exe" spec.executables = [] spec.require_paths = ["lib"] From daaf6c365d50328b139ef3d062531d138639ff4e Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Fri, 15 Dec 2023 12:57:22 +0300 Subject: [PATCH 07/32] Fmt --- ext/src/lib.rs | 74 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/ext/src/lib.rs b/ext/src/lib.rs index 61535b6..b2b3664 100644 --- a/ext/src/lib.rs +++ b/ext/src/lib.rs @@ -81,28 +81,34 @@ mod tests { } } -#[cfg(feature = "jruby")] -use std::os::raw::c_void; #[cfg(feature = "jruby")] use robusta_jni::convert::{Signature, TryFromJavaValue, TryIntoJavaValue}; #[cfg(feature = "jruby")] use robusta_jni::jni::{ - JavaVM, JNIEnv, NativeMethod, objects::{JClass, JString}, strings::JNIString, + objects::{JClass, JString}, + strings::JNIString, sys::{jint, JNI_ERR, JNI_VERSION_1_4}, + JNIEnv, JavaVM, NativeMethod, }; +#[cfg(feature = "jruby")] +use std::os::raw::c_void; #[cfg(feature = "jruby")] -extern "system" fn hello<'local>(env: JNIEnv<'local>, - _class: JClass<'local>, - name: >::Source, +extern "system" fn hello<'local>( + env: JNIEnv<'local>, + _class: JClass<'local>, + name: >::Source, ) -> >::Target { let name_res: robusta_jni::jni::errors::Result = TryFromJavaValue::try_from(name, &env); match name_res { Ok(name_conv) => { let res = format!("Hello, {}", name_conv); - let res_res: robusta_jni::jni::errors::Result<::Target> = TryIntoJavaValue::try_into(res, &env); + let res_res: robusta_jni::jni::errors::Result<::Target> = + TryIntoJavaValue::try_into(res, &env); match res_res { - Ok(conv_res) => { return conv_res; } + Ok(conv_res) => { + return conv_res; + } Err(err) => { // No need to handle err, ClassNotFoundException will be thrown implicitly let _ = env.throw_new("java/lang/RuntimeException", format!("{:?}", err)); @@ -123,31 +129,42 @@ extern "system" fn hello<'local>(env: JNIEnv<'local>, #[allow(non_snake_case)] #[no_mangle] pub extern "system" fn JNI_OnLoad<'local>(vm: JavaVM, _: *mut c_void) -> jint { - let Ok(env) = vm.get_env() else { return JNI_ERR; }; - let Ok(clazz) = env.find_class( - "oxi/test/OxiTest" - ) else { return JNI_ERR; }; - let hello_func = hello as unsafe extern "system" fn(env: JNIEnv<'local>, _class: JClass<'local>, name: JString<'local>) -> JString<'local>; + let Ok(env) = vm.get_env() else { + return JNI_ERR; + }; + let Ok(clazz) = env.find_class("oxi/test/OxiTest") else { + return JNI_ERR; + }; + let hello_func = hello + as unsafe extern "system" fn( + env: JNIEnv<'local>, + _class: JClass<'local>, + name: JString<'local>, + ) -> JString<'local>; let hello_ptr = hello_func as *mut c_void; let build_xml_method = NativeMethod { name: JNIString::from("helloNative"), - sig: JNIString::from(format!("({}){}", - ::SIG_TYPE, - ::SIG_TYPE)), + sig: JNIString::from(format!( + "({}){}", + ::SIG_TYPE, + ::SIG_TYPE + )), fn_ptr: hello_ptr, }; - let Ok(_) = env.register_native_methods(clazz, &[build_xml_method]) else { return JNI_ERR; }; + let Ok(_) = env.register_native_methods(clazz, &[build_xml_method]) else { + return JNI_ERR; + }; JNI_VERSION_1_4 } #[cfg(feature = "jruby")] #[cfg(test)] mod tests { + use crate::hello; + use jni::objects::{JClass, JString}; + use robusta_jni::convert::TryFromJavaValue; use robusta_jni::jni::{Executor, InitArgsBuilder, JavaVM}; use std::sync::Arc; - use jni::objects::{JClass, JString}; - use robusta_jni::convert::{TryFromJavaValue}; - use crate::hello; #[test] fn test_simple_hello() { @@ -155,13 +172,16 @@ mod tests { let jvm = Arc::new(JavaVM::new(jvm_args).unwrap()); let exec = Executor::new(jvm); - let val: String = exec.with_attached(|env| { - let name = env.new_string("world")?; - // Too hard to load a real class and call env.call_static_method() - let raw_res: JString = hello(*(env), JClass::from(std::ptr::null_mut()), name); - let res: jni::errors::Result = TryFromJavaValue::try_from(JString::from(raw_res), &env); - res - }).unwrap(); + let val: String = exec + .with_attached(|env| { + let name = env.new_string("world")?; + // Too hard to load a real class and call env.call_static_method() + let raw_res: JString = hello(*(env), JClass::from(std::ptr::null_mut()), name); + let res: jni::errors::Result = + TryFromJavaValue::try_from(JString::from(raw_res), &env); + res + }) + .unwrap(); assert_eq!(val, "Hello, world"); } From 129027830cfe0631396014ac1d4056f00347cf82 Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Fri, 15 Dec 2023 13:10:51 +0300 Subject: [PATCH 08/32] Bump java version for macos --- Rakefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Rakefile b/Rakefile index 420d381..65636f7 100644 --- a/Rakefile +++ b/Rakefile @@ -15,8 +15,8 @@ if java_p Rake::JavaExtensionTask.new("oxi_test", GEMSPEC) do |ext| ext.lib_dir = "lib/oxi/test" ext.ext_dir = "ext" - ext.source_version = "11" # or "8" - ext.target_version = "11" # or "8" + ext.source_version = "17" + ext.target_version = "17" end Rake::Task[:compile].rename(:java_compile) From 168c90071aaac48a48698d593ab851c5f5b2f800 Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Fri, 15 Dec 2023 13:30:40 +0300 Subject: [PATCH 09/32] Fix --- .github/workflows/ci.yml | 6 ++++++ Rakefile | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7e853bd..199f93f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,6 +38,12 @@ jobs: steps: - uses: actions/checkout@v3 + - uses: actions/setup-java@v4 + if: startsWith( matrix.ruby, "jruby") + with: + distribution: 'microsoft' + java-version: '21' + - uses: oxidize-rb/actions/setup-ruby-and-rust@v1 with: ruby-version: ${{ matrix.ruby }} diff --git a/Rakefile b/Rakefile index 65636f7..420d381 100644 --- a/Rakefile +++ b/Rakefile @@ -15,8 +15,8 @@ if java_p Rake::JavaExtensionTask.new("oxi_test", GEMSPEC) do |ext| ext.lib_dir = "lib/oxi/test" ext.ext_dir = "ext" - ext.source_version = "17" - ext.target_version = "17" + ext.source_version = "11" # or "8" + ext.target_version = "11" # or "8" end Rake::Task[:compile].rename(:java_compile) From 81bdd12faf485670ff96f6fb2b450fe59738aa0a Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Fri, 15 Dec 2023 13:31:54 +0300 Subject: [PATCH 10/32] Fix --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 199f93f..4e9e36a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,7 +39,7 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-java@v4 - if: startsWith( matrix.ruby, "jruby") + if: startsWith( matrix.ruby, 'jruby') with: distribution: 'microsoft' java-version: '21' From 059d8723f04acdb1b9f505406cb6c3f385e82f1d Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Fri, 15 Dec 2023 21:52:30 +0300 Subject: [PATCH 11/32] Fix lib path --- Rakefile | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Rakefile b/Rakefile index 420d381..13d78dc 100644 --- a/Rakefile +++ b/Rakefile @@ -53,12 +53,16 @@ end task :cargo_test do if java_p - env = { - # For libjvm.so - "LD_LIBRARY_PATH" => File.join( - java.lang.System.getProperty("java.home"), "lib", "server" - ) - } + # For libjvm.so + lib_path = File.join( + java.lang.System.getProperty("java.home"), "lib", "server" + ) + if Gem.win_platform? + env = { "PATH" => "#{ENV["PATH"]};#{lib_path}" } + else + env = { "LD_LIBRARY_PATH" => lib_path } + end + sh env, "cargo test --features jruby_dev" else sh "cargo test --features mri_dev" From 21de34661ad61a5d4485771301a3ea02f864493e Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Fri, 15 Dec 2023 22:14:17 +0300 Subject: [PATCH 12/32] debug --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4e9e36a..e50cf9c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,6 +23,7 @@ jobs: runs-on: ${{ matrix.os }} needs: ci-data strategy: + fail-fast: false # TODO: Remove, temporary matrix: os: ["ubuntu-latest", "macos-latest", "windows-latest"] ruby: ${{ fromJSON(needs.ci-data.outputs.result).stable-ruby-versions }} From cadd6f087fb76effeaae1bc44ee0c850461f9b14 Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Wed, 20 Dec 2023 01:29:01 +0300 Subject: [PATCH 13/32] debug --- .github/workflows/ci.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e50cf9c..398a4f2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,7 +50,10 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true cargo-cache: true - cache-version: v2 + cache-version: v2-tmp + + - name: Debug session + uses: mxschmitt/action-tmate@v3 - name: Run ruby tests run: bundle exec rake From 0b6b6c6ac6f96535eba0d66d6f42e4640dfc76e2 Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Wed, 20 Dec 2023 01:32:35 +0300 Subject: [PATCH 14/32] debug --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 398a4f2..0c35877 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,11 +46,12 @@ jobs: java-version: '21' - uses: oxidize-rb/actions/setup-ruby-and-rust@v1 + continue-on-error: true with: ruby-version: ${{ matrix.ruby }} bundler-cache: true cargo-cache: true - cache-version: v2-tmp + cache-version: v2 - name: Debug session uses: mxschmitt/action-tmate@v3 From 58a3a6c1c918b07f626d7cc4ab252b963cc0036b Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Wed, 20 Dec 2023 08:50:57 +0300 Subject: [PATCH 15/32] Use forked github actions --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0c35877..fefa931 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,7 +45,7 @@ jobs: distribution: 'microsoft' java-version: '21' - - uses: oxidize-rb/actions/setup-ruby-and-rust@v1 + - uses: uvlad7/oxidize-rb-actions/setup-ruby-and-rust@main continue-on-error: true with: ruby-version: ${{ matrix.ruby }} From 647d7ff72ae5b2212313f373c71506c5b10531ec Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Thu, 21 Dec 2023 06:42:09 +0300 Subject: [PATCH 16/32] Patch for CI --- .github/workflows/ci.yml | 6 +++--- Gemfile | 2 +- Rakefile | 13 +++++++------ oxi-test.gemspec | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fefa931..71f13c6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,7 +23,6 @@ jobs: runs-on: ${{ matrix.os }} needs: ci-data strategy: - fail-fast: false # TODO: Remove, temporary matrix: os: ["ubuntu-latest", "macos-latest", "windows-latest"] ruby: ${{ fromJSON(needs.ci-data.outputs.result).stable-ruby-versions }} @@ -34,8 +33,9 @@ jobs: os: "ubuntu-latest" - ruby: jruby-9.4.3.0 os: "macos-latest" - - ruby: jruby-9.4.3.0 - os: "windows-latest" +# robusta appears not to work on windows +# - ruby: jruby-9.4.3.0 +# os: "windows-latest" steps: - uses: actions/checkout@v3 diff --git a/Gemfile b/Gemfile index a862504..8399995 100644 --- a/Gemfile +++ b/Gemfile @@ -6,5 +6,5 @@ gemspec gem "rake", "~> 13.0" gem "rake-compiler", "~> 1.2" -gem "rb_sys", git: "https://github.com/uvlad7/rb-sys", ref: "4d5bd86" +gem "rb_sys", git: "https://github.com/uvlad7/rb-sys", branch: "feature/jruby" gem "minitest", "~> 5.16" diff --git a/Rakefile b/Rakefile index 13d78dc..d8f61b7 100644 --- a/Rakefile +++ b/Rakefile @@ -53,14 +53,15 @@ end task :cargo_test do if java_p - # For libjvm.so - lib_path = File.join( - java.lang.System.getProperty("java.home"), "lib", "server" - ) + java_home = java.lang.System.getProperty("java.home") + lib_path = [ + File.join(java_home, "lib", "server"), # For libjvm.so + File.join(java_home, "lib"), # For libjli.dylib + ] if Gem.win_platform? - env = { "PATH" => "#{ENV["PATH"]};#{lib_path}" } + env = { "PATH" => "#{ENV["PATH"]};#{lib_path.join(";")}" } else - env = { "LD_LIBRARY_PATH" => lib_path } + env = { "LD_LIBRARY_PATH" => lib_path.join(":"), "DYLD_LIBRARY_PATH" => lib_path.join(":") } end sh env, "cargo test --features jruby_dev" diff --git a/oxi-test.gemspec b/oxi-test.gemspec index d18d478..b17557f 100644 --- a/oxi-test.gemspec +++ b/oxi-test.gemspec @@ -28,5 +28,5 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.extensions = ["ext/extconf.rb"] - spec.add_dependency "rb_sys", "~> 0.9" + # spec.add_dependency "rb_sys", "~> 0.9" end From 3c6282614bfd5a728ebcc5fec1510fef28cf508a Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Thu, 21 Dec 2023 06:46:48 +0300 Subject: [PATCH 17/32] Use the same java as robusta --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 71f13c6..df30299 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -42,8 +42,8 @@ jobs: - uses: actions/setup-java@v4 if: startsWith( matrix.ruby, 'jruby') with: - distribution: 'microsoft' - java-version: '21' + distribution: 'temurin' + java-version: '17' - uses: uvlad7/oxidize-rb-actions/setup-ruby-and-rust@main continue-on-error: true From ebaf51f7d9a770517f56ada7a9396d12fffdbc2e Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Thu, 21 Dec 2023 07:14:02 +0300 Subject: [PATCH 18/32] Remove dbg --- .github/workflows/ci.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index df30299..19c6ef9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -53,9 +53,6 @@ jobs: cargo-cache: true cache-version: v2 - - name: Debug session - uses: mxschmitt/action-tmate@v3 - - name: Run ruby tests run: bundle exec rake From 830baa4f9eb1cdd5e5c773e370552016a63adbc0 Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Thu, 21 Dec 2023 20:54:25 +0300 Subject: [PATCH 19/32] Fix tests --- .github/workflows/ci.yml | 31 ++++++++++++++++--------------- Rakefile | 17 +++++++++++------ oxi-test.gemspec | 2 +- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 19c6ef9..be81637 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,6 +23,7 @@ jobs: runs-on: ${{ matrix.os }} needs: ci-data strategy: + fail-fast: false matrix: os: ["ubuntu-latest", "macos-latest", "windows-latest"] ruby: ${{ fromJSON(needs.ci-data.outputs.result).stable-ruby-versions }} @@ -33,9 +34,8 @@ jobs: os: "ubuntu-latest" - ruby: jruby-9.4.3.0 os: "macos-latest" -# robusta appears not to work on windows -# - ruby: jruby-9.4.3.0 -# os: "windows-latest" + - ruby: jruby-9.4.3.0 + os: "windows-latest" steps: - uses: actions/checkout@v3 @@ -59,15 +59,16 @@ jobs: - name: Lint rust run: cargo clippy && cargo fmt --check - - name: Test source gem - shell: bash - run: | - bundle exec rake build - gem install --verbose pkg/*.gem - - if ruby -roxi/test -e "puts Oxi::Test.hello('world!')" | grep "Hello, world!"; then - echo "Success" - else - echo "Failure" - exit 1 - fi + # Temporary disable until new rb-sys gem is released +# - name: Test source gem +# shell: bash +# run: | +# bundle exec rake build +# gem install --verbose pkg/*.gem +# +# if ruby -roxi/test -e "puts Oxi::Test.hello('world!')" | grep "Hello, world!"; then +# echo "Success" +# else +# echo "Failure" +# exit 1 +# fi diff --git a/Rakefile b/Rakefile index d8f61b7..dd691bb 100644 --- a/Rakefile +++ b/Rakefile @@ -54,14 +54,19 @@ end task :cargo_test do if java_p java_home = java.lang.System.getProperty("java.home") - lib_path = [ - File.join(java_home, "lib", "server"), # For libjvm.so - File.join(java_home, "lib"), # For libjli.dylib - ] if Gem.win_platform? - env = { "PATH" => "#{ENV["PATH"]};#{lib_path.join(";")}" } + bin_path = [ + File.join(java_home, "bin", "server"), + File.join(java_home, "bin"), + ] + env = { "PATH" => "#{ENV["PATH"]};#{bin_path.join(";")}" } else - env = { "LD_LIBRARY_PATH" => lib_path.join(":"), "DYLD_LIBRARY_PATH" => lib_path.join(":") } + lib_path = [ + File.join(java_home, "lib", "server"), # For libjvm.so + File.join(java_home, "lib"), # For libjli.dylib + ] + var_name = RUBY_PLATFORM =~ /darwin/ ? "DYLD_FALLBACK_LIBRARY_PATH" : "LD_LIBRARY_PATH" + env = { var_name => "#{ENV[var_name]}:#{lib_path.join(":")}" } end sh env, "cargo test --features jruby_dev" diff --git a/oxi-test.gemspec b/oxi-test.gemspec index b17557f..d18d478 100644 --- a/oxi-test.gemspec +++ b/oxi-test.gemspec @@ -28,5 +28,5 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.extensions = ["ext/extconf.rb"] - # spec.add_dependency "rb_sys", "~> 0.9" + spec.add_dependency "rb_sys", "~> 0.9" end From 702a53ff08f14985b50b75f28c1d4f625a1e5897 Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Thu, 21 Dec 2023 21:03:56 +0300 Subject: [PATCH 20/32] Dbg --- .github/workflows/ci.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index be81637..be55de9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,8 +25,8 @@ jobs: strategy: fail-fast: false matrix: - os: ["ubuntu-latest", "macos-latest", "windows-latest"] - ruby: ${{ fromJSON(needs.ci-data.outputs.result).stable-ruby-versions }} +# os: ["ubuntu-latest", "macos-latest", "windows-latest"] +# ruby: ${{ fromJSON(needs.ci-data.outputs.result).stable-ruby-versions }} include: - os: windows-latest ruby: mswin @@ -46,13 +46,15 @@ jobs: java-version: '17' - uses: uvlad7/oxidize-rb-actions/setup-ruby-and-rust@main - continue-on-error: true with: ruby-version: ${{ matrix.ruby }} bundler-cache: true cargo-cache: true cache-version: v2 + - name: Debug session + uses: mxschmitt/action-tmate@v3 + - name: Run ruby tests run: bundle exec rake From 81254d6c5c93b4b1cdc4d7f98bdd989fc9c0a95f Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Thu, 21 Dec 2023 21:33:27 +0300 Subject: [PATCH 21/32] Dbg --- .github/workflows/ci.yml | 3 ++- Rakefile | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index be55de9..a390a7c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,8 +25,9 @@ jobs: strategy: fail-fast: false matrix: -# os: ["ubuntu-latest", "macos-latest", "windows-latest"] + os: ["windows-latest"] #["ubuntu-latest", "macos-latest", "windows-latest"] # ruby: ${{ fromJSON(needs.ci-data.outputs.result).stable-ruby-versions }} + ruby: ["3.2"] include: - os: windows-latest ruby: mswin diff --git a/Rakefile b/Rakefile index dd691bb..bc60ac9 100644 --- a/Rakefile +++ b/Rakefile @@ -65,7 +65,7 @@ task :cargo_test do File.join(java_home, "lib", "server"), # For libjvm.so File.join(java_home, "lib"), # For libjli.dylib ] - var_name = RUBY_PLATFORM =~ /darwin/ ? "DYLD_FALLBACK_LIBRARY_PATH" : "LD_LIBRARY_PATH" + var_name = RbConfig::MAKEFILE_CONFIG["RUBY_PLATFORM"] =~ /darwin/ ? "DYLD_FALLBACK_LIBRARY_PATH" : "LD_LIBRARY_PATH" env = { var_name => "#{ENV[var_name]}:#{lib_path.join(":")}" } end From 592f0ccb16ddebc025f32b2711f766d860330cfa Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Thu, 21 Dec 2023 22:12:30 +0300 Subject: [PATCH 22/32] Dbg --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a390a7c..d2d12fd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,8 +35,8 @@ jobs: os: "ubuntu-latest" - ruby: jruby-9.4.3.0 os: "macos-latest" - - ruby: jruby-9.4.3.0 - os: "windows-latest" +# - ruby: jruby-9.4.3.0 +# os: "windows-latest" steps: - uses: actions/checkout@v3 @@ -51,7 +51,7 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true cargo-cache: true - cache-version: v2 + cache-version: v3 - name: Debug session uses: mxschmitt/action-tmate@v3 From 6bb83d0a0dc2657b389d501c971d402673fc2b9a Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Thu, 21 Dec 2023 22:27:47 +0300 Subject: [PATCH 23/32] Dbg, broken cache cleaned --- .github/workflows/ci.yml | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d2d12fd..eef8b16 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,11 +23,9 @@ jobs: runs-on: ${{ matrix.os }} needs: ci-data strategy: - fail-fast: false matrix: - os: ["windows-latest"] #["ubuntu-latest", "macos-latest", "windows-latest"] -# ruby: ${{ fromJSON(needs.ci-data.outputs.result).stable-ruby-versions }} - ruby: ["3.2"] + os: ["ubuntu-latest", "macos-latest", "windows-latest"] + ruby: ${{ fromJSON(needs.ci-data.outputs.result).stable-ruby-versions }} include: - os: windows-latest ruby: mswin @@ -51,10 +49,7 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true cargo-cache: true - cache-version: v3 - - - name: Debug session - uses: mxschmitt/action-tmate@v3 + cache-version: v2 - name: Run ruby tests run: bundle exec rake From 92c023e4fb6f6d03349128a35869503e8abc2bc5 Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Fri, 22 Dec 2023 03:12:41 +0300 Subject: [PATCH 24/32] Reoder CI matrix params to keep names consistent --- .github/workflows/ci.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eef8b16..394af3d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,12 +29,12 @@ jobs: include: - os: windows-latest ruby: mswin - - ruby: jruby-9.4.3.0 - os: "ubuntu-latest" - - ruby: jruby-9.4.3.0 - os: "macos-latest" -# - ruby: jruby-9.4.3.0 -# os: "windows-latest" + - os: "ubuntu-latest" + ruby: jruby-9.4.3.0 + - os: "macos-latest" + ruby: jruby-9.4.3.0 +# - os: "windows-latest" +# ruby: jruby-9.4.3.0 steps: - uses: actions/checkout@v3 From 0cda32785a8d7b45e3ef92170401207a9b60a89e Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Fri, 22 Dec 2023 03:13:58 +0300 Subject: [PATCH 25/32] Impl test with embedding like in MRI variant --- Rakefile | 16 ++++++-- ext/src/lib.rs | 100 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 106 insertions(+), 10 deletions(-) diff --git a/Rakefile b/Rakefile index bc60ac9..ae3104c 100644 --- a/Rakefile +++ b/Rakefile @@ -46,27 +46,37 @@ Rake::TestTask.new(:ruby_test) do |t| t.test_files = FileList["test/**/*_test.rb"] end +task ruby_test: [:compile] + desc "Build native extension for a given platform (i.e. `rake 'native[x86_64-linux]'`)" task :native, [:platform] do |_t, platform:| sh "bundle", "exec", "rb-sys-dock", "--platform", platform, "--build" end -task :cargo_test do +task :cargo_test, [:compile] do if java_p java_home = java.lang.System.getProperty("java.home") + class_path = [ + ENV["CLASSPATH"], + File.join(RbConfig::CONFIG["libdir"], RbConfig::CONFIG["LIBRUBY"]), # jruby.jar + File.join( + File.absolute_path(File.dirname(__FILE__)), + "lib", "oxi", "test", "oxi_test.jar" + ) + ] if Gem.win_platform? bin_path = [ File.join(java_home, "bin", "server"), File.join(java_home, "bin"), ] - env = { "PATH" => "#{ENV["PATH"]};#{bin_path.join(";")}" } + env = { "PATH" => "#{ENV["PATH"]};#{bin_path.join(";")}", "CLASSPATH" => class_path.join(";") } else lib_path = [ File.join(java_home, "lib", "server"), # For libjvm.so File.join(java_home, "lib"), # For libjli.dylib ] var_name = RbConfig::MAKEFILE_CONFIG["RUBY_PLATFORM"] =~ /darwin/ ? "DYLD_FALLBACK_LIBRARY_PATH" : "LD_LIBRARY_PATH" - env = { var_name => "#{ENV[var_name]}:#{lib_path.join(":")}" } + env = { var_name => "#{ENV[var_name]}:#{lib_path.join(":")}", "CLASSPATH" => class_path.join(":") } end sh env, "cargo test --features jruby_dev" diff --git a/ext/src/lib.rs b/ext/src/lib.rs index b2b3664..880989e 100644 --- a/ext/src/lib.rs +++ b/ext/src/lib.rs @@ -160,25 +160,111 @@ pub extern "system" fn JNI_OnLoad<'local>(vm: JavaVM, _: *mut c_void) -> jint { #[cfg(feature = "jruby")] #[cfg(test)] mod tests { - use crate::hello; - use jni::objects::{JClass, JString}; + use crate::JNI_OnLoad; + use jni::objects::{JObject, JString, JValue}; + use jni::sys::JNI_VERSION_1_4; use robusta_jni::convert::TryFromJavaValue; use robusta_jni::jni::{Executor, InitArgsBuilder, JavaVM}; use std::sync::Arc; #[test] fn test_simple_hello() { - let jvm_args = InitArgsBuilder::new().build().unwrap(); + let jvm_args = InitArgsBuilder::new() + .option(&*format!( + "-Djava.class.path={}", + std::env::var("CLASSPATH").unwrap() + )) + .option("--add-opens=java.base/sun.nio.ch=ALL-UNNAMED") + .option("--add-opens=java.base/java.io=ALL-UNNAMED") + .build() + .unwrap(); let jvm = Arc::new(JavaVM::new(jvm_args).unwrap()); let exec = Executor::new(jvm); let val: String = exec .with_attached(|env| { - let name = env.new_string("world")?; - // Too hard to load a real class and call env.call_static_method() - let raw_res: JString = hello(*(env), JClass::from(std::ptr::null_mut()), name); + assert_eq!( + JNI_OnLoad(env.get_java_vm()?, std::ptr::null_mut()), + JNI_VERSION_1_4 + ); + let j_res = env + .call_static_method( + env.find_class("oxi/test/OxiTest")?, + "helloNative", + "(Ljava/lang/String;)Ljava/lang/String;", + &[JValue::Object(JObject::from(env.new_string("world")?))], + )? + .l()?; + let res: jni::errors::Result = + TryFromJavaValue::try_from(JString::from(j_res), &env); + res + }) + .unwrap(); + + assert_eq!(val, "Hello, world"); + + let val: String = exec + .with_attached(|env| { + // https://github.com/jruby/jruby/wiki/DirectJRubyEmbedding + // Create runtime instance + let load_paths = + env.new_object(env.find_class("java/util/ArrayList")?, "()V", &[])?; + let runtime = env + .call_static_method( + env.find_class("org/jruby/javasupport/JavaEmbedUtils")?, + "initialize", + "(Ljava/util/List;)Lorg/jruby/Ruby;", + &[JValue::Object(load_paths)], + )? + .l()?; + let evaler = env + .call_static_method( + env.find_class("org/jruby/javasupport/JavaEmbedUtils")?, + "newRuntimeAdapter", + "()Lorg/jruby/RubyRuntimeAdapter;", + &[], + )? + .l()?; + + // Init library + // Cannot move, and cannot change JNI_OnLoad signature, so use a hack + assert_eq!( + JNI_OnLoad(env.get_java_vm()?, std::ptr::null_mut()), + JNI_VERSION_1_4 + ); + assert_eq!( + env.call_method( + env.new_object(env.find_class("oxi/test/OxiTestService")?, "()V", &[])?, + "basicLoad", + "(Lorg/jruby/Ruby;)Z", + &[JValue::Object(runtime)], + )? + .z()?, + true + ); + + // Eval and get result + let rb_res = env.call_method( + evaler, "eval", + "(Lorg/jruby/Ruby;Ljava/lang/String;)Lorg/jruby/runtime/builtin/IRubyObject;", + &[JValue::Object(runtime), JValue::Object(JObject::from( + env.new_string("Oxi::Test.hello('world')")? + ))], + )?.l()?; + let j_res = env + .call_method(rb_res, "asJavaString", "()Ljava/lang/String;", &[])? + .l()?; let res: jni::errors::Result = - TryFromJavaValue::try_from(JString::from(raw_res), &env); + TryFromJavaValue::try_from(JString::from(j_res), &env); + + // Shutdown and terminate instance + env.call_static_method( + env.find_class("org/jruby/javasupport/JavaEmbedUtils")?, + "terminate", + "(Lorg/jruby/Ruby;)V", + &[JValue::Object(runtime)], + )?; + res }) .unwrap(); From 2c9a04c44458a9af850a7746f4278bfbf40ab4cd Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Fri, 22 Dec 2023 03:43:55 +0300 Subject: [PATCH 26/32] Revert debug-related changes, fix formatting --- .github/workflows/ci.yml | 31 +++++++++++++++---------------- Gemfile | 2 +- ext/extconf.rb | 2 +- ext/oxi/test/OxiTest.java | 2 +- lib/oxi/test.rb | 1 - 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 394af3d..5834443 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,8 +33,8 @@ jobs: ruby: jruby-9.4.3.0 - os: "macos-latest" ruby: jruby-9.4.3.0 -# - os: "windows-latest" -# ruby: jruby-9.4.3.0 + - os: "windows-latest" + ruby: jruby-9.4.3.0 steps: - uses: actions/checkout@v3 @@ -44,7 +44,7 @@ jobs: distribution: 'temurin' java-version: '17' - - uses: uvlad7/oxidize-rb-actions/setup-ruby-and-rust@main + - uses: actions/setup-java@v4 with: ruby-version: ${{ matrix.ruby }} bundler-cache: true @@ -57,16 +57,15 @@ jobs: - name: Lint rust run: cargo clippy && cargo fmt --check - # Temporary disable until new rb-sys gem is released -# - name: Test source gem -# shell: bash -# run: | -# bundle exec rake build -# gem install --verbose pkg/*.gem -# -# if ruby -roxi/test -e "puts Oxi::Test.hello('world!')" | grep "Hello, world!"; then -# echo "Success" -# else -# echo "Failure" -# exit 1 -# fi + - name: Test source gem + shell: bash + run: | + bundle exec rake build + gem install --verbose pkg/*.gem + + if ruby -roxi/test -e "puts Oxi::Test.hello('world!')" | grep "Hello, world!"; then + echo "Success" + else + echo "Failure" + exit 1 + fi diff --git a/Gemfile b/Gemfile index 8399995..a96d6fa 100644 --- a/Gemfile +++ b/Gemfile @@ -6,5 +6,5 @@ gemspec gem "rake", "~> 13.0" gem "rake-compiler", "~> 1.2" -gem "rb_sys", git: "https://github.com/uvlad7/rb-sys", branch: "feature/jruby" +gem "rb_sys", "~> 0.9.65" gem "minitest", "~> 5.16" diff --git a/ext/extconf.rb b/ext/extconf.rb index a0b2fd8..c774cd0 100644 --- a/ext/extconf.rb +++ b/ext/extconf.rb @@ -7,4 +7,4 @@ create_rust_makefile("oxi/test/oxi_test") do |r| r.features = java_p ? %w[jruby] : %w[mri] -end \ No newline at end of file +end diff --git a/ext/oxi/test/OxiTest.java b/ext/oxi/test/OxiTest.java index fc3a631..7aafe66 100644 --- a/ext/oxi/test/OxiTest.java +++ b/ext/oxi/test/OxiTest.java @@ -17,4 +17,4 @@ public class OxiTest { public static IRubyObject hello(ThreadContext context, IRubyObject self, RubyString name) { return RubyString.newString(context.getRuntime(), OxiTest.helloNative(name.asJavaString())); } -} \ No newline at end of file +} diff --git a/lib/oxi/test.rb b/lib/oxi/test.rb index dab5227..4db7c78 100644 --- a/lib/oxi/test.rb +++ b/lib/oxi/test.rb @@ -11,7 +11,6 @@ require "oxi/test/jruby" if RUBY_PLATFORM.include?("java") - module Oxi module Test class Error < StandardError; end From 60564a8ec3f96c64050e0cbf40d84859f5cd9d3f Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Fri, 22 Dec 2023 03:45:04 +0300 Subject: [PATCH 27/32] Fix typo --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5834443..e476c03 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,7 +44,7 @@ jobs: distribution: 'temurin' java-version: '17' - - uses: actions/setup-java@v4 + - uses: oxidize-rb/actions/setup-ruby-and-rust@v1 with: ruby-version: ${{ matrix.ruby }} bundler-cache: true From 108ef876fb5b858dfbe22c24a9bbfc332e4c91a9 Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Sat, 23 Dec 2023 05:16:33 +0300 Subject: [PATCH 28/32] Fix var name --- ext/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/src/lib.rs b/ext/src/lib.rs index 880989e..b02aa30 100644 --- a/ext/src/lib.rs +++ b/ext/src/lib.rs @@ -142,7 +142,7 @@ pub extern "system" fn JNI_OnLoad<'local>(vm: JavaVM, _: *mut c_void) -> jint { name: JString<'local>, ) -> JString<'local>; let hello_ptr = hello_func as *mut c_void; - let build_xml_method = NativeMethod { + let hello_method = NativeMethod { name: JNIString::from("helloNative"), sig: JNIString::from(format!( "({}){}", @@ -151,7 +151,7 @@ pub extern "system" fn JNI_OnLoad<'local>(vm: JavaVM, _: *mut c_void) -> jint { )), fn_ptr: hello_ptr, }; - let Ok(_) = env.register_native_methods(clazz, &[build_xml_method]) else { + let Ok(_) = env.register_native_methods(clazz, &[hello_method]) else { return JNI_ERR; }; JNI_VERSION_1_4 From 681f391f8d317ea40af0d25dea0622156df484a0 Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Sat, 23 Dec 2023 05:24:30 +0300 Subject: [PATCH 29/32] Fix --- ext/oxi/test/OxiTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/oxi/test/OxiTest.java b/ext/oxi/test/OxiTest.java index 7aafe66..b8b4240 100644 --- a/ext/oxi/test/OxiTest.java +++ b/ext/oxi/test/OxiTest.java @@ -7,7 +7,7 @@ import org.jruby.runtime.builtin.IRubyObject; @SuppressWarnings("serial") -@JRubyModule(name = "Xml") +@JRubyModule(name = "OxiTest") public class OxiTest { private static native String helloNative(String name); From da82bdbd6ab51b0b6a6407bc37fc4068fe7a5057 Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Sat, 23 Dec 2023 08:37:37 +0300 Subject: [PATCH 30/32] Fix Cargo.toml deps --- ext/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/Cargo.toml b/ext/Cargo.toml index 5b541f1..3402c15 100644 --- a/ext/Cargo.toml +++ b/ext/Cargo.toml @@ -15,7 +15,7 @@ rb-sys = { version = "0.9", features = [ "bindgen-deprecated-types", "stable-api-compiled-fallback", ], optional = true } -jni = { version = "*", optional = true } +jni = { version = "0.19.0", optional = true } robusta_jni = { version = "0.2.2", optional = true } [build-dependencies] From 3c08b4a605d6b00e616682286148b431c6f5845b Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Sat, 23 Dec 2023 23:33:34 +0300 Subject: [PATCH 31/32] Fix gemspec, add jar --- oxi-test.gemspec | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/oxi-test.gemspec b/oxi-test.gemspec index d18d478..2db1917 100644 --- a/oxi-test.gemspec +++ b/oxi-test.gemspec @@ -21,7 +21,9 @@ Gem::Specification.new do |spec| ext_files = %w[rs rb] ext_files << "java" if java_p - spec.files = Dir["lib/**/*.rb", "ext/**/*.{#{ext_files.join(",")}}", + lib_files = %w[rb] + lib_files << "jar" if java_p + spec.files = Dir["lib/**/*.{#{lib_files.join(",")}}", "ext/**/*.{#{ext_files.join(",")}}", "Cargo.lock", "Cargo.toml", "ext/Cargo.toml", "LICENSE.txt", "README.md"] spec.bindir = "exe" spec.executables = [] From 6ec4e5d46014e04ff656d4bbd0e0ad6196d3066e Mon Sep 17 00:00:00 2001 From: uvlad7 Date: Mon, 25 Dec 2023 05:08:30 +0300 Subject: [PATCH 32/32] Remove java files from sources, only jars are distributed --- oxi-test.gemspec | 1 - 1 file changed, 1 deletion(-) diff --git a/oxi-test.gemspec b/oxi-test.gemspec index 2db1917..5805183 100644 --- a/oxi-test.gemspec +++ b/oxi-test.gemspec @@ -20,7 +20,6 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= 2.3.0" ext_files = %w[rs rb] - ext_files << "java" if java_p lib_files = %w[rb] lib_files << "jar" if java_p spec.files = Dir["lib/**/*.{#{lib_files.join(",")}}", "ext/**/*.{#{ext_files.join(",")}}",