mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-02 18:02:59 +00:00
fix: #443
This commit is contained in:
parent
ada421cd5c
commit
3fc42f65e8
4 changed files with 59 additions and 19 deletions
|
@ -226,6 +226,20 @@ impl Compiler {
|
|||
Ok(CompleteArtifact::new(last, arti.warns))
|
||||
}
|
||||
|
||||
pub fn exec_compile(
|
||||
&mut self,
|
||||
src: String,
|
||||
mode: &str,
|
||||
) -> Result<CompleteArtifact<ExitStatus>, ErrorArtifact> {
|
||||
let arti = self.compile(src, mode)?;
|
||||
let stat = arti
|
||||
.object
|
||||
.exec(self.cfg.py_magic_num, self.cfg.output.clone())
|
||||
.expect("failed to dump a .pyc file (maybe permission denied)");
|
||||
let stat = ExitStatus::new(stat.code().unwrap_or(0), arti.warns.len(), 0);
|
||||
Ok(CompleteArtifact::new(stat, arti.warns))
|
||||
}
|
||||
|
||||
pub fn compile(
|
||||
&mut self,
|
||||
src: String,
|
||||
|
|
|
@ -3,8 +3,10 @@ use std::fmt::Write as _;
|
|||
use std::fs::File;
|
||||
use std::io::{BufReader, Read, Write as _};
|
||||
use std::path::Path;
|
||||
use std::process::ExitStatus;
|
||||
|
||||
use erg_common::impl_display_from_debug;
|
||||
use erg_common::io::Output;
|
||||
#[allow(unused_imports)]
|
||||
use erg_common::log;
|
||||
use erg_common::opcode::CommonOpcode;
|
||||
|
@ -12,7 +14,7 @@ use erg_common::opcode308::Opcode308;
|
|||
use erg_common::opcode309::Opcode309;
|
||||
use erg_common::opcode310::Opcode310;
|
||||
use erg_common::opcode311::{BinOpCode, Opcode311};
|
||||
use erg_common::python_util::{env_magic_number, PythonVersion};
|
||||
use erg_common::python_util::{env_magic_number, exec_py_code, PythonVersion};
|
||||
use erg_common::serialize::*;
|
||||
use erg_common::Str;
|
||||
|
||||
|
@ -439,6 +441,19 @@ impl CodeObj {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn exec(self, py_magic_num: Option<u32>, output: Output) -> std::io::Result<ExitStatus> {
|
||||
let mut bytes = Vec::with_capacity(16);
|
||||
let py_magic_num = py_magic_num.unwrap_or_else(env_magic_number);
|
||||
let python_ver = get_ver_from_magic_num(py_magic_num);
|
||||
bytes.append(&mut self.into_bytes(python_ver));
|
||||
let mut bytecode = "".to_string();
|
||||
for b in bytes {
|
||||
write!(bytecode, "\\x{b:0>2x}").unwrap();
|
||||
}
|
||||
let code = format!("import marshal; exec(marshal.loads(b'{bytecode}'))");
|
||||
exec_py_code(&code, output)
|
||||
}
|
||||
|
||||
fn tables_info(&self) -> String {
|
||||
let mut tables = "".to_string();
|
||||
if !self.consts.is_empty() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue