Skip to content

Commit

Permalink
[JS] Workaround JsCompiler handling of interface constructor in a mor…
Browse files Browse the repository at this point in the history
…e general way.

PiperOrigin-RevId: 704775914
  • Loading branch information
gkdn authored and copybara-github committed Dec 10, 2024
1 parent cde746e commit 558b06b
Show file tree
Hide file tree
Showing 197 changed files with 280 additions and 242 deletions.
4 changes: 1 addition & 3 deletions jre/java/java/lang/Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public final class Class<T> implements Type, Serializable {
*/
@JsMethod
@HasNoSideEffects
private static Class<?> $get(Object obj, @JsOptional Double opt_dimensionCount) {
// TODO(b/79389970): Change the argument to Constructor.
Constructor ctor = JsUtils.uncheckedCast(obj);
private static Class<?> $get(Constructor ctor, @JsOptional Double opt_dimensionCount) {
int dimensionCount = JsUtils.coerceToInt(opt_dimensionCount);
return ctor.cache("$$class/" + dimensionCount, () -> new Class(ctor, dimensionCount));
}
Expand Down
7 changes: 3 additions & 4 deletions jre/java/javaemul/internal/Casts.native.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/**
* // TODO(b/79389970): change castType to Function.
* @param {*} instance
* @param {Object} castType
* @param {!Constructor} castType
* @return {*}
*/
$Casts.$to = function(instance, castType) {
return $Casts.$toInternal(
instance, /** @type {function(*):boolean} */ (castType.$isInstance),
/** @type {Constructor} */ (castType));
instance, /** @type {function(*):boolean} */
(/** @type {?} */ (castType).$isInstance), castType);
};
3 changes: 1 addition & 2 deletions jre/java/javaemul/internal/nativebootstrap/Util.impl.java.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ class Util {
}

/**
* // TODO(b/79389970): change ctor to Function
* @param {Object} ctor
* @param {!Constructor} ctor
* @param {string} name
* @public
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
java.lang.RuntimeException: __the_message__!
at com.google.j2cl.junit.integration.stacktrace.data.KotlinThrowsInJsFunction$MyFunctionImpl.run(KotlinThrowsInJsFunction.kt:50)
at lambda(jre/java/jre.js/javaemul/internal/nativebootstrap/Util.impl.java.js:208:31)
at lambda(jre/java/jre.js/javaemul/internal/nativebootstrap/Util.impl.java.js:207:31)
at com.google.j2cl.junit.integration.stacktrace.data.KotlinThrowsInJsFunction.executesFunction(KotlinThrowsInJsFunction.kt:55)
at com.google.j2cl.junit.integration.stacktrace.data.KotlinThrowsInJsFunction$1.run(KotlinThrowsInJsFunction.kt:41)
at com.google.j2cl.junit.integration.stacktrace.data.KotlinThrowsInJsFunction.executesFunction(KotlinThrowsInJsFunction.kt:55)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
java.lang.RuntimeException: __the_message__!
at com.google.j2cl.junit.integration.stacktrace.data.KotlinThrowsInJsFunction$MyFunctionImpl.run(KotlinThrowsInJsFunction.kt:50)
at apply(<blaze-out>/jre/java/jre.js/javaemul/internal/nativebootstrap/Util.impl.java.js:208:31)
at apply(<blaze-out>/jre/java/jre.js/javaemul/internal/nativebootstrap/Util.impl.java.js:207:31)
at com.google.j2cl.junit.integration.stacktrace.data.KotlinThrowsInJsFunction.executesFunction(KotlinThrowsInJsFunction.kt:55)
at com.google.j2cl.junit.integration.stacktrace.data.KotlinThrowsInJsFunction$1.run(KotlinThrowsInJsFunction.kt:41)
at com.google.j2cl.junit.integration.stacktrace.data.KotlinThrowsInJsFunction.executesFunction(KotlinThrowsInJsFunction.kt:55)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
java.lang.RuntimeException: __the_message__!
at com.google.j2cl.junit.integration.stacktrace.data.ThrowsInJsFunction$MyFunctionImpl.run(ThrowsInJsFunction.java:55)
at lambda(jre/java/jre.js/javaemul/internal/nativebootstrap/Util.impl.java.js:208:31)
at lambda(jre/java/jre.js/javaemul/internal/nativebootstrap/Util.impl.java.js:207:31)
at com.google.j2cl.junit.integration.stacktrace.data.ThrowsInJsFunction.executesFunction(ThrowsInJsFunction.java:60)
at com.google.j2cl.junit.integration.stacktrace.data.ThrowsInJsFunction$1.run(ThrowsInJsFunction.java:46)
at com.google.j2cl.junit.integration.stacktrace.data.ThrowsInJsFunction.executesFunction(ThrowsInJsFunction.java:60)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
java.lang.RuntimeException: __the_message__!
at com.google.j2cl.junit.integration.stacktrace.data.ThrowsInJsFunction$MyFunctionImpl.run(ThrowsInJsFunction.java:55)
at apply(<blaze-out>/jre/java/jre.js/javaemul/internal/nativebootstrap/Util.impl.java.js:208:31)
at apply(<blaze-out>/jre/java/jre.js/javaemul/internal/nativebootstrap/Util.impl.java.js:207:31)
at com.google.j2cl.junit.integration.stacktrace.data.ThrowsInJsFunction.executesFunction(ThrowsInJsFunction.java:60)
at com.google.j2cl.junit.integration.stacktrace.data.ThrowsInJsFunction$1.run(ThrowsInJsFunction.java:46)
at com.google.j2cl.junit.integration.stacktrace.data.ThrowsInJsFunction.executesFunction(ThrowsInJsFunction.java:60)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.j2cl.transpiler.passes.AddDisambiguatingSuperMethodForwardingStubs;
import com.google.j2cl.transpiler.passes.AddEntryPointBridgesWasm;
import com.google.j2cl.transpiler.passes.AddEnumImplicitMethods;
import com.google.j2cl.transpiler.passes.AddInterfaceConstructorCasts;
import com.google.j2cl.transpiler.passes.AddJavaLangObjectForwardingMethods;
import com.google.j2cl.transpiler.passes.AddNothingReturnStatements;
import com.google.j2cl.transpiler.passes.AddVisibilityMethodBridgesJ2kt;
Expand Down Expand Up @@ -349,6 +350,7 @@ public ImmutableList<Supplier<NormalizationPass>> getPassFactories(BackendOption
// variable motion.
NormalizeMultiExpressions::new,
RemoveUnneededJsDocCasts::new,
AddInterfaceConstructorCasts::new,
NormalizeJsDocCastExpressions::new,
NormalizeJsAwaitMethodInvocations::new,
RemoveUnneededNotNullChecks::new,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2024 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.j2cl.transpiler.passes;

import com.google.j2cl.transpiler.ast.AbstractRewriter;
import com.google.j2cl.transpiler.ast.JavaScriptConstructorReference;
import com.google.j2cl.transpiler.ast.JsDocCastExpression;
import com.google.j2cl.transpiler.ast.Node;
import com.google.j2cl.transpiler.ast.Type;
import com.google.j2cl.transpiler.ast.TypeDescriptors;

/** Adds jsdoc casts to workaround JsCompiler handling of interface constructor (b/79389970). */
public final class AddInterfaceConstructorCasts extends NormalizationPass {

@Override
public void applyTo(Type type) {
type.accept(
new AbstractRewriter() {
@Override
public Node rewriteJavaScriptConstructorReference(
JavaScriptConstructorReference constructorReference) {
if (!constructorReference.getReferencedTypeDeclaration().isInterface()) {
return constructorReference;
}

return JsDocCastExpression.newBuilder()
.setCastTypeDescriptor(TypeDescriptors.get().nativeFunction)
.setExpression(constructorReference)
.build();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.google.j2cl.transpiler.ast.FieldAccess;
import com.google.j2cl.transpiler.ast.InstanceOfExpression;
import com.google.j2cl.transpiler.ast.JavaScriptConstructorReference;
import com.google.j2cl.transpiler.ast.JsDocCastExpression;
import com.google.j2cl.transpiler.ast.Method;
import com.google.j2cl.transpiler.ast.MethodCall;
import com.google.j2cl.transpiler.ast.MethodDescriptor;
Expand Down Expand Up @@ -72,12 +71,7 @@ private static void synthesizeMarkImplementorCalls(Type type) {
// Type.$markImplementor(Type);
type.addLoadTimeStatement(
createMarkImplementorCall(
type.getTypeDescriptor(),
// TODO(b/79389970): remove cast after b/79389970 is handled in Closure.
JsDocCastExpression.newBuilder()
.setCastTypeDescriptor(TypeDescriptors.get().nativeFunction)
.setExpression(new JavaScriptConstructorReference(type.getDeclaration()))
.build()));
type.getTypeDescriptor(), new JavaScriptConstructorReference(type.getDeclaration())));
} else {
// Call markImplementor on all interfaces that are directly implemented by the class to
// implement the support instanceOf Interface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class A {
}
}
A.$markImplementor(/**@type {Function}*/ (A));
$Util.$setClassMetadataForInterface(A, 'abstractinnerclass.InnerClasses$A');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (A), 'abstractinnerclass.InnerClasses$A');

exports = A;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MyInterface {
}
}
MyInterface.$markImplementor(/**@type {Function}*/ (MyInterface));
$Util.$setClassMetadataForInterface(MyInterface, 'abstractinterfaceimpl.MyInterface');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (MyInterface), 'abstractinterfaceimpl.MyInterface');

