dyon
dyon copied to clipboard
Passing objects from Rust to the script should be easier
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…
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.
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…
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())));
Notice that it is possible to use the current library.
@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.
I see there's a Call
thingy now! :) but it doesn't support currents
, only arguments
You can you do this:
fn main(a: any) {
~ a := a // Set current object.
...
}
main
is written by the user, my API is that they get currents from my Rust code
OK. Perhaps we could add some features to Call
to make it support current objects?
Exactly, that's precisely what I want
Do you want to try designing this thing?