Bytecode : Bytecode compiler
This module is a JSRE bytecode compiler, which can compile js
files into JSRE engine bytecode virtual machine executable file format jsc
. The loading speed of the jsc
module is much faster than the js
module.
User can use the following code to import the bytecode
module.
var bytecode = require('bytecode');
Support
The following shows bytecode
module APIs available for each permissions.
User Mode | Privilege Mode | |
---|---|---|
bytecode.compile | ● | |
bytecode.run | ● | |
bytecode.load | ● |
Bytecode Object
bytecode.compile(path[, source[ args]])
path
{String} The path of the js file to be compiled.source
{String} The content of the js file that needs to be compiled. deafult: read the contents of the file specified bypath
.args
{String} If you are compiling a function, you can specify the arguments of the function. default: undefined.- Returns: {Buffer} Compile the generated bytecode buffer.
This method can compile js files or js functions, and return the result of the compilation, if an error occurs, an exception will be thrown.
When JSRE loads modules, it will first try to load jsc
bytecode files in the same directory.
Example
- Compile file:
var source =
`
(function() {
console.log('Hello Compiler!');
})();
`;
var bc = bytecode.compile('test', source);
bytecode.run(bc);
- Compile function:
var source =
`
return a + b;
`;
var args = 'a, b';
var bc = bytecode.compile('test', source, args);
var func = bytecode.load(bc);
console.log(func(1, 1));
- Compile module:
var source =
`
module.exports.func = function(a, b) {
return a + b;
}
`;
var args = 'exports, require, module, native';
var bc = bytecode.compile('./mtest.js', source, args);
fs.writeFile('./mtest.js', source);
fs.writeFile('./mtest.jsc', bc);
var mtest = require('./mtest');
console.log(mtest.func(1, 1));
bytecode.run(bc)
bc
{Buffer} Compiled bytecode buffer.- Returns: {Any} Bytecode execution return value.
Execute a previously compiled bytecode program.
bytecode.load(bc)
bc
{Buffer} Compiled bytecode buffer.- Returns: {Function} Bytecode function object reference.
Get bytecode function reference.