Skip to content

Commit

Permalink
Making scryer-prolog build successful with wasm32-wasi by commenting …
Browse files Browse the repository at this point in the history
…out lots of code and adapting to 32-bit system. Very WIP, discussion-only
  • Loading branch information
Rujia Liu authored and Rujia Liu committed Aug 19, 2023
1 parent 7da321a commit 0507f78
Show file tree
Hide file tree
Showing 19 changed files with 272 additions and 164 deletions.
50 changes: 34 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ rust-version = "1.63"

[features]

[lib]
crate-type = ["cdylib", "rlib"]

[build-dependencies]
indexmap = "1.0.2"
proc-macro2 = "1.0.36"
Expand All @@ -29,55 +32,70 @@ walkdir = "2"
bit-set = "0.5.3"
bitvec = "1"
cpu-time = "1.0.0"
crossterm = "0.20.0"
#crossterm = "0.20.0"
dirs-next = "2.0.0"
divrem = "0.1.0"
fxhash = "0.2.1"
git-version = "0.3.4"
hostname = "0.3.1"
#hostname = "0.3.1"
indexmap = "1.0.2"
lazy_static = "1.4.0"
lexical = "5.2.2"
libc = "0.2.62"
modular-bitfield = "0.11.2"
ctrlc = "3.2.2"
modular-bitfield = { path = "thirdparty/modular-bitfield" }
#ctrlc = "3.2.2"
ordered-float = "2.6.0"
phf = { version = "0.9", features = ["macros"] }
ref_thread_local = "0.0.0"
rustyline = "12.0.0"
ring = "0.16.13"
#rustyline = "12.0.0"
#ring = "0.16.13"
ripemd160 = "0.8.0"
sha3 = "0.8.2"
blake2 = "0.8.1"
crrl = "0.2.0"
native-tls = "0.2.4"
#native-tls = "0.2.4"
chrono = "0.4.11"
select = "0.6.0"
roxmltree = "0.11.0"
base64 = "0.12.3"
smallvec = "1.8.0"
sodiumoxide = "0.2.6"
#sodiumoxide = "0.2.6"
static_assertions = "1.1.0"
ryu = "1.0.9"
hyper = { version = "1.0.0-rc.3", features = ["full"] }
tokio = { version = "1.28.2", features = ["full"] }
#hyper = { version = "1.0.0-rc.3", features = ["full"] }
#tokio = { version = "1.28.2", features = ["full"] }
tokio = { version = "1.28.2", features = ["sync", "macros", "io-util", "rt", "time"] }
futures = "0.3"
libloading = "0.7"
derive_deref = "1.1.1"
http-body-util = "0.1.0-rc.2"
bytes = "1"
reqwest = { version = "0.11.18", features = ["blocking"] }
dashu = { git = "https://github.com/coasys/dashu.git" }
libffi = { git = "https://github.com/coasys/libffi-rs.git", branch = "windows-space" }
#reqwest = { version = "0.11.18", features = ["blocking"] }
#reqwest_wasi = { version = "0.11.18", features = ["blocking"] }
dashu = { path = "thirdparty/dashu" }
#libffi = { path = "thirdparty/libffi-rs/libffi-rs" }
rand = "0.8.5"

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.10", features = ["js"] }
console_error_panic_hook = "0.1"
console_log = "1.0"
wasm-bindgen = "0.2.86"
wasm-bindgen-futures = "0.4"
serde-wasm-bindgen = "0.5"
web-sys = { version = "0.3", features = [
"Document",
"Window",
"Element",
]}

[dev-dependencies]
assert_cmd = "1.0.3"
predicates-core = "1.0.2"
serial_test = "2.0.0"

[patch.crates-io]
modular-bitfield = { git = "https://github.com/mthom/modular-bitfield" }

[profile.release]
debug = true

[profile.dev]
opt-level = 1
2 changes: 1 addition & 1 deletion build/static_string_indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub fn index_static_strings(instruction_rs_path: &std::path::Path) -> TokenStrea
Err(_) => {}
}

let indices = (0..visitor.static_strs.len()).map(|i| i << 3);
let indices = (0..visitor.static_strs.len()).map(|i| (i << 3) as u64); // affects out/static_atoms.rs
let indices_iter = indices.clone();

let static_strs_len = visitor.static_strs.len();
Expand Down
20 changes: 11 additions & 9 deletions src/arena.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::http::{HttpListener, HttpResponse};
//use crate::http::{HttpListener, HttpResponse};
use crate::machine::loader::LiveLoadState;
use crate::machine::machine_indices::*;
use crate::machine::streams::*;
Expand Down Expand Up @@ -566,6 +566,7 @@ impl ArenaAllocated for TcpListener {
}
}

