forked from denghongcai/pickle-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpickle.js
275 lines (246 loc) · 8.48 KB
/
pickle.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
Copyright (c) 2008 Frank Salim
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
* pickle.js
* a JavaScript module for parsing and serializing Python objects
*/
var pickle = (new function() {
var self = this
/* Opcodes
* !.. not all opcodes are currently supported!
* ... 1.0 should support all opcodes in pickle v0 (text)
*/
var MARK = "("
var STOP = "."
var INT = "I"
var FLOAT = "F"
var NONE = "N"
var STRING = "S"
var APPEND = "a"
var DICT = "d"
var GET = "g"
var LIST = "l"
var PUT = "p"
var SETITEM = "s"
var TUPLE = "t"
var TRUE = "I01\n"
var FALSE = "I00\n"
/* Other magic constants that are not opcodes
*/
NEWLINE = "\n"
function MARK_OBJECT(){}
SQUO = "'"
/* Prototypes for Tuple and Dict objects
*/
var Tuple = function() {}
Tuple.prototype = new Array()
Tuple.prototype.isTuple = true
var Dict = function() {}
Dict.prototype = new Object()
Dict.prototype.isDict = true
/*
* loads(string) -> object
* load a pickled Python object from a string
*/
self.loads = function(pickle) {
var stack = []
var memo = []
var ops = pickle.split(NEWLINE)
var op
for (var i=0; i<ops.length; i++) {
op = ops[i]
process_op(op, memo, stack)
}
return stack.pop()
}
// to speed up op processing, declare two args here
var arg0
var arg1
var arg2
var process_op = function(op, memo, stack) {
if (op.length === 0)
return
switch (op[0]) {
case MARK:
stack.push(MARK_OBJECT)
process_op(op.slice(1), memo, stack)
break
case STOP:
//console.log("stop")
break
case INT:
// booleans are a special case of integers
if (op[1] === "0") {
arg0 = (op[2] === "1")
stack.push(arg0)
break
}
arg0 = parseInt(op.slice(1))
//console.log("int", arg0)
stack.push(arg0)
break
case FLOAT:
arg0 = parseFloat(op.slice(1))
//console.log("int", arg0)
stack.push(arg0)
break
case STRING:
arg0 = eval(op.slice(1))
stack.push(arg0)
//console.log("string", arg0)
break
case NONE:
stack.push(null)
process_op(op.slice(1), memo, stack)
break
case APPEND:
arg0 = stack.pop()
//console.log("appending to", stack[stack.length-1])
stack[stack.length-1].push(arg0)
process_op(op.slice(1), memo, stack)
break
case DICT:
arg0 = new Dict()
while( stack[stack.length-1] != MARK_OBJECT )
{
arg2 = stack.pop()
arg1 = stack.pop()
arg0[arg1] = arg2
}
stack.pop()
stack.push(arg0)
process_op(op.slice(1), memo, stack)
break
case GET:
arg0 = parseInt(op.slice(-1))
arg1 = memo[arg0]
stack.push(arg1)
//console.log("getting", arg1)
break
case LIST:
arg0 = []
arg1 = stack.pop()
while( arg1 != MARK_OBJECT )
{
arg0.push(arg1)
arg1 = stack.pop()
}
stack.push(arg0)
process_op(op.slice(1), memo, stack)
break
case PUT:
arg0 = parseInt(op.slice(1))
arg1 = stack[stack.length-1]
memo[arg0] = arg1
//console.log("memo", arg0, arg1)
break
case SETITEM:
arg1 = stack.pop()
arg0 = stack.pop()
stack[stack.length-1][arg0] = arg1
//console.log("current before set", stack)
process_op(op.slice(1), memo, stack)
break
case TUPLE:
arg0 = new Tuple()
arg1 = undefined
do {
if ( arg1 !== undefined )
arg0.unshift(arg1)
arg1 = stack.pop()
} while( arg1 != MARK_OBJECT );
stack.push(arg0)
process_op(op.slice(1), memo, stack)
break
default:
throw new Error("unknown opcode " + op[0])
}
}
/*
* dumps(object) -> string
* serializes a JavaScript object to a Python pickle
*/
self.dumps = function(obj) {
// pickles always end with a stop
return _dumps(obj) + STOP
}
var _check_memo = function(obj, memo) {
for (var i=0; i<memo.length; i++) {
if (memo[i] === obj) {
return i
}
}
return -1
}
var _dumps = function(obj, memo) {
memo = memo || []
if (obj === null) {
return NONE
}
if (typeof(obj) === "object") {
var p = _check_memo(obj, memo)
if (p !== -1) {
return GET + p + NEWLINE
}
var t = obj.constructor.name
switch (t) {
case Array().constructor.name:
var s = MARK + LIST + PUT + memo.length + NEWLINE
memo.push(obj)
for (var i=0; i<obj.length; i++) {
s += _dumps(obj[i], memo) + APPEND
}
return s
break
case Object().constructor.name:
var s = MARK + DICT + PUT + memo.length + NEWLINE
memo.push(obj)
for (var key in obj) {
//console.log(key)
//push the value, then the key, then 'set'
s += _dumps(obj[key], memo)
s += _dumps(key, memo)
s += SETITEM
}
return s
break
default:
throw new Error("Cannot pickle this object: " + t)
}
} else if (typeof(obj) === "string") {
var p = _check_memo(obj, memo)
if (p !== -1) {
return GET + p + NEWLINE
}
var escaped = obj.replace("\\","\\\\","g")
.replace("'", "\\'", "g")
.replace("\n", "\\n", "g")
var s = STRING + SQUO + escaped + SQUO + NEWLINE
+ PUT + memo.length + NEWLINE
memo.push(obj)
return s
} else if (typeof(obj) === "number") {
return FLOAT + obj + NEWLINE
} else if (typeof(obj) === "boolean") {
return obj ? TRUE : FALSE
} else {
throw new Error("Cannot pickle this type: " + typeof(obj))
}
}
}())