torch2424 / as-bind

Isomorphic library to handle passing high-level data structures between AssemblyScript and JavaScript 🤝🚀
https://torch2424.github.io/as-bind/
MIT License
245 stars 23 forks source link

feat: type-generation (fixes #98) #101

Closed mathe42 closed 2 years ago

mathe42 commented 3 years ago

This PR additonaly includes some code simplification in the transform - so the code is more readable.

Todo:

This PR includes type support for features that as-bind currently can not handle!

torch2424 commented 3 years ago

@mathe42 I merged in #100 , so feel free to rebase on that for the conflicting files 😄

And like last time, let me know when this is close to done, and I can start doing reviews 😄 🎉

mathe42 commented 3 years ago

@torch2424 I have currently no idea how to configure if the types are generated and where they are saved...

I thought of useing a compiler flag like "asc --as-bind-types file.d.ts". Or something in asconfig.json but there is no way to access it in the transform.

The only other thing is that the generated types are not well formated and as developers propably want them in there git they should be properly formated.

So please have a look...

Usage with TS is:

import * as AsBind from 'as-bind'

import {IMPORT, EXPORT} from './path/to/type/file'

const instance = AsBind.instantiate<IMPORT, EXPORT>(fetch('...'), {/*imports*/})
torch2424 commented 3 years ago

@mathe42

I have currently no idea how to configure if the types are generated and where they are saved... I thought of useing a compiler flag like "asc --as-bind-types file.d.ts". Or something in asconfig.json but there is no way to access it in the transform.

Ahhh, that's an interesting problem! 😯 Well, there's nothing stopping us from shipping down a node script script as a CLI app for this task! Like we could allow for:

as-bind generate-types [Path to as-bind JSON file].

So if you think we can write a solution that uses the final exported JSON file, I think we'd be good to go? 😄

The only other thing is that the generated types are not well formated and as developers propably want them in there git they should be properly formated.

Could you explain here? Do you mean the generated typings file won't be formatted correctly? If so, I wonder if we can use prettier from within a CLI app, to auto format for us? 🤔

mathe42 commented 3 years ago

I just realized that I'm stupid 😂

All we need is to run tsc with some custom content prepended on each file like:

type u8 = number
type StaticArray<T> = Array<T>

// ...

That should be much better.... And after tsc we just add some transforms to match some special cases if needed. (optimaly there are no such cases)....

torch2424 commented 3 years ago

@mathe42

I just realized that I'm stupid joy

You are not stupid haha! :) I know you meant it in a fun way, but it's worth saying you are not just in case 😄

Re: solution

That sounds great to me! 😄 I'm stoked you came up with this!


So! I also wanted to mention real quick, my apologies for the late reply! I had a busy week! I'm also going to be on vacation next week, so I'll try and get through the reviews and things then if that is okay! 😄