diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/handlers.h | 9 | ||||
-rw-r--r-- | src/handlers.rs | 27 | ||||
-rw-r--r-- | src/main.cc | 67 | ||||
-rw-r--r-- | src/msg.fbs | 101 |
4 files changed, 185 insertions, 19 deletions
diff --git a/src/handlers.h b/src/handlers.h new file mode 100644 index 000000000..921ae62b1 --- /dev/null +++ b/src/handlers.h @@ -0,0 +1,9 @@ +// Copyright 2018 Ryan Dahl <ry@tinyclouds.org> +// All rights reserved. MIT License. +#ifndef HANDLERS_H_ +#define HANDLERS_H_ +extern "C" { +void handle_code_fetch(const char* module_specifier, + const char* containing_file); +} +#endif // HANDLERS_H_ diff --git a/src/handlers.rs b/src/handlers.rs new file mode 100644 index 000000000..64a076f11 --- /dev/null +++ b/src/handlers.rs @@ -0,0 +1,27 @@ +// Copyright 2018 Ryan Dahl <ry@tinyclouds.org> +// All rights reserved. MIT License. +extern crate libc; + +use libc::c_char; +use std::ffi::CStr; + +fn string_from_ptr(ptr: *const c_char) -> String { + let cstr = unsafe { CStr::from_ptr(ptr as *const i8) }; + String::from(cstr.to_str().unwrap()) +} + +#[no_mangle] +pub extern "C" fn handle_code_fetch( + module_specifier: *const c_char, + containing_file: *const c_char, +) { + let module_specifier = string_from_ptr(module_specifier); + let containing_file = string_from_ptr(containing_file); + + println!( + "handle_code_fetch. module_specifier = {} containing_file = {}", + module_specifier, containing_file + ); + + unimplemented!(); +} diff --git a/src/main.cc b/src/main.cc index 9596dbddf..9dee442df 100644 --- a/src/main.cc +++ b/src/main.cc @@ -12,15 +12,17 @@ #include "deno.h" #include "flatbuffers/flatbuffers.h" +#include "src/handlers.h" #include "src/msg_generated.h" #include "third_party/v8/src/base/logging.h" +namespace deno { + static char** global_argv; static int global_argc; -void MessagesFromJS(Deno* d, const char* channel, deno_buf buf) { - printf("MessagesFromJS %s\n", channel); - +// Sends StartRes message +void HandleStart(Deno* d) { flatbuffers::FlatBufferBuilder builder; char cwdbuf[1024]; @@ -32,22 +34,56 @@ void MessagesFromJS(Deno* d, const char* channel, deno_buf buf) { for (int i = 0; i < global_argc; ++i) { args.push_back(builder.CreateString(global_argv[i])); } - auto start_argv = builder.CreateVector(args); - - deno::MsgBuilder msg_builder(builder); - msg_builder.add_command(deno::Command_START); - msg_builder.add_start_cwd(start_cwd); - msg_builder.add_start_argv(start_argv); - - auto response = msg_builder.Finish(); - builder.Finish(response); + auto start_argv = builder.CreateVector(args); + auto start_msg = CreateStartRes(builder, start_cwd, start_argv); + auto base = CreateBase(builder, 0, Any_StartRes, start_msg.Union()); + builder.Finish(base); deno_buf bufout{reinterpret_cast<const char*>(builder.GetBufferPointer()), builder.GetSize()}; deno_set_response(d, bufout); } -int main(int argc, char** argv) { +void HandleCodeFetch(Deno* d, const CodeFetch* msg) { + auto module_specifier = msg->module_specifier()->c_str(); + auto containing_file = msg->containing_file()->c_str(); + printf("HandleCodeFetch module_specifier = %s containing_file = %s\n", + module_specifier, containing_file); + // Call into rust. + handle_code_fetch(module_specifier, containing_file); +} + +void MessagesFromJS(Deno* d, const char* channel, deno_buf buf) { + auto data = reinterpret_cast<const uint8_t*>(buf.data); + flatbuffers::Verifier verifier(data, buf.len); + DCHECK(verifier.VerifyBuffer<Base>()); + + auto base = flatbuffers::GetRoot<Base>(buf.data); + auto msg_type = base->msg_type(); + const char* msg_type_name = EnumNamesAny()[msg_type]; + printf("MessagesFromJS channel %s, msg_type = %d, msg_type_name = %s\n", + channel, msg_type, msg_type_name); + switch (msg_type) { + case Any_Start: + HandleStart(d); + break; + + case Any_CodeFetch: + HandleCodeFetch(d, base->msg_as_CodeFetch()); + break; + + case Any_NONE: + CHECK(false && "Got message with msg_type == Any_NONE"); + break; + + default: + printf("Unhandled message %s\n", msg_type_name); + CHECK(false && "Unhandled message"); + break; + } +} + +int deno_main(int argc, char** argv) { deno_init(); deno_set_flags(&argc, argv); @@ -61,4 +97,9 @@ int main(int argc, char** argv) { exit(1); } deno_delete(d); + return 0; } + +} // namespace deno + +int main(int argc, char** argv) { return deno::deno_main(argc, argv); } diff --git a/src/msg.fbs b/src/msg.fbs index 3dbb8e420..bda3d6354 100644 --- a/src/msg.fbs +++ b/src/msg.fbs @@ -1,12 +1,101 @@ namespace deno; -enum Command: byte { - START = 0, +union Any { + Start, + StartRes, + CodeFetch, + CodeFetchRes, + CodeCache, + Exit, + TimerStart, + TimerReady, + TimerClear, + FetchReq, + FetchRes, + ReadFileSync, + ReadFileSyncRes, + WriteFileSync, } -table Msg { - command: Command; - start_cwd: string; - start_argv: [string]; +table Base { + error: string; + msg: Any; } +struct Start { + unused: int8; +} + +table StartRes { + cwd: string; + argv: [string]; + debug_flag: bool; +} + +table CodeFetch { + module_specifier: string; + containing_file: string; +} + +table CodeFetchRes { + // If it's a non-http module, moduleName and filename will be the same. + // For http modules, moduleName is its resolved http URL, and filename + // is the location of the locally downloaded source code. + module_name: string; + filename: string; + source_code: string; + output_code: string; // Non-empty only if cached. +} + +table CodeCache { + filename: string; + source_code: string; + output_code: string; +} + +struct Exit { + code: int; +} + +struct TimerStart { + id: uint; + interval: bool; + delay: int; +} + +struct TimerReady { + id: uint; + done: bool; +} + +struct TimerClear { + id: uint; +} + +table FetchReq { + id: uint; + url: string; + // header_line: [string]; +} + +table FetchRes { + id: uint; + status: int; + header_line: [string]; + body: [byte]; +} + +table ReadFileSync { + filename: string; +} + +table ReadFileSyncRes { + data: [byte]; +} + +table WriteFileSync { + filename: string; + data: [byte]; + perm: uint; + // perm specified by https://godoc.org/os#FileMode +} |