Skip to content

Commit 2443340

Browse files
committed
New documentation website
1 parent d3c3403 commit 2443340

22 files changed

+15911
-1032
lines changed

doc/quickjs.texi

Lines changed: 0 additions & 1032 deletions
This file was deleted.

docs/.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Dependencies
2+
/node_modules
3+
4+
# Production
5+
/build
6+
7+
# Generated files
8+
.docusaurus
9+
.cache-loader
10+
11+
# Misc
12+
.DS_Store
13+
.env.local
14+
.env.development.local
15+
.env.test.local
16+
.env.production.local
17+
18+
npm-debug.log*
19+
yarn-debug.log*
20+
yarn-error.log*

docs/.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
18

docs/babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
3+
};

docs/docs/building.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
# Building
6+
7+
QuickJS uses [CMake] as its main build system, with an additional helper [Makefile].
8+
9+
:::note
10+
Windows users will need to run the CMake commands manually.
11+
:::
12+
13+
## Compiling everything
14+
15+
```bash
16+
make
17+
```
18+
19+
This will build the `qjs` and `qjsc` executables and other test tools. Head over [here](./cli) for
20+
instructions on how to use them.
21+
22+
## Debug builds
23+
24+
```bash
25+
make debug
26+
```
27+
28+
This will produce a debug build without optimizations, suitable for developers.
29+
30+
## Running test262
31+
32+
```bash
33+
make test262
34+
```
35+
36+
This will run the test262 suite.
37+
38+
```bash
39+
make test262-update
40+
```
41+
42+
This will run the test262 suite and update the error / pass report, useful after
43+
implementing a new feature that would alter the result of the test suite.
44+
45+
[CMake]: https://cmake.org
46+
[Makefile]: https://www.gnu.org/software/make/

