Skip to content

Commit

Permalink
fix: c type const should be readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpLink committed Dec 12, 2023
1 parent c10a789 commit baa5628
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
37 changes: 26 additions & 11 deletions packages/lib/src/gir-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -834,20 +834,24 @@ export class GirModule {
const type: GirType | undefined = girVar.type?.[0]
const cType = type?.$?.['c:type']

// Ignore depreciated `allow-none` if one of the new implementation `optional` or `nullable` is set
if (a.optional || a.nullable) {
return girBool(a.nullable)
}

// UTF-8 string pointers can be null, e.g. `gchar*`, see https://github.com/gjsify/ts-for-gir/issues/108
if (type?.$?.name === 'utf8' && cType?.endsWith('*')) {
if (type?.$?.name === 'utf8' && !cType?.startsWith('const ') && cType?.endsWith('*')) {
if (girVar._fullSymName?.endsWith('DEBUG_CONTROLLER_EXTENSION_POINT_NAME'))
console.debug('typeIsNullable (UTF-8 string)', type?.$?.name, cType, girVar.type)
return true
}

// If the default value is NULL, handle this as nullable
if (a['default-value'] === 'NULL') return true

// Ignore depreciated `allow-none` if one of the new implementation `optional` or `nullable` is set
if (a.optional || a.nullable) {
return girBool(a.nullable)
} else {
return girBool(a.nullable) || girBool(a['allow-none']) || girBool(a['null-ok'])
if (a['default-value'] === 'NULL') {
return true
}

return girBool(a.nullable) || girBool(a['allow-none']) || girBool(a['null-ok'])
}

/**
Expand All @@ -874,6 +878,19 @@ export class GirModule {
}
}

/**
* Checks if the property is readonly.
* @param girCallback
*/
private typeIsReadonly(girProp: GirPropertyElement | GirFieldElement): boolean {
const cType = girProp.type?.[0].$?.['c:type']
return (
!girBool(girProp.$.writable) ||
girBool((girProp as GirPropertyElement).$['construct-only']) ||
(cType ? cType.startsWith('const ') : false) // c type is const and therefore readonly, isn't?
)
}

private setParameterTsData(
girParam: GirCallableParamElement,
girParams: GirCallableParamElement[],
Expand Down Expand Up @@ -1138,9 +1155,7 @@ export class GirModule {

if (optional === undefined) optional = this.typeIsOptional(girProp)
if (nullable === undefined) nullable = this.typeIsNullable(girProp)

const readonly =
!girBool(girProp.$.writable) || (!construct && girBool((girProp as GirPropertyElement).$['construct-only']))
const readonly = !construct && this.typeIsReadonly(girProp)

let tsData: TsProperty | undefined

Expand Down
4 changes: 2 additions & 2 deletions packages/lib/src/transformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export const PRIMITIVE_TYPE_MAP = {
'gchar*': 'string', // TODO CHECKME
'gchar**': 'string', // TODO CHECKME
'gchar***': 'string', // TODO CHECKME
'const gchar*': 'string', // TODO CHECKME
'const char*': 'string', // TODO CHECKME
'const gchar*': 'string', // TODO is read-only
'const char*': 'string', // TODO is read-only

// Booleans
gboolean: 'boolean',
Expand Down

0 comments on commit baa5628

Please sign in to comment.