Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

fix(primitives): Better primitive support #13

Merged
merged 1 commit into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 65 additions & 24 deletions packages/clangffi/src/lib/tsgen/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import debug from "debug";
import { CXTypeKind, Type } from "libclang-bindings";
import { Type } from "libclang-bindings";
import { StringBuilder } from "../string-builder";
import { ITypeNameResolver, LineEndings } from "../types";
import { resolveName, simpleDesugar } from "../util";
import { simpleDesugar } from "../util";

/**
* log for resolving ref types
Expand All @@ -13,32 +13,52 @@ const resolveLog = debug("clangffi:tsgen:resolve");
* Resolver that matches native types with their typescript types
*/
export class TSResolver implements ITypeNameResolver {
// TODO(bengreenier): ensure this list is exhaustive
lookup = {
void: "void",
uint8_t: "number",
int: "number",
"unsigned int": "number",
int8_t: "number",
uint16_t: "number",
uint8_t: "number",
int16_t: "number",
uint32_t: "number",
uint16_t: "number",
int32_t: "number",
uint32_t: "number",
int64_t: "number",
uint64_t: "number",
float: "number",
double: "number",
// "long double": "unclear spec",
// "unclear spec": "ref.types.Object",
"char*": "string",
bool: "boolean",
byte: "number",
char: "number",
"char*": "string",
"signed char": "number",
uchar: "number",
"unsigned char": "number",
short: "number",
long: "number",
time_t: "number",
size_t: "number",
"long long": "number",
"short int": "number",
"signed short": "number",
"signed short int": "number",
ushort: "number",
"unsigned short": "number",
"unsigned short int": "number",
int: "number",
signed: "number",
"signed int": "number",
unsigned: "number",
"unsigned int": "number",
long: "number",
"long int": "number",
"signed long": "number",
"signed long int": "number",
"unsigned long": "number",
"unsigned long int": "number",
"long long": "number",
"long long int": "number",
"signed long long": "number",
"signed long long int": "number",
"unsigned long long": "number",
"unsigned long long int": "number",
size_t: "number",
};

createPointer(str: string): string {
Expand Down Expand Up @@ -86,29 +106,50 @@ export class RefResolver implements ITypeNameResolver {
// TODO(bengreenier): ensure this list is exhaustive
lookup = {
void: "ref.types.void",
uint8_t: "ref.types.uint8",
int: "ref.types.int",
"unsigned int": "ref.types.uint",
int8_t: "ref.types.int8",
uint16_t: "ref.types.uint16",
uint8_t: "ref.types.uint8",
int16_t: "ref.types.int16",
uint32_t: "ref.types.uint32",
uint16_t: "ref.types.uint16",
int32_t: "ref.types.int32",
uint64_t: "ref.types.int64",
uint32_t: "ref.types.uint32",
int64_t: "ref.types.int64",
uint64_t: "ref.types.uint64",
float: "ref.types.float",
double: "ref.types.double",
// "long double": "unclear spec",
// "unclear spec": "ref.types.Object",
"char*": "ref.types.CString",
bool: "ref.types.bool",
byte: "ref.types.byte",
char: "ref.types.char",
"char*": "ref.types.CString",
"signed char": "ref.types.char",
uchar: "ref.types.uchar",
"unsigned char": "ref.types.uchar",
short: "ref.types.short",
long: "ref.types.long",
time_t: "ref.types.longlong",
size_t: "ref.types.ulonglong",
"long long": "ref.types.longlong",
"short int": "ref.types.short",
"signed short": "ref.types.short",
"signed short int": "ref.types.short",
ushort: "ref.types.ushort",
"unsigned short": "ref.types.ushort",
"unsigned short int": "ref.types.ushort",
int: "ref.types.int",
signed: "ref.types.int",
"signed int": "ref.types.int",
unsigned: "ref.types.uint",
"unsigned int": "ref.types.uint",
long: "ref.types.long",
"long int": "ref.types.long",
"signed long": "ref.types.long",
"signed long int": "ref.types.long",
"unsigned long": "ref.types.ulong",
"unsigned long int": "ref.types.ulong",
"long long": "ref.types.longlong",
"long long int": "ref.types.longlong",
"signed long long": "ref.types.longlong",
"signed long long int": "ref.types.longlong",
"unsigned long long": "ref.types.ulonglong",
"unsigned long long int": "ref.types.ulonglong",
size_t: "ref.types.size_t",
};

createPointer(str: string): string {
Expand Down
54 changes: 53 additions & 1 deletion packages/clangffi/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,64 @@ export enum LineEndings {
CRLF = "crlf",
}

/**
* Represents a lookup table in an `ITypeResolver'
*/
export interface INativeLookupTable {
[key: string]: string;
void: string;
int8_t: string;
uint8_t: string;
int16_t: string;
uint16_t: string;
int32_t: string;
uint32_t: string;
int64_t: string;
uint64_t: string;
float: string;
double: string;
// "long double": string
// "unclear spec": string,
"char*": string;
bool: string;
byte: string;
char: string;
"signed char": string;
uchar: string;
"unsigned char": string;
short: string;
"short int": string;
"signed short": string;
"signed short int": string;
ushort: string;
"unsigned short": string;
"unsigned short int": string;
int: string;
signed: string;
"signed int": string;
unsigned: string;
"unsigned int": string;
long: string;
"long int": string;
"signed long": string;
"signed long int": string;
"unsigned long": string;
"unsigned long int": string;
"long long": string;
"long long int": string;
"signed long long": string;
"signed long long int": string;
"unsigned long long": string;
"unsigned long long int": string;
size_t: string;
}

/**
* A lightweight type resolver for use during generation
* This allows us to swap between raw TS types and ref types while sharing the type resolution logic
*/
export interface ITypeNameResolver {
lookup: { [key: string]: string };
lookup: INativeLookupTable;
createPointer(str: string): string;
createArray(str: string): string;
createEnum(str: string): string;
Expand Down