From 54b4e57a28034ce1cb609dec71c5637c17e12f21 Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Wed, 6 Jul 2022 22:49:13 +0900 Subject: [PATCH 1/2] fix --no-default-features build --- vm/src/stdlib/builtins.rs | 21 +++++---- vm/src/vm/compile.rs | 90 ++++++++++++++++++++++++++++++++++++++- vm/src/vm/mod.rs | 86 +------------------------------------ 3 files changed, 99 insertions(+), 98 deletions(-) diff --git a/vm/src/stdlib/builtins.rs b/vm/src/stdlib/builtins.rs index 4a051eb41..67d402b86 100644 --- a/vm/src/stdlib/builtins.rs +++ b/vm/src/stdlib/builtins.rs @@ -18,10 +18,8 @@ mod builtins { int::PyIntRef, iter::PyCallableIterator, list::{PyList, SortOptions}, - PyByteArray, PyBytes, PyBytesRef, PyCode, PyDictRef, PyStr, PyStrRef, PyTuple, - PyTupleRef, PyType, + PyByteArray, PyBytes, PyDictRef, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType, }, - class::PyClassImpl, common::{hash::PyHash, str::to_ascii}, format::call_object_format, function::Either, @@ -32,12 +30,11 @@ mod builtins { protocol::{PyIter, PyIterReturn}, py_io, readline::{Readline, ReadlineResult}, - scope::Scope, stdlib::sys, types::PyComparisonOp, AsObject, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine, }; - use num_traits::{Signed, ToPrimitive, Zero}; + use num_traits::{Signed, ToPrimitive}; #[pyfunction] fn abs(x: PyObjectRef, vm: &VirtualMachine) -> PyResult { @@ -123,7 +120,7 @@ mod builtins { } #[cfg(feature = "rustpython-ast")] { - use crate::stdlib::ast; + use crate::{class::PyClassImpl, stdlib::ast}; let mode_str = args.mode.as_str(); @@ -152,6 +149,8 @@ mod builtins { } #[cfg(feature = "rustpython-parser")] { + use crate::builtins::PyBytesRef; + use num_traits::Zero; use rustpython_parser::parser; let source = Either::::try_from_object(vm, args.source)?; @@ -215,7 +214,7 @@ mod builtins { #[cfg(feature = "rustpython-compiler")] impl ScopeArgs { - fn make_scope(self, vm: &VirtualMachine) -> PyResult { + fn make_scope(self, vm: &VirtualMachine) -> PyResult { let (globals, locals) = match self.globals { Some(globals) => { if !globals.contains_key(identifier!(vm, __builtins__), vm) { @@ -239,7 +238,7 @@ mod builtins { ), }; - let scope = Scope::with_builtins(Some(locals), globals, vm); + let scope = crate::scope::Scope::with_builtins(Some(locals), globals, vm); Ok(scope) } } @@ -249,7 +248,7 @@ mod builtins { #[cfg(feature = "rustpython-compiler")] #[pyfunction] fn eval( - source: Either>, + source: Either>, scope: ScopeArgs, vm: &VirtualMachine, ) -> PyResult { @@ -261,7 +260,7 @@ mod builtins { #[cfg(feature = "rustpython-compiler")] #[pyfunction] fn exec( - source: Either>, + source: Either>, scope: ScopeArgs, vm: &VirtualMachine, ) -> PyResult { @@ -271,7 +270,7 @@ mod builtins { #[cfg(feature = "rustpython-compiler")] fn run_code( vm: &VirtualMachine, - source: Either>, + source: Either>, scope: ScopeArgs, mode: compile::Mode, func: &str, diff --git a/vm/src/vm/compile.rs b/vm/src/vm/compile.rs index a475d5e3a..401cece01 100644 --- a/vm/src/vm/compile.rs +++ b/vm/src/vm/compile.rs @@ -1,7 +1,9 @@ use crate::{ - builtins::PyCode, + builtins::{PyCode, PyDictRef}, compile::{self, CompileError, CompileOpts}, - PyRef, VirtualMachine, + convert::TryFromObject, + scope::Scope, + AsObject, PyObjectRef, PyRef, PyResult, VirtualMachine, }; impl VirtualMachine { @@ -31,4 +33,88 @@ impl VirtualMachine { ) -> Result, CompileError> { compile::compile(source, mode, source_path, opts).map(|code| self.ctx.new_code(code)) } + + pub fn run_script(&self, scope: Scope, path: &str) -> PyResult<()> { + if get_importer(path, self)?.is_some() { + self.insert_sys_path(self.new_pyobj(path))?; + let runpy = self.import("runpy", None, 0)?; + let run_module_as_main = runpy.get_attr("_run_module_as_main", self)?; + self.invoke( + &run_module_as_main, + (identifier!(self, __main__).to_owned(), false), + )?; + return Ok(()); + } + + let dir = std::path::Path::new(path) + .parent() + .unwrap() + .to_str() + .unwrap(); + self.insert_sys_path(self.new_pyobj(dir))?; + + match std::fs::read_to_string(path) { + Ok(source) => { + self.run_code_string(scope, &source, path.to_owned())?; + } + Err(err) => { + error!("Failed reading file '{}': {}", path, err); + std::process::exit(1); + } + } + Ok(()) + } + + pub fn run_code_string(&self, scope: Scope, source: &str, source_path: String) -> PyResult { + let code_obj = self + .compile(source, crate::compile::Mode::Exec, source_path.clone()) + .map_err(|err| self.new_syntax_error(&err))?; + // trace!("Code object: {:?}", code_obj.borrow()); + scope.globals.set_item( + identifier!(self, __file__), + self.new_pyobj(source_path), + self, + )?; + self.run_code_obj(code_obj, scope) + } + + pub fn run_block_expr(&self, scope: Scope, source: &str) -> PyResult { + let code_obj = self + .compile( + source, + crate::compile::Mode::BlockExpr, + "".to_owned(), + ) + .map_err(|err| self.new_syntax_error(&err))?; + // trace!("Code object: {:?}", code_obj.borrow()); + self.run_code_obj(code_obj, scope) + } +} + +fn get_importer(path: &str, vm: &VirtualMachine) -> PyResult> { + let path_importer_cache = vm.sys_module.get_attr("path_importer_cache", vm)?; + let path_importer_cache = PyDictRef::try_from_object(vm, path_importer_cache)?; + if let Some(importer) = path_importer_cache.get_item_opt(path, vm)? { + return Ok(Some(importer)); + } + let path = vm.ctx.new_str(path); + let path_hooks = vm.sys_module.get_attr("path_hooks", vm)?; + let mut importer = None; + let path_hooks: Vec = path_hooks.try_into_value(vm)?; + for path_hook in path_hooks { + match vm.invoke(&path_hook, (path.clone(),)) { + Ok(imp) => { + importer = Some(imp); + break; + } + Err(e) if e.fast_isinstance(vm.ctx.exceptions.import_error) => continue, + Err(e) => return Err(e), + } + } + Ok(if let Some(imp) = importer { + let imp = path_importer_cache.get_or_insert(vm, path.into(), || imp.clone())?; + Some(imp) + } else { + None + }) } diff --git a/vm/src/vm/mod.rs b/vm/src/vm/mod.rs index aabc62d18..bcec61d3d 100644 --- a/vm/src/vm/mod.rs +++ b/vm/src/vm/mod.rs @@ -24,7 +24,7 @@ use crate::{ bytecode, codecs::CodecsRegistry, common::{hash::HashSecret, lock::PyMutex, rc::PyRc}, - convert::{ToPyObject, TryFromObject}, + convert::ToPyObject, frame::{ExecutionResult, Frame, FrameRef}, frozen, function::{ArgMapping, FuncArgs}, @@ -726,62 +726,6 @@ impl VirtualMachine { Ok(()) } - pub fn run_script(&self, scope: Scope, path: &str) -> PyResult<()> { - if get_importer(path, self)?.is_some() { - self.insert_sys_path(self.new_pyobj(path))?; - let runpy = self.import("runpy", None, 0)?; - let run_module_as_main = runpy.get_attr("_run_module_as_main", self)?; - self.invoke( - &run_module_as_main, - (identifier!(self, __main__).to_owned(), false), - )?; - return Ok(()); - } - - let dir = std::path::Path::new(path) - .parent() - .unwrap() - .to_str() - .unwrap(); - self.insert_sys_path(self.new_pyobj(dir))?; - - match std::fs::read_to_string(path) { - Ok(source) => { - self.run_code_string(scope, &source, path.to_owned())?; - } - Err(err) => { - error!("Failed reading file '{}': {}", path, err); - std::process::exit(1); - } - } - Ok(()) - } - - pub fn run_code_string(&self, scope: Scope, source: &str, source_path: String) -> PyResult { - let code_obj = self - .compile(source, crate::compile::Mode::Exec, source_path.clone()) - .map_err(|err| self.new_syntax_error(&err))?; - // trace!("Code object: {:?}", code_obj.borrow()); - scope.globals.set_item( - identifier!(self, __file__), - self.new_pyobj(source_path), - self, - )?; - self.run_code_obj(code_obj, scope) - } - - pub fn run_block_expr(&self, scope: Scope, source: &str) -> PyResult { - let code_obj = self - .compile( - source, - crate::compile::Mode::BlockExpr, - "".to_owned(), - ) - .map_err(|err| self.new_syntax_error(&err))?; - // trace!("Code object: {:?}", code_obj.borrow()); - self.run_code_obj(code_obj, scope) - } - pub fn run_module(&self, module: &str) -> PyResult<()> { let runpy = self.import("runpy", None, 0)?; let run_module_as_main = runpy.get_attr("_run_module_as_main", self)?; @@ -790,34 +734,6 @@ impl VirtualMachine { } } -fn get_importer(path: &str, vm: &VirtualMachine) -> PyResult> { - let path_importer_cache = vm.sys_module.get_attr("path_importer_cache", vm)?; - let path_importer_cache = PyDictRef::try_from_object(vm, path_importer_cache)?; - if let Some(importer) = path_importer_cache.get_item_opt(path, vm)? { - return Ok(Some(importer)); - } - let path = vm.ctx.new_str(path); - let path_hooks = vm.sys_module.get_attr("path_hooks", vm)?; - let mut importer = None; - let path_hooks: Vec = path_hooks.try_into_value(vm)?; - for path_hook in path_hooks { - match vm.invoke(&path_hook, (path.clone(),)) { - Ok(imp) => { - importer = Some(imp); - break; - } - Err(e) if e.fast_isinstance(vm.ctx.exceptions.import_error) => continue, - Err(e) => return Err(e), - } - } - Ok(if let Some(imp) = importer { - let imp = path_importer_cache.get_or_insert(vm, path.into(), || imp.clone())?; - Some(imp) - } else { - None - }) -} - impl AsRef for VirtualMachine { fn as_ref(&self) -> &Context { &self.ctx From 74ba229e9f322686b488d965409cfe19450ede67 Mon Sep 17 00:00:00 2001 From: Jeong Yunwon Date: Wed, 6 Jul 2022 22:58:42 +0900 Subject: [PATCH 2/2] Add freebsd build to CI --- .github/workflows/ci.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index e9f12d03b..27e9ca65a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -124,6 +124,16 @@ jobs: command: check args: --target i686-unknown-linux-musl + - uses: actions-rs/toolchain@v1 + with: + target: x86_64-unknown-freebsd + + - name: Check compilation for freebsd + uses: actions-rs/cargo@v1 + with: + command: check + args: --target x86_64-unknown-freebsd + - uses: actions-rs/toolchain@v1 with: target: wasm32-unknown-unknown