-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModuleReader.js
More file actions
141 lines (118 loc) · 3.47 KB
/
Copy pathModuleReader.js
File metadata and controls
141 lines (118 loc) · 3.47 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
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
import LMRObjectHeader from './interpreter/LMRObjectHeader.js';
import LMRSlotObject from './interpreter/LMRSlotObject.js';
import LMRByteObject from './interpreter/LMRByteObject.js';
import LMRSmallInteger from './interpreter/LMRSmallInteger.js';
import { readFileSync } from 'fs';
export const ObjectTypes = Object.freeze({
SlotObject: 1,
ByteObject: 2,
SmallIntegerObject: 3,
Import: 4
})
/**
* @param {*} environment is a dictionary mapping names to objects, that is used for module imports
*
* A ModuleReader is an object that creates a Powertalk module by reading it from a JSON file.
* The format of the module is pretty simple: it has an object table, and an export map.
* Objects in the table can be of four types: SmallInteger, ByteObject, SlotObject and Import
* - Import objects have a name, which is used to find the corresponding object passed in
* the environment dictionary.
* - Small integers are immediates
* - Byte objects have bytes, hash and behavior
* - Slot objects have slots, hash and behavior.
* Both slots and behaviors are stored as indexes in the object table.
* The reader generates all object instances in a first pass, and then changes references to
* other objects by accessing the corresponding index in the object table.
*/
const ModuleReader = class {
constructor(environment = {})
{
this.environment = environment;
}
loadFile(path) {
let rawdata = readFileSync(path);
let module = JSON.parse(rawdata);
this.loadObjects(module);
return this;
}
loadObjects(module) {
this.objects = module.objects.map(obj => this.recreate(obj));
this.objects.forEach(obj => this.linkSlots(obj));
this.exports = Object.fromEntries(module.exports.map( exp => [exp[0], this.objects[exp[1]]]));
return this.objects;
}
objectNamed(name)
{
return this.exports[name];
}
recreate(object) {
switch (this.objectType(object)) {
case ObjectTypes.SlotObject:
return this.newSlotObject(object);
case ObjectTypes.ByteObject:
return this.newByteObject(object);
case ObjectTypes.SmallIntegerObject:
return this.newSmallInteger(object);
case ObjectTypes.Import:
return this.environment[object.name];
default:
throw "unknown object type";
}
}
newSlotObject(object)
{
let result = new LMRSlotObject();
result._slots = this.objectSlots(object);
result._header = new LMRObjectHeader();
result._header._behavior = this.objectBehavior(object);
result._header._hash = this.objectHash(object);
return result;
}
newByteObject(object)
{
let result = new LMRByteObject();
result._bytes = this.objectBytes(object);
result._header = new LMRObjectHeader();
result._header._behavior = this.objectBehavior(object);
result._header._hash = this.objectHash(object);
return result;
}
newSmallInteger(object)
{
let result = new LMRSmallInteger();
result._value = object[1];
return result;
}
linkSlots(object) {
if (object.isImmediate())
return;
object._header._behavior = this.objects[object._header._behavior];
if (!object.isBytes())
object._slots = object._slots.map(index => this.objects[index]);
}
objectType(object)
{
return object[0];
}
objectSlots(object)
{
return object[1];
}
objectBytes(object)
{
return object[1];
}
objectHeader(object)
{
return object[2];
}
objectBehavior(object)
{
return this.objectHeader(object)[0];
}
objectHash(object)
{
return this.objectHeader(object)[1];
}
}
export default ModuleReader