mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-11 05:34:11 +03:00
Have hello-world example use C
This commit is contained in:
parent
3683f20838
commit
2fc3305423
76
examples/hello-world/platform/host.c
Normal file
76
examples/hello-world/platform/host.c
Normal file
@ -0,0 +1,76 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
void* roc_alloc(size_t size, unsigned int alignment) {
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
void* roc_realloc(void* ptr, size_t old_size, size_t new_size, unsigned int alignment) {
|
||||
return realloc(ptr, new_size);
|
||||
}
|
||||
|
||||
void roc_dealloc(void* ptr, unsigned int alignment) {
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
struct RocStr {
|
||||
char* bytes;
|
||||
size_t len;
|
||||
};
|
||||
|
||||
struct RocCallResult {
|
||||
size_t flag;
|
||||
struct RocStr content;
|
||||
};
|
||||
|
||||
extern void roc__mainForHost_1_exposed(struct RocCallResult *re);
|
||||
|
||||
const size_t MAX_STACK_STR_BYTES = 1024;
|
||||
|
||||
int main() {
|
||||
// make space for the result
|
||||
struct RocCallResult callresult;
|
||||
|
||||
// call roc to populate the callresult
|
||||
roc__mainForHost_1_exposed(&callresult);
|
||||
struct RocStr str = callresult.content;
|
||||
|
||||
// Convert from RocStr to C string (null-terminated char*)
|
||||
size_t len = str.len;
|
||||
char* c_str;
|
||||
|
||||
// Allocate on the stack unless the string is particularly big.
|
||||
// (Don't want a stack overflow!)
|
||||
if (len <= MAX_STACK_STR_BYTES) {
|
||||
c_str = (char*)alloca(len + 1);
|
||||
} else {
|
||||
c_str = (char*)malloc(len + 1);
|
||||
}
|
||||
|
||||
memcpy(c_str, str.bytes, len);
|
||||
|
||||
// null-terminate
|
||||
c_str[len] = 0;
|
||||
|
||||
// Print the string to stdout
|
||||
printf("%s\n", c_str);
|
||||
|
||||
// Pointer to the beginning of the RocStr's actual allocation, which is
|
||||
// the size_t immediately preceding the first stored byte.
|
||||
size_t* str_base_ptr = (size_t*)str.bytes - 1;
|
||||
|
||||
// If *str_base_ptr is equal to 0, then the string is in the
|
||||
// read-only data section of the binary, and can't be freed!
|
||||
if (*str_base_ptr != 0) {
|
||||
roc_dealloc(str_base_ptr, 8);
|
||||
}
|
||||
|
||||
// If we malloc'd c_str, free it.
|
||||
if (len > MAX_STACK_STR_BYTES) {
|
||||
free(c_str);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
const std = @import("std");
|
||||
const str = @import("str");
|
||||
const RocStr = str.RocStr;
|
||||
const testing = std.testing;
|
||||
const expectEqual = testing.expectEqual;
|
||||
const expect = testing.expect;
|
||||
|
||||
comptime {
|
||||
// This is a workaround for https://github.com/ziglang/zig/issues/8218
|
||||
// which is only necessary on macOS.
|
||||
//
|
||||
// Once that issue is fixed, we can undo the changes in
|
||||
// 177cf12e0555147faa4d436e52fc15175c2c4ff0 and go back to passing
|
||||
// -fcompiler-rt in link.rs instead of doing this. Note that this
|
||||
// workaround is present in many host.zig files, so make sure to undo
|
||||
// it everywhere!
|
||||
if (std.builtin.os.tag == .macos) {
|
||||
_ = @import("compiler_rt");
|
||||
}
|
||||
}
|
||||
|
||||
extern fn malloc(size: usize) callconv(.C) ?*c_void;
|
||||
extern fn realloc(c_ptr: [*]align(@alignOf(u128)) u8, size: usize) callconv(.C) ?*c_void;
|
||||
extern fn free(c_ptr: [*]align(@alignOf(u128)) u8) callconv(.C) void;
|
||||
|
||||
export fn roc_alloc(size: usize, alignment: u32) callconv(.C) ?*c_void {
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
export fn roc_realloc(c_ptr: *c_void, old_size: usize, new_size: usize, alignment: u32) callconv(.C) ?*c_void {
|
||||
return realloc(@alignCast(16, @ptrCast([*]u8, c_ptr)), new_size);
|
||||
}
|
||||
|
||||
export fn roc_dealloc(c_ptr: *c_void, alignment: u32) callconv(.C) void {
|
||||
free(@alignCast(16, @ptrCast([*]u8, c_ptr)));
|
||||
}
|
||||
|
||||
const mem = std.mem;
|
||||
const Allocator = mem.Allocator;
|
||||
|
||||
extern fn roc__mainForHost_1_exposed(*RocCallResult) void;
|
||||
|
||||
const RocCallResult = extern struct { flag: usize, content: RocStr };
|
||||
|
||||
const Unit = extern struct {};
|
||||
|
||||
pub export fn main() i32 {
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
const stderr = std.io.getStdErr().writer();
|
||||
|
||||
// make space for the result
|
||||
var callresult = RocCallResult{ .flag = 0, .content = RocStr.empty() };
|
||||
|
||||
// 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
|
||||
roc__mainForHost_1_exposed(&callresult);
|
||||
|
||||
// stdout the result
|
||||
stdout.print("{s}\n", .{callresult.content.asSlice()}) catch unreachable;
|
||||
|
||||
callresult.content.deinit();
|
||||
|
||||
// end time
|
||||
var ts2: std.os.timespec = undefined;
|
||||
std.os.clock_gettime(std.os.CLOCK_REALTIME, &ts2) catch unreachable;
|
||||
|
||||
const delta = to_seconds(ts2) - to_seconds(ts1);
|
||||
|
||||
stderr.print("runtime: {d:.3}ms\n", .{delta * 1000}) catch unreachable;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
fn to_seconds(tms: std.os.timespec) f64 {
|
||||
return @intToFloat(f64, tms.tv_sec) + (@intToFloat(f64, tms.tv_nsec) / 1_000_000_000.0);
|
||||
}
|
Loading…
Reference in New Issue
Block a user