Skip to content

Commit

Permalink
Created WrappedInteger, so no longer have to generate a large handful…
Browse files Browse the repository at this point in the history
… of new objects per draw loop
  • Loading branch information
Jonathan Feinberg committed Aug 17, 2010
1 parent 13ccf86 commit 85a0024
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
18 changes: 10 additions & 8 deletions 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
Expand All @@ -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;
Expand Down Expand Up @@ -68,9 +70,8 @@ abstract public class PAppletJythonDriver extends PApplet {
// "<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();
}

Expand All @@ -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));

Expand Down
13 changes: 13 additions & 0 deletions 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);
}

}
26 changes: 22 additions & 4 deletions src/jycessing/build/Binding.java
Expand Up @@ -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");
Expand All @@ -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");
Expand Down
18 changes: 15 additions & 3 deletions src/jycessing/build/DriverGenerator.java
Expand Up @@ -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",
Expand Down Expand Up @@ -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());
}
}
Expand Down Expand Up @@ -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();
}
}
5 changes: 5 additions & 0 deletions template/DriverImpl.java
Expand Up @@ -32,6 +32,11 @@ protected void populateBuiltins() {
%METHOD_BINDINGS%
}

@Override
protected void setIntegerFields() {
%INTEGER_FIELD_BINDINGS%
}

@Override
protected void setFields() {
%FIELD_BINDINGS%
Expand Down

0 comments on commit 85a0024

Please sign in to comment.