Skip to main content

Runtime Creation

The runtime represents a JavaScript engine instance with its own object heap. These functions manage the lifecycle of the runtime.

JS_NewRuntime

Creates a new JavaScript runtime with default memory allocators.

Returns

Pointer to the newly created runtime, or NULL if allocation failed.

Description

Creates a new JSRuntime instance representing an independent JavaScript execution environment. Each runtime has its own object heap and cannot share objects with other runtimes.
Multiple runtimes can exist simultaneously but cannot exchange objects between them. No multi-threading is supported within a single runtime.

Example


JS_NewRuntime2

Creates a new JavaScript runtime with custom memory allocators.

Parameters

const JSMallocFunctions *
required
Pointer to a structure containing custom memory allocation functions.
void *
User-defined pointer passed to all allocator functions. Can be NULL.

Returns

Pointer to the newly created runtime, or NULL if allocation failed.

Description

Creates a runtime with custom memory management. This is useful for:
  • Tracking memory allocations
  • Implementing custom memory pools
  • Integrating with existing memory management systems
  • Adding allocation hooks for debugging
The JSMallocFunctions structure must contain:

Example


JS_FreeRuntime

Frees a JavaScript runtime and all associated resources.

Parameters

JSRuntime *
required
The runtime to free.

Description

Destroys a runtime and releases all associated memory. This function:
  • Frees all contexts associated with the runtime
  • Runs garbage collection to free all objects
  • Executes any registered finalizers in LIFO order
  • Releases all allocated memory
All contexts created from this runtime must be freed with JS_FreeContext() before calling JS_FreeRuntime(). After calling this function, the runtime pointer becomes invalid.

Example

Debug Information

If dump flags are enabled, JS_FreeRuntime can print diagnostics: