Skip to main content

v8/
lib.rs

1// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.
2
3//! # Example
4//!
5//! ```rust
6//! let platform = v8::new_default_platform(0, false).make_shared();
7//! v8::V8::initialize_platform(platform);
8//! v8::V8::initialize();
9//!
10//! let isolate = &mut v8::Isolate::new(Default::default());
11//!
12//! let scope = std::pin::pin!(v8::HandleScope::new(isolate));
13//! let scope = &mut scope.init();
14//! let context = v8::Context::new(scope, Default::default());
15//! let scope = &mut v8::ContextScope::new(scope, context);
16//!
17//! let code = v8::String::new(scope, "'Hello' + ' World!'").unwrap();
18//! println!("javascript code: {}", code.to_rust_string_lossy(scope));
19//!
20//! let script = v8::Script::compile(scope, code, None).unwrap();
21//! let result = script.run(scope).unwrap();
22//! let result = result.to_string(scope).unwrap();
23//! println!("result: {}", result.to_rust_string_lossy(scope));
24//! ```
25
26#![allow(clippy::missing_safety_doc)]
27
28#[macro_use]
29extern crate bitflags;
30extern crate temporal_capi;
31
32mod array_buffer;
33mod array_buffer_view;
34mod bigint;
35mod binding;
36mod context;
37pub use context::ContextOptions;
38pub mod cppgc;
39mod data;
40mod date;
41mod exception;
42mod external;
43mod external_references;
44pub mod fast_api;
45mod fixed_array;
46mod function;
47mod gc;
48mod get_property_names_args_builder;
49mod handle;
50pub mod icu;
51mod isolate;
52mod isolate_create_params;
53mod locker;
54mod microtask;
55mod module;
56mod name;
57mod number;
58mod object;
59mod platform;
60mod primitive_array;
61mod primitives;
62mod private;
63mod promise;
64mod property_attribute;
65mod property_descriptor;
66mod property_filter;
67mod property_handler_flags;
68mod proxy;
69mod regexp;
70mod scope;
71mod script;
72mod script_or_module;
73mod shared_array_buffer;
74mod snapshot;
75mod string;
76mod support;
77mod symbol;
78mod template;
79mod typed_array;
80mod unbound_module_script;
81mod unbound_script;
82mod unlocker;
83mod value;
84mod value_deserializer;
85mod value_serializer;
86mod wasm;
87
88pub mod inspector;
89pub mod json;
90pub mod script_compiler;
91// This module is intentionally named "V8" rather than "v8" to match the
92// C++ namespace "v8::V8".
93#[allow(non_snake_case)]
94pub mod V8;
95
96pub use array_buffer::*;
97pub use data::*;
98pub use exception::*;
99pub use external_references::ExternalReference;
100pub use function::*;
101pub use gc::*;
102pub use get_property_names_args_builder::*;
103pub use handle::Eternal;
104pub use handle::Global;
105pub use handle::Handle;
106pub use handle::Local;
107pub use handle::SealedLocal;
108pub use handle::TracedReference;
109pub use handle::Weak;
110pub use isolate::GarbageCollectionType;
111pub use isolate::HeapSpaceStatistics;
112pub use isolate::HeapStatistics;
113pub use isolate::HostCreateShadowRealmContextCallback;
114pub use isolate::HostImportModuleDynamicallyCallback;
115pub use isolate::HostImportModuleWithPhaseDynamicallyCallback;
116pub use isolate::HostInitializeImportMetaObjectCallback;
117pub use isolate::Isolate;
118pub use isolate::IsolateHandle;
119pub use isolate::MemoryPressureLevel;
120pub use isolate::MessageCallback;
121pub use isolate::MessageErrorLevel;
122pub use isolate::MicrotasksPolicy;
123pub use isolate::ModuleImportPhase;
124pub use isolate::NearHeapLimitCallback;
125pub use isolate::OomDetails;
126pub use isolate::OomErrorCallback;
127pub use isolate::OwnedIsolate;
128pub use isolate::PromiseHook;
129pub use isolate::PromiseHookType;
130pub use isolate::PromiseRejectCallback;
131pub use isolate::RealIsolate;
132pub use isolate::TimeZoneDetection;
133pub use isolate::UseCounterCallback;
134pub use isolate::UseCounterFeature;
135pub use isolate::WasmAsyncSuccess;
136pub use isolate::current_raw_isolate_ptr;
137pub use isolate_create_params::CreateParams;
138pub use locker::Locker;
139pub use microtask::MicrotaskQueue;
140pub use module::*;
141pub use object::*;
142pub use platform::Platform;
143pub use platform::new_default_platform;
144pub use platform::new_single_threaded_default_platform;
145pub use platform::new_unprotected_default_platform;
146pub use primitives::*;
147pub use promise::{PromiseRejectEvent, PromiseRejectMessage, PromiseState};
148pub use property_attribute::*;
149pub use property_descriptor::*;
150pub use property_filter::*;
151pub use property_handler_flags::*;
152pub use regexp::RegExpCreationFlags;
153pub use scope::AllowJavascriptExecutionScope;
154pub use unlocker::Unlocker;
155// pub use scope::CallbackScope;
156pub use scope::CallbackScope;
157pub use scope::ContextScope;
158pub use scope::DisallowJavascriptExecutionScope;
159pub use scope::EscapableHandleScope;
160pub use scope::PinCallbackScope;
161pub use scope::PinScope;
162pub use scope::PinnedRef;
163pub use scope::ScopeStorage;
164// pub use scope::HandleScope;
165pub use isolate::UnsafeRawIsolatePtr;
166pub use scope::HandleScope;
167pub use scope::OnFailure;
168pub use scope::TryCatch;
169pub use script::ScriptOrigin;
170pub use script_compiler::CachedData;
171pub use snapshot::FunctionCodeHandling;
172pub use snapshot::StartupData;
173pub use string::Encoding;
174pub use string::NewStringType;
175pub use string::OneByteConst;
176pub use string::ValueView;
177pub use string::ValueViewData;
178pub use string::WriteFlags;
179pub use string::WriteOptions;
180pub use support::SharedPtr;
181pub use support::SharedRef;
182pub use support::UniquePtr;
183pub use support::UniqueRef;
184pub use template::*;
185pub use value_deserializer::ValueDeserializer;
186pub use value_deserializer::ValueDeserializerHelper;
187pub use value_deserializer::ValueDeserializerImpl;
188pub use value_serializer::ValueSerializer;
189pub use value_serializer::ValueSerializerHelper;
190pub use value_serializer::ValueSerializerImpl;
191pub use wasm::CompiledWasmModule;
192pub use wasm::WasmStreaming;
193
194/// https://v8.dev/docs/version-numbers
195pub const MAJOR_VERSION: u32 = binding::v8__MAJOR_VERSION;
196/// https://v8.dev/docs/version-numbers
197pub const MINOR_VERSION: u32 = binding::v8__MINOR_VERSION;
198/// https://v8.dev/docs/version-numbers
199pub const BUILD_NUMBER: u32 = binding::v8__BUILD_NUMBER;
200/// https://v8.dev/docs/version-numbers
201pub const PATCH_LEVEL: u32 = binding::v8__PATCH_LEVEL;
202/// https://v8.dev/docs/version-numbers
203pub const VERSION_STRING: &str =
204  // TODO: cleanup when Result::unwrap is const stable.
205  match binding::v8__VERSION_STRING.to_str() {
206    Ok(v) => v,
207    Err(_) => panic!("Unable to convert CStr to &str??"),
208  };
209
210// TODO(piscisaureus): Ideally this trait would not be exported.
211pub use support::MapFnTo;
212
213pub const TYPED_ARRAY_MAX_SIZE_IN_HEAP: usize =
214  binding::v8__TYPED_ARRAY_MAX_SIZE_IN_HEAP as _;
215
216#[cfg(test)]
217#[allow(unused)]
218pub(crate) fn initialize_v8() {
219  use std::sync::Once;
220
221  static INIT: Once = Once::new();
222  INIT.call_once(|| {
223    V8::initialize_platform(new_default_platform(0, false).make_shared());
224    V8::initialize();
225  });
226}