mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 15:29:22 +00:00
support for $noarrow macro
By default, JSweet generates arrow functions for Java lambda (the 'this' semantics is closer to Java). Wrapping a function in $noarrow will force JSweet to produce a plain old JavaScript function rather than an arrow function.
This commit is contained in:
parent
e39ded506f
commit
a91bd7ec8f
@ -206,6 +206,18 @@ public final class Lang {
|
|||||||
*/
|
*/
|
||||||
native public static def.js.Function function(Supplier<?> function);
|
native public static def.js.Function function(Supplier<?> function);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Forces JSweet to create a plain JavaScript function rather than an
|
||||||
|
* "arrow" function.
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Hence, <code>function((a) -> { return a * a; })</code> transpiles to
|
||||||
|
* <code>(a) => { return a * a; }</code>. But
|
||||||
|
* <code>$noarrow(function((a) -> { return a * a; }))</code> transpiles to
|
||||||
|
* <code>function(a) { return a * a; }</code>
|
||||||
|
*/
|
||||||
|
native public static def.js.Function $noarrow(def.js.Function function);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Casts a functional interface to a JavaScript function object.
|
* Casts a functional interface to a JavaScript function object.
|
||||||
*
|
*
|
||||||
|
|||||||
@ -157,6 +157,18 @@ public final class Lang {
|
|||||||
*/
|
*/
|
||||||
native public static def.js.Array<Float> array(float[] array);
|
native public static def.js.Array<Float> array(float[] array);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Forces JSweet to create a plain JavaScript function rather than an
|
||||||
|
* "arrow" function.
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Hence, <code>function((a) -> { return a * a; })</code> transpiles to
|
||||||
|
* <code>(a) => { return a * a; }</code>. But
|
||||||
|
* <code>$noarrow(function((a) -> { return a * a; }))</code> transpiles to
|
||||||
|
* <code>function(a) { return a * a; }</code>
|
||||||
|
*/
|
||||||
|
native public static def.js.Function $noarrow(def.js.Function function);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Casts a functional interface to a JavaScript function object.
|
* Casts a functional interface to a JavaScript function object.
|
||||||
*
|
*
|
||||||
|
|||||||
@ -4508,7 +4508,8 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
public void visitLambda(JCLambda lamba) {
|
public void visitLambda(JCLambda lamba) {
|
||||||
boolean regularFunction = false;
|
boolean regularFunction = false;
|
||||||
if (getParent() instanceof JCMethodInvocation
|
if (getParent() instanceof JCMethodInvocation
|
||||||
&& ((JCMethodInvocation) getParent()).meth.toString().endsWith("function")) {
|
&& ((JCMethodInvocation) getParent()).meth.toString().endsWith("function") && getParentOfParent() instanceof JCMethodInvocation
|
||||||
|
&& ((JCMethodInvocation) getParentOfParent()).meth.toString().endsWith("$noarrow")) {
|
||||||
MethodInvocationElement invocation = (MethodInvocationElement) ExtendedElementFactory.INSTANCE
|
MethodInvocationElement invocation = (MethodInvocationElement) ExtendedElementFactory.INSTANCE
|
||||||
.create(getParent());
|
.create(getParent());
|
||||||
if (JSweetConfig.UTIL_CLASSNAME.equals(invocation.getMethod().getEnclosingElement().toString())) {
|
if (JSweetConfig.UTIL_CLASSNAME.equals(invocation.getMethod().getEnclosingElement().toString())) {
|
||||||
|
|||||||
@ -495,6 +495,10 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
|
|||||||
print("typeof ").print(invocationElement.getArgument(0));
|
print("typeof ").print(invocationElement.getArgument(0));
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
case "$noarrow":
|
||||||
|
print(invocationElement.getArgument(0));
|
||||||
|
return true;
|
||||||
|
|
||||||
case "equalsStrict":
|
case "equalsStrict":
|
||||||
print("(").print(invocationElement.getArgument(0)).print(" === ")
|
print("(").print(invocationElement.getArgument(0)).print(" === ")
|
||||||
.print(invocationElement.getArgument(1)).print(")");
|
.print(invocationElement.getArgument(1)).print(")");
|
||||||
|
|||||||
@ -266,6 +266,17 @@ public abstract class AbstractTreeScanner extends TreeScanner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the parent of the immediate parent in the printer's scanning stack.
|
||||||
|
*/
|
||||||
|
public JCTree getParentOfParent() {
|
||||||
|
if (this.stack.size() >= 3) {
|
||||||
|
return this.stack.get(this.stack.size() - 3);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the parent element in the printer's scanning stack.
|
* Gets the parent element in the printer's scanning stack.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -46,7 +46,7 @@ class Globals {
|
|||||||
Function originalMethod = (Function) descriptor.$get("value");
|
Function originalMethod = (Function) descriptor.$get("value");
|
||||||
|
|
||||||
// editing the descriptor/value parameter
|
// editing the descriptor/value parameter
|
||||||
descriptor.$set("value", function(() -> {
|
descriptor.$set("value", $noarrow(function(() -> {
|
||||||
Array<Object> args = $array();
|
Array<Object> args = $array();
|
||||||
for (int _i = 0; _i < arguments.length; _i++) {
|
for (int _i = 0; _i < arguments.length; _i++) {
|
||||||
array(args)[_i - 0] = arguments[_i];
|
array(args)[_i - 0] = arguments[_i];
|
||||||
@ -58,7 +58,7 @@ class Globals {
|
|||||||
console.log("Call: " + key + "(" + a + ") => " + r);
|
console.log("Call: " + key + "(" + a + ") => " + r);
|
||||||
trace.push("Call: " + key + "(" + a + ") => " + r);
|
trace.push("Call: " + key + "(" + a + ") => " + r);
|
||||||
return result;
|
return result;
|
||||||
}));
|
})));
|
||||||
|
|
||||||
// return edited descriptor as opposed to overwriting the descriptor
|
// return edited descriptor as opposed to overwriting the descriptor
|
||||||
return descriptor;
|
return descriptor;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user