0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-22 06:09:47 -05:00
denoland-rusty-v8/src/lib.rs

170 lines
5.5 KiB
Rust
Raw Normal View History

2020-01-02 13:57:00 -05:00
// Copyright 2019-2020 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
//!
//! ```rust
2019-12-27 11:14:54 -05:00
//! use rusty_v8 as v8;
2019-12-27 19:41:44 +08:00
//!
//! let platform = v8::new_default_platform();
2019-12-27 11:14:54 -05:00
//! v8::V8::initialize_platform(platform);
//! v8::V8::initialize();
2019-12-27 19:41:44 +08:00
//!
2019-12-27 11:14:54 -05:00
//! let mut create_params = v8::Isolate::create_params();
//! create_params.set_array_buffer_allocator(v8::new_default_allocator());
//! let mut isolate = v8::Isolate::new(create_params);
//!
//! let mut handle_scope = v8::HandleScope::new(&mut isolate);
//! let scope = handle_scope.enter();
//!
//! let context = v8::Context::new(scope);
//! let mut context_scope = v8::ContextScope::new(scope, context);
//! let scope = context_scope.enter();
//!
//! 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
//!
//! let mut script = v8::Script::compile(scope, context, code, None).unwrap();
//! let result = script.run(scope, context).unwrap();
//! let result = result.to_string(scope).unwrap();
//! println!("result: {}", result.to_rust_string_lossy(scope));
2019-12-27 19:41:44 +08:00
//! ```
2020-01-21 09:35:00 -05:00
//!
//! # Design of Scopes
//!
//! Although the end is in sight, the design is still a bit in flux.
//!
//! The general idea is that the various scope classes mediate access to the v8
//! Isolate and the various items on its heap (Local/Global handles,
//! return/escape slots, etc.). At any point in time there exists only one scope
//! object that is directly accessible, which guarantees that all interactions
//! with the Isolate are safe.
//!
//! A Scope as such is not a trait (there is currently an internal
//! ScopeDefinition trait but that's only there to make implementation easier).
//!
//! Rather, there are a number of traits that are implemented for the scopes
//! they're applicable to, you've probably seen most of them already. The
//! InIsolate which gives access to &mut Isolate is implemented for all scopes,
//! ToLocal (I might rename that) is implemented for all Scopes in which new
//! Local handles can be created and it sets the appropriate lifetime on them.
//!
//! Furthermore, many callbacks will receive receive an appropriate Scope object
//! as their first argument, which 'encodes' the the state the isolate is in
//! when the callback is called. E.g. a FunctionCallbackScope implements
//! InIsolate + and ToLocal (it acts as a HandleScope).
//! HostImportModuleDynamicallyScope would also implement InIsolate plus
//! EscapeLocal (it doesn't act like a HandleScope, but it lets you safely
//! escape one MaybeLocal which is returned to the caller).
2020-01-21 09:35:00 -05:00
//!
//! In a nutshell, that's it.
//!
//! Open TODOs are:
//! - Add these automatic scopes to more callbacks (in progress) and get rid of
//! the necessity to import the MapFnTo trait.
//! - Fully integrate TryCatch blocks into the scope system (currently a
//! TryCatch works like a scope internally but it's not integrated).
//! - Add methods to some on some of the scopes like get_context() for ContextScope.
//! - Rename/reorganize/document.
2019-12-27 19:41:44 +08:00
#![allow(clippy::missing_safety_doc)]
2019-10-22 22:58:11 -07:00
#![allow(dead_code)]
2019-12-04 08:03:17 +01:00
#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate lazy_static;
extern crate libc;
mod array_buffer;
mod array_buffer_view;
2019-12-04 14:12:27 +01:00
mod context;
2019-12-30 15:28:39 +01:00
mod data;
2019-12-09 02:26:58 +01:00
mod exception;
mod external_references;
2019-12-11 04:43:22 +01:00
mod function;
mod global;
mod handle_scope;
mod isolate;
mod local;
2019-12-20 14:56:32 +01:00
mod module;
2019-12-04 08:03:17 +01:00
mod number;
2019-12-10 01:14:07 +01:00
mod object;
mod platform;
mod primitive_array;
mod primitives;
2019-12-14 02:18:30 +01:00
mod promise;
mod property_attribute;
mod scope_traits;
2019-12-04 14:12:27 +01:00
mod script;
mod script_or_module;
2019-12-28 16:29:42 -05:00
mod shared_array_buffer;
mod snapshot;
mod string;
mod support;
mod template;
2019-12-21 00:28:08 +01:00
mod try_catch;
2019-12-25 20:37:25 -05:00
mod uint8_array;
mod value;
pub mod inspector;
pub mod json;
pub mod scope;
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
pub use array_buffer::*;
2019-12-04 14:12:27 +01:00
pub use context::Context;
2019-12-30 15:28:39 +01:00
pub use data::*;
pub use exception::*;
pub use external_references::ExternalReference;
pub use external_references::ExternalReferences;
pub use function::*;
pub use global::Global;
pub use handle_scope::EscapableHandleScope;
pub use handle_scope::HandleScope;
pub use isolate::CreateParams;
pub use isolate::HostImportModuleDynamicallyCallback;
pub use isolate::HostInitializeImportMetaObjectCallback;
pub use isolate::Isolate;
pub use isolate::IsolateHandle;
pub use isolate::MessageCallback;
pub use isolate::OwnedIsolate;
pub use isolate::PromiseRejectCallback;
pub use local::Local;
pub use module::*;
2020-01-02 12:01:36 -05:00
pub use object::*;
pub use platform::new_default_platform;
pub use platform::Platform;
pub use platform::Task;
2020-01-23 11:43:00 -05:00
// TODO(ry) TaskBase and TaskImpl ideally shouldn't be part of the public API.
pub use platform::TaskBase;
pub use platform::TaskImpl;
pub use primitive_array::PrimitiveArray;
pub use primitives::*;
2019-12-30 15:28:39 +01:00
pub use promise::{PromiseRejectEvent, PromiseRejectMessage, PromiseState};
pub use property_attribute::*;
pub use scope::CallbackScope;
pub use scope::ContextScope;
pub use scope::FunctionCallbackScope;
pub use scope::PropertyCallbackScope;
pub use scope::Scope;
pub use scope_traits::*;
2019-12-04 14:12:27 +01:00
pub use script::{Script, ScriptOrigin};
pub use script_or_module::ScriptOrModule;
pub use snapshot::FunctionCodeHandling;
pub use snapshot::OwnedStartupData;
pub use snapshot::SnapshotCreator;
pub use snapshot::StartupData;
pub use string::NewStringType;
pub use support::SharedRef;
2020-01-17 00:12:25 +01:00
pub use support::UniquePtr;
pub use support::UniqueRef;
pub use template::*;
pub use try_catch::{TryCatch, TryCatchScope};
// TODO(piscisaureus): Ideally this trait would not be exported.
pub use support::MapFnTo;