Merge remote-tracking branch 'origin/main' into https-packages

This commit is contained in:
Richard Feldman 2022-11-25 19:50:06 -05:00
commit b2beeb770e
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
85 changed files with 2666 additions and 1064 deletions

View file

@ -8,7 +8,7 @@ use roc_collections::all::MutSet;
use roc_gen_llvm::llvm::externs::add_default_roc_externs;
use roc_gen_llvm::{llvm::build::LlvmBackendMode, run_roc::RocCallResult};
use roc_load::{EntryPoint, ExecutionMode, LoadConfig, Threading};
use roc_mono::ir::OptLevel;
use roc_mono::ir::{CrashTag, OptLevel};
use roc_packaging::cache::RocCacheDir;
use roc_region::all::LineInfo;
use roc_reporting::report::{RenderTarget, DEFAULT_PALETTE};
@ -546,7 +546,10 @@ macro_rules! assert_wasm_evals_to {
}
#[allow(dead_code)]
pub fn try_run_lib_function<T>(main_fn_name: &str, lib: &libloading::Library) -> Result<T, String> {
pub fn try_run_lib_function<T>(
main_fn_name: &str,
lib: &libloading::Library,
) -> Result<T, (String, CrashTag)> {
unsafe {
let main: libloading::Symbol<unsafe extern "C" fn(*mut RocCallResult<T>)> = lib
.get(main_fn_name.as_bytes())
@ -567,6 +570,7 @@ macro_rules! assert_llvm_evals_to {
use bumpalo::Bump;
use inkwell::context::Context;
use roc_gen_llvm::llvm::build::LlvmBackendMode;
use roc_mono::ir::CrashTag;
let arena = Bump::new();
let context = Context::create();
@ -596,7 +600,10 @@ macro_rules! assert_llvm_evals_to {
#[cfg(windows)]
std::mem::forget(given);
}
Err(msg) => panic!("Roc failed with message: \"{}\"", msg),
Err((msg, tag)) => match tag {
CrashTag::Roc => panic!(r#"Roc failed with message: "{}""#, msg),
CrashTag::User => panic!(r#"User crash with message: "{}""#, msg),
},
}
// artificially extend the lifetime of `lib`
@ -657,29 +664,6 @@ macro_rules! assert_evals_to {
}};
}
#[allow(unused_macros)]
macro_rules! expect_runtime_error_panic {
($src:expr) => {{
#[cfg(feature = "gen-llvm-wasm")]
$crate::helpers::llvm::assert_wasm_evals_to!(
$src,
false, // fake value/type for eval
bool,
$crate::helpers::llvm::identity,
true // ignore problems
);
#[cfg(not(feature = "gen-llvm-wasm"))]
$crate::helpers::llvm::assert_llvm_evals_to!(
$src,
false, // fake value/type for eval
bool,
$crate::helpers::llvm::identity,
true // ignore problems
);
}};
}
#[allow(dead_code)]
pub fn identity<T>(value: T) -> T {
value
@ -691,5 +675,3 @@ pub(crate) use assert_evals_to;
pub(crate) use assert_llvm_evals_to;
#[allow(unused_imports)]
pub(crate) use assert_wasm_evals_to;
#[allow(unused_imports)]
pub(crate) use expect_runtime_error_panic;

View file

@ -32,23 +32,3 @@ pub unsafe fn roc_realloc(
pub unsafe fn roc_dealloc(c_ptr: *mut c_void, _alignment: u32) {
libc::free(c_ptr)
}
/// # Safety
/// The Roc application needs this.
#[no_mangle]
pub unsafe fn roc_panic(c_ptr: *mut c_void, tag_id: u32) {
use roc_gen_llvm::llvm::build::PanicTagId;
use std::ffi::CStr;
use std::os::raw::c_char;
match PanicTagId::try_from(tag_id) {
Ok(PanicTagId::NullTerminatedString) => {
let slice = CStr::from_ptr(c_ptr as *const c_char);
let string = slice.to_str().unwrap();
eprintln!("Roc hit a panic: {}", string);
std::process::exit(1);
}
Err(_) => unreachable!(),
}
}

View file

@ -6,6 +6,7 @@ use roc_gen_wasm::DEBUG_SETTINGS;
use roc_load::{ExecutionMode, LoadConfig, Threading};
use roc_packaging::cache::RocCacheDir;
use roc_reporting::report::DEFAULT_PALETTE_HTML;
use roc_std::RocStr;
use roc_wasm_module::{Export, ExportType};
use std::marker::PhantomData;
use std::path::PathBuf;
@ -195,7 +196,7 @@ where
let parsed = Module::parse(&env, &wasm_bytes[..]).expect("Unable to parse module");
let mut module = rt.load_module(parsed).expect("Unable to load module");
let panic_msg: Rc<Mutex<Option<(i32, i32)>>> = Default::default();
let panic_msg: Rc<Mutex<Option<(i32, u32)>>> = Default::default();
link_module(&mut module, panic_msg.clone());
let test_wrapper = module
@ -204,12 +205,18 @@ where
match test_wrapper.call() {
Err(e) => {
if let Some((msg_ptr, msg_len)) = *panic_msg.lock().unwrap() {
if let Some((msg_ptr, tag)) = *panic_msg.lock().unwrap() {
let memory: &[u8] = get_memory(&rt);
let msg_bytes = &memory[msg_ptr as usize..][..msg_len as usize];
let msg = std::str::from_utf8(msg_bytes).unwrap();
let msg = RocStr::decode(memory, msg_ptr as _);
Err(format!("Roc failed with message: \"{}\"", msg))
dbg!(tag);
let msg = match tag {
0 => format!(r#"Roc failed with message: "{}""#, msg),
1 => format!(r#"User crash with message: "{}""#, msg),
tag => format!(r#"Got an invald panic tag: "{}""#, tag),
};
Err(msg)
} else {
Err(format!("{}", e))
}
@ -255,7 +262,7 @@ where
let parsed = Module::parse(&env, wasm_bytes).expect("Unable to parse module");
let mut module = rt.load_module(parsed).expect("Unable to load module");
let panic_msg: Rc<Mutex<Option<(i32, i32)>>> = Default::default();
let panic_msg: Rc<Mutex<Option<(i32, u32)>>> = Default::default();
link_module(&mut module, panic_msg.clone());
let expected_len = num_refcounts as i32;
@ -318,13 +325,13 @@ fn read_i32(memory: &[u8], ptr: usize) -> i32 {
i32::from_le_bytes(bytes)
}
fn link_module(module: &mut Module, panic_msg: Rc<Mutex<Option<(i32, i32)>>>) {
fn link_module(module: &mut Module, panic_msg: Rc<Mutex<Option<(i32, u32)>>>) {
let try_link_panic = module.link_closure(
"env",
"send_panic_msg_to_rust",
move |_call_context, args: (i32, i32)| {
move |_call_context, (msg_ptr, tag): (i32, u32)| {
let mut w = panic_msg.lock().unwrap();
*w = Some(args);
*w = Some((msg_ptr, tag));
Ok(())
},
);
@ -392,19 +399,6 @@ macro_rules! assert_evals_to {
}};
}
#[allow(unused_macros)]
macro_rules! expect_runtime_error_panic {
($src:expr) => {{
$crate::helpers::wasm::assert_evals_to!(
$src,
false, // fake value/type for eval
bool,
$crate::helpers::wasm::identity,
true // ignore problems
);
}};
}
#[allow(dead_code)]
pub fn identity<T>(value: T) -> T {
value
@ -432,8 +426,5 @@ macro_rules! assert_refcounts {
#[allow(unused_imports)]
pub(crate) use assert_evals_to;
#[allow(unused_imports)]
pub(crate) use expect_runtime_error_panic;
#[allow(unused_imports)]
pub(crate) use assert_refcounts;

View file

@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
@ -123,12 +124,11 @@ void roc_dealloc(void *ptr, unsigned int alignment)
//--------------------------
extern void send_panic_msg_to_rust(char* msg, int len);
extern void send_panic_msg_to_rust(void* msg, uint32_t tag_id);
void roc_panic(char *msg, unsigned int tag_id)
void roc_panic(void* msg, unsigned int tag_id)
{
int len = strlen(msg);
send_panic_msg_to_rust(msg, len);
send_panic_msg_to_rust(msg, tag_id);
exit(101);
}