Skip to content

Commit

Permalink
Merge pull request #514 from Hulxv/correct-script-size
Browse files Browse the repository at this point in the history
fix: incorrectly calculating the size of script that is loaded from memory
  • Loading branch information
giarve authored Jul 10, 2024
2 parents 76f02c0 + c7a3a12 commit a4526c3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion source/ports/rs_port/src/loaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn from_memory(tag: impl ToString, script: impl ToString) -> Result<(), Meta
metacall_load_from_memory(
c_tag.as_ptr(),
c_script.as_ptr(),
script.len(),
script.len() + 1,
ptr::null_mut(),
)
} != 0
Expand Down
9 changes: 9 additions & 0 deletions source/ports/rs_port/tests/inlines_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@ fn inlines() {
print("hello world")
}
}
if loaders::from_memory("py", "").is_ok() {
py! {print("hello world")}
}

if loaders::from_memory("node", "").is_ok() {
node! {
console.log("hello world");
}
}
if loaders::from_memory("node", "").is_ok() {
node! {console.log("hello world")}
}

if loaders::from_memory("ts", "").is_ok() {
ts! {
console.log("hello world");
}
}
if loaders::from_memory("ts", "").is_ok() {
ts! {console.log("hello world")}
}
}
8 changes: 4 additions & 4 deletions source/ports/rs_port/tests/loaders_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use std::{

// Two different names to avoid conflicts when testing both load_from_memory and load_from_files
// in a single test.
const SCRIPT1: &str = "function greet1() { return 'hi there!' } \nmodule.exports = { greet1 };";
const SCRIPT1: &str = "function greet1() { return 'hi there!' } \nmodule.exports = { greet1 }";
const SCRIPT2: &str = "function greet2() { return 'hi there!' } \nmodule.exports = { greet2 };";

const SCRIPT3: &str = "console.log('Hello world')";
fn call_greet(test: &str, num: u32) {
let out = metacall_no_arg::<String>(format!("greet{}", num)).unwrap();
if out.as_str() == "hi there!" {
Expand All @@ -25,8 +25,9 @@ fn call_greet(test: &str, num: u32) {

fn load_from_memory_test() {
loaders::from_memory("node", SCRIPT1).unwrap();

call_greet("load_from_memory", 1);

loaders::from_memory("node", SCRIPT3).unwrap();
}

fn load_from_file_test() {
Expand All @@ -51,7 +52,6 @@ fn load_from_file_test() {
#[test]
fn loaders() {
let _d = switch::initialize().unwrap();

// Testing load_from_memory
load_from_memory_test();

Expand Down

0 comments on commit a4526c3

Please sign in to comment.