exports = MyInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class I {
}
}
I.$markImplementor(/**@type {Function}*/ (I));
$Util.$setClassMetadataForInterface(I, 'abstractmethodoverridedefault.AbstractMethodOverrideDefault$I');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (I), 'abstractmethodoverridedefault.AbstractMethodOverrideDefault$I');

exports = I;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class J {
}
}
J.$markImplementor(/**@type {Function}*/ (J));
$Util.$setClassMetadataForInterface(J, 'abstractmethodoverridedefault.AbstractMethodOverrideDefault$J');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (J), 'abstractmethodoverridedefault.AbstractMethodOverrideDefault$J');

exports = J;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SuperInterface {
static $loadModules() {}
}
SuperInterface.$markImplementor(/**@type {Function}*/ (SuperInterface));
$Util.$setClassMetadataForInterface(SuperInterface, 'accidentaloverride.SuperInterface');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (SuperInterface), 'accidentaloverride.SuperInterface');

exports = SuperInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Foo {
/**@const {number} @nodts*/
Foo.f_CONSTANT__annotation_Main_Foo = 123;
Foo.$markImplementor(/**@type {Function}*/ (Foo));
$Util.$setClassMetadataForInterface(Foo, 'annotation.Main$Foo');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (Foo), 'annotation.Main$Foo');

