Skip to content

Commit 4f411d4

Browse files
committed
Add test for loading rs package with dependencies.
Signed-off-by: Tricster <[email protected]>
1 parent 565dc3c commit 4f411d4

File tree

11 files changed

+551
-2
lines changed

11 files changed

+551
-2
lines changed

source/loaders/rs_loader/rust/compiler/src/wrapper/class.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::cell::RefCell;
44
use std::cell::RefMut;
55
use std::collections::HashMap;
66
use std::convert::TryInto;
7-
use std::ffi::CStr;
7+
use std::ffi::{CStr, CString};
88
use std::fmt;
99
use std::sync::Arc;
1010
type Result<T, E = i32> = core::result::Result<T, E>;
@@ -546,7 +546,10 @@ impl ToMetaResult for f64 {
546546

547547
impl ToMetaResult for String {
548548
fn to_meta_result(self) -> Result<MetacallValue> {
549-
Ok(unsafe { metacall_value_create_string(self.as_ptr() as *const i8, self.len()) })
549+
let length = self.len();
550+
let cstring = CString::new(self).expect("Unable to cast String to CString");
551+
let ptr = cstring.as_ptr();
552+
Ok(unsafe { metacall_value_create_string(ptr, length) })
550553
}
551554
}
552555

source/scripts/rust/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
1010
include(RustProject)
1111

1212
add_subdirectory(basic)
13+
add_subdirectory(melody)

source/scripts/rust/cmake/RustProject.cmake

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,63 @@ function(rust_package target version script)
116116

117117
endfunction()
118118

119+
function(cargo_package target version)
120+
121+
# Configuration
122+
set(PACKAGE_NAME ${target})
123+
set(PACKAGE_VERSION ${version})
124+
set(PACKAGE_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/build/timestamp")
125+
126+
set(configuration ${RUST_PROJECT_CONFIG_PATH}/RustProject.cmake.in)
127+
set(language "rust")
128+
# Define upper and lower versions of the language
129+
string(TOLOWER ${language} language_lower)
130+
131+
# Define project target name
132+
set(custom_target "${language_lower}-${target}")
133+
134+
# Define target for the configuration
135+
set(PACKAGE_TARGET "${custom_target}")
136+
137+
# Create project file
138+
configure_file(${configuration} ${custom_target}-config.cmake @ONLY)
139+
140+
# Set custom target
141+
add_custom_target(${custom_target} ALL)
142+
143+
#
144+
# Deployment
145+
#
146+
147+
# Install cmake script config
148+
#install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${custom_target}/${custom_target}-config.cmake"
149+
# DESTINATION ${INSTALL_CMAKE}/${custom_target}
150+
# COMPONENT runtime
151+
#)
152+
153+
# CMake config
154+
#install(EXPORT ${custom_target}-export
155+
# NAMESPACE ${META_PROJECT_NAME}::
156+
# DESTINATION ${INSTALL_CMAKE}/${custom_target}
157+
# COMPONENT dev
158+
#)
159+
160+
# Set project properties
161+
set_target_properties(${custom_target}
162+
PROPERTIES
163+
${DEFAULT_PROJECT_OPTIONS}
164+
FOLDER "${IDE_FOLDER}/${language}"
165+
)
166+
167+
# Compile project
168+
add_custom_command(TARGET ${custom_target} PRE_BUILD
169+
# fix the version of rustc
170+
COMMAND ${Rust_RUSTUP_EXECUTABLE} default nightly-2021-12-04
171+
COMMAND ${Rust_CARGO_EXECUTABLE} build
172+
--manifest-path ${CMAKE_CURRENT_SOURCE_DIR}/Cargo.toml
173+
--target-dir ${LOADER_SCRIPT_PATH}
174+
)
175+
176+
# Include generated project file
177+
include(${CMAKE_CURRENT_BINARY_DIR}/${custom_target}-config.cmake)
178+
endfunction()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cargo_package(cargo_melody 0.1.0)

source/scripts/rust/melody/Cargo.lock

Lines changed: 216 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "melody"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
melody_compiler = "0.18.1"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use melody_compiler::compiler;
2+
3+
pub fn compile(s: String) -> String {
4+
let ret = compiler(&s).unwrap_or_else(|x| format!("Compiler error {:?}", x));
5+
dbg!(&ret);
6+
ret
7+
}

source/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ add_subdirectory(metacall_wasm_python_port_test)
220220
add_subdirectory(metacall_rust_test)
221221
add_subdirectory(metacall_rust_load_from_mem_test)
222222
add_subdirectory(metacall_rust_load_from_package_test)
223+
add_subdirectory(metacall_rust_load_from_package_dep_test)
223224
add_subdirectory(metacall_rust_load_from_package_class_test)
224225
add_subdirectory(metacall_rust_class_test)
225226
add_subdirectory(metacall_c_test)

0 commit comments

Comments
 (0)