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 microtask;
54mod module;
55mod name;
56mod number;
57mod object;
58mod platform;
59mod primitive_array;
60mod primitives;
61mod private;
62mod promise;
63mod property_attribute;
64mod property_descriptor;
65mod property_filter;
66mod property_handler_flags;
67mod proxy;
68mod regexp;
69mod scope;
70mod script;
71mod script_or_module;
72mod shared_array_buffer;
73mod snapshot;
74mod string;
75mod support;
76mod symbol;
77mod template;
78mod typed_array;
79mod unbound_module_script;
80mod unbound_script;
81mod value;
82mod value_deserializer;
83mod value_serializer;
84mod wasm;
85
86pub mod crdtp;
87pub mod inspector;
88pub mod json;
89pub mod script_compiler;
90#[cfg(feature = "simdutf")]
91pub mod simdutf;
92// This module is intentionally named "V8" rather than "v8" to match the
93// C++ namespace "v8::V8".
94#[allow(non_snake_case)]
95pub mod V8;
96
97pub use array_buffer::*;
98pub use data::*;
99pub use exception::*;
100pub use external_references::ExternalReference;
101pub use function::*;
102pub use gc::*;
103pub use get_property_names_args_builder::*;
104pub use handle::Eternal;
105pub use handle::Global;
106pub use handle::Handle;
107pub use handle::Local;
108pub use handle::SealedLocal;
109pub use handle::TracedReference;
110pub use handle::Weak;
111pub use isolate::GarbageCollectionType;
112pub use isolate::HeapCodeStatistics;
113pub use isolate::HeapSpaceStatistics;
114pub use isolate::HeapStatistics;
115pub use isolate::HostCreateShadowRealmContextCallback;
116pub use isolate::HostImportModuleDynamicallyCallback;
117pub use isolate::HostImportModuleWithPhaseDynamicallyCallback;
118pub use isolate::HostInitializeImportMetaObjectCallback;
119pub use isolate::Isolate;
120pub use isolate::IsolateHandle;
121pub use isolate::MemoryPressureLevel;
122pub use isolate::MessageCallback;
123pub use isolate::MessageErrorLevel;
124pub use isolate::MicrotasksPolicy;
125pub use isolate::ModuleImportPhase;
126pub use isolate::NearHeapLimitCallback;
127pub use isolate::OomDetails;
128pub use isolate::OomErrorCallback;
129pub use isolate::OwnedIsolate;
130pub use isolate::PromiseHook;
131pub use isolate::PromiseHookType;
132pub use isolate::PromiseRejectCallback;
133pub use isolate::RealIsolate;
134pub use isolate::TimeZoneDetection;
135pub use isolate::UseCounterCallback;
136pub use isolate::UseCounterFeature;
137pub use isolate::WasmAsyncSuccess;
138pub use isolate_create_params::CreateParams;
139pub use microtask::MicrotaskQueue;
140pub use module::*;
141pub use object::*;
142pub use platform::IdleTask;
143pub use platform::Platform;
144pub use platform::PlatformImpl;
145pub use platform::Task;
146pub use platform::new_custom_platform;
147pub use platform::new_default_platform;
148pub use platform::new_single_threaded_default_platform;
149pub use platform::new_unprotected_default_platform;
150pub use primitives::*;
151pub use promise::{PromiseRejectEvent, PromiseRejectMessage, PromiseState};
152pub use property_attribute::*;
153pub use property_descriptor::*;
154pub use property_filter::*;
155pub use property_handler_flags::*;
156pub use regexp::RegExpCreationFlags;
157pub use scope::AllowJavascriptExecutionScope;
158// pub use scope::CallbackScope;
159pub use scope::CallbackScope;
160pub use scope::ContextScope;
161pub use scope::DisallowJavascriptExecutionScope;
162pub use scope::EscapableHandleScope;
163pub use scope::PinCallbackScope;
164pub use scope::PinScope;
165pub use scope::PinnedRef;
166pub use scope::ScopeStorage;
167// pub use scope::HandleScope;
168pub use isolate::UnsafeRawIsolatePtr;
169pub use scope::HandleScope;
170pub use scope::OnFailure;
171pub use scope::TryCatch;
172pub use script::ScriptOrigin;
173pub use script_compiler::CachedData;
174pub use snapshot::FunctionCodeHandling;
175pub use snapshot::StartupData;
176pub use string::Encoding;
177pub use string::NewStringType;
178pub use string::OneByteConst;
179pub use string::ValueView;
180pub use string::ValueViewData;
181pub use string::WriteFlags;
182pub use string::WriteOptions;
183pub use string::latin1_to_utf8;
184pub use support::SharedPtr;
185pub use support::SharedRef;
186pub use support::UniquePtr;
187pub use support::UniqueRef;
188pub use template::*;
189pub use value_deserializer::ValueDeserializer;
190pub use value_deserializer::ValueDeserializerHelper;
191pub use value_deserializer::ValueDeserializerImpl;
192pub use value_serializer::ValueSerializer;
193pub use value_serializer::ValueSerializerHelper;
194pub use value_serializer::ValueSerializerImpl;
195pub use wasm::CompiledWasmModule;
196pub use wasm::ModuleCachingInterface;
197pub use wasm::WasmModuleCompilation;
198pub use wasm::WasmStreaming;
199
200/// https://v8.dev/docs/version-numbers
201pub const MAJOR_VERSION: u32 = binding::v8__MAJOR_VERSION;
202/// https://v8.dev/docs/version-numbers
203pub const MINOR_VERSION: u32 = binding::v8__MINOR_VERSION;
204/// https://v8.dev/docs/version-numbers
205pub const BUILD_NUMBER: u32 = binding::v8__BUILD_NUMBER;
206/// https://v8.dev/docs/version-numbers
207pub const PATCH_LEVEL: u32 = binding::v8__PATCH_LEVEL;
208/// https://v8.dev/docs/version-numbers
209pub const VERSION_STRING: &str =
210  // TODO: cleanup when Result::unwrap is const stable.
211  match binding::v8__VERSION_STRING.to_str() {
212    Ok(v) => v,
213    Err(_) => panic!("Unable to convert CStr to &str??"),
214  };
215
216// TODO(piscisaureus): Ideally this trait would not be exported.
217pub use support::MapFnTo;
218
219pub const TYPED_ARRAY_MAX_SIZE_IN_HEAP: usize =
220  binding::v8__TYPED_ARRAY_MAX_SIZE_IN_HEAP as _;
221
222#[cfg(test)]
223#[allow(unused)]
224pub(crate) fn initialize_v8() {
225  use std::sync::Once;
226
227  static INIT: Once = Once::new();
228  INIT.call_once(|| {
229    V8::initialize_platform(new_default_platform(0, false).make_shared());
230    V8::initialize();
231  });
232}