diff options
| author | Fuwn <[email protected]> | 2022-03-13 07:17:44 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-03-13 07:17:44 -0700 |
| commit | 58966d1102cc6baafd90269ab2523d3f6da98e55 (patch) | |
| tree | a48a0bef996bf2cef942a6d4a0b9ea23c668afe3 /examples | |
| download | senpy-ffi-58966d1102cc6baafd90269ab2523d3f6da98e55.tar.xz senpy-ffi-58966d1102cc6baafd90269ab2523d3f6da98e55.zip | |
feat(senpy_ffi): 0.1.0 :star:
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/ffi.lua | 34 | ||||
| -rw-r--r-- | examples/ffi.php | 25 | ||||
| -rw-r--r-- | examples/ffi.pl | 23 | ||||
| -rw-r--r-- | examples/ffi.py | 86 |
4 files changed, 168 insertions, 0 deletions
diff --git a/examples/ffi.lua b/examples/ffi.lua new file mode 100644 index 0000000..b7f3a10 --- /dev/null +++ b/examples/ffi.lua @@ -0,0 +1,34 @@ +local ffi = require("ffi") + +local os = ffi.os +local extension + +if os == "Linux" then + extension = "so" +elseif os == "Windows" then + extension = "dll" +else + extension = "dylib" +end + +ffi.cdef[[ + int status(void); +]] + +local C = ffi.load("target/debug/senpy_ffi." .. extension) + +-- status +local c_status = C.status() +local status + +if c_status == 1 then + status = "up" +elseif c_status == 0 then + status = "down" +elseif c_status == -1 then + status = "not down, but unreachable" +else + status = "unknown" +end + +print("status: api.senpy.club is " .. status) diff --git a/examples/ffi.php b/examples/ffi.php new file mode 100644 index 0000000..cf22c4a --- /dev/null +++ b/examples/ffi.php @@ -0,0 +1,25 @@ +<?php + +$extension = (PHP_OS_FAMILY == "Darwin" ? "dylib" : "so"); + +$ffi = FFI::cdef( + "int status(void);", + "target/debug/libsenpy_ffi.$extension" +); + +$c_status = $ffi->status(); +$status; + +if ($c_status == 1): + $status = "up"; +elseif ($c_status == 0): + $status = "down"; +elseif ($c_status == -1): + $status = "not down, but unreachable"; +else: + $status = "unknown"; +endif; + +echo "status: api.senpy.club is " . $status . "\r\n"; + +?> diff --git a/examples/ffi.pl b/examples/ffi.pl new file mode 100644 index 0000000..1ea3802 --- /dev/null +++ b/examples/ffi.pl @@ -0,0 +1,23 @@ +use v5.30.0; +use FFI::Raw; + +my $status = FFI::Raw->new( + "target/debug/libsenpy_ffi.so", + "status", + FFI::Raw::int +); + +my $c_status = $status->call(); +my $senpy_status; + +if ($c_status == 1) { + $senpy_status = "up"; +} elsif ($c_status == 0) { + $senpy_status = "down"; +} elsif ($c_status == -1) { + $senpy_status = "not down, but unreachable"; +} else { + $senpy_status = "unknown"; +} + +say "status: api.senpy.club is " . $senpy_status; diff --git a/examples/ffi.py b/examples/ffi.py new file mode 100644 index 0000000..19cc1ac --- /dev/null +++ b/examples/ffi.py @@ -0,0 +1,86 @@ +import sys +from cffi import FFI + +prefix: str = {"win32": ""}.get(sys.platform, "lib") +extension: str = {"darwin": ".dylib", "win32": ".dll"}.get(sys.platform, ".so") + +ffi = FFI() +ffi.cdef( + """ + typedef struct { char *language; char *image; } random_t; + + char **language(const char *); + + char **languages(void); + + random_t *random_new(void); + void random_populate(random_t *); + void random_free(random_t *); + char *random_get(const random_t *, const char *); + + int status(void); + """ +) + +C = ffi.dlopen("target/debug/{}senpy_ffi{}".format(prefix, extension)) + +# languages +languages = C.languages() +languages_list: list[str] = [] + +for i in range(int(ffi.string(languages[0]))): + languages_list.append(ffi.string(languages[i]).decode("utf-8")) + +languages_list.pop(0) + +print("languages:", languages_list) + +# random +class Random: + def __init__(self) -> None: + self.obj = C.random_new() + + def __enter__(self): + return self + + def __exit__(self, _, __, ___) -> None: + C.random_free(self.obj) + + def populate(self) -> None: + C.random_populate(self.obj) + + def get(self) -> (str, str): + return ( + ffi.string(C.random_get(self.obj, "language".encode("utf-8"))).decode("utf-8"), + ffi.string(C.random_get(self.obj, "image".encode("utf-8"))).decode("utf-8"), + ) + +with Random() as random: + random.populate() + print("random:", random.get()) + +# status +c_status: int = C.status() +status: str = "" + +if c_status == 1: + status = "up" +elif c_status == 0: + status = "down" +elif c_status == -1: + status = "not down, but unreachable" +else: + status = "unknown" + +print("status: api.senpy.club is", status) + +# language +images = C.language("ASM".encode("utf-8")) +images_list: list[str] = [] + +for i in range(int(ffi.string(images[0]))): + images_list.append(ffi.string(images[i]).decode("utf-8")) + +images_list.pop(0) + +print("images:", images_list) |