@carbonenginejs/runtime-utils 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE +23 -0
  3. package/README.md +56 -0
  4. package/THIRD-PARTY-NOTICES.md +38 -0
  5. package/docs/README.md +81 -0
  6. package/docs/architecture.md +101 -0
  7. package/docs/concepts/foundation-consolidation.md +86 -0
  8. package/docs/const-kb.md +92 -0
  9. package/docs/core-types/DECORATOR-TODOS.md +25 -0
  10. package/docs/core-types/README.md +203 -0
  11. package/docs/reference/api.md +70 -0
  12. package/docs/reference/classes/README.md +115 -0
  13. package/package.json +132 -0
  14. package/src/arrays.js +5 -0
  15. package/src/audio/audioFormats.js +34 -0
  16. package/src/audio/index.js +1 -0
  17. package/src/box3.js +1385 -0
  18. package/src/bytes.js +56 -0
  19. package/src/compression.js +56 -0
  20. package/src/constants/index.js +6 -0
  21. package/src/constants.js +15 -0
  22. package/src/curve.js +419 -0
  23. package/src/d3d/dxgiFormats.js +46 -0
  24. package/src/d3d/index.js +2 -0
  25. package/src/d3d/primitiveTopology.js +11 -0
  26. package/src/document/CjsCarbonDocument.js +212 -0
  27. package/src/document/CjsClassRegistry.js +373 -0
  28. package/src/document/CjsDocumentDehydrator.js +142 -0
  29. package/src/document/CjsDocumentHydrator.js +156 -0
  30. package/src/document/CjsStructRegistry.js +348 -0
  31. package/src/document/hydrationAdapter.js +129 -0
  32. package/src/document/index.js +6 -0
  33. package/src/geometry/box.js +137 -0
  34. package/src/geometry/cylinder.js +244 -0
  35. package/src/geometry/helpers/LICENSE +15 -0
  36. package/src/geometry/helpers/earcut.js +766 -0
  37. package/src/geometry/helpers/misc.js +103 -0
  38. package/src/geometry/index.js +8 -0
  39. package/src/geometry/json.js +165 -0
  40. package/src/geometry/lathe.js +172 -0
  41. package/src/geometry/octahedron.js +0 -0
  42. package/src/geometry/plane.js +81 -0
  43. package/src/geometry/shape.js +95 -0
  44. package/src/geometry/sphere.js +123 -0
  45. package/src/geometry/torus.js +96 -0
  46. package/src/graphics/colorSpaces.js +22 -0
  47. package/src/graphics/index.js +4 -0
  48. package/src/graphics/pixelFormats.js +158 -0
  49. package/src/graphics/textureDimensions.js +22 -0
  50. package/src/graphics/trinityEnums.js +87 -0
  51. package/src/index.js +58 -0
  52. package/src/is.js +524 -0
  53. package/src/json.js +23 -0
  54. package/src/lifecycle/CjsLifecycleState.js +77 -0
  55. package/src/lifecycle/index.js +1 -0
  56. package/src/lne3.js +497 -0
  57. package/src/lookup.js +48 -0
  58. package/src/mat3.js +131 -0
  59. package/src/mat4.js +699 -0
  60. package/src/math/index.js +25 -0
  61. package/src/math/scalar.js +63 -0
  62. package/src/media/index.js +1 -0
  63. package/src/media/mediaTypes.js +50 -0
  64. package/src/mesh.js +394 -0
  65. package/src/model/CjsEventEmitter.js +333 -0
  66. package/src/model/CjsModel.js +1544 -0
  67. package/src/model/CjsModelState.js +72 -0
  68. package/src/model/index.js +4 -0
  69. package/src/model/sourceRecordUtils.js +54 -0
  70. package/src/noise.js +310 -0
  71. package/src/num.js +827 -0
  72. package/src/path.js +21 -0
  73. package/src/pln.js +762 -0
  74. package/src/pool.js +160 -0
  75. package/src/quat.js +144 -0
  76. package/src/ray3.js +1085 -0
  77. package/src/renderContext/formats.js +145 -0
  78. package/src/renderContext/index.js +5 -0
  79. package/src/renderContext/presentation.js +125 -0
  80. package/src/renderContext/resources.js +27 -0
  81. package/src/renderContext/upscaling.js +22 -0
  82. package/src/renderContext/window.js +20 -0
  83. package/src/runtime/CjsRuntimeState.js +50 -0
  84. package/src/schema/CjsSchema.js +1009 -0
  85. package/src/schema/index.js +17 -0
  86. package/src/shader/index.js +1 -0
  87. package/src/shader/shaderStages.js +37 -0
  88. package/src/sph3.js +754 -0
  89. package/src/tangent.js +288 -0
  90. package/src/text.js +40 -0
  91. package/src/tri3.js +650 -0
  92. package/src/types/carbonTypes.js +635 -0
  93. package/src/types/index.js +2 -0
  94. package/src/utils.js +58 -0
  95. package/src/validation.js +46 -0
  96. package/src/vec2.js +229 -0
  97. package/src/vec3.js +1172 -0
  98. package/src/vec4.js +347 -0
  99. package/src/vertex.js +108 -0
  100. package/src/webgpu/index.js +1 -0
  101. package/src/webgpu/textureFormats.js +121 -0
