Skip to content

Commit dbf0c3e

Browse files
authored
Merge pull request #3634 from youknowone/wasm32
Fix wasm32 build
2 parents 584d972 + cc8735a commit dbf0c3e

File tree

7 files changed

+55
-38
lines changed

7 files changed

+55
-38
lines changed

.github/workflows/ci.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ jobs:
107107
command: check
108108
args: --target aarch64-linux-android
109109

110+
- uses: actions-rs/toolchain@v1
111+
with:
112+
target: wasm32-unknown-unknown
113+
114+
- name: Check compilation for wasm32
115+
uses: actions-rs/cargo@v1
116+
with:
117+
command: check
118+
args: --target wasm32-unknown-unknown --no-default-features
119+
110120
- uses: actions-rs/toolchain@v1
111121
with:
112122
target: wasm32-wasi

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ libc = "0.2.123"
4646
flame = { version = "0.2.2", optional = true }
4747
flamescope = { version = "0.1.2", optional = true }
4848

49-
[target.'cfg(not(target_os = "wasi"))'.dependencies]
49+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
5050
rustyline = "9.1.2"
5151

5252
[dev-dependencies]

src/shell/helper.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
#![cfg_attr(target_os = "wasi", allow(dead_code))]
2-
use rustpython_vm::builtins::{PyDictRef, PyStrRef};
3-
use rustpython_vm::VirtualMachine;
4-
use rustpython_vm::{function::ArgIterable, PyResult, TryFromObject};
1+
#![cfg_attr(target_arch = "wasm32", allow(dead_code))]
2+
use rustpython_vm::{
3+
builtins::{PyDictRef, PyStrRef},
4+
function::ArgIterable,
5+
PyResult, TryFromObject, VirtualMachine,
6+
};
57

68
pub struct ShellHelper<'vm> {
79
vm: &'vm VirtualMachine,
@@ -140,7 +142,7 @@ impl<'vm> ShellHelper<'vm> {
140142
}
141143

142144
cfg_if::cfg_if! {
143-
if #[cfg(not(target_os = "wasi"))] {
145+
if #[cfg(not(target_arch = "wasm32"))] {
144146
use rustyline::{
145147
completion::Completer, highlight::Highlighter, hint::Hinter, validate::Validator, Context,
146148
Helper,

vm/src/exceptions.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,17 +1023,22 @@ pub(crate) fn raw_os_error_to_exc_type(errno: i32, vm: &VirtualMachine) -> Optio
10231023
}
10241024
}
10251025

1026+
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
1027+
pub(crate) fn raw_os_error_to_exc_type(_errno: i32, _vm: &VirtualMachine) -> Option<PyTypeRef> {
1028+
None
1029+
}
1030+
10261031
pub(super) mod types {
10271032
use crate::common::lock::PyRwLock;
1028-
#[cfg_attr(target_os = "wasi", allow(unused_imports))]
1033+
#[cfg_attr(target_arch = "wasm32", allow(unused_imports))]
10291034
use crate::{
10301035
builtins::{traceback::PyTracebackRef, PyInt, PyTupleRef, PyTypeRef},
10311036
convert::ToPyResult,
10321037
function::FuncArgs,
10331038
PyObjectRef, PyRef, PyResult, VirtualMachine,
10341039
};
10351040
use crossbeam_utils::atomic::AtomicCell;
1036-
#[cfg_attr(target_os = "wasi", allow(unused_imports))]
1041+
#[cfg_attr(target_arch = "wasm32", allow(unused_imports))]
10371042
use std::ops::Deref;
10381043

10391044
// This module is designed to be used as `use builtins::*;`.

vm/src/stdlib/io.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,34 @@ cfg_if::cfg_if! {
99
}
1010
}
1111

12-
use crate::{PyObjectRef, PyResult, TryFromObject, VirtualMachine};
13-
pub(crate) use _io::io_open as open;
12+
use crate::{
13+
builtins::PyBaseExceptionRef,
14+
convert::{ToPyException, ToPyObject},
15+
PyObjectRef, PyResult, TryFromObject, VirtualMachine,
16+
};
17+
pub use _io::io_open as open;
18+
19+
impl ToPyException for &'_ std::io::Error {
20+
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
21+
use std::io::ErrorKind;
22+
23+
let excs = &vm.ctx.exceptions;
24+
#[allow(unreachable_patterns)] // some errors are just aliases of each other
25+
let exc_type = match self.kind() {
26+
ErrorKind::NotFound => excs.file_not_found_error.clone(),
27+
ErrorKind::PermissionDenied => excs.permission_error.clone(),
28+
ErrorKind::AlreadyExists => excs.file_exists_error.clone(),
29+
ErrorKind::WouldBlock => excs.blocking_io_error.clone(),
30+
_ => self
31+
.raw_os_error()
32+
.and_then(|errno| crate::exceptions::raw_os_error_to_exc_type(errno, vm))
33+
.unwrap_or_else(|| excs.os_error.clone()),
34+
};
35+
let errno = self.raw_os_error().to_pyobject(vm);
36+
let msg = vm.ctx.new_str(self.to_string()).into();
37+
vm.new_exception(exc_type, vec![errno, msg])
38+
}
39+
}
1440

1541
pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
1642
let ctx = &vm.ctx;

vm/src/stdlib/os.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ use crate::{
88
VirtualMachine,
99
};
1010
use std::{
11-
ffi, fs,
12-
io::{self, ErrorKind},
11+
ffi, fs, io,
1312
path::{Path, PathBuf},
1413
};
1514

@@ -236,31 +235,6 @@ impl PathOrFd {
236235
}
237236
}
238237

239-
impl ToPyException for io::Error {
240-
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
241-
(&self).to_pyexception(vm)
242-
}
243-
}
244-
impl ToPyException for &'_ io::Error {
245-
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
246-
let excs = &vm.ctx.exceptions;
247-
#[allow(unreachable_patterns)] // some errors are just aliases of each other
248-
let exc_type = match self.kind() {
249-
ErrorKind::NotFound => excs.file_not_found_error.clone(),
250-
ErrorKind::PermissionDenied => excs.permission_error.clone(),
251-
ErrorKind::AlreadyExists => excs.file_exists_error.clone(),
252-
ErrorKind::WouldBlock => excs.blocking_io_error.clone(),
253-
_ => self
254-
.raw_os_error()
255-
.and_then(|errno| crate::exceptions::raw_os_error_to_exc_type(errno, vm))
256-
.unwrap_or_else(|| excs.os_error.clone()),
257-
};
258-
let errno = self.raw_os_error().to_pyobject(vm);
259-
let msg = vm.ctx.new_str(self.to_string()).into();
260-
vm.new_exception(exc_type, vec![errno, msg])
261-
}
262-
}
263-
264238
#[cfg(unix)]
265239
impl ToPyException for nix::Error {
266240
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {

vm/src/stdlib/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Implementation of the _thread module
2-
#[cfg_attr(target_os = "wasi", allow(unused_imports))]
2+
#[cfg_attr(target_arch = "wasm32", allow(unused_imports))]
33
pub(crate) use _thread::{make_module, RawRMutex};
44

55
#[pymodule]

0 commit comments

Comments
 (0)