Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Wasm] Generate properties / methods for enums #2391

Open
hewigovens opened this issue Jul 19, 2022 · 4 comments
Open

[Wasm] Generate properties / methods for enums #2391

hewigovens opened this issue Jul 19, 2022 · 4 comments
Labels

Comments

@hewigovens
Copy link
Contributor

properties in TWCoinType.h are not generated right now

@hewigovens hewigovens changed the title [Wasm] Generate properties for enums [Wasm] Generate properties / methods for enums Aug 18, 2022
@hewigovens
Copy link
Contributor Author

hewigovens commented Aug 18, 2022

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 hewigovens assigned hewigovens and unassigned Milerius Aug 18, 2022
@hewigovens hewigovens assigned miloserdow and unassigned hewigovens Oct 5, 2022
@hewigovens
Copy link
Contributor Author

hewigovens commented Oct 5, 2022

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
Copy link
Collaborator

Milerius commented Jan 24, 2023

@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
Copy link
Collaborator

Milerius commented Feb 6, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants
@hewigovens @miloserdow @Milerius and others