mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 07:19: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);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
|
||||
@ -157,6 +157,18 @@ public final class Lang {
|
||||
*/
|
||||
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.
|
||||
*
|
||||
|
||||
@ -4508,7 +4508,8 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
||||
public void visitLambda(JCLambda lamba) {
|
||||
boolean regularFunction = false;
|
||||
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
|
||||
.create(getParent());
|
||||
if (JSweetConfig.UTIL_CLASSNAME.equals(invocation.getMethod().getEnclosingElement().toString())) {
|
||||
|
||||
@ -495,6 +495,10 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
|
||||
print("typeof ").print(invocationElement.getArgument(0));
|
||||
return true;
|
||||
|
||||
case "$noarrow":
|
||||
print(invocationElement.getArgument(0));
|
||||
return true;
|
||||
|
||||
case "equalsStrict":
|
||||
print("(").print(invocationElement.getArgument(0)).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.
|
||||
*/
|
||||
|
||||
@ -46,7 +46,7 @@ class Globals {
|
||||
Function originalMethod = (Function) descriptor.$get("value");
|
||||
|
||||
// editing the descriptor/value parameter
|
||||
descriptor.$set("value", function(() -> {
|
||||
descriptor.$set("value", $noarrow(function(() -> {
|
||||
Array<Object> args = $array();
|
||||
for (int _i = 0; _i < arguments.length; _i++) {
|
||||
array(args)[_i - 0] = arguments[_i];
|
||||
@ -58,7 +58,7 @@ class Globals {
|
||||
console.log("Call: " + key + "(" + a + ") => " + r);
|
||||
trace.push("Call: " + key + "(" + a + ") => " + r);
|
||||
return result;
|
||||
}));
|
||||
})));
|
||||
|
||||
// return edited descriptor as opposed to overwriting the descriptor
|
||||
return descriptor;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user