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

Passing objects from Rust to the script should be easier #489

Open
valpackett opened this issue Dec 2, 2017 · 11 comments
Open

Passing objects from Rust to the script should be easier #489

valpackett opened this issue Dec 2, 2017 · 11 comments

Comments

@valpackett
Copy link
Contributor

valpackett commented Dec 2, 2017

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…

@dkushner
Copy link

dkushner commented Dec 5, 2017

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.

@valpackett
Copy link
Contributor Author

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…

@dkushner
Copy link

dkushner commented Dec 5, 2017

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())));

@bvssvni
Copy link
Member

bvssvni commented Dec 6, 2017

Notice that it is possible to use the current library.

@dkushner
Copy link

@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.

@valpackett
Copy link
Contributor Author

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

@bvssvni
Copy link
Member

bvssvni commented Feb 19, 2018

You can you do this:

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

@valpackett
Copy link
Contributor Author

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

@bvssvni
Copy link
Member

bvssvni commented Feb 19, 2018

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

@valpackett
Copy link
Contributor Author

Exactly, that's precisely what I want

@bvssvni
Copy link
Member

bvssvni commented Feb 19, 2018

Do you want to try designing this thing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants