Add support for serialization and deserialization.
This commit is contained in:
Родитель
32219dfb38
Коммит
89195e2df6
|
@ -13,7 +13,9 @@ apiClasses=\
|
|||
src/org/mozilla/javascript/PropertyException.java,\
|
||||
src/org/mozilla/javascript/Script.java,\
|
||||
src/org/mozilla/javascript/Scriptable.java,\
|
||||
src/org/mozilla/javascript/ScriptableInputStream.java,\
|
||||
src/org/mozilla/javascript/ScriptableObject.java,\
|
||||
src/org/mozilla/javascript/ScriptableOutputStream.java,\
|
||||
src/org/mozilla/javascript/SecuritySupport.java,\
|
||||
src/org/mozilla/javascript/WrapHandler.java,\
|
||||
src/org/mozilla/javascript/Wrapper.java,\
|
||||
|
|
|
@ -49,6 +49,8 @@ import java.lang.reflect.InvocationTargetException;
|
|||
|
||||
public class FunctionObject extends NativeFunction {
|
||||
|
||||
static final long serialVersionUID = -4074285335521944312L;
|
||||
|
||||
/**
|
||||
* Create a JavaScript function object from a Java method.
|
||||
*
|
||||
|
@ -515,7 +517,7 @@ public class FunctionObject extends NativeFunction {
|
|||
}
|
||||
try {
|
||||
return invoker.invoke(thisObj, args);
|
||||
} catch (RuntimeException e) {
|
||||
} catch (Exception e) {
|
||||
throw new InvocationTargetException(e);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,10 +35,14 @@
|
|||
|
||||
package org.mozilla.javascript;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.Serializable;
|
||||
import org.mozilla.javascript.debug.DebuggableScript;
|
||||
|
||||
class InterpretedFunction extends NativeFunction implements DebuggableScript {
|
||||
class InterpretedFunction extends NativeFunction
|
||||
implements DebuggableScript, Serializable
|
||||
{
|
||||
|
||||
static final long serialVersionUID = -6235150451107527319L;
|
||||
|
||||
InterpretedFunction(Context cx,
|
||||
InterpreterData theData,
|
||||
|
@ -122,4 +126,4 @@ class InterpretedFunction extends NativeFunction implements DebuggableScript {
|
|||
InterpreterData itsData;
|
||||
Scriptable itsClosure;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -36,9 +36,11 @@
|
|||
|
||||
package org.mozilla.javascript;
|
||||
|
||||
import java.util.Vector;
|
||||
import java.io.Serializable;
|
||||
|
||||
class InterpreterData {
|
||||
class InterpreterData implements Serializable {
|
||||
|
||||
static final long serialVersionUID = 4815333329084415557L;
|
||||
|
||||
static final int INITIAL_MAX_ICODE_LENGTH = 1024;
|
||||
static final int INITIAL_STRINGTABLE_SIZE = 64;
|
||||
|
|
|
@ -41,6 +41,7 @@ package org.mozilla.javascript;
|
|||
|
||||
import java.lang.reflect.*;
|
||||
import java.util.Hashtable;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* This is the default implementation of the Scriptable interface. This
|
||||
|
@ -56,7 +57,9 @@ import java.util.Hashtable;
|
|||
* @author Norris Boyd
|
||||
*/
|
||||
|
||||
public abstract class ScriptableObject implements Scriptable {
|
||||
public abstract class ScriptableObject implements Scriptable, Serializable {
|
||||
|
||||
static final long serialVersionUID = 2762574228534679611L;
|
||||
|
||||
/**
|
||||
* The empty property attribute.
|
||||
|
@ -1766,6 +1769,12 @@ public abstract class ScriptableObject implements Scriptable {
|
|||
return result;
|
||||
}
|
||||
|
||||
private void readObject(ObjectInputStream in)
|
||||
throws IOException, ClassNotFoundException
|
||||
{
|
||||
in.defaultReadObject();
|
||||
lastAccess = REMOVED;
|
||||
}
|
||||
|
||||
/**
|
||||
* The prototype of this object.
|
||||
|
@ -1785,9 +1794,9 @@ public abstract class ScriptableObject implements Scriptable {
|
|||
private int count;
|
||||
|
||||
// cache; may be removed for smaller memory footprint
|
||||
private Slot lastAccess = REMOVED;
|
||||
transient private Slot lastAccess = REMOVED;
|
||||
|
||||
private static class Slot {
|
||||
private static class Slot implements Serializable {
|
||||
static final int HAS_GETTER = 0x01;
|
||||
static final int HAS_SETTER = 0x02;
|
||||
|
||||
|
|
|
@ -35,6 +35,8 @@
|
|||
|
||||
package org.mozilla.javascript;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Map to associate non-negative integers to objects or integers.
|
||||
* The map does not synchronize any of its operation, so either use
|
||||
|
@ -45,7 +47,9 @@ package org.mozilla.javascript;
|
|||
*
|
||||
*/
|
||||
|
||||
class UintMap {
|
||||
class UintMap implements Serializable {
|
||||
|
||||
static final long serialVersionUID = -55740507849272970L;
|
||||
|
||||
// Map implementation via hashtable,
|
||||
// follows "The Art of Computer Programming" by Donald E. Knuth
|
||||
|
|
|
@ -37,8 +37,10 @@
|
|||
|
||||
package org.mozilla.javascript.regexp;
|
||||
|
||||
import org.mozilla.javascript.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.mozilla.javascript.*;
|
||||
|
||||
/**
|
||||
* This class implements the RegExp native object.
|
||||
|
@ -2239,7 +2241,10 @@ class CompilerState {
|
|||
}
|
||||
|
||||
|
||||
class RENode {
|
||||
class RENode implements Serializable {
|
||||
|
||||
static final long serialVersionUID = -5896495686381169903L;
|
||||
|
||||
public static final int ANCHORED = 0x01; /* anchored at the front */
|
||||
public static final int SINGLE = 0x02; /* matches a single char */
|
||||
public static final int NONEMPTY = 0x04; /* does not match empty string */
|
||||
|
|
|
@ -58,7 +58,8 @@ public class Global extends ImporterTopLevel {
|
|||
// that these functions are not part of ECMA.
|
||||
super(cx);
|
||||
String[] names = { "print", "quit", "version", "load", "help",
|
||||
"loadClass", "defineClass", "spawn", "sync" };
|
||||
"loadClass", "defineClass", "spawn", "sync",
|
||||
"serialize", "deserialize" };
|
||||
try {
|
||||
defineFunctionProperties(names, Global.class,
|
||||
ScriptableObject.DONTENUM);
|
||||
|
@ -240,6 +241,41 @@ public class Global extends ImporterTopLevel {
|
|||
}
|
||||
}
|
||||
|
||||
public static void serialize(Context cx, Scriptable thisObj,
|
||||
Object[] args, Function funObj)
|
||||
throws IOException
|
||||
{
|
||||
if (args.length < 2) {
|
||||
throw Context.reportRuntimeError(
|
||||
"Expected an object to serialize and a filename to write " +
|
||||
"the serialization to");
|
||||
}
|
||||
Object obj = args[0];
|
||||
String filename = cx.toString(args[1]);
|
||||
FileOutputStream fos = new FileOutputStream(filename);
|
||||
Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
|
||||
ScriptableOutputStream out = new ScriptableOutputStream(fos, scope);
|
||||
out.writeObject(obj);
|
||||
out.close();
|
||||
}
|
||||
|
||||
public static Object deserialize(Context cx, Scriptable thisObj,
|
||||
Object[] args, Function funObj)
|
||||
throws IOException, ClassNotFoundException
|
||||
{
|
||||
if (args.length < 1) {
|
||||
throw Context.reportRuntimeError(
|
||||
"Expected a filename to read the serialization from");
|
||||
}
|
||||
String filename = cx.toString(args[0]);
|
||||
FileInputStream fis = new FileInputStream(filename);
|
||||
Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
|
||||
ObjectInputStream in = new ScriptableInputStream(fis, scope);
|
||||
Object deserialized = in.readObject();
|
||||
in.close();
|
||||
return cx.toObject(deserialized, scope);
|
||||
}
|
||||
|
||||
/**
|
||||
* The spawn function runs a given function or script in a different
|
||||
* thread.
|
||||
|
|
Загрузка…
Ссылка в новой задаче