Skip to content

Deriving typed array ids from RTTIΒ #20

@dcodeIO

Description

@dcodeIO

This is a little idea to get rid of the requirement to specify an additional entry file.

Instead of exporting the IDs, it is also possible to obtain these from RTTI, which is located in static memory at exports.__rtti_base. RTTI is somewhat limited, but as-bind's currently supported types fit it well.

// ╒═══════════════════ Typeinfo interpretation ═══════════════════╕
//    3                   2                   1
//  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0  bits
// β”œβ”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”€ ◄─ __rtti_base
// β”‚                             count                             β”‚
// β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•‘ ┐
// β”‚                      Typeinfo#flags [id=0]                    β”‚ id < count
// β”œ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
// β”‚                      Typeinfo#base  [id=0]                    β”‚
// β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
// β”‚                              ...                              β”‚

All the ArrayBufferViews have a special flag TypeinfoFlags.ARRAYBUFFERVIEW. Their integer or floating point type can be derived from TypeinfoFlags.VALUE_ALIGN_X in combination with TypeinfoFlags.VALUE_SIGNED and TypeinfoFlags.VALUE_FLOAT.

However, doing so imposes the limitation that if no Int16Array for example is used within the module, it won't be present in RTTI. Instantiating one should not be useful anyway in this case as there is nothing accepting an Int16Array on the Wasm side.

This would also work with Array<T>. These use TypeinfoFlags.ARRAYBUFFERVIEW | TypeinfoFlags.ARRAY and have the same flags otherwise. However, special care must be taken if T is managed, which is indicated by TypeinfoFlags.VALUE_MANAGED.

The other supported types have fixed IDs btw and are always present, so one doesn't need an export for these:

  • ArrayBuffer: 0
  • String: 1
  • ArrayBufferView: 2 (this can be used to find typed arrays, which use this ID as a base)

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions