moduleglobal
Global object, the base object accessible to all scripts
object
Buffer
Binary data cache object forioData processing for reading and writing, seeBufferobject.
1Buffer global.Buffer;
URL
CreateUrlObjectrequest object, seeUrlObject
1UrlObject global.URL;
TextDecoder
TextDecoderdecoding object, seeTextDecoderobject.
1TextDecoder global.TextDecoder;
TextEncoder
TextEncoderEncoding objects, seeTextEncoderobject.
1TextEncoder global.TextEncoder;
console
Console access object
1console global.console;
process
process object
1process global.process;
performance
Basic performance monitoring module
1performance global.performance;
static function
run
run a script
1static global.run(String fname);
Call parameters:
- fname: String, specifies the path of the script to be run
require
Loads a module and returns the module object, see @ref module for more information
1static Value global.require(String id);
Call parameters:
- id: String, specifies the name of the module to be loaded
Return results:
- Value, returns the export object of the loaded module
require can be used to load basic modules and file modules.
The basic module is the module initialized when the sandbox is created. When referencing, you only need to pass the corresponding id, such as require("net").
The file module is a user-defined module, and a relative path starting with ./ or ../ needs to be passed when referencing. The file module supports .js, .jsc and .jsondocument.
The file module also supports the package.json format. When the module has a directory structure, require will first query main in package.json. If it is not found, it will try to load index.js, index.jsc or index.json in the path.
If the reference path does not start with ./ or ../ and is not a basic module, require will search from node_modules in the path of the current module and recurse to the upper directory.
The basic process is as follows:
setTimeout
Call a function after a specified amount of time
1
2
3static Timer global.setTimeout(Function callback,
Number timeout = 1,
...args);
Call parameters:
- callback: Function, specify the callback function
- timeout: Number, specifies the delay time, in milliseconds. If it exceeds 2^31, it will be executed immediately.
- args: ..., additional parameters, passed into the specified callback, optional.
Return results:
- Timer, returns the timer object
clearTimeout
Clear the specified timer
1static global.clearTimeout(Value t);
Call parameters:
- t: Value, specifies the timer to be cleared
setInterval
Call the function after a specified interval
1
2
3static Timer global.setInterval(Function callback,
Number timeout,
...args);
Call parameters:
- callback: Function, specify the callback function
- timeout: Number, specifying the interval time in milliseconds. If it exceeds 2^31, it will be executed immediately.
- args: ..., additional parameters, passed into the specified callback, optional.
Return results:
- Timer, returns the timer object
clearInterval
Clear the specified timer
1static global.clearInterval(Value t);
Call parameters:
- t: Value, specifies the timer to be cleared
setHrInterval
The function is called after every specified time interval. This is a high-precision timer that will actively interrupt the execution timer of the running JavaScript script.
1
2
3static Timer global.setHrInterval(Function callback,
Number timeout,
...args);
Call parameters:
- callback: Function, specify the callback function
- timeout: Number, specifying the interval time in milliseconds. If it exceeds 2^31, it will be executed immediately.
- args: ..., additional parameters, passed into the specified callback, optional.
Return results:
- Timer, returns the timer object
Since the setHrInterval timer will interrupt the running code execution callback, do not modify data that may affect other modules in the callback function, or call any api function marked async in the callback, otherwise unpredictable results will occur. For example:
1
2
3
4
5
6
7
8
9
10var timers = require('timers');
var cnt = 0;
timers.setHrInterval(() => {
cnt++;
}, 100);
while (cnt < 10);
console.error("===============================> done");
In this code, the loop on line 8 will not end because of the change of cnt, because JavaScript will determine that cnt will not be changed during the loop when optimizing the code.
clearHrInterval
Clear the specified timer
1static global.clearHrInterval(Value t);
Call parameters:
- t: Value, specifies the timer to be cleared
setImmediate
Execute the callback function immediately during the next idle time
1
2static Timer global.setImmediate(Function callback,
...args);
Call parameters:
- callback: Function, specify the callback function
- args: ..., additional parameters, passed into the specified callback, optional.
Return results:
- Timer, returns the timer object
clearImmediate
Clear the specified timer
1static global.clearImmediate(Value t);
Call parameters:
- t: Value, specifies the timer to be cleared
btoa
bybase64way to encode data
1
2static String global.btoa(Buffer data,
Boolean url = false);
Call parameters:
- data:Buffer, the data to be encoded
- url: Boolean, specifies whether to useurlsafe character encoding
Return results:
- String, returns the encoded string
atob
bybase64Way to decode string into binary data
1static Buffer global.atob(String data);
Call parameters:
- data: String, the string to be decoded
Return results:
- Buffer, returns the decoded binary data
gc
Mandatory garbage collection
1static global.gc();
static properties
global
Object, global object
1static readonly Object new global;
globalThis
Object, global object
1static readonly Object global.globalThis;