Skip to content

Commit

Permalink
feat: moved performance to native
Browse files Browse the repository at this point in the history
  • Loading branch information
samir-byte committed Sep 10, 2024
1 parent 9987d11 commit 1557582
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
10 changes: 0 additions & 10 deletions apps/yasumu/src-tauri/runtime/00_performance.ts

This file was deleted.

3 changes: 2 additions & 1 deletion apps/yasumu/src-tauri/src/tanxium/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use tauri::Manager;
mod crypto;
mod typescript;
mod yasumu_runtime;
mod performance;

use crate::commands::workspace::WorkspaceState;
use typescript::transpile_typescript;

fn setup_runtime(ctx: &mut Context, app: tauri::AppHandle) {
let path = app.path();
let runtime_files = vec![
"runtime/00_performance.ts",
"runtime/01_headers.js",
"runtime/02_runtime.ts",
"runtime/03_console.ts",
Expand Down Expand Up @@ -64,6 +64,7 @@ pub async fn evaluate_javascript(

crypto::crypto_init(&mut ctx);
yasumu_runtime::runtime_init(&mut ctx, current_workspace, app.clone(), id, ts_supported, test.unwrap_or(false));
performance::performance_init(&mut ctx);

// enable strict mode
ctx.strict(true);
Expand Down
34 changes: 34 additions & 0 deletions apps/yasumu/src-tauri/src/tanxium/performance/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use boa_engine::{
js_str, js_string, object::ObjectInitializer, property::Attribute, Context, JsValue,
NativeFunction,
};

pub fn performance_init(context: &mut Context) {
let time_origin = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs_f64();
let performance = ObjectInitializer::new(context)
.function(
NativeFunction::from_fn_ptr(|_, _, _| {
let result = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs_f64();

Ok(JsValue::Rational(result))
}),
js_string!("now"),
0,
)
.property(
js_str!("timeOrigin"),
JsValue::Rational(time_origin),
Attribute::all(),
)
.build();

context
.register_global_property(js_str!("performance"), performance, Attribute::all())
.unwrap();
}
15 changes: 1 addition & 14 deletions apps/yasumu/src-tauri/src/tanxium/yasumu_runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use boa_engine::{
js_str, js_string, object::ObjectInitializer, property::Attribute, Context, JsString, JsValue,
NativeFunction,
js_str, object::ObjectInitializer, property::Attribute, Context, JsString, JsValue,
};

pub fn runtime_init(
Expand Down Expand Up @@ -64,18 +63,6 @@ pub fn runtime_init(
.property(js_str!("features"), app_script_features, Attribute::all())
.property(js_str!("versions"), yasumu_version, Attribute::all())
.property(js_str!("workspace"), yasumu_workspace, Attribute::all())
.function(
NativeFunction::from_fn_ptr(|_, _, _| {
let result = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs_f64();

Ok(JsValue::Rational(result))
}),
js_string!("nanoseconds"),
0,
)
.build();

context
Expand Down

0 comments on commit 1557582

Please sign in to comment.