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 inspector;
87pub mod json;
88pub mod script_compiler;
89// This module is intentionally named "V8" rather than "v8" to match the
90// C++ namespace "v8::V8".
91#[allow(non_snake_case)]
92pub mod V8;
93
94pub use array_buffer::*;
95pub use data::*;
96pub use exception::*;
97pub use external_references::ExternalReference;
98pub use function::*;
99pub use gc::*;
100pub use get_property_names_args_builder::*;
101pub use handle::Eternal;
102pub use handle::Global;
103pub use handle::Handle;
104pub use handle::Local;
105pub use handle::SealedLocal;
106pub use handle::TracedReference;
107pub use handle::Weak;
108pub use isolate::GarbageCollectionType;
109pub use isolate::HeapCodeStatistics;
110pub use isolate::HeapSpaceStatistics;
111pub use isolate::HeapStatistics;
112pub use isolate::HostCreateShadowRealmContextCallback;
113pub use isolate::HostImportModuleDynamicallyCallback;
114pub use isolate::HostImportModuleWithPhaseDynamicallyCallback;
115pub use isolate::HostInitializeImportMetaObjectCallback;
116pub use isolate::Isolate;
117pub use isolate::IsolateHandle;
118pub use isolate::Locker;
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::UnenteredIsolate;
134pub use isolate::UseCounterCallback;
135pub use isolate::UseCounterFeature;
136pub use isolate::WasmAsyncSuccess;
137pub use isolate_create_params::CreateParams;
138pub use microtask::MicrotaskQueue;
139pub use module::*;
140pub use object::*;
141pub use platform::Platform;
142pub use platform::PlatformImpl;
143pub use platform::new_custom_platform;
144pub use platform::new_default_platform;
145pub use platform::new_single_threaded_default_platform;
146pub use platform::new_unprotected_default_platform;
147pub use primitives::*;
148pub use promise::{PromiseRejectEvent, PromiseRejectMessage, PromiseState};
149pub use property_attribute::*;
150pub use property_descriptor::*;
151pub use property_filter::*;
152pub use property_handler_flags::*;
153pub use regexp::RegExpCreationFlags;
154pub use scope::AllowJavascriptExecutionScope;
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 string::latin1_to_utf8;
181pub use support::SharedPtr;
182pub use support::SharedRef;
183pub use support::UniquePtr;
184pub use support::UniqueRef;
185pub use template::*;
186pub use value_deserializer::ValueDeserializer;
187pub use value_deserializer::ValueDeserializerHelper;
188pub use value_deserializer::ValueDeserializerImpl;
189pub use value_serializer::ValueSerializer;
190pub use value_serializer::ValueSerializerHelper;
191pub use value_serializer::ValueSerializerImpl;
192pub use wasm::CompiledWasmModule;
193pub use wasm::ModuleCachingInterface;
194pub use wasm::WasmModuleCompilation;
195pub use wasm::WasmStreaming;
196
197/// https://v8.dev/docs/version-numbers
198pub const MAJOR_VERSION: u32 = binding::v8__MAJOR_VERSION;
199/// https://v8.dev/docs/version-numbers
200pub const MINOR_VERSION: u32 = binding::v8__MINOR_VERSION;
201/// https://v8.dev/docs/version-numbers
202pub const BUILD_NUMBER: u32 = binding::v8__BUILD_NUMBER;
203/// https://v8.dev/docs/version-numbers
204pub const PATCH_LEVEL: u32 = binding::v8__PATCH_LEVEL;
205/// https://v8.dev/docs/version-numbers
206pub const VERSION_STRING: &str =
207  // TODO: cleanup when Result::unwrap is const stable.
208  match binding::v8__VERSION_STRING.to_str() {
209    Ok(v) => v,
210    Err(_) => panic!("Unable to convert CStr to &str??"),
211  };
212
213// TODO(piscisaureus): Ideally this trait would not be exported.
214pub use support::MapFnTo;
215
216pub const TYPED_ARRAY_MAX_SIZE_IN_HEAP: usize =
217  binding::v8__TYPED_ARRAY_MAX_SIZE_IN_HEAP as _;
218
219#[cfg(test)]
220#[allow(unused)]
221pub(crate) fn initialize_v8() {
222  use std::sync::Once;
223
224  static INIT: Once = Once::new();
225  INIT.call_once(|| {
226    V8::initialize_platform(new_default_platform(0, false).make_shared());
227    V8::initialize();
228  });
229}