Skip to content

Commit

Permalink
don't eval in to_string_value to improve performace
Browse files Browse the repository at this point in the history
  • Loading branch information
lublak committed Nov 28, 2024
1 parent 252b4b0 commit 454b519
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
9 changes: 2 additions & 7 deletions src/value.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use rquickjs::{context::EvalOptions, CatchResultExt, Coerced, FromJs};
use rquickjs::{context::EvalOptions, CatchResultExt};
use serde::{Deserialize, Serialize};

#[derive(Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -106,12 +106,7 @@ impl JSBytesValue {
match type_field_value.as_ref() {
"eval" => {
if let Some(JSBytesValue::String(js)) = value.get("value") {
let mut options = EvalOptions::default();
options.global = true;
return ctx.eval_with_options::<rquickjs::Value, _>(js.to_owned(), options)
.catch(&ctx)
.map(|v| Coerced::<rquickjs::String<'js>>::from_js(ctx, v).map(|v| v.to_string().map_err(|e| e.to_string())).map_err(|e| e.to_string())?)
.map_err(|e| format!("eval error: {}", e.to_string()))?
return Ok(js.to_owned())
} else {
return Err("eval typed values needs to be a string".to_string())
}
Expand Down
Binary file modified typst-package/ctxjs.wasm
Binary file not shown.
6 changes: 6 additions & 0 deletions typst-package/main.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#import "lib.typ" as ctxjs

#{
_ = ctxjs.create-context("test")
let test = ctxjs.eval-format("test", "((callback) => {{ return callback(); }})({test})", ("test":ctxjs.eval-later("test", "() => {return 5;}"),))
}

0 comments on commit 454b519

Please sign in to comment.