From 0213053856148379992212b189390f222c6cb460 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 23 Jul 2018 14:13:12 -0400 Subject: [PATCH] Remove handlers crate target. - Add build scripts for Rust flatbuffers. - Rewrites some reply.cc methods in Rust. - Changes some struct elements to table in msg.fbs (rust flatbuffers lacks support currently) - Renames handlers_test to test_rs. - This reorg is needed to make progress on the code cache handler. --- BUILD.gn | 93 ++++---- build_extra/flatbuffers/rust/BUILD.gn | 5 + .../flatbuffers/rust/rust_flatbuffer.gni | 44 ++++ build_extra/rust/rust.gni | 1 + js/main.ts | 5 +- js/msg_generated.ts | 214 ++++++++++++++---- src/binding.rs | 24 +- src/handlers.rs | 112 ++++++--- src/main.rs | 5 + src/msg.fbs | 10 +- src/reply.cc | 29 --- src/reply.h | 10 - tools/test.py | 6 +- 13 files changed, 377 insertions(+), 181 deletions(-) create mode 100644 build_extra/flatbuffers/rust/BUILD.gn create mode 100644 build_extra/flatbuffers/rust/rust_flatbuffer.gni diff --git a/BUILD.gn b/BUILD.gn index 6b1d5d17b4..848dc6a59a 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -2,6 +2,7 @@ import("//third_party/v8/gni/v8.gni") import("//third_party/v8/snapshot_toolchain.gni") import("//build_extra/flatbuffers/flatbuffer.gni") +import("//build_extra/flatbuffers/rust/rust_flatbuffer.gni") import("//build_extra/deno.gni") import("//build_extra/rust/rust.gni") @@ -10,8 +11,8 @@ group("all") { deps = [ ":deno", ":deno_ns", - ":handlers_test", ":test_cc", + ":test_rs", ] } @@ -33,12 +34,29 @@ config("deno_config") { } } +main_extern = [ + "$rust_build:libc", + "$rust_build:log", + "$rust_build:url", + "//build_extra/flatbuffers/rust:flatbuffers", + ":msg_rs", + + # Indirect rust depdendencies also need to be listed here: + # * Linking to `:handlers` or `:libdeno` isn't possible, because they + # already contain some symbols exported by `handlers.rs`. These duplicate + # symbols trip up the linker. + # * The `rust_test` and `rust_executable` templates only produce an object + # file, and then invoke an external linker. Transitive rust depencenies + # are not resolved in either step. + "$rust_build:idna", + "$rust_build:percent_encoding", + "$rust_build:unicode_bidi", + "$rust_build:unicode_normalization", +] + rust_executable("deno") { source_root = "src/main.rs" - extern = [ - "$rust_build:libc", - "$rust_build:log", - ] + extern = main_extern deps = [ ":libdeno", ] @@ -50,43 +68,15 @@ rust_executable("deno") { # ns = no snapshot rust_executable("deno_ns") { source_root = "src/main.rs" - extern = [ - "$rust_build:libc", - "$rust_build:log", - ] + extern = main_extern deps = [ ":libdeno_nosnapshot", ] } -rust_staticlib("handlers") { - source_root = "src/handlers.rs" - extern = [ - "$rust_build:libc", - "$rust_build:url", - "$rust_build:log", - ] -} - -rust_test("handlers_test") { - source_root = "src/handlers.rs" - extern = [ - "$rust_build:libc", - "$rust_build:url", - "$rust_build:log", - - # Indirect rust depdendencies also need to be listed here: - # * Linking to `:handlers` or `:libdeno` isn't possible, because they - # already contain some symbols exported by `handlers.rs`. These duplicate - # symbols trip up the linker. - # * The `rust_test` and `rust_executable` templates only produce an object - # file, and then invoke an external linker. Transitive rust depencenies - # are not resolved in either step. - "$rust_build:idna", - "$rust_build:percent_encoding", - "$rust_build:unicode_bidi", - "$rust_build:unicode_normalization", - ] +rust_test("test_rs") { + source_root = "src/main.rs" + extern = main_extern deps = [ ":deno_bindings", ] @@ -100,8 +90,7 @@ executable("test_cc") { ] deps = [ ":deno_base_test", - ":deno_bindings", - ":handlers", + ":deno_flatbuffer_builder", "//testing/gtest:gtest", ] configs += [ ":deno_config" ] @@ -115,7 +104,6 @@ static_library("libdeno") { deps = [ ":create_snapshot_deno", ":deno_bindings", - ":handlers", ] configs += [ ":deno_config" ] } @@ -154,20 +142,30 @@ v8_source_set("deno_base_test") { configs = [ ":deno_config" ] } -v8_source_set("deno_bindings") { +v8_source_set("deno_flatbuffer_builder") { sources = [ "src/flatbuffer_builder.cc", "src/flatbuffer_builder.h", + ] + deps = [ + ":deno_base", + ] + public_deps = [ + "build_extra/flatbuffers:flatbuffers", + ] + configs = [ ":deno_config" ] +} + +v8_source_set("deno_bindings") { + sources = [ "src/reply.cc", "src/reply.h", ] deps = [ ":deno_base", + ":deno_flatbuffer_builder", ":msg_cpp", ] - public_deps = [ - "build_extra/flatbuffers:flatbuffers", - ] configs = [ ":deno_config" ] } @@ -232,7 +230,6 @@ source_set("libdeno_nosnapshot") { deps = [ ":bundle", ":deno_bindings", - ":handlers", ] configs += [ ":deno_config" ] bundle_outputs = get_target_outputs(":bundle") @@ -274,6 +271,12 @@ ts_flatbuffer("msg_ts") { ] } +rust_flatbuffer("msg_rs") { + sources = [ + "src/msg.fbs", + ] +} + # Generates $target_gen_dir/snapshot_deno.cc create_snapshot("deno") { js = "$target_gen_dir/bundle/main.js" diff --git a/build_extra/flatbuffers/rust/BUILD.gn b/build_extra/flatbuffers/rust/BUILD.gn new file mode 100644 index 0000000000..25ba496eee --- /dev/null +++ b/build_extra/flatbuffers/rust/BUILD.gn @@ -0,0 +1,5 @@ +import("//build_extra/rust/rust.gni") + +rust_component("flatbuffers") { + source_root = "//third_party/flatbuffers/rust/flatbuffers/src/lib.rs" +} diff --git a/build_extra/flatbuffers/rust/rust_flatbuffer.gni b/build_extra/flatbuffers/rust/rust_flatbuffer.gni new file mode 100644 index 0000000000..f5280a8d3a --- /dev/null +++ b/build_extra/flatbuffers/rust/rust_flatbuffer.gni @@ -0,0 +1,44 @@ +import("//build_extra/rust/rust.gni") + +# TODO(ry) "flatbuffer.gni" should be "flatbuffers.gni" we should be consistant +# in our pluralization. +import("//build_extra/flatbuffers/flatbuffer.gni") + +template("rust_flatbuffer") { + action_name = "${target_name}_gen" + source_set_name = target_name + compiled_action_foreach(action_name) { + tool = "$flatbuffers_build_location:flatc" + + sources = invoker.sources + deps = [] + out_dir = target_gen_dir + + outputs = [ + "$out_dir/{{source_name_part}}_generated.rs", + ] + + args = [ + "--rust", + "-o", + rebase_path(out_dir, root_build_dir), + "-I", + rebase_path("//", root_build_dir), + ] + args += [ "{{source}}" ] + + # The deps may have steps that have to run before running flatc. + if (defined(invoker.deps)) { + deps += invoker.deps + } + } + + rust_component(source_set_name) { + sources = get_target_outputs(":$action_name") + source_root = sources[0] + deps = [ + ":$action_name", + ] + extern = [ "//build_extra/flatbuffers/rust:flatbuffers" ] + } +} diff --git a/build_extra/rust/rust.gni b/build_extra/rust/rust.gni index 2fc8228376..f783126ed6 100644 --- a/build_extra/rust/rust.gni +++ b/build_extra/rust/rust.gni @@ -163,6 +163,7 @@ template("rust_component") { "is_test", "testonly", "args", + "deps", ]) if (!defined(invoker.crate_type)) { crate_type = "rlib" diff --git a/js/main.ts b/js/main.ts index 818e6aa126..7c6fc7dc57 100644 --- a/js/main.ts +++ b/js/main.ts @@ -16,10 +16,11 @@ function assignCmdId(): number { function startMsg(cmdId: number): Uint8Array { const builder = new flatbuffers.Builder(); - const msg = fbs.Start.createStart(builder, 0); + fbs.Start.startStart(builder); + const startOffset = fbs.Start.endStart(builder); fbs.Base.startBase(builder); fbs.Base.addCmdId(builder, cmdId); - fbs.Base.addMsg(builder, msg); + fbs.Base.addMsg(builder, startOffset); fbs.Base.addMsgType(builder, fbs.Any.Start); builder.finish(fbs.Base.endBase(builder)); return builder.asUint8Array(); diff --git a/js/msg_generated.ts b/js/msg_generated.ts index af40bf1484..83d75490f2 100644 --- a/js/msg_generated.ts +++ b/js/msg_generated.ts @@ -197,11 +197,21 @@ __init(i:number, bb:flatbuffers.ByteBuffer):Start { return this; }; +/** + * @param {flatbuffers.ByteBuffer} bb + * @param {Start=} obj + * @returns {Start} + */ +static getRootAsStart(bb:flatbuffers.ByteBuffer, obj?:Start):Start { + return (obj || new Start).__init(bb.readInt32(bb.position()) + bb.position(), bb); +}; + /** * @returns {number} */ unused():number { - return this.bb!.readInt8(this.bb_pos); + var offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? this.bb!.readInt8(this.bb_pos + offset) : 0; }; /** @@ -209,7 +219,7 @@ unused():number { * @returns {boolean} */ mutate_unused(value:number):boolean { - var offset = this.bb!.__offset(this.bb_pos, 0); + var offset = this.bb!.__offset(this.bb_pos, 4); if (offset === 0) { return false; @@ -219,15 +229,28 @@ mutate_unused(value:number):boolean { return true; }; +/** + * @param {flatbuffers.Builder} builder + */ +static startStart(builder:flatbuffers.Builder) { + builder.startObject(1); +}; + /** * @param {flatbuffers.Builder} builder * @param {number} unused + */ +static addUnused(builder:flatbuffers.Builder, unused:number) { + builder.addFieldInt8(0, unused, 0); +}; + +/** + * @param {flatbuffers.Builder} builder * @returns {flatbuffers.Offset} */ -static createStart(builder:flatbuffers.Builder, unused: number):flatbuffers.Offset { - builder.prep(1, 1); - builder.writeInt8(unused); - return builder.offset(); +static endStart(builder:flatbuffers.Builder):flatbuffers.Offset { + var offset = builder.endObject(); + return offset; }; } @@ -735,11 +758,21 @@ __init(i:number, bb:flatbuffers.ByteBuffer):Exit { return this; }; +/** + * @param {flatbuffers.ByteBuffer} bb + * @param {Exit=} obj + * @returns {Exit} + */ +static getRootAsExit(bb:flatbuffers.ByteBuffer, obj?:Exit):Exit { + return (obj || new Exit).__init(bb.readInt32(bb.position()) + bb.position(), bb); +}; + /** * @returns {number} */ code():number { - return this.bb!.readInt32(this.bb_pos); + var offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? this.bb!.readInt32(this.bb_pos + offset) : 0; }; /** @@ -747,7 +780,7 @@ code():number { * @returns {boolean} */ mutate_code(value:number):boolean { - var offset = this.bb!.__offset(this.bb_pos, 0); + var offset = this.bb!.__offset(this.bb_pos, 4); if (offset === 0) { return false; @@ -757,15 +790,28 @@ mutate_code(value:number):boolean { return true; }; +/** + * @param {flatbuffers.Builder} builder + */ +static startExit(builder:flatbuffers.Builder) { + builder.startObject(1); +}; + /** * @param {flatbuffers.Builder} builder * @param {number} code + */ +static addCode(builder:flatbuffers.Builder, code:number) { + builder.addFieldInt32(0, code, 0); +}; + +/** + * @param {flatbuffers.Builder} builder * @returns {flatbuffers.Offset} */ -static createExit(builder:flatbuffers.Builder, code: number):flatbuffers.Offset { - builder.prep(4, 4); - builder.writeInt32(code); - return builder.offset(); +static endExit(builder:flatbuffers.Builder):flatbuffers.Offset { + var offset = builder.endObject(); + return offset; }; } @@ -795,11 +841,21 @@ __init(i:number, bb:flatbuffers.ByteBuffer):TimerStart { return this; }; +/** + * @param {flatbuffers.ByteBuffer} bb + * @param {TimerStart=} obj + * @returns {TimerStart} + */ +static getRootAsTimerStart(bb:flatbuffers.ByteBuffer, obj?:TimerStart):TimerStart { + return (obj || new TimerStart).__init(bb.readInt32(bb.position()) + bb.position(), bb); +}; + /** * @returns {number} */ id():number { - return this.bb!.readUint32(this.bb_pos); + var offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? this.bb!.readUint32(this.bb_pos + offset) : 0; }; /** @@ -807,7 +863,7 @@ id():number { * @returns {boolean} */ mutate_id(value:number):boolean { - var offset = this.bb!.__offset(this.bb_pos, 0); + var offset = this.bb!.__offset(this.bb_pos, 4); if (offset === 0) { return false; @@ -821,7 +877,8 @@ mutate_id(value:number):boolean { * @returns {boolean} */ interval():boolean { - return !!this.bb!.readInt8(this.bb_pos + 4); + var offset = this.bb!.__offset(this.bb_pos, 6); + return offset ? !!this.bb!.readInt8(this.bb_pos + offset) : false; }; /** @@ -829,7 +886,7 @@ interval():boolean { * @returns {boolean} */ mutate_interval(value:boolean):boolean { - var offset = this.bb!.__offset(this.bb_pos, 4); + var offset = this.bb!.__offset(this.bb_pos, 6); if (offset === 0) { return false; @@ -843,7 +900,8 @@ mutate_interval(value:boolean):boolean { * @returns {number} */ delay():number { - return this.bb!.readInt32(this.bb_pos + 8); + var offset = this.bb!.__offset(this.bb_pos, 8); + return offset ? this.bb!.readInt32(this.bb_pos + offset) : 0; }; /** @@ -861,20 +919,44 @@ mutate_delay(value:number):boolean { return true; }; +/** + * @param {flatbuffers.Builder} builder + */ +static startTimerStart(builder:flatbuffers.Builder) { + builder.startObject(3); +}; + /** * @param {flatbuffers.Builder} builder * @param {number} id + */ +static addId(builder:flatbuffers.Builder, id:number) { + builder.addFieldInt32(0, id, 0); +}; + +/** + * @param {flatbuffers.Builder} builder * @param {boolean} interval + */ +static addInterval(builder:flatbuffers.Builder, interval:boolean) { + builder.addFieldInt8(1, +interval, +false); +}; + +/** + * @param {flatbuffers.Builder} builder * @param {number} delay + */ +static addDelay(builder:flatbuffers.Builder, delay:number) { + builder.addFieldInt32(2, delay, 0); +}; + +/** + * @param {flatbuffers.Builder} builder * @returns {flatbuffers.Offset} */ -static createTimerStart(builder:flatbuffers.Builder, id: number, interval: boolean, delay: number):flatbuffers.Offset { - builder.prep(4, 12); - builder.writeInt32(delay); - builder.pad(3); - builder.writeInt8(+interval); - builder.writeInt32(id); - return builder.offset(); +static endTimerStart(builder:flatbuffers.Builder):flatbuffers.Offset { + var offset = builder.endObject(); + return offset; }; } @@ -904,11 +986,21 @@ __init(i:number, bb:flatbuffers.ByteBuffer):TimerReady { return this; }; +/** + * @param {flatbuffers.ByteBuffer} bb + * @param {TimerReady=} obj + * @returns {TimerReady} + */ +static getRootAsTimerReady(bb:flatbuffers.ByteBuffer, obj?:TimerReady):TimerReady { + return (obj || new TimerReady).__init(bb.readInt32(bb.position()) + bb.position(), bb); +}; + /** * @returns {number} */ id():number { - return this.bb!.readUint32(this.bb_pos); + var offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? this.bb!.readUint32(this.bb_pos + offset) : 0; }; /** @@ -916,7 +1008,7 @@ id():number { * @returns {boolean} */ mutate_id(value:number):boolean { - var offset = this.bb!.__offset(this.bb_pos, 0); + var offset = this.bb!.__offset(this.bb_pos, 4); if (offset === 0) { return false; @@ -930,7 +1022,8 @@ mutate_id(value:number):boolean { * @returns {boolean} */ done():boolean { - return !!this.bb!.readInt8(this.bb_pos + 4); + var offset = this.bb!.__offset(this.bb_pos, 6); + return offset ? !!this.bb!.readInt8(this.bb_pos + offset) : false; }; /** @@ -938,7 +1031,7 @@ done():boolean { * @returns {boolean} */ mutate_done(value:boolean):boolean { - var offset = this.bb!.__offset(this.bb_pos, 4); + var offset = this.bb!.__offset(this.bb_pos, 6); if (offset === 0) { return false; @@ -948,18 +1041,36 @@ mutate_done(value:boolean):boolean { return true; }; +/** + * @param {flatbuffers.Builder} builder + */ +static startTimerReady(builder:flatbuffers.Builder) { + builder.startObject(2); +}; + /** * @param {flatbuffers.Builder} builder * @param {number} id + */ +static addId(builder:flatbuffers.Builder, id:number) { + builder.addFieldInt32(0, id, 0); +}; + +/** + * @param {flatbuffers.Builder} builder * @param {boolean} done + */ +static addDone(builder:flatbuffers.Builder, done:boolean) { + builder.addFieldInt8(1, +done, +false); +}; + +/** + * @param {flatbuffers.Builder} builder * @returns {flatbuffers.Offset} */ -static createTimerReady(builder:flatbuffers.Builder, id: number, done: boolean):flatbuffers.Offset { - builder.prep(4, 8); - builder.pad(3); - builder.writeInt8(+done); - builder.writeInt32(id); - return builder.offset(); +static endTimerReady(builder:flatbuffers.Builder):flatbuffers.Offset { + var offset = builder.endObject(); + return offset; }; } @@ -989,11 +1100,21 @@ __init(i:number, bb:flatbuffers.ByteBuffer):TimerClear { return this; }; +/** + * @param {flatbuffers.ByteBuffer} bb + * @param {TimerClear=} obj + * @returns {TimerClear} + */ +static getRootAsTimerClear(bb:flatbuffers.ByteBuffer, obj?:TimerClear):TimerClear { + return (obj || new TimerClear).__init(bb.readInt32(bb.position()) + bb.position(), bb); +}; + /** * @returns {number} */ id():number { - return this.bb!.readUint32(this.bb_pos); + var offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? this.bb!.readUint32(this.bb_pos + offset) : 0; }; /** @@ -1001,7 +1122,7 @@ id():number { * @returns {boolean} */ mutate_id(value:number):boolean { - var offset = this.bb!.__offset(this.bb_pos, 0); + var offset = this.bb!.__offset(this.bb_pos, 4); if (offset === 0) { return false; @@ -1011,15 +1132,28 @@ mutate_id(value:number):boolean { return true; }; +/** + * @param {flatbuffers.Builder} builder + */ +static startTimerClear(builder:flatbuffers.Builder) { + builder.startObject(1); +}; + /** * @param {flatbuffers.Builder} builder * @param {number} id + */ +static addId(builder:flatbuffers.Builder, id:number) { + builder.addFieldInt32(0, id, 0); +}; + +/** + * @param {flatbuffers.Builder} builder * @returns {flatbuffers.Offset} */ -static createTimerClear(builder:flatbuffers.Builder, id: number):flatbuffers.Offset { - builder.prep(4, 4); - builder.writeInt32(id); - return builder.offset(); +static endTimerClear(builder:flatbuffers.Builder):flatbuffers.Offset { + var offset = builder.endObject(); + return offset; }; } diff --git a/src/binding.rs b/src/binding.rs index 72e8af996b..1ef376dff1 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 e8e197e60c..0f6ed094d4 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 2b0a72c917..f9f0172b6e 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 b07da99cf4..cb1cf57a91 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 ffb0d493a0..435f76f74c 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 4b16204da3..8815abd52d 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); } diff --git a/tools/test.py b/tools/test.py index e62efd0f6b..3edfa4e015 100755 --- a/tools/test.py +++ b/tools/test.py @@ -24,9 +24,9 @@ def main(argv): check_exists(test_cc) run([test_cc]) - handlers_test = os.path.join(build_dir, "handlers_test" + executable_suffix) - check_exists(handlers_test) - run([handlers_test]) + test_rs = os.path.join(build_dir, "test_rs" + executable_suffix) + check_exists(test_rs) + run([test_rs]) deno_exe = os.path.join(build_dir, "deno" + executable_suffix) check_exists(deno_exe)