Skip to content

Commit 4f9a71f

Browse files
committed
Change compile::compile() to take a Mode instead of an &Mode
1 parent 87f96f1 commit 4f9a71f

File tree

10 files changed

+22
-21
lines changed

10 files changed

+22
-21
lines changed

benchmarks/bench.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn bench_rustpy_nbody(b: &mut test::Bencher) {
9494

9595
let vm = VirtualMachine::default();
9696

97-
let code = match vm.compile(source, &compile::Mode::Single, "<stdin>".to_string()) {
97+
let code = match vm.compile(source, compile::Mode::Single, "<stdin>".to_string()) {
9898
Ok(code) => code,
9999
Err(e) => panic!("{:?}", e),
100100
};
@@ -113,7 +113,7 @@ fn bench_rustpy_mandelbrot(b: &mut test::Bencher) {
113113

114114
let vm = VirtualMachine::default();
115115

116-
let code = match vm.compile(source, &compile::Mode::Single, "<stdin>".to_string()) {
116+
let code = match vm.compile(source, compile::Mode::Single, "<stdin>".to_string()) {
117117
Ok(code) => code,
118118
Err(e) => panic!("{:?}", e),
119119
};

compiler/src/compile.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct Compiler<O: OutputStream = BasicOutputStream> {
3131
/// Compile a given sourcecode into a bytecode object.
3232
pub fn compile(
3333
source: &str,
34-
mode: &Mode,
34+
mode: Mode,
3535
source_path: String,
3636
optimize: u8,
3737
) -> Result<CodeObject, CompileError> {
@@ -102,6 +102,7 @@ pub fn compile_program_single(
102102
})
103103
}
104104

105+
#[derive(Clone, Copy)]
105106
pub enum Mode {
106107
Exec,
107108
Eval,

derive/src/compile_bytecode.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl CompilationSource {
4141
fn compile_string(
4242
&self,
4343
source: &str,
44-
mode: &compile::Mode,
44+
mode: compile::Mode,
4545
module_name: String,
4646
) -> Result<CodeObject, Diagnostic> {
4747
compile::compile(source, mode, module_name, 0)
@@ -50,7 +50,7 @@ impl CompilationSource {
5050

5151
fn compile(
5252
&self,
53-
mode: &compile::Mode,
53+
mode: compile::Mode,
5454
module_name: String,
5555
) -> Result<HashMap<String, FrozenModule>, Diagnostic> {
5656
Ok(match &self.kind {
@@ -94,7 +94,7 @@ impl CompilationSource {
9494
&self,
9595
path: &Path,
9696
parent: String,
97-
mode: &compile::Mode,
97+
mode: compile::Mode,
9898
) -> Result<HashMap<String, FrozenModule>, Diagnostic> {
9999
let mut code_map = HashMap::new();
100100
let paths = fs::read_dir(&path).map_err(|err| {
@@ -225,7 +225,7 @@ impl PyCompileInput {
225225
)
226226
})?
227227
.compile(
228-
&mode.unwrap_or(compile::Mode::Exec),
228+
mode.unwrap_or(compile::Mode::Exec),
229229
module_name.unwrap_or_else(|| "frozen".to_string()),
230230
)
231231
}

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ fn run_rustpython(vm: &VirtualMachine, matches: &ArgMatches) -> PyResult<()> {
333333

334334
fn _run_string(vm: &VirtualMachine, scope: Scope, source: &str, source_path: String) -> PyResult {
335335
let code_obj = vm
336-
.compile(source, &compile::Mode::Exec, source_path.clone())
336+
.compile(source, compile::Mode::Exec, source_path.clone())
337337
.map_err(|err| vm.new_syntax_error(&err))?;
338338
// trace!("Code object: {:?}", code_obj.borrow());
339339
scope
@@ -426,7 +426,7 @@ fn test_run_script() {
426426
}
427427

428428
fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> Result<(), CompileError> {
429-
match vm.compile(source, &compile::Mode::Single, "<stdin>".to_string()) {
429+
match vm.compile(source, compile::Mode::Single, "<stdin>".to_string()) {
430430
Ok(code) => {
431431
match vm.run_code_obj(code, scope.clone()) {
432432
Ok(value) => {

vm/src/builtins.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ fn builtin_compile(args: CompileArgs, vm: &VirtualMachine) -> PyResult<PyCodeRef
127127

128128
let mode = get_compile_mode(vm, &args.mode.value)?;
129129

130-
vm.compile(&source, &mode, args.filename.value.to_string())
130+
vm.compile(&source, mode, args.filename.value.to_string())
131131
.map_err(|err| vm.new_syntax_error(&err))
132132
}
133133

@@ -175,7 +175,7 @@ fn builtin_eval(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
175175
} else if objtype::isinstance(source, &vm.ctx.str_type()) {
176176
let mode = compile::Mode::Eval;
177177
let source = objstr::get_value(source);
178-
vm.compile(&source, &mode, "<string>".to_string())
178+
vm.compile(&source, mode, "<string>".to_string())
179179
.map_err(|err| vm.new_syntax_error(&err))?
180180
} else {
181181
return Err(vm.new_type_error("code argument must be str or code object".to_string()));
@@ -202,7 +202,7 @@ fn builtin_exec(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
202202
let code_obj = if objtype::isinstance(source, &vm.ctx.str_type()) {
203203
let mode = compile::Mode::Exec;
204204
let source = objstr::get_value(source);
205-
vm.compile(&source, &mode, "<string>".to_string())
205+
vm.compile(&source, mode, "<string>".to_string())
206206
.map_err(|err| vm.new_syntax_error(&err))?
207207
} else if let Ok(code_obj) = PyCodeRef::try_from_object(vm, source.clone()) {
208208
code_obj

vm/src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::vm::VirtualMachine;
44
use rustpython_compiler::compile;
55

66
pub fn eval(vm: &VirtualMachine, source: &str, scope: Scope, source_path: &str) -> PyResult {
7-
match vm.compile(source, &compile::Mode::Eval, source_path.to_string()) {
7+
match vm.compile(source, compile::Mode::Eval, source_path.to_string()) {
88
Ok(bytecode) => {
99
debug!("Code object: {:?}", bytecode);
1010
vm.run_code_obj(bytecode, scope)

vm/src/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn import_file(
6868
) -> PyResult {
6969
let code_obj = compile::compile(
7070
&content,
71-
&compile::Mode::Exec,
71+
compile::Mode::Exec,
7272
file_path,
7373
vm.settings.optimize,
7474
)

vm/src/vm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ impl VirtualMachine {
877877
pub fn compile(
878878
&self,
879879
source: &str,
880-
mode: &compile::Mode,
880+
mode: compile::Mode,
881881
source_path: String,
882882
) -> Result<PyCodeRef, CompileError> {
883883
compile::compile(source, mode, source_path, self.settings.optimize)

wasm/lib/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const TS_CMT_START: &'static str = "/*";
6363
/// receive the Python kwargs as the `this` argument.
6464
/// - `stdout?`: `(out: string) => void`: A function to replace the native print
6565
/// function, by default `console.log`.
66-
pub fn eval_py(source: String, options: Option<Object>) -> Result<JsValue, JsValue> {
66+
pub fn eval_py(source: &str, options: Option<Object>) -> Result<JsValue, JsValue> {
6767
let options = options.unwrap_or_else(Object::new);
6868
let js_vars = {
6969
let prop = Reflect::get(&options, &"vars".into())?;

wasm/lib/src/vm_class.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,13 @@ impl WASMVirtualMachine {
271271
})?
272272
}
273273

274-
fn run(&self, source: String, mode: compile::Mode) -> Result<JsValue, JsValue> {
274+
fn run(&self, source: &str, mode: compile::Mode) -> Result<JsValue, JsValue> {
275275
self.assert_valid()?;
276276
self.with_unchecked(
277277
|StoredVirtualMachine {
278278
ref vm, ref scope, ..
279279
}| {
280-
let code = vm.compile(&source, &mode, "<wasm>".to_string());
280+
let code = vm.compile(source, mode, "<wasm>".to_string());
281281
let code = code.map_err(|err| {
282282
let js_err = SyntaxError::new(&format!("Error parsing Python code: {}", err));
283283
let _ =
@@ -295,16 +295,16 @@ impl WASMVirtualMachine {
295295
)
296296
}
297297

298-
pub fn exec(&self, source: String) -> Result<JsValue, JsValue> {
298+
pub fn exec(&self, source: &str) -> Result<JsValue, JsValue> {
299299
self.run(source, compile::Mode::Exec)
300300
}
301301

302-
pub fn eval(&self, source: String) -> Result<JsValue, JsValue> {
302+
pub fn eval(&self, source: &str) -> Result<JsValue, JsValue> {
303303
self.run(source, compile::Mode::Eval)
304304
}
305305

306306
#[wasm_bindgen(js_name = execSingle)]
307-
pub fn exec_single(&self, source: String) -> Result<JsValue, JsValue> {
307+
pub fn exec_single(&self, source: &str) -> Result<JsValue, JsValue> {
308308
self.run(source, compile::Mode::Single)
309309
}
310310
}

0 commit comments

Comments
 (0)