@ohah/hwpjs 0.1.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/Hwpjs.podspec +27 -0
  2. package/LICENSE +21 -0
  3. package/README.md +87 -0
  4. package/android/CMakeLists.txt +50 -0
  5. package/android/build.gradle +110 -0
  6. package/android/gradle.properties +5 -0
  7. package/android/src/main/AndroidManifest.xml +3 -0
  8. package/android/src/main/java/rs/craby/hwpjs/HwpjsPackage.kt +59 -0
  9. package/android/src/main/jni/OnLoad.cpp +22 -0
  10. package/android/src/main/jni/include/CrabySignals.h +53 -0
  11. package/android/src/main/jni/include/cxx.h +1150 -0
  12. package/android/src/main/jni/include/ffi.rs.h +1041 -0
  13. package/android/src/main/jni/libs/arm64-v8a/libhwpjs-prebuilt.a +0 -0
  14. package/android/src/main/jni/libs/arm64-v8a/libreactnative-prebuilt.a +0 -0
  15. package/android/src/main/jni/libs/armeabi-v7a/libhwpjs-prebuilt.a +0 -0
  16. package/android/src/main/jni/libs/armeabi-v7a/libreactnative-prebuilt.a +0 -0
  17. package/android/src/main/jni/libs/x86/libhwpjs-prebuilt.a +0 -0
  18. package/android/src/main/jni/libs/x86/libreactnative-prebuilt.a +0 -0
  19. package/android/src/main/jni/libs/x86_64/libhwpjs-prebuilt.a +0 -0
  20. package/android/src/main/jni/libs/x86_64/libreactnative-prebuilt.a +0 -0
  21. package/android/src/main/jni/src/ffi.rs.cc +1179 -0
  22. package/android/stubs/CMakeLists.txt +31 -0
  23. package/android/stubs/Hwpjs_stub.cpp +9 -0
  24. package/android/stubs/Hwpjs_stub.h +12 -0
  25. package/cpp/CrabyUtils.hpp +91 -0
  26. package/cpp/CxxHwpjsModule.cpp +125 -0
  27. package/cpp/CxxHwpjsModule.hpp +53 -0
  28. package/cpp/bridging-generated.hpp +158 -0
  29. package/dist/browser.js +1 -0
  30. package/dist/hwpjs-napi.wasi-browser.js +61 -0
  31. package/dist/hwpjs-napi.wasi.cjs +113 -0
  32. package/dist/hwpjs.wasi-browser.js +61 -0
  33. package/dist/hwpjs.wasi.cjs +113 -0
  34. package/dist/index.d.ts +97 -0
  35. package/dist/index.js +577 -0
  36. package/dist/react-native/index.cjs +40 -0
  37. package/dist/react-native/index.cjs.map +1 -0
  38. package/dist/react-native/index.d.cts +22 -0
  39. package/dist/react-native/index.d.mts +22 -0
  40. package/dist/react-native/index.mjs +40 -0
  41. package/dist/react-native/index.mjs.map +1 -0
  42. package/dist/wasi-worker-browser.mjs +32 -0
  43. package/dist/wasi-worker.mjs +63 -0
  44. package/ios/HwpjsModuleProvider.mm +47 -0
  45. package/ios/framework/libhwpjs.xcframework/Info.plist +44 -0
  46. package/ios/framework/libhwpjs.xcframework/ios-arm64/libhwpjs-prebuilt.a +0 -0
  47. package/ios/framework/libhwpjs.xcframework/ios-arm64_x86_64-simulator/libhwpjs-prebuilt.a +0 -0
  48. package/ios/framework/libreactnative.xcframework/Info.plist +44 -0
  49. package/ios/framework/libreactnative.xcframework/ios-arm64/libreactnative-prebuilt.a +0 -0
  50. package/ios/framework/libreactnative.xcframework/ios-arm64_x86_64-simulator/libreactnative-prebuilt.a +0 -0
  51. package/ios/include/CrabySignals.h +53 -0
  52. package/ios/include/cxx.h +1150 -0
  53. package/ios/include/ffi.rs.h +1041 -0
  54. package/ios/src/ffi.rs.cc +1179 -0
  55. package/package.json +147 -0
  56. package/react-native.config.js +24 -0
