JS_Eval - Code Evaluation
TheJS_Eval() function evaluates JavaScript source code from a string.
JS_Eval
Evaluate JavaScript code.Parameters
ctx- The JavaScript contextinput- The source code string (must be zero-terminated:input[input_len] = '\0')input_len- Length of the source codefilename- Filename for error messages and stack traces (can be any descriptive string)eval_flags- Evaluation flags
Returns
Returns the result of evaluation, orJS_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 specificthis binding.
Example
JS_EvalFunction
Execute compiled bytecode.Parameters
ctx- The JavaScript contextfun_obj- Bytecode object fromJS_Eval()withJS_EVAL_FLAG_COMPILE_ONLY
Returns
Returns the result of execution.Example
Module Detection
JS_DetectModule
Detect if input is a module.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
inputstring must be zero-terminated:input[input_len] = '\0' - Input can be pure ASCII or UTF-8 encoded
- Always check for
JS_EXCEPTIONbefore using the result - Use
js_std_dump_error()from quickjs-libc to print exceptions JS_EVAL_FLAG_COMPILE_ONLYis useful for ahead-of-time compilationJS_EVAL_FLAG_ASYNCwithJS_EVAL_TYPE_GLOBALallows top-level await and returns a promise