moonbit-docs icon indicating copy to clipboard operation
moonbit-docs copied to clipboard

Export to JS output Struct::method logic upgrade

Open DmitrijOkeanij opened this issue 1 year ago • 4 comments

Moonbit is great! I am thinking about writing Core of my browser project on Moonbit. I am trying to start and face export case (or problem) I create test code of Xxx struct with new functionality

struct Xxx {
  id: Int;
  title: String;
  text: String;
} derive(Show)

struct Glob {
  mut maxId : Int
}

pub fn next(self: Glob) -> Int {
  self.maxId = self.maxId + 1;
  self.maxId
}

let glob : Glob = {maxId : 0}

pub fn Xxx::new() -> Xxx {
  {
    id: glob.next(),
    title: "title1",
    text: "text11"
  }
}

When I tried to export my class, object Xxx to my browser JavaScript I tryed this code

{
  "link": {
    "js": {
      "exports": [
        "Xxx::new", // not working 
         "Xxx:new", // not working 
         "Xxx.new", // not working 
         "new" // working, but result exported function is just new
      ],
      "format": "esm"
    }
  }
}

"new" is working, but result exported function is just named new

  1. it conflicts with js new
  2. it is full separete of my Xxx struct, on js layer not connected to it, and I can not connect this logically. Just new.

I think need to make some good model to make exported code more freandly and structured. May be to allow "Xxx::new" to export with name Xxx_new or to allow any custom export name like. Or to make all Xxx methods export in container object Xxx { method1, method2 } to use as Xxx.method?

And the second question, is there way to export full Xxx struct with all functions and all related data?

Sorry for my bad english

DmitrijOkeanij avatar Nov 23 '24 18:11 DmitrijOkeanij

Hi, you can define alias as new:xxx_new.

Currently we only support exporting plain functions, i.e. none methods. We do not export things at type level.

peter-jerry-ye avatar Nov 24 '24 00:11 peter-jerry-ye

Do you have plans to export others staff?

I am thinking about how to export things as core domain objects (constants, functions, data types and others) in JS Project (Vue, Svelte...) For example, I have Page item with id, title, and text in domain logic of my project. I want to write all core on moonbit and then use it from JS (Svelte or Vue) on site level.

The best way is to write directly on moonbit all site on Vue , Svelte...

This is a question about plans of moonbit, and future capabilities.

DmitrijOkeanij avatar Nov 24 '24 08:11 DmitrijOkeanij

It is under discussion. We currently have generated .d.ts type signature, and I think we currently only use any. We need to determine our ABI and see how we export them.

peter-jerry-ye avatar Nov 25 '24 01:11 peter-jerry-ye

Do you have plans to export others staff?

No, at least in near future.

We only support export functions for now. If you faced name conflicts, you can use this workaround:

fn xxx_new(...) {
  XX::new(...)
}

hackwaly avatar Dec 25 '24 10:12 hackwaly