Giter Site home page Giter Site logo

kotlin-native-bigdecimal's Introduction

Kotlin/Native BigDecimal (Kotlin Multiplatform for iOS)

This is a drop-in replacement for java.math.BigDecimal. If you have a Kotlin/JVM project, want to port it to Kotlin/Native iOS, but struggle because you are using java.math.BigDecimal and cannot find an implementation, this is the library you want to use.

The code is production-ready and used in the iOS port of HiPER Scientific Calculator.

This library has the same API as java.math.BigDecimal, all you need to do is to add the GitHub Packages repository and the library to your build.gradle.kts, because binary packages are now available.

val iosArm64Main by getting {
    dependencies {
        implementation("com.crossoid:kotlin-native-bigdecimal:1.0")
    }
}

Then, you can import the BigDecimal classes as if you were developing for Kotlin/JVM:

import java.math.BigDecimal

Please see the example project for exact details how to import the library and how to use it.

How does it work

The library is a port of java.math.BigDecimal from the Android Open Source Project that I've converted from Java to Kotlin using the Android Studio's Java -> Kotlin converter, fixed and/or adapted various places, and implemented the native part.

For the native part, I've rewritten the JNI version to Kotlin/Native using the cinterops.

The BigDecimal as a whole builds on top of the Google's BoringSSL BIGNUM implementation, so you will need to build a BoringSSL static library for all this to work. If you are interested in the details, see bignum/README.md; or just continue reading how to build it all.

Building

I've tested this both with the iOS Simulator and a device. To build, do:

  • Install dependencies for building BoringSSL

    You need CMake and Go.

  • Build BoringSSL

    See bignum/README.md for details, but mostly down to:

      cd bignum/ios
    
      git clone [email protected]:google/boringssl.git
      cd boringssl
      mkdir build-arm64
      cd build-arm64
    

    Simulator:

      /Applications/CMake.app/Contents/bin/cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS=-fPIC -DCMAKE_OSX_SYSROOT=iphonesimulator -DCMAKE_OSX_ARCHITECTURES=arm64 ..
      make -j8
    

    Device:

      /Applications/CMake.app/Contents/bin/cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS=-fPIC -DCMAKE_OSX_SYSROOT=iphoneos -DCMAKE_OSX_ARCHITECTURES=arm64 ..
      make -j8
    
      cd ../../../..
    
  • Build the BigDecimal.klib and BigDecimal-cinterop-boringssl.klib

    Simulator:

      ./gradlew compileKotlinIosSimulatorArm64
    

    (resulting libraries are in: build/classes/kotlin/iosSimulatorArm64/main/)

    Device:

      ./gradlew compileKotlinIosArm64
    

    (resulting libraries are in: build/classes/kotlin/iosArm64/main/)

  • Incorporate the resulting klibs into your project

    You can copy them to some convenient location, and then update your build.gradle.kts:

      val iosMain by getting {
          dependencies {
              implementation(files("libs/BigDecimal.klib"))
              implementation(files("libs/BigDecimal-cinterop-boringssl.klib"))
          }
      }
    

    Then change the imports of java.math.BigDecimal to kendy.math.BigDecimal and you are done.

Contributing

I use this code in production in the iOS port of HiPER Scientific Calculator where it undergoes over 2000 unit tests, so I am pretty sure it is stable and produces good results.

As such, it does not need too much work, apart from making it more generally usable as a drop-in replacement for the Java BigDecimal (like convenience classes, extension functions, etc.). But if I find bugs, I will fix them.

I'll be excited to incorporate your patches if you want to contribute! Here are some ideas what to improve if you want to help:

  • The code builds with various warnings - I'll appreciate patches to fix those.

  • The build of BoringSSL has to be done manually - would be great to extend the build.gradle.kts to clone / build it as part of the build just out of the box, and for the correct platform.

  • Automate the build of the Kotlin/JVM part too - so that it can be used for Android development too if necessary for some reason.

  • Unit tests! There are no incorporated automated tests so far, would be great to have at least few as a start...

  • And anything else you'd be interested in :-)

For patches, please just do PR's & I'll review them. For bugs, please create GitHub issues.

kotlin-native-bigdecimal's People

Contributors

kendy avatar asemy avatar

Stargazers

 avatar  avatar Bakht Ergashev avatar Tóth László avatar Braden Farmer avatar PhiNM avatar Brent Zey avatar Maximilian Pröll avatar jershell avatar Jose Rubio avatar Alexander Brandon Coles avatar Onuralp SEZER avatar  avatar Marc Dugger avatar Jeffrey Sadeli avatar Gennadi Kudrjavtsev avatar Gonçalo Ferreira avatar シミズヒカリ avatar Roman Podymov avatar Simon Walter avatar Momid avatar WukongRework.exe BROKE avatar Alex Riedler avatar Alex avatar RyuNen344 avatar M R 3 Y avatar 吴上阿吉 avatar Louis CAD avatar Baptiste Candellier avatar Aleksey Mikhailov avatar Guillermo Orellana avatar xiaobailong24 avatar Suresh avatar Christoph avatar Art Shendrik avatar pablichjenkov avatar Daniel Lin avatar Scott Pierce avatar Emanuele avatar LEE avatar Elián Fabián avatar  avatar Jindřich Houska avatar Jørgen Svennevik Notland avatar  avatar Trey Perry avatar Tobias avatar  avatar

Watchers

 avatar Bakht Ergashev avatar

kotlin-native-bigdecimal's Issues

kotlinx.serialization support

I would love to see kotlinx.serialization support out of the box, like kotlinx.datetime does it. What do you think?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.