mastodon.world is one of the many independent Mastodon servers you can use to participate in the fediverse.
Generic Mastodon server for anyone to use.

Server stats:

8.1K
active users

#rustyscript

0 posts0 participants0 posts today

#rustlang #rust #rustyscript

I'm trying to access a function exported by a module in an eval function but I can't find how to do it. Is there a way to do it ?

I guess I could just evaluate the module content, but it seems not optimal.

ex :


let mut rt = Runtime::new(Default::default()).unwrap();
let module_flop = Module::new("flop.mjs", "const flop = () => { console.log('flop'); }; export default flop;");
let flop_handle = rt.load_module(&module_flop).unwrap();
// rt.eval::<()>("import {flop} from './flop.js'"); // import are not allowed here...
rt.eval::<()>("flop();").unwrap();

So in my quest to execute and stop a js script from a rust application, using rustyscript, I made some progress.

It's a bit awkward but with a combinaison of tokio::task::spawn_blocking and static_runtime from rustyscript, I can run a script line by line and stop between lines.

The trouble is I can't load a module and use it in eval functions. And if I make a module per line, I don't want to have to somehow export and re-import my context each for each line.

And Iced-rs is of the hook for now. I may or may not rant about it when I get to refactor the whole mess.

deno-ast has a tagged version that don't compile, which is used by rustyscript in one of it's default features.

How the fuck do you let this happen ???

I don't necessarily blame rustyscript here since it merely use deno and it's easy to let this kind of bug slide into your project...

But deno? Hello ? Why are you tagging code that don't compile ???

#rust #rustlang #tokio #rustyscript

I don't understand why I can't abort a thread in which I construct, setup then use a rustyscript runtime. The runtime stays in this single thread. Yet the program crash because there is a block_on instruction in the load_module function?

I can stop a script by killing the application but I can't abort the thread?

This seems kinda dumb.

So...

You might have read me tell you about how I was... not having a great time trying to make #rustyscript and #iced_rs work well together and failing....

Specifically I couldn't find a way to stop the execution of a script. (I have script with long pauses waiting for stuff to happen IRL, and when the script is wrong, having to either close the application or wait until the end of the script is not great).

Turns out... I can't do that. Either because rustyscript's "load_modules" or "eval" can't be cancelled... Or maybe because a function I declare for the runtime use a blocking operation.

I still have some tests to do, but more to the point I need to really up my #tokio game.

Continued thread

I mean there are probably difficult threading stuff happening in both iced-rs and rustyscript...

And I'm trying to tie them up with tokio, maybe adding a level of complexity...

Still, I don't think this needs be such a headhache.
I'm not even sure who to ask for help at this point.

I need to know why this code is fine :

let a : Arc<RwLock<i32>> = Arc::new(1.into());
let toto = async_callback!( |_args: Vec<Value>| async move {
println!("{}", a.read().await);
Ok::<Value, rustyscript::Error>(Value::Null)
});
toto(Vec::new()).await;

and this one isn't:

let mut js_executor = Runtime::new(Default::default())?;

let a : Arc<RwLock<i32>> = Arc::new(1.into());

js_executor.register_async_function("toto", async_callback!( |_args: Vec<Value>| async move {
println!("{}", a.read().await);
Ok::<Value, rustyscript::Error>(Value::Null)
})
)?;

This is driving me nuts.

I'm yet again defeated by iced-rs and threads management...

What I want to do is let the application run a js script (for reasons) that can send tcp request to a device (again, for reasons). And I have this working. It's a bit of a mess but it works.

Only now I want to be able to stop the script (without alt-F4 the whole application) because the script can take some time to execute and it's annoying when there is an error in the script.

Only rustyscript doesn't have a mechanism for that, so right now I'm resorting to make a subscriber for the js execution. Which is a pain, but I think I more or less got it...

Except...

#rust #rustlang

I'm trying to make #rustyscript work with #iced-rs but it doesn't seems to want to play nice.

Specifically I want to need able to run a js script with rustyscript and keep the option to stop the script when I want. But Rustyscript Runtime doesn't play well with iced-rs' own concurrency mechanisms.

The other difficulty is I need to register rust function in the js runtime, and I don't find a way to do that using Workers.

I'm basically running circle around the problem... I got something to compile but the js execution thread crashes with this message :

Cannot start a runtime from within a runtime. This happens because a function (likeblock_on) attempted to block the current thread while the thread is being used to drive asynchronous tasks.