Skip to content

Commit 71735d6

Browse files
authored
Use random rs output name. (#334)
* Use random rs output name. Signed-off-by: Tricster <[email protected]> * Test/rs: Change melody to serde_json. Signed-off-by: Tricster <[email protected]> * Ports/py: export load_from_package. Signed-off-by: Tricster <[email protected]> Signed-off-by: Tricster <[email protected]>
1 parent 7750f24 commit 71735d6

File tree

10 files changed

+81
-225
lines changed

10 files changed

+81
-225
lines changed

source/loaders/rs_loader/rust/compiler/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ dlopen = "0.1.8"
1111
libffi = "3.0.0"
1212
cargo_toml = "0.11.5"
1313
lazy_static = "1.4.0"
14-
itertools = "0.10.3"
14+
itertools = "0.10.3"
15+
fastrand = "1.4"

source/loaders/rs_loader/rust/compiler/src/lib.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,19 @@ impl CompilerCallbacks {
509509
}
510510
}
511511

512+
fn generate_random_string(length: usize) -> String {
513+
let charset_str = "abcdefghijklmnopqrstuvwxyz";
514+
let chars: Vec<char> = charset_str.chars().collect();
515+
let mut result = String::with_capacity(length);
516+
517+
unsafe {
518+
for _ in 0..length {
519+
result.push(*chars.get_unchecked(fastrand::usize(0..chars.len())));
520+
}
521+
}
522+
result
523+
}
524+
512525
impl rustc_driver::Callbacks for CompilerCallbacks {
513526
fn config(&mut self, config: &mut Config) {
514527
if matches!(self.source.source, Source::Package { .. }) {
@@ -573,8 +586,26 @@ impl rustc_driver::Callbacks for CompilerCallbacks {
573586
config.input_path = Some(self.source.input_path.clone());
574587
}
575588
// Set up output
576-
config.output_file = Some(self.source.output.clone());
577-
589+
if self.is_parsing {
590+
let random_string = generate_random_string(5);
591+
let mut output_path = self.source.output.clone();
592+
let new_filename = format!(
593+
"{}_{}",
594+
output_path
595+
.file_prefix()
596+
.expect("Unable to get file prefix")
597+
.to_string_lossy(),
598+
random_string
599+
);
600+
output_path.set_file_name(new_filename);
601+
if let Some(ext) = self.source.output.extension() {
602+
output_path.set_extension(ext);
603+
}
604+
config.output_file = Some(output_path.clone());
605+
self.source.output = output_path;
606+
} else {
607+
config.output_file = Some(self.source.output.clone());
608+
}
578609
// Setting up default compiler flags
579610
config.opts.output_types = config::OutputTypes::new(&[(config::OutputType::Exe, None)]);
580611
config.opts.optimize = config::OptLevel::Default;

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ fn generate_class_wrapper_for_package(classes: &Vec<&crate::Class>) -> String {
129129
}
130130
pub fn generate_wrapper(callbacks: CompilerCallbacks) -> std::io::Result<CompilerCallbacks> {
131131
match callbacks.source.source {
132-
Source::Package { path } => {
132+
Source::Package { ref path } => {
133133
let mut content = String::new();
134134
let function_wrapper = generate_function_wrapper_for_package(&callbacks.functions);
135135
content.push_str(&function_wrapper);
@@ -152,9 +152,13 @@ pub fn generate_wrapper(callbacks: CompilerCallbacks) -> std::io::Result<Compile
152152
wrapper_file.write_all(b"mod metacall_class;\nuse metacall_class::*;\n")?;
153153
wrapper_file.write_all(content.as_bytes())?;
154154

155+
let mut source = Source::new(Source::Package {
156+
path: path.to_path_buf(),
157+
});
158+
source.output = callbacks.source.output;
155159
// construct new callback
156160
Ok(CompilerCallbacks {
157-
source: Source::new(Source::Package { path: path }),
161+
source,
158162
is_parsing: false,
159163
..callbacks
160164
})
@@ -190,10 +194,11 @@ pub fn generate_wrapper(callbacks: CompilerCallbacks) -> std::io::Result<Compile
190194
wrapper_file.write_all(content.as_bytes())?;
191195
let dst = format!("include!({:?});", callbacks.source.input_path.clone());
192196
wrapper_file.write_all(dst.as_bytes())?;
193-
197+
let mut source = Source::new(Source::File { path: source_path });
198+
source.output = callbacks.source.output;
194199
// construct new callback
195200
Ok(CompilerCallbacks {
196-
source: Source::new(Source::File { path: source_path }),
201+
source,
197202
is_parsing: false,
198203
..callbacks
199204
})
@@ -217,12 +222,13 @@ pub fn generate_wrapper(callbacks: CompilerCallbacks) -> std::io::Result<Compile
217222
wrapper_file.write_all(content.as_bytes())?;
218223
let dst = format!("include!({:?});", source_path.join("script.rs"));
219224
wrapper_file.write_all(dst.as_bytes())?;
220-
225+
let mut source = Source::new(Source::File {
226+
path: source_path.join("wrapped_script.rs"),
227+
});
228+
source.output = callbacks.source.output;
221229
// construct new callback
222230
Ok(CompilerCallbacks {
223-
source: Source::new(Source::File {
224-
path: source_path.join("wrapped_script.rs"),
225-
}),
231+
source,
226232
is_parsing: false,
227233
..callbacks
228234
})

source/ports/py_port/metacall/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
# See the License for the specific language governing permissions and
1818
# limitations under the License.
1919

20-
from metacall.api import metacall, metacall_load_from_file, metacall_load_from_memory, metacall_inspect
20+
from metacall.api import metacall, metacall_load_from_file, metacall_load_from_memory, metacall_load_from_package, metacall_inspect

source/scripts/rust/basic/source/basic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn add_float(num_1: f32, num_2: f32) -> f32 {
1818
pub fn run() {
1919
let dir = env::temp_dir();
2020
let mut f = File::create(dir.join("hello.txt")).unwrap();
21-
f.write(b"Hello metacall");
21+
let _ = f.write(b"Hello metacall");
2222
}
2323

2424
// pub fn add_vec(vec: &mut Vec<i32>) -> i32 {

source/scripts/rust/melody/Cargo.lock

Lines changed: 17 additions & 194 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
[package]
2-
name = "melody"
2+
name = "json-wrapper"
33
version = "0.1.0"
44
edition = "2021"
55

6+
crate-type=["lib"]
7+
68
[dependencies]
7-
melody_compiler = "0.18.1"
9+
serde_json = "1.0"
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use melody_compiler::compiler;
1+
use serde_json::Value;
22

33
pub fn compile(s: String) -> String {
4-
let ret = compiler(&s).unwrap_or_else(|x| format!("Compiler error {:?}", x));
5-
dbg!(&ret);
6-
ret
4+
let v: Value = serde_json::from_str(&s).unwrap();
5+
v["name"].to_string()
76
}

source/tests/metacall_python_port_test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Check if loaders are enabled
2-
if(NOT OPTION_BUILD_LOADERS OR NOT OPTION_BUILD_LOADERS_PY OR NOT OPTION_BUILD_LOADERS_RB OR NOT OPTION_BUILD_LOADERS_NODE OR NOT OPTION_BUILD_PORTS OR NOT OPTION_BUILD_PORTS_PY OR NOT OPTION_BUILD_SCRIPTS OR NOT OPTION_BUILD_SCRIPTS_PY OR NOT OPTION_BUILD_SCRIPTS_RB OR NOT OPTION_BUILD_SCRIPTS_NODE)
2+
if(NOT OPTION_BUILD_LOADERS OR NOT OPTION_BUILD_LOADERS_PY OR NOT OPTION_BUILD_LOADERS_RB OR NOT OPTION_BUILD_LOADERS_NODE OR NOT OPTION_BUILD_LOADERS_RS OR NOT OPTION_BUILD_PORTS OR NOT OPTION_BUILD_PORTS_PY OR NOT OPTION_BUILD_SCRIPTS OR NOT OPTION_BUILD_SCRIPTS_PY OR NOT OPTION_BUILD_SCRIPTS_RB OR NOT OPTION_BUILD_SCRIPTS_RS OR NOT OPTION_BUILD_SCRIPTS_NODE)
33
return()
44
endif()
55

0 commit comments

Comments
 (0)