Atom Operations
This page covers all the functions for creating, converting, duplicating, and freeing atoms in QuickJS.Creating Atoms
JS_NewAtom
Creates an atom from a null-terminated C string.ctx- The JavaScript contextstr- A null-terminated UTF-8 encoded string
JS_ATOM_NULL on error
Example:
JS_NewAtomLen
Creates an atom from a string with explicit length.ctx- The JavaScript contextstr- A UTF-8 encoded string (may contain null bytes)len- The length of the string in bytes
JS_ATOM_NULL on error
Example:
JS_NewAtomUInt32
Creates an atom from a 32-bit unsigned integer.ctx- The JavaScript contextn- An unsigned 32-bit integer
Converting Atoms
JS_AtomToValue
Converts an atom to a JavaScript string value.ctx- The JavaScript contextatom- The atom to convert
JSValue containing the string, or JS_EXCEPTION on error
Example:
JS_AtomToString
Converts an atom to a JavaScript string value (same asJS_AtomToValue).
ctx- The JavaScript contextatom- The atom to convert
JSValue containing the string, or JS_EXCEPTION on error
Note: This is functionally identical to JS_AtomToValue() and exists for API consistency.
JS_AtomToCString
Converts an atom to a temporary C string.ctx- The JavaScript contextatom- The atom to convert
NULL on error. Must be freed with JS_FreeCString().
Example:
JS_AtomToCStringLen
Converts an atom to a C string with length information.ctx- The JavaScript contextplen- Pointer to receive the string length, orNULLatom- The atom to convert
NULL on error. Must be freed with JS_FreeCString().
Example:
JS_ValueToAtom
Converts a JavaScript value to an atom.ctx- The JavaScript contextval- A JavaScript value (typically a string or symbol)
JS_ATOM_NULL on error
Description:
This function converts a JavaScript value to an atom. It’s particularly useful when you receive a value from JavaScript that you need to use as a property key.
Example:
Reference Counting
JS_DupAtom
Duplicates an atom (increments its reference count).ctx- The JavaScript contextv- The atom to duplicate
JS_DupAtomRT
Duplicates an atom using the runtime.rt- The JavaScript runtimev- The atom to duplicate
JS_DupAtom() but operates at the runtime level. Use this when you don’t have a context available.
JS_FreeAtom
Frees an atom (decrements its reference count).ctx- The JavaScript contextv- The atom to free
JS_FreeAtomRT
Frees an atom using the runtime.rt- The JavaScript runtimev- The atom to free
JS_FreeAtom() but operates at the runtime level.
Common Patterns
Pattern 1: Simple Property Access
Pattern 2: Reusing Atoms
Pattern 3: Storing Atoms
Pattern 4: Array Index Access
Memory Management Best Practices
-
Always free atoms you create: Every
JS_NewAtom()must have a correspondingJS_FreeAtom(). -
Match Dup with Free: Every
JS_DupAtom()needs aJS_FreeAtom(). - Don’t double-free: Only free an atom once per creation/duplication.
-
Check for NULL: After creating an atom, check if it’s
JS_ATOM_NULLbefore using it. - Cache frequently used atoms: Create commonly-used atoms once and reuse them.
Performance Considerations
- Atom creation has overhead: Creating atoms is more expensive than using existing ones.
-
Use convenience functions when appropriate: For one-time property access,
JS_GetPropertyStr()is simpler than creating an atom. - Intern common strings: QuickJS automatically interns common strings, so creating atoms for them is efficient.
See Also
- Atoms Overview - Understanding atoms and their use cases