@hms-networks/kolibri-js-core
Version:
Kolibri Core library containing common kolibri models and utils
65 lines • 2.65 kB
JavaScript
;
/*
* Copyright 2021 HMS Industrial Networks AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http: //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Opcode = void 0;
const long_1 = __importDefault(require("long"));
const errorcode_1 = require("./../../common/errorcode");
const constant_1 = require("../../common/constant");
const buffer_length_builder_1 = require("../buffer_utils/buffer_length_builder");
class Opcode {
constructor(name, code) {
this.name = name;
this.code = code;
this.bufferLengthBuilder = new buffer_length_builder_1.BufferLengthBuilder();
}
getName() {
return this.name;
}
getCode() {
return this.code;
}
getDataTypeSize(dataType, value) {
return this.bufferLengthBuilder.getDataTypeSize(dataType, value);
}
writeTriggerN(ocBuf, triggerN, dataType) {
let value = dataType === constant_1.constants.DT_UNSIGNED_64BIT || dataType === constant_1.constants.DT_SIGNED_64BIT
? long_1.default.fromNumber(triggerN)
: triggerN;
ocBuf.writeDataType(value, dataType);
}
readTriggerN(ocBuf, dataType, kpo) {
// triggerN has the corresponding unsigned data type for integer data types
let dt = dataType > constant_1.constants.DT_SIGNED_64BIT || dataType % 2 ? dataType : dataType - 1;
let value = ocBuf.readDataType(dt);
// 64-bit integer triggerN values are limited to [0, 2^53 - 1] and are
// represented as 64-bit floating point values internally.
if (dt === constant_1.constants.DT_UNSIGNED_64BIT) {
if (value.lessThan(long_1.default.ZERO) || value.greaterThan(constant_1.constants.DT_FLOAT64_MAX_INT)) {
throw new errorcode_1.KolibriError(errorcode_1.errorcode.INVALID_VALUE, kpo);
}
return value.toNumber();
}
else {
return value;
}
}
}
exports.Opcode = Opcode;
//# sourceMappingURL=opcode.js.map