Subversion Repositories pub

Compare Revisions

No changes between revisions

Ignore whitespace Rev 68 → Rev 65

/pluggablejs/trunk/ChangeLog
1,22 → 1,4
1.1.1build36: 2008-07-05
* Re-fixed including from within jar
* General bugfixes
+ Released
 
1.1.1build35: not a release
* Fixed loading of plugins from within jar
* Fixed including (lang.include()) files from within jar
* Added code injection to Sandbox while executing (SandboxAccessor's can
benefit from this)
* Made PluginProperties public again, otherwise it couldn't be modified
from JS
* Made PluginReader public
* Simplified plugin's properties retrieval (plugin identification over
the first line)
 
 
1.1.1build34: not a release
* Bumped revision number
* Renamed the $net object to $net_outlyer to be less intrusive
* Interactive shell prints the result of each line (optionally and on by default)
* Cleaner use of internal variables, there's no longer a need to
23,7 → 5,7
reserve any variable name / namespace beyond $net_outlyer
* lang.array() family of methods to create Java arrays
* lang.var_dump() to help in debugging
* (optional) lang.for_each() as alternative to "for each"
* lang.for_each()
 
1.1.0build33: 2008-07-01
* Sandbox can be created with no associated file (only boilerPlate code
36,7 → 18,6
* More opaque hierarchies where appropriate
* Output.print()
* PluginObject.features as a Set instead of a List
+ Released
 
1.1.0build32: not a release
* Removed the deprecated versions of Sandbox.createDelayedImplementation()
52,4 → 33,3
 
1.0build29: 2008-06-29
* Initial public pre-release
+ Released
/pluggablejs/trunk/src/net/outlyer/plugins/Sandbox_R1.java
File deleted
Property changes:
Deleted: svn:keywords
-Rev Id Date
\ No newline at end of property
/pluggablejs/trunk/src/net/outlyer/plugins/Shell.java
62,12 → 62,11
try {
// undocumented features: when
// $net_outlyer.runtime.internal.shell.autoEOL is true an extra println() is
// $net_outlyer.runtime.internal.shell,autoEOL is true an extra println() is
// issued after each eval
// and when
// $net_outlyer.runtime.internal.shell.inspect is true the result
// of each line is automatically printed
// arrayDump is used internally to print JavaScript arrays
rhino.eval(getInternalObjectFieldName("shell")+" = { " +
" autoEOL: false, inspect: true, arrayDump: null };");
jsArray = rhino.eval("new Array()");
75,8 → 74,8
// Define the arrayDump internal function
rhino.eval(arrayDumpFn+
" = function(arr) {\n" +
" if (0==arr.length) { java.lang.System.out.println('[]'); return; }\n"+
" var s = new String('[');" +
" var s = new String();" +
" s += '[';\n" +
" for (var i=0;i<arr.length;++i) {\n" +
" s += arr[i]+',';\n" +
" }\n" +
156,7 → 155,7
pe.exportObject("ui", new GUI());
pe.exportObject("gui", new GUI());
pe.exportObject("cui", new UI()); // cui stands for Console UI
pe.exportObject("lang", new RedundantLanguageExtensions());
pe.exportObject("lang", new LanguageExtensions());
final PluginObject po = new PluginObject();
pe.setPluginObject(po);
 
/pluggablejs/trunk/src/net/outlyer/plugins/Internal.java
26,7 → 26,7
 
// $Id$
 
import javax.script.Invocable;
import java.util.Map;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
 
67,18 → 67,14
private static String mapName = "$_";
static String getInternalInitialisation() {
// Commented-out is the the closer-to-Java version
return String.format("= {\n" +
// " %2$s: new java.util.HashMap(),\n" +
" %2$s: {},\n" +
" %2$s: new java.util.HashMap(),\n" +
" set: function(n,v) {\n"+
// " %1$s.put(n,v);\n" +
" %1$s[n] = v;\n" +
" %1$s.put(n,v);\n" +
" return v;\n" +
" },\n" +
" get: function(n) {\n" +
// " return %1$s.get(n);\n" +
" return %1$s[n];\n" +
" return %1$s.get(n);\n" +
" }\n"+
"};",
getInternalObjectFieldName(mapName),
85,38 → 81,16
mapName);
}
 
static void setInternalValue(final ScriptEngine e,
static void setInternalValue(final ScriptEngine engine,
final String name,
final Object value) throws ScriptException {
assert null != e;
assert null != name;
// final Map map = (Map) e.eval(getInternalObjectFieldName(mapName));
// map.put(name, value);
try {
assert e instanceof Invocable;
final Invocable ie = (Invocable) e;
ie.invokeMethod(e.eval(getInternalObjectName()), "set", name, value);
}
catch (final NoSuchMethodException x) {
throw new ScriptException("Internal error while storing " +
"internal variable: "+x.getMessage());
}
final Map map = (Map) engine.eval(getInternalObjectFieldName(mapName));
map.put(name, value);
}
 
static Object getInternalValue(final ScriptEngine e,
final String name) throws ScriptException {
assert null != e;
assert null != name;
// final Map map = (Map) e.eval(getInternalObjectFieldName(mapName));
// return map.get(name);
try {
assert e instanceof Invocable;
final Invocable ie = (Invocable) e;
return ie.invokeMethod(e.eval(getInternalObjectName()), name);
}
catch (final NoSuchMethodException x) {
throw new ScriptException("Internal error while accessing " +
"internal variable: "+x.getMessage());
}
final Map map = (Map) e.eval(getInternalObjectFieldName(mapName));
return map.get(name);
}
}
/pluggablejs/trunk/src/net/outlyer/plugins/utils/RedundantLanguageExtensions.java
File deleted
Property changes:
Deleted: svn:keyword
-Rev Id Date
\ No newline at end of property
/pluggablejs/trunk/src/net/outlyer/plugins/utils/LanguageExtensions.java
28,6 → 28,7
 
import java.lang.reflect.Array;
import java.util.Arrays;
import net.outlyer.plugins.Functor;
import net.outlyer.plugins.SandboxAccessor;
 
/**
38,16 → 39,8
public class LanguageExtensions extends LanguageExtensions_V1
implements SandboxAccessor {
 
public final int interfaceRevision;
public static final int revision = 2;
 
public LanguageExtensions() {
this(2);
}
protected LanguageExtensions(int revision) {
interfaceRevision = revision;
}
 
// The array() family is mostly redundant since rhino translates
// automatically as required from JavaScript arrays to Java arrays
// but there are some edge-cases in which they might be desirable
56,7 → 49,7
* Create an array of the same class as the provided object.
* @param tplt Template for the array, must be non-null and non-Void
* @param size Size of the array to create (can be 0)
* @since {@link #interfaceRevision} 2
* @since {@link #revision} 2
*/
public <T> T[] array(final T tplt, int size) {
if (null == tplt || Void.class == tplt.getClass()) {
69,7 → 62,7
* Create an array of the provided class.
* @param c Class of the array elements, must be non-null and different from Void.class
* @param size Size of the array (can be 0)
* @since {@link #interfaceRevision} 2
* @since {@link #revision} 2
*/
public <T> T[] array(final Class<T> c, int size) {
return (T[]) Array.newInstance(c, size);
83,7 → 76,7
* <code>initValues.length</code> instead) (i.e., use 0 to
* get the same size as initValues)
* @param initValues Initial values to put in the array
* @since {@link #interfaceRevision} 2
* @since {@link #revision} 2
*/
public <T> T[] array(final T[] initValues, int size) {
if (null == initValues) {
102,7 → 95,7
/**
* Shorthand form, allows getting a Java array from a JavaScript array.
* @param initValues
* @since {@link #interfaceRevision} 2
* @since {@link #revision} 2
*/
public <T> T[] array(final T[] initValues) {
return array(initValues, initValues.length);
169,4 → 162,36
}
return sb.toString();
}
 
/**
* Apply a function to each element of an iterable collection.
* @see #for_each(Object[], Functor)
* @param c Collection to which to apply
* @param f Function to apply
* @since {@link #revision} 2
*/
public <T> void for_each(final Iterable<T> c, final Functor<Object, T> f) {
for (final T elem : c) {
f.apply(elem);
}
}
/**
* Apply a function to each element of an array
* A JavaScript function can be used transparently as a functor, e.g.:
* <br />
* <code>var arr = lang.array(new Array(1, 2, 3));<br />
* lang.for_each(arr, function(x) { err.println("Element: "+x); });</code>>
* Will produce:
* <pre>Element: 1<br />Element: 2<br />Element: 3</pre>
* @param a Array to which to apply
* @param f Function to apply
* @since {@link #revision} 2
*/
public <T> void for_each(final T[] a, final Functor<Object, T> f) {
if (null == a || null == f) {
return; // FIXME: Error handling
}
for_each(Arrays.asList(a), f);
}
}
/pluggablejs/trunk/src/net/outlyer/plugins/utils/LanguageExtensions_V1.java
1,5 → 1,11
package net.outlyer.plugins.utils;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.script.ScriptException;
import net.outlyer.plugins.SandboxAccessorImpl;
 
/*
* Copyright (c) 2008, Toni Corvera. All rights reserved.
*
26,16 → 32,6
 
// $Id$
 
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.script.ScriptException;
import net.outlyer.plugins.PluginException;
import net.outlyer.plugins.PluginReader;
import net.outlyer.plugins.Sandbox;
import net.outlyer.plugins.SandboxAccessorImpl;
 
/**
* Initial version of LanguageExtensions.
* @see LanguageExtensions
65,53 → 61,19
* Load an external file
* @deprecated Not yet stabilised
*/
public Object include(final String path) { // TODO: Support jar URIs
public Object include(final String fileName) { // TODO: Support jar URIs
try {
if (path.startsWith("file:/") || path.startsWith("jar:file:/")) {
return include(new URI(path));
}
final File f = new File(path);
if (f.isAbsolute()) {
return include(f.toURI());
}
// Get a reasonable path
final Sandbox s = getSandbox();
final URI callerUri = s.loadedFrom();
// This isn't very elegant but works (in most cases?)...
final String c = callerUri.toString();
final int idx = callerUri.toString().lastIndexOf('/');
final URI reqUri = new URI(c.substring(0, idx)+"/"+path).normalize();
return include(reqUri);
return getSandbox().getCurrentEngine().eval(new FileReader(new File(fileName)));
}
catch (final URISyntaxException e) {
e.printStackTrace();
}
return null;
}
 
public Object include(final URI uri) {
if (null == uri) {
// TODO: Handle better
return null;
}
if (null == uri.getScheme() ||
(!"file".equals(uri.getScheme()) && !"jar".equals(uri.getScheme()))) {
throw new IllegalArgumentException("Only file and jar-contained files can be included");
}
 
try {
return getSandbox().inject(new PluginReader(uri));
}
catch (final IOException e) {
catch (final FileNotFoundException e) {
// FIXME: Handle better
System.err.println("Failed to read " + uri);
System.err.println("Failed to read " + fileName);
}
catch (final PluginException e) {
catch (final ScriptException e) {
// FIXME: Handle better
System.err.println("Exception: " + e.getMessage());
}
return null;
}
 
}
/pluggablejs/trunk/src/net/outlyer/plugins/SandboxImpl.java
30,7 → 30,6
// http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/
 
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Collection;
163,6 → 162,7
if (null != prependText) {
rhino.eval(prependText);
}
if (null != pluginUri) {
result = rhino.eval(new PluginReader(pluginUri));
}
228,7 → 228,7
preCode.append("};\n");
 
final ScriptEngine rhino = execute(preCode.toString(), null, null).rhino;
 
try {
final StringBuilder retriever = new StringBuilder();
retriever.append("new ")
358,27 → 358,4
public ScriptEngine getCurrentEngine() {
return currentEngine;
}
 
/**
* {@inheritDoc}
*/
public Object inject(final Reader code) throws IllegalStateException, PluginException {
synchronized (currentEngine) { // Probably not needed
if (null == currentEngine) {
throw new IllegalStateException("Can only inject code while a sandbox is executing");
}
try {
return currentEngine.eval(code);
}
catch (final ScriptException e) {
throw new PluginException("Failed to inject code: " + e.getMessage(), e);
}
}
}
 
@Override public String toString() {
return "Sandbox("+getClass().getSimpleName()+")["+loadedFrom()+"]";
}
}
/pluggablejs/trunk/src/net/outlyer/plugins/PluginEnvironment.java
280,7 → 280,7
 
try {
// Define a Plugin object with a field named type
rhino.eval("var plugin={ type: null, apiVersion: null, name: null };");
rhino.eval("var plugin={ type: null, apiVersion: null };");
}
catch (ScriptException e) {
assert false;
301,15 → 301,18
script.getLineNumber() <= linesToCheckForSupport) {
try {
rhino.eval(line);
// XXX: Note that undefined doesn't convert to null, is this safe?
// Note that undefined doesn't convert to null!
if (null == type) {
type = rhino.eval("plugin.type;");
rhino.eval("$_1_$ = (undefined===plugin.type)?null:plugin.type;");
type = rhino.get("$_1_$");
}
if (null == apiVersion) {
apiVersion = rhino.eval("plugin.apiVersion;");
rhino.eval("$_1_$ = (undefined===plugin.apiVersion)?null:plugin.apiVersion;");
apiVersion = rhino.get("$_1_$");
}
if (null == name) {
name = rhino.eval("plugin.name;");
rhino.eval("$_1_$ = (undefined===plugin.name)?null:plugin.name;");
name = rhino.get("$_1_$");
}
}
catch (final ScriptException e) {
335,6 → 338,7
apiVersion = 0;
}
// name can be null
final PluginProperties pp = new PluginProperties();
try {
pp.apiVersion = Double.valueOf(apiVersion.toString()).intValue();
/pluggablejs/trunk/src/net/outlyer/plugins/PluginProperties.java
30,15 → 30,7
* Common properties of the plugin object. All of them
* take sane defaults.
*/
// Can't be package-protected or its fields won't be modifiable from
// js code!
// This limitation appears to apply only to fields, not methods
public class PluginProperties extends SandboxAccessorImpl {
 
// This class isn't meant to be used outside of this package, but must be
// public (see comment above)
PluginProperties(){}
class PluginProperties extends SandboxAccessorImpl {
/**
* pluggablejs Api Version against which this plugin was written.
* Default: 1
66,9 → 58,4
* @deprecated The use of this field is yet to be defined
*/
public Object properties; // FIXME: To be defined
 
@Override public String toString() {
return String.format("PluginProperties{ name=%s, type=%s, apiVersion=%d }",
name, type, apiVersion);
}
}
/pluggablejs/trunk/src/net/outlyer/plugins/Sandbox.java
34,7 → 34,7
* Public methods provided by the sandbox.
*/
// Note that all methods defined directly in this interface imply a call to {@link #execute}.
public interface Sandbox extends RuntimeHooks, SandboxProperties, Sandbox_R1 {
public interface Sandbox extends RuntimeHooks, SandboxProperties {
/**
* Creates an implementation of an interface from a JS object
* containing the interface's methods.
47,9 → 47,7
* @see #createDelayedImplementation(Class,Map)
* @see #execute()
*/
<T> T createDelayedImplementation(final Class<T> interfaceClass,
final String objectName)
throws PluginExecutionException;
<T> T createDelayedImplementation(final Class<T> interfaceClass, final String objectName) throws PluginExecutionException;
 
/**
* Creates an implementation of an interface from inline JS code.
89,7 → 87,7
<T> T createDelayedImplementation(final Class<T> interfaceClass,
final Map<Method, String> methodToCodeMap)
throws PluginExecutionException;
 
/**
* Run the script
*/
96,14 → 94,6
void execute() throws PluginExecutionException;
 
/**
* Obtains a reference to the engine currently in use, if any.
* If called during an execution it will return the current engine, from calling
* outside will return null.
* @return Current ScriptEngine
*/
ScriptEngine getCurrentEngine();
 
/**
* Obtains object containing the plugin object
* <br />
* <b>Note</b>: Implies a call to {@link #execute()}.
111,4 → 101,12
* @return Plugin object associated to this sandbox
*/
BasePluginObject getPluginObject() throws PluginExecutionException;
 
/**
* Obtains a reference to the engine currently in use, if any.
* If called during an execution it will return the current engine, from calling
* outside will return null.
* @return Current ScriptEngine
*/
ScriptEngine getCurrentEngine();
}
/pluggablejs/trunk/src/net/outlyer/plugins/PluginLocator.java
81,6 → 81,7
void add(final String fileName, final PluginProperties pp, final Sandbox sbox) {
byName.put(pp.name, sbox);
byFilename.put(fileName, sbox);
if (byType.get(pp.type) == null) {
byType.put(pp.type, new LinkedList<Sandbox>());
}
298,7 → 299,7
/**
* Scan over a directory inside a jar file
* @param jarUri Directory to scan
* @return List of plugins in directory <code>jarUri</code>
* @return LIst of plugins in directory <code>jarUri</code>
*/
private void scan(final URI jarUri) {
assert jarUri.getScheme().equals("jar");
320,11 → 321,12
if (url.startsWith(root) && !je.isDirectory()) {
// Accepted URI
 
try {
final URI pluginUri = new URI("jar:"+jarFile+"!"+url);
 
final Sandbox sb = environment.createSandbox(pluginUri);
final PluginProperties pp = PluginEnvironment.fetchPluginProperties(pluginUri);
final PluginProperties pp = PluginEnvironment.fetchPluginProperties(jarUri);
// JarEntry's getName() returns the full path
final String fileName = new File(je.getName()).getName();
if (pp.name == null) {
/pluggablejs/trunk/src/net/outlyer/plugins/PluginReader.java
36,16 → 36,13
import java.net.URI;
 
/**
* java.io.Reader for plugins, tries to abstract the plugin location (file or jar).
* Generally users of the library may only need such functionality in case
* some needs be fed to an existing sandbox (e.g.
* {@link net.outlyer.plugins.utils.LanguageExtensions_v1#include}).
* java.io.Reader for plugins, tries to abstract the plugin location (file or jar)
*/
public class PluginReader extends Reader {
class PluginReader extends Reader {
 
private final Reader readerImpl;
 
public PluginReader(final URI uri) throws IOException {
PluginReader(final URI uri) throws IOException {
super();
assert null != uri;
 
/pluggablejs/trunk/build.num
1,3 → 1,3
#Build Number for ANT. Do not edit!
#Sat Jul 05 15:58:10 CEST 2008
build.number=36
#Wed Jul 02 21:19:34 CEST 2008
build.number=34
/pluggablejs/trunk/build.xml
9,7 → 9,7
<property name="release" location="release"/>
<property name="depends" location="lib"/>
 
<property name="version" value="1.1.1"/>
<property name="version" value="1.1.0"/>
<property name="jarbasename" value="pluggablejs-${version}build" /><!-- followed by build.number, can't resolve yet -->
 
<target name="init">
/pluggablejs/trunk/debian/changelog
1,9 → 1,3
pluggablejs (1.1.1build36-upstream.0) experimental; urgency=low
 
* New release.
 
-- Toni Corvera <outlyer@gmail.com> Sat, 05 Jul 2008 15:58:16 +0200
 
pluggablejs (1.1.0build33-upstream.0) experimental; urgency=low
 
* New release.
/pluggablejs/trunk/.
Property changes:
Modified: svn:mergeinfo
Reverse-merged /pluggablejs/branches/1.1.1build36:r67