quickjs/intrinsics.rs
1//! `JS_AddIntrinsic*`: installing the built-in objects a context needs.
2//!
3//! # The one structural fact
4//!
5//! This engine installs every intrinsic when a context's interpreter is built
6//! (`builtins::install`), so a context that exists at all already has them. The reference
7//! splits installation into sixteen calls because it wants a *reduced* context — `JS_NewContextRaw`
8//! plus a chosen subset — and because a context it never evaluates in must cost nothing.
9//!
10//! So these functions are all the same function, and they answer `0` (success). That is not a
11//! stub: it is what "the intrinsic is installed" means here, and the alternative — a
12//! lazily-filled realm that can be half-installed — would add a state to the engine that no
13//! script can observe and that every builtin would have to check.
14//!
15//! # `JS_AddIntrinsicIntl` and `JS_AddIntrinsicTemporal` answer `-1`
16//!
17//! Those are the two the engine genuinely does not have: `Intl` needs a locale database and a
18//! collator, and `Temporal` is M13 (unstarted). Answering success would let a host believe a
19//! script can use `Intl.NumberFormat`, and the script's `TypeError` would then look like a
20//! bug in the host rather than a missing feature in the library.
21
22use crate::runtime::JsContext;
23
24/// The answer every installed intrinsic gives: `0`, meaning success.
25fn installed(_ctx: *mut JsContext) -> core::ffi::c_int {
26 0
27}
28
29/// `JS_AddIntrinsicBaseObjects`: `Object`, `Function`, `Array`, `String`, `Number`, …
30#[unsafe(no_mangle)]
31pub extern "C" fn JS_AddIntrinsicBaseObjects(ctx: *mut JsContext) -> core::ffi::c_int {
32 installed(ctx)
33}
34
35/// `JS_AddIntrinsicDate`: `Date`.
36#[unsafe(no_mangle)]
37pub extern "C" fn JS_AddIntrinsicDate(ctx: *mut JsContext) -> core::ffi::c_int {
38 installed(ctx)
39}
40
41/// `JS_AddIntrinsicEval`: `eval` and `Function`'s runtime compilation.
42#[unsafe(no_mangle)]
43pub extern "C" fn JS_AddIntrinsicEval(ctx: *mut JsContext) -> core::ffi::c_int {
44 installed(ctx)
45}
46
47/// `JS_AddIntrinsicStringNormalize`: `String.prototype.normalize`.
48#[unsafe(no_mangle)]
49pub extern "C" fn JS_AddIntrinsicStringNormalize(ctx: *mut JsContext) -> core::ffi::c_int {
50 installed(ctx)
51}
52
53/// `JS_AddIntrinsicRegExp`: `RegExp`.
54#[unsafe(no_mangle)]
55pub extern "C" fn JS_AddIntrinsicRegExp(ctx: *mut JsContext) -> core::ffi::c_int {
56 installed(ctx)
57}
58
59/// `JS_AddIntrinsicRegExpCompiler`: the regexp-to-bytecode compiler.
60///
61/// Void because the reference has no way to fail it; the engine's regexp engine is a
62/// backtracking matcher over the parsed pattern, so there is nothing to compile.
63#[unsafe(no_mangle)]
64pub extern "C" fn JS_AddIntrinsicRegExpCompiler(_ctx: *mut JsContext) {}
65
66/// `JS_AddIntrinsicJSON`: `JSON`.
67#[unsafe(no_mangle)]
68pub extern "C" fn JS_AddIntrinsicJSON(ctx: *mut JsContext) -> core::ffi::c_int {
69 installed(ctx)
70}
71
72/// `JS_AddIntrinsicProxy`: `Proxy` and `Reflect`.
73#[unsafe(no_mangle)]
74pub extern "C" fn JS_AddIntrinsicProxy(ctx: *mut JsContext) -> core::ffi::c_int {
75 installed(ctx)
76}
77
78/// `JS_AddIntrinsicMapSet`: `Map`, `Set`, `WeakMap`, `WeakSet`.
79#[unsafe(no_mangle)]
80pub extern "C" fn JS_AddIntrinsicMapSet(ctx: *mut JsContext) -> core::ffi::c_int {
81 installed(ctx)
82}
83
84/// `JS_AddIntrinsicTypedArrays`: `ArrayBuffer`, the views and `DataView`.
85#[unsafe(no_mangle)]
86pub extern "C" fn JS_AddIntrinsicTypedArrays(ctx: *mut JsContext) -> core::ffi::c_int {
87 installed(ctx)
88}
89
90/// `JS_AddIntrinsicPromise`: `Promise`, and `Symbol.asyncIterator`'s machinery.
91#[unsafe(no_mangle)]
92pub extern "C" fn JS_AddIntrinsicPromise(ctx: *mut JsContext) -> core::ffi::c_int {
93 installed(ctx)
94}
95
96/// `JS_AddIntrinsicWeakRef`: `WeakRef` and `FinalizationRegistry`.
97#[unsafe(no_mangle)]
98pub extern "C" fn JS_AddIntrinsicWeakRef(ctx: *mut JsContext) -> core::ffi::c_int {
99 installed(ctx)
100}
101
102/// `JS_AddIntrinsicIntl`: `Intl`, which this engine does not have.
103///
104/// `-1` is the reference's failure answer, and the context is left untouched.
105#[unsafe(no_mangle)]
106pub extern "C" fn JS_AddIntrinsicIntl(_ctx: *mut JsContext) -> core::ffi::c_int {
107 -1
108}
109
110/// `JS_AddIntrinsicTemporal`: `Temporal`, which is M13 and unstarted.
111#[unsafe(no_mangle)]
112pub extern "C" fn JS_AddIntrinsicTemporal(_ctx: *mut JsContext) -> core::ffi::c_int {
113 -1
114}
115
116/// `JS_SetLocaleCallbacks`: install a locale implementation.
117///
118/// There is no place to put one — nothing in the engine consults locale callbacks, because
119/// there is no `Intl` and `toLocaleString` falls back to `toString` — so the pointer is
120/// accepted and dropped. A host that installs one gets the default behaviour, which the README
121/// records next to `JS_AddIntrinsicIntl`.
122///
123/// # Safety
124///
125/// `callbacks` is ignored.
126#[unsafe(no_mangle)]
127pub unsafe extern "C" fn JS_SetLocaleCallbacks(
128 _ctx: *mut JsContext,
129 _callbacks: *const core::ffi::c_void,
130) {
131}