All Projects → OsamaAbbas → Bytenode

OsamaAbbas / Bytenode

Licence: mit
A minimalist bytecode compiler for Node.js

Programming Languages

javascript
184084 projects - #8 most used programming language
bytecode
52 projects

Projects that are alternatives of or similar to Bytenode

Wasp
A programming language that understands what a web app is.
Stars: ✭ 940 (-7.11%)
Mutual labels:  compiler
Rustiny
A Rust-like language compiling to x86-64 assembler
Stars: ✭ 34 (-96.64%)
Mutual labels:  compiler
The Hack General Purpose Computer
Using HDL, from Boolean algebra and elementary logic gates to building a Central Processing Unit, a memory system, and a hardware platform, leading up to a 16-bit general-purpose computer. Then, implementing the modern software hierarchy designed to enable the translation and execution of object-based, high-level languages on a bare-bone computer hardware platform; Including Virtual machine,Compiler and Operating system.
Stars: ✭ 39 (-96.15%)
Mutual labels:  compiler
Monomorphist
monomorphist - a JavaScript performance companion
Stars: ✭ 30 (-97.04%)
Mutual labels:  v8
Rust V8worker2
Minimal Rust binding to V8 (based on ry/libv8worker2)
Stars: ✭ 33 (-96.74%)
Mutual labels:  v8
Mini Haskell
A self-hosting mini Haskell compiler with a mini C runtime.
Stars: ✭ 37 (-96.34%)
Mutual labels:  compiler
Rum
💀 Compiler for the Rum language
Stars: ✭ 21 (-97.92%)
Mutual labels:  compiler
Mlml
self-hosted compiler for a subset of OCaml
Stars: ✭ 41 (-95.95%)
Mutual labels:  compiler
Mooncraft
Lua to Commandblock compiler
Stars: ✭ 33 (-96.74%)
Mutual labels:  compiler
Llvm Tutorial Standalone
DEPRECATED (Use: https://github.com/llvm-hs/llvm-hs-kaleidoscope )
Stars: ✭ 38 (-96.25%)
Mutual labels:  compiler
V8 Cffi
Embed the V8 Javascript Interpreter into Python
Stars: ✭ 31 (-96.94%)
Mutual labels:  v8
Unlisp Llvm
Compiler for a toy Lisp language
Stars: ✭ 33 (-96.74%)
Mutual labels:  compiler
Smallerc
Simple C compiler
Stars: ✭ 986 (-2.57%)
Mutual labels:  compiler
Write A Programming Language
How to make a new language(and why we shouldn't?)
Stars: ✭ 29 (-97.13%)
Mutual labels:  compiler
Iotz
compile things easy 🚀
Stars: ✭ 39 (-96.15%)
Mutual labels:  compiler
Sml Compiler
A compiler for Standard ML, somewhat
Stars: ✭ 22 (-97.83%)
Mutual labels:  compiler
Compcert
The CompCert formally-verified C compiler
Stars: ✭ 984 (-2.77%)
Mutual labels:  compiler
Antlr4 Calculator
Simple antlr4 calculator.
Stars: ✭ 40 (-96.05%)
Mutual labels:  compiler
Nativejit
A C++ expression -> x64 JIT
Stars: ✭ 999 (-1.28%)
Mutual labels:  compiler
Rascal
A simple Pascal interpreter written in rust.
Stars: ✭ 38 (-96.25%)
Mutual labels:  compiler

Bytenode

A minimalist bytecode compiler for Node.js.

This tool truly compiles your JavaScript code into V8 bytecode, so that you can protect your source code. It can be used with Node.js >= 5.7.x, as well as Electron and NW.js (check examples/ directory).


Install

npm install --save bytenode

Or globally:

sudo npm install -g bytenode

Known Issues and Limitations

  • In Node 10.x, Bytenode does not work in debug mode. See #29.

  • Any code depends on Function.prototype.toString function will break, because Bytenode remove the source code from .jsc files and put a dummy code instead. See #34.

  • In recent versions of Node, the --no-flush-bytecode must be set. Bytenode sets it internally, but if you encounter any issues, try to run Node with that flag: $ node --no-flush-bytecode server.js. See #41.

  • Async arrow functions cause crash in Electron apps if used in render processes. See #47.


Bytenode CLI

  Usage: bytenode [option] [ FILE... | - ] [arguments]

  Options:
    -h, --help                        show help information.
    -v, --version                     show bytenode version.

    -c, --compile [ FILE... | - ]     compile stdin, a file, or a list of files
    -n, --no-module                   compile without producing commonjs module
    -e, --electron                    compile for Electron

  Examples:

  $ bytenode -c script.js             compile `script.js` to `script.jsc`.
  $ bytenode -c server.js app.js
  $ bytenode -c src/*.js              compile all `.js` files in `src/` directory.

  $ bytenode script.jsc [arguments]   run `script.jsc` with arguments.
  $ bytenode                          open Node REPL with bytenode pre-loaded.

Examples:

  • Compile express-server.js to express-server.jsc.
[email protected]:~$ bytenode --compile express-server.js
  • Run your compiled file express-server.jsc.
[email protected]:~$ bytenode express-server.jsc
Server listening on port 3000
  • Compile all .js files in ./app directory.
[email protected]:~$ bytenode --compile ./app/*.js
  • Compile all .js files in your project.
[email protected]:~$ bytenode --compile ./**/*.js

Note: you may need to enable globstar option in bash (you should add it to ~/.bashrc): shopt -s globstar

  • Starting from v1.0.0, bytenode can compile from stdin.
$ echo 'console.log("Hello");' | bytenode --compile - > hello.jsc

Bytenode API

You have to run node with this flag node --no-lazy in order to compile all functions in your code eagerly. Or, if you have no control on how your code will be run, you can use v8.setFlagsFromString('--no-lazy') function as well.

const bytenode = require('bytenode');

bytenode.compileCode(javascriptCode) → {Buffer}

Generates v8 bytecode buffer.

  • Parameters:
Name Type Description
javascriptCode string JavaScript source that will be compiled to bytecode.
  • Returns:

{Buffer} The generated bytecode.

  • Example:
let helloWorldBytecode = bytenode.compileCode(`console.log('Hello World!');`);

This helloWorldBytecode bytecode can be saved to a file. However, if you want to use your code as a module (i.e. if your file has some exports), you have to compile it using bytenode.compileFile({compileAsModule: true}), or wrap your code manually, using Module.wrap() function.


bytenode.compileElectronCode(javascriptCode) → {Promise<Buffer>}

Asynchronous function which generates v8 bytecode buffer for Electron.

Same as bytenode.compileCode(), but generates bytecode for the version of Electron currently installed in node_modules.

  • Parameters:
Name Type Description
javascriptCode string JavaScript source that will be compiled to bytecode.
  • Returns:

{Promise<Buffer>} A Promise which resolves with the generated bytecode.

  • Example:
let helloWorldBytecode = await bytenode.compileElectronCode(`console.log('Hello World!');`);

This helloWorldBytecode bytecode can be saved to a file. However, if you want to use your code as a module (i.e. if your file has some exports), you have to compile it using bytenode.compileFile({compileAsModule: true}), or wrap your code manually, using Module.wrap() function.


bytenode.runBytecode(bytecodeBuffer) → {any}

Runs v8 bytecode buffer and returns the result.

  • Parameters:
Name Type Description
bytecodeBuffer Buffer The buffer object that was created using compileCode function.
  • Returns:

{any} The result of the very last statement executed in the script.

  • Example:
bytenode.runBytecode(helloWorldBytecode);
// prints: Hello World!

bytenode.compileFile(args, output) → {Promise<string>}

Asyncrhonous function which compiles JavaScript file to .jsc file.

  • Parameters:
Name Type Description
args object | string
args.filename string The JavaScript source file that will be compiled.
args.compileAsModule boolean If true, the output will be a commonjs module. Default: true.
args.electron boolean If true, the output will be a compiled through Electrong. Default: false.
args.output string The output filename. Defaults to the same path and name of the original file, but with .jsc extension.
output string The output filename. (Deprecated: use args.output instead)
  • Returns:

{Promise<string>}: A Promise that resolves as the compiled filename.

  • Examples:
let compiledFilename = bytenode.compileFile({
  filename: '/path/to/your/file.js',
  output: '/path/to/compiled/file.jsc' // if omitted, it defaults to '/path/to/your/file.jsc'
});

Previous code will produce a commonjs module that can be required using require function.

let compiledFilename = await bytenode.compileFile({
  filename: '/path/to/your/file.js',
  output: '/path/to/compiled/file.jsc',
  compileAsModule: false
});

Previous code will produce a direct .jsc file, that can be run using bytenode.runBytecodeFile() function. It can NOT be required as a module. Please note that compileAsModule MUST be false in order to turn it off. Any other values (including: null, "", etc) will be treated as true. (It had to be done this way in order to keep the old code valid.)


bytenode.runBytecodeFile(filename) → {any}

Runs .jsc file and returns the result.

  • Parameters:
Name Type
filename string
  • Returns:

{any} The result of the very last statement executed in the script.

  • Example:
// test.js
console.log('Hello World!');
bytenode.runBytecodeFile('/path/to/test.jsc');
// prints: Hello World!

require(filename) → {any}

  • Parameters:
Name Type
filename string
  • Returns:

{any} exported module content

  • Example:
let myModule = require('/path/to/your/file.jsc');

Just like regular .js modules. You can also omit the extension .jsc.

.jsc file must have been compiled using bytenode.compileFile(), or have been wrapped inside Module.wrap() function. Otherwise it won't work as a module and it can NOT be required.

Please note .jsc files must run with the same Node.js version that was used to compile it (using same architecture of course). Also, .jsc files are CPU-agnostic. However, you should run your tests before and after deployment, because V8 sanity checks include some checks related to CPU supported features, so this may cause errors in some rare cases.


Acknowledgements

I had the idea of this tool many years ago. However, I finally decided to implement it after seeing this issue by @hashseed. Also, some parts was inspired by v8-compile-cache by @zertosh.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].