Git Product home page Git Product logo

react-native-haskell-shelley's Introduction

DISCLAIMER: the code contained in this repo is experimental and still in WIP status.

react-native-haskell-shelley

Getting started

$ npm install react-native-haskell-shelley --save

Mostly automatic installation

$ react-native link react-native-haskell-shelley

Usage

See examples in App.js.

How to add new classes and functions

The process is basically as follows: we start by writting a rust wrapper of some struct method from our target rust library. Both iOS and Android require specific rust wrappers, so there are separate folder (rust/ios and rust/android). When this project is compiled by the host react native app, all the wrappers are transformed into a native library. In Android, java can directly interact with the rust binaries (the instructions for compiling our rust library are in build.gradle), while in iOS there is an additional step in which the rust library is transformed into C, with which can we easily interact with through Objective-C. This intermediate step is contained in ios/build.sh, where we basically use cbindgen to automatically generate C binaries as well as C headers (which are written in rust/include/react_native_haskell_shelley.h). After writing the corresponding iOS and Android wrappers, we finally just write a simple JS library in index.js and define its types in index.d.ts.

Android

For every new class:

  • Add a new rust module named <class_name.rs> (snake_case) in rust/src/android/. Here is where we add rust wrappers of the corresponding rust struct methods from the library we want to bind. You can check other modules to see how this is done in rust/src/android/.
  • Add a use declaration in rust/src/android/mod.rs

Now you are ready to add functions for your class/rust structure.

For every new function in the module:

  • Add a rust wrapper of the form: pub unsafe extern "C" fn Java_io_emurgo_rnhaskellshelley_Native_functionNameInCamelCase

  • Declare an equivalent java function to the target rust function in android/src/main/java/io/emurgo/rnhaskellshelley/Native.java. The function name must be in camelCase and corresponds to the last part (in camelCase) of the rust wrapper signature mentioned above.

  • Add the implementation of the java function that will be exposed to react native in android/src/main/java/io/emurgo/rnhaskellshelley/HaskellShelleyModule.java. Note that the types and signatures in HaskellShelleyModule.java are different from Native.jave. In the former, we use java types while in the later we use rust types, ie., we match the signatures of the corresponding rust wrappers.

iOS

For every new class:

  • Add a new rust module named <class_name.rs> (snake_case) in rust/src/ios/.
  • Add a use declaration in rust/src/ios/mod.rs

As you may have noticed, the two steps above are equivalent to those with Android. The difference is that the rust wrappers are written differently.

For every new function in the module:

  • Add a rust wrapper of the form: pub unsafe extern "C" fn function_name_in_snake_case
  • Write a iOS-native wrapper in Objective-C in ios/HaskellShelley.m. In contrast to Android (java), the iOS native wrappers can't directly interact with rust so we actually use a C library. This C library is automatically generated by cargo when the project is built.

Additional steps in Rust

  • Any new struct from cardano_serialization_lib that is required either as an input or output in our rust wrappers must implement the RPtrRepresentable trait. Add them in ptr_impl.rs.

Javascript

For new classes and methods:

  1. Add the javascript class signature in index.d.ts
  2. Add the javascript class implementation index.js

react-native-haskell-shelley's People

Contributors

dependabot[bot] avatar taipa-ibl avatar truchq avatar v-almonacid avatar

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.