Skip to content

Commit

Permalink
feat: abstract JitStatus struct for luau.load
Browse files Browse the repository at this point in the history
* Creates a new struct called `JitStatus` in lune_utils, which is placed
  in `AppData` during initialization.
* The luau built-in now respects the JIT preference by using
  `JitStatus`.
  • Loading branch information
CompeyDev committed Oct 17, 2024
1 parent 6c39190 commit 1025bb7
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 51 deletions.
94 changes: 47 additions & 47 deletions Cargo.lock

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

10 changes: 8 additions & 2 deletions crates/lune-std-luau/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use mlua::prelude::*;

use lune_utils::TableBuilder;
use lune_utils::{jit::JitStatus, TableBuilder};

mod options;

Expand Down Expand Up @@ -78,7 +78,13 @@ fn load_source<'lua>(
// changed, otherwise disable JIT since it'll fall back anyways
lua.enable_jit(options.codegen_enabled && !env_changed);
let function = chunk.into_function()?;
lua.enable_jit(true);
lua.enable_jit(
lua.app_data_ref::<JitStatus>()
.ok_or(LuaError::runtime(
"Failed to get current JitStatus ref from AppData",
))?
.enabled(),
);

Ok(function)
}
28 changes: 28 additions & 0 deletions crates/lune-utils/src/jit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#[derive(Debug, Clone, Copy, Default)]
pub struct JitStatus(bool);

impl JitStatus {
pub fn new(enabled: bool) -> Self {
Self(enabled)
}

pub fn set_status(&mut self, enabled: bool) {
self.0 = enabled;
}

pub fn enabled(self) -> bool {
self.0
}
}

impl From<JitStatus> for bool {
fn from(val: JitStatus) -> Self {
val.enabled()
}
}

impl From<bool> for JitStatus {
fn from(val: bool) -> Self {
Self::new(val)
}
}
1 change: 1 addition & 0 deletions crates/lune-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod table_builder;
mod version_string;

pub mod fmt;
pub mod jit;
pub mod path;

pub use self::table_builder::TableBuilder;
Expand Down
11 changes: 9 additions & 2 deletions crates/lune/src/rt/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
},
};

use lune_utils::jit::JitStatus;
use mlua::prelude::*;
use mlua_luau_scheduler::{Functions, Scheduler};
use self_cell::self_cell;
Expand Down Expand Up @@ -100,6 +101,7 @@ impl RuntimeInner {
*/
pub struct Runtime {
inner: RuntimeInner,
jit_status: JitStatus,
}

impl Runtime {
Expand All @@ -113,6 +115,7 @@ impl Runtime {
pub fn new() -> Self {
Self {
inner: RuntimeInner::create().expect("Failed to create runtime"),
jit_status: JitStatus::default(),
}
}

Expand All @@ -134,8 +137,8 @@ impl Runtime {
Enables or disables JIT compilation.
*/
#[must_use]
pub fn with_jit(self, enabled: bool) -> Self {
self.inner.lua().enable_jit(enabled);
pub fn with_jit(mut self, jit_status: impl Into<JitStatus>) -> Self {
self.jit_status = jit_status.into();
self
}

Expand Down Expand Up @@ -164,6 +167,10 @@ impl Runtime {
eprintln!("{}", RuntimeError::from(e));
});

// Enable / disable the JIT as requested and store the current status as AppData
lua.set_app_data(self.jit_status);
lua.enable_jit(self.jit_status.enabled());

// Load our "main" thread
let main = lua
.load(script_contents.as_ref())
Expand Down

0 comments on commit 1025bb7

Please sign in to comment.