Command Line Options
ckb-js-vm Command Line Options
When an on-chain script is invoked by exec
or spawn
syscalls, it can accept command line arguments. The
ckb-js-vm supports the following options to control its execution behavior:
-c <filename>
: Compile JavaScript source code to bytecode, making it more efficient for on-chain execution-e <code>
: Execute JavaScript code directly from the command line string-r <filename>
: Read and execute JavaScript code from the specified file-t <target>
: Specify the target resource cell's code_hash and hash_type in hexadecimal format-f
: Enable file system mode, which provides support for JavaScript modules and imports
Note, the -c
and -r
options can only work with ckb-debugger
. The -c
option is particularly useful for preparing
optimized bytecode as described in the previous section. When no options are specified, ckb-js-vm runs in its default
mode. These command line options provide valuable debugging capabilities during development.
Compiling JavaScript into Bytecode
The ckb-js-vm includes built-in functionality for compiling JavaScript code into bytecode, which improves execution efficiency on-chain. You can use this feature as follows:
ckb-debugger --read-file hello.js --bin build/ckb-js-vm -- -c hello.bc
This command:
- Uses
--read-file hello.js
to provide the JavaScript source file to ckb-debugger - Specifies the ckb-js-vm binary with
--bin build/ckb-js-vm
- Passes the
-c hello.bc
option to ckb-js-vm (everything after--
)
The process compiles hello.js
and outputs the bytecode to hello.bc
. The --read-file
option is specific to
ckb-debugger and allows it to read a file as a data source. Command line arguments after the --
separator are passed
directly to the on-chain script, enabling the use of the -c
compilation flag.
Note that this compilation functionality requires the ckb-debugger environment and cannot work independently.
QuickJS bytecode is version-specific and not portable between different QuickJS versions. This compilation approach ensures that generated bytecode is always compatible with the exact QuickJS version used in ckb-js-vm.