2021-02-13 07:31:18 -05:00
|
|
|
// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.
|
2019-11-01 13:50:12 -04:00
|
|
|
|
2019-12-27 11:14:54 -05:00
|
|
|
//! # Example
|
2019-12-27 19:41:44 +08:00
|
|
|
//!
|
2020-01-18 05:55:09 +01:00
|
|
|
//! ```rust
|
2021-07-01 16:05:07 +02:00
|
|
|
//! let platform = v8::new_default_platform(0, false).make_shared();
|
2019-12-27 11:14:54 -05:00
|
|
|
//! v8::V8::initialize_platform(platform);
|
|
|
|
//! v8::V8::initialize();
|
2019-12-27 19:41:44 +08:00
|
|
|
//!
|
2020-06-03 07:38:34 +02:00
|
|
|
//! let isolate = &mut v8::Isolate::new(Default::default());
|
2020-01-18 05:55:09 +01:00
|
|
|
//!
|
2020-06-03 07:38:34 +02:00
|
|
|
//! let scope = &mut v8::HandleScope::new(isolate);
|
2020-01-18 05:55:09 +01:00
|
|
|
//! let context = v8::Context::new(scope);
|
2020-06-03 07:38:34 +02:00
|
|
|
//! let scope = &mut v8::ContextScope::new(scope, context);
|
2020-01-18 05:55:09 +01:00
|
|
|
//!
|
|
|
|
//! let code = v8::String::new(scope, "'Hello' + ' World!'").unwrap();
|
|
|
|
//! println!("javascript code: {}", code.to_rust_string_lossy(scope));
|
2019-12-27 19:41:44 +08:00
|
|
|
//!
|
2020-11-13 21:55:52 +01:00
|
|
|
//! let script = v8::Script::compile(scope, code, None).unwrap();
|
2020-06-26 02:56:24 +02:00
|
|
|
//! let result = script.run(scope).unwrap();
|
2020-01-18 05:55:09 +01:00
|
|
|
//! let result = result.to_string(scope).unwrap();
|
|
|
|
//! println!("result: {}", result.to_rust_string_lossy(scope));
|
2019-12-27 19:41:44 +08:00
|
|
|
//! ```
|
|
|
|
|
2019-11-15 16:21:34 -08:00
|
|
|
#![allow(clippy::missing_safety_doc)]
|
2019-10-22 22:58:11 -07:00
|
|
|
|
2019-12-04 08:03:17 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate bitflags;
|
2019-11-15 16:21:34 -08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
2019-11-15 12:57:34 -05:00
|
|
|
|
2019-12-21 08:50:59 -05:00
|
|
|
mod array_buffer;
|
2020-01-17 09:26:42 -05:00
|
|
|
mod array_buffer_view;
|
2020-09-29 19:20:07 -05:00
|
|
|
mod bigint;
|
2019-12-04 14:12:27 +01:00
|
|
|
mod context;
|
2019-12-30 15:28:39 +01:00
|
|
|
mod data;
|
2020-09-02 04:28:08 +02:00
|
|
|
mod date;
|
2019-12-09 02:26:58 +01:00
|
|
|
mod exception;
|
2020-07-03 14:01:42 +02:00
|
|
|
mod external;
|
2019-12-27 09:12:16 -05:00
|
|
|
mod external_references;
|
2022-07-07 16:30:35 +05:30
|
|
|
pub mod fast_api;
|
2021-02-05 00:22:26 +01:00
|
|
|
mod fixed_array;
|
2019-12-11 04:43:22 +01:00
|
|
|
mod function;
|
2022-09-03 21:41:40 +05:30
|
|
|
mod get_property_names_args_builder;
|
2020-06-30 05:12:13 +02:00
|
|
|
mod handle;
|
2021-02-09 09:17:02 -05:00
|
|
|
pub mod icu;
|
2019-12-04 00:57:06 -05:00
|
|
|
mod isolate;
|
2020-04-20 21:18:03 +02:00
|
|
|
mod isolate_create_params;
|
2019-12-20 14:56:32 +01:00
|
|
|
mod module;
|
2020-07-05 17:28:13 +02:00
|
|
|
mod name;
|
2019-12-04 08:03:17 +01:00
|
|
|
mod number;
|
2019-12-10 01:14:07 +01:00
|
|
|
mod object;
|
2020-01-17 09:26:42 -05:00
|
|
|
mod platform;
|
2019-12-21 06:38:26 -05:00
|
|
|
mod primitive_array;
|
2019-12-05 17:03:18 -05:00
|
|
|
mod primitives;
|
2020-09-29 19:20:07 -05:00
|
|
|
mod private;
|
2019-12-14 02:18:30 +01:00
|
|
|
mod promise;
|
2020-01-20 17:16:24 +01:00
|
|
|
mod property_attribute;
|
2022-09-03 21:41:40 +05:30
|
|
|
mod property_filter;
|
2020-04-01 23:47:19 +02:00
|
|
|
mod proxy;
|
2020-06-03 07:38:34 +02:00
|
|
|
mod scope;
|
2019-12-04 14:12:27 +01:00
|
|
|
mod script;
|
2019-12-21 06:38:26 -05:00
|
|
|
mod script_or_module;
|
2019-12-28 16:29:42 -05:00
|
|
|
mod shared_array_buffer;
|
2019-12-24 14:03:32 +01:00
|
|
|
mod snapshot;
|
2019-12-04 10:10:15 -05:00
|
|
|
mod string;
|
2019-12-04 00:57:06 -05:00
|
|
|
mod support;
|
2020-09-29 19:20:07 -05:00
|
|
|
mod symbol;
|
2020-01-19 00:38:41 +01:00
|
|
|
mod template;
|
2020-10-06 13:16:22 -05:00
|
|
|
mod typed_array;
|
2021-03-05 18:26:37 +08:00
|
|
|
mod unbound_module_script;
|
2021-01-12 11:39:51 +08:00
|
|
|
mod unbound_script;
|
2019-12-06 09:36:34 -05:00
|
|
|
mod value;
|
2020-10-06 18:39:38 +02:00
|
|
|
mod value_deserializer;
|
|
|
|
mod value_serializer;
|
2020-12-31 16:06:29 +01:00
|
|
|
mod wasm;
|
2019-12-04 00:57:06 -05:00
|
|
|
|
2019-12-04 10:31:54 -05:00
|
|
|
pub mod inspector;
|
2019-12-20 08:47:20 -05:00
|
|
|
pub mod json;
|
2019-12-20 14:54:20 -05:00
|
|
|
pub mod script_compiler;
|
2019-11-30 08:47:26 -08:00
|
|
|
// This module is intentionally named "V8" rather than "v8" to match the
|
|
|
|
// C++ namespace "v8::V8".
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub mod V8;
|
2019-10-17 16:46:54 -07:00
|
|
|
|
2019-12-25 10:56:27 -05:00
|
|
|
pub use array_buffer::*;
|
2020-09-29 19:20:07 -05:00
|
|
|
pub use bigint::*;
|
2019-12-30 15:28:39 +01:00
|
|
|
pub use data::*;
|
2019-12-20 08:47:20 -05:00
|
|
|
pub use exception::*;
|
2020-01-03 16:52:05 -05:00
|
|
|
pub use external_references::ExternalReference;
|
2019-12-27 09:12:16 -05:00
|
|
|
pub use external_references::ExternalReferences;
|
2020-01-04 15:23:36 +01:00
|
|
|
pub use function::*;
|
2022-09-03 21:41:40 +05:30
|
|
|
pub use get_property_names_args_builder::*;
|
2020-06-30 05:12:13 +02:00
|
|
|
pub use handle::Global;
|
|
|
|
pub use handle::Handle;
|
|
|
|
pub use handle::Local;
|
feat: Weak handles and finalizers (#895)
This change adds support for weak handles that don't prevent GC of the
referenced objects, through the `v8::Weak<T>` API. A weak handle can
be empty (if it was created empty or its object was GC'd) or
non-empty, and if non-empty it allows getting its object as a global
or local.
When creating a `v8::Weak` you can also set a finalizer that will be
called at some point after the object is GC'd, as long as the weak
handle is still alive at that point. This finalization corresponds to
the second-pass callback in `kParameter` mode in the C++ API, so it
will only be called after the object is GC'd. The finalizer function
is a `FnOnce` that may close over data, and which takes a
`&mut Isolate` as an argument.
The C++ finalization API doesn't guarantee _when_ or even _if_ the
finalizer will ever be called, but in order to prevent memory leaks,
the rusty_v8 wrapper ensures that it will be called at some point,
even if it's just before the isolate gets dropped.
`v8::Weak<T>` implements `Clone`, but a finalizer is tied to a single
weak handle, so its clones won't be able to keep the finalizer alive.
And in fact, cloning will create a new weak handle that isn't tied to
a finalizer at all. `v8::Weak::clone_with_finalizer` can be used to
make a clone of a weak handle which has a finalizer tied to it.
Note that `v8::Weak<T>` doesn't implement `Hash`, because the hash
would have to change once the handle's object is GC'd, which is a big
gotcha and would break some of the algorithms that rely on hashes,
such as the Rust std's `HashMap`.
2022-05-09 12:20:55 +02:00
|
|
|
pub use handle::Weak;
|
2020-08-23 16:16:45 +02:00
|
|
|
pub use isolate::HeapStatistics;
|
2022-05-24 16:26:12 +02:00
|
|
|
pub use isolate::HostCreateShadowRealmContextCallback;
|
2022-03-09 14:41:46 +01:00
|
|
|
pub use isolate::HostImportModuleDynamicallyCallback;
|
2020-01-03 16:52:05 -05:00
|
|
|
pub use isolate::HostInitializeImportMetaObjectCallback;
|
|
|
|
pub use isolate::Isolate;
|
2020-02-12 11:33:58 -05:00
|
|
|
pub use isolate::IsolateHandle;
|
2022-11-30 08:25:10 -08:00
|
|
|
pub use isolate::MemoryPressureLevel;
|
2020-01-03 16:52:05 -05:00
|
|
|
pub use isolate::MessageCallback;
|
2020-08-23 16:25:16 +02:00
|
|
|
pub use isolate::MicrotasksPolicy;
|
2020-07-30 18:40:18 +02:00
|
|
|
pub use isolate::NearHeapLimitCallback;
|
2022-06-14 16:26:50 -06:00
|
|
|
pub use isolate::OomDetails;
|
|
|
|
pub use isolate::OomErrorCallback;
|
2020-01-03 16:52:05 -05:00
|
|
|
pub use isolate::OwnedIsolate;
|
2020-10-12 22:33:46 +02:00
|
|
|
pub use isolate::PromiseHook;
|
|
|
|
pub use isolate::PromiseHookType;
|
2020-01-03 16:52:05 -05:00
|
|
|
pub use isolate::PromiseRejectCallback;
|
2022-08-13 17:55:56 +02:00
|
|
|
pub use isolate::WasmAsyncSuccess;
|
2020-04-20 21:18:03 +02:00
|
|
|
pub use isolate_create_params::CreateParams;
|
2019-12-23 20:23:55 -05:00
|
|
|
pub use module::*;
|
2020-01-02 12:01:36 -05:00
|
|
|
pub use object::*;
|
2020-01-17 09:26:42 -05:00
|
|
|
pub use platform::new_default_platform;
|
2021-04-12 21:40:52 +02:00
|
|
|
pub use platform::new_single_threaded_default_platform;
|
2020-01-17 09:26:42 -05:00
|
|
|
pub use platform::Platform;
|
2019-12-05 17:03:18 -05:00
|
|
|
pub use primitives::*;
|
2020-09-29 19:20:07 -05:00
|
|
|
pub use private::*;
|
2019-12-30 15:28:39 +01:00
|
|
|
pub use promise::{PromiseRejectEvent, PromiseRejectMessage, PromiseState};
|
2020-01-20 17:16:24 +01:00
|
|
|
pub use property_attribute::*;
|
2022-09-03 21:41:40 +05:30
|
|
|
pub use property_filter::*;
|
2020-04-01 23:47:19 +02:00
|
|
|
pub use proxy::*;
|
2020-01-18 05:55:09 +01:00
|
|
|
pub use scope::CallbackScope;
|
|
|
|
pub use scope::ContextScope;
|
2020-06-03 07:38:34 +02:00
|
|
|
pub use scope::EscapableHandleScope;
|
|
|
|
pub use scope::HandleScope;
|
2020-06-26 01:40:53 +02:00
|
|
|
pub use scope::TryCatch;
|
2020-03-05 17:41:43 -08:00
|
|
|
pub use script::ScriptOrigin;
|
2021-03-05 18:26:37 +08:00
|
|
|
pub use script_compiler::CachedData;
|
2020-01-03 16:52:05 -05:00
|
|
|
pub use snapshot::FunctionCodeHandling;
|
|
|
|
pub use snapshot::StartupData;
|
2019-12-04 10:10:15 -05:00
|
|
|
pub use string::NewStringType;
|
2021-01-13 00:49:59 +01:00
|
|
|
pub use string::WriteOptions;
|
2020-04-20 19:34:48 +02:00
|
|
|
pub use support::SharedPtr;
|
2019-12-26 03:32:22 +01:00
|
|
|
pub use support::SharedRef;
|
2020-01-17 00:12:25 +01:00
|
|
|
pub use support::UniquePtr;
|
2019-12-24 16:10:40 -05:00
|
|
|
pub use support::UniqueRef;
|
2020-09-29 19:20:07 -05:00
|
|
|
pub use symbol::*;
|
2020-01-19 00:38:41 +01:00
|
|
|
pub use template::*;
|
2020-10-06 18:39:38 +02:00
|
|
|
pub use value_deserializer::ValueDeserializer;
|
|
|
|
pub use value_deserializer::ValueDeserializerHelper;
|
|
|
|
pub use value_deserializer::ValueDeserializerImpl;
|
|
|
|
pub use value_serializer::ValueSerializer;
|
|
|
|
pub use value_serializer::ValueSerializerHelper;
|
|
|
|
pub use value_serializer::ValueSerializerImpl;
|
2021-09-17 18:02:40 +02:00
|
|
|
pub use wasm::CompiledWasmModule;
|
2020-12-31 16:06:29 +01:00
|
|
|
pub use wasm::WasmStreaming;
|
2020-01-04 15:23:36 +01:00
|
|
|
|
|
|
|
// TODO(piscisaureus): Ideally this trait would not be exported.
|
|
|
|
pub use support::MapFnTo;
|