docs/docs/cli.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
sidebar_position: 4
3+
---
4+
5+
# The qjs and qjsc CLI tools
6+
7+
## `qjs` - The QuickJS JavaScript interpreter
8+
9+
The `qjs` executable runs the JavaScript interpreter. It includes a simple standard
10+
library and REPL.
11+
12+
```
13+
$ qjs
14+
usage: qjs [options] [file [args]]
15+
-h --help list options
16+
-e --eval EXPR evaluate EXPR
17+
-i --interactive go to interactive mode
18+
-m --module load as ES6 module (default=autodetect)
19+
--script load as ES6 script (default=autodetect)
20+
-I --include file include an additional file
21+
--std make 'std' and 'os' available to the loaded script
22+
-T --trace trace memory allocation
23+
-d --dump dump the memory usage stats
24+
-D --dump-flags flags for dumping debug data (see DUMP_* defines)
25+
--memory-limit n limit the memory usage to 'n' Kbytes
26+
--stack-size n limit the stack size to 'n' Kbytes
27+
--unhandled-rejection dump unhandled promise rejections
28+
-q --quit just instantiate the interpreter and quit
29+
```
30+
31+
The following dump flags are supported:
32+
33+
```
34+
DUMP_BYTECODE_FINAL 0x01 /* dump pass 3 final byte code */
35+
DUMP_BYTECODE_PASS2 0x02 /* dump pass 2 code */
36+
DUMP_BYTECODE_PASS1 0x04 /* dump pass 1 code */
37+
DUMP_BYTECODE_HEX 0x10 /* dump bytecode in hex */
38+
DUMP_BYTECODE_PC2LINE 0x20 /* dump line number table */
39+
DUMP_BYTECODE_STACK 0x40 /* dump compute_stack_size */
40+
DUMP_BYTECODE_STEP 0x80 /* dump executed bytecode */
41+
DUMP_READ_OBJECT 0x100 /* dump the marshalled objects at load time */
42+
DUMP_FREE 0x200 /* dump every object free */
43+
DUMP_GC 0x400 /* dump the occurrence of the automatic GC */
44+
DUMP_GC_FREE 0x800 /* dump objects freed by the GC */
45+
DUMP_MODULE_RESOLVE 0x1000 /* dump module resolution steps */
46+
DUMP_PROMISE 0x2000 /* dump promise steps */
47+
DUMP_LEAKS 0x4000 /* dump leaked objects and strings in JS_FreeRuntime */
48+
DUMP_ATOM_LEAKS 0x8000 /* dump leaked atoms in JS_FreeRuntime */
49+
DUMP_MEM 0x10000 /* dump memory usage in JS_FreeRuntime */
50+
DUMP_OBJECTS 0x20000 /* dump objects in JS_FreeRuntime */
51+
DUMP_ATOMS 0x40000 /* dump atoms in JS_FreeRuntime */
52+
DUMP_SHAPES 0x80000 /* dump shapes in JS_FreeRuntime */
53+
```
54+
55+
## `qjsc` - The QuickJS JavaScript compiler
56+
57+
The `qjsc` executable runs the JavaScript compiler, it can generate bytecode from
58+
source files which can then be embedded in an executable, or it can generate the necessary
59+
scaffolding to build a C application which embeds QuickJS.
60+
61+
```
62+
$ qjsc
63+
usage: qjsc [options] [files]
64+
65+
options are:
66+
-b output raw bytecode instead of C code
67+
-e output main() and bytecode in a C file
68+
-o output set the output filename
69+
-n script_name set the script name (as used in stack traces)
70+
-N cname set the C name of the generated data
71+
-m compile as JavaScript module (default=autodetect)
72+
-D module_name compile a dynamically loaded module or worker
73+
-M module_name[,cname] add initialization code for an external C module
74+
-p prefix set the prefix of the generated C names
75+
-s strip the source code, specify twice to also strip debug info
76+
-S n set the maximum stack size to 'n' bytes (default=262144)
77+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"label": "Developer Guide",
3+
"position": 6,
4+
"link": {
5+
"type": "generated-index"
6+
}
7+
}

docs/docs/developer-guide/api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# API Reference
2+
3+
WIP.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Internals
2+
3+
## Bytecode
4+
5+
The compiler generates bytecode directly with no intermediate
6+
representation such as a parse tree, hence it is very fast. Several
7+
optimizations passes are done over the generated bytecode.
8+
9+
A stack-based bytecode was chosen because it is simple and generates
10+
compact code.
11+
12+
For each function, the maximum stack size is computed at compile time so that
13+
no runtime stack overflow tests are needed.
14+
15+
A separate compressed line number table is maintained for the debug
16+
information.
17+
18+
Access to closure variables is optimized and is almost as fast as local
19+
variables.
20+
21+
Direct `eval` in strict mode is optimized.
22+
23+
## Runtime
24+
25+
### Strings
26+
27+
Strings are stored either as an 8 bit or a 16 bit array of
28+
characters. Hence random access to characters is always fast.
29+
30+
The C API provides functions to convert JavaScript Strings to C UTF-8 encoded
31+
strings. The most common case where the JavaScript string contains
32+
only ASCII characters involves no copying.
33+
34+
### Objects
35+
36+
The object shapes (object prototype, property names and flags) are shared
37+
between objects to save memory.
38+
39+
Arrays with no holes (except at the end of the array) are optimized.
40+
41+
TypedArray accesses are optimized.
42+
43+
### Atoms
44+
45+
Object property names and some strings are stored as Atoms (unique
46+
strings) to save memory and allow fast comparison. Atoms are
47+
represented as a 32 bit integer. Half of the atom range is reserved for
48+
immediate integer literals from 0 to 2^31-1.
49+
50+
### Numbers
51+
52+
Numbers are represented either as 32-bit signed integers or 64-bit IEEE-754
53+
floating point values. Most operations have fast paths for the 32-bit
54+
integer case.
55+
56+
### Garbage collection
57+
58+
Reference counting is used to free objects automatically and
59+
deterministically. A separate cycle removal pass is done when the allocated
60+
memory becomes too large. The cycle removal algorithm only uses the
61+
reference counts and the object content, so no explicit garbage
62+
collection roots need to be manipulated in the C code.
63+
64+
### JSValue
65+
66+
It is a JavaScript value which can be a primitive type (such as
67+
Number, String, ...) or an Object. NaN boxing is used in the 32-bit version
68+
to store 64-bit floating point numbers. The representation is
69+
optimized so that 32-bit integers and reference counted values can be
70+
efficiently tested.
71+
72+
In 64-bit code, JSValue are 128-bit large and no NaN boxing is used. The
73+
rationale is that in 64-bit code memory usage is less critical.
74+
75+
In both cases (32 or 64 bits), JSValue exactly fits two CPU registers,
76+
so it can be efficiently returned by C functions.
77+
78+
### Function call
79+
80+
The engine is optimized so that function calls are fast. The system
81+
stack holds the JavaScript parameters and local variables.
82+
83+
### RegExp
84+
85+
A specific regular expression engine was developed. It is both small
86+
and efficient and supports all the ES2020+ features including the
87+
Unicode properties. As the JavaScript compiler, it directly generates
88+
bytecode without a parse tree.
89+
90+
Backtracking with an explicit stack is used so that there is no
91+
recursion on the system stack. Simple quantifiers are specifically
92+
optimized to avoid recursions.
93+
94+
Infinite recursions coming from quantifiers with empty terms are
95+
avoided.
96+
97+
The full regexp library weights about 15 KiB (x86 code), excluding the
98+
Unicode library.
99+
100+
### Unicode
101+
102+
A specific Unicode library was developed so that there is no
103+
dependency on an external large Unicode library such as ICU. All the
104+
Unicode tables are compressed while keeping a reasonable access
105+
speed.
106+
107+
The library supports case conversion, Unicode normalization, Unicode
108+
script queries, Unicode general category queries and all Unicode
109+
binary properties.
110+
111+
The full Unicode library weights about 45 KiB (x86 code).
112+
113+
### BigInt
114+
115+
BigInt is implemented with the [libbf](https://bellard.org/libbf) library.
116+
It weights about 90 KiB (x86 code) and provides arbitrary precision IEEE 754 floating
117+
point operations and transcendental functions with exact rounding.

docs/docs/developer-guide/intro.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# The QuickJS C API
6+
7+
The C API was designed to be simple and efficient. The C API is
8+
defined in the header `quickjs.h`.
9+
10+
## Runtime and contexts
11+
12+
`JSRuntime` represents a JavaScript runtime corresponding to an
13+
object heap. Several runtimes can exist at the same time but they
14+
cannot exchange objects. Inside a given runtime, no multi-threading is
15+
supported.
16+
17+
`JSContext` represents a JavaScript context (or Realm). Each
18+
JSContext has its own global objects and system objects. There can be
19+
several JSContexts per JSRuntime and they can share objects, similar
20+
to frames of the same origin sharing JavaScript objects in a
21+
web browser.
22+
23+
## JSValue
24+
25+
`JSValue` represents a JavaScript value which can be a primitive
26+
type or an object. Reference counting is used, so it is important to
27+
explicitly duplicate (`JS_DupValue()`, increment the reference
28+
count) or free (`JS_FreeValue()`, decrement the reference count)
29+
JSValues.
30+
31+
## C functions
32+
33+
C functions can be created with
34+
`JS_NewCFunction()`. `JS_SetPropertyFunctionList()` is a
35+
shortcut to easily add functions, setters and getters properties to a
36+
given object.
37+
38+
Unlike other embedded JavaScript engines, there is no implicit stack,
39+
so C functions get their parameters as normal C parameters. As a
40+
general rule, C functions take constant `JSValue`s as parameters
41+
(so they don't need to free them) and return a newly allocated (=live)
42+
`JSValue`.
43+
44+
## Exceptions
45+
46+
Most C functions can return a JavaScript exception. It
47+
must be explicitly tested and handled by the C code. The specific
48+
`JSValue` `JS_EXCEPTION` indicates that an exception
49+
occurred. The actual exception object is stored in the
50+
`JSContext` and can be retrieved with `JS_GetException()`.
51+
52+
## Script evaluation
53+
54+
Use `JS_Eval()` to evaluate a script or module source.
55+
56+
If the script or module was compiled to bytecode with `qjsc`, it
57+
can be evaluated by calling `js_std_eval_binary()`. The advantage
58+
is that no compilation is needed so it is faster and smaller because
59+
the compiler can be removed from the executable if no `eval` is
60+
required.
61+
62+
Note: the bytecode format is linked to a given QuickJS
63+
version. Moreover, no security check is done before its
64+
execution. Hence the bytecode should not be loaded from untrusted
65+
sources.
66+
67+
## JS Classes
68+
69+
C opaque data can be attached to a JavaScript object. The type of the
70+
C opaque data is determined with the class ID (`JSClassID`) of
71+
the object. Hence the first step is to register a new class ID and JS
72+
class (`JS_NewClassID()`, `JS_NewClass()`). Then you can
73+
create objects of this class with `JS_NewObjectClass()` and get or
74+
set the C opaque point with `JS_GetOpaque()` / `JS_SetOpaque()`.
75+
76+
When defining a new JS class, it is possible to declare a finalizer
77+
which is called when the object is destroyed. The finalizer should be
78+
used to release C resources. It is invalid to execute JS code from
79+
it. A `gc_mark` method can be provided so that the cycle removal
80+
algorithm can find the other objects referenced by this object. Other
81+
methods are available to define exotic object behaviors.
82+
83+
The Class ID are allocated per-runtime. The
84+
`JSClass` are allocated per `JSRuntime`. `JS_SetClassProto()`
85+
is used to define a prototype for a given class in a given
86+
`JSContext`. `JS_NewObjectClass()` sets this prototype in the
87+
created object.
88+
89+
Examples are available in `quickjs-libc.c`.
90+
91+
## C Modules
92+
93+
Native ES6 modules are supported and can be dynamically or statically
94+
linked. The standard library `quickjs-libc.c` is a good example
95+
of a native module.
96+
97+
## Memory handling
98+
99+
Use `JS_SetMemoryLimit()` to set a global memory allocation limit
100+
to a given `JSRuntime`.
101+
102+
Custom memory allocation functions can be provided with `JS_NewRuntime2()`.
103+
104+
The maximum system stack size can be set with `JS_SetMaxStackSize()`.
105+
106+
## Execution timeout and interrupts
107+
108+
Use `JS_SetInterruptHandler()` to set a callback which is
109+
regularly called by the engine when it is executing code. This
110+
callback can be used to implement an execution timeout.
111+
112+
It is used by the command line interpreter to implement a
113+
`Ctrl-C` handler.

0 commit comments

Comments
 (0)