-
Notifications
You must be signed in to change notification settings - Fork 109
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
Add a helper for asserting kwargs shape and extracting params #2091
Comments
Use this new converter function in |
This ask is related to #2092. We should probably add a converter function specific to |
I've been playing around a little bit with As per the description, there is validation on the kwargs. What I find interesting is this though: [3.1.2] > String.new("hello", 1)
(irb):2:in `initialize': wrong number of arguments (given 2, expected 0..1) (ArgumentError)
from (irb):2:in `new'
from (irb):2:in `<main>'
from /home/ben/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/irb-1.4.1/exe/irb:11:in `<top (required)>'
from /home/ben/.rbenv/versions/3.1.2/bin/irb:25:in `load'
from /home/ben/.rbenv/versions/3.1.2/bin/irb:25:in `<main>' Specifically, it doesn't look like This lead me down some rabbit holes, and found some fun things in the vendored mruby source:
☝️ The things surrounded in And then to do a sanity check, it seems that kwargs should always be last in the arguments: [3.1.2] > Time.at(0, 100, in: "Z")
=> 1970-01-01 00:00:00.0001 UTC
[3.1.2] > Time.at(0, in: "Z", 100)
/home/ben/.rbenv/versions/3.1.2/lib/ruby/3.1.0/irb/workspace.rb:119:in `eval': (irb):20: syntax error, unexpected ')', expecting => (SyntaxError)
Time.at(0, in: "Z", 100)
^
from /home/ben/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/irb-1.4.1/exe/irb:11:in `<top (required)>'
from /home/ben/.rbenv/versions/3.1.2/bin/irb:25:in `load'
from /home/ben/.rbenv/versions/3.1.2/bin/irb:25:in `<main>' And I see that right now, artichoke doesn't have any extraction methods for pulling out I'm also not sure how/where If so, how do you feel about this psuedo-ish code for extracting the unsafe extern "C" fn string_initialize(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
let s = mrb_get_args!(mrb, optional = 1);
// validation of the kwargs
let kwargs = mrb_get_args!(mrb, kwargs = "capacity,encoding"); // Note: I need to research how this is meant to work
unwrap_interpreter!(mrb, to => guard);
let value = Value::from(slf);
let s = s.map(Value::from);
// types included for brevity (wouldn't expect these to be needed)
let capacity: Option<Value> = kwargs.get("capacity");.
let encoding: Option<Value> = kwargs.get("encoding");
let result = trampoline::initialize(&mut guard, value, capacity, encoding, s);
match result {
Ok(value) => value.inner(),
Err(exception) => error::raise(guard, exception),
}
} |
Ah, I stumbled upon the actual The most interesting part (their examples are very nice too): struct mrb_kwargs
{
uint32_t num; /* number of keyword arguments */
uint32_t required; /* number of required keyword arguments */
const mrb_sym *table; /* C array of symbols for keyword names */
mrb_value *values; /* keyword argument values */
mrb_value *rest; /* keyword rest (dict) */
}; Which leans me more towards this kind of macro: let kwargs: OptionalKwargs = mrb_get_args!(mrb, required = false, ["capacity","encoding"]);
let capacity: Option<Value> = kwargs.get("capacity");
let rest: Option<Value> = kwargs.rest(); // Should unbox to Hash
// I believe `mrb_get_args` will enforce the argument to exist, so it feels "safe" to not wrap in Option
let kwargs: RequiredKwargs = mrb_get_args!(mrb, required = true, ["capacity","encoding"]);
let capacity: Value = kwargs.get("capacity");
let encoding: Value = kwargs.get("encoding");
//let rest: Value = kwargs.rest(); Assumption this would not work, since required forces checking of all args ☝️ Could just be |
Quick note here, |
As a tip, grepping for |
Agreed per the conversation we had on Discord. This looks like we should extract this to the
convert
module.String::new
wants the same functionality with itscapacity:
kwarg:Originally posted by @lopopolo in #1956 (comment)
The text was updated successfully, but these errors were encountered: