UNPKG

@glimmer/util

Version:

Common utilities used in Glimmer

105 lines (98 loc) 11.2 kB
import { debugAssert as assert } from './assert'; export function isHandle(value) { return value >= 0; } export function isNonPrimitiveHandle(value) { return value > 3 /* ENCODED_UNDEFINED_HANDLE */ ; } export function constants(...values) { return [false, true, null, undefined, ...values]; } export function isSmallInt(value) { return value % 1 === 0 && value <= 536870911 /* MAX_INT */ && value >= -536870912 /* MIN_INT */ ; } export function encodeNegative(num) { if (false /* LOCAL_DEBUG */ ) { assert(num % 1 === 0 && num >= -536870912 /* MIN_INT */ && num < 0, `Could not encode negative: ${num}`); } return num & -536870913 /* SIGN_BIT */ ; } export function decodeNegative(num) { if (false /* LOCAL_DEBUG */ ) { assert(num % 1 === 0 && num < ~536870911 /* MAX_INT */ && num >= -1073741824 /* MIN_SMI */ , `Could not decode negative: ${num}`); } return num | ~-536870913 /* SIGN_BIT */ ; } export function encodePositive(num) { if (false /* LOCAL_DEBUG */ ) { assert(num % 1 === 0 && num >= 0 && num <= 536870911 /* MAX_INT */ , `Could not encode positive: ${num}`); } return ~num; } export function decodePositive(num) { if (false /* LOCAL_DEBUG */ ) { assert(num % 1 === 0 && num <= 0 && num >= ~536870911 /* MAX_INT */ , `Could not decode positive: ${num}`); } return ~num; } export function encodeHandle(num) { if (false /* LOCAL_DEBUG */ ) { assert(num % 1 === 0 && num >= 0 && num <= 1073741823 /* MAX_SMI */ , `Could not encode handle: ${num}`); } return num; } export function decodeHandle(num) { if (false /* LOCAL_DEBUG */ ) { assert(num % 1 === 0 && num <= 1073741823 /* MAX_SMI */ && num >= 0, `Could not decode handle: ${num}`); } return num; } export function encodeImmediate(num) { num |= 0; return num < 0 ? encodeNegative(num) : encodePositive(num); } export function decodeImmediate(num) { num |= 0; return num > -536870913 /* SIGN_BIT */ ? decodePositive(num) : decodeNegative(num); } // Warm [1, 2, 3].forEach(x => decodeHandle(encodeHandle(x))); [1, -1].forEach(x => decodeImmediate(encodeImmediate(x))); //# sourceMappingURL=data:application/json;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