Module base module

module global

The global object, the base object that all scripts can access

object

Buffer

binary data cache object forioData processing for read and write, seeBufferobject.

1
Buffer global.Buffer;

TextDecoder

TextDecoderdecode object, seeTextDecoderobject.

1
TextDecoder global.TextDecoder;

TextEncoder

TextEncoderencoding object, seeTextEncoderobject.

1
TextEncoder global.TextEncoder;

console

console access object

1
console global.console;

process

process object

1
process global.process;

static function

run

run a script

1 2
static global.run(String fname, Array argv = []);

Call parameters:

  • fname: String, specifies the path of the script to run
  • argv: Array, specify the parameters to run, this parameter can be obtained using argv in the script

require

Loads a module and returns the module object, see @ref module for more information

1
static Value global.require(String id);

Call parameters:

  • id: String, specifies the name of the module to load

Return result:

  • Value, returns the exported object of the loaded module

require can be used to load base modules, file modules.

The basic module is the module initialized when the sandbox is created, and you only need to pass the corresponding id when referencing, such as require("net").

The file module is a user-defined module, and a relative path starting with ./ or ../ should 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 the main in package.json, and 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 searches node_modules under the path where the current module is located, and recurses in the upper directory.

The basic process is as follows:

%0 start start is_native is internal module? start->is_native resolve path.resolve has_file module exists? resolve->has_file search recursive lookup node_modules from the current path search->has_file load load end end load->end is_native->end Yes is_mod is module? is_native->is_mod No is_mod->search Yes is_abs is absolute? is_mod->is_abs No is_abs->resolve No is_abs->has_file Yes has_file->load Yes has_ext module.js exists? has_file->has_ext No has_ext->load Yes has_package /package.json exists? has_ext->has_package No has_main main exists? has_package->has_main Yes has_index index.js exists? has_package->has_index No has_main->load Yes has_main->has_index No has_index->load Yes has_index->end No

setTimeout

call a function after a specified time

1 2 3
static Timer global.setTimeout(Function callback, Number timeout = 1, ...args);

Call parameters:

  • callback: Function, specifies 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 result:

  • Timer, returns the timer object

clearTimeout

clear the specified timer

1
static global.clearTimeout(Value t);

Call parameters:

  • t: Value, specifies the timer to clear

setInterval

Call the function after every specified time interval

1 2 3
static Timer global.setInterval(Function callback, Number timeout, ...args);

Call parameters:

  • callback: Function, specifies the callback function
  • timeout: Number, Time to specify the interval, in milliseconds. If it exceeds 2^31, it will be executed immediately.
  • args: ..., additional parameters, passed into the specified callback, optional.

Return result:

  • Timer, returns the timer object

clearInterval

clear the specified timer

1
static global.clearInterval(Value t);

Call parameters:

  • t: Value, specifies the timer to clear

setHrInterval

The function is called after a specified time interval. This is a high-precision timer that will actively interrupt the execution timer of the running JavaScript script.

1 2 3
static Timer global.setHrInterval(Function callback, Number timeout, ...args);

Call parameters:

  • callback: Function, specifies the callback function
  • timeout: Number, Time to specify the interval, in milliseconds. If it exceeds 2^31, it will be executed immediately.
  • args: ..., additional parameters, passed into the specified callback, optional.

Return result:

  • Timer, returns the timer object

Since the timer of setHrInterval will interrupt the running code to execute the callback, do not modify the data that may affect other modules in the callback function, or call any api function marked as async in the callback, otherwise it will produce unpredictable results. E.g:

1 2 3 4 5 6 7 8 9 10
var timers = require('timers'); var cnt = 0; timers.setHrInterval(() => { cnt++; }, 100); while (cnt < 10); console.error("===============================> done");

In this code, the loop on line 8 does not end because cnt changes, because JavaScript optimizes the code and assumes that cnt will not be changed during the loop.


clearHrInterval

clear the specified timer

1
static global.clearHrInterval(Value t);

Call parameters:

  • t: Value, specifies the timer to clear

setImmediate

The callback function is executed immediately at the next idle time

1 2
static Timer global.setImmediate(Function callback, ...args);

Call parameters:

  • callback: Function, specifies the callback function
  • args: ..., additional parameters, passed into the specified callback, optional.

Return result:

  • Timer, returns the timer object

clearImmediate

clear the specified timer

1
static global.clearImmediate(Value t);

Call parameters:

  • t: Value, specifies the timer to clear

btoa

bybase64way to encode data

1 2
static 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 result:

  • String, returns the encoded string

atob

bybase64way to decode string to binary data

1
static Buffer global.atob(String data);

Call parameters:

  • data: String, the string to decode

Return result:

  • Buffer, returns the decoded binary data

GC

Mandatory Garbage Collection

1
static global.GC();

static properties

Master

Worker,Workerhost object, only ifWorkerEntry script is valid

1
static readonly Worker global.Master;

global

Object, the global object

1
static readonly Object new global;

argv

Array, get the running parameters of the current script, start js to get the process startup parameters, and the script executed by run get the passed parameters

1
static readonly Array global.argv;

__filename

String, the current script filename

1
static readonly String global.__filename;

__dirname

String, the directory where the current script is located

1
static readonly String global.__dirname;