napi_run_script
functionv1js_native_api.hThis function executes a string of JavaScript code and returns its result with the following caveats:
NAPI_EXTERN napi_status napi_run_script(napi_env env,
napi_value script,
napi_value* result);
[in] env: The environment that the API is invoked under.[in] script: A JavaScript string containing the script to execute.[out] result: The value resulting from having executed the script.
This function executes a string of JavaScript code and returns its result with the following caveats:
- Unlike
eval, this function does not allow the script to access the current lexical scope, and therefore also does not allow to access the module scope, meaning that pseudo-globals such asrequirewill not be available. - The script can access the global scope. Function and
vardeclarations in the script will be added to theglobalobject. Variable declarations made usingletandconstwill be visible globally, but will not be added to theglobalobject. - The value of
thisisglobalwithin the script.