-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: Initial migration to rquickjs * More progress on migration: Migrated the `Console` and `Random` APIs; got to the `StreamIO` API. * Migrate all of the APIs to use rquickjs This commit migrates the `console`, `stream_io`, `random` and `text_encoding` APIs to use rquickjs. One notable change in this commit is the introduction of the `Args` struct to tie the lifetime of `Ctx<'js>` and `Rest<Value<'js>>` arguments, given that explicit lifetime binding is not possible in Rust closures at the moment. * Finalize migration of the toolchain as a whole * Uncomment integration tests * Throw `TypeError` when `fatal` decoding is specified. * Fix clippy errors * Update documentation where applicable and remove reference to older crates * Handle converting from JS strings to Rust strings for text encoding * Migrate serializers and deserializers to use rquickjs and move them under the javy crate * Move quickcheck to the top level Cargo.toml `cargo` doesn't seem to pickup the fact that `quickcheck` is only used in tests. * Update versions and CHANGELOGs * Audit dependencies And prune unused ones * Improve safety comment * Port previous implementation of read sync I wanted to avoid as much as possible using the underlying QuickJS unsafe APIs, but that introduced behavioral changes and bugs. So for now, I'm sticking to the previous implementation. * Review comments * Improve comment for why `ManuallyDrop` * Use `ManuallyDrop` for Context * Format comments where applicable * Use rquickjs 0.6.0 through a fork This commit introduces rquickjs 0.6.0 through a fork that contains Wasm specific performance improvements. We intend to upstream these improvements. * Cargo vet config Adds special policy for rquickjs given that we're currently in a fork. * Remove comment, cargo vet doesn't like them
- Loading branch information
1 parent
8f4468c
commit 235b5a6
Showing
30 changed files
with
3,622 additions
and
1,621 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,54 @@ | ||
use anyhow::Result; | ||
use javy::{quickjs::JSValue, Runtime}; | ||
use anyhow::{Error, Result}; | ||
use javy::{ | ||
quickjs::{prelude::Func, Object}, | ||
Runtime, | ||
}; | ||
|
||
use crate::{APIConfig, JSApiSet}; | ||
|
||
pub struct Random; | ||
|
||
impl JSApiSet for Random { | ||
fn register(&self, runtime: &Runtime, _config: &APIConfig) -> Result<()> { | ||
let ctx = runtime.context(); | ||
ctx.global_object()?.get_property("Math")?.set_property( | ||
"random", | ||
// TODO figure out if we can lazily initialize the PRNG | ||
ctx.wrap_callback(|_ctx, _this, _args| Ok(JSValue::Float(fastrand::f64())))?, | ||
)?; | ||
runtime.context().with(|cx| { | ||
let globals = cx.globals(); | ||
let math: Object<'_> = globals.get("Math").expect("Math global to be defined"); | ||
math.set("random", Func::from(fastrand::f64))?; | ||
|
||
Ok::<_, Error>(()) | ||
})?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{random::Random, APIConfig, JSApiSet}; | ||
use anyhow::Result; | ||
use javy::Runtime; | ||
use anyhow::{Error, Result}; | ||
use javy::{ | ||
quickjs::{context::EvalOptions, Value}, | ||
Runtime, | ||
}; | ||
|
||
#[test] | ||
fn test_random() -> Result<()> { | ||
let runtime = Runtime::default(); | ||
Random.register(&runtime, &APIConfig::default())?; | ||
let ctx = runtime.context(); | ||
ctx.eval_global("test.js", "result = Math.random()")?; | ||
let result = ctx.global_object()?.get_property("result")?.as_f64()?; | ||
assert!(result >= 0.0); | ||
assert!(result < 1.0); | ||
runtime.context().with(|this| { | ||
let mut eval_opts = EvalOptions::default(); | ||
eval_opts.strict = false; | ||
this.eval_with_options("result = Math.random()", eval_opts)?; | ||
let result: f64 = this | ||
.globals() | ||
.get::<&str, Value<'_>>("result")? | ||
.as_float() | ||
.unwrap(); | ||
assert!(result >= 0.0); | ||
assert!(result < 1.0); | ||
Ok::<_, Error>(()) | ||
})?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.