Skip to content
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
9 changes: 1 addition & 8 deletions packages/nitrogen/src/createPlatformSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,7 @@ export function getHybridObjectSpec(
if (Node.isPropertyDeclaration(member)) {
const memberType = member.getType()
const isOptional = memberSymbol.isOptional() || memberType.getUnionTypes().some((unionType) => unionType.isUndefined())
// Android Fabric props still use public C++ storage.
let propertyTarget: TypeParsingTarget
if (view && language === 'kotlin' && !member.isReadonly()) {
propertyTarget = { ...target, language: 'c++' }
} else {
propertyTarget = target
}
const propType = createType(propertyTarget, memberType, isOptional, member.getTypeNode())
const propType = createType(target, memberType, isOptional, member.getTypeNode())
properties.push(new Property(memberName, propType, member.isReadonly()))
} else if (Node.isMethodDeclaration(member)) {
methods.push(new InstanceMethod(member, target))
Expand Down
66 changes: 66 additions & 0 deletions packages/nitrogen/src/syntax/kotlin/types/KotlinStoredType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type { NitroConfig } from '../../../config/NitroConfig.js'
import type { Language } from '../../../getPlatformSpecs.js'
import type { SourceFile, SourceImport } from '../../SourceFile.js'
import type { GetCodeOptions, Type, TypeKind } from '../../types/Type.js'
import { wrapKotlinType } from '../wrapKotlinType.js'
import { convertJniValue, type KotlinType } from './KotlinType.js'

/** A direct JNI value retained across native threads and Fabric props snapshots. */
export class KotlinStoredType implements Type {
readonly type: KotlinType

constructor(type: Type, config: NitroConfig) {
this.type = wrapKotlinType(type, { config: config })
}

get kind(): TypeKind {
return this.type.kind
}

get isEquatable(): boolean {
return this.type.isEquatable
}

getCode(language: Language, options: GetCodeOptions): string {
switch (language) {
case 'c++': {
if (this.type.jniValueKind === 'primitive') {
return this.type.getCode(language, options)
}
const jniClass = this.type.getCode(language, { ...options, context: 'type-argument' })
const storedType = `jni::global_ref<${jniClass}>`
if (options.context === 'parameter') return `const ${storedType}&`
return storedType
}
default:
return this.type.getCode(language, options)
}
}

/** Borrow an object, or adapt a scalar whose JavaScript ABI differs from JNI. */
getJniValue(value: string): string {
if (this.type.jniValueKind === 'object') return `${value}.get()`
return convertJniValue(this.type, value, 'to-jni')
}

getExtraFiles(): SourceFile[] {
return this.type.getExtraFiles()
}

getRequiredImports(language: Language): SourceImport[] {
const imports = this.type.getRequiredImports(language)
switch (language) {
case 'c++':
if (this.type.jniValueKind === 'object') {
imports.push({
language: language,
declaration: { header: 'NitroModules/JSIConverter+JNIGlobalRef.hpp', space: 'system' },
})
}
break
default:
break
}
return imports
}
}
41 changes: 21 additions & 20 deletions packages/nitrogen/src/views/kotlin/KotlinHybridViewManager.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { wrapKotlinType } from '../../syntax/kotlin/wrapKotlinType.js'
import { getJniCode } from '../../syntax/kotlin/types/KotlinType.js'
import type { SourceFile } from '../../syntax/SourceFile.js'
import type { HybridObjectSpec } from '../../syntax/HybridObjectSpec.js'
import {
createViewComponentShadowNodeFiles,
getViewComponentNames,
getHybridViewComponentName,
getHybridViewProperties,
type HybridViewPropertyStorage,
} from '../CppHybridViewComponent.js'
import { createFileMetadataString, escapeCppName } from '../../syntax/helpers.js'
import { getHybridObjectName } from '../../syntax/getHybridObjectName.js'
import { indent } from '../../utils.js'
import { getImportsCode, getImportCode } from '../../syntax/getImportCode.js'
import { getKotlinImplementation } from '../../syntax/kotlin/KotlinMethodHelpers.js'
import { createKotlinConverters, getKotlinConverter } from '../../syntax/kotlin/KotlinConverter.js'
import { KotlinInstanceMethod } from '../../syntax/kotlin/KotlinInstanceMethod.js'
import { KotlinStoredType } from '../../syntax/kotlin/types/KotlinStoredType.js'
import { KotlinHybridViewType } from './KotlinHybridViewType.js'

export function createKotlinHybridViewManager(spec: HybridObjectSpec<'kotlin'>): SourceFile[] {
Expand All @@ -25,22 +27,19 @@ export function createKotlinHybridViewManager(spec: HybridObjectSpec<'kotlin'>):
const implementation = getKotlinImplementation(spec)
const viewImplementation = implementation.className
const implementationImport = getImportCode(implementation.kotlinImport)
const viewProperties = getHybridViewProperties(spec)
const cppFiles = createViewComponentShadowNodeFiles(spec, {
platform: 'android',
properties: viewProperties.map(({ property, config }) => ({ property: property, config: config, type: property.type })),
})
const propertyFiles = viewProperties.flatMap(({ property }) => property.type.getExtraFiles())
const propertyConverters = viewProperties.map(({ property, config }) => createKotlinConverters(property.type, config))
const converterFiles = propertyConverters.flatMap((converter) => converter.files)
const converterImports = propertyConverters.flatMap((converter) => converter.imports)
const viewProperties = getHybridViewProperties(spec).map(({ property, config }) => ({
property: property,
config: config,
type: new KotlinStoredType(property.type, config),
})) satisfies readonly HybridViewPropertyStorage[]
const cppFiles = createViewComponentShadowNodeFiles(spec, { platform: 'android', properties: viewProperties })
const propertyFiles = viewProperties.flatMap(({ type }) => type.getExtraFiles())
const componentName = getHybridViewComponentName(spec)
const componentHeader = spec.config.getCxxHeader(`views/${component}.hpp`, 'private')
const updaterHeader = spec.config.getCxxHeader(`views/J${stateUpdaterName}.hpp`, 'private')
const updaterIncludes = getImportsCode([
{ language: 'c++', declaration: updaterHeader },
{ language: 'c++', declaration: componentHeader },
...converterImports,
])
const declarationIncludes = getImportsCode([
{
Expand Down Expand Up @@ -222,18 +221,21 @@ public:
} // namespace ${cxxNamespace}
`.trim()

const propsUpdaterCalls = viewProperties.map(({ property, config }) => {
const propsUpdaterCalls = viewProperties.map(({ property, type }) => {
const name = escapeCppName(property.name)
const setter = property.getSetterName('other')
const converter = getKotlinConverter({
cppType: property.type,
kotlinType: wrapKotlinType(property.type, { config: config }),
})
const setter = property.cppSetter
if (setter == null) {
throw new Error(`HybridView property ${spec.name}.${property.name} requires a setter.`)
}
const kotlinSetter = new KotlinInstanceMethod({ method: setter, accessor: { kind: 'setter', property: property } })
const jniParameterType = getJniCode(type.type, { context: 'parameter', fullyQualified: true })
const jniValue = type.getJniValue(`newProps->${name}.get()`)
return `
if (oldProps == nullptr
? newProps->${name}.isProvided()
: !newProps->${name}.hasSameValue(oldProps->${name})) {
hybridView->${setter}(${converter}::toJNI(newProps->${name}.get()));
static const auto __setter = ${JHybridTSpec}::JavaPart::javaClassStatic()->getMethod<void(${jniParameterType})>("${kotlinSetter.name}");
__setter(javaView, ${jniValue});
}
`.trim()
})
Expand Down Expand Up @@ -318,7 +320,6 @@ void J${stateUpdaterName}::updateViewProps(jni::alias_ref<jni::JClass> /* class
return [
...cppFiles,
...propertyFiles,
...converterFiles,
{
content: viewManagerCode,
language: 'kotlin',
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading