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

Integrate src/ with core/ #1919

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 15 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
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ ts_sources = [
"js/metrics.ts",
"js/mkdir.ts",
"js/mock_builtin.js",
"js/msg_ring.ts",
"js/net.ts",
"js/os.ts",
"js/permissions.ts",
Expand Down
1 change: 0 additions & 1 deletion core/libdeno.rs

This file was deleted.

187 changes: 187 additions & 0 deletions core/libdeno.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use libc::c_char;
use libc::c_int;
use libc::c_void;
use libc::size_t;
use std::ops::{Deref, DerefMut};
use std::ptr::null;

// TODO(F001): change this definition to `extern { pub type isolate; }`
// After RFC 1861 is stablized. See https://github.com/rust-lang/rust/issues/43467.
#[repr(C)]
pub struct isolate {
_unused: [u8; 0],
}

/// If "alloc_ptr" is not null, this type represents a buffer which is created
/// in C side, and then passed to Rust side by `deno_recv_cb`. Finally it should
/// be moved back to C side by `deno_respond`. If it is not passed to
/// `deno_respond` in the end, it will be leaked.
///
/// If "alloc_ptr" is null, this type represents a borrowed slice.
#[repr(C)]
pub struct deno_buf {
alloc_ptr: *const u8,
alloc_len: usize,
data_ptr: *const u8,
data_len: usize,
pub zero_copy_id: usize,
}

/// `deno_buf` can not clone, and there is no interior mutability.
/// This type satisfies Send bound.
unsafe impl Send for deno_buf {}

impl deno_buf {
#[inline]
pub fn empty() -> Self {
Self {
alloc_ptr: null(),
alloc_len: 0,
data_ptr: null(),
data_len: 0,
zero_copy_id: 0,
}
}

#[inline]
pub unsafe fn from_raw_parts(ptr: *const u8, len: usize) -> Self {
Self {
alloc_ptr: null(),
alloc_len: 0,
data_ptr: ptr,
data_len: len,
zero_copy_id: 0,
}
}
}

/// Converts Rust &Buf to libdeno `deno_buf`.
impl<'a> From<&'a [u8]> for deno_buf {
#[inline]
fn from(x: &'a [u8]) -> Self {
Self {
alloc_ptr: null(),
alloc_len: 0,
data_ptr: x.as_ref().as_ptr(),
data_len: x.len(),
zero_copy_id: 0,
}
}
}

impl Deref for deno_buf {
type Target = [u8];
#[inline]
fn deref(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.data_ptr, self.data_len) }
}
}

impl DerefMut for deno_buf {
#[inline]
fn deref_mut(&mut self) -> &mut [u8] {
unsafe {
if self.alloc_ptr.is_null() {
panic!("Can't modify the buf");
}
std::slice::from_raw_parts_mut(self.data_ptr as *mut u8, self.data_len)
}
}
}

impl AsRef<[u8]> for deno_buf {
#[inline]
fn as_ref(&self) -> &[u8] {
&*self
}
}

impl AsMut<[u8]> for deno_buf {
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
if self.alloc_ptr.is_null() {
panic!("Can't modify the buf");
}
&mut *self
}
}

#[allow(non_camel_case_types)]
pub type deno_recv_cb = unsafe extern "C" fn(
user_data: *mut c_void,
control_buf: deno_buf, // deprecated
zero_copy_buf: deno_buf,
);

#[allow(non_camel_case_types)]
pub type deno_mod = i32;

#[allow(non_camel_case_types)]
type deno_resolve_cb = unsafe extern "C" fn(
user_data: *mut c_void,
specifier: *const c_char,
referrer: deno_mod,
) -> deno_mod;

#[repr(C)]
pub struct deno_config {
pub will_snapshot: c_int,
pub load_snapshot: deno_buf,
pub shared: deno_buf,
pub recv_cb: deno_recv_cb,
}

extern "C" {
pub fn deno_init();
pub fn deno_v8_version() -> *const c_char;
pub fn deno_set_v8_flags(argc: *mut c_int, argv: *mut *mut c_char);
pub fn deno_new(config: deno_config) -> *const isolate;
pub fn deno_delete(i: *const isolate);
pub fn deno_last_exception(i: *const isolate) -> *const c_char;
pub fn deno_check_promise_errors(i: *const isolate);
pub fn deno_lock(i: *const isolate);
pub fn deno_unlock(i: *const isolate);
pub fn deno_respond(
i: *const isolate,
user_data: *const c_void,
buf: deno_buf,
);
pub fn deno_zero_copy_release(i: *const isolate, zero_copy_id: usize);
pub fn deno_execute(
i: *const isolate,
user_data: *const c_void,
js_filename: *const c_char,
js_source: *const c_char,
);

// Modules

pub fn deno_mod_new(
i: *const isolate,
main: bool,
name: *const c_char,
source: *const c_char,
) -> deno_mod;

pub fn deno_mod_imports_len(i: *const isolate, id: deno_mod) -> size_t;

pub fn deno_mod_imports_get(
i: *const isolate,
id: deno_mod,
index: size_t,
) -> *const c_char;

pub fn deno_mod_instantiate(
i: *const isolate,
user_data: *const c_void,
id: deno_mod,
resolve_cb: deno_resolve_cb,
);

pub fn deno_mod_evaluate(
i: *const isolate,
user_data: *const c_void,
id: deno_mod,
);
}
49 changes: 0 additions & 49 deletions core/shared.rs

This file was deleted.

45 changes: 23 additions & 22 deletions js/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ import * as flatbuffers from "./flatbuffers";
import * as msg from "gen/msg_generated";
import * as errors from "./errors";
import * as util from "./util";
import * as msgRing from "./msg_ring";

let nextCmdId = 0;
const promiseTable = new Map<number, util.Resolvable<msg.Base>>();

let fireTimers: () => void;

export function setFireTimersCallback(fn: () => void): void {
fireTimers = fn;
}

export function handleAsyncMsgFromRust(ui8: Uint8Array): void {
// If a the buffer is empty, recv() on the native side timed out and we
// did not receive a message.
if (ui8 && ui8.length) {
const bb = new flatbuffers.ByteBuffer(ui8);
export function handleAsyncMsgFromRust(_unused: Uint8Array): void {
util.log("handleAsyncMsgFromRust start");
util.assert(_unused == null);
let i = 0;
let ui8: Uint8Array | null;
while ((ui8 = msgRing.rx.receive(Uint8Array)) !== null) {
util.log(`handleAsyncMsgFromRust ${i++}`);
const bb = new flatbuffers.ByteBuffer(ui8!);
const base = msg.Base.getRootAsBase(bb);
const cmdId = base.cmdId();
const promise = promiseTable.get(cmdId);
Expand All @@ -31,8 +29,6 @@ export function handleAsyncMsgFromRust(ui8: Uint8Array): void {
promise!.resolve(base);
}
}
// Fire timers that have become runnable.
fireTimers();
}

function sendInternal(
Expand All @@ -41,17 +37,23 @@ function sendInternal(
inner: flatbuffers.Offset,
data: undefined | ArrayBufferView,
sync = true
): [number, null | Uint8Array] {
): number {
const cmdId = nextCmdId++;
msg.Base.startBase(builder);
msg.Base.addInner(builder, inner);
msg.Base.addInnerType(builder, innerType);
msg.Base.addSync(builder, sync);
msg.Base.addCmdId(builder, cmdId);
builder.finish(msg.Base.endBase(builder));
const res = libdeno.send(builder.asUint8Array(), data);

const ui8 = builder.asUint8Array();
msgRing.tx.send(ui8);

// Somehow put this in the shared buffer.

libdeno.send(null, data);
builder.inUse = false;
return [cmdId, res];
return cmdId;
}

// @internal
Expand All @@ -61,8 +63,7 @@ export function sendAsync(
inner: flatbuffers.Offset,
data?: ArrayBufferView
): Promise<msg.Base> {
const [cmdId, resBuf] = sendInternal(builder, innerType, inner, data, false);
util.assert(resBuf == null);
const cmdId = sendInternal(builder, innerType, inner, data, false);
const promise = util.createResolvable<msg.Base>();
promiseTable.set(cmdId, promise);
return promise;
Expand All @@ -75,13 +76,13 @@ export function sendSync(
inner: flatbuffers.Offset,
data?: ArrayBufferView
): null | msg.Base {
const [cmdId, resBuf] = sendInternal(builder, innerType, inner, data, true);
const cmdId = sendInternal(builder, innerType, inner, data, true);
util.assert(cmdId >= 0);
if (resBuf == null) {
let resBuf: Uint8Array | null = msgRing.rx.receive(Uint8Array);
if (resBuf == null || resBuf.length === 0) {
return null;
} else {
const u8 = new Uint8Array(resBuf!);
const bb = new flatbuffers.ByteBuffer(u8);
const bb = new flatbuffers.ByteBuffer(resBuf);
const baseRes = msg.Base.getRootAsBase(bb);
errors.maybeThrowError(baseRes);
return baseRes;
Expand Down
7 changes: 5 additions & 2 deletions js/libdeno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ interface EvalErrorInfo {
interface Libdeno {
recv(cb: MessageCallback): void;

send(control: ArrayBufferView, data?: ArrayBufferView): null | Uint8Array;
send(
control: null | ArrayBufferView,
data?: ArrayBufferView
): null | Uint8Array;

print(x: string, isErr?: boolean): void;

shared: ArrayBuffer;
shared: SharedArrayBuffer;

/** Evaluate provided code in the current context.
* It differs from eval(...) in that it does not create a new context.
Expand Down
Loading