June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,8 @@
#include <stdio.h>
/* gcc -shared -fPIC -nostartfiles fakeimglib.c -o fakeimglib.so */
int openimage(const char *s)
{
static int handle = 100;
fprintf(stderr, "opening %s\n", s);
return handle++;
}

View file

@ -0,0 +1,34 @@
// Kotlin Native version 0.5
import kotlinx.cinterop.*
import platform.posix.*
import platform.linux.*
typealias Func = (String)-> Int
var handle = 0
fun myOpenImage(s: String): Int {
fprintf(stderr, "internal openImage opens %s...\n", s)
return handle++
}
fun main(args: Array<String>) {
var imgHandle: Int
val imglib = dlopen("./fakeimglib.so", RTLD_LAZY)
if (imglib != null) {
val fp = dlsym(imglib, "openimage")
if (fp != null) {
val extOpenImage: CPointer<CFunction<Func>> = fp.reinterpret()
imgHandle = extOpenImage("fake.img")
}
else {
imgHandle = myOpenImage("fake.img")
}
dlclose(imglib)
}
else {
imgHandle = myOpenImage("fake.img")
}
println("opened with handle $imgHandle")
}

View file

@ -2,8 +2,8 @@ use NativeCall;
constant libX11 = '/usr/lib/x86_64-linux-gnu/libX11.so.6';
sub XOpenDisplay(Str $s --> Int) is native(libX11) {*}
sub XCloseDisplay(Int $i --> Int) is native(libX11) {*}
sub XOpenDisplay(Str $s --> int32) is native(libX11) {*}
sub XCloseDisplay(int32 $i --> int32) is native(libX11) {*}
if try my $d = XOpenDisplay ":0.0" {
say "ID = $d";

View file

@ -0,0 +1,7 @@
>>> import ctypes
>>> # libc = ctypes.cdll.msvcrt # Windows
>>> # libc = ctypes.CDLL('libc.dylib') # Mac
>>> libc = ctypes.CDLL('libc.so') # Linux and most other *nix
>>> libc.printf(b'hi there, %s\n', b'world')
hi there, world.
17

View file

@ -0,0 +1,10 @@
>>> from cffi import FFI
>>> ffi = FFI()
>>> ffi.cdef("""
... int printf(const char *format, ...); // copy-pasted from the man page
... """)
>>> C = ffi.dlopen(None) # loads the entire C namespace
>>> arg = ffi.new("char[]", b"world") # equivalent to C code: char arg[] = "world";
>>> C.printf(b"hi there, %s.\n", arg) # call printf
hi there, world.
17