/*
impl ArenaAllocated for HttpListener {
type PtrToAllocated = TypedArenaPtr<HttpListener>;
Expand Down Expand Up @@ -609,7 +610,7 @@ impl ArenaAllocated for HttpResponse {
}
}
}

*/
impl ArenaAllocated for IndexPtr {
type PtrToAllocated = TypedArenaPtr<IndexPtr>;

Expand Down Expand Up @@ -655,8 +656,9 @@ impl ArenaAllocated for IndexPtr {

#[derive(Clone, Copy, Debug)]
struct AllocSlab {
next: *mut AllocSlab,
header: ArenaHeader,
next: *mut AllocSlab, // 4 bytes for wasi32
_padding: u32, // 4 bytes
header: ArenaHeader, // 8 bytes
}

#[derive(Debug)]
Expand Down Expand Up @@ -695,13 +697,13 @@ unsafe fn drop_slab_in_place(value: &mut AllocSlab) {
ptr::drop_in_place(value.payload_offset::<StreamLayout<CharReader<NamedTcpStream>>>());
}
ArenaHeaderTag::NamedTlsStream => {
ptr::drop_in_place(value.payload_offset::<StreamLayout<CharReader<NamedTlsStream>>>());
// ptr::drop_in_place(value.payload_offset::<StreamLayout<CharReader<NamedTlsStream>>>());
}
ArenaHeaderTag::HttpReadStream => {
ptr::drop_in_place(value.payload_offset::<StreamLayout<CharReader<HttpReadStream>>>());
// ptr::drop_in_place(value.payload_offset::<StreamLayout<CharReader<HttpReadStream>>>());
}
ArenaHeaderTag::HttpWriteStream => {
ptr::drop_in_place(value.payload_offset::<StreamLayout<CharReader<HttpWriteStream>>>());
// ptr::drop_in_place(value.payload_offset::<StreamLayout<CharReader<HttpWriteStream>>>());
}
ArenaHeaderTag::ReadlineStream => {
ptr::drop_in_place(value.payload_offset::<StreamLayout<ReadlineStream>>());
Expand All @@ -724,10 +726,10 @@ unsafe fn drop_slab_in_place(value: &mut AllocSlab) {
ptr::drop_in_place(value.payload_offset::<TcpListener>());
}
ArenaHeaderTag::HttpListener => {
ptr::drop_in_place(value.payload_offset::<HttpListener>());
// ptr::drop_in_place(value.payload_offset::<HttpListener>());
}
ArenaHeaderTag::HttpResponse => {
ptr::drop_in_place(value.payload_offset::<HttpResponse>());
// ptr::drop_in_place(value.payload_offset::<HttpResponse>());
}
ArenaHeaderTag::StandardOutputStream => {
ptr::drop_in_place(value.payload_offset::<StreamLayout<StandardOutputStream>>());
Expand Down
14 changes: 7 additions & 7 deletions src/atom_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use modular_bitfield::prelude::*;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Atom {
pub index: usize,
pub index: u64,
}

const_assert!(mem::size_of::<Atom>() == 8);
Expand Down Expand Up @@ -156,27 +156,27 @@ impl Atom {

#[inline(always)]
pub fn is_static(self) -> bool {
self.index < STRINGS.len() << 3
(self.index as usize) < STRINGS.len() << 3
}

#[inline(always)]
pub fn as_ptr(self) -> *const u8 {
if self.is_static() {
ptr::null()
} else {
(get_atom_tbl_buf_base() as usize + self.index - (STRINGS.len() << 3)) as *const u8
(get_atom_tbl_buf_base() as usize + (self.index as usize) - (STRINGS.len() << 3)) as *const u8
}
}

#[inline(always)]
pub fn from(index: usize) -> Self {
Self { index }
Self { index: index as u64 }
}

#[inline(always)]
pub fn len(self) -> usize {
if self.is_static() {
STRINGS[self.index >> 3].len()
STRINGS[(self.index >> 3) as usize].len()
} else {
unsafe { ptr::read(self.as_ptr() as *const AtomHeader).len() as _ }
}
Expand Down Expand Up @@ -208,7 +208,7 @@ impl Atom {
let ptr = self.as_ptr();

if ptr.is_null() {
return STRINGS[self.index >> 3];
return STRINGS[(self.index >> 3) as usize];
}

let header = ptr::read::<AtomHeader>(ptr as *const _);
Expand Down Expand Up @@ -339,7 +339,7 @@ impl AtomTable {
write_to_ptr(string, len_ptr);

let atom = Atom {
index: (STRINGS.len() << 3) + len_ptr as usize - ptr_base,
index: ((STRINGS.len() << 3) + len_ptr as usize - ptr_base) as u64,
};

self.table.insert(atom);
Expand Down
4 changes: 2 additions & 2 deletions src/bin/scryer-prolog.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
fn main() -> std::process::ExitCode {
use std::sync::atomic::Ordering;
use scryer_prolog::*;

/*
ctrlc::set_handler(move || {
scryer_prolog::machine::INTERRUPT.store(true, Ordering::Relaxed);
}).unwrap();

*/
let mut wam = machine::Machine::new();
wam.run_top_level()
}
2 changes: 2 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and for each field: we add to the pointer until we're aligned to the next data t
and finally we add the pointer the size of what we've written.
*/

/*
use crate::atom_table::Atom;
use std::alloc::{alloc, Layout};
Expand Down Expand Up @@ -442,3 +443,4 @@ pub enum FFIError {
FunctionNotFound,
StructNotFound,
}
*/
6 changes: 4 additions & 2 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use std::future::Future;
use std::pin::Pin;
use http_body_util::Full;
use bytes::Bytes;
use hyper::service::Service;
use hyper::{body::Incoming as IncomingBody, Request, Response};
//use hyper::service::Service;
//use hyper::{body::Incoming as IncomingBody, Request, Response};

/*
pub struct HttpListener {
pub incoming: std::sync::mpsc::Receiver<HttpRequest>
}
Expand Down Expand Up @@ -52,3 +53,4 @@ impl Service<Request<IncomingBody>> for HttpService {
}
}
}
*/
2 changes: 1 addition & 1 deletion src/machine/disjuncts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct BranchNumber {
impl Default for BranchNumber {
fn default() -> Self {
Self {
branch_num: Rational::from(1usize << 63),
branch_num: Rational::from(1u64 << 63),
delta: Rational::from(1),
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/machine/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4290,11 +4290,11 @@ impl Machine {
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
}
&Instruction::CallDefineForeignStruct => {
try_or_throw!(self.machine_st, self.define_foreign_struct());
// try_or_throw!(self.machine_st, self.define_foreign_struct());
step_or_fail!(self, self.machine_st.p += 1);
}
&Instruction::ExecuteDefineForeignStruct => {
try_or_throw!(self.machine_st, self.define_foreign_struct());
// try_or_throw!(self.machine_st, self.define_foreign_struct());
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
}
&Instruction::CallCurrentTime => {
Expand Down
5 changes: 3 additions & 2 deletions src/machine/machine_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::arena::*;
use crate::atom_table::*;
use crate::parser::ast::*;

use crate::ffi::FFIError;
//use crate::ffi::FFIError;
use crate::forms::*;
use crate::machine::heap::*;
use crate::machine::loader::CompilationTarget;
Expand Down Expand Up @@ -537,7 +537,7 @@ impl MachineState {
from: ErrorProvenance::Received,
}
}

/*
pub(super) fn ffi_error(&mut self, err: FFIError) -> MachineError {
let error_atom = match err {
FFIError::ValueCast => atom!("value_cast"),
Expand All @@ -555,6 +555,7 @@ impl MachineState {
from: ErrorProvenance::Constructed,
}
}
*/

pub(super) fn error_form(&mut self, err: MachineError, src: FunctorStub) -> MachineStub {
let h = self.heap.len();
Expand Down
2 changes: 1 addition & 1 deletion src/machine/machine_indices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl IndexPtr {
#[derive(Debug, Clone, Copy, Ord, Hash, PartialOrd, Eq, PartialEq)]
pub struct CodeIndex(TypedArenaPtr<IndexPtr>);

const_assert!(std::mem::align_of::<CodeIndex>() == 8);
const_assert!(std::mem::align_of::<CodeIndex>() == 4); // 4 bytes for wasi, because TypedArenaPtr is ptr::NonNull<T>

impl Deref for CodeIndex {
type Target = TypedArenaPtr<IndexPtr>;
Expand Down
2 changes: 1 addition & 1 deletion src/machine/mock_wam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl Machine {
user_error,
load_contexts: vec![],
runtime,
foreign_function_table: Default::default(),
//foreign_function_table: Default::default(),
};

let mut lib_path = current_dir();
Expand Down
12 changes: 8 additions & 4 deletions src/machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::arena::*;
use crate::arithmetic::*;
use crate::atom_table::*;
use crate::forms::*;
use crate::ffi::ForeignFunctionTable;
//use crate::ffi::ForeignFunctionTable;
use crate::instructions::*;
use crate::machine::args::*;
use crate::machine::compile::*;
Expand Down Expand Up @@ -68,7 +68,7 @@ pub struct Machine {
pub(super) user_error: Stream,
pub(super) load_contexts: Vec<LoadContext>,
pub(super) runtime: Runtime,
pub(super) foreign_function_table: ForeignFunctionTable,
//pub(super) foreign_function_table: ForeignFunctionTable,
}

#[derive(Debug)]
Expand Down Expand Up @@ -425,7 +425,11 @@ impl Machine {
let user_output = Stream::stdout(&mut machine_st.arena);
let user_error = Stream::stderr(&mut machine_st.arena);

let runtime = tokio::runtime::Runtime::new()
// let runtime = tokio::runtime::Runtime::new()
// .unwrap();
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();

let mut wam = Machine {
Expand All @@ -437,7 +441,7 @@ impl Machine {
user_error,
load_contexts: vec![],
runtime,
foreign_function_table: Default::default(),
//foreign_function_table: Default::default(),
};

let mut lib_path = current_dir();
Expand Down
Loading

0 comments on commit 0507f78

Please sign in to comment.