Skip to content

Commit 1bcbc22

Browse files
Deploy to GitHub pages
0 parents  commit 1bcbc22

33 files changed

+16377
-0
lines changed

Binary.html

Lines changed: 980 additions & 0 deletions
Large diffs are not rendered by default.

Binary.js.html

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>JSDoc: Source: Binary.js</title>
6+
7+
<script src="scripts/prettify/prettify.js"> </script>
8+
<script src="scripts/prettify/lang-css.js"> </script>
9+
<!--[if lt IE 9]>
10+
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11+
<![endif]-->
12+
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
13+
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
14+
</head>
15+
16+
<body>
17+
18+
<div id="main">
19+
20+
<h1 class="page-title">Source: Binary.js</h1>
21+
22+
23+
24+
25+
26+
27+
<section>
28+
<article>
29+
<pre class="prettyprint source linenums"><code>// Not efficient enough...
30+
// import { nonenumerable } from 'core-decorators';
31+
32+
import Types from './Types'
33+
34+
/** Class allowing `@binary` members */
35+
class Binary {
36+
// TODO:
37+
// - Handle Endianess
38+
// - Handle Clamped
39+
40+
// Class props
41+
// Slowers down 4x times...
42+
// @nonenumerable
43+
static _size
44+
/**
45+
* Static getter for the class binary size
46+
* @return {number} - The class binary size
47+
*/
48+
static get binarySize () {
49+
return this._size
50+
}
51+
52+
// @nonenumerable
53+
static _binaryProps
54+
/**
55+
* Static getter for the class binary props
56+
* @return {array} - The list of binary props
57+
*/
58+
static get binaryProps () {
59+
return this._binaryProps
60+
}
61+
62+
/**
63+
* Fills an array with objects of this class using a unique buffer
64+
* @param {ArrayBuffer/SharedArrayBuffer/DataView} binOrDv - The buffer where the data lives
65+
* @param {number} length - The number of objects to add to the array
66+
* @param {number} initialOffset - Buffer offset before this object data start
67+
* @param {array} list - The array where new objects will be added
68+
* @return {array} - The array {@link list} where the objects have been added
69+
*/
70+
// @nonenumerable
71+
static arrayFactory (binOrDV, length, initialOffset = 0, list = []) {
72+
// Optimize: Generate a single DataView for all elements
73+
const dv = binOrDV instanceof DataView ? binOrDV : new DataView(binOrDV)
74+
75+
for (let i = 0; i &lt; length; i++) {
76+
list.push(new this(dv, initialOffset + this._size * i))
77+
}
78+
79+
return list
80+
}
81+
82+
// Prototype props
83+
// @nonenumerable
84+
_initialOffset
85+
// @nonenumerable
86+
_bin
87+
// @nonenumerable
88+
__dv
89+
/**
90+
* Getter of the DataView containing this object's data
91+
* @return {DataView} - The DataView
92+
*/
93+
// @nonenumerable
94+
get _dv () {
95+
this.__dv =
96+
this?.__dv ??
97+
new DataView(this._bin, this._initialOffset, this.constructor._size)
98+
return this.__dv
99+
}
100+
101+
/**
102+
* Transform this object into a JSON string containing all the binary members
103+
* @return {string} - The JSON string
104+
* @method
105+
*/
106+
// @nonenumerable
107+
toJSON = () =>
108+
this.constructor._binaryProps.reduce(
109+
(acc, prop) => ({
110+
...acc,
111+
[prop]: this[prop]
112+
}),
113+
{}
114+
)
115+
116+
/**
117+
* Save own initial offset at binary data
118+
* @param {ArrayBuffer/SharedArrayBuffer/DataView} binOrDv - The buffer where the data lives
119+
* @param {number} initialOffset - Buffer offset before this object data start
120+
* @param {boolean} isLazy - If true and {@link binOrDv} is not a {DataView}, wait until first acces before Instantiating the __dv
121+
*/
122+
constructor (binOrDV, initialOffset = 0, isLazy = true) {
123+
this._initialOffset = initialOffset
124+
if (binOrDV instanceof DataView) {
125+
this.__dv = binOrDV
126+
} else {
127+
this._bin = binOrDV
128+
if (!isLazy) {
129+
this._dv // Call getter
130+
}
131+
}
132+
}
133+
134+
/**
135+
* Get a single byte (as unsigned integer) from a position
136+
* @param {number} offset - The position of the byte to get
137+
* @return {number} - The unsigned numerical number at the specified position
138+
* @method
139+
*/
140+
// @nonenumerable
141+
getByteAt = (offset) =>
142+
Types.Uint8.get(this._dv, this._initialOffset + offset)
143+
}
144+
145+
/*
146+
Object.defineProperty(Binary, "_size", {
147+
enumerable: false,
148+
writable: true,
149+
});
150+
Object.defineProperty(Binary, "binarySize", {
151+
enumerable: false,
152+
get () { return this._size }
153+
});
154+
155+
Object.defineProperty(Binary, "_binaryProps", {
156+
enumerable: false,
157+
writable: true,
158+
});
159+
Object.defineProperty(Binary, "binaryProps", {
160+
enumerable: false,
161+
get () { return this._binaryProps }
162+
});
163+
*/
164+
165+
export default Binary
166+
</code></pre>
167+
</article>
168+
</section>
169+
170+
171+
172+
173+
</div>
174+
175+
<nav>
176+
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Binary.html">Binary</a></li><li><a href="BinaryArrayBase.html">BinaryArrayBase</a></li></ul><h3>Global</h3><ul><li><a href="global.html#BinaryArray">BinaryArray</a></li><li><a href="global.html#BinaryArrayHandler">BinaryArrayHandler</a></li><li><a href="global.html#Types">Types</a></li><li><a href="global.html#binary">binary</a></li><li><a href="global.html">binarySize</a></li><li><a href="global.html#decorator">decorator</a></li><li><a href="global.html#iterator">iterator</a></li><li><a href="global.html#propertyDescriptor">propertyDescriptor</a></li><li><a href="global.html#propertyDescriptorGetter">propertyDescriptorGetter</a></li><li><a href="global.html#propertyDescriptorSetter">propertyDescriptorSetter</a></li><li><a href="global.html#withBinary">withBinary</a></li></ul>
177+
</nav>
178+
179+
<br class="clear">
180+
181+
<footer>
182+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.3</a> on Wed Jun 12 2024 07:48:28 GMT+0000 (Coordinated Universal Time)
183+
</footer>
184+
185+
<script> prettyPrint(); </script>
186+
<script src="scripts/linenumber.js"> </script>
187+
</body>
188+
</html>

