Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update magnus 0.6.0 [Attempt #2] #226

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 25 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ winch = ["wasmtime/winch"]

[dependencies]
lazy_static = "1.4.0"
magnus = { version = "0.5.5", features = ["rb-sys-interop"] }
magnus = { version = "0.6", features = ["rb-sys", "deprecated-send-sync-value"] }
rb-sys = { version = "*", default-features = false, features = [
"stable-api-compiled-fallback",
] }
Expand Down
2 changes: 1 addition & 1 deletion ext/src/helpers/symbol_enum.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::static_id::StaticId;
use magnus::{exception::arg_error, Error, Symbol, TryConvert, Value};
use magnus::{exception::arg_error, prelude::*, Error, Symbol, TryConvert, Value};
use std::fmt::Display;

/// Represents an enum as a set of Symbols (using `StaticId`).
Expand Down
6 changes: 3 additions & 3 deletions ext/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use magnus::Error;
use magnus::{Error, Ruby};
mod helpers;
mod ruby_api;

Expand All @@ -12,10 +12,10 @@ pub(crate) use ruby_api::*;
rb_sys::set_global_tracking_allocator!();

#[magnus::init]
pub fn init() -> Result<(), Error> {
pub fn init(ruby: &Ruby) -> Result<(), Error> {
#[cfg(ruby_gte_3_0)]
unsafe {
rb_sys::rb_ext_ractor_safe(true);
}
ruby_api::init()
ruby_api::init(ruby)
}
2 changes: 1 addition & 1 deletion ext/src/ruby_api/caller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'a> Caller<'a> {
/// @def export(name)
/// @see Instance#export
pub fn export(rb_self: Obj<Caller<'a>>, name: RString) -> Result<Option<Extern<'a>>, Error> {
let caller = rb_self.get();
let caller = rb_self;
let inner = caller.handle.get_mut()?;

if let Some(export) = inner.get_export(unsafe { name.as_str() }?) {
Expand Down
4 changes: 2 additions & 2 deletions ext/src/ruby_api/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{define_rb_intern, helpers::SymbolEnum};
use lazy_static::lazy_static;
use magnus::{
exception::{arg_error, type_error},
prelude::*,
r_hash::ForEach,
Error, RHash, Symbol, TryConvert, Value,
};
Expand Down Expand Up @@ -165,8 +166,7 @@ impl TryFrom<ConfigEntry> for String {
impl TryFrom<ConfigEntry> for Option<String> {
type Error = magnus::Error;
fn try_from(value: ConfigEntry) -> Result<Self, Self::Error> {
let val: Option<String> = value.1.try_convert().map_err(|_| value.invalid_type())?;
Ok(val)
<Option<String>>::try_convert(value.1).map_err(|_| value.invalid_type())
}
}

Expand Down
14 changes: 7 additions & 7 deletions ext/src/ruby_api/convert.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{define_rb_intern, err, error, helpers::SymbolEnum};
use lazy_static::lazy_static;
use magnus::{Error, IntoValue, RArray, Symbol, TryConvert, TypedData, Value};
use magnus::{prelude::*, Error, IntoValue, RArray, Ruby, Symbol, TryConvert, TypedData, Value};
use wasmtime::{ExternRef, Val, ValType};

use super::{func::Func, global::Global, memory::Memory, store::StoreContextValue, table::Table};
Expand Down Expand Up @@ -99,18 +99,18 @@ unsafe impl Send for ExternRefValue {}
unsafe impl Sync for ExternRefValue {}

pub trait ToExtern {
fn to_extern(&self) -> Result<wasmtime::Extern, Error>;
fn to_extern(&self, ruby: &Ruby) -> Result<wasmtime::Extern, Error>;
}

impl ToExtern for Value {
fn to_extern(&self) -> Result<wasmtime::Extern, Error> {
if self.is_kind_of(Func::class()) {
fn to_extern(&self, ruby: &Ruby) -> Result<wasmtime::Extern, Error> {
if self.is_kind_of(Func::class(ruby)) {
Ok(<&Func>::try_convert(*self)?.into())
} else if self.is_kind_of(Memory::class()) {
} else if self.is_kind_of(Memory::class(ruby)) {
Ok(<&Memory>::try_convert(*self)?.into())
} else if self.is_kind_of(Table::class()) {
} else if self.is_kind_of(Table::class(ruby)) {
Ok(<&Table>::try_convert(*self)?.into())
} else if self.is_kind_of(Global::class()) {
} else if self.is_kind_of(Global::class(ruby)) {
Ok(<&Global>::try_convert(*self)?.into())
} else {
Err(Error::new(
Expand Down
11 changes: 6 additions & 5 deletions ext/src/ruby_api/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use super::{
};
use crate::error;
use magnus::{
class, function, memoize, method, scan_args, typed_data::Obj, value::Id, Error, Module, Object,
RHash, RString, TryConvert, Value,
class, function, method, prelude::*, scan_args, typed_data::Obj, value::LazyId, Error, Module,
Object, RHash, RString, Ruby, TryConvert, Value,
};
use std::{
collections::hash_map::DefaultHasher,
Expand Down Expand Up @@ -178,15 +178,16 @@ impl Engine {
/// then serialized modules from one engine can be deserialized by the
/// other.
/// @return [String] The hex formatted string that can be used to check precompiled module compatibility.
pub fn precompile_compatibility_key(rb_self: Obj<Self>) -> Result<RString, Error> {
let ivar_id = *memoize!(Id: Id::new("precompile_compatibility_key"));
pub fn precompile_compatibility_key(ruby: &Ruby, rb_self: Obj<Self>) -> Result<RString, Error> {
static ID: LazyId = LazyId::new("precompile_compatibility_key");
let ivar_id = LazyId::get_inner_with(&ID, ruby);

if let Ok(cached) = rb_self.ivar_get::<_, RString>(ivar_id) {
return Ok(cached);
}

let mut hasher = DefaultHasher::new();
let engine = rb_self.get().inner.clone();
let engine = rb_self.inner.clone();
engine.precompile_compatibility_hash().hash(&mut hasher);
let hex_encoded = format!("{:x}", hasher.finish());
let key = RString::new(&hex_encoded);
Expand Down
35 changes: 13 additions & 22 deletions ext/src/ruby_api/errors.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
use crate::ruby_api::root;
use magnus::{gc, Error};
use magnus::{memoize, ExceptionClass, Module};
use magnus::{value::Lazy, Error, ExceptionClass, Module, Ruby};

/// Base error class for all Wasmtime errors.
pub fn base_error() -> ExceptionClass {
*memoize!(ExceptionClass: {
let err = root().const_get("Error").unwrap();
gc::register_mark_object(err);
err
})
static ERR: Lazy<ExceptionClass> = Lazy::new(|_| root().const_get("Error").unwrap());
let ruby = Ruby::get().unwrap();
ruby.get_inner(&ERR)
}

/// Raised when failing to convert the return value of a Ruby-backed Func to
/// Wasm types.
pub fn result_error() -> ExceptionClass {
*memoize!(ExceptionClass: {
let err = root().const_get("ResultError").unwrap();
gc::register_mark_object(err);
err
})
static ERR: Lazy<ExceptionClass> = Lazy::new(|_| root().const_get("ResultError").unwrap());
let ruby = Ruby::get().unwrap();
ruby.get_inner(&ERR)
}

/// Raised when converting an {Extern} to its concrete type fails.
pub fn conversion_error() -> ExceptionClass {
*memoize!(ExceptionClass: {
let err = root().const_get("ConversionError").unwrap();
gc::register_mark_object(err);
err
})
static ERR: Lazy<ExceptionClass> = Lazy::new(|_| root().const_get("ConversionError").unwrap());
let ruby = Ruby::get().unwrap();
ruby.get_inner(&ERR)
}

/// Raised when a WASI program terminates early by calling +exit+.
pub fn wasi_exit_error() -> ExceptionClass {
*memoize!(ExceptionClass: {
let err = root().const_get("WasiExit").unwrap();
gc::register_mark_object(err);
err
})
static ERR: Lazy<ExceptionClass> = Lazy::new(|_| root().const_get("WasiExit").unwrap());
let ruby = Ruby::get().unwrap();
ruby.get_inner(&ERR)
}

#[macro_export]
Expand Down
Loading