summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/binding.rs24
-rw-r--r--src/handlers.rs112
-rw-r--r--src/main.rs5
-rw-r--r--src/msg.fbs10
-rw-r--r--src/reply.cc29
-rw-r--r--src/reply.h10
6 files changed, 99 insertions, 91 deletions
diff --git a/src/binding.rs b/src/binding.rs
index 72e8af996..1ef376dff 100644
--- a/src/binding.rs
+++ b/src/binding.rs
@@ -1,11 +1,9 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
#![allow(dead_code)]
-
extern crate libc;
use libc::c_char;
use libc::c_int;
use libc::c_void;
-use libc::uint32_t;
#[repr(C)]
pub struct DenoC {
@@ -14,10 +12,10 @@ pub struct DenoC {
#[repr(C)]
pub struct deno_buf {
- alloc_ptr: *mut u8,
- alloc_len: usize,
- data_ptr: *mut u8,
- data_len: usize,
+ pub alloc_ptr: *mut u8,
+ pub alloc_len: usize,
+ pub data_ptr: *mut u8,
+ pub data_len: usize,
}
type DenoRecvCb = unsafe extern "C" fn(d: *const DenoC, buf: deno_buf);
@@ -36,18 +34,4 @@ extern "C" {
js_source: *const c_char,
) -> c_int;
pub fn deno_handle_msg_from_js(d: *const DenoC, buf: deno_buf);
- pub fn deno_reply_error(
- d: *const DenoC,
- cmd_id: uint32_t,
- msg: *const c_char,
- );
- pub fn deno_reply_null(d: *const DenoC, cmd_id: uint32_t);
- pub fn deno_reply_code_fetch(
- d: *const DenoC,
- cmd_id: uint32_t,
- module_name: *const c_char,
- filename: *const c_char,
- source_code: *const c_char,
- output_code: *const c_char,
- );
}
diff --git a/src/handlers.rs b/src/handlers.rs
index e8e197e60..0f6ed094d 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -1,20 +1,16 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
-extern crate libc;
-#[macro_use]
-extern crate log;
-extern crate url;
-
+use binding::{deno_buf, deno_set_response, DenoC};
+use flatbuffers;
use libc::c_char;
+use libc::uint32_t;
+use msg_generated::deno as msg;
use std::ffi::CStr;
-use std::ffi::CString;
use std::fs::File;
use std::io::Read;
use std::path::Path;
+use url;
use url::Url;
-mod binding;
-use binding::{deno_reply_code_fetch, deno_reply_error, DenoC};
-
// TODO(ry) SRC_DIR is just a placeholder for future caching functionality.
static SRC_DIR: &str = "/Users/rld/.deno/src/";
const ASSET_PREFIX: &str = "/$asset$/";
@@ -30,9 +26,14 @@ fn string_from_ptr(ptr: *const c_char) -> String {
String::from(cstr.to_str().unwrap())
}
-fn as_cstring(s: &String) -> CString {
- CString::new(s.as_str()).unwrap()
+/*
+// reply_start partially implemented here https://gist.github.com/ry/297c83e0ac8722c045db1b097cdb6afc
+pub fn deno_handle_msg_from_js(d: *const DenoC, buf: deno_buf) {
+ let s = std::slice::from_raw_parts(buf.data_ptr, buf.data_len);
+ buf.data_ptr
+ get_root()
}
+*/
// Prototype: https://github.com/ry/deno/blob/golang/os.go#L56-L68
#[allow(dead_code)]
@@ -183,6 +184,68 @@ fn test_resolve_module() {
}
}
+pub fn reply_code_fetch(
+ d: *const DenoC,
+ cmd_id: uint32_t,
+ module_name: &String,
+ filename: &String,
+ source_code: &String,
+ output_code: &String,
+) {
+ let mut builder = flatbuffers::FlatBufferBuilder::new();
+ let msg_args = msg::CodeFetchResArgs {
+ module_name: builder.create_string(module_name),
+ filename: builder.create_string(filename),
+ source_code: builder.create_string(source_code),
+ output_code: builder.create_string(output_code),
+ ..Default::default()
+ };
+ let msg = msg::CreateCodeFetchRes(&mut builder, &msg_args);
+ builder.finish(msg);
+ let args = msg::BaseArgs {
+ cmdId: cmd_id,
+ msg: Some(msg.union()),
+ msg_type: msg::Any::CodeFetchRes,
+ ..Default::default()
+ };
+ set_response_base(d, &mut builder, &args)
+}
+
+fn reply_error(d: *const DenoC, cmd_id: u32, msg: &String) {
+ let mut builder = flatbuffers::FlatBufferBuilder::new();
+ // println!("reply_error{}", msg);
+ let args = msg::BaseArgs {
+ cmdId: cmd_id,
+ error: builder.create_string(msg),
+ ..Default::default()
+ };
+ set_response_base(d, &mut builder, &args)
+}
+
+fn set_response_base(
+ d: *const DenoC,
+ builder: &mut flatbuffers::FlatBufferBuilder,
+ args: &msg::BaseArgs,
+) {
+ let base = msg::CreateBase(builder, &args);
+ builder.finish(base);
+ let data = builder.get_active_buf_slice();
+ // println!("buf slice {} {} {} {} {}", data[0], data[1], data[2], data[3], data[4]);
+ let buf = deno_buf {
+ // TODO(ry)
+ // The deno_buf / ImportBuf / ExportBuf semantics should be such that we do not need to yield
+ // ownership. Temporarally there is a hack in ImportBuf that when alloc_ptr is null, it will
+ // memcpy the deno_buf into V8 instead of doing zero copy.
+ alloc_ptr: 0 as *mut u8,
+ alloc_len: 0,
+ data_ptr: data.as_ptr() as *mut u8,
+ data_len: data.len(),
+ };
+ // println!("data_ptr {:p}", data_ptr);
+ // println!("data_len {}", data.len());
+ unsafe { deno_set_response(d, buf) }
+}
+
// https://github.com/ry/deno/blob/golang/os.go#L100-L154
#[no_mangle]
pub extern "C" fn handle_code_fetch(
@@ -198,8 +261,7 @@ pub extern "C" fn handle_code_fetch(
if result.is_err() {
let err = result.unwrap_err();
let errmsg = format!("{} {} {}", err, module_specifier, containing_file);
- let errmsg_c = as_cstring(&errmsg);
- unsafe { deno_reply_error(d, cmd_id, errmsg_c.as_ptr()) };
+ reply_error(d, cmd_id, &errmsg);
return;
}
let (module_name, filename) = result.unwrap();
@@ -224,8 +286,7 @@ pub extern "C" fn handle_code_fetch(
if result.is_err() {
let err = result.unwrap_err();
let errmsg = format!("{} {}", err, filename);
- let errmsg_c = as_cstring(&errmsg);
- unsafe { deno_reply_error(d, cmd_id, errmsg_c.as_ptr()) };
+ reply_error(d, cmd_id, &errmsg);
return;
}
let mut f = result.unwrap();
@@ -233,24 +294,21 @@ pub extern "C" fn handle_code_fetch(
if result.is_err() {
let err = result.unwrap_err();
let errmsg = format!("{} {}", err, filename);
- let errmsg_c = as_cstring(&errmsg);
- unsafe { deno_reply_error(d, cmd_id, errmsg_c.as_ptr()) };
+ reply_error(d, cmd_id, &errmsg);
return;
}
}
let output_code = String::new(); //load_output_code_cache(filename, source_code);
- unsafe {
- deno_reply_code_fetch(
- d,
- cmd_id,
- as_cstring(&module_name).as_ptr(),
- as_cstring(&filename).as_ptr(),
- as_cstring(&source_code).as_ptr(),
- as_cstring(&output_code).as_ptr(),
- )
- }
+ reply_code_fetch(
+ d,
+ cmd_id,
+ &module_name,
+ &filename,
+ &source_code,
+ &output_code,
+ )
}
fn is_remote(_module_name: &String) -> bool {
diff --git a/src/main.rs b/src/main.rs
index 2b0a72c91..f9f0172b6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,12 +1,17 @@
extern crate libc;
#[macro_use]
extern crate log;
+extern crate flatbuffers;
+extern crate msg_rs as msg_generated;
+extern crate url;
use libc::c_int;
use std::ffi::CStr;
use std::ffi::CString;
use std::ptr;
+mod handlers;
+pub use handlers::*;
mod binding;
use binding::{
deno_delete, deno_execute, deno_handle_msg_from_js, deno_init,
diff --git a/src/msg.fbs b/src/msg.fbs
index b07da99cf..cb1cf57a9 100644
--- a/src/msg.fbs
+++ b/src/msg.fbs
@@ -23,7 +23,7 @@ table Base {
msg: Any;
}
-struct Start {
+table Start {
unused: int8;
}
@@ -54,22 +54,22 @@ table CodeCache {
output_code: string;
}
-struct Exit {
+table Exit {
code: int;
}
-struct TimerStart {
+table TimerStart {
id: uint;
interval: bool;
delay: int;
}
-struct TimerReady {
+table TimerReady {
id: uint;
done: bool;
}
-struct TimerClear {
+table TimerClear {
id: uint;
}
diff --git a/src/reply.cc b/src/reply.cc
index ffb0d493a..435f76f74 100644
--- a/src/reply.cc
+++ b/src/reply.cc
@@ -21,35 +21,6 @@
extern "C" {
-void deno_reply_error(Deno* d, uint32_t cmd_id, const char* error_msg) {
- // printf("deno_reply_error: %s\n", error_msg);
- deno::FlatBufferBuilder builder;
- auto error_msg_ = error_msg ? builder.CreateString(error_msg) : 0;
- auto base = deno::CreateBase(builder, cmd_id, error_msg_);
- builder.Finish(base);
- deno_set_response(d, builder.ExportBuf());
-}
-
-void deno_reply_null(Deno* d, uint32_t cmd_id) {
- deno_reply_error(d, cmd_id, nullptr);
-}
-
-void deno_reply_code_fetch(Deno* d, uint32_t cmd_id, const char* module_name,
- const char* filename, const char* source_code,
- const char* output_code) {
- deno::FlatBufferBuilder builder;
- auto module_name_ = builder.CreateString(module_name);
- auto filename_ = builder.CreateString(filename);
- auto source_code_ = builder.CreateString(source_code);
- auto output_code_ = builder.CreateString(output_code);
- auto code_fetch_res = deno::CreateCodeFetchRes(
- builder, module_name_, filename_, source_code_, output_code_);
- auto base = deno::CreateBase(builder, cmd_id, 0, deno::Any_CodeFetchRes,
- code_fetch_res.Union());
- builder.Finish(base);
- deno_set_response(d, builder.ExportBuf());
-}
-
void deno_reply_start(Deno* d, uint32_t cmd_id, int argc, char* argv[],
char* cwd) {
deno::FlatBufferBuilder builder;
diff --git a/src/reply.h b/src/reply.h
index 4b16204da..8815abd52 100644
--- a/src/reply.h
+++ b/src/reply.h
@@ -12,16 +12,6 @@
#include "deno.h"
extern "C" {
-
-void deno_reply_null(Deno* d, uint32_t cmd_id);
-void deno_reply_error(Deno* d, uint32_t cmd_id, const char* error_msg);
-
-void deno_reply_start(Deno* d, uint32_t cmd_id, int argc, char* argv[],
- char* cwd);
-void deno_reply_code_fetch(Deno* d, uint32_t cmd_id, const char* module_name,
- const char* filename, const char* source_code,
- const char* output_code);
-
// Parse incoming messages with C++ Flatbuffers, call into rust handlers.
void deno_handle_msg_from_js(Deno* d, deno_buf buf);
}