awslabs / llrt

LLRT (Low Latency Runtime) is an experimental, lightweight JavaScript runtime designed to address the growing demand for fast and efficient Serverless applications.
Apache License 2.0
8.04k stars 355 forks source link

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

Closed i18nsite closed 2 months ago

i18nsite commented 4 months ago

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?

richarddavison commented 4 months ago

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 commented 2 months ago

@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