builtin: use NSLog on iOS for print (#9665)

pull/9670/head
Leah Lundqvist 2021-04-10 16:40:43 +02:00 committed by GitHub
parent 64391efa4d
commit 38c517c1a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 0 deletions

8
thirdparty/ios/ios.m vendored 100644
View File

@ -0,0 +1,8 @@
#import <Foundation/Foundation.h>
void WrappedNSLog(const char *message,...) {
va_list args;
va_start(args, message);
NSLog(@"%@",[[NSString alloc] initWithFormat:[NSString stringWithUTF8String:message] arguments:args]);
va_end(args);
}

View File

@ -89,6 +89,12 @@ pub fn eprintln(s string) {
} else {
C.fprintf(C.stderr, c'%.*s\n', s.len, s.str)
}
} $else $if ios {
if s.str == 0 {
C.WrappedNSLog(c'eprintln(NIL)\n')
} else {
C.WrappedNSLog(s.str)
}
} $else {
if s.str == 0 {
C.write(2, c'eprintln(NIL)\n', 14)
@ -110,6 +116,13 @@ pub fn eprint(s string) {
} else {
C.fprintf(C.stderr, c'%.*s', s.len, s.str)
}
} $else $if ios {
// TODO: Implement a buffer as NSLog doesn't have a "print"
if s.str == 0 {
C.WrappedNSLog(c'eprint(NIL)')
} else {
C.WrappedNSLog(s.str)
}
} $else {
if s.str == 0 {
C.write(2, c'eprint(NIL)', 11)
@ -125,6 +138,9 @@ pub fn eprint(s string) {
pub fn print(s string) {
$if android {
C.fprintf(C.stdout, c'%.*s', s.len, s.str)
} $else $if ios {
// TODO: Implement a buffer as NSLog doesn't have a "print"
C.WrappedNSLog(s.str)
} $else {
C.write(1, s.str, s.len)
}
@ -142,6 +158,8 @@ pub fn println(s string) {
if s.str == 0 {
$if android {
C.fprintf(C.stdout, c'println(NIL)\n')
} $else $if ios {
C.WrappedNSLog(c'println(NIL)')
} $else {
C.write(1, c'println(NIL)\n', 13)
}
@ -149,6 +167,8 @@ pub fn println(s string) {
}
$if android {
C.fprintf(C.stdout, c'%.*s\n', s.len, s.str)
} $else $if ios {
C.WrappedNSLog(s.str)
} $else {
C.write(1, s.str, s.len)
C.write(1, c'\n', 1)

View File

@ -0,0 +1,6 @@
module builtin
// TODO: Remove this later, added to make sure v self works
$if ios {
#include "@VROOT/thirdparty/ios/ios.m"
}

View File

@ -451,3 +451,6 @@ fn C.dup2(oldfd int, newfd int) int
// used by gl, stbi, freetype
fn C.glTexImage2D()
// used by ios for println
fn C.WrappedNSLog(str &byte)

View File

@ -141,6 +141,9 @@ pub fn (prefs &Preferences) should_compile_c(file string) bool {
if (file.ends_with('_macos.c.v') || file.ends_with('_macos.v')) && prefs.os != .macos {
return false
}
if (file.ends_with('_ios.c.v') || file.ends_with('_ios.v')) && prefs.os != .ios {
return false
}
if file.ends_with('_nix.c.v') && prefs.os == .windows {
return false
}