-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #252 from Wodann/refactor/libloading
refactor: move library loading logic to separate crate
- Loading branch information
Showing
9 changed files
with
99 additions
and
28 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "mun_libloader" | ||
version = "0.1.0" | ||
authors = ["The Mun Team <[email protected]>"] | ||
edition = "2018" | ||
description = "Functionality for loading Mun libraries" | ||
documentation = "https://docs.mun-lang.org/v0.2" | ||
readme = "README.md" | ||
homepage = "https://mun-lang.org" | ||
repository = "https://github.com/mun-lang/mun" | ||
license = "MIT OR Apache-2.0" | ||
keywords = ["game", "hot-reloading", "language", "mun", "scripting"] | ||
categories = ["game-development", "mun"] | ||
|
||
[dependencies] | ||
abi = { version = "=0.2.0", path = "../mun_abi", package = "mun_abi" } | ||
anyhow = "1.0" | ||
libloading = "0.6" | ||
tempfile = "3" |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
../../LICENSE-APACHE |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
../../LICENSE-MIT |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
../../README.md |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
mod temp_library; | ||
|
||
use std::{ffi::c_void, path::Path}; | ||
|
||
pub use temp_library::TempLibrary; | ||
|
||
pub struct MunLibrary(TempLibrary); | ||
|
||
impl MunLibrary { | ||
pub fn new(library_path: &Path) -> Result<Self, anyhow::Error> { | ||
let library = TempLibrary::new(library_path)?; | ||
|
||
// Verify that the `*.munlib` contains all required functions | ||
let _get_abi_version_fn: libloading::Symbol<'_, extern "C" fn() -> u32> = | ||
unsafe { library.library().get(abi::GET_VERSION_FN_NAME.as_bytes()) }?; | ||
|
||
let _get_info_fn: libloading::Symbol<'_, extern "C" fn() -> abi::AssemblyInfo> = | ||
unsafe { library.library().get(abi::GET_INFO_FN_NAME.as_bytes()) }?; | ||
|
||
let _set_allocator_handle_fn: libloading::Symbol<'_, extern "C" fn(*mut c_void)> = unsafe { | ||
library | ||
.library() | ||
.get(abi::SET_ALLOCATOR_HANDLE_FN_NAME.as_bytes()) | ||
}?; | ||
|
||
Ok(MunLibrary(library)) | ||
} | ||
|
||
pub fn into_inner(self) -> TempLibrary { | ||
self.0 | ||
} | ||
|
||
pub fn get_abi_version(&self) -> u32 { | ||
let get_abi_version_fn: libloading::Symbol<'_, extern "C" fn() -> u32> = unsafe { | ||
self.0 | ||
.library() | ||
.get(abi::GET_VERSION_FN_NAME.as_bytes()) | ||
.unwrap() | ||
}; | ||
|
||
get_abi_version_fn() | ||
} | ||
|
||
pub fn get_info(&self) -> abi::AssemblyInfo { | ||
let get_info_fn: libloading::Symbol<'_, extern "C" fn() -> abi::AssemblyInfo> = unsafe { | ||
self.0 | ||
.library() | ||
.get(abi::GET_INFO_FN_NAME.as_bytes()) | ||
.unwrap() | ||
}; | ||
|
||
get_info_fn() | ||
} | ||
|
||
pub fn set_allocator_handle(&mut self, allocator_ptr: *mut c_void) { | ||
let set_allocator_handle_fn: libloading::Symbol<'_, extern "C" fn(*mut c_void)> = unsafe { | ||
self.0 | ||
.library() | ||
.get(abi::SET_ALLOCATOR_HANDLE_FN_NAME.as_bytes()) | ||
.unwrap() | ||
}; | ||
|
||
set_allocator_handle_fn(allocator_ptr); | ||
} | ||
} |
File renamed without changes.
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