exports = Foo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Zoo {
static $loadModules() {}
}
Zoo.$markImplementor(/**@type {Function}*/ (Zoo));
$Util.$setClassMetadataForInterface(Zoo, 'annotation.Main$Zoo');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (Zoo), 'annotation.Main$Zoo');

exports = Zoo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SomeInterface {
/**@private {SomeClass} @nodts*/
SomeInterface.$static_implicitlyStatic__anonymousclass_SomeInterface;
SomeInterface.$markImplementor(/**@type {Function}*/ (SomeInterface));
$Util.$setClassMetadataForInterface(SomeInterface, 'anonymousclass.SomeInterface');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (SomeInterface), 'anonymousclass.SomeInterface');

exports = SomeInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class G {
}
}
G.$markImplementor(/**@type {Function}*/ (G));
$Util.$setClassMetadataForInterface(G, 'bridgejsmethod.Main$G');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (G), 'bridgejsmethod.Main$G');

exports = G;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class I {
}
}
I.$markImplementor(/**@type {Function}*/ (I));
$Util.$setClassMetadataForInterface(I, 'bridgejsmethod.Main$I');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (I), 'bridgejsmethod.Main$I');

exports = I;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class J {
}
}
J.$markImplementor(/**@type {Function}*/ (J));
$Util.$setClassMetadataForInterface(J, 'bridgejsmethod.Main$J');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (J), 'bridgejsmethod.Main$J');

exports = J;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class M {
}
}
M.$markImplementor(/**@type {Function}*/ (M));
$Util.$setClassMetadataForInterface(M, 'bridgejsmethod.Main$M');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (M), 'bridgejsmethod.Main$M');

exports = M;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class P {
}
}
P.$markImplementor(/**@type {Function}*/ (P));
$Util.$setClassMetadataForInterface(P, 'bridgejsmethod.Main$P');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (P), 'bridgejsmethod.Main$P');

exports = P;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class I {
}
}
I.$markImplementor(/**@type {Function}*/ (I));
$Util.$setClassMetadataForInterface(I, 'bridgemethods.BridgeMethodDefault$I');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (I), 'bridgemethods.BridgeMethodDefault$I');

exports = I;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class II {
static $loadModules() {}
}
II.$markImplementor(/**@type {Function}*/ (II));
$Util.$setClassMetadataForInterface(II, 'bridgemethods.BridgeMethodDefault$II');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (II), 'bridgemethods.BridgeMethodDefault$II');

