Permalink
Browse files

let's start using preconstructor instead of fluent

  • Loading branch information...
1 parent 6939239 commit f9f00e784542754ed4bfda17e734d51de73047be @qmx qmx committed May 12, 2012
Showing with 21 additions and 25 deletions.
  1. +21 −25 src/main/java/org/dynjs/parser/statement/CallStatement.java
@@ -15,17 +15,17 @@
*/
package org.dynjs.parser.statement;
-import java.util.List;
-
import me.qmx.jitescript.CodeBlock;
import org.antlr.runtime.tree.Tree;
import org.dynjs.compiler.DynJSCompiler;
import org.dynjs.parser.Statement;
import org.dynjs.runtime.DynThreadContext;
import org.dynjs.runtime.RT;
-import static me.qmx.jitescript.CodeBlock.*;
-import static me.qmx.jitescript.util.CodegenUtils.*;
+import java.util.List;
+
+import static me.qmx.jitescript.util.CodegenUtils.p;
+import static me.qmx.jitescript.util.CodegenUtils.sig;
public class CallStatement extends BaseStatement implements Statement {
@@ -42,26 +42,22 @@ public CallStatement(final Tree tree, final DynThreadContext context, final Stat
@Override
public CodeBlock getCodeBlock() {
- CodeBlock codeBlock = newCodeBlock();
- codeBlock = codeBlock
- .bipush(args.size())
- .anewarray(p(Object.class))
- .astore(4);
-
- for (int i = 0; i < args.size(); i++) {
- codeBlock = codeBlock
- .aload(4)
- .bipush(i)
- .append(args.get(i).getCodeBlock())
- .aastore();
- }
-
- codeBlock = codeBlock
- .append(lhs.getCodeBlock())
- .aload(DynJSCompiler.Arities.CONTEXT)
- .aload(4)
- .invokedynamic("dyn:call", sig(Object.class, Object.class, DynThreadContext.class, Object[].class), RT.BOOTSTRAP, RT.BOOTSTRAP_ARGS);
-
- return codeBlock;
+ return new CodeBlock() {{
+ bipush(args.size());
+ anewarray(p(Object.class));
+ astore(4);
+
+ for (int i = 0; i < args.size(); i++) {
+ aload(4);
+ bipush(i);
+ append(args.get(i).getCodeBlock());
+ aastore();
+ }
+
+ append(lhs.getCodeBlock());
+ aload(DynJSCompiler.Arities.CONTEXT);
+ aload(4);
+ invokedynamic("dyn:call", sig(Object.class, Object.class, DynThreadContext.class, Object[].class), RT.BOOTSTRAP, RT.BOOTSTRAP_ARGS);
+ }};
@juanplopes
juanplopes May 14, 2012

Usually don't like this kind of construction. Creating a new type just for this sub-optimal syntax sugar seems too much to me.

@qmx
qmx May 14, 2012 Member

The problem with method chains is that errors pop out at the beginning line, so you'll never know who NPEd until you debug it. Just for this it's worth the hassle.

a() // throws NPE
 .b(x.getWTF()) 
 .c(y.getLol()) //NPE is here
}
}

0 comments on commit f9f00e7

Please sign in to comment.