-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
383 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
packages/nitro-codegen/src/syntax/types/ArrayBufferType.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { Language } from '../../getPlatformSpecs.js' | ||
import type { SourceFile } from '../SourceFile.js' | ||
import type { Type } from './Type.js' | ||
|
||
export class ArrayBufferType implements Type { | ||
get canBePassedByReference(): boolean { | ||
return false | ||
} | ||
|
||
getCode(language: Language): string { | ||
switch (language) { | ||
case 'c++': | ||
return 'std::shared_ptr<ArrayBuffer>' | ||
default: | ||
throw new Error( | ||
`Language ${language} is not yet supported for ArrayBufferType!` | ||
) | ||
} | ||
} | ||
getExtraFiles(): SourceFile[] { | ||
return [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Language } from '../../getPlatformSpecs.js' | ||
import type { SourceFile } from '../SourceFile.js' | ||
import type { Type } from './Type.js' | ||
|
||
export class ArrayType implements Type { | ||
readonly itemType: Type | ||
|
||
constructor(itemType: Type) { | ||
this.itemType = itemType | ||
} | ||
|
||
get canBePassedByReference(): boolean { | ||
return true | ||
} | ||
|
||
getCode(language: Language): string { | ||
const itemCode = this.itemType.getCode(language) | ||
|
||
switch (language) { | ||
case 'c++': | ||
return `std::vector<${itemCode}>` | ||
default: | ||
throw new Error( | ||
`Language ${language} is not yet supported for ArrayType!` | ||
) | ||
} | ||
} | ||
getExtraFiles(): SourceFile[] { | ||
return this.itemType.getExtraFiles() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { Language } from '../../getPlatformSpecs.js' | ||
import type { SourceFile } from '../SourceFile.js' | ||
import type { Type } from './Type.js' | ||
|
||
export class BigIntType implements Type { | ||
get canBePassedByReference(): boolean { | ||
return false | ||
} | ||
|
||
getCode(language: Language): string { | ||
switch (language) { | ||
case 'c++': | ||
return 'int64_t' | ||
default: | ||
throw new Error( | ||
`Language ${language} is not yet supported for BigIntType!` | ||
) | ||
} | ||
} | ||
getExtraFiles(): SourceFile[] { | ||
return [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { Language } from '../../getPlatformSpecs.js' | ||
import type { SourceFile } from '../SourceFile.js' | ||
import type { Type } from './Type.js' | ||
|
||
export class BooleanType implements Type { | ||
get canBePassedByReference(): boolean { | ||
return false | ||
} | ||
|
||
getCode(language: Language): string { | ||
switch (language) { | ||
case 'c++': | ||
return 'bool' | ||
default: | ||
throw new Error( | ||
`Language ${language} is not yet supported for BooleanType!` | ||
) | ||
} | ||
} | ||
getExtraFiles(): SourceFile[] { | ||
return [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Language } from '../../getPlatformSpecs.js' | ||
import type { SourceFile } from '../SourceFile.js' | ||
import type { Type } from './Type.js' | ||
|
||
export class EnumType implements Type { | ||
readonly enumName: string | ||
readonly declarationFile: SourceFile | ||
|
||
constructor(enumName: string, declarationFile: SourceFile) { | ||
this.enumName = enumName | ||
this.declarationFile = declarationFile | ||
} | ||
|
||
get canBePassedByReference(): boolean { | ||
return false | ||
} | ||
|
||
getCode(language: Language): string { | ||
switch (language) { | ||
case 'c++': | ||
return this.enumName | ||
default: | ||
throw new Error( | ||
`Language ${language} is not yet supported for NumberType!` | ||
) | ||
} | ||
} | ||
getExtraFiles(): SourceFile[] { | ||
return [this.declarationFile] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { Language } from '../../getPlatformSpecs.js' | ||
import type { SourceFile } from '../SourceFile.js' | ||
import type { Type } from './Type.js' | ||
|
||
export class FunctionType implements Type { | ||
readonly returnType: Type | ||
readonly parameters: Type[] | ||
|
||
constructor(returnType: Type, parameters: Type[]) { | ||
this.returnType = returnType | ||
this.parameters = parameters | ||
} | ||
|
||
get canBePassedByReference(): boolean { | ||
return true | ||
} | ||
|
||
getCode(language: Language): string { | ||
const returnCode = this.returnType.getCode(language) | ||
const parametersCode = this.parameters.map((p) => p.getCode(language)) | ||
|
||
switch (language) { | ||
case 'c++': | ||
return `std::function<${returnCode}(${parametersCode.join(', ')})>` | ||
default: | ||
throw new Error( | ||
`Language ${language} is not yet supported for FunctionType!` | ||
) | ||
} | ||
} | ||
getExtraFiles(): SourceFile[] { | ||
return [ | ||
...this.returnType.getExtraFiles(), | ||
...this.parameters.flatMap((p) => p.getExtraFiles()), | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { Language } from '../../getPlatformSpecs.js' | ||
import type { SourceFile } from '../SourceFile.js' | ||
import type { Type } from './Type.js' | ||
|
||
export class NullType implements Type { | ||
get canBePassedByReference(): boolean { | ||
return false | ||
} | ||
|
||
getCode(language: Language): string { | ||
switch (language) { | ||
case 'c++': | ||
return 'std::nullptr_t' | ||
default: | ||
throw new Error( | ||
`Language ${language} is not yet supported for NullType!` | ||
) | ||
} | ||
} | ||
getExtraFiles(): SourceFile[] { | ||
return [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { Language } from '../../getPlatformSpecs.js' | ||
import type { SourceFile } from '../SourceFile.js' | ||
import type { Type } from './Type.js' | ||
|
||
export class NumberType implements Type { | ||
get canBePassedByReference(): boolean { | ||
return false | ||
} | ||
|
||
getCode(language: Language): string { | ||
switch (language) { | ||
case 'c++': | ||
return 'double' | ||
default: | ||
throw new Error( | ||
`Language ${language} is not yet supported for NumberType!` | ||
) | ||
} | ||
} | ||
getExtraFiles(): SourceFile[] { | ||
return [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { Language } from '../../getPlatformSpecs.js' | ||
import type { SourceFile } from '../SourceFile.js' | ||
import type { Type } from './Type.js' | ||
|
||
export class PromiseType implements Type { | ||
readonly resultingType: Type | ||
|
||
constructor(resultingType: Type) { | ||
this.resultingType = resultingType | ||
} | ||
|
||
get canBePassedByReference(): boolean { | ||
return false | ||
} | ||
|
||
getCode(language: Language): string { | ||
const resultingCode = this.resultingType.getCode(language) | ||
switch (language) { | ||
case 'c++': | ||
return `std::future<${resultingCode}>` | ||
default: | ||
throw new Error( | ||
`Language ${language} is not yet supported for PromiseType!` | ||
) | ||
} | ||
} | ||
getExtraFiles(): SourceFile[] { | ||
return this.resultingType.getExtraFiles() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { Language } from '../../getPlatformSpecs.js' | ||
import type { SourceFile } from '../SourceFile.js' | ||
import type { Type } from './Type.js' | ||
|
||
export class RecordType implements Type { | ||
readonly keyType: Type | ||
readonly valueType: Type | ||
|
||
constructor(keyType: Type, valueType: Type) { | ||
this.keyType = keyType | ||
this.valueType = valueType | ||
} | ||
|
||
get canBePassedByReference(): boolean { | ||
return true | ||
} | ||
|
||
getCode(language: Language): string { | ||
const keyCode = this.keyType.getCode(language) | ||
const valueCode = this.valueType.getCode(language) | ||
|
||
switch (language) { | ||
case 'c++': | ||
return `std::unordered_map<${keyCode}, ${valueCode}>` | ||
default: | ||
throw new Error( | ||
`Language ${language} is not yet supported for RecordType!` | ||
) | ||
} | ||
} | ||
getExtraFiles(): SourceFile[] { | ||
return [...this.keyType.getExtraFiles(), ...this.valueType.getExtraFiles()] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { Language } from '../../getPlatformSpecs.js' | ||
import type { SourceFile } from '../SourceFile.js' | ||
import type { Type } from './Type.js' | ||
|
||
export class StringType implements Type { | ||
get canBePassedByReference(): boolean { | ||
return true | ||
} | ||
|
||
getCode(language: Language): string { | ||
switch (language) { | ||
case 'c++': | ||
return 'std::string' | ||
default: | ||
throw new Error( | ||
`Language ${language} is not yet supported for StringType!` | ||
) | ||
} | ||
} | ||
getExtraFiles(): SourceFile[] { | ||
return [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Language } from '../../getPlatformSpecs.js' | ||
import type { SourceFile } from '../SourceFile.js' | ||
import type { Type } from './Type.js' | ||
|
||
export class StructType implements Type { | ||
readonly structName: string | ||
readonly declarationFile: SourceFile | ||
|
||
constructor(structName: string, declarationFile: SourceFile) { | ||
this.structName = structName | ||
this.declarationFile = declarationFile | ||
} | ||
|
||
get canBePassedByReference(): boolean { | ||
return true | ||
} | ||
|
||
getCode(language: Language): string { | ||
switch (language) { | ||
case 'c++': | ||
return this.structName | ||
default: | ||
throw new Error( | ||
`Language ${language} is not yet supported for StructType!` | ||
) | ||
} | ||
} | ||
getExtraFiles(): SourceFile[] { | ||
return [this.declarationFile] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { Language } from '../../getPlatformSpecs.js' | ||
import type { SourceFile } from '../SourceFile.js' | ||
|
||
/** | ||
* Represents a TypeScript Type that can be represented in a native language (C++, Swift, Kotlin) | ||
*/ | ||
export interface Type { | ||
/** | ||
* Get whether this type can be passed by reference in C++ (`const T&` vs `T`) | ||
*/ | ||
readonly canBePassedByReference: boolean | ||
/** | ||
* Get the native code required to represent this type for the given language (C++, Swift, Kotlin). | ||
* | ||
* E.g. for a `number` type, this would return `'double'` in C++. | ||
*/ | ||
getCode(language: Language): string | ||
/** | ||
* Get all required extra files that need to be available/imported for this type to properly work. | ||
* | ||
* E.g. for `Promise<T>`, `T` needs to be imported so it will be returned in `getExtraFiles()`. | ||
*/ | ||
getExtraFiles(): SourceFile[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { Language } from '../../getPlatformSpecs.js' | ||
import type { SourceFile } from '../SourceFile.js' | ||
import type { Type } from './Type.js' | ||
|
||
export class VoidType implements Type { | ||
get canBePassedByReference(): boolean { | ||
return false | ||
} | ||
|
||
getCode(language: Language): string { | ||
switch (language) { | ||
case 'c++': | ||
return 'void' | ||
case 'swift': | ||
return 'Void' | ||
case 'kotlin': | ||
return 'Unit' | ||
default: | ||
throw new Error( | ||
`Language ${language} is not yet supported for VoidType!` | ||
) | ||
} | ||
} | ||
getExtraFiles(): SourceFile[] { | ||
return [] | ||
} | ||
} |