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

use rquickjs for release builds #1823

Merged
merged 8 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 136 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ snafu = "0.8.0"
validator = { version = "0.16", features = ["derive"] }
zxcvbn = "2"
deno_core = "0.272.0"
rquickjs = { version = "0.5", features = ["macro"] }
quick-js = "0.4"

[workspace.dependencies.uuid]
version = "1.6.1"
Expand Down
14 changes: 12 additions & 2 deletions fastn-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,26 @@ license.workspace = true
repository.workspace = true
homepage.workspace = true

[features]
default = ["quickjs"]
deno-core = ["deno_core"]
quickjs = ["rquickjs", "quick-js"]

[dependencies]
#rquickjs = { workspace = true, optional = true }
pretty.workspace = true
itertools.workspace = true
indoc.workspace = true
fastn-grammar.workspace = true
prettify-js.workspace = true
camino.workspace = true
deno_core.workspace = true
deno_core = { workspace = true, optional = true }
thiserror.workspace = true

[target.'cfg(not(windows))'.dependencies]
quick-js = { workspace = true, optional = true }

[target.'cfg(windows)'.dependencies]
rquickjs = { workspace = true, optional = true }

[dev-dependencies]
indoc.workspace = true
74 changes: 64 additions & 10 deletions fastn-js/src/ssr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(not(feature = "quickjs"))]
const DELETE_DENO: &str = "delete Deno;";

#[derive(thiserror::Error, Debug)]
Expand All @@ -12,23 +13,76 @@ pub enum SSRError {
type Result<T> = std::result::Result<T, SSRError>;

pub fn run_test(js: &str) -> Result<Vec<bool>> {
let mut runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions::default());

eval::<Vec<bool>>(
&mut runtime,
deno_core::FastString::from(format!("{DELETE_DENO}{js}")),
)
#[cfg(feature = "quickjs")]
{
#[cfg(target_os = "windows")]
{
Ok(rquickjs::Context::full(&rquickjs::Runtime::new().unwrap())
.unwrap()
.with(|ctx| ctx.eval::<Vec<bool>, _>(js).unwrap()))
}
#[cfg(not(target_os = "windows"))]
{
// Added logging support from console from within context
let context = quick_js::Context::builder()
.console(
|level: quick_js::console::Level, args: Vec<quick_js::JsValue>| {
eprintln!("{}: {:?}", level, args);
},
)
.build()
.unwrap();
Ok::<Vec<bool>, SSRError>(context.eval_as::<Vec<bool>>(js).unwrap())
}
}
#[cfg(not(feature = "quickjs"))]
{
let mut runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions::default());
eval::<Vec<bool>>(
&mut runtime,
deno_core::FastString::from(format!("{DELETE_DENO}{js}")),
)
}
}

pub fn ssr_str(js: &str) -> Result<String> {
let mut runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions::default());

let all_js = fastn_js::all_js_with_test();
let js = format!("{DELETE_DENO}{all_js}{js}");

eval::<String>(&mut runtime, deno_core::FastString::from(js))
#[cfg(feature = "quickjs")]
{
let js = format!("{all_js}{js}");

#[cfg(target_os = "windows")]
{
Ok(rquickjs::Context::full(&rquickjs::Runtime::new().unwrap())
.unwrap()
.with(|ctx| ctx.eval::<String, _>(js).unwrap()))
}
#[cfg(not(target_os = "windows"))]
{
// Added logging support from console from within context
let context = quick_js::Context::builder()
.console(
|level: quick_js::console::Level, args: Vec<quick_js::JsValue>| {
eprintln!("{}: {:?}", level, args);
},
)
.build()
.unwrap();
Ok::<std::string::String, SSRError>(context.eval_as::<String>(js.as_str()).unwrap())
}
}
#[cfg(not(feature = "quickjs"))]
{
let mut runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions::default());

let js = format!("{DELETE_DENO}{all_js}{js}");

eval::<String>(&mut runtime, deno_core::FastString::from(js))
}
}

#[cfg(not(feature = "quickjs"))]
fn eval<T: deno_core::serde::Deserialize<'static>>(
context: &mut deno_core::JsRuntime,
code: deno_core::FastString,
Expand Down
4 changes: 4 additions & 0 deletions fastn.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ rustPlatform.buildRustPackage {

nativeBuildInputs = [ pkg-config ];

# TODO:use deno_core. can't do this until we can build rusty_v8 with musl
# libc
buildFeatures = [ "quickjs" ];

buildInputs = lib.optional stdenv.targetPlatform.isWindows [
windows.mingw_w64_pthreads
windows.pthreads
Expand Down
Loading