From dee9ed7ba9021596cb9b43b64a0f49e98b3cfeb2 Mon Sep 17 00:00:00 2001 From: Hamza Remmal <56235032+hamzaremmal@users.noreply.github.com> Date: Tue, 14 Nov 2023 17:48:55 +0100 Subject: [PATCH] Encode the name of the attributes in selectDynamic and applyDynamic --- compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala | 3 ++- library/src/scala/reflect/Selectable.scala | 4 ++-- tests/run/i18612-a.scala | 6 ++++++ tests/run/i18612-b.scala | 6 ++++++ tests/run/i18612-c.scala | 7 +++++++ tests/run/i18612-d.scala | 7 +++++++ 6 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 tests/run/i18612-a.scala create mode 100644 tests/run/i18612-b.scala create mode 100644 tests/run/i18612-c.scala create mode 100644 tests/run/i18612-d.scala diff --git a/compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala b/compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala index d0694617f61e..6f8b3fc6f135 100644 --- a/compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala +++ b/compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala @@ -36,6 +36,7 @@ import dotty.tools.dotc.transform.sjs.JSSymUtils.* import JSEncoding.* import ScopedVar.withScopedVars +import scala.reflect.NameTransformer /** Main codegen for Scala.js IR. * @@ -4218,7 +4219,7 @@ class JSCodeGen()(using genCtx: Context) { } } - val methodName = MethodName.reflectiveProxy(methodNameStr, formalParamTypeRefs) + val methodName = MethodName.reflectiveProxy(NameTransformer.encode(methodNameStr), formalParamTypeRefs) js.Apply(js.ApplyFlags.empty, selectedValueTree, js.MethodIdent(methodName), actualArgs)(jstpe.AnyType) } diff --git a/library/src/scala/reflect/Selectable.scala b/library/src/scala/reflect/Selectable.scala index 6da2b0ff88cd..ac8e502128bb 100644 --- a/library/src/scala/reflect/Selectable.scala +++ b/library/src/scala/reflect/Selectable.scala @@ -21,7 +21,7 @@ trait Selectable extends scala.Selectable: final def selectDynamic(name: String): Any = val rcls = selectedValue.getClass try - val fld = rcls.getField(name).nn + val fld = rcls.getField(NameTransformer.encode(name)).nn ensureAccessible(fld) fld.get(selectedValue) catch case ex: NoSuchFieldException => @@ -35,7 +35,7 @@ trait Selectable extends scala.Selectable: */ final def applyDynamic(name: String, paramTypes: Class[?]*)(args: Any*): Any = val rcls = selectedValue.getClass - val mth = rcls.getMethod(name, paramTypes*).nn + val mth = rcls.getMethod(NameTransformer.encode(name), paramTypes*).nn ensureAccessible(mth) mth.invoke(selectedValue, args.asInstanceOf[Seq[AnyRef]]*) diff --git a/tests/run/i18612-a.scala b/tests/run/i18612-a.scala new file mode 100644 index 000000000000..286c538ab354 --- /dev/null +++ b/tests/run/i18612-a.scala @@ -0,0 +1,6 @@ +class X extends scala.reflect.Selectable: + val `+` = "1" + +@main def Test = + val x = X() + assert(x.selectDynamic("+") == "1") \ No newline at end of file diff --git a/tests/run/i18612-b.scala b/tests/run/i18612-b.scala new file mode 100644 index 000000000000..ed02ea296e41 --- /dev/null +++ b/tests/run/i18612-b.scala @@ -0,0 +1,6 @@ +class X extends scala.reflect.Selectable: + val plus = "1" + +@main def Test = + val x = X() + assert(x.selectDynamic("plus") == "1") \ No newline at end of file diff --git a/tests/run/i18612-c.scala b/tests/run/i18612-c.scala new file mode 100644 index 000000000000..0f4ca99eac38 --- /dev/null +++ b/tests/run/i18612-c.scala @@ -0,0 +1,7 @@ +class X extends scala.reflect.Selectable: + def + = "1" + +@main def Test = + val x = X() + assert(x.selectDynamic("+") == "1") + assert(x.applyDynamic("+")() == "1") \ No newline at end of file diff --git a/tests/run/i18612-d.scala b/tests/run/i18612-d.scala new file mode 100644 index 000000000000..82bc9f67f86a --- /dev/null +++ b/tests/run/i18612-d.scala @@ -0,0 +1,7 @@ +class X extends scala.reflect.Selectable: + def plus = "1" + +@main def Test = + val x = X() + assert(x.selectDynamic("plus") == "1") + assert(x.applyDynamic("plus")() == "1") \ No newline at end of file