Skip to main content

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.
Parameters:
  • ctx - The JavaScript context
  • str - A null-terminated UTF-8 encoded string
Returns: A new atom, or JS_ATOM_NULL on error Example:

JS_NewAtomLen

Creates an atom from a string with explicit length.
Parameters:
  • ctx - The JavaScript context
  • str - A UTF-8 encoded string (may contain null bytes)
  • len - The length of the string in bytes
Returns: A new atom, or JS_ATOM_NULL on error Example:

JS_NewAtomUInt32

Creates an atom from a 32-bit unsigned integer.
Parameters:
  • ctx - The JavaScript context
  • n - An unsigned 32-bit integer
Returns: A new atom representing the number Description: This is useful for array indices and other numeric property keys. JavaScript automatically converts numeric strings to numbers for property access. Example:

Converting Atoms

JS_AtomToValue

Converts an atom to a JavaScript string value.
Parameters:
  • ctx - The JavaScript context
  • atom - The atom to convert
Returns: A new JSValue containing the string, or JS_EXCEPTION on error Example:

JS_AtomToString

Converts an atom to a JavaScript string value (same as JS_AtomToValue).
Parameters:
  • ctx - The JavaScript context
  • atom - The atom to convert
Returns: A new 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.
Parameters:
  • ctx - The JavaScript context
  • atom - The atom to convert
Returns: A null-terminated C string, or NULL on error. Must be freed with JS_FreeCString(). Example:

JS_AtomToCStringLen

Converts an atom to a C string with length information.
Parameters:
  • ctx - The JavaScript context
  • plen - Pointer to receive the string length, or NULL
  • atom - The atom to convert
Returns: A C string, or NULL on error. Must be freed with JS_FreeCString(). Example:

JS_ValueToAtom

Converts a JavaScript value to an atom.
Parameters:
  • ctx - The JavaScript context
  • val - A JavaScript value (typically a string or symbol)
Returns: A new atom, or 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).
Parameters:
  • ctx - The JavaScript context
  • v - The atom to duplicate
Returns: The same atom value Description: Increments the reference count of the atom. This is necessary when you need to store an atom or pass it to multiple parts of your code. Example:

JS_DupAtomRT

Duplicates an atom using the runtime.
Parameters:
  • rt - The JavaScript runtime
  • v - The atom to duplicate
Returns: The same atom value Description: Same as 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).
Parameters:
  • ctx - The JavaScript context
  • v - The atom to free
Description: Decrements the reference count of the atom. When the count reaches zero, the atom is removed from the intern table. You must call this for every atom you create or duplicate. Example:

JS_FreeAtomRT

Frees an atom using the runtime.
Parameters:
  • rt - The JavaScript runtime
  • v - The atom to free
Description: Same as 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

  1. Always free atoms you create: Every JS_NewAtom() must have a corresponding JS_FreeAtom().
  2. Match Dup with Free: Every JS_DupAtom() needs a JS_FreeAtom().
  3. Don’t double-free: Only free an atom once per creation/duplication.
  4. Check for NULL: After creating an atom, check if it’s JS_ATOM_NULL before using it.
  5. Cache frequently used atoms: Create commonly-used atoms once and reuse them.

Performance Considerations

  1. Atom creation has overhead: Creating atoms is more expensive than using existing ones.
  2. Use convenience functions when appropriate: For one-time property access, JS_GetPropertyStr() is simpler than creating an atom.
  3. Intern common strings: QuickJS automatically interns common strings, so creating atoms for them is efficient.

See Also