ammo.js icon indicating copy to clipboard operation
ammo.js copied to clipboard

TypeScript declarations?

Open brundonsmith opened this issue 6 years ago • 14 comments

It would be awesome to have TypeScript declarations available. I don't know if these would have to be created manually or if they could somehow be scraped from the C++ ones, but I'd think the latter could be done at least as a starting point.

brundonsmith avatar Jan 31 '19 01:01 brundonsmith

Interesting idea! I think we could best do this by modifying the webidl binder in emscripten, so that as it generates all the glue C++ and JS (from the webidl file) it can also emit a TypeScript file on the side.

The tool is here if someone wants to try (let me know if you have questions): https://github.com/emscripten-core/emscripten/blob/incoming/tools/webidl_binder.py

kripken avatar Jan 31 '19 03:01 kripken

This may be of interest: https://github.com/charto/nbind

devmattrick avatar Mar 09 '19 01:03 devmattrick

I've written a preliminary Node program which attempts to scrape the Bullet docs online and generate Ammo.js type declarations. It still has issues, but anecdotally it appears to get 60%-80% correct so far.

https://github.com/brundonsmith/ammo-scraper

brundonsmith avatar Mar 17 '19 01:03 brundonsmith

Question: how are namespaces represented in the JS version? It seemed like, from the WebIDL docs, you can just replace :: with _ in the member names? Is that correct?

brundonsmith avatar Mar 17 '19 03:03 brundonsmith

I ended up finding that I should just strip the namespaces altogether.

In its current state, this set of types more or less correctly types the terrain example script. The exceptions are Ammo._malloc and Ammo.HEAPF32. Are these WebIDL artifacts? Do they just mirror C++ built-in constructs?

brundonsmith avatar Mar 19 '19 01:03 brundonsmith

@kripken is there any progress or plans on this? I would love to see a .d.ts coming straight from the ammo.js repository.

giniedp avatar Sep 19 '19 19:09 giniedp

There is some work in emscripten on typescript declaration support, which may eventually be relevant here, but nothing yet (https://github.com/emscripten-core/emscripten/issues/7083#issuecomment-531224615).

kripken avatar Sep 19 '19 19:09 kripken

We would definitely appreciate contributions in that area!

kripken avatar Sep 19 '19 19:09 kripken

thx. I've been working on a .idl -> .d.ts generator in the past hours. This is my progress so far https://github.com/giniedp/ammojs-typed

I had to make some adjustments to the idl to make it work. All steps are described in the readme. I am planning a more thorough integration check and a couple examples, maybe hosted on https://stackblitz.com

However the embind approach seems to be more reasonable. I'd have a look into it too.

giniedp avatar Sep 20 '19 12:09 giniedp

so i have an example on stackblitz out now https://stackblitz.com/edit/ammojs-typed-falling-cubes

giniedp avatar Sep 22 '19 19:09 giniedp

That's a cool idea @giniedp! That would make more sense for ammo actually, since we don't use embind.

Might be worth opening an issue on emscripten as other people might be interested in this too. I'd definitely like to see something like this in emscripten myself, sounds very useful!

kripken avatar Sep 23 '19 20:09 kripken

Finally had some time and made progress.

Here is a tool that you can use to generate declarations for emscripten modules (or browser libs if you wish) https://github.com/giniedp/webidl2ts

The https://github.com/giniedp/ammojs-typed project is upgraded so it uses that tool. Basically all work that ammojs-typed does can be migrated to this project with ease. It boils down to a simple command that can be run after emscripten modules have been generated (see the generate command here). There is just a little idl adjustment needed before doing so (see Readme)

giniedp avatar Jul 12 '20 21:07 giniedp

ammojs-typed

bihongbin3027 avatar Jan 22 '22 03:01 bihongbin3027