@@ -5,11 +5,17 @@ import { Registry } from './registry';
5
5
6
6
export class Parser {
7
7
private _reader : Reader ;
8
+ public persistent_load : ( pid : string ) => any ;
8
9
9
10
registry : Registry = new Registry ( ) ;
10
11
11
- constructor ( buffer : Uint8Array | Int8Array | Uint8ClampedArray ) {
12
+ constructor ( buffer : Uint8Array | Int8Array | Uint8ClampedArray , persistent_load ?: ( pid : string ) => any ) {
12
13
this . _reader = new Reader ( buffer ) ;
14
+ this . persistent_load =
15
+ persistent_load ||
16
+ ( ( pid : string ) => {
17
+ throw new Error ( `Unsupported persistent id: \`${ pid } \`.` ) ;
18
+ } ) ;
13
19
}
14
20
15
21
load ( ) {
@@ -20,6 +26,8 @@ export class Parser {
20
26
while ( reader . hasNext ( ) ) {
21
27
const opcode = reader . byte ( ) ;
22
28
// console.log(`${(reader.position - 1).toString()} ${opcode}`);
29
+ // console.log('metastack:', metastack, '\nstack:', stack);
30
+ // console.log('\nmemo:', Array.from(memo.entries()));
23
31
switch ( opcode ) {
24
32
// Structural
25
33
case OP . PROTO : {
@@ -296,6 +304,12 @@ export class Parser {
296
304
stack . push ( obj ) ;
297
305
break ;
298
306
}
307
+ case OP . PERSID :
308
+ stack . push ( this . persistent_load ( reader . line ( ) ) ) ;
309
+ break ;
310
+ case OP . BINPERSID :
311
+ stack . push ( this . persistent_load ( stack . pop ( ) ) ) ;
312
+ break ;
299
313
case OP . BUILD : {
300
314
const state = stack . pop ( ) ;
301
315
const obj = stack [ stack . length - 1 ] ;
@@ -318,8 +332,6 @@ export class Parser {
318
332
}
319
333
320
334
default :
321
- // console.log('metastack:', metastack, '\nstack:', stack);
322
- // console.log('\nmemo:', Array.from(memo.entries()));
323
335
throw new Error ( `Unsupported opcode '${ opcode } '.` ) ;
324
336
}
325
337
}
0 commit comments