Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide stricter types and restructure concerning types. #18

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "Redis Source Available License 2.0 (RSALv2) or the Server Side Public
[dependencies]

[build-dependencies]
bindgen = "0.59.2"
bindgen = "0.59"
vergen = { version = "8", features = ["git", "gitcl"] }
lazy_static = "1"

Expand Down
469 changes: 267 additions & 202 deletions src/lib.rs

Large diffs are not rendered by default.

45 changes: 24 additions & 21 deletions src/v8/v8_context.rs → src/v8/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
* the Server Side Public License v1 (SSPLv1).
*/
//! See [Context].

use crate::v8_c_raw::bindings::{
v8_ContextEnter, v8_FreeContext, v8_GetPrivateData, v8_NewContext, v8_ResetPrivateData,
Expand All @@ -14,23 +15,23 @@ use std::marker::PhantomData;
use std::os::raw::c_void;
use std::ptr;

use crate::v8::isolate::V8Isolate;
use crate::v8::isolate_scope::V8IsolateScope;
use crate::v8::v8_context_scope::V8ContextScope;
use crate::v8::v8_object_template::V8LocalObjectTemplate;
use crate::v8::context_scope::ContextScope;
use crate::v8::isolate::Isolate;
use crate::v8::isolate_scope::IsolateScope;
use crate::v8::types::object_template::LocalObjectTemplate;

/// An RAII data guard which resets the private data slot after going
/// out of scope.
pub struct V8ContextDataGuard<'context, 'data, T: 'data> {
pub struct ContextDataGuard<'context, 'data, T: 'data> {
/// A raw index to reset after the guard goes out of scope.
index: RawIndex,
/// The context in which the guard should reset the variable.
context: &'context V8Context,
context: &'context Context,
_phantom_data: PhantomData<&'data T>,
}
impl<'context, 'data, T: 'data> V8ContextDataGuard<'context, 'data, T> {
impl<'context, 'data, T: 'data> ContextDataGuard<'context, 'data, T> {
/// Creates a new data guard with the provided index and context scope.
pub(crate) fn new<I: Into<RawIndex>>(index: I, context: &'context V8Context) -> Self {
pub(crate) fn new<I: Into<RawIndex>>(index: I, context: &'context Context) -> Self {
let index = index.into();
Self {
index,
Expand All @@ -40,23 +41,25 @@ impl<'context, 'data, T: 'data> V8ContextDataGuard<'context, 'data, T> {
}
}

impl<'context, 'data, T: 'data> Drop for V8ContextDataGuard<'context, 'data, T> {
impl<'context, 'data, T: 'data> Drop for ContextDataGuard<'context, 'data, T> {
fn drop(&mut self) {
self.context.reset_private_data_raw(self.index);
}
}

pub struct V8Context {
/// A sandboxed execution context with its own set of built-in objects
/// and functions.
pub struct Context {
pub(crate) inner_ctx: *mut v8_context,
}

unsafe impl Sync for V8Context {}
unsafe impl Send for V8Context {}
unsafe impl Sync for Context {}
unsafe impl Send for Context {}

impl V8Context {
pub(crate) fn new(isolate: &V8Isolate, globals: Option<&V8LocalObjectTemplate>) -> Self {
impl Context {
pub(crate) fn new(isolate: &Isolate, globals: Option<&LocalObjectTemplate>) -> Self {
let inner_ctx = match globals {
Some(g) => unsafe { v8_NewContext(isolate.inner_isolate, g.inner_obj) },
Some(g) => unsafe { v8_NewContext(isolate.inner_isolate, g.0.inner_val) },
None => unsafe { v8_NewContext(isolate.inner_isolate, ptr::null_mut()) },
};
Self { inner_ctx }
Expand All @@ -69,10 +72,10 @@ impl V8Context {
#[must_use]
pub fn enter<'isolate_scope, 'isolate>(
&self,
isolate_scope: &'isolate_scope V8IsolateScope<'isolate>,
) -> V8ContextScope<'isolate_scope, 'isolate> {
isolate_scope: &'isolate_scope IsolateScope<'isolate>,
) -> ContextScope<'isolate_scope, 'isolate> {
let inner_ctx_ref = unsafe { v8_ContextEnter(self.inner_ctx) };
V8ContextScope {
ContextScope {
inner_ctx_ref,
exit_on_drop: true,
isolate_scope,
Expand All @@ -94,10 +97,10 @@ impl V8Context {
&'context self,
index: I,
data: &'data T,
) -> V8ContextDataGuard<'context, 'data, T> {
) -> ContextDataGuard<'context, 'data, T> {
let index = index.into();
self.set_private_data_raw(index, data);
V8ContextDataGuard::new(index, self)
ContextDataGuard::new(index, self)
}

/// Resets a private data on the context considering the index as
Expand Down Expand Up @@ -130,7 +133,7 @@ impl V8Context {
}
}

impl Drop for V8Context {
impl Drop for Context {
fn drop(&mut self) {
unsafe { v8_FreeContext(self.inner_ctx) }
}
Expand Down
Loading