Skip to main content

JS_Eval - Code Evaluation

The JS_Eval() function evaluates JavaScript source code from a string.

JS_Eval

Evaluate JavaScript code.

Parameters

  • ctx - The JavaScript context
  • input - The source code string (must be zero-terminated: input[input_len] = '\0')
  • input_len - Length of the source code
  • filename - Filename for error messages and stack traces (can be any descriptive string)
  • eval_flags - Evaluation flags

Returns

Returns the result of evaluation, or JS_EXCEPTION on error.

Evaluation Flags

Example: Basic Evaluation

Example: Module Evaluation

Example: Strict Mode

Example: Compile Only

JS_Eval2

Evaluate JavaScript code with extended options.

JSEvalOptions Structure

Example

JS_EvalThis

Evaluate code with a specific this binding.

Example

JS_EvalFunction

Execute compiled bytecode.

Parameters

  • ctx - The JavaScript context
  • fun_obj - Bytecode object from JS_Eval() with JS_EVAL_FLAG_COMPILE_ONLY

Returns

Returns the result of execution.

Example

Module Detection

JS_DetectModule

Detect if input is a module.
Returns true if parsing the input as a module produces no syntax errors. Note: This is a naive approach - non-strict classic scripts may parse as a module but have different runtime semantics.

Example

Notes

  • The input string must be zero-terminated: input[input_len] = '\0'
  • Input can be pure ASCII or UTF-8 encoded
  • Always check for JS_EXCEPTION before using the result
  • Use js_std_dump_error() from quickjs-libc to print exceptions
  • JS_EVAL_FLAG_COMPILE_ONLY is useful for ahead-of-time compilation
  • JS_EVAL_FLAG_ASYNC with JS_EVAL_TYPE_GLOBAL allows top-level await and returns a promise