metacall / core

MetaCall: The ultimate polyglot programming experience.
https://metacall.io
Apache License 2.0
1.58k stars 163 forks source link

bug: incorrectly calculating the size of script that is loaded from memory #513

Closed hulxv closed 4 months ago

hulxv commented 4 months ago

🐛 Bug Report

the script that is loaded from memory lost the last character because its size is not correct and misses a one-character from its length.

Possible Solution

https://github.com/metacall/core/blob/76f02c051c9bfd84926331f8ada8b9506a12f04d/source/ports/rs_port/src/loaders.rs#L82-L87

 metacall_load_from_memory( 
     c_tag.as_ptr(), 
     c_script.as_ptr(), 
-    script.len(), 
+    script.len() + 1, 
     ptr::null_mut(), 
)

Steps to Reproduce

source code

use metacall::{loaders, switch};
fn main() {
    let _metacall = switch::initialize().unwrap();
    loaders::from_memory("node", "console.log('Hello world')").unwrap();
}

behavior

Exception in node_loader_trampoline_load_from_memory while loading: console.log('Hello world' 0x5b3b086100b0-0x5b3b08610080-26-2913836576:1
console.log('Hello world'
            ^^^^^^^^^^^^^

SyntaxError: missing ) after argument list
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1274:20)
    at Module._compile (node:internal/modules/cjs/loader:1320:27)
    at node_loader_trampoline_load_from_memory (/gnu/store/ka685gr3ah26nmjfny1kwixmv2nsc2ms-metacall-0.7.8/lib/bootstrap.js:182:5)
hulxv commented 4 months ago

@devraymondsh take a look

devraymondsh commented 4 months ago

@Hulxv Nice catch. The core accepts size and the Rust loader incorrectly passes length.