llrt icon indicating copy to clipboard operation
llrt copied to clipboard

Is there any example code on how to use llrt as an embedded js engine?

Open i18nsite opened this issue 1 year ago • 1 comments

for example:

import {readFileSync} from 'fs'
export default (fp)=>readFileSync(fp);

How should I embed llrt and call the default function and get the return value in rust?

How should I inject native functions into llrt using rust?

i18nsite avatar May 12 '24 10:05 i18nsite

Hi @i18n-now

The best example right now would be to look at: https://github.com/awslabs/llrt/blob/main/llrt/src/main.rs

However I don't think we re-export everything you'd need from rquickjs binding lib.

To get return value in rust you have to access the js context and it can be done like this:

  let vm = Vm::new().await?;
  vm.ctx.with(|ctx| => {
     
     ctx.globals().set("myFunction",|myValue:String|{
          println!("Hello from JS: {}",myValue);
     })
     
  }).await;

richarddavison avatar May 15 '24 19:05 richarddavison

@i18n-now we're working on decoupling LLRT from the AWS dependencies and publishing the crates. That will make it a bit simpler to integrate. I'll close this issue as it's being tracked here: https://github.com/awslabs/llrt/issues/351

richarddavison avatar Jul 12 '24 21:07 richarddavison