wallet-core icon indicating copy to clipboard operation
wallet-core copied to clipboard

[Wasm] Generate properties / methods for enums

Open hewigovens opened this issue 2 years ago • 4 comments

properties in TWCoinType.h are not generated right now

hewigovens avatar Jul 19 '22 22:07 hewigovens

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 avatar Aug 18 '22 02:08 hewigovens

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()
    ;
}

hewigovens avatar Oct 05 '22 20:10 hewigovens

@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 avatar Jan 24 '23 08:01 Milerius

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

Milerius avatar Feb 06 '23 07:02 Milerius