1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 23:34:47 -05:00

Remove deno_get_data()

Instead, pass the isolate data to the dispatch callback directly.
This commit is contained in:
Bert Belder 2018-10-08 11:04:33 +02:00
parent e742af10aa
commit 4f3250bc43
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
5 changed files with 16 additions and 23 deletions

View file

@ -243,7 +243,7 @@ void Send(const v8::FunctionCallbackInfo<v8::Value>& args) {
DCHECK_EQ(d->currentArgs, nullptr);
d->currentArgs = &args;
d->cb(d, req_id, control, data);
d->cb(d->user_data, req_id, control, data);
if (d->currentArgs == nullptr) {
// This indicates that deno_repond() was called already.
@ -431,11 +431,6 @@ void deno_init() {
v8::V8::Initialize();
}
void* deno_get_data(const Deno* d) {
CHECK(d->user_data != nullptr);
return d->user_data;
}
const char* deno_v8_version() { return v8::V8::GetVersion(); }
void deno_set_v8_flags(int* argc, char** argv) {

View file

@ -23,8 +23,8 @@ typedef struct deno_s Deno;
// A callback to receive a message from a libdeno.send() javascript call.
// control_buf is valid for only for the lifetime of this callback.
// data_buf is valid until deno_respond() is called.
typedef void (*deno_recv_cb)(Deno* d, int32_t req_id, deno_buf control_buf,
deno_buf data_buf);
typedef void (*deno_recv_cb)(void* user_data, int32_t req_id,
deno_buf control_buf, deno_buf data_buf);
void deno_init();
const char* deno_v8_version();
@ -33,9 +33,6 @@ void deno_set_v8_flags(int* argc, char** argv);
Deno* deno_new(deno_recv_cb cb);
void deno_delete(Deno* d);
// Returns the void* user_data provided in deno_new.
void* deno_get_data(Deno*);
// Returns false on error.
// Get error text with deno_last_exception().
// 0 = fail, 1 = success

View file

@ -69,16 +69,17 @@ TEST(LibDenoTest, RecvReturnEmpty) {
TEST(LibDenoTest, RecvReturnBar) {
static int count = 0;
Deno* d = deno_new([](auto deno, int req_id, auto buf, auto data_buf) {
Deno* d = deno_new([](auto user_data, int req_id, auto buf, auto data_buf) {
auto d = reinterpret_cast<Deno*>(user_data);
assert_null(data_buf);
count++;
EXPECT_EQ(static_cast<size_t>(3), buf.data_len);
EXPECT_EQ(buf.data_ptr[0], 'a');
EXPECT_EQ(buf.data_ptr[1], 'b');
EXPECT_EQ(buf.data_ptr[2], 'c');
deno_respond(deno, nullptr, req_id, strbuf("bar"));
deno_respond(d, user_data, req_id, strbuf("bar"));
});
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "RecvReturnBar()"));
EXPECT_TRUE(deno_execute(d, d, "a.js", "RecvReturnBar()"));
EXPECT_EQ(count, 1);
deno_delete(d);
}
@ -91,7 +92,8 @@ TEST(LibDenoTest, DoubleRecvFails) {
TEST(LibDenoTest, SendRecvSlice) {
static int count = 0;
Deno* d = deno_new([](auto deno, int req_id, auto buf, auto data_buf) {
Deno* d = deno_new([](auto user_data, int req_id, auto buf, auto data_buf) {
auto d = reinterpret_cast<Deno*>(user_data);
assert_null(data_buf);
static const size_t alloc_len = 1024;
size_t i = count++;
@ -115,9 +117,9 @@ TEST(LibDenoTest, SendRecvSlice) {
buf2.data_ptr[0] = 200 + i;
buf2.data_ptr[buf2.data_len - 1] = 200 - i;
// Send back.
deno_respond(deno, nullptr, req_id, buf2);
deno_respond(d, user_data, req_id, buf2);
});
EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "SendRecvSlice()"));
EXPECT_TRUE(deno_execute(d, d, "a.js", "SendRecvSlice()"));
EXPECT_EQ(count, 5);
deno_delete(d);
}

View file

@ -100,8 +100,8 @@ impl Isolate {
self as *mut _ as *mut c_void
}
pub fn from_c<'a>(d: *const libdeno::isolate) -> &'a mut Isolate {
let ptr = unsafe { libdeno::deno_get_data(d) } as *mut _;
pub fn from_void_ptr<'a>(ptr: *mut c_void) -> &'a mut Isolate {
let ptr = ptr as *mut _;
unsafe { &mut *ptr }
}
@ -236,7 +236,7 @@ impl From<Buf> for libdeno::deno_buf {
// Dereferences the C pointer into the Rust Isolate object.
extern "C" fn pre_dispatch(
d: *const libdeno::isolate,
user_data: *mut c_void,
req_id: i32,
control_buf: libdeno::deno_buf,
data_buf: libdeno::deno_buf,
@ -256,7 +256,7 @@ extern "C" fn pre_dispatch(
)
};
let isolate = Isolate::from_c(d);
let isolate = Isolate::from_void_ptr(user_data);
let dispatch = isolate.dispatch;
let (is_sync, op) = dispatch(isolate, control_slice, data_slice);

View file

@ -20,7 +20,7 @@ pub struct deno_buf {
}
type DenoRecvCb = unsafe extern "C" fn(
d: *const isolate,
user_data: *mut c_void,
req_id: i32,
buf: deno_buf,
data_buf: deno_buf,
@ -33,7 +33,6 @@ extern "C" {
pub fn deno_new(cb: DenoRecvCb) -> *const isolate;
pub fn deno_delete(i: *const isolate);
pub fn deno_last_exception(i: *const isolate) -> *const c_char;
pub fn deno_get_data(i: *const isolate) -> *mut c_void;
pub fn deno_respond(
i: *const isolate,
user_data: *mut c_void,