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:
Renaud Pawlak 2017-05-11 17:31:32 +02:00
parent e39ded506f
commit a91bd7ec8f
6 changed files with 1205 additions and 1165 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -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())) {

View File

@ -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(")");

View File

@ -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.
*/ */

View File

@ -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;