Closed Bug 266418 Opened 20 years ago Closed 20 years ago

Can not serialize regular expressions

Categories

(Rhino Graveyard :: Core, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: szegedia, Assigned: igor)

Details

Attachments

(1 file)

User-Agent:       Opera/7.54 (Windows NT 5.0; U)  [en]
Build Identifier: 

An attempt to serialize a regular expression (i.e. as part of a continuation) 
will fail since the classes org.mozilla.javascript.regexp.RECompiled and org.
mozilla.javascript.regexp.RECharSet (defined in file NativeRegExp.java) are not 
serializable.

Reproducible: Always
Steps to Reproduce:
Execute the following snippet of Java code:

Context cx = Context.enter();
ScriptableObject s1 = new NativeObject();
cx.initStandardObjects(s1);
cx.compileString("var x = /^\\s*(.*)/;", "", 1, null).exec(cx, s1);
new ScriptableOutputStream(new ByteArrayOutputStream(), s1).writeObject(s1.
get("x", s1));
Actual Results:  
The following exception was thrown:

java.io.NotSerializableException: org.mozilla.javascript.regexp.RECompiled
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1054)
	at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1332)
	at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1304)
	at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1247)
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1052)
	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
	at Test.main(Test.java:31)

Expected Results:  
silently succeed
Can somebody reassure me that regexp literals occurring in a script using the /.
../ syntax are stored inside inside InterpretedData, so if I'm taking care to 
stub InterpretedData instances in serialization, they won't get serialized at 
all, and this problem only arises if a regexp is dinamically constructed by a 
script. Right?
The patch adds "impelments Serializable" to classes representing compiled
regexps and ads proper synchronization for code to initialize RECharSet lazily.
(In reply to comment #1)
> Can somebody reassure me that regexp literals occurring in a script using the /.
> ../ syntax are stored inside inside InterpretedData, so if I'm taking care to 
> stub InterpretedData instances in serialization, they won't get serialized at 
> all, and this problem only arises if a regexp is dinamically constructed by a 
> script. Right?

Although its true that compiled scripts are stored in the InterpreterData, they
still will be serialized even if InterpreterData would be stubbed.

InterpreterFunction during initialization wraps the compiled regexps as
NativeRegExp to store them in InterpreterFunction.functionRegExps and that
forces serialization.

I committed the fix.
Status: NEW → RESOLVED
Closed: 20 years ago
Resolution: --- → FIXED
Target Milestone: --- → 1.6R1
(In reply to comment #3)

Would it be possible to add some kind of public accessor to InterpeterData.
itsRegExpLiterals, either through the DebuggableScript interface or some other 
way? That way I could access its elements to create stubs for them too.
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: