mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-11-24 15:19:31 -05:00
use Isolate for creating primitives (#1082)
This commit is contained in:
parent
8f8636b4f7
commit
5dd41e9dba
6 changed files with 114 additions and 37 deletions
|
@ -94,36 +94,74 @@ fn main() {
|
||||||
global.set(scope, name.into(), value.into()).unwrap();
|
global.set(scope, name.into(), value.into()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
extern "C" fn callback(info: *const v8::FunctionCallbackInfo) {
|
||||||
|
let scope = unsafe { &mut v8::CallbackScope::new(&*info) };
|
||||||
|
let mut rv =
|
||||||
|
unsafe { v8::ReturnValue::from_function_callback_info(info) };
|
||||||
|
rv.set(v8::undefined(scope).into());
|
||||||
|
}
|
||||||
|
let func = v8::Function::new_raw(scope, callback).unwrap();
|
||||||
|
let name = v8::String::new(scope, "undefined_from_scope").unwrap();
|
||||||
|
global.set(scope, name.into(), func.into()).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
extern "C" fn callback(info: *const v8::FunctionCallbackInfo) {
|
||||||
|
let mut rv =
|
||||||
|
unsafe { v8::ReturnValue::from_function_callback_info(info) };
|
||||||
|
let mut info = unsafe {
|
||||||
|
v8::FunctionCallbackArguments::from_function_callback_info(info)
|
||||||
|
};
|
||||||
|
rv.set(v8::undefined(unsafe { info.get_isolate() }).into());
|
||||||
|
}
|
||||||
|
let func = v8::Function::new_raw(scope, callback).unwrap();
|
||||||
|
let name = v8::String::new(scope, "undefined_from_isolate").unwrap();
|
||||||
|
global.set(scope, name.into(), func.into()).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
let runs = 100_000_000;
|
let runs = 100_000_000;
|
||||||
|
|
||||||
for x in [
|
for (group_name, benches) in [
|
||||||
"new_",
|
(
|
||||||
"new_raw",
|
"function_overhead",
|
||||||
"new_set_uint32",
|
&[
|
||||||
"new_raw_set_uint32",
|
"new_",
|
||||||
"new_fast",
|
"new_raw",
|
||||||
|
"new_set_uint32",
|
||||||
|
"new_raw_set_uint32",
|
||||||
|
"new_fast",
|
||||||
|
][..],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"primitives",
|
||||||
|
&["undefined_from_scope", "undefined_from_isolate"][..],
|
||||||
|
),
|
||||||
] {
|
] {
|
||||||
let code = format!(
|
println!("Running {} ...", group_name);
|
||||||
"
|
for x in benches {
|
||||||
function bench() {{ return {}(); }};
|
let code = format!(
|
||||||
runs = {};
|
"
|
||||||
start = Date.now();
|
function bench() {{ return {}(); }};
|
||||||
for (i = 0; i < runs; i++) bench();
|
runs = {};
|
||||||
Date.now() - start;
|
start = Date.now();
|
||||||
",
|
for (i = 0; i < runs; i++) bench();
|
||||||
x, runs
|
Date.now() - start;
|
||||||
);
|
",
|
||||||
|
x, runs
|
||||||
|
);
|
||||||
|
|
||||||
let r = eval(scope, &code).unwrap();
|
let r = eval(scope, &code).unwrap();
|
||||||
let number = r.to_number(scope).unwrap();
|
let number = r.to_number(scope).unwrap();
|
||||||
let total_ms = number.number_value(scope).unwrap();
|
let total_ms = number.number_value(scope).unwrap();
|
||||||
let total_ns = 1e6 * total_ms;
|
let total_ns = 1e6 * total_ms;
|
||||||
let ns_per_run = total_ns / (runs as f64);
|
let ns_per_run = total_ns / (runs as f64);
|
||||||
let mops_per_sec = (runs as f64) / (total_ms / 1000.0) / 1e6;
|
let mops_per_sec = (runs as f64) / (total_ms / 1000.0) / 1e6;
|
||||||
println!(
|
println!(
|
||||||
"{:.1} ns per run {:.1} million ops/sec → {}",
|
" {:.1} ns per run {:.1} million ops/sec → {}",
|
||||||
ns_per_run, mops_per_sec, x
|
ns_per_run, mops_per_sec, x
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ use crate::support::{int, Opaque};
|
||||||
use crate::Context;
|
use crate::Context;
|
||||||
use crate::Function;
|
use crate::Function;
|
||||||
use crate::HandleScope;
|
use crate::HandleScope;
|
||||||
|
use crate::Isolate;
|
||||||
use crate::Local;
|
use crate::Local;
|
||||||
use crate::Name;
|
use crate::Name;
|
||||||
use crate::Object;
|
use crate::Object;
|
||||||
|
@ -69,6 +70,9 @@ extern "C" {
|
||||||
fn v8__FunctionCallbackInfo__NewTarget(
|
fn v8__FunctionCallbackInfo__NewTarget(
|
||||||
this: *const FunctionCallbackInfo,
|
this: *const FunctionCallbackInfo,
|
||||||
) -> *const Value;
|
) -> *const Value;
|
||||||
|
fn v8__FunctionCallbackInfo__GetIsolate(
|
||||||
|
this: *const FunctionCallbackInfo,
|
||||||
|
) -> *mut Isolate;
|
||||||
|
|
||||||
fn v8__PropertyCallbackInfo__GetReturnValue(
|
fn v8__PropertyCallbackInfo__GetReturnValue(
|
||||||
this: *const PropertyCallbackInfo,
|
this: *const PropertyCallbackInfo,
|
||||||
|
@ -233,6 +237,11 @@ impl<'s> FunctionCallbackArguments<'s> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub unsafe fn get_isolate(&mut self) -> &mut Isolate {
|
||||||
|
&mut *v8__FunctionCallbackInfo__GetIsolate(self.info)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the receiver. This corresponds to the "this" value.
|
/// Returns the receiver. This corresponds to the "this" value.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn this(&self) -> Local<'s, Object> {
|
pub fn this(&self) -> Local<'s, Object> {
|
||||||
|
|
|
@ -107,6 +107,11 @@ impl<'s, T> Local<'s, T> {
|
||||||
NonNull::new(ptr as *mut _).map(|nn| Self::from_non_null(nn))
|
NonNull::new(ptr as *mut _).map(|nn| Self::from_non_null(nn))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub(crate) unsafe fn from_raw_unchecked(ptr: *const T) -> Self {
|
||||||
|
Self(NonNull::new_unchecked(ptr as *mut _), PhantomData)
|
||||||
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub(crate) unsafe fn from_non_null(nn: NonNull<T>) -> Self {
|
pub(crate) unsafe fn from_non_null(nn: NonNull<T>) -> Self {
|
||||||
Self(nn, PhantomData)
|
Self(nn, PhantomData)
|
||||||
|
|
|
@ -1123,6 +1123,18 @@ impl DerefMut for OwnedIsolate {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AsMut<Isolate> for OwnedIsolate {
|
||||||
|
fn as_mut(&mut self) -> &mut Isolate {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsMut<Isolate> for Isolate {
|
||||||
|
fn as_mut(&mut self) -> &mut Isolate {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl HeapStatistics {
|
impl HeapStatistics {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn total_heap_size(&self) -> usize {
|
pub fn total_heap_size(&self) -> usize {
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use crate::isolate::Isolate;
|
use crate::isolate::Isolate;
|
||||||
use crate::Boolean;
|
use crate::Boolean;
|
||||||
use crate::HandleScope;
|
|
||||||
use crate::Local;
|
use crate::Local;
|
||||||
use crate::Primitive;
|
use crate::Primitive;
|
||||||
|
|
||||||
|
@ -12,24 +11,29 @@ extern "C" {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn null<'s>(scope: &mut HandleScope<'s, ()>) -> Local<'s, Primitive> {
|
pub fn null<'a, R>(scope: &mut R) -> Local<'a, Primitive>
|
||||||
unsafe { scope.cast_local(|sd| v8__Null(sd.get_isolate_ptr())) }.unwrap()
|
where
|
||||||
|
R: AsMut<Isolate>,
|
||||||
|
{
|
||||||
|
unsafe { Local::from_raw_unchecked(v8__Null(scope.as_mut())) }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn undefined<'s>(scope: &mut HandleScope<'s, ()>) -> Local<'s, Primitive> {
|
pub fn undefined<'a, R>(scope: &mut R) -> Local<'a, Primitive>
|
||||||
unsafe { scope.cast_local(|sd| v8__Undefined(sd.get_isolate_ptr())) }.unwrap()
|
where
|
||||||
|
R: AsMut<Isolate>,
|
||||||
|
{
|
||||||
|
unsafe { Local::from_raw_unchecked(v8__Undefined(scope.as_mut())) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Boolean {
|
impl Boolean {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn new<'s>(
|
pub fn new<'a, R>(scope: &mut R, value: bool) -> Local<'a, Boolean>
|
||||||
scope: &mut HandleScope<'s, ()>,
|
where
|
||||||
value: bool,
|
R: AsMut<Isolate>,
|
||||||
) -> Local<'s, Boolean> {
|
{
|
||||||
unsafe {
|
unsafe {
|
||||||
scope.cast_local(|sd| v8__Boolean__New(sd.get_isolate_ptr(), value))
|
Local::from_raw_unchecked(v8__Boolean__New(scope.as_mut(), value))
|
||||||
}
|
}
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -553,6 +553,15 @@ fn get_isolate_from_handle() {
|
||||||
check_eval(scope, Some(false), "3.3 / 3.3");
|
check_eval(scope, Some(false), "3.3 / 3.3");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn handles_from_isolate() {
|
||||||
|
let _setup_guard = setup();
|
||||||
|
let isolate = &mut v8::Isolate::new(Default::default());
|
||||||
|
let _ = v8::null(isolate);
|
||||||
|
let _ = v8::undefined(isolate);
|
||||||
|
let _ = v8::Boolean::new(isolate, true);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn array_buffer() {
|
fn array_buffer() {
|
||||||
let _setup_guard = setup();
|
let _setup_guard = setup();
|
||||||
|
|
Loading…
Reference in a new issue