exports = II;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class JJ {
static $loadModules() {}
}
JJ.$markImplementor(/**@type {Function}*/ (JJ));
$Util.$setClassMetadataForInterface(JJ, 'bridgemethods.BridgeMethodDefault$JJ');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (JJ), 'bridgemethods.BridgeMethodDefault$JJ');

exports = JJ;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class I {
}
}
I.$markImplementor(/**@type {Function}*/ (I));
$Util.$setClassMetadataForInterface(I, 'bridgemethods.BridgeMethodShadowedSignature$I');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (I), 'bridgemethods.BridgeMethodShadowedSignature$I');

exports = I;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Getter {
}
}
Getter.$markImplementor(/**@type {Function}*/ (Getter));
$Util.$setClassMetadataForInterface(Getter, 'bridgemethods.Getter');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (Getter), 'bridgemethods.Getter');

exports = Getter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class I {
}
}
I.$markImplementor(/**@type {Function}*/ (I));
$Util.$setClassMetadataForInterface(I, 'bridgemethods.I');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (I), 'bridgemethods.I');

exports = I;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class InterfaceOverridingDefaultMethod {
}
}
InterfaceOverridingDefaultMethod.$markImplementor(/**@type {Function}*/ (InterfaceOverridingDefaultMethod));
$Util.$setClassMetadataForInterface(InterfaceOverridingDefaultMethod, 'bridgemethods.InterfaceOverridingDefaultMethod');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (InterfaceOverridingDefaultMethod), 'bridgemethods.InterfaceOverridingDefaultMethod');

exports = InterfaceOverridingDefaultMethod;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class InterfaceWithDefaultMethod {
static $loadModules() {}
}
InterfaceWithDefaultMethod.$markImplementor(/**@type {Function}*/ (InterfaceWithDefaultMethod));
$Util.$setClassMetadataForInterface(InterfaceWithDefaultMethod, 'bridgemethods.InterfaceWithDefaultMethod');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (InterfaceWithDefaultMethod), 'bridgemethods.InterfaceWithDefaultMethod');

exports = InterfaceWithDefaultMethod;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class J2ktIncompatible {
static $loadModules() {}
}
J2ktIncompatible.$markImplementor(/**@type {Function}*/ (J2ktIncompatible));
$Util.$setClassMetadataForInterface(J2ktIncompatible, 'bridgemethods.J2ktIncompatible');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (J2ktIncompatible), 'bridgemethods.J2ktIncompatible');

exports = J2ktIncompatible;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Map {
}
}
Map.$markImplementor(/**@type {Function}*/ (Map));
$Util.$setClassMetadataForInterface(Map, 'bridgemethods.Map');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (Map), 'bridgemethods.Map');

exports = Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SomeInterface {
static $loadModules() {}
}
SomeInterface.$markImplementor(/**@type {Function}*/ (SomeInterface));
$Util.$setClassMetadataForInterface(SomeInterface, 'bridgemethods.SomeInterface');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (SomeInterface), 'bridgemethods.SomeInterface');

exports = SomeInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SomeOtherFooInterface {
}
}
SomeOtherFooInterface.$markImplementor(/**@type {Function}*/ (SomeOtherFooInterface));
$Util.$setClassMetadataForInterface(SomeOtherFooInterface, 'bridgemethods.SomeOtherFooInterface');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (SomeOtherFooInterface), 'bridgemethods.SomeOtherFooInterface');

exports = SomeOtherFooInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SpecializedInterface {
static $loadModules() {}
}
SpecializedInterface.$markImplementor(/**@type {Function}*/ (SpecializedInterface));
$Util.$setClassMetadataForInterface(SpecializedInterface, 'bridgemethods.SpecializedInterface');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (SpecializedInterface), 'bridgemethods.SpecializedInterface');

exports = SpecializedInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SpecializingReturnInterface {
}
}
SpecializingReturnInterface.$markImplementor(/**@type {Function}*/ (SpecializingReturnInterface));
$Util.$setClassMetadataForInterface(SpecializingReturnInterface, 'bridgemethods.SpecializingReturnInterface');
$Util.$setClassMetadataForInterface(/**@type {Function}*/ (SpecializingReturnInterface), 'bridgemethods.SpecializingReturnInterface');

exports = SpecializingReturnInterface;

Expand Down
Loading

0 comments on commit 558b06b

Please sign in to comment.