nv-constexpr-simple-codify
v1.0.7
Published
A slow but precise object-to-code converter designed specifically for **macro compilation tools** and compile-time code generation.
Readme
nv-constexpr-simple-codify
A slow but precise object-to-code converter designed specifically for macro compilation tools and compile-time code generation.
Its a readable But less-suportted-type version of nv-buf-data-to-code.
⚠️ Important Notice
This library is NOT optimized for performance. It is intentionally slow because it generates executable JavaScript code rather than serialized data. Use it only when you need:
- Compile-time constant evaluation
- Macro expansion systems
- Generating code from configuration objects
- Static analysis tools that output executable code
Do NOT use this for runtime serialization or high-frequency operations.
Supported Leaf Types (1:1 C++ Mapping)
Every supported leaf type has a direct 1:1 counterpart in C++. This makes the output ideal for generating C++ constexpr-compatible code.
| JavaScript Type | C++ Equivalent | Output Format | Example |
|-----------------|----------------|---------------|---------|
| undefined | Special NaN sentinel | undefined | undefined (see note below) |
| null | std::nullptr_t / NULL | null | null |
| boolean | bool | true / false | true |
| string | std::string / const char* | JSON-stringified | "hello\nworld" |
| number | double / float / int | String representation | 123.45 |
| bigint | std::int64_t / std::uint64_t | n suffix notation | 9007199254740993n |
| ArrayBuffer | std::array<uint8_t> (raw buffer) | IIFE with byte array | (()=>{ var ab = new ArrayBuffer(8);(new Uint8Array(ab)).set([0,32,64,96,128,160,192,224]);return(ab);})() |
| SharedArrayBuffer | std::shared_ptr<std::array<uint8_t>> | IIFE with byte array | Same format as ArrayBuffer |
| DataView | struct with manual byte access | new DataView(...) | (new DataView((new Uint8Array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])).buffer)) |
| Uint8Array | std::array<uint8_t> | new Uint8Array([...]) | (new Uint8Array([1,2,255])) |
| Uint8ClampedArray | std::array<uint8_t> (clamped) | new Uint8ClampedArray([...]) | (new Uint8ClampedArray([0,128,255])) |
| Int8Array | std::array<int8_t> | new Int8Array([...]) | (new Int8Array([-128,0,127])) |
| Uint16Array | std::array<uint16_t> | new Uint16Array([...]) | (new Uint16Array([1,65535])) |
| Int16Array | std::array<int16_t> | new Int16Array([...]) | (new Int16Array([-32768,32767])) |
| Uint32Array | std::array<uint32_t> | new Uint32Array([...]) | (new Uint32Array([1,4294967295])) |
| Int32Array | std::array<int32_t> | new Int32Array([...]) | (new Int32Array([-2147483648,2147483647])) |
| Float32Array | std::array<float> | new Float32Array([...]) | (new Float32Array([1.5,-2.5,0])) |
| Float64Array | std::array<double> | new Float64Array([...]) | (new Float64Array([1.7976931348623157e+308,-Infinity,NaN])) |
| BigUint64Array | std::array<uint64_t> | new BigUint64Array([...]) | (new BigUint64Array([18446744073709551615n])) |
| BigInt64Array | std::array<int64_t> | new BigInt64Array([...]) | (new BigInt64Array([-9223372036854775808n])) |
| Date | std::chrono::system_clock::time_point | new Date(timestamp) | (new Date(1705321845123)) |
| RegExp | do NOT use std::regex | /pattern/flags | /test\/pattern/gi |
Special Note on undefined
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.
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.
RegExp use a third-party library that supports compile-time regex (do NOT use std::regex)
String
new String (typeof !== string BUT instanceof String) ; this is for special use: to generate var-name/identifier ; will NOT be quoted.
string will be JSON.stringify(quoted)
Container Support
Only two container types are supported:
- Arrays
[]- Indexed collections - Plain Objects
{}- String-keyed collections
Nested structures are fully supported (arrays within objects, objects within arrays, etc.).
Unsupported containers:
Map- Not supportedSet- Not supportedWeakMap- Not supportedWeakSet- Not supportedArray-like objects- Not supported- Custom classes/constructors - Not supported
API
codify(obj, space = 0)
Main entry point. Converts an object to a code string.
obj- The object to convert (must contain only supported types)space- Optional indentation:0or""(empty string) - No indentation, compact outputnumber- Number of spaces per levelstring- Custom indentation string
Returns: string - Executable JavaScript code
Internal Functions
For advanced use cases, the following internal functions are exported:
_codify_leaf(o)- Convert a single leaf value_codify_ary(ary, depth, space, recv, is_lst, key)- Process arrays_codify_dict(dict, depth, space, recv, is_lst, key)- Process objects_codify_typed_ary(o, Cls)- Convert TypedArrays/DataView_codify_ab(o, ClsName)- Convert ArrayBuffer/SharedArrayBuffer
Use Cases
This tool is designed for:
- Macro Compilation Systems - Generate code at compile time
- Static Configuration - Convert config objects to constants
- Code Generation Tools - Build source code from templates
- Build-time Optimizations - Pre-compute values during build
- Domain-Specific Languages - Implement DSL compilers
Performance Note
This library prioritizes code fidelity over execution speed. It is significantly slower than JSON.stringify because:
- It generates executable code, not just data
- It handles many more data types
- It performs recursive processing with string building
- It's not optimized for large datasets
Do NOT use it For runtime serialization.
License
Any
