diff --git a/doc/jsweet-language-specifications.md b/doc/jsweet-language-specifications.md index ed20fc16..a12ed488 100644 --- a/doc/jsweet-language-specifications.md +++ b/doc/jsweet-language-specifications.md @@ -494,7 +494,7 @@ Globals myVariable = null; One must remember that `Globals` classes and `global` packages are erased at runtime so that their members will be directly accessible. For instance `mypackage.Globals.m()` in a JSweet program corresponds to the `mypackage.m()` function in the generated code and in the JavaScript VM at runtime. Also, `mypackage.globals.Globals.m()` corresponds to *m()*. -In order to erase packages in the generated code, programmers can also use the `@Root` annotation, which will be explained in Section \[packaging\]. +In order to erase packages in the generated code, programmers can also use the `@Root` annotation, which will be explained in Section \[packaging\]. ### Optional parameters diff --git a/doc/jsweet-language-specifications.tex b/doc/jsweet-language-specifications.tex index d604c75f..02a929f6 100644 --- a/doc/jsweet-language-specifications.tex +++ b/doc/jsweet-language-specifications.tex @@ -521,7 +521,7 @@ Globals myVariable = null; One must remember that \texttt{Globals} classes and \texttt{global} packages are erased at runtime so that their members will be directly accessible. For instance \texttt{mypackage.Globals.m()} in a JSweet program corresponds to the \texttt{mypackage.m()} function in the generated code and in the JavaScript VM at runtime. Also, \texttt{mypackage.globals.Globals.m()} corresponds to \emph{m()}. -In order to erase packages in the generated code, programmers can also use the \texttt{@Root} annotation, which will be explained in Section~\ref{packaging}. +In order to erase packages in the generated code, programmers can also use the \texttt{@Root} annotation, which will be explained in Section \ref{packaging}. \section{Optional parameters} \label{optional-parameters} diff --git a/src/main/java/org/jsweet/JSweetConfig.java b/src/main/java/org/jsweet/JSweetConfig.java index 3770c8ce..b263d1fe 100644 --- a/src/main/java/org/jsweet/JSweetConfig.java +++ b/src/main/java/org/jsweet/JSweetConfig.java @@ -186,6 +186,10 @@ public abstract class JSweetConfig { /** The constant for indexed assignment function. */ public static final String INDEXED_SET_FUCTION_NAME = "$set"; public static final String INDEXED_DELETE_FUCTION_NAME = "$delete"; + public static final String INDEXED_GET_STATIC_FUCTION_NAME = "$getStatic"; + /** The constant for indexed assignment function. */ + public static final String INDEXED_SET_STATIC_FUCTION_NAME = "$setStatic"; + public static final String INDEXED_DELETE_STATIC_FUCTION_NAME = "$deleteStatic"; public static final String NEW_FUNCTION_NAME = "$new"; public static final String ANONYMOUS_FUNCTION_NAME = "$apply"; public static final String ANONYMOUS_STATIC_FUNCTION_NAME = "$applyStatic"; diff --git a/src/main/java/org/jsweet/transpiler/typescript/Java2TypeScriptAdapter.java b/src/main/java/org/jsweet/transpiler/typescript/Java2TypeScriptAdapter.java index a570b00f..c61552f9 100644 --- a/src/main/java/org/jsweet/transpiler/typescript/Java2TypeScriptAdapter.java +++ b/src/main/java/org/jsweet/transpiler/typescript/Java2TypeScriptAdapter.java @@ -13,7 +13,7 @@ * 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 org.jsweet.transpiler.typescript; import static org.jsweet.JSweetConfig.ANNOTATION_ERASED; @@ -26,6 +26,9 @@ import static org.jsweet.JSweetConfig.GLOBALS_PACKAGE_NAME; import static org.jsweet.JSweetConfig.INDEXED_DELETE_FUCTION_NAME; import static org.jsweet.JSweetConfig.INDEXED_GET_FUCTION_NAME; import static org.jsweet.JSweetConfig.INDEXED_SET_FUCTION_NAME; +import static org.jsweet.JSweetConfig.INDEXED_DELETE_STATIC_FUCTION_NAME; +import static org.jsweet.JSweetConfig.INDEXED_GET_STATIC_FUCTION_NAME; +import static org.jsweet.JSweetConfig.INDEXED_SET_STATIC_FUCTION_NAME; import static org.jsweet.JSweetConfig.LANG_PACKAGE; import static org.jsweet.JSweetConfig.TUPLE_CLASSES_PACKAGE; import static org.jsweet.JSweetConfig.UNION_CLASS_NAME; @@ -291,6 +294,16 @@ public class Java2TypeScriptAdapter extends AbstractPrinterAdapter { } return true; } + if (matchesMethod(targetClassName, targetMethodName, null, INDEXED_GET_STATIC_FUCTION_NAME)) { + if (isWithinGlobals(targetClassName)) { + report(invocation, JSweetProblem.GLOBAL_INDEXER_GET); + return true; + } + + getPrinter().print(fieldAccess.selected).print("[").print(invocation.args.head).print("]"); + return true; + } + if (matchesMethod(targetClassName, targetMethodName, null, INDEXED_SET_FUCTION_NAME)) { if (isWithinGlobals(targetClassName)) { @@ -317,6 +330,18 @@ public class Java2TypeScriptAdapter extends AbstractPrinterAdapter { } return true; } + + if (matchesMethod(targetClassName, targetMethodName, null, INDEXED_SET_STATIC_FUCTION_NAME)) { + + if (isWithinGlobals(targetClassName)) { + report(invocation, JSweetProblem.GLOBAL_INDEXER_SET); + return true; + } + + getPrinter().print(fieldAccess.selected).print("[").print(invocation.args.head).print("] = ").print(invocation.args.tail.head); + return true; + } + if (matchesMethod(targetClassName, targetMethodName, null, INDEXED_DELETE_FUCTION_NAME)) { if (isWithinGlobals(targetClassName)) { report(invocation, JSweetProblem.GLOBAL_DELETE); @@ -331,6 +356,16 @@ public class Java2TypeScriptAdapter extends AbstractPrinterAdapter { return true; } + if (matchesMethod(targetClassName, targetMethodName, null, INDEXED_DELETE_STATIC_FUCTION_NAME)) { + if (isWithinGlobals(targetClassName)) { + report(invocation, JSweetProblem.GLOBAL_DELETE); + return true; + } + + getPrinter().print("delete ").print(fieldAccess.selected).print("[").print(invocation.args.head).print("]"); + return true; + } + if (targetClassName != null && targetClassName.endsWith(GLOBALS_CLASS_NAME)) { if (getPrinter().getContext().useModules) { if (JSweetConfig.GLOBALS_PACKAGE_NAME.equals(targetType.getEnclosingElement().getSimpleName().toString())) {