-
Notifications
You must be signed in to change notification settings - Fork 0
/
winapijson.js
598 lines (553 loc) · 19.3 KB
/
winapijson.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
function callListeners(target,key,value){
if (key in target._callbacks) {
target._callbacks[key].forEach(callback => callback(value));
delete target._callbacks[key];
}
}
function lazySet(object, property, someExpensiveComputation) {
Object.defineProperty(object, property, {
get() {
const actualData = someExpensiveComputation();
//callListeners(this,property,actualData);
Object.defineProperty(this, property, {
value: actualData,
writable: true,
configurable: true
});
return actualData;
},
configurable: true,
enumerable: true
});
}
var groupby = require("lodash.groupby")
var _get = require("lodash.get")
var _set = require("lodash.set")
var ffi = require('ffi-napi');
var ref = require('ref-napi');
var Struct = require('ref-struct-di')(ref)
var Union = require('ref-union-di')(ref);
var ArrayB = require('ref-array-di')(ref);
var assert = require('node:assert');
var path = require('node:path');
var fs = require('node:fs');
var util = require('node:util');
//Should this be optional or something?
Buffer.prototype._toJSON=Buffer.prototype.toJSON
Buffer.prototype.toJSON=function toJSON(){
var obj=this._toJSON();
var size=this?.type?.size;
var indirection=this.type.indirection;
var type=this?.type?.name;
var address=this.address();
return {...obj,size,indirection,type,address};
}
const isObject = (input) => (
null !== input && typeof input === 'object' && Object.getPrototypeOf(input).isPrototypeOf(Object)
)
/**
* @param {object} obj
* @param {string} path
* @param {any} value
*/
const setByString = (obj, path, value) => {
const pList = Array.isArray(path) ? path : path.split('.');
const len = pList.length;
// changes second last key to {}
for (let i = 0; i < len - 1; i++) {
const elem = pList[i];
if (!obj[elem] || !isObject(obj[elem])) {
obj[elem] = {};
}
obj = obj[elem];
}
// set value to second last key
obj[pList[len - 1]] = value;
};
function flattenObject(o, prefix = '', result = {}, keepNull = true) {
if (typeof o!=="object" || (keepNull && o==null)) {
result[prefix] = o;
return result;
}
for (let i in o) {
let pref = prefix;
if (Array.isArray(o)) {
pref = pref + `[${i}]`;
} else {
if (Object.keys(prefix).length === 0) {
pref = i;
} else {
pref = prefix + '.' + i;
}
}
flattenObject(o[i], pref, result, keepNull);
}
return result;
}
function StructType(){
function toJSONrec(){
var that=this;
while(that.toJSON==toJSONrec){//find previous JSON definition :)
that=that.__proto__;
}
//assert(this.__proto__.__proto__.toJSON!=toJSONrec,"this shouldn't be the same...")
return Object.entries(that.toJSON.call(this)).map(([k,v])=>[k,(v.toJSON)?v.toJSON():v])
}
//if(b)b.packed=true; else b={packed:true};
var obj=Struct(...arguments);
var someproto=Object.create(Object.getPrototypeOf(obj.prototype))
someproto.toJSON=toJSONrec;
someproto.fromObject=function(d){
//var l=this;
Object.entries(flattenObject(d)).forEach(([k,v])=>setByString(this,k,v))
}
Object.setPrototypeOf(obj.prototype,someproto)
obj.defineProperties=(_=>Object.entries(_).forEach(([k,v])=>obj.defineProperty(k,v)));
return obj;
}
function ArrayType(){
function toJSONrec(){
return this._toJSON().map(_=>_.toJSON?_.toJSON():_);
}
var obj=ArrayB(...arguments);
obj.prototype._toJSON=obj.prototype.toJSON
obj.prototype.toJSON=toJSONrec;
return obj;
}
var wchar_size = process.platform == 'win32' ? 2 : 4;
ref.types.wchar_t = ref.types.ushort;
ref.types.WCString = Object.create(ref.types.CString)
ref.types.WCString.get = function get (buf, offset) {
var _buf = buf.readPointer(offset)
if (_buf.isNull()) {
return null
}
var stringBuf = _buf.reinterpretUntilZeros(wchar_size)
return stringBuf.toString('utf16le') // TODO: decode UTF-32 on Unix
};
ref.types.WCString.set = function set (buf, offset, val) {
// TODO: better UTF-16 and UTF-32 encoding
var _buf = new Buffer((val.length + 1) * wchar_size)
_buf.fill(0)
var l = 0
for (var i = wchar_size - 1; i < _buf.length; i += wchar_size) {
_buf[i] = val.charCodeAt(l++)
}
return buf.writePointer(_buf, offset);
};
var ntvhlprfun=({size,indirection,alignment,ffi_type})=>({size,indirection,alignment,ffi_type});
var nativehelper = new Proxy({
Byte: {
typeobj: (ntvhlprfun)(ref.types.uint8),
writebuf: "writeUint8",
readbuf: "readUint8",
},SByte: {
typeobj: (ntvhlprfun)(ref.types.int8),
writebuf: "writeInt8",
readbuf: "readInt8",
},Single: {
typeobj: (ntvhlprfun)(ref.types.float),
writebuf: "writeFloatLE",
readbuf: "readFloatLE",
},Boolean: {
typeobj: (ntvhlprfun)(ref.types.uint8),
writebuf: "writeUint8",
readbuf: "readUint8",
}
}, {
get: (target, prop) => {
if (prop in target) {
return target[prop];
} else {
const propLowerCase = prop.toLowerCase();
const typeobjExists = propLowerCase in ref.types;
if (typeobjExists) {
const typeobj = (ntvhlprfun)(ref.types[propLowerCase]);
const writebuf = `write${prop}LE`;
const readbuf = `read${prop}LE`;
if (Buffer.prototype[writebuf] && Buffer.prototype[readbuf]) {
target[prop] = { typeobj, writebuf, readbuf };
return target[prop];
}
}
}
},
});
function longeSTring(a, b) {
return a.length > b.length ? a : b;
}
//var eventEmitter = new events.EventEmitter();
var wintypes = new Proxy({}, {
defineProperty: function (target, key, descriptor) {
if ("value" in descriptor) {
if (!target.hasOwnProperty("_callbacks")) {
target._callbacks = {};
}
callListeners(target,key,descriptor.value)
}
return Object.defineProperty(target, key, descriptor);
},
set(target, property, value, receiver) {
if (!(property in target)) {
if (!target.hasOwnProperty("_callbacks")) {
target._callbacks = {};
}
callListeners(target,property,value);
}
Reflect.set(target, property, value, receiver);
return true;
},
get: function(target, name) {
var arr, lname;
if (name in target) return target[name];
else if (name.toLowerCase() in ref.types)
return target[name] = ref.types[name.toLowerCase()]
else if (name in nativehelper){
return target[name] = nativehelper[name].typeobj;
}
else if ((arr = name.match(/^[LNS]?P(C?(U?(N?(Z?(Z?(.*)|.*)|.*)|.*)|.*)|.*)/)?.slice(1).filter(_ => this.has(target, _)))?.length | 0 > 0) {
if ((lname = arr.reduce(longeSTring)) == "STR") {
return target[name] = ref.types.CString;
} else
return target[name] = ref.refType(this.get(target, lname));
} //else if (name[0] == "H") return target[name] = ref.types.uint64;
},
has: function(target, name) {
if (name in target ||
name.toLowerCase() in ref.types ||
name.match(/^[LNS]?P(C?(U?(N?(Z?(Z?(.*)|.*)|.*)|.*)|.*)|.*)/)?.slice(1).filter(_ => this.has(target, _))?.length | 0 > 0 ||
//name[0] == "H" ||
name == "STR") return true;
else return false;
}
});
wintypes.attr = function (property) {
return new Promise((resolve, reject) => {
if (property in wintypes) {
resolve(wintypes[property]);
} else {
if (!wintypes._callbacks) {
wintypes._callbacks = {};
}
if (!wintypes._callbacks[property]) {
wintypes._callbacks[property] = [];
}
wintypes._callbacks[property].push(value => {
resolve(value);
});
}
});
};
function methodProxy(obj1, property1, path) {
Object.defineProperty(obj1, property1, {
get() {
//console.log("just so I can get this right, this is ",this,path)
return _get(this, path);
},
set(value) {
_set(this, path, value);
},
configurable: true,
enumerable: true
});
}
var CFTypeConstructor = function(rtype, args) {
return {
size: ref.sizeof.pointer,
alignment: ref.alignof.pointer,
rtype,
name: `(${args.map(a=>a.name).join(',')}) -> ${rtype.name}`,
ffi_type: ffi.FFI_TYPES.pointer,
args,
indirection: 1,
get: function get(buf, offset) {
const _buf = ref.readPointer(buf, offset)
if (ref.isNull(_buf)) {
return null;
}
return ffi.ForeignFunction(_buf, rtype, args);
},
set: function set(buf, offset, val) {
let _buf
if (Buffer.isBuffer(val)) {
_buf = val;
} else if ("_buf" in val) {
_buf = val._buf;
} else {
// assume fn
assert(typeof val=="function","val is not a function.")
_buf = ffi.Callback(rtype, args, val);
val._buf = _buf; //make lifetime valid as long as val is valid.
}
return ref.writePointer(buf, offset, _buf);
}
}
}
wintypes.IntPtr = ref.types[`int${ref.sizeof.pointer*8}`];
wintypes.UIntPtr = ref.types[`uint${ref.sizeof.pointer*8}`];
function objApiToJSApi(obj) {
if (obj.Kind == "Struct") {
return objApiStructToJSStruct(obj);
} else if (obj.Kind == "Enum") {
return objApiEnumToJSEnum(obj);
} else if (obj.Kind == "Union") {
return objApiUnionToJSUnion(obj);
} else if (obj.Kind == "Array") {
return objApiArrayToJSArray(obj);
}
}
function getNestedTypes(objApi) {
return Object.fromEntries(Object.entries((objApi.NestedTypes?.length) ? groupby(objApi.NestedTypes.map(objApiToJSApi), _ => ( /*console.log("this will break",_),*/ _.Name)) : {}).map(([a, b]) => (assert(b.length == 1), [a, b[0]])));
}
function getFields(someType) {
return Object.keys(someType.fields).flatMap(a => (someType.anonfields?.includes(a)) ? getFields(someType.fields[a].type) : a)
}
function objApiStructToJSStruct(objApi) {
var nestedTypes = getNestedTypes(objApi);
console.log(`STRUCT nestedTypes: `, objApi.Fields.map(a => [a.Name, a.Type.Name]).filter(([n, b]) => /Anonymous.*?_e__/.test(b)).map(([a, b]) => [a, Object.keys(nestedTypes[b].fields)]));
var obj = StructType(Object.fromEntries(objApi.Fields.map(_ => [_.Name, convertApiTypeToJSType(_.Type, nestedTypes)])));
obj["anonfields"] = objApi.Fields.map(a => [a.Name, a.Type.Name]).filter(([n, b]) => /Anonymous.*?_e__/.test(b)).map(([a, b]) => {
console.log(getFields(nestedTypes[b]), a);
var attrs = getFields(nestedTypes[b]);
attrs.forEach(name => {
methodProxy(obj.prototype, name, `${a}.${name}`)
})
return a
});
obj.Name = objApi.Name;
return obj
}
function convertApiTypeToJSType(type, nestedTypes) {
if (type.Kind == "Native") {
if (!wintypes[type.Name]) {
throw new Error(`${type.Name} not found in wintypes`);
} else {
return wintypes[type.Name];
}
} else if (type.Kind == "ApiRef") {
if (type.Parents.length == 0 && (!nestedTypes || (nestedTypes && !nestedTypes[type.Name]))) {
if (!wintypes[type.Name]) {
throw new Error(`${type.Name} not found in wintypes`);
} else {
return wintypes[type.Name];
}
} else
if (nestedTypes) {
if (!nestedTypes[type.Name]) {
throw new Error(`${type.Name} not found in nestedTypes: ${Object.keys(nestedTypes)}`);
} else {
return nestedTypes[type.Name];
}
} else {
}
} else if (type.Kind == "Array") {
return ArrayType(convertApiTypeToJSType(type.Child, nestedTypes), type.Shape.Size);
} else if (type.Kind == "PointerTo") {
return ref.refType(convertApiTypeToJSType(type.Child, nestedTypes));
} else if (type.Kind == "LPArray") {
//For C there is no difference, but for us there is, we can think about how to handle this later.
return ref.refType(convertApiTypeToJSType(type.Child, nestedTypes));
} else {
throw new Error("type " + type.Kind + " not found")
}
}
function objApiEnumToJSEnum(objApi) {
var nestedTypes = getNestedTypes(objApi);
return "todoEnum"
}
function objApiUnionToJSUnion(objApi) {
var nestedTypes = getNestedTypes(objApi);
console.log(`UNION nestedTypes: `, objApi.Fields.map(a => [a.Name, a.Type.Name]).filter(([n, b]) => /Anonymous.*?_e__/.test(b)).map(([a, b]) => [a, Object.keys(nestedTypes[b].fields)]));
var obj = new Union(Object.fromEntries(objApi.Fields.map(_ => [_.Name, convertApiTypeToJSType(_.Type, nestedTypes)])));
obj["anonfields"] = objApi.Fields.map(a => [a.Name, a.Type.Name]).filter(([n, b]) => /Anonymous.*?_e__/.test(b)).map(([a, b]) => {
console.log(getFields(nestedTypes[b]), a);
var attrs = getFields(nestedTypes[b]);
attrs.forEach(name => {
methodProxy(obj.prototype, name, `${a}.${name}`)
})
return a
});
obj.Name = objApi.Name
return obj
}
function objApiArrayToJSArray(objApi) {
var nestedTypes = getNestedTypes(objApi);
return "todoArray"
}
function fun2Type(E) {
var r = CFTypeConstructor(convertApiTypeToJSType(E.ReturnType), E.Params.map(a => convertApiTypeToJSType(a.Type)));
//assert(E.ReturnAttrs.length == 0); These are C# attributes,
if (E.Platform !== null) {
console.log(`Loading function ${E.Name} with Platform ${E.Platform}`)
}
r.SetLastError = E.SetLastError;
r.Params = E.Params;
return r;
}
function convertWinapiInterface2ffi(interfaceWinApiJson){
//an interface is just a struct :o
var sumInterface=StructType();
//Create type wrapper around struct
function InterfaceStruct(strobject) {
//is structObj a StructType or an Object
if (strobject instanceof sumInterface) {
this._$struct = strobject;
} else {
//whether buffer or Object should still work.
this._$struct = new sumInterface(strobject);
this._$struct['ref.buffer'].type=InterfaceStruct;
}
//This is definining how to call the methods from the JavaScript and not from the FFI api
/*interfaceWinApiJson.Methods.map(_=>_.Name).forEach((k) =>
this[k] = function(...args) {
return this._$struct[k]?.(this.ref(), ...args);
}
)*/
return this;
}
InterfaceStruct.prototype.ref = function(...args) {
return this._$struct.ref(...args)
}
Object.setPrototypeOf(InterfaceStruct, sumInterface);
InterfaceStruct.get = function(buf, offset) {
assert(offset==0,"offset is not zero, what do?");
return new this(buf);
}
InterfaceStruct.GUID=new wintypes.GUID(Buffer.from(interfaceWinApiJson.Guid.split('-').join(''), "hex"));
InterfaceStruct.methods=[];
if(interfaceWinApiJson.Interface){
assert(interfaceWinApiJson.Interface.Kind=='ApiRef',"Kind!=ApiRef")
assert(interfaceWinApiJson.Interface.TargetKind== 'Com',"TargetKind!=Com")
sumInterface.defineProperty("_anonymous__$parent",wintypes[interfaceWinApiJson.Interface.Name]);
wintypes[interfaceWinApiJson.Interface.Name].methods.forEach(prop=>methodProxy(InterfaceStruct.prototype, prop, `_anonymous__$parent.${prop}`))
InterfaceStruct.methods=wintypes[interfaceWinApiJson.Interface.Name].methods.slice();
}
var methods=interfaceWinApiJson.Methods.map(_=>[_.Name,method2Type(InterfaceStruct,_)]);
methods.forEach(_=>sumInterface.defineProperty(..._));
InterfaceStruct.methods=[...InterfaceStruct.methods,...methods.map(([n,_])=>n)];
return InterfaceStruct;
}
function method2Type(InterfaceStruct,E) {
var r = CFTypeConstructor(convertApiTypeToJSType(E.ReturnType), [ref.refType(InterfaceStruct),...E.Params.map(a => convertApiTypeToJSType(a.Type))]);
assert(E.ReturnAttrs.length == 0);
if (E.Platform !== null) {
console.log(`Loading function ${E.Name} with Platform ${E.Platform}`)
}
r.SetLastError = E.SetLastError;
r.Params = E.Params;
return r;
}
function appendDLL(filePath) {
const fileExtension = path.extname(filePath);
if (fileExtension === "") {
return filePath + ".dll";
} else {
return filePath;
}
}
function getDll(dllname) {
var dllstr = appendDLL(dllname);
if (dll[dllname]) return dll[dllname];
dll[dllname] = ffi.DynamicLibrary(dllstr, ffi.DynamicLibrary.FLAGS.RTLD_LAZY);
return dll[dllname];
}
var dll = {};
var functions = {};
var clsIDs={}
const debug = false
var readdir = util.promisify(fs.readdir);
async function requireJSONFiles(dirPath) {
const fileNames = await readdir(dirPath);
const jsonFiles = fileNames.filter(fileName => fileName.endsWith('.json'));
var jsonarr = {};
for (const fileName of jsonFiles) {
const filePath = path.join(dirPath, fileName);
const fileData = require("./" + filePath);
jsonarr[fileName] = fileData;
}
return jsonarr;
}
function procedureConvertApiFromJSON(apitoconvert) {
var typesbykind = groupby(apitoconvert.Types, a => a.Kind);
typesbykind.Enum?.forEach(enumv => {
lazySet(wintypes, enumv.Name, _ => ({
name: enumv.Name,
...nativehelper[enumv.IntegerBase].typeobj,
values: (enumv.Values.map(a => {
let x = {};
x[a.Name] = a.Value;
return x
}).reduce((a, b) => ({
...a,
...b
}))),
get(buf, offset) {
const _buf = buf[nativehelper[enumv.IntegerBase].readbuf](offset);
if (debug) {
if (enumv.Flags) {
return {
flags: Object.entries(this.values).filter(a => _buf & a[1]).map(a => a[0]),
_$debugValue: _buf
}
} else {
return {
possibleFlags: Object.entries(this.values).filter(a => a[1] == _buf).map(a => a[0]),
_$debugValue: _buf
}
}
} else
return _buf;
},
set(buf, offset, val) {
var setstr = nativehelper[enumv.IntegerBase].writebuf
if (debug && val._$debugValue) {
buf[setstr](val._$debugValue, offset);
} else if (Buffer.isBuffer(val)) {
val.copy(buf, offset, 0, this.size);
} else if (typeof val == "number") {
buf[setstr](val, offset);
} else {
var a = +this.values[val]
a = a ?? val;
buf[setstr](a, offset);
}
}
}));
});
typesbykind.NativeTypedef?.forEach(A => lazySet(wintypes, A.Name, _ => convertApiTypeToJSType(A.Def)));
//ComClassID are instances of interfaces
typesbykind.Com?.forEach(I=>lazySet(wintypes,I.Name,_=>convertWinapiInterface2ffi(I)))
typesbykind.ComClassID?.forEach(A=>{
var i=A.Name;
var clsID=new wintypes.GUID(Buffer.from(A.Guid.split('-').join(''), "hex"));
clsIDs[i]=clsID;
});
typesbykind.FunctionPointer?.forEach(E => lazySet(wintypes, E.Name, _ => fun2Type(E)));
typesbykind.Struct?.forEach(Struct => lazySet(wintypes, Struct.Name, _ => {
return objApiToJSApi(Struct);
}));
typesbykind.Union?.forEach(Union => lazySet(wintypes, Union.Name, _ => {
return objApiToJSApi(Union);
}));
apitoconvert.Constants?.forEach(_ => {
wintypes.attr(_.Type.Name).then(v=>{
v.constants = v.constants ?? {};
v.constants[_.Name] = _.Value;
});
});
apitoconvert.Functions?.forEach(a => lazySet(functions, a.Name, _ => ref.alloc(fun2Type(a), getDll(a.DllImport).get(a.Name))));
}
wintypes.GUID = StructType({
Data1: wintypes.ULONG,
Data2: wintypes.USHORT,
Data3: wintypes.USHORT,
Data4: ArrayType(wintypes.UCHAR, 8)
});
wintypes.Guid=wintypes.IID = wintypes.GUID;
wintypes.FMTID = wintypes.GUID;
//var apis = requireJSONFiles("win32json/api");
module.exports={lazySet,groupby,_get,_set,ffi,ref,StructType,Union,ArrayType,assert,path,nativehelper,longeSTring,wintypes,methodProxy,CFTypeConstructor,objApiToJSApi,getNestedTypes,
getFields,objApiStructToJSStruct,convertApiTypeToJSType,objApiEnumToJSEnum,objApiUnionToJSUnion,objApiArrayToJSArray,fun2Type,
convertWinapiInterface2ffi,method2Type,appendDLL,getDll,dll,functions,clsIDs,debug,readdir,requireJSONFiles,procedureConvertApiFromJSON,
done:requireJSONFiles("win32json/api").then(_=>Object.values(_).forEach(procedureConvertApiFromJSON))
};