trustwallet / wallet-core

Cross-platform, cross-blockchain wallet library.
https://developer.trustwallet.com/wallet-core
Apache License 2.0
2.85k stars 1.6k forks source link

[Wasm] Generate properties / methods for enums #2391

Open hewigovens opened 2 years ago

hewigovens commented 2 years ago

properties in TWCoinType.h are not generated right now

hewigovens commented 2 years ago

Currently we're using Embind enum_<TWCoinType>("CoinType") to export enum types to Javascript

EMSCRIPTEN_BINDINGS(Wasm_TWCoinType) {
    enum_<TWCoinType>("CoinType")
    ...
}

It's not very intuitive to add properties / methods support, so it might be better to use class_<TWCoinType>("TWCoinType") instead, easy to add property and function later, but with this approach, we need to codegen Typescript enum for CoinType too to list all the enum cases, pseudo code

import { TWCoinType } from "<path or package>"

enum CoinType {
  bitcoin = 0,
  ethereum = 60,
}

// a method to convert CoinType to TWCoinType and call properties / methods 

https://www.typescriptlang.org/docs/handbook/enums.html

hewigovens commented 2 years ago

After some testing, we'd better to generate extra Enum class for each enum

class CoinTypeEnum {
  CoinTypeEnum(TWCoinType value)
    : value(value)
  {}

  TWCoinType getValue() const { return value; }
  void setValue(TWCoinType value_) { value = value_; }
}

EMSCRIPTEN_BINDINGS(Wasm_TWCoinType_EnumClass) {
    class_<CoinTypeEnum>("CoinTypeEnum")
          .constructor()
          .property()
          .function()
    ;
}
Milerius commented 1 year ago

@MaximPestryakov Here the idea is too create a class additionally to the enum in order to have properties, the codegen tools need to iterate through possible properties and gen as described by @hewigovens above. properties / function are in TWCoinType.h

Milerius commented 1 year ago

Similar to swift enum extension it's how we should generate the code for wasm enum:

header:

// Copyright © 2017-2023 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
//
// This is a GENERATED FILE, changes made here WILL BE LOST.
//

#pragma once

#include "TrustWalletCore/TWCoinType.h"

#include <emscripten/bind.h>

using namespace emscripten;

namespace TW::Wasm {

class WasmCoinTypeExtension {
public:
    TWCoinType value;

    WasmCoinTypeExtension(TWCoinType value) : value(value) {}

    auto blockchain();

    // others function to codegen
};

}

c++:

// Copyright © 2017-2023 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
//
// This is a GENERATED FILE, changes made here WILL BE LOST.
//

#include "CoinTypeExtension.h"

using namespace emscripten;

namespace TW::Wasm {
    auto WasmCoinTypeExtension::blockchain() {
        return TWCoinTypeBlockchain(this->value);
    }

    EMSCRIPTEN_BINDINGS(Wasm_CoinTypeExtension) {
        class_<WasmCoinTypeExtension>("CoinTypeExtension")
            .constructor<TWCoinType>()
            .function("blockchain", &WasmCoinTypeExtension::blockchain);
    }
}

Swift extensions: https://github.com/trustwallet/wallet-core/blob/master/codegen/lib/templates/swift/enum_extension.erb May force to apply: https://github.com/trustwallet/wallet-core/blob/master/codegen/lib/templates/ts/class_d.erb on this new created helper classes in order to generate the typescript