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

Export to JS output Struct::method logic upgrade #333

Open
DmitrijOkeanij opened this issue Nov 23, 2024 · 4 comments
Open

Export to JS output Struct::method logic upgrade #333

DmitrijOkeanij opened this issue Nov 23, 2024 · 4 comments
Assignees

Comments

@DmitrijOkeanij
Copy link

DmitrijOkeanij commented Nov 23, 2024

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 DmitrijOkeanij changed the title Export Struct::method Export to JS output Struct::method logic upgrade Nov 23, 2024
@peter-jerry-ye
Copy link
Collaborator

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.

@DmitrijOkeanij
Copy link
Author

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.

@peter-jerry-ye
Copy link
Collaborator

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.

@hackwaly
Copy link

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

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

No branches or pull requests

3 participants