package/Hwpjs.podspec ADDED
@@ -0,0 +1,27 @@
1
+ require "json"
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = "Hwpjs"
7
+ s.version = package["version"]
8
+ s.summary = package["description"]
9
+ s.homepage = package["homepage"]
10
+ s.license = package["license"]
11
+ s.authors = package["author"]
12
+
13
+ s.platforms = { :ios => min_ios_version_supported }
14
+ s.source = { :git => "https://ohah.github.io/hwpjs.git", :tag => "#{s.version}" }
15
+
16
+ s.source_files = ["ios/**/*.{m,mm,cc,cpp}", "cpp/**/*.cpp"]
17
+ s.vendored_frameworks = "ios/framework/libhwpjs.xcframework"
18
+ s.pod_target_xcconfig = {
19
+ "HEADER_SEARCH_PATHS" => [
20
+ '"${PODS_TARGET_SRCROOT}/cpp"',
21
+ '"${PODS_TARGET_SRCROOT}/ios/include"',
22
+ ].join(' '),
23
+ "CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
24
+ }
25
+
26
+ install_modules_dependencies(s)
27
+ end
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 N-API for Rust
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # `@napi-rs/package-template`
2
+
3
+ ![https://github.com/napi-rs/package-template/actions](https://github.com/napi-rs/package-template/workflows/CI/badge.svg)
4
+
5
+ > Template project for writing node packages with napi-rs.
6
+
7
+ # Usage
8
+
9
+ 1. Click **Use this template**.
10
+ 2. **Clone** your project.
11
+ 3. Run `yarn install` to install dependencies.
12
+ 4. Run `yarn napi rename -n [@your-scope/package-name] -b [binary-name]` command under the project folder to rename your package.
13
+
14
+ ## Install this test package
15
+
16
+ ```bash
17
+ yarn add @napi-rs/package-template
18
+ ```
19
+
20
+ ## Ability
21
+
22
+ ### Build
23
+
24
+ After `yarn build/npm run build` command, you can see `package-template.[darwin|win32|linux].node` file in project root. This is the native addon built from [lib.rs](./src/lib.rs).
25
+
26
+ ### Test
27
+
28
+ With [ava](https://github.com/avajs/ava), run `yarn test/npm run test` to testing native addon. You can also switch to another testing framework if you want.
29
+
30
+ ### CI
31
+
32
+ With GitHub Actions, each commit and pull request will be built and tested automatically in [`node@20`, `@node22`] x [`macOS`, `Linux`, `Windows`] matrix. You will never be afraid of the native addon broken in these platforms.
33
+
34
+ ### Release
35
+
36
+ Release native package is very difficult in old days. Native packages may ask developers who use it to install `build toolchain` like `gcc/llvm`, `node-gyp` or something more.
37
+
38
+ With `GitHub actions`, we can easily prebuild a `binary` for major platforms. And with `N-API`, we should never be afraid of **ABI Compatible**.
39
+
40
+ The other problem is how to deliver prebuild `binary` to users. Downloading it in `postinstall` script is a common way that most packages do it right now. The problem with this solution is it introduced many other packages to download binary that has not been used by `runtime codes`. The other problem is some users may not easily download the binary from `GitHub/CDN` if they are behind a private network (But in most cases, they have a private NPM mirror).
41
+
42
+ In this package, we choose a better way to solve this problem. We release different `npm packages` for different platforms. And add it to `optionalDependencies` before releasing the `Major` package to npm.
43
+
44
+ `NPM` will choose which native package should download from `registry` automatically. You can see [npm](./npm) dir for details. And you can also run `yarn add @napi-rs/package-template` to see how it works.
45
+
46
+ ## Develop requirements
47
+
48
+ - Install the latest `Rust`
49
+ - Install `Node.js@10+` which fully supported `Node-API`
50
+ - Install `yarn@1.x`
51
+
52
+ ## Test in local
53
+
54
+ - yarn
55
+ - yarn build
56
+ - yarn test
57
+
58
+ And you will see:
59
+
60
+ ```bash
61
+ $ ava --verbose
62
+
63
+ ✔ sync function from native code
64
+ ✔ sleep function from native code (201ms)
65
+
66
+
67
+ 2 tests passed
68
+ ✨ Done in 1.12s.
69
+ ```
70
+
71
+ ## Release package
72
+
73
+ Ensure you have set your **NPM_TOKEN** in the `GitHub` project setting.
74
+
75
+ In `Settings -> Secrets`, add **NPM_TOKEN** into it.
76
+
77
+ When you want to release the package:
78
+
79
+ ```bash
80
+ npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]
81
+
82
+ git push
83
+ ```
84
+
85
+ GitHub actions will do the rest job for you.
86
+
87
+ > WARN: Don't run `npm publish` manually.
@@ -0,0 +1,50 @@
1
+ # Auto generated by Craby. DO NOT EDIT.
2
+ cmake_minimum_required(VERSION 3.13)
3
+
4
+ project(craby-hwpjs)
5
+
6
+ set (CMAKE_VERBOSE_MAKEFILE ON)
7
+ set (CMAKE_CXX_STANDARD 20)
8
+
9
+ find_package(ReactAndroid REQUIRED CONFIG)
10
+
11
+ # Import the pre-built Craby library
12
+ add_library(hwpjs-lib STATIC IMPORTED)
13
+ set_target_properties(hwpjs-lib PROPERTIES
14
+ IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/src/main/jni/libs/${ANDROID_ABI}/libhwpjs-prebuilt.a"
15
+ )
16
+ target_include_directories(hwpjs-lib INTERFACE
17
+ "${CMAKE_SOURCE_DIR}/src/main/jni/include"
18
+ )
19
+
20
+ # Generated C++ source files by Craby
21
+ add_library(cxx-hwpjs SHARED
22
+ src/main/jni/OnLoad.cpp
23
+ src/main/jni/src/ffi.rs.cc
24
+ ../cpp/CxxHwpjsModule.cpp
25
+ )
26
+ target_include_directories(cxx-hwpjs PRIVATE
27
+ ../cpp
28
+ )
29
+
30
+ target_link_libraries(cxx-hwpjs
31
+ # android
32
+ ReactAndroid::reactnative
33
+ ReactAndroid::jsi
34
+ # hwpjs-lib
35
+ hwpjs-lib
36
+ )
37
+
38
+ # From ReactAndroid/cmake-utils/folly-flags.cmake
39
+ target_compile_definitions(cxx-hwpjs PRIVATE
40
+ -DFOLLY_NO_CONFIG=1
41
+ -DFOLLY_HAVE_CLOCK_GETTIME=1
42
+ -DFOLLY_USE_LIBCPP=1
43
+ -DFOLLY_CFG_NO_COROUTINES=1
44
+ -DFOLLY_MOBILE=1
45
+ -DFOLLY_HAVE_RECVMMSG=1
46
+ -DFOLLY_HAVE_PTHREAD=1
47
+ # Once we target android-23 above, we can comment
48
+ # the following line. NDK uses GNU style stderror_r() after API 23.
49
+ -DFOLLY_HAVE_XSI_STRERROR_R=1
50
+ )
@@ -0,0 +1,110 @@
1
+ def reactNativeArchitectures() {
2
+ def value = rootProject.getProperties().get("reactNativeArchitectures")
3
+ return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
4
+ }
5
+
6
+ buildscript {
7
+ ext.getExtOrDefault = {name ->
8
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['Hwpjs_' + name]
9
+ }
10
+
11
+ repositories {
12
+ google()
13
+ mavenCentral()
14
+ }
15
+
16
+ dependencies {
17
+ classpath "com.android.tools.build:gradle:8.7.2"
18
+ // noinspection DifferentKotlinGradleVersion
19
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
20
+ }
21
+ }
22
+
23
+ apply plugin: "com.android.library"
24
+ apply plugin: "kotlin-android"
25
+ apply plugin: "com.facebook.react"
26
+
27
+ def getExtOrIntegerDefault(name) {
28
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Hwpjs_" + name]).toInteger()
29
+ }
30
+
31
+ android {
32
+ namespace "rs.craby.hwpjs"
33
+
34
+ compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
35
+
36
+ defaultConfig {
37
+ minSdkVersion getExtOrIntegerDefault("minSdkVersion")
38
+ targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
39
+
40
+ externalNativeBuild {
41
+ cmake {
42
+ targets "cxx-hwpjs"
43
+ cppFlags "-frtti -fexceptions -Wall -Wextra -fstack-protector-all"
44
+ arguments "-DANDROID_STL=c++_shared", "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
45
+ abiFilters (*reactNativeArchitectures())
46
+ buildTypes {
47
+ debug {
48
+ cppFlags "-O1 -g"
49
+ }
50
+ release {
51
+ cppFlags "-O2"
52
+ }
53
+ }
54
+ }
55
+ }
56
+ }
57
+
58
+ externalNativeBuild {
59
+ cmake {
60
+ path "CMakeLists.txt"
61
+ }
62
+ }
63
+
64
+ buildFeatures {
65
+ buildConfig true
66
+ prefab true
67
+ }
68
+
69
+ buildTypes {
70
+ debug {
71
+ jniDebuggable true
72
+ }
73
+ release {
74
+ minifyEnabled false
75
+ externalNativeBuild {
76
+ cmake {
77
+ arguments "-DCMAKE_BUILD_TYPE=Release"
78
+ }
79
+ }
80
+ }
81
+ }
82
+
83
+ lintOptions {
84
+ disable "GradleCompatible"
85
+ }
86
+
87
+ compileOptions {
88
+ sourceCompatibility JavaVersion.VERSION_1_8
89
+ targetCompatibility JavaVersion.VERSION_1_8
90
+ }
91
+ }
92
+
93
+ repositories {
94
+ mavenCentral()
95
+ google()
96
+ }
97
+
98
+ def kotlin_version = getExtOrDefault("kotlinVersion")
99
+
100
+ dependencies {
101
+ implementation "com.facebook.react:react-android"
102
+ implementation "com.facebook.react:hermes-engine"
103
+ implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
104
+ }
105
+
106
+ react {
107
+ jsRootDir = file("../src/")
108
+ libraryName = "Hwpjs_stub"
109
+ codegenJavaPackageName = "rs.craby.hwpjs"
110
+ }
@@ -0,0 +1,5 @@
1
+ ReactNative_kotlinVersion=2.0.21
2
+ ReactNative_minSdkVersion=24
3
+ ReactNative_targetSdkVersion=34
4
+ ReactNative_compileSdkVersion=35
5
+ ReactNative_ndkVersion=27.1.12297006
@@ -0,0 +1,3 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
+ package="rs.craby.hwpjs">
3
+ </manifest>
@@ -0,0 +1,59 @@
1
+ package rs.craby.hwpjs
2
+
3
+ import com.facebook.react.BaseReactPackage
4
+ import com.facebook.react.bridge.NativeModule
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.bridge.ReactContextBaseJavaModule
7
+ import com.facebook.react.module.model.ReactModuleInfo
8
+ import com.facebook.react.module.model.ReactModuleInfoProvider
9
+ import com.facebook.react.turbomodule.core.interfaces.TurboModule
10
+ import com.facebook.soloader.SoLoader
11
+ import javax.annotation.Nonnull
12
+
13
+ class HwpjsPackage : BaseReactPackage() {
14
+ companion object {
15
+ val JNI_PREPARE_MODULE_NAME = setOf(
16
+ "__crabyHwpjs_JNI_prepare__"
17
+ )
18
+ }
19
+
20
+ init {
21
+ SoLoader.loadLibrary("cxx-hwpjs")
22
+ }
23
+
24
+ override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
25
+ if (name in JNI_PREPARE_MODULE_NAME) {
26
+ nativeSetDataPath(reactContext.filesDir.absolutePath)
27
+ return HwpjsPackage.TurboModulePlaceholder(reactContext, name)
28
+ }
29
+ return null
30
+ }
31
+
32
+ override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
33
+ return ReactModuleInfoProvider {
34
+ val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
35
+ JNI_PREPARE_MODULE_NAME.forEach { name ->
36
+ moduleInfos[name] = ReactModuleInfo(
37
+ name,
38
+ name,
39
+ false, // canOverrideExistingModule
40
+ false, // needsEagerInit
41
+ false, // isCxxModule
42
+ true, // isTurboModule
43
+ )
44
+ }
45
+ moduleInfos
46
+ }
47
+ }
48
+
49
+ private external fun nativeSetDataPath(dataPath: String)
50
+
51
+ class TurboModulePlaceholder(reactContext: ReactApplicationContext?, private val name: String) :
52
+ ReactContextBaseJavaModule(reactContext),
53
+ TurboModule {
54
+ @Nonnull
55
+ override fun getName(): String {
56
+ return name
57
+ }
58
+ }
59
+ }
@@ -0,0 +1,22 @@
1
+ // Auto generated by Craby. DO NOT EDIT.
2
+ #include <CxxHwpjsModule.hpp>
3
+ #include <ReactCommon/CxxTurboModuleUtils.h>
4
+ #include <jni.h>
5
+
6
+ jint JNI_OnLoad(JavaVM *vm, void *reserved) {
7
+ facebook::react::registerCxxModuleToGlobalModuleMap(
8
+ craby::hwpjs::modules::CxxHwpjsModule::kModuleName,
9
+ [](std::shared_ptr<facebook::react::CallInvoker> jsInvoker) {
10
+ return std::make_shared<craby::hwpjs::modules::CxxHwpjsModule>(jsInvoker);
11
+ });
12
+ return JNI_VERSION_1_6;
13
+ }
14
+
15
+ extern "C"
16
+ JNIEXPORT void JNICALL
17
+ Java_rs_craby_hwpjs_HwpjsPackage_nativeSetDataPath(JNIEnv *env, jclass clazz, jstring jDataPath) {
18
+ const char* cDataPath = env->GetStringUTFChars(jDataPath, nullptr);
19
+ auto dataPath = std::string(cDataPath);
20
+ env->ReleaseStringUTFChars(jDataPath, cDataPath);
21
+ craby::hwpjs::modules::CxxHwpjsModule::dataPath = dataPath;
22
+ }
@@ -0,0 +1,53 @@
1
+ #pragma once
2
+
3
+ #include "cxx.h"
4
+
5
+ #include <functional>
6
+ #include <memory>
7
+ #include <mutex>
8
+ #include <unordered_map>
9
+
10
+ namespace craby {
11
+ namespace reactnative {
12
+ namespace signals {
13
+
14
+ using Delegate = std::function<void(const std::string& signalName)>;
15
+
16
+ class SignalManager {
17
+ public:
18
+ static SignalManager& getInstance() {
19
+ static SignalManager instance;
20
+ return instance;
21
+ }
22
+
23
+ void emit(uintptr_t id, rust::Str name) const {
24
+ std::lock_guard<std::mutex> lock(mutex_);
25
+ auto it = delegates_.find(id);
26
+ if (it != delegates_.end()) {
27
+ it->second(std::string(name));
28
+ }
29
+ }
30
+
31
+ void registerDelegate(uintptr_t id, Delegate delegate) const {
32
+ std::lock_guard<std::mutex> lock(mutex_);
33
+ delegates_.insert_or_assign(id, delegate);
34
+ }
35
+
36
+ void unregisterDelegate(uintptr_t id) const {
37
+ std::lock_guard<std::mutex> lock(mutex_);
38
+ delegates_.erase(id);
39
+ }
40
+
41
+ private:
42
+ SignalManager() = default;
43
+ mutable std::unordered_map<uintptr_t, Delegate> delegates_;
44
+ mutable std::mutex mutex_;
45
+ };
46
+
47
+ inline const SignalManager& getSignalManager() {
48
+ return SignalManager::getInstance();
49
+ }
50
+
51
+ } // namespace signals
52
+ } // namespace reactnative
53
+ } // namespace craby