1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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 Normal file
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 { } else {
C.fprintf(C.stderr, c'%.*s\n', s.len, s.str) 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 { } $else {
if s.str == 0 { if s.str == 0 {
C.write(2, c'eprintln(NIL)\n', 14) C.write(2, c'eprintln(NIL)\n', 14)
@ -110,6 +116,13 @@ pub fn eprint(s string) {
} else { } else {
C.fprintf(C.stderr, c'%.*s', s.len, s.str) 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 { } $else {
if s.str == 0 { if s.str == 0 {
C.write(2, c'eprint(NIL)', 11) C.write(2, c'eprint(NIL)', 11)
@ -125,6 +138,9 @@ pub fn eprint(s string) {
pub fn print(s string) { pub fn print(s string) {
$if android { $if android {
C.fprintf(C.stdout, c'%.*s', s.len, s.str) 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 { } $else {
C.write(1, s.str, s.len) C.write(1, s.str, s.len)
} }
@ -142,6 +158,8 @@ pub fn println(s string) {
if s.str == 0 { if s.str == 0 {
$if android { $if android {
C.fprintf(C.stdout, c'println(NIL)\n') C.fprintf(C.stdout, c'println(NIL)\n')
} $else $if ios {
C.WrappedNSLog(c'println(NIL)')
} $else { } $else {
C.write(1, c'println(NIL)\n', 13) C.write(1, c'println(NIL)\n', 13)
} }
@ -149,6 +167,8 @@ pub fn println(s string) {
} }
$if android { $if android {
C.fprintf(C.stdout, c'%.*s\n', s.len, s.str) C.fprintf(C.stdout, c'%.*s\n', s.len, s.str)
} $else $if ios {
C.WrappedNSLog(s.str)
} $else { } $else {
C.write(1, s.str, s.len) C.write(1, s.str, s.len)
C.write(1, c'\n', 1) 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 // used by gl, stbi, freetype
fn C.glTexImage2D() 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 { if (file.ends_with('_macos.c.v') || file.ends_with('_macos.v')) && prefs.os != .macos {
return false 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 { if file.ends_with('_nix.c.v') && prefs.os == .windows {
return false return false
} }