-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReader.java
More file actions
86 lines (76 loc) · 2.12 KB
/
Reader.java
File metadata and controls
86 lines (76 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import org.graalvm.polyglot.*;
import java.io.*;
import java.util.*;
public class Reader {
private DataInputStream in;
private Context c;
public Map<Integer, Value> seenCache = new HashMap<Integer, Value>();
public Reader(InputStream in, Context c) {
this.in = new DataInputStream(in);
this.c = c;
}
public Value read() throws Exception {
seenCache = new HashMap<Integer, Value>();
return readHelper();
}
public Value readHelper() throws Exception {
Value out = null;
//check if seen before or known type
int seenindex = in.readInt();
short typeindex = in.readShort();
if(typeindex < TypesDB.fromInt.length) {
switch(TypesDB.fromInt[typeindex]) {
case BOOLEAN:
//System.out.println("found boolean");
out = c.asValue(in.readBoolean());
break;
case NATIVE_POINTER:
throw new IllegalArgumentException("don't handle native pointers yet");
case NULL:
//System.out.println("found null");
out = c.asValue(null);
break;
case NUMBER:
//System.out.println("found number");
out = c.asValue(in.readInt());
break;
case STRING:
//System.out.println("found string");
out = c.asValue(in.readUTF());
break;
case SEEN:
//System.out.println("found cached value");
return seenCache.get(seenindex);
default:
throw new IllegalArgumentException("we should never get here");
}
} else { //Complex
out = TypesDB.newInstance(typeindex, c);
}
seenCache.put(seenindex, out);
//check array data
long arraylength = in.readLong();
if(arraylength != 0) {
//System.out.println("array of length " + arraylength);
}
for(int i = 0; i < arraylength; i++) {
out.setArrayElement(i, readHelper());
}
//if we have a type, check named members
for(String s : TypesDB.getMembers(typeindex)) {
//System.out.println("now reading in " + s);
if(s.equals("__proto__")) { //TODO: for some reason can't serialize js inheritance chain
continue;
}
Value member = readHelper();
if(member != null) {
out.putMember(s, member);
}
}
return out;
}
public void close() throws Exception {
in.close();
in = null;
}
}