Skip to content
Browse files

Created WrappedInteger, so no longer have to generate a large handful…

… of new objects per draw loop
  • Loading branch information...
1 parent 13ccf86 commit 85a0024a4c2b31c8e2003507f47885de9edada77 @jdf committed Aug 16, 2010
View
18 src/jycessing/PAppletJythonDriver.java
@@ -1,12 +1,12 @@
/*
* Copyright 2010 Jonathan Feinberg
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -31,15 +31,17 @@
import processing.core.PConstants;
/**
- *
+ *
* @author Jonathan Feinberg <jdf@pobox.com>
- *
+ *
*/
@SuppressWarnings("serial")
abstract public class PAppletJythonDriver extends PApplet {
abstract protected void populateBuiltins();
+ abstract protected void setIntegerFields();
+
abstract protected void setFields();
protected final PyStringMap builtins;
@@ -68,9 +70,8 @@
// "<string>"
private void interpretSketch() {
Py.setSystemState(interp.getSystemState());
- Py.exec(Py.compile_flags(
- programText, pySketchPath, CompileMode.exec, new CompilerFlags()),
- interp.getLocals(), null);
+ Py.exec(Py.compile_flags(programText, pySketchPath, CompileMode.exec,
+ new CompilerFlags()), interp.getLocals(), null);
Py.flushLine();
}
@@ -83,6 +84,7 @@ public PAppletJythonDriver(final InteractiveConsole interp, final String sketchP
this.interp = interp;
initializeStatics(builtins);
populateBuiltins();
+ setIntegerFields();
setFields();
builtins.__setitem__("this", Py.java2py(this));
View
13 src/jycessing/WrappedInteger.java
@@ -0,0 +1,13 @@
+package jycessing;
+
+import org.python.core.PyInteger;
+
+abstract public class WrappedInteger extends PyInteger {
+
+ abstract public int getValue();
+
+ public WrappedInteger() {
+ super(0);
+ }
+
+}
View
26 src/jycessing/build/Binding.java
@@ -34,24 +34,37 @@ public boolean hasGlobal() {
return global != null;
}
+ public Class<?> getGlobalType() {
+ return global.getType();
+ }
+
public String toString() {
final boolean hasMethods = methods.size() > 0;
+ final boolean isWrappedInteger = hasGlobal()
+ && global.getType().equals(int.class);
final StringBuilder sb = new StringBuilder();
if (isPythonBuiltin) {
sb.append("final PyObject ").append(name).append("_builtin = ");
sb.append(String.format("builtins.__getitem__(\"%s\");\n", name));
}
sb.append(String.format("builtins.__setitem__(\"%s\", ", name));
+
if (hasGlobal()) {
- sb.append(TypeUtil.pyConversionPrefix(global.getType()));
- sb.append(global.getName());
- sb.append(")");
+ if (isWrappedInteger) {
+ sb.append("new WrappedInteger()");
+ } else {
+ sb.append(TypeUtil.pyConversionPrefix(global.getType()));
+ sb.append(global.getName());
+ sb.append(")");
+ }
} else {
sb.append("new PyObject()");
}
- if (hasMethods) {
+ if (hasMethods || isWrappedInteger) {
sb.append("{");
+ }
+ if (hasMethods) {
sb
.append("\tpublic PyObject __call__(final PyObject[] args, final String[] kws) {\n");
sb.append("\t\tswitch(args.length) {\n");
@@ -73,6 +86,11 @@ public String toString() {
sb.append(m.toString());
}
sb.append("\t\t}\n\t}\n");
+ }
+ if (isWrappedInteger) {
+ sb.append("\tpublic int getValue() { return ").append(name).append("; }\n");
+ }
+ if (hasMethods || isWrappedInteger) {
sb.append("}");
}
sb.append(");\n");
View
18 src/jycessing/build/DriverGenerator.java
@@ -34,7 +34,7 @@
@SuppressWarnings("serial")
public class DriverGenerator {
- final String BAD_METHOD = "^(init|handleDraw|draw|parse[A-Z].*|arraycopy|openStream|str)$";
+ final String BAD_METHOD = "^(init|handleDraw|draw|parse[A-Z].*|arraycopy|openStream|str|.*Pressed)$";
private static final Set<String> BAD_FIELDS = new HashSet<String>(Arrays.asList(
"screen", "args", "recorder", "frame", "g", "selectedFile", "keyEvent",
@@ -107,7 +107,17 @@ public String getMethodBindings() {
public String getFieldBindings() {
final StringBuilder sb = new StringBuilder();
for (final Binding b : bindings.values()) {
- if (b.hasGlobal()) {
+ if (b.hasGlobal() && !b.getGlobalType().equals(int.class)) {
+ sb.append(b.toString());
+ }
+ }
+ return sb.toString();
+ }
+
+ public String getIntegerFieldBindings() {
+ final StringBuilder sb = new StringBuilder();
+ for (final Binding b : bindings.values()) {
+ if (b.hasGlobal() && b.getGlobalType().equals(int.class)) {
sb.append(b.toString());
}
}
@@ -136,9 +146,11 @@ public static void main(final String[] args) throws Exception {
.getMethodBindings());
final String withFieldBindings = withMethodBindings.replace("%FIELD_BINDINGS%",
gen.getFieldBindings());
+ final String withIntegerFieldBindings = withFieldBindings.replace(
+ "%INTEGER_FIELD_BINDINGS%", gen.getIntegerFieldBindings());
final FileWriter out = new FileWriter("generated/jycessing/DriverImpl.java");
- out.write(withFieldBindings);
+ out.write(withIntegerFieldBindings);
out.close();
}
}
View
5 template/DriverImpl.java
@@ -32,6 +32,11 @@ protected void populateBuiltins() {
%METHOD_BINDINGS%
}
+ @Override
+ protected void setIntegerFields() {
+ %INTEGER_FIELD_BINDINGS%
+ }
+
@Override
protected void setFields() {
%FIELD_BINDINGS%

0 comments on commit 85a0024

Please sign in to comment.
Something went wrong with that request. Please try again.