From 501b6b0d75d1864d9e9adb1749f0109ca3ebd4a2 Mon Sep 17 00:00:00 2001 From: bakacai <142713922+bakacai@users.noreply.github.com> Date: Sat, 27 Jul 2024 14:15:23 +0800 Subject: [PATCH] add cangjie binding (#117) --- README.md | 46 +++++++++++++++++++++++++++++++++++ cangjie/README.md | 14 +++++++++++ cangjie/c-src/CMakeLists.txt | 10 ++++++++ cangjie/c-src/ruapu-binding.c | 14 +++++++++++ cangjie/cjpm.toml | 16 ++++++++++++ cangjie/src/main.cj | 11 +++++++++ cangjie/src/ruapu/ruapu.cj | 30 +++++++++++++++++++++++ 7 files changed, 141 insertions(+) create mode 100644 cangjie/README.md create mode 100644 cangjie/c-src/CMakeLists.txt create mode 100644 cangjie/c-src/ruapu-binding.c create mode 100644 cangjie/cjpm.toml create mode 100644 cangjie/src/main.cj create mode 100644 cangjie/src/ruapu/ruapu.cj diff --git a/README.md b/README.md index ea76537..0d5355b 100644 --- a/README.md +++ b/README.md @@ -495,6 +495,52 @@ class Example { + + + +### ruapu with cangjie + + + + +
+ +Compile ruapu library + +```bash +cd cangjie +cd c-src +cmake . +make +``` +run example +```bash +cd cangjie +cjpm run +``` +or compile example +```bash +cd cangjie +cjpm build +./target/release/bin/main +``` + + +Use ruapu in cangjie + +```swift +import ruapu.* +main(): Int64 { + ruapu_init() + let neon_supported = ruapu_supports("neon") + println("supports neon: ${neon_supported}") + let d = ruapu_rua() + for (i in d) { + println(i) + } + return 0 +} +```
diff --git a/cangjie/README.md b/cangjie/README.md new file mode 100644 index 0000000..459e648 --- /dev/null +++ b/cangjie/README.md @@ -0,0 +1,14 @@ +## ruapu cangjie + +当前代码仓颉版本: `0.51.4` + +> 仓颉目前处于内测阶段,不保证该代码在更新的仓颉编译器版本中可用 + +```bash +cd cangjie +cd c-src +cmake . +make +cd .. +cjpm run +``` \ No newline at end of file diff --git a/cangjie/c-src/CMakeLists.txt b/cangjie/c-src/CMakeLists.txt new file mode 100644 index 0000000..29a3c1b --- /dev/null +++ b/cangjie/c-src/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.5) + +project(ruapu) + +include_directories(../../) + + +add_library(ruapu + ruapu-binding.c +) \ No newline at end of file diff --git a/cangjie/c-src/ruapu-binding.c b/cangjie/c-src/ruapu-binding.c new file mode 100644 index 0000000..1cdb38e --- /dev/null +++ b/cangjie/c-src/ruapu-binding.c @@ -0,0 +1,14 @@ +#define RUAPU_IMPLEMENTATION +#include "ruapu.h" + +void ruapu_init_c() { + ruapu_init(); +} + +int ruapu_supports_c(const char* isa) { + return ruapu_supports(isa); +} + +const char* const* ruapu_rua_c() { + return ruapu_rua(); +} \ No newline at end of file diff --git a/cangjie/cjpm.toml b/cangjie/cjpm.toml new file mode 100644 index 0000000..4e54508 --- /dev/null +++ b/cangjie/cjpm.toml @@ -0,0 +1,16 @@ +[dependencies] + +[package] + cjc-version = "0.51.4" + compile-option = "" + description = "ruapu" + link-option = "" + name = "ruapu_cangjie" + output-type = "executable" + src-dir = "" + target-dir = "" + version = "1.0.0" + package-configuration = {} + +[ffi.c] + ruapu = { path = "./c-src/" } diff --git a/cangjie/src/main.cj b/cangjie/src/main.cj new file mode 100644 index 0000000..cbc0ab7 --- /dev/null +++ b/cangjie/src/main.cj @@ -0,0 +1,11 @@ +import ruapu.* +main(): Int64 { + ruapu_init() + let neon_supported = ruapu_supports("neon") + println("supports neon: ${neon_supported}") + let d = ruapu_rua() + for (i in d) { + println(i) + } + return 0 +} \ No newline at end of file diff --git a/cangjie/src/ruapu/ruapu.cj b/cangjie/src/ruapu/ruapu.cj new file mode 100644 index 0000000..8781b80 --- /dev/null +++ b/cangjie/src/ruapu/ruapu.cj @@ -0,0 +1,30 @@ +package ruapu + +from std import collection.ArrayList + +foreign func ruapu_init_c(): Unit + +foreign func ruapu_supports_c(s: CString): Int32 + +foreign func ruapu_rua_c(): CPointer + +public func ruapu_init(): Unit { + unsafe{ ruapu_init_c() } +} + +public func ruapu_supports(isa: String): Bool { + var isa_c = unsafe { LibC.mallocCString(isa) } + let result_c = unsafe { ruapu_supports_c(isa_c) } + return result_c == 1 +} + +public func ruapu_rua(): Array { + var ruaList = ArrayList() + var sp = unsafe { ruapu_rua_c() } + while(!unsafe { sp.read().isNull() }) { + let temp = unsafe { sp.read() } + ruaList.append(temp.toString()) + sp = unsafe { sp + 1 } + } + return Array(ruaList) +} \ No newline at end of file