BinaryArray.js.html

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>JSDoc: Source: BinaryArray.js</title>
6+
7+
<script src="scripts/prettify/prettify.js"> </script>
8+
<script src="scripts/prettify/lang-css.js"> </script>
9+
<!--[if lt IE 9]>
10+
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11+
<![endif]-->
12+
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
13+
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
14+
</head>
15+
16+
<body>
17+
18+
<div id="main">
19+
20+
<h1 class="page-title">Source: BinaryArray.js</h1>
21+
22+
23+
24+
25+
26+
27+
<section>
28+
<article>
29+
<pre class="prettyprint source linenums"><code>/** Class for returning array members from {@link Binary} objects */
30+
class BinaryArrayBase {
31+
// @member
32+
type
33+
// @member
34+
dv
35+
// @member
36+
offset
37+
// @member
38+
length
39+
// @member
40+
bytes
41+
42+
/**
43+
* Creates a new customized array
44+
* @param {DataView} dv - The DataView handling the buffer where the data lives
45+
* @param {object} type - The type of the array members from {@link Types}
46+
* @param {number} offset - The offset of the first member of the array into the buffer
47+
* @param {number} length - The length of the array
48+
*/
49+
constructor (dv, type, offset, length) {
50+
this.type = type
51+
this.dv = dv
52+
this.offset = offset
53+
this.length = length
54+
this.bytes = length * type.bytes
55+
}
56+
57+
/**
58+
* Proxy array methods using this iterator
59+
* @param {function} fn - The function to apply on the array elements
60+
* @return {array} - The new generated array (not bound to original values)
61+
* @method
62+
*/
63+
map = (fn) => Array.from(this, fn)
64+
// reduce = (...args) => Array.prototype.reduce.call([...this], ...args);
65+
66+
/**
67+
* Transform this array into a JSON string
68+
* @return {string} - The JSON string representing this array
69+
* @method
70+
*/
71+
toJSON = () => JSON.parse(JSON.stringify(this.map()));
72+
73+
/**
74+
* Make a generator iterator
75+
* @method
76+
* @generator
77+
* @function iterator
78+
* @yield {any} - Each of this array elements of type {@link Types}
79+
* @name iterator
80+
*/
81+
* [Symbol.iterator] () {
82+
// Deconstruct to optimize and ease reading
83+
const {
84+
length,
85+
dv,
86+
offset,
87+
type: { get, bytes }
88+
} = this
89+
90+
// Use a new index for each iterator. This makes multiple
91+
// iterations over the iterable safe for non-trivial cases,
92+
// such as use of break or nested looping over the same iterable.
93+
for (let index = 0; index &lt; length; index++) {
94+
yield get(dv, offset + bytes * index)
95+
}
96+
}
97+
}
98+
99+
/**
100+
* A Proxy handler for the {@link BinaryArray} class to allow accessing its elements
101+
* @enum
102+
*/
103+
const BinaryArrayHandler = {
104+
/**
105+
* Getter for the elements of the handled {@link BinaryArray}
106+
* @param {BinaryArray} target - The handled {@link BinaryArray} instance
107+
* @param {string} prop - The property to return (only handled when prop is a string representing a number)
108+
* @return {any} - The element at {@link prop} position, or a reflected value from {@link target}
109+
*/
110+
get (target, prop) {
111+
// Very inefficient way
112+
// Need to:
113+
// - Override Array internals, but are private
114+
// - Override `[]` operator, but it's not possible
115+
if (prop === '0' || (typeof prop === 'string' &amp;&amp; Number(prop) > 0)) {
116+
// Destructure to optimize
117+
const {
118+
dv,
119+
offset,
120+
type: { get, bytes }
121+
} = target
122+
return get(dv, offset + bytes * Number(prop))
123+
}
124+
125+
// Return original value
126+
return Reflect.get(target, prop)
127+
},
128+
129+
/**
130+
* Setter for the elements of the handled {@link BinaryArray}
131+
* @param {BinaryArray} target - The handled {@link BinaryArray} instance
132+
* @param {string} prop - The property to set (only handled when prop is a string representing a number)
133+
* @param {any} value - The value to assign to the {@link prop}'th element
134+
* @return {boolean} - If {@link prop} is numericalish, true (as needed for JS setters), else the return value from the {@link target} reflected setter
135+
*/
136+
set (target, prop, value) {
137+
if (prop === '0' || (typeof prop === 'string' &amp;&amp; Number(prop) > 0)) {
138+
// Destructure to optimize
139+
const {
140+
dv,
141+
offset,
142+
type: { set, bytes }
143+
} = target
144+
set(dv, offset + bytes * Number(prop), value)
145+
return true
146+
}
147+
return Reflect.set(target, prop, value)
148+
}
149+
}
150+
151+
// #TODO: BUG: Argument Spread Operator not working
152+
// well when packing with webpack
153+
/**
154+
* Proxy creator for {@link BinaryArrayBase}
155+
* @param {DataView} dv - The DataView handling the buffer where the data lives
156+
* @param {object} type - The type of the array members from {@link Types}
157+
* @param {number} offset - The offset before the first member of the array
158+
* @param {number} length - The length of the array
159+
* @return {Proxy} - The proxy to {@link BinaryArrayBase} with {@link BinaryArrayHandler} as proxy handler
160+
*/
161+
const BinaryArray = (dv, type, offset, length) => {
162+
return new Proxy(
163+
new BinaryArrayBase(dv, type, offset, length),
164+
BinaryArrayHandler
165+
)
166+
}
167+
168+
export default BinaryArray
169+
</code></pre>
170+
</article>
171+
</section>
172+
173+
174+
175+
176+
</div>
177+
178+
<nav>
179+
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Binary.html">Binary</a></li><li><a href="BinaryArrayBase.html">BinaryArrayBase</a></li></ul><h3>Global</h3><ul><li><a href="global.html#BinaryArray">BinaryArray</a></li><li><a href="global.html#BinaryArrayHandler">BinaryArrayHandler</a></li><li><a href="global.html#Types">Types</a></li><li><a href="global.html#binary">binary</a></li><li><a href="global.html">binarySize</a></li><li><a href="global.html#decorator">decorator</a></li><li><a href="global.html#iterator">iterator</a></li><li><a href="global.html#propertyDescriptor">propertyDescriptor</a></li><li><a href="global.html#propertyDescriptorGetter">propertyDescriptorGetter</a></li><li><a href="global.html#propertyDescriptorSetter">propertyDescriptorSetter</a></li><li><a href="global.html#withBinary">withBinary</a></li></ul>
180+
</nav>
181+
182+
<br class="clear">
183+
184+
<footer>
185+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.3</a> on Wed Jun 12 2024 07:48:28 GMT+0000 (Coordinated Universal Time)
186+
</footer>
187+
188+
<script> prettyPrint(); </script>
189+
<script src="scripts/linenumber.js"> </script>
190+
</body>
191+
</html>

0 commit comments

Comments
 (0)