Skip to content

Commit

Permalink
feat: Implement Method, Parameter and Property for Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jul 26, 2024
1 parent 093627e commit ff0c102
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
18 changes: 18 additions & 0 deletions packages/nitro-codegen/src/syntax/Method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ ${signature} {
}`.trim()
}
}
case 'kotlin': {
const params = this.parameters.map((p) => p.getCode('kotlin'))
const returnType = this.returnType.getCode('kotlin')
let signature = `fun ${this.name}(${params.join(', ')}): ${returnType}`

if (modifiers?.inline) signature = `inline ${signature}`
if (modifiers?.override) signature = `override ${signature}`

if (body == null) {
return signature
} else {
return `
${signature} {
${indent(body, ' ')}
}
`.trim()
}
}
default:
throw new Error(
`Language ${language} is not yet supported for property getters!`
Expand Down
8 changes: 5 additions & 3 deletions packages/nitro-codegen/src/syntax/Parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,19 @@ export class Parameter implements CodeNode {
}

getCode(language: Language): string {
const cppName = escapeCppName(this.name)
const name = escapeCppName(this.name)
switch (language) {
case 'c++':
let cppType = this.type.getCode('c++')
if (this.type.canBePassedByReference) {
// T -> const T&
cppType = toReferenceType(cppType)
}
return `${cppType} ${cppName}`
return `${cppType} ${name}`
case 'swift':
return `${cppName}: ${this.type.getCode('swift')}`
return `${name}: ${this.type.getCode('swift')}`
case 'kotlin':
return `${name}: ${this.type.getCode('kotlin')}`
default:
throw new Error(
`Language ${language} is not yet supported for parameters!`
Expand Down
21 changes: 21 additions & 0 deletions packages/nitro-codegen/src/syntax/Property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ set {
}
return `var ${this.name}: ${type} { ${accessors} }`
}
case 'kotlin': {
const type = this.type.getCode('swift')
const keyword = this.isReadonly ? 'val' : 'var'
const lines: string[] = []
lines.push(`${keyword} ${this.name}: ${type}`)
if (body != null) {
lines.push(`
get() {
${body.getter}
}
`)
if (!this.isReadonly) {
lines.push(`
set(value) {
${body.setter}
}
`)
}
}
return lines.join('\n')
}
default:
throw new Error(
`Language ${language} is not yet supported for properties!`
Expand Down
3 changes: 2 additions & 1 deletion packages/react-native-nitro-image/src/specs/Image.nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ interface ImageSize {
readonly height: number
}

export interface Image extends HybridObject<{ ios: 'swift' }> {
export interface Image
extends HybridObject<{ ios: 'swift'; android: 'kotlin' }> {
readonly size: ImageSize
readonly pixelFormat: PixelFormat

Expand Down

0 comments on commit ff0c102

Please sign in to comment.