Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Markus Westerlind
@Marwes
No, it only reflects oneargument
No reason there couldn't be a fn call_vararg(&self, args: &[T]) -> Result<R> on Function however
Markus Westerlind
@Marwes
You can get a function like FunctionRef<Hole> and call that. Just make sure that you have checked that the function you call has the correct number of arguments and argument types before calling into it
Snarpix
@Snarpix
Thank you! It really helps)
Snarpix
@Snarpix

I have found a way to convert serde_json::Value to gluon::JsonValue.
But I can't find a way to convert JsonValue to something I can examine, without converting it to String.
JsonValue member is private, so I can't write a conversion function myself.

    let thread = gluon::new_vm();
    // To avoid deadlock
    // Workaround for https://github.com/gluon-lang/gluon/issues/805
    thread.load_script("<pre-imports>", r#"
        import! std.json.de
        ()
    "#).unwrap();
    let j: serde_json::Value = json!({"name": "Name", "value": 1});
    let gj: gluon::vm::api::json::JsonValue = {
        let mut context = thread.current_context();
        JsonValue::deserialize_state(&mut context, j).unwrap()
    };

Is there a way to convert JsonValue to serde_json::Value or something?

Snarpix
@Snarpix
Oh, I have noticed pull request just now. It seems to fix the problem. Thanks!
Snarpix
@Snarpix
Hi!
I tried to use call_vararg function, but run into that OwnedFunction<Hole> is not Clone. Is it fundamental limitation?
I am using clone to work around gluon-lang/gluon#771
error[E0599]: no method named `clone` found for type `gluon_vm::api::function::Function<gluon_vm::thread::RootedThread, gluon_vm::api::Hole>` in the current scope
  --> src/vm/mod.rs:94:40
   |
94 |                 let mut func = vm.func.clone();
   |                                        ^^^^^ method not found in `gluon_vm::api::function::Function<gluon_vm::thread::RootedThread, gluon_vm::api::Hole>`
   |
   = note: the method `clone` exists but the following trait bounds were not satisfied:
           `gluon_vm::api::function::Function<gluon_vm::thread::RootedThread, gluon_vm::api::Hole> : std::clone::Clone`
Markus Westerlind
@Marwes
No, the clone derive should just be changed to an explicit impl so the bounds can be relaxed https://github.com/gluon-lang/gluon/blob/4e03c0f61f09747d593060d36c8cecbd43b233a7/vm/src/api/function.rs#L129
Happy to take a PR for it!
Hole ought to be Clone as well
Which would indirectly fix it
Snarpix
@Snarpix
I tried to use call_any with Clone implemented for Hole.
I have arbitrary number of arguments with arbitrary types and call_any takes IntoIterator<Item = A : Pushable> - as I understand I should pass Vec<Box<dyn Pushable>>.
But there is no implementation of Pushable trait for Box<dyn Pushable>.
I have tried to add a paththrough impl for Box<T: Pushable>, but it conflicts with Pushable for T: vm::Userdata implementation.
Since we don't have specialization or negative trait bounds in stable Rust - guess I'm out of luck?
Markus Westerlind
@Marwes
:/ right
Hmm, no
Pushable is implemented on Variants/Opaque/ etc so you can give it an iterator of one of those values https://docs.rs/gluon_vm/0.13.1/gluon_vm/api/trait.Pushable.html#implementors
And those are already "dynamically typed" so you can put any value into those
I don't know what kind of dynamic value you have but it should at least be possible to convert it into one of those
Markus Westerlind
@Marwes
Any Pushable value can be put into a RootedValue via https://docs.rs/gluon_vm/0.13.1/gluon_vm/api/trait.Pushable.html#method.marshal which the provides https://docs.rs/gluon_vm/0.13.1/gluon_vm/thread/struct.RootedValue.html#method.get_variant which you can put in your argument iterator
RootedValue should be Pushable on its own however, and there should be a way to marshall into a stack rooted value as well without needing to create a reference counted RootedValue
And the API needs some pruning... Lots of arbitrary methods left over after making changes to support stack rooting
Snarpix
@Snarpix
@Marwes I have types that are Pushable. Int, String, serde_json::Value
Markus Westerlind
@Marwes
Since you have an arbitrary number of arguments I assume they are stored in a Vec<MyEnum> which you want to pass to gluon as arguments
enum MyEnum {
     Int(Int),
    String(String),
  etc 
}
Snarpix
@Snarpix
It's such a shame that Box<dyn Pushable> can't be Pushable. :/ It looks like most straight-forward way.
Markus Westerlind
@Marwes
Maybe, but it forces a Box which isn't ideal
Snarpix
@Snarpix
Yeah, that's right, I could make Pushable Enum.
Markus Westerlind
@Marwes
You can just pass around RootedValue
If you don't need to inspect the values much
If you havea fixed set of things that your arguments may take an enum is the way to go though
I think Box only fails because Box is #[fundamental]. So a newtype wrapper around it would do what you want
Snarpix
@Snarpix
Yeah, enum looks like a good solution, I haven't thought about it
Snarpix
@Snarpix
I want to do a runtime typechecking for gluon function.
I got function type, I'm using arg_iter() to iterate over its argument types. So I want to get gluon type to compare with argument type from arg_iter().
Am I doing it the right way?
fn get_gluon_type(vm: &RootedThread, t: ArgsType) -> Option<ArcType> {
    match t {
        ArgsType::Bool => vm.get_type::<bool>(),
        ArgsType::Byte => vm.get_type::<u8>(),
        ArgsType::I16 => vm.get_type::<VmInt>(),
        ArgsType::I32 => vm.get_type::<VmInt>(),
        ArgsType::I64 => vm.get_type::<VmInt>(),
        ArgsType::U16 => vm.get_type::<VmInt>(),
        ArgsType::U32 => vm.get_type::<VmInt>(),
        ArgsType::U64 => vm.get_type::<VmInt>(),
        ArgsType::Str => vm.get_type::<String>(),
        ArgsType::Float => vm.get_type::<f64>(),
        ArgsType::Json => vm.get_type::<serde_json::Value>(),
    }
}
Markus Westerlind
@Marwes
Call <TYPE as VmType>::make_type(vm) instead
Snarpix
@Snarpix
Thank you very much!
Günter Zöchbauer
@zoechi

Is there a way to run gluon scripts with a hashbang as

./myin.glu

(without gluon?)

When I try, I get

Could not find module '..main'. Searched ..

Günter Zöchbauer
@zoechi
Seems this should be supported since a long time gluon-lang/gluon#304
gluon --version
gluon_repl 0.13.1
Markus Westerlind
@Marwes
It should work but there seems to be a bug in filename -> module translation
Günter Zöchbauer
@zoechi
ok.
So when I run gluon ./main.glu I get the same error (with or without hasbang)
gluon main.glu works.
So the problem is that when I run it directly I need to add a path ./
/home/zoechi/source/gluon_test1/main.glu
works
Markus Westerlind
@Marwes
Yep
Günter Zöchbauer
@zoechi
Tanks!
jakbyte
@jakbyte
can you have inline functions?
Markus Westerlind
@Marwes
Sure! Lambdas via \x -> x and functions can be written with let f x = x in ...
jakbyte
@jakbyte
thanks!
jakbyte
@jakbyte
oh, i was just reading that lol
guess i missed it