This repository has been archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
240 lines (220 loc) · 6.46 KB
/
index.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
(function QuiGonJS() {'use strict';
/*! (c) Andrea Giammarchi - ISC */
const {
assign,
create,
defineProperty,
freeze,
getOwnPropertyNames,
keys,
seal,
setPrototypeOf
} = Object;
/* istanbul ignore next */
const G = typeof(self) === 'object' ? self : global;
const casts = new Map;
const types = [];
// basic primitives, casted via constructors
// let {boolean: init} = 1;
// const {number: nums} = [0, '1.2', 3];
[
{boolean: 'Boolean'},
{number: 'Number'},
{object: 'Object'},
{string: 'String'},
]
.forEach(definition => {
const [type] = keys(definition);
const Class = G[definition[type]];
const cast = self => typeof(self) === type ? self : Class(self);
casts.set(type, cast);
defineProperty(Array.prototype, type, {get: map(cast)});
defineProperty(Object.prototype, type, {get: fn(cast)});
types.push(definition);
});
// primitives that cannot be casted (throws TypeError)
// const {symbol: iterator} = Symbol.iterator;
[
{symbol: 'Symbol'},
]
.forEach(definition => {
const [type] = keys(definition);
const name = definition[type];
/* istanbul ignore else */
if (typeof(G[name]) === 'function') {
const cast = self => {
if (typeof(self) === type)
return self;
throw new TypeError(String(self) + ' is not a ' + name);
};
casts.set(type, cast);
defineProperty(Array.prototype, type, {get: map(cast)});
defineProperty(Object.prototype, type, {get: fn(cast)});
types.push(definition);
}
});
// Rust like primitives (plus uc8) casted via value assignment
// const {f32: coords} = [lat, long];
[
{f: 'Float32Array'},
{i: 'Int32Array'},
{u: 'Uint32Array'},
{f32: 'Float32Array'},
{f64: 'Float64Array'},
{i8: 'Int8Array'},
{i16: 'Int16Array'},
{i32: 'Int32Array'},
{u8: 'Uint8Array'},
{u16: 'Uint16Array'},
{u32: 'Uint32Array'},
// extra Qui-Gon Jinn Script type
{uc8: 'Uint8ClampedArray'},
]
.forEach(definition => {
const [type] = keys(definition);
const Class = G[definition[type]];
/* istanbul ignore else */
if (typeof(Class) === 'function') {
const reference = new Class(1);
const cast = self => ((reference[0] = self), reference[0]);
casts.set(type, cast);
defineProperty(Array.prototype, type, {
get() { return new Class(this); }
});
defineProperty(Object.prototype, type, {get: fn(cast)});
types.push(definition);
}
});
// still Rust like values with different casting
[
{i64: 'BigInt64Array'},
{u64: 'BigUint64Array'},
]
.forEach(definition => {
const [type] = keys(definition);
const Class = definition[type];
const cast = G[Class.slice(0, Class.indexOf('64'))];
if (typeof(cast) === 'function' && typeof(G[Class]) === 'function') {
casts.set(type, cast);
defineProperty(Array.prototype, type, {get: map(cast)});
defineProperty(Object.prototype, type, {get: fn(cast)});
types.push(definition);
}
});
// arrays (aka Rust mutable tuple)
// const {array: numbers} = [1, 2, '3', 4];
[
{array: 'Array'}
]
.forEach(definition => {
const [type] = keys(definition);
const Class = G[definition[type]];
const cast = self => Class.from(self);
casts.set(type, cast);
defineProperty(Array.prototype, type, {get() { return cast(this); }});
defineProperty(Object.prototype, type, {get: fn(cast)});
types.push(definition);
});
// struct (simple Rust structs)
// const {struct: Point} = [{i32: 'x'}, {i32: 'y'}];
defineProperty(Array.prototype, 'struct', {get() {
const descriptors = create(null);
const {prototype} = Struct;
this.forEach(definition => {
const [type] = keys(definition);
const name = definition[type];
const cast = getCast(type);
const _ = new WeakMap;
descriptors[name] = {
enumerable: true,
get() { return _.get(this); },
set(value) { _.set(this, cast(value)); }
};
});
freeze(prototype);
return freeze(Struct);
function Struct(self) {
return seal(assign(create(prototype, descriptors), self));
}
}});
// registered struct (simple Rust structs)
// const {struct: Point} = {Point: [{f32: 'x'}, {f32: 'y'}]};
// const {Point: p} = Point(0, 0);
defineProperty(Object.prototype, 'struct', {get() {
const [type] = keys(this);
register(type);
return this[type].struct;
}});
// const {enum: WebEvent} = [
// 'Loading',
// 'Loaded',
// {Click: [{i64: 'x'}, {i64: 'y'}] }}
// ];
defineProperty(Array.prototype, 'enum', {get() {
const Enum = create(null);
this.forEach(definition => {
if (typeof definition === 'string') {
Enum[definition] = Symbol(definition);
} else {
const [name] = keys(definition);
const assignments = [];
definition[name].forEach(definition => {
const [type] = keys(definition);
const name = definition[type];
const cast = getCast(type);
assignments.push({name, cast});
});
const fn = (self) => {
assignments.forEach(definition => {
const {name, cast} = definition;
fn[name] = cast(self[name]);
});
return fn;
};
setPrototypeOf(fn, null);
getOwnPropertyNames(fn).forEach(name => {
delete fn[name];
});
Enum[name] = fn;
}
});
return freeze(Enum);
}});
// registered enum (simple Rust enums)
// const {enum: Color} = {Color: ['Red', 'Green', 'Blue']};
// console.log(Color.Red);
defineProperty(Object.prototype, 'enum', {get() {
const [type] = keys(this);
register(type);
return this[type].enum;
}});
defineProperty(G, 'types', {value: types.map(type => keys(type)[0])});
function fn(cast) {
return function () {
return typeof(this) === 'function' ?
wrapFn(cast, this) :
cast(this);
};
}
function getCast(type) {
if (!casts.has(type))
throw new TypeError('unknown type ' + type);
return casts.get(type);
}
function map(cast) {
return function () {
return this.map(cast);
};
}
function register(type) {
if (!types.includes(type)) {
types.push(type);
defineProperty(Object.prototype, type, {get() { return this; }});
}
}
function wrapFn(cast, fn) {
return function () {
return cast(fn.apply(this, arguments));
};
}
}());