Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Extended argument types #48

Merged
merged 20 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc
Expand Up @@ -5,6 +5,9 @@
},
"parser": "@babel/eslint-parser",
"extends": "airbnb-base",
"globals": {
"BigInt": true
},
"rules": {
"max-classes-per-file": "off",
"class-methods-use-this": "off",
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -32,6 +32,7 @@ osc-js is an [Open Sound Control](http://opensoundcontrol.org/) library for Java
- Featuring all [OSC 1.0 specifications](http://opensoundcontrol.org/spec-1_0)
- OSC Address pattern matching
- Time-critical OSC Bundles with Timetags
- Extended (nonstandard) argument types

## Documentation

Expand Down
4 changes: 2 additions & 2 deletions src/atomic/float32.js
@@ -1,4 +1,4 @@
import { isFloat } from '../common/utils'
import { isNumber } from '../common/utils'

import Atomic from '../atomic'

Expand All @@ -11,7 +11,7 @@ export default class AtomicFloat32 extends Atomic {
* @param {number} [value] Float number
*/
constructor(value) {
if (value && !isFloat(value)) {
if (value && !isNumber(value)) {
throw new Error('OSC AtomicFloat32 constructor expects value of type float')
}

Expand Down
38 changes: 38 additions & 0 deletions src/atomic/float64.js
@@ -0,0 +1,38 @@
import { isNumber } from '../common/utils'

import Atomic from '../atomic'

/**
* 64-bit big-endian IEEE 754 floating point number OSC Atomic Data Type
*/
export default class AtomicFloat64 extends Atomic {
/**
* Create an AtomicFloat64 instance
* @param {number} [value] Float number
*/
constructor(value) {
if (value && !isNumber(value)) {
throw new Error('OSC AtomicFloat64 constructor expects value of type float')
}

super(value)
}

/**
* Interpret the given number as packed binary data
* @return {Uint8Array} Packed binary data
*/
pack() {
return super.pack('setFloat64', 8)
}

/**
* Unpack binary data from DataView and read a Float64 number
* @param {DataView} dataView The DataView holding the binary representation of the value
* @param {number} [initialOffset=0] Offset of DataView before unpacking
* @return {number} Offset after unpacking
*/
unpack(dataView, initialOffset = 0) {
return super.unpack(dataView, 'getFloat64', 8, initialOffset)
}
}
48 changes: 48 additions & 0 deletions src/atomic/int64.js
@@ -0,0 +1,48 @@
import Atomic from '../atomic'

const MAX_INT64 = BigInt('9223372036854775807')
const MIN_INT64 = BigInt('-9223372036854775808')

/**
* 64-bit big-endian two's complement integer OSC Atomic Data Type
*/
export default class AtomicInt64 extends Atomic {
/**
* Create an AtomicInt64 instance
* @param {number} [value] Initial integer value
*/
constructor(value) {
if (value && typeof value !== 'bigint') {
throw new Error('OSC AtomicInt64 constructor expects value of type BigInt')
}

if (value && (value < MIN_INT64 || value > MAX_INT64)) {
throw new Error('OSC AtomicInt64 value is out of bounds')
}

let tmp
if (value) {
tmp = BigInt.asIntN(64, value)
}

super(tmp)
}

/**
* Interpret the given number as packed binary data
* @return {Uint8Array} Packed binary data
*/
pack() {
return super.pack('setBigInt64', 8)
}

/**
* Unpack binary data from DataView and read a Int64 number
* @param {DataView} dataView The DataView holding the binary representation of the value
* @param {number} [initialOffset=0] Offset of DataView before unpacking
* @return {number} Offset after unpacking
*/
unpack(dataView, initialOffset = 0) {
return super.unpack(dataView, 'getBigInt64', 8, initialOffset)
}
}
47 changes: 47 additions & 0 deletions src/atomic/uint64.js
@@ -0,0 +1,47 @@
import Atomic from '../atomic'

const MAX_UINT64 = BigInt('18446744073709551615')

/**
* Unsigned 64-bit big-endian two's complement integer OSC Atomic Data Type
*/
export default class AtomicUInt64 extends Atomic {
/**
* Create an AtomicUInt64 instance
* @param {number} [value] Initial integer value
*/
constructor(value) {
if (value && typeof value !== 'bigint') {
throw new Error('OSC AtomicUInt64 constructor expects value of type BigInt')
}

if (value && (value < 0 || value > MAX_UINT64)) {
throw new Error('OSC AtomicUInt64 value is out of bounds')
}

let tmp
if (value) {
tmp = BigInt.asUintN(64, value)
}

super(tmp)
}

/**
* Interpret the given number as packed binary data
* @return {Uint8Array} Packed binary data
*/
pack() {
return super.pack('setBigUint64', 8)
}

/**
* Unpack binary data from DataView and read a UInt64 number
* @param {DataView} dataView The DataView holding the binary representation of the value
* @param {number} [initialOffset=0] Offset of DataView before unpacking
* @return {number} Offset after unpacking
*/
unpack(dataView, initialOffset = 0) {
return super.unpack(dataView, 'getBigUint64', 8, initialOffset)
}
}
9 changes: 9 additions & 0 deletions src/common/utils.js
Expand Up @@ -16,6 +16,15 @@ export function isFloat(n) {
return Number(n) === n && n % 1 !== 0
}

/**
* Check if given object is a number
* @param {*} n
* @return {boolean}
*/
export function isNumber(n) {
return Number(n) === n
}

/**
* Check if given object is a string
* @param {*} n
Expand Down