dyon icon indicating copy to clipboard operation
dyon copied to clipboard

Passing objects from Rust to the script should be easier

Open valpackett opened this issue 7 years ago • 11 comments

Hi. I'm currently working on a little tool that uses Dyon as a scripting engine. I have an existing Rust object that needs to be passed to the script. This is what I came up with to add a current object to main:

    let current_name: Arc<String> = Arc::new("some_thing".into());
    match module.find_function(&Arc::new("main".into()), 0) {
        FnIndex::Loaded(i) => module.functions[i as usize].currents.push(Current {
            name: current_name.clone(),
            source_range: Range::empty(0),
            mutable: false,
        }),
        x => panic!("Weird main function: {:?}", x),
    }
    let mut rt = Runtime::new();
    rt.local_stack.push((current_name.clone(), rt.stack.len()));
    rt.current_stack.push((current_name, rt.stack.len()));
    rt.stack.push(Variable::RustObject(Arc::new(Mutex::new(my_object_thingy)) as RustObject));

Would be nice to have an easier method to do this. Without importing range, pushing to three runtime stacks, explicitly finding main

UPDATE: published the initial commit of the project! This thing has been extracted into a macro there. Would be nice to see macros like these in dyon itself…

valpackett avatar Dec 02 '17 12:12 valpackett

While on this topic of the difficulty locating and invoking arbitrary functions, is it at all possible in Dyon to do this sort of thing to a non-main function? I'm looking for some functionality like Dyon-based event handlers where I can invoke particular functions by name, passing in current state data. Will move this question to another issue if I'm too off-topic.

dkushner avatar Dec 05 '17 17:12 dkushner

Yeah I'm interested in that too, for other projects… :)

I think you should be able to add current objects to any function using this code. But I haven't looked into invoking an arbitrary function.

Also, would be nice to be able to register arbitrary event handlers from main, including closures, not just named functions.

I'm surprised there wasn't much work in this direction, this is a very common use case for any embeddable scripting interpreter…

valpackett avatar Dec 05 '17 18:12 valpackett

So, this is a naive solution:

let mut module = Module::new();
error(load_str(&*asset.name, Arc::new(asset.source.clone()), &mut module));
error(self.host.call_str("init", &[], &Arc::new(module.clone())));

dkushner avatar Dec 05 '17 22:12 dkushner

Notice that it is possible to use the current library.

bvssvni avatar Dec 06 '17 02:12 bvssvni

@bvssvni: perhaps I'm misunderstanding its application but how would that library interact with a Dyon script I am executing from my Rust code? It would seem that what that library does is essentially create global values which, while acceptable in a scripting language, seems like terribly bad form for Rust. It also claims to be "safe" but I'm unsure in what sense this is meant, since every example of its use references unsafe code.

dkushner avatar Dec 11 '17 01:12 dkushner

I see there's a Call thingy now! :) but it doesn't support currents, only arguments

valpackett avatar Feb 19 '18 19:02 valpackett

You can you do this:

fn main(a: any) {
    ~ a := a // Set current object.
    ...
}

bvssvni avatar Feb 19 '18 20:02 bvssvni

main is written by the user, my API is that they get currents from my Rust code

valpackett avatar Feb 19 '18 21:02 valpackett

OK. Perhaps we could add some features to Call to make it support current objects?

bvssvni avatar Feb 19 '18 21:02 bvssvni

Exactly, that's precisely what I want

valpackett avatar Feb 19 '18 21:02 valpackett

Do you want to try designing this thing?

bvssvni avatar Feb 19 '18 21:02 bvssvni