Skip to content

Commit

Permalink
big refactors, work towards hlrt
Browse files Browse the repository at this point in the history
  • Loading branch information
avafloww committed Jun 21, 2023
1 parent 5c9f8bd commit 62094c9
Show file tree
Hide file tree
Showing 27 changed files with 628 additions and 551 deletions.
File renamed without changes.
54 changes: 42 additions & 12 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
resolver = "2"
members = [
"components/macros",
"components/injector",
"components/llrt",
]
13 changes: 3 additions & 10 deletions components/llrt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ build = "build.rs"
crate-type = ["cdylib"]

[dependencies]
grebuloff-macros = { path = "../macros" }
ffxivclientstructs = { path = "../../deps/FFXIVClientStructs/rust/lib", features = ["async-resolution"] }
dll-syringe = { version = "0.15.2", default-features = false, features = ["payload-utils"] }
msgbox = "0.7.0"
Expand All @@ -25,16 +26,8 @@ anyhow = "1.0.71"
include_dir = "0.7.3"
v8 = "0.73.0"
deno_ast = { version = "0.27.1", features = ["transpiling"] }
inventory = "0.3.6"
itertools = "0.10.5"

[build-dependencies]
vergen = { version = "8.2.1", features = ["build", "git", "gitcl"] }
walkdir = "2"

[profile.dev]
opt-level = 0

[profile.dev.package.ffxivclientstructs]
opt-level = 0

[profile.dev.package."*"]
opt-level = 3
67 changes: 0 additions & 67 deletions components/llrt/build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#![feature(string_leak)]

// use deno_core::snapshot_util::{create_snapshot, CreateSnapshotOptions};
// use deno_core::{include_js_files, Extension, ExtensionFileSource, ExtensionFileSourceCode};
use std::error::Error;
use std::path::Path;
use vergen::EmitBuilder;
use walkdir::WalkDir;

const LIBHLRT_BASE: &str = "../build/libhlrt/dist";

Expand All @@ -18,70 +12,9 @@ fn emit_build_meta() -> Result<(), Box<dyn Error>> {
Ok(())
}

fn build_libhlrt_snapshot() -> Result<(), Box<dyn Error>> {
// let mut files = Vec::new();
//
// let base_path = Path::new(&LIBHLRT_BASE).canonicalize()?;
// for entry in WalkDir::new(&base_path).into_iter().filter_map(|e| e.ok()) {
// let path = entry.path();
// match path.extension() {
// Some(ext) if ext == "js" => {
// println!("cargo:rerun-if-changed={}", path.display());
//
// // get the absolute path to the file
// let path = path.canonicalize()?;
//
// // get the path to the file, relative to the base path
// let rel_path = path.strip_prefix(&base_path).unwrap();
//
// // normalize rel_path to use forward slashes, strip leading slash, and strip extension
// let rel_path = rel_path
// .to_str()
// .unwrap()
// .replace("\\", "/")
// .leak() // doesn't really matter, this is a build script
// .trim_start_matches('/')
// .trim_end_matches(".js");
//
// let rel_path = format!("ext:grebuloff/{}", rel_path);
//
// let js_file = ExtensionFileSource {
// specifier: rel_path.leak(),
// code: ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(path),
// };
//
// files.push(js_file);
// }
// _ => continue,
// }
// }
// let files = include_js_files!(grebuloff
// "../build/libhlrt/dist/index.js",
// // "../build/libhlrt/dist/console.js",
// );
//
// // build the extension
// let ext = Extension::builder("grebuloff").esm(files).build();
//
// // snapshot time!
// let out_path =
// Path::new(std::env::var("OUT_DIR").unwrap().as_str()).join("libhlrt_snapshot.bin");
//
// create_snapshot(CreateSnapshotOptions {
// cargo_manifest_dir: env!("CARGO_MANIFEST_DIR"),
// snapshot_path: out_path,
// startup_snapshot: None,
// extensions: vec![ext],
// compression_cb: None,
// snapshot_module_load_cb: None,
// });

Ok(())
}
fn main() -> Result<(), Box<dyn Error>> {
// Rerun if libhlrt changes, so we can regenerate the snapshot
println!("cargo:rerun-if-changed={}", LIBHLRT_BASE);
build_libhlrt_snapshot()?;

// Emit build metadata
emit_build_meta()?;
Expand Down
2 changes: 1 addition & 1 deletion components/llrt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async fn init_sync_on_tokio(runtime_dir: PathBuf, dalamud_pipe_name: Option<Vec<

info!("--------------------------------------------------");
info!(
"Grebuloff Runtime starting (load method: {:?})",
"Grebuloff Low-Level Runtime starting (load method: {:?})",
get_load_method()
);
info!("Build time: {}", env!("VERGEN_BUILD_TIMESTAMP"));
Expand Down
40 changes: 40 additions & 0 deletions components/llrt/src/runtime/callable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use super::engine::{Invocation, JsEngine, JsObject, JsResult, JsValue};

/// A JsCallable is the data structure used to represent a function in LLRT
/// that is callable from JavaScript. It contains the Rust function pointer,
/// and the function's name. Note that privileges are handled by the HLRT.
pub struct JsCallable {
/// The name of the function, as it will appear in JavaScript.
pub name: &'static str,

/// The function pointer.
pub func: fn(Invocation) -> JsResult<JsValue>,
}

impl JsCallable {
pub const fn new(name: &'static str, func: fn(Invocation) -> JsResult<JsValue>) -> Self {
Self { name, func }
}
}

pub fn register_all(engine: &JsEngine, dest: &JsObject) -> JsResult<()> {
for callable in inventory::iter::<JsCallable> {
dest.set(callable.name, engine.create_function(callable.func))?;
}

Ok(())
}

inventory::collect!(JsCallable);

#[macro_export]
macro_rules! register_js_callable {
($name:expr, $func:ident) => {
inventory::submit! {
crate::runtime::callable::JsCallable::new(
$name,
$func
)
}
};
}
25 changes: 14 additions & 11 deletions components/llrt/src/runtime/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::engine::{Invocation, JsEngine};
use super::{callable, engine::JsEngine};
use grebuloff_macros::js_callable;
use log::info;
use std::collections::HashMap;
use std::sync::{Arc, Once, OnceLock};
Expand Down Expand Up @@ -114,26 +115,28 @@ fn runtime_thread(options: ContextOptions, context_tx: oneshot::Sender<Arc<JsEng
info!("runtime thread stopping for context: {:?}", options);
}

fn add_base_api(engine: &JsEngine) -> crate::runtime::engine::Result<()> {
fn add_base_api(engine: &JsEngine) -> crate::runtime::engine::JsResult<()> {
let global = engine.global();

let console = engine.create_object();
console.set("log", engine.create_function(handle_console))?;
let grebuloff = engine.create_object();
let llrt = engine.create_object();

global.set("console", console)?;
callable::register_all(engine, &llrt)?;

grebuloff.set("LLRT", llrt)?;
global.set("Grebuloff", grebuloff)?;

Ok(())
}

fn add_privileged_api(engine: &JsEngine) -> crate::runtime::engine::Result<()> {
fn add_privileged_api(engine: &JsEngine) -> crate::runtime::engine::JsResult<()> {
let mut _global = engine.global();

Ok(())
}

fn handle_console(inv: Invocation) -> crate::runtime::engine::Result<()> {
let msg = inv.args.from::<String>(&inv.engine, 0)?;

info!("console.log: {:?}", msg);
Ok(())
#[js_callable]
fn print(msg: String) -> bool {
info!("print: {:?}", msg);
true
}
Loading

0 comments on commit 62094c9

Please sign in to comment.