UNPKG

rc-js-util

Version:

A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.

192 lines 8.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StableIdStore = exports.populateTypedArrayConstructorMap = exports.numberSpecializations = exports.getNumberSpecialization = exports.bufferCategory = exports.numberCategory = exports.IdSpecialization = exports.IdCategory = exports.StableId = exports.getNumberIdentifier = exports.ENumberIdentifier = void 0; const shared_buffer_view_js_1 = require("../web-assembly/shared-memory/shared-buffer-view.js"); const _debug_js_1 = require("../debug/_debug.js"); const number_get_hex_string_js_1 = require("../number/impl/number-get-hex-string.js"); /** * @public * Matches ENumberIdentifier in `RTTI.hpp`. */ var ENumberIdentifier; (function (ENumberIdentifier) { ENumberIdentifier[ENumberIdentifier["U8"] = 0] = "U8"; ENumberIdentifier[ENumberIdentifier["U16"] = 1] = "U16"; ENumberIdentifier[ENumberIdentifier["U32"] = 2] = "U32"; ENumberIdentifier[ENumberIdentifier["U64"] = 3] = "U64"; ENumberIdentifier[ENumberIdentifier["I8"] = 4] = "I8"; ENumberIdentifier[ENumberIdentifier["I16"] = 5] = "I16"; ENumberIdentifier[ENumberIdentifier["I32"] = 6] = "I32"; ENumberIdentifier[ENumberIdentifier["I64"] = 7] = "I64"; ENumberIdentifier[ENumberIdentifier["F32"] = 8] = "F32"; ENumberIdentifier[ENumberIdentifier["F64"] = 9] = "F64"; })(ENumberIdentifier || (exports.ENumberIdentifier = ENumberIdentifier = {})); /** * @public * Given a typed array constructor, get the identifier which matches up with ENumberIdentifier in `RTTI.hpp`. */ function getNumberIdentifier(ctor) { return numberIdentifierMapping.get(ctor); } exports.getNumberIdentifier = getNumberIdentifier; const numberIdentifierMapping = new Map([ [Uint8Array, ENumberIdentifier.U8], [Uint16Array, ENumberIdentifier.U16], [Uint32Array, ENumberIdentifier.U32], [BigUint64Array, ENumberIdentifier.U64], [Int8Array, ENumberIdentifier.I8], [Int16Array, ENumberIdentifier.I16], [Int32Array, ENumberIdentifier.I32], [BigInt64Array, ENumberIdentifier.I64], [Float32Array, ENumberIdentifier.F32], [Float64Array, ENumberIdentifier.F64], ]); /** * @public * Mirrors the C++ class of the same name. This can be used as a key to get a unique number which can be matched on the C++ side. */ class StableId { constructor(name) { this.name = name; } } exports.StableId = StableId; /** * @public * Mirrors the C++ class of the same name. Each category can have multiple kinds of specializations. * * @remarks There should only be one instance of a conceptual class (which should have a unique name) */ class IdCategory extends StableId { } exports.IdCategory = IdCategory; /** * @public * Mirrors the C++ class of the same name. A specialization of a conceptual category. E.g. a number (category) which is a float 32 (specialization). * * @remarks There should only be one instance of a conceptual class (which should have a unique name) */ class IdSpecialization extends StableId { constructor(category, specializationName) { super(specializationName); this.category = category; } } exports.IdSpecialization = IdSpecialization; /** * @public * Supported types of number across C++ and JavaScript. */ exports.numberCategory = new IdCategory("JSU_NUMBER"); /** * @public * A buffer that can be shared with C++. */ exports.bufferCategory = new IdCategory("JSU_BUFFER"); /** * @public * Given a Typed array constructor, get back the associated {@link StableId}. */ function getNumberSpecialization(ctor) { return numberSpecializationMapping.get(ctor); } exports.getNumberSpecialization = getNumberSpecialization; /** * @public * {@link IdSpecialization} for all supported number types. */ exports.numberSpecializations = new class NumberSpecializations { constructor() { this.u8 = new IdSpecialization(exports.numberCategory, "JSU_U8"); this.u16 = new IdSpecialization(exports.numberCategory, "JSU_U16"); this.u32 = new IdSpecialization(exports.numberCategory, "JSU_U32"); this.u64 = new IdSpecialization(exports.numberCategory, "JSU_U64"); this.i8 = new IdSpecialization(exports.numberCategory, "JSU_I8"); this.i16 = new IdSpecialization(exports.numberCategory, "JSU_I16"); this.i32 = new IdSpecialization(exports.numberCategory, "JSU_I32"); this.i64 = new IdSpecialization(exports.numberCategory, "JSU_I64"); this.f32 = new IdSpecialization(exports.numberCategory, "JSU_F32"); this.f64 = new IdSpecialization(exports.numberCategory, "JSU_F64"); } }(); const numberSpecializationMapping = new Map([ [Uint8Array, exports.numberSpecializations.u8], [Uint16Array, exports.numberSpecializations.u16], [Uint32Array, exports.numberSpecializations.u32], [BigUint64Array, exports.numberSpecializations.u64], [Int8Array, exports.numberSpecializations.i8], [Int16Array, exports.numberSpecializations.i16], [Int32Array, exports.numberSpecializations.i32], [BigInt64Array, exports.numberSpecializations.i64], [Float32Array, exports.numberSpecializations.f32], [Float64Array, exports.numberSpecializations.f64], ]); /** * @internal */ function populateTypedArrayConstructorMap(constructors) { const m = new Map(); m.set(Float64Array, constructors.f64); m.set(Float32Array, constructors.f32); if (constructors.i64 != null) { m.set(BigInt64Array, constructors.i64); } if (constructors.u64 != null) { m.set(BigUint64Array, constructors.u64); } m.set(Int32Array, constructors.i32); m.set(Uint32Array, constructors.u32); m.set(Int16Array, constructors.i16); m.set(Uint16Array, constructors.u16); m.set(Int8Array, constructors.i8); m.set(Uint8Array, constructors.u8); m.set(Uint8ClampedArray, constructors.u8c); return m; } exports.populateTypedArrayConstructorMap = populateTypedArrayConstructorMap; /** * @public * {@inheritDoc IStableStore} */ class StableIdStore { constructor(wrapper) { this.wrapper = wrapper; this.ids = new Map(); /** * @internal */ this.idBuffer = null; } initialize() { this.idBuffer = new shared_buffer_view_js_1.SharedBufferView(this.wrapper, this.wrapper.rootNode, Uint16Array, this.wrapper.instance._jsUtilGetRuntimeMappingAddress(), 32 * Uint16Array.BYTES_PER_ELEMENT); } getId(key) { let id = this.ids.get(key); if (id == null) { const namePtr = this.wrapper.instance.stringToNewUTF8(key.name); if (_BUILD.DEBUG) { _debug_js_1._Debug.verboseLog(["WASM", "MEMORY", "ALLOCATIONS"], `StableIdStore: created temporary string at ${(0, number_get_hex_string_js_1.numberGetHexString)(namePtr)} (${key.name}).`); } id = this.wrapper.instance._jsUtilGetRuntimeMappingId(namePtr); this.ids.set(key, id); this.wrapper.instance._jsUtilFree(namePtr); } return id; } setSpecializations(sharedObjectHandle, specializations) { _BUILD.DEBUG && _debug_js_1._Debug.assert(this.idBuffer != null, "attempted to set ids before initialization happened..."); _BUILD.DEBUG && _debug_js_1._Debug.assert(specializations.length <= 32, "a maximum of 32 specializations can be added at one time"); const idBuffer = this.idBuffer.getArray(); // [[category, specialization], ...] for (let i = 0, iEnd = specializations.length; i < iEnd; i++) { const specialization = specializations[i]; idBuffer[i * 2] = this.getId(specialization); idBuffer[i * 2 + 1] = this.getId(specialization.category); } this.wrapper.instance._jsUtilAddRuntimeMappings(specializations.length, sharedObjectHandle.pointer); } hasId(sharedObjectHandle, specialization) { return Boolean(this.wrapper.instance._jsUtilHasRuntimeMappingId(sharedObjectHandle.pointer, this.getId(specialization.category), this.getId(specialization))); } } exports.StableIdStore = StableIdStore; //# sourceMappingURL=rtti-interop.js.map