update hosts with new string conventions

This commit is contained in:
Folkert 2022-03-13 00:16:21 +01:00
parent ec403958a3
commit ba40b6a957
No known key found for this signature in database
GPG Key ID: 1F17F6FFD112B97C
4 changed files with 27 additions and 18 deletions

View File

@ -20,7 +20,8 @@ comptime {
const mem = std.mem;
const Allocator = mem.Allocator;
extern fn roc__mainForHost_1_exposed(RocList) RocList;
extern fn roc__mainForHost_1_exposed_generic(output: *RocList, input: *RocList) void;
// extern fn roc__mainForHost_1_exposed_generic(input: *RocList) RocList;
const Align = extern struct { a: usize, b: usize };
extern fn malloc(size: usize) callconv(.C) ?*align(@alignOf(Align)) c_void;
@ -69,11 +70,11 @@ export fn roc_panic(c_ptr: *c_void, tag_id: u32) callconv(.C) void {
std.process.exit(0);
}
export fn roc_memcpy(dst: [*]u8, src: [*]u8, size: usize) callconv(.C) void{
export fn roc_memcpy(dst: [*]u8, src: [*]u8, size: usize) callconv(.C) void {
return memcpy(dst, src, size);
}
export fn roc_memset(dst: [*]u8, value: i32, size: usize) callconv(.C) void{
export fn roc_memset(dst: [*]u8, value: i32, size: usize) callconv(.C) void {
return memset(dst, value, size);
}
@ -99,14 +100,17 @@ pub export fn main() u8 {
numbers[i] = @mod(@intCast(i64, i), 12);
}
const roc_list = RocList{ .elements = numbers, .length = NUM_NUMS };
var roc_list = RocList{ .elements = numbers, .length = NUM_NUMS };
// start time
var ts1: std.os.timespec = undefined;
std.os.clock_gettime(std.os.CLOCK_REALTIME, &ts1) catch unreachable;
// actually call roc to populate the callresult
var callresult = roc__mainForHost_1_exposed(roc_list);
var callresult: RocList = undefined;
roc__mainForHost_1_exposed_generic(&callresult, &roc_list);
// const callresult: RocList = roc__mainForHost_1_exposed_generic(&roc_list);
// stdout the result
const length = std.math.min(20, callresult.length);
@ -126,9 +130,9 @@ pub export fn main() u8 {
}
}
const delta = to_seconds(ts2) - to_seconds(ts1);
stderr.print("runtime: {d:.3}ms\n", .{delta * 1000}) catch unreachable;
// TODO apparently the typestamps are still (partially) undefined?
// const delta = to_seconds(ts2) - to_seconds(ts1);
// stderr.print("runtime: {d:.3}ms\n", .{delta * 1000}) catch unreachable;
return 0;
}

View File

@ -13,7 +13,7 @@ use std::os::raw::c_char;
extern "C" {
#[link_name = "roc__mainForHost_1_exposed_generic"]
fn roc_main(args: RocStr, output: *mut u8) -> ();
fn roc_main(output: *mut u8, args: &RocStr);
#[link_name = "roc__mainForHost_size"]
fn roc_main_size() -> i64;
@ -74,7 +74,7 @@ pub unsafe extern "C" fn roc_memset(dst: *mut c_void, c: i32, n: usize) -> *mut
#[no_mangle]
pub extern "C" fn rust_main() -> i32 {
let arg = env::args().skip(1).next().unwrap();
let arg = env::args().nth(1).unwrap();
let arg = RocStr::from(arg.as_str());
let size = unsafe { roc_main_size() } as usize;
@ -84,7 +84,11 @@ pub extern "C" fn rust_main() -> i32 {
// TODO allocate on the stack if it's under a certain size
let buffer = std::alloc::alloc(layout);
roc_main(arg, buffer);
roc_main(buffer, &arg);
// arg has been passed to roc now, and it assumes ownership.
// so we must not touch its refcount now
std::mem::forget(arg);
let result = call_the_closure(buffer);
@ -141,14 +145,14 @@ pub extern "C" fn roc_fx_getChar() -> u8 {
}
#[no_mangle]
pub extern "C" fn roc_fx_putLine(line: ManuallyDrop<RocStr>) {
pub extern "C" fn roc_fx_putLine(line: &RocStr) {
let string = line.as_str();
println!("{}", string);
std::io::stdout().lock().flush();
}
#[no_mangle]
pub extern "C" fn roc_fx_putRaw(line: ManuallyDrop<RocStr>) {
pub extern "C" fn roc_fx_putRaw(line: &RocStr) {
let string = line.as_str();
print!("{}", string);
std::io::stdout().lock().flush();
@ -185,8 +189,9 @@ pub extern "C" fn roc_fx_closeFile(br_ptr: *mut BufReader<File>) {
}
#[no_mangle]
pub extern "C" fn roc_fx_openFile(name: ManuallyDrop<RocStr>) -> *mut BufReader<File> {
match File::open(name.as_str()) {
pub extern "C" fn roc_fx_openFile(name: &RocStr) -> *mut BufReader<File> {
let string = name.as_str();
match File::open(string) {
Ok(f) => {
let br = BufReader::new(f);
@ -199,7 +204,7 @@ pub extern "C" fn roc_fx_openFile(name: ManuallyDrop<RocStr>) -> *mut BufReader<
}
#[no_mangle]
pub extern "C" fn roc_fx_withFileOpen(name: ManuallyDrop<RocStr>, buffer: *const u8) {
pub extern "C" fn roc_fx_withFileOpen(name: &RocStr, buffer: *const u8) {
// TODO: figure out accepting a closure in an fx and passing data to it.
// let f = File::open(name.as_str()).expect("Unable to open file");
// let mut br = BufReader::new(f);

View File

@ -119,7 +119,7 @@ pub extern "C" fn roc_fx_getLine() -> RocStr {
}
#[no_mangle]
pub extern "C" fn roc_fx_putLine(line: ManuallyDrop<RocStr>) {
pub extern "C" fn roc_fx_putLine(line: &RocStr) {
let string = line.as_str();
println!("{}", string);
}

View File

@ -146,7 +146,7 @@ fn roc_fx_getLine_help() !RocStr {
return str.RocStr.init(@ptrCast([*]const u8, line), line.len);
}
pub export fn roc_fx_putLine(rocPath: str.RocStr) i64 {
pub export fn roc_fx_putLine(rocPath: *str.RocStr) i64 {
const stdout = std.io.getStdOut().writer();
for (rocPath.asSlice()) |char| {