Ruby support #1

Open
jaroslaw wants to merge 165 commits from ruby into main
3 changed files with 16 additions and 5 deletions
Showing only changes of commit 07331f8d48 - Show all commits

6
.cargo/config.toml Normal file
View file

@ -0,0 +1,6 @@
[build]
# Without this flag, when linking static libruby, the linker removes symbols
# (such as `_rb_ext_ractor_safe`) which it thinks are dead code... but they are
# not, and they need to be included for the `embed` feature to work with static
# Ruby.
rustflags = ["-C", "link-dead-code=on", "-C", "link-arg=-lz"]

View file

@ -33,7 +33,6 @@ mlua = { version = "0.9.8", features = [
], optional = true } ], optional = true }
magnus = { version = "0.7.1", optional = true } magnus = { version = "0.7.1", optional = true }
rb-sys = { version = "*", default-features = false, features = ["link-ruby", "ruby-static"], optional = true } rb-sys = { version = "*", default-features = false, features = ["link-ruby", "ruby-static"], optional = true }
openssl-sys = { version = "0.9", features = ["vendored"] }
crossbeam-channel = "0.5.15" crossbeam-channel = "0.5.15"
[[example]] [[example]]

View file

@ -21,16 +21,14 @@ use magnus::{
DataType, DataTypeFunctions, IntoValue, Object, RClass, RModule, Ruby, TryConvert, TypedData, DataType, DataTypeFunctions, IntoValue, Object, RClass, RModule, Ruby, TryConvert, TypedData,
}; };
use magnus::{method, prelude::*}; use magnus::{method, prelude::*};
use rb_sys::{ use rb_sys::{ruby_finalize, ruby_init_stack, VALUE};
ruby_exec_node, ruby_finalize, ruby_init_stack, ruby_options, ruby_process_options, VALUE,
};
use serde::Deserialize; use serde::Deserialize;
use crate::{ use crate::{
assets::GetExtensions, assets::GetExtensions,
callback::{FromRuntimeValueWithEngine, IntoRuntimeValueWithEngine}, callback::{FromRuntimeValueWithEngine, IntoRuntimeValueWithEngine},
promise::Promise, promise::Promise,
FuncArgs, Runtime, ScriptingError, ENTITY_VAR_NAME, FuncArgs, Runtime, ScriptingError,
}; };
#[derive(Resource)] #[derive(Resource)]
@ -74,10 +72,18 @@ impl RubyThread {
let (sender, receiver) = crossbeam_channel::unbounded::<Box<dyn FnOnce(Ruby) + Send>>(); let (sender, receiver) = crossbeam_channel::unbounded::<Box<dyn FnOnce(Ruby) + Send>>();
let handle = thread::spawn(move || { let handle = thread::spawn(move || {
let argc: i32 = 0;
let argv = vec![CString::new("ruby").unwrap(), CString::new("-e").unwrap()];
let mut argv = argv
.iter()
.map(|cs| cs.as_ptr() as *mut _)
.collect::<Vec<_>>();
unsafe { unsafe {
let mut variable_in_this_stack_frame: VALUE = 0; let mut variable_in_this_stack_frame: VALUE = 0;
ruby_init_stack(&mut variable_in_this_stack_frame as *mut VALUE as *mut _); ruby_init_stack(&mut variable_in_this_stack_frame as *mut VALUE as *mut _);
rb_sys::ruby_init(); rb_sys::ruby_init();
rb_sys::ruby_options(argc, argv.as_mut_ptr());
}; };
while let Ok(f) = receiver.recv() { while let Ok(f) = receiver.recv() {
let ruby = Ruby::get().expect("Failed to get a handle to Ruby API"); let ruby = Ruby::get().expect("Failed to get a handle to Ruby API");