Skip to content

Commit

Permalink
Render native field of native JsType as lateinit.
Browse files Browse the repository at this point in the history
Field on native JsType are left uninitialized as they are defined in Javascript.
Kotlin enforces field initialization during their declaration. To workaround this issue, we will mark uninitialized native field as `lateinit`. J2CL does not produce any code for native `JsType` and this has no effect on the final generated code.

A better option would be to use a dummy function named `definesExternally()` that can be used as default initializer.

PiperOrigin-RevId: 571162134
  • Loading branch information
jDramaix authored and copybara-github committed Oct 5, 2023
1 parent 23017dd commit 224eb27
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion transpiler/java/com/google/j2cl/transpiler/ast/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ public boolean isKtLateInit() {
FieldDescriptor descriptor = getDescriptor();
boolean isTestProperty =
descriptor.getEnclosingTypeDescriptor().getTypeDeclaration().isAnnotatedWithJUnitRunWith();
return (descriptor.getKtInfo().isUninitializedWarningSuppressed() || isTestProperty)
return (descriptor.getKtInfo().isUninitializedWarningSuppressed()
|| isTestProperty
// TODO(303529872): initialize native field with a dummy function known by the J2CL
// transpiler.
|| descriptor.isNative())
&& !descriptor.isFinal()
&& !descriptor.getTypeDescriptor().isNullable()
&& !hasInitializer();
Expand Down

0 comments on commit 224eb27

Please sign in to comment.