-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
61 lines (58 loc) · 1.92 KB
/
api.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
const ffi = require('ffi-napi');
const ref = require('ref-napi');
const Struct = require('ref-struct-di')(ref);
const Union = require('ref-union-di')(ref);
const ArrayType = require('ref-array-di')(ref);
var e = (typeof process.env.fmodex_global_export == 'undefined') ? exports : global;
var library_exports = [];
var enerator;
exports.e = e;
exports.l = {};
exports.functions = {};
exports.ffi = ffi;
exports.ref = ref;
exports.Func = ffi.Function;
exports.Struct = Struct;
exports.Union = Union;
exports.ArrayType = ArrayType;
exports.library_exports = library_exports;
exports.from_hex = function(hex_str) {
return parseInt(hex_str, 16);
}
exports.en = function(reset) {
if (typeof reset !== 'undefined')
enerator = reset;
return enerator++;
}
exports.encoder = new TextEncoder();
exports.decoder = new TextDecoder();
exports.default_terminator = '\x00';
exports.from_utf16 = function(str, terminator = exports.default_terminator) {
str += terminator;
var arr = new Uint16Array(str.length);
for (var i = 0; i < str.length; i++) {
arr[i] = str.charCodeAt(i);
}
return arr;
}
exports.to_utf16 = function(arr, terminator = exports.default_terminator) {
return exports.trim_terminator(String.fromCharCode(...arr), terminator);
}
exports.from_utf8 = function(str, terminator = exports.default_terminator) {
return encoder.encode(str + terminator);
}
exports.to_utf8 = function(arr, terminator = exports.default_terminator) {
return exports.trim_terminator(decoder.decode(arr), terminator);
}
exports.trim_terminator = function(str, terminator = exports.default_terminator) {
const terminator_index = str.indexOf(terminator);
if (!terminator || terminator_index < 0)
return str;
return str.substr(0, terminator_index);
}
exports.push_export = function(sublibrary_export) {
library_exports.push(sublibrary_export);
}
exports.push_functions = function(sublibrary_functions) {
library_functions.push(sublibrary_functions);
}