nv-constexpr-simple-codify 1.0.0 → 1.0.2
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.
- package/README.md +3 -0
- package/TEST/tst-is-supported.js +22 -0
- package/index.js +140 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -50,9 +50,12 @@ Every supported leaf type has a direct 1:1 counterpart in C++. This makes the ou
|
|
|
50
50
|
|
|
51
51
|
`undefined` does not have a direct C++ equivalent. In C++ code generation scenarios, it should be replaced with a **special NaN sentinel value** (nan is a range)`) or a designated invalid marker. The codifier outputs `undefined` as-is for transparency, leaving the C++ mapping decision to the downstream compiler logic.
|
|
52
52
|
|
|
53
|
+
`BigInt` Note on Large BigInt: For values exceeding 64 bits, GCC's __int128can be used. For arbitrarily large values beyond __int128range, consider directly copying V8's bigint.ccimplementation into your project.
|
|
54
|
+
|
|
53
55
|
`RegExp` use a third-party library that supports compile-time regex (do NOT use std::regex)
|
|
54
56
|
|
|
55
57
|
|
|
58
|
+
|
|
56
59
|
## Container Support
|
|
57
60
|
|
|
58
61
|
**Only two container types are supported:**
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
const {is_supported} = require("../index");
|
|
3
|
+
|
|
4
|
+
// 正常情况
|
|
5
|
+
console.log(is_supported({ a: 1n, b: [1, 2, 3] }));
|
|
6
|
+
// Reason { reason: null, __proto__: Boolean, [[PrimitiveValue]]: true }
|
|
7
|
+
|
|
8
|
+
// 循环引用
|
|
9
|
+
const circ = { a: 1 };
|
|
10
|
+
circ.self = circ;
|
|
11
|
+
console.log(is_supported(circ));
|
|
12
|
+
// Reason { reason: "Circular reference detected", __proto__: Boolean, [[PrimitiveValue]]: false }
|
|
13
|
+
|
|
14
|
+
// 不支持的类型
|
|
15
|
+
console.log(is_supported(new Set()));
|
|
16
|
+
// Reason { reason: "Set/Map/WeakMap/WeakSet/WeakRef is not supported", ... }
|
|
17
|
+
|
|
18
|
+
console.log(is_supported(Promise.resolve()));
|
|
19
|
+
// Reason { reason: "Promise is not supported", ... }
|
|
20
|
+
|
|
21
|
+
console.log(is_supported(function() {}));
|
|
22
|
+
// Reason { reason: "Function is not supported", ... }
|
package/index.js
CHANGED
|
@@ -198,8 +198,148 @@ function codify (obj,space=0){
|
|
|
198
198
|
}
|
|
199
199
|
|
|
200
200
|
|
|
201
|
+
const is_prms = (o) => o[Symbol.toStringTag] === "Promise";
|
|
202
|
+
const is_sg = (o) => o[Symbol.toStringTag] === 'Generator';
|
|
203
|
+
const is_ag = (o) => o[Symbol.toStringTag] === 'AsyncGenerator';
|
|
204
|
+
const is_g = (o) => is_sg(o) || is_ag(o);
|
|
205
|
+
const is_iter = (o) => o[Symbol.toStringTag] ? (o[Symbol.toStringTag].includes("Iterator")) : false;
|
|
206
|
+
class Reason extends Boolean {
|
|
207
|
+
reason = null;
|
|
208
|
+
constructor(reason) {
|
|
209
|
+
super((reason === null || reason === undefined) ? true : false);
|
|
210
|
+
this.reason = reason;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// 用于循环引用检测的 WeakSet
|
|
215
|
+
function is_supported(o, seen = new WeakSet()) {
|
|
216
|
+
// 1. 循环引用检测
|
|
217
|
+
if (o !== null && typeof o === 'object') {
|
|
218
|
+
if (seen.has(o)) {
|
|
219
|
+
return new Reason("Circular reference detected");
|
|
220
|
+
}
|
|
221
|
+
seen.add(o);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// 2. 排除 Symbol
|
|
225
|
+
if (typeof o === 'symbol') {
|
|
226
|
+
return new Reason("Symbol is not supported");
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// 3. 排除 Function
|
|
230
|
+
if (typeof o === 'function') {
|
|
231
|
+
return new Reason("Function is not supported");
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// 4. 排除 Promise
|
|
235
|
+
if (is_prms(o)) {
|
|
236
|
+
return new Reason("Promise is not supported");
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// 5. 排除 Generator / AsyncGenerator
|
|
240
|
+
if (is_g(o)) {
|
|
241
|
+
return new Reason("Generator/AsyncGenerator is not supported");
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// 6. 排除 Iterator
|
|
245
|
+
if (is_iter(o)) {
|
|
246
|
+
return new Reason("Iterator is not supported");
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// 7. 支持的基础类型
|
|
250
|
+
if (
|
|
251
|
+
o === undefined ||
|
|
252
|
+
o === null ||
|
|
253
|
+
typeof o === 'boolean' ||
|
|
254
|
+
typeof o === 'string' ||
|
|
255
|
+
typeof o === 'number' ||
|
|
256
|
+
typeof o === 'bigint'
|
|
257
|
+
) {
|
|
258
|
+
return new Reason(null); // 无理由,就是支持
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// 8. 支持的特殊对象类型
|
|
262
|
+
if (o instanceof Date) {
|
|
263
|
+
return new Reason(null);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (o instanceof RegExp) {
|
|
267
|
+
return new Reason(null);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (o instanceof ArrayBuffer || o?.constructor?.name === 'SharedArrayBuffer') {
|
|
271
|
+
return new Reason(null);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (o instanceof DataView) {
|
|
275
|
+
return new Reason(null);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// TypedArrays
|
|
279
|
+
if (
|
|
280
|
+
o instanceof Uint8Array ||
|
|
281
|
+
o instanceof Uint8ClampedArray ||
|
|
282
|
+
o instanceof Int8Array ||
|
|
283
|
+
o instanceof Uint16Array ||
|
|
284
|
+
o instanceof Int16Array ||
|
|
285
|
+
o instanceof Uint32Array ||
|
|
286
|
+
o instanceof Int32Array ||
|
|
287
|
+
o instanceof Float32Array ||
|
|
288
|
+
o instanceof Float64Array ||
|
|
289
|
+
o instanceof BigUint64Array ||
|
|
290
|
+
o instanceof BigInt64Array
|
|
291
|
+
) {
|
|
292
|
+
return new Reason(null);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// 9. 容器只能是 Array 和 plain Object
|
|
296
|
+
if (Array.isArray(o)) {
|
|
297
|
+
for (const item of o) {
|
|
298
|
+
const res = is_supported(item, seen);
|
|
299
|
+
if (!res) {
|
|
300
|
+
return res; // 已经是 Reason(false)
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return new Reason(null);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (o !== null && typeof o === 'object') {
|
|
307
|
+
// 排除不支持的容器类型
|
|
308
|
+
if (
|
|
309
|
+
o instanceof Set ||
|
|
310
|
+
o instanceof Map ||
|
|
311
|
+
o instanceof WeakMap ||
|
|
312
|
+
o instanceof WeakSet ||
|
|
313
|
+
o instanceof WeakRef
|
|
314
|
+
) {
|
|
315
|
+
return new Reason("Set/Map/WeakMap/WeakSet/WeakRef is not supported");
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
// 检查是否是普通对象(plain object)
|
|
320
|
+
const proto = Object.getPrototypeOf(o);
|
|
321
|
+
if (proto !== Object.prototype && proto !== null) {
|
|
322
|
+
return new Reason("Custom class instance is not supported (only plain objects)");
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
// 递归检查每个属性值
|
|
327
|
+
for (const key in o) {
|
|
328
|
+
const res = is_supported(o[key], seen);
|
|
329
|
+
if (!res) {
|
|
330
|
+
return res;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return new Reason(null);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// 其他未知类型不支持
|
|
337
|
+
return new Reason("Unknown type is not supported");
|
|
338
|
+
}
|
|
339
|
+
|
|
201
340
|
|
|
202
341
|
module.exports = codify;
|
|
342
|
+
module.exports.is_supported = is_supported;
|
|
203
343
|
module.exports._codify_typed_ary = _codify_typed_ary;
|
|
204
344
|
module.exports._codify_ab = _codify_ab;
|
|
205
345
|
module.exports._codify_leaf = _codify_leaf;
|