package/src/is.js ADDED
@@ -0,0 +1,524 @@
1
+
2
+ import { equals } from "./num.js";
3
+
4
+
5
+ const toString = Object.prototype.toString;
6
+
7
+ /**
8
+ * Checks if a value is a date
9
+ * @param s
10
+ * @returns {boolean}
11
+ */
12
+ export function isDate(s)
13
+ {
14
+ return !!(s && s instanceof Date);
15
+ }
16
+
17
+ /**
18
+ * Checks if a value is a map
19
+ * @param o
20
+ * @returns {boolean}
21
+ */
22
+ export function isMap(o)
23
+ {
24
+ return !!(o && o instanceof Map);
25
+ }
26
+
27
+ /**
28
+ * Checks if a value is a set
29
+ * @param o
30
+ * @returns {boolean}
31
+ */
32
+ export function isSet(o)
33
+ {
34
+ return !!(o && o instanceof Set);
35
+ }
36
+
37
+ /**
38
+ * Checks if an object is iterable
39
+ * @param {*} a
40
+ * @returns {boolean}
41
+ */
42
+ export function isIterable(a)
43
+ {
44
+ return a !== null && a !== undefined && typeof a[Symbol.iterator] === "function";
45
+ }
46
+
47
+ /**
48
+ * Checks if a value is an array
49
+ * @param {*} a
50
+ * @returns {Boolean}
51
+ */
52
+ export const isArray = Array.isArray;
53
+
54
+ /**
55
+ * Checks if a value is an array or a typed array
56
+ * @param {*} a
57
+ * @param {number} [minLength=0]
58
+ * @returns {Boolean}
59
+ */
60
+ export function isArrayLike(a, minLength = 0)
61
+ {
62
+ return !!(a && (isArray(a) || isTyped(a)) && a.length >= minLength);
63
+ }
64
+
65
+ /**
66
+ * Checks if a function is async
67
+ * @param {*} a
68
+ * @returns {boolean}
69
+ */
70
+ export function isAsyncFunction(a)
71
+ {
72
+ return !!(isFunction(a) && a.constructor.name === "AsyncFunction");
73
+ }
74
+
75
+
76
+ /**
77
+ * Checks if a value is a boolean
78
+ * @param {*} a
79
+ * @returns {Boolean}
80
+ */
81
+ export function isBoolean(a)
82
+ {
83
+ return isTag(a, "[object Boolean]");
84
+ }
85
+
86
+ /**
87
+ * Checks if a value is an html canvas element
88
+ * @param {*} a
89
+ * @returns {Boolean}
90
+ */
91
+ export function isCanvas(a)
92
+ {
93
+ return typeof HTMLCanvasElement !== "undefined" && a instanceof HTMLCanvasElement;
94
+ }
95
+
96
+ /**
97
+ * Checks if a value is a descriptor
98
+ * @author jay phelps
99
+ * @param {*} a
100
+ * @returns {Boolean}
101
+ */
102
+ export function isDescriptor(a)
103
+ {
104
+ if (!a || !a.hasOwnProperty)
105
+ {
106
+ return false;
107
+ }
108
+
109
+ const keys = [ "value", "initializer", "get", "set" ];
110
+
111
+ for (let i = 0, l = keys.length; i < l; i++)
112
+ {
113
+ if (a.hasOwnProperty(keys[i]))
114
+ {
115
+ return true;
116
+ }
117
+ }
118
+
119
+ return false;
120
+ }
121
+
122
+ /**
123
+ * Checks if a value is a valid sof DNA string
124
+ * @param {*} a
125
+ */
126
+ export function isDNA(a)
127
+ {
128
+ return isString(a) && /^[\w-]+:[\w-]+:[\w-]+$/.test(a);
129
+ }
130
+
131
+ /**
132
+ * Checks if a value is an error
133
+ * @param {*} a
134
+ * @returns {Boolean}
135
+ */
136
+ export function isError(a)
137
+ {
138
+ return !!(a && (a instanceof Error || a.constructor?.__category === "Error"));
139
+ }
140
+
141
+ /**
142
+ * Checks if a value is a number
143
+ * @param {*} a
144
+ * @returns {Boolean}
145
+ */
146
+ export function isNumber(a)
147
+ {
148
+ return isTag(a, "[object Number]");
149
+ }
150
+
151
+ /**
152
+ * Checks if a value is a function
153
+ * @param {*} a
154
+ * @returns {Boolean}
155
+ */
156
+ export function isFunction(a)
157
+ {
158
+ return typeof a === "function";
159
+ }
160
+
161
+ /**
162
+ * Checks if a value is null or undefined
163
+ * @param {*} a
164
+ * @returns {Boolean}
165
+ */
166
+ export function isNoU(a)
167
+ {
168
+ return a == null;
169
+ }
170
+
171
+ /**
172
+ * Checks if a value is null
173
+ * @param {*} a
174
+ * @returns {Boolean}
175
+ */
176
+ export function isNull(a)
177
+ {
178
+ return a === null;
179
+ }
180
+
181
+ /**
182
+ * Checks if a value is an object and not null
183
+ * @param {*} a
184
+ * @returns {Boolean}
185
+ */
186
+ export function isObject(a)
187
+ {
188
+ const type = typeof a;
189
+ return a !== null && (type === "object" || type === "function");
190
+ }
191
+
192
+ /**
193
+ * Checks if a value has the type object, and is not null
194
+ * @param {*} a
195
+ * @returns {Boolean}
196
+ */
197
+ export function isObjectLike(a)
198
+ {
199
+ return a !== null && typeof a === "object";
200
+ }
201
+
202
+ /**
203
+ * Is Object object
204
+ * @param {*} a
205
+ * @returns {Boolean}
206
+ */
207
+ export function isObjectObject(a)
208
+ {
209
+ return a !== null && isTag(a, "[object Object]");
210
+ }
211
+
212
+ /**
213
+ * Checks if a value is a plain object
214
+ * @author lodash
215
+ * @param {*} a
216
+ * @returns {Boolean}
217
+ */
218
+ export function isPlain(a)
219
+ {
220
+ if (!isObject(a) || !isObjectObject(a))
221
+ {
222
+ return false;
223
+ }
224
+
225
+ if (Object.getPrototypeOf(a) === null)
226
+ {
227
+ return true;
228
+ }
229
+
230
+ let proto = a;
231
+
232
+ while (Object.getPrototypeOf(proto) !== null)
233
+ {
234
+ proto = Object.getPrototypeOf(proto);
235
+ }
236
+
237
+ return Object.getPrototypeOf(a) === proto;
238
+ }
239
+
240
+ /**
241
+ * Checks if a value is a primary type
242
+ * @param {*} a
243
+ * @returns {Boolean}
244
+ */
245
+ export function isPrimary(a)
246
+ {
247
+ return isBoolean(a) || isNumber(a) || isString(a);
248
+ }
249
+
250
+ /**
251
+ * Checks if a value is a promise
252
+ * @param {*} a
253
+ * @returns {Boolean}
254
+ */
255
+ export function isPromise(a)
256
+ {
257
+ return isObject(a) && isFunction(a.then);
258
+ }
259
+
260
+ /**
261
+ * Checks if a value is a string
262
+ * @param {*} a
263
+ * @returns {Boolean}
264
+ */
265
+ export function isString(a)
266
+ {
267
+ return isTag(a, "[object String]");
268
+ }
269
+
270
+ /**
271
+ * Checks if a value is a symbol
272
+ * @param {*} a
273
+ * @returns {Boolean}
274
+ */
275
+ export function isSymbol(a)
276
+ {
277
+ return typeof a === "symbol" || isTag(a, "[object Symbol]");
278
+ }
279
+
280
+ /**
281
+ * Checks if a class is extended from another
282
+ * @param {*} Constructor
283
+ * @param {*} SuperConstructor
284
+ * @returns {boolean}
285
+ */
286
+ export function isSubclassOf(Constructor, SuperConstructor)
287
+ {
288
+ if (!isFunction(Constructor)) return false;
289
+
290
+ while (isFunction(Constructor))
291
+ {
292
+ if (Constructor === SuperConstructor) return true;
293
+ Constructor = Reflect.getPrototypeOf(Constructor);
294
+ }
295
+
296
+ return false;
297
+ }
298
+
299
+ /**
300
+ * Checks if a value has a given tag
301
+ * @param {*} a
302
+ * @param {String} tag
303
+ * @returns {Boolean}
304
+ */
305
+ export function isTag(a, tag)
306
+ {
307
+ return toString.call(a) === tag;
308
+ }
309
+
310
+ /**
311
+ * Checks if a string is preceded with 'Tr2' or 'Tri'
312
+ * @param {String} string
313
+ * @returns {boolean}
314
+ */
315
+ export function isTr2OrTri(string)
316
+ {
317
+ return !!(string && (string.indexOf("Tr2") === 0 || string.indexOf("Tri") === 0));
318
+ }
319
+
320
+ /**
321
+ * Converts a string that is preceeded with a 'Tr2' or 'Tri' to 'Tw2'
322
+ * @param string
323
+ * @returns {void|*|string}
324
+ */
325
+ export function toTw2(string)
326
+ {
327
+ return isTr2OrTri(string) ? string.replace("Tr2", "Tw2").replace("Tri", "Tw2") : string;
328
+ }
329
+
330
+
331
+ /**
332
+ * Gets an object's type in uppercase
333
+ * @param {*} a
334
+ * @returns {string}
335
+ */
336
+ export function getTypeUpper(a)
337
+ {
338
+ return Object.prototype.toString.call(a).slice(8, -1).toUpperCase();
339
+ }
340
+
341
+ /**
342
+ * Checks if a value is a typed array
343
+ * @param {*} a
344
+ * @returns {Boolean}
345
+ */
346
+ export function isTyped(a)
347
+ {
348
+ return a ? !!(a.buffer instanceof ArrayBuffer && a.BYTES_PER_ELEMENT) : false;
349
+ }
350
+
351
+ /**
352
+ * Checks if a value is undefined
353
+ * @param {*} a
354
+ * @returns {Boolean}
355
+ */
356
+ export function isUndefined(a)
357
+ {
358
+ return a === undefined;
359
+ }
360
+
361
+ /**
362
+ * Checks if a value is arraylike and only contains numbers
363
+ * @param {*} a
364
+ * @param {!Number} [len]
365
+ * @returns {Boolean}
366
+ */
367
+ export function isVector(a, len)
368
+ {
369
+ if (a)
370
+ {
371
+ if (isTyped(a))
372
+ {
373
+ return len === undefined ? true : a.length === len;
374
+ }
375
+
376
+ if (isArray(a))
377
+ {
378
+ if (len !== undefined && a.length !== len)
379
+ {
380
+ return false;
381
+ }
382
+
383
+ for (let i = 0; i < a.length; i++)
384
+ {
385
+ if (!isNumber(a[i])) return false;
386
+ }
387
+
388
+ return true;
389
+ }
390
+ }
391
+ return false;
392
+ }
393
+
394
+ /**
395
+ * Checks if a value is a vector of length 2
396
+ * @param {*} a
397
+ * @returns {boolean}
398
+ */
399
+ export function isVector2(a)
400
+ {
401
+ return isVector(a, 2);
402
+ }
403
+
404
+ /**
405
+ * Checks if a value is a vector of length 3
406
+ * @param {*} a
407
+ * @returns {boolean}
408
+ */
409
+ export function isVector3(a)
410
+ {
411
+ return isVector(a, 3);
412
+ }
413
+
414
+ /**
415
+ * Checks if a value is a vector of length 4
416
+ * @param {*} a
417
+ * @returns {boolean}
418
+ */
419
+ export function isVector4(a)
420
+ {
421
+ return isVector(a, 4);
422
+ }
423
+
424
+ /**
425
+ * Checks if a value is a vector of length 9
426
+ * @param {*} a
427
+ * @returns {boolean}
428
+ */
429
+ export function isMatrix3(a)
430
+ {
431
+ return isVector(a, 9);
432
+ }
433
+
434
+ /**
435
+ * Checks if a value is a vector of length 16
436
+ * @param {*} a
437
+ * @returns {boolean}
438
+ */
439
+ export function isMatrix4(a)
440
+ {
441
+ return isVector(a, 16);
442
+ }
443
+
444
+ /**
445
+ * Checks if two primary values are equal
446
+ * - Allows for numbers to be "almost" equal
447
+ * @param {String|Boolean|Number} a
448
+ * @param {String|Boolean|Number} b
449
+ * @returns {boolean}
450
+ */
451
+ export function isPrimaryEqual(a, b)
452
+ {
453
+ if (a === b) return true;
454
+
455
+ return isNumber(a) && isNumber(b) ? equals(a, b) : false;
456
+ }
457
+
458
+ /**
459
+ * Checks two vectors for equality
460
+ * - Allows for numbers to be "almost" equal
461
+ * @param {Array|TypedArray} a
462
+ * @param {Array|TypedArray} b
463
+ * @returns {boolean}
464
+ */
465
+ export function isVectorEqual(a, b)
466
+ {
467
+ if (a === b) return true;
468
+
469
+ if (a.length !== b.length) return false;
470
+
471
+ for (let i = 0; i < a.length; i++)
472
+ {
473
+ if (!equals(a[i], b[i])) return false;
474
+ }
475
+
476
+ return true;
477
+ }
478
+
479
+ export function isDateEqual(a, b)
480
+ {
481
+ return a.getTime() === b.getTime();
482
+ }
483
+
484
+ /**
485
+ * Checks two parameters for equality
486
+ * TODO: Optimize
487
+ * TODO: Circular references
488
+ * - Allows for numbers to be "almost" equal
489
+ * @param {String|Boolean|Number|Array|TypedArray} a
490
+ * @param {String|Boolean|Number|Array|TypedArray} b
491
+ * @returns {boolean}
492
+ */
493
+ export function isEqual(a, b)
494
+ {
495
+ if (a === b) return true;
496
+
497
+ // allow "almost" equal numbers
498
+ if (isNumber(a)) return isNumber(b) ? equals(a, b) : false;
499
+ //if(isDate(a))) return isDate(b) ? isEqual(a, b) : false;
500
+ if (isVector(a)) return isVector(b) ? isVectorEqual(a, b) : false;
501
+ if (!isObjectObject(a) || !isObjectObject(b)) return false;
502
+ if (a.constructor !== b.constructor) return false;
503
+
504
+ const
505
+ aKeys = Object.keys(a),
506
+ bKeys = Object.keys(b);
507
+
508
+ if (aKeys.length !== bKeys.length) return false;
509
+
510
+ for (let i = 0; i < aKeys.length; i++)
511
+ {
512
+ let key = aKeys[i];
513
+ if (!Object.prototype.hasOwnProperty.call(b, key)) return false;
514
+ if (!isEqual(a[key], b[key])) return false;
515
+ }
516
+
517
+ return true;
518
+ }
519
+
520
+ // Neutral runtime-utils spellings preserve the established core predicates.
521
+ export const isNullish = isNoU;
522
+ export const isPlainObject = isPlain;
523
+ export const isPromiseLike = isPromise;
524
+ export const isTypedArray = isTyped;
package/src/json.js ADDED
@@ -0,0 +1,23 @@
1
+ import { decodeUtf8, encodeUtf8 } from "./text.js";
2
+
3
+ /** Encodes JSON with explicit indentation and trailing-newline behavior. */
4
+ export function encodeJson(value, options = {})
5
+ {
6
+ const space = options.space ?? 2;
7
+ const text = JSON.stringify(value, options.replacer ?? null, space);
8
+
9
+ if (text === undefined)
10
+ {
11
+ throw new TypeError("JSON input is not serializable as a top-level value.");
12
+ }
13
+
14
+ return encodeUtf8(options.trailingNewline === false ? text : `${text}\n`);
15
+ }
16
+
17
+ /** Decodes JSON from a string or UTF-8 byte input. */
18
+ export function decodeJson(value, options = {})
19
+ {
20
+ const text = typeof value === "string" ? value : decodeUtf8(value, options);
21
+
22
+ return JSON.parse(text, options.reviver);
23
+ }
@@ -0,0 +1,77 @@
1
+ import { ensureRuntimeState, getRuntimeState } from "../runtime/CjsRuntimeState.js";
2
+
3
+ export const CJS_LIFECYCLE = Object.freeze({
4
+ ALIVE: "alive",
5
+ DESTROY_PENDING: "destroyPending",
6
+ DESTROYING: "destroying",
7
+ DESTROYED: "destroyed"
8
+ });
9
+
10
+ /** Inspectable lifecycle state shared by participating runtime objects. */
11
+ export class CjsLifecycleState
12
+ {
13
+
14
+ status = CJS_LIFECYCLE.ALIVE;
15
+
16
+ Is(status)
17
+ {
18
+ return this.status === status;
19
+ }
20
+
21
+ IsAlive()
22
+ {
23
+ return this.Is(CJS_LIFECYCLE.ALIVE);
24
+ }
25
+
26
+ IsAvailable()
27
+ {
28
+ return this.IsAlive();
29
+ }
30
+
31
+ }
32
+
33
+ /**
34
+ * Installs shared, non-enumerable lifecycle state on an object.
35
+ *
36
+ * @param {object|Function} target
37
+ * @returns {CjsLifecycleState}
38
+ */
39
+ export function initializeLifecycleState(target)
40
+ {
41
+ const state = ensureRuntimeState(target);
42
+ if (Object.prototype.hasOwnProperty.call(state, "lifecycle"))
43
+ {
44
+ if (!(state.lifecycle instanceof CjsLifecycleState))
45
+ {
46
+ throw new TypeError("Existing __state.lifecycle must be a CjsLifecycleState.");
47
+ }
48
+ return state.lifecycle;
49
+ }
50
+
51
+ const lifecycle = new CjsLifecycleState();
52
+ Object.defineProperty(state, "lifecycle", {
53
+ value: lifecycle,
54
+ enumerable: true,
55
+ configurable: false,
56
+ writable: false
57
+ });
58
+ return lifecycle;
59
+ }
60
+
61
+ /**
62
+ * Gets lifecycle state without enrolling the target in lifecycle management.
63
+ *
64
+ * An absent state means the target is ordinarily alive and unmanaged.
65
+ *
66
+ * @param {object|Function} target
67
+ * @returns {CjsLifecycleState|null}
68
+ */
69
+ export function getLifecycleState(target)
70
+ {
71
+ const lifecycle = getRuntimeState(target)?.lifecycle ?? null;
72
+ if (lifecycle !== null && !(lifecycle instanceof CjsLifecycleState))
73
+ {
74
+ throw new TypeError("Existing __state.lifecycle must be a CjsLifecycleState.");
75
+ }
76
+ return lifecycle;
77
+ }
@@ -0,0 +1 @@
1
+ export * from "./CjsLifecycleState.js";