From bfe08aa4629ee2834ac5c7eaea4db164db1e66ae Mon Sep 17 00:00:00 2001 From: Faris Amali Alis Date: Fri, 6 Jul 2018 15:19:19 +0800 Subject: Rename deno.cc to binding.cc and other renames (#339) Fixes #336 --- src/binding.cc | 315 +++++++++++++++++++++++++++++++++++++++++++++++ src/deno.cc | 315 ----------------------------------------------- src/deno.h | 53 ++++++++ src/deno_internal.h | 44 ------- src/from_snapshot.cc | 4 +- src/include/deno.h | 53 -------- src/internal.h | 44 +++++++ src/main.cc | 2 +- src/mock_runtime_test.cc | 2 +- src/snapshot_creator.cc | 4 +- 10 files changed, 418 insertions(+), 418 deletions(-) create mode 100644 src/binding.cc delete mode 100644 src/deno.cc create mode 100644 src/deno.h delete mode 100644 src/deno_internal.h delete mode 100644 src/include/deno.h create mode 100644 src/internal.h (limited to 'src') diff --git a/src/binding.cc b/src/binding.cc new file mode 100644 index 000000000..cd8c67ef5 --- /dev/null +++ b/src/binding.cc @@ -0,0 +1,315 @@ +/* +Copyright 2018 Ryan Dahl . All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to +deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +IN THE SOFTWARE. +*/ +#include +#include +#include +#include + +#include "third_party/v8/include/libplatform/libplatform.h" +#include "third_party/v8/include/v8.h" +#include "third_party/v8/src/base/logging.h" + +#include "internal.h" +#include "deno.h" + +namespace deno { + +// Extracts a C string from a v8::V8 Utf8Value. +const char* ToCString(const v8::String::Utf8Value& value) { + return *value ? *value : ""; +} + +static inline v8::Local v8_str(const char* x) { + return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x, + v8::NewStringType::kNormal) + .ToLocalChecked(); +} + +void HandleException(v8::Local context, + v8::Local exception) { + auto* isolate = context->GetIsolate(); + v8::HandleScope handle_scope(isolate); + v8::Context::Scope context_scope(context); + + auto message = v8::Exception::CreateMessage(isolate, exception); + auto onerrorStr = v8::String::NewFromUtf8(isolate, "onerror"); + auto onerror = context->Global()->Get(onerrorStr); + + if (onerror->IsFunction()) { + auto func = v8::Local::Cast(onerror); + v8::Local args[5]; + auto line = + v8::Integer::New(isolate, message->GetLineNumber(context).FromJust()); + auto column = + v8::Integer::New(isolate, message->GetStartColumn(context).FromJust()); + args[0] = exception->ToString(); + args[1] = message->GetScriptResourceName(); + args[2] = line; + args[3] = column; + args[4] = exception; + func->Call(context->Global(), 5, args); + /* message, source, lineno, colno, error */ + } else { + v8::String::Utf8Value exceptionStr(isolate, exception); + printf("Unhandled Exception %s\n", ToCString(exceptionStr)); + message->PrintCurrentStackTrace(isolate, stdout); + } +} + +/* +bool AbortOnUncaughtExceptionCallback(v8::Isolate* isolate) { + return true; +} + +void MessageCallback2(Local message, v8::Local data) { + printf("MessageCallback2\n\n"); +} + +void FatalErrorCallback2(const char* location, const char* message) { + printf("FatalErrorCallback2\n"); +} +*/ + +void ExitOnPromiseRejectCallback( + v8::PromiseRejectMessage promise_reject_message) { + auto* isolate = v8::Isolate::GetCurrent(); + Deno* d = static_cast(isolate->GetData(0)); + DCHECK_EQ(d->isolate, isolate); + v8::HandleScope handle_scope(d->isolate); + auto exception = promise_reject_message.GetValue(); + auto context = d->context.Get(d->isolate); + HandleException(context, exception); +} + +void Print(const v8::FunctionCallbackInfo& args) { + CHECK_EQ(args.Length(), 1); + auto* isolate = args.GetIsolate(); + v8::HandleScope handle_scope(isolate); + v8::String::Utf8Value str(isolate, args[0]); + const char* cstr = ToCString(str); + printf("%s\n", cstr); + fflush(stdout); +} + +// Sets the recv callback. +void Recv(const v8::FunctionCallbackInfo& args) { + v8::Isolate* isolate = args.GetIsolate(); + Deno* d = reinterpret_cast(isolate->GetData(0)); + DCHECK_EQ(d->isolate, isolate); + + v8::HandleScope handle_scope(isolate); + + if (!d->recv.IsEmpty()) { + isolate->ThrowException(v8_str("deno.recv already called.")); + return; + } + + v8::Local v = args[0]; + CHECK(v->IsFunction()); + v8::Local func = v8::Local::Cast(v); + + d->recv.Reset(isolate, func); +} + +void Send(const v8::FunctionCallbackInfo& args) { + v8::Isolate* isolate = args.GetIsolate(); + Deno* d = static_cast(isolate->GetData(0)); + DCHECK_EQ(d->isolate, isolate); + + v8::Locker locker(d->isolate); + v8::EscapableHandleScope handle_scope(isolate); + + CHECK_EQ(args.Length(), 2); + v8::Local channel_v = args[0]; + CHECK(channel_v->IsString()); + v8::String::Utf8Value channel_vstr(isolate, channel_v); + const char* channel = *channel_vstr; + + v8::Local ab_v = args[1]; + CHECK(ab_v->IsArrayBuffer()); + + auto ab = v8::Local::Cast(ab_v); + auto contents = ab->GetContents(); + + // data is only a valid pointer until the end of this call. + const char* data = + const_cast(reinterpret_cast(contents.Data())); + deno_buf buf{data, contents.ByteLength()}; + + DCHECK_EQ(d->currentArgs, nullptr); + d->currentArgs = &args; + + d->cb(d, channel, buf); + + d->currentArgs = nullptr; +} + +bool Execute(v8::Local context, const char* js_filename, + const char* js_source) { + auto* isolate = context->GetIsolate(); + v8::Isolate::Scope isolate_scope(isolate); + v8::HandleScope handle_scope(isolate); + + v8::Context::Scope context_scope(context); + + v8::TryCatch try_catch(isolate); + + auto name = v8_str(js_filename); + auto source = v8_str(js_source); + + v8::ScriptOrigin origin(name); + + auto script = v8::Script::Compile(context, source, &origin); + + if (script.IsEmpty()) { + DCHECK(try_catch.HasCaught()); + HandleException(context, try_catch.Exception()); + return false; + } + + auto result = script.ToLocalChecked()->Run(context); + + if (result.IsEmpty()) { + DCHECK(try_catch.HasCaught()); + HandleException(context, try_catch.Exception()); + return false; + } + + return true; +} + +void InitializeContext(v8::Isolate* isolate, v8::Local context, + const char* js_filename, const char* js_source) { + v8::HandleScope handle_scope(isolate); + v8::Context::Scope context_scope(context); + + auto global = context->Global(); + + auto deno_val = v8::Object::New(isolate); + CHECK(global->Set(context, deno::v8_str("deno"), deno_val).FromJust()); + + auto print_tmpl = v8::FunctionTemplate::New(isolate, Print); + auto print_val = print_tmpl->GetFunction(context).ToLocalChecked(); + CHECK(deno_val->Set(context, deno::v8_str("print"), print_val).FromJust()); + + auto recv_tmpl = v8::FunctionTemplate::New(isolate, Recv); + auto recv_val = recv_tmpl->GetFunction(context).ToLocalChecked(); + CHECK(deno_val->Set(context, deno::v8_str("recv"), recv_val).FromJust()); + + auto send_tmpl = v8::FunctionTemplate::New(isolate, Send); + auto send_val = send_tmpl->GetFunction(context).ToLocalChecked(); + CHECK(deno_val->Set(context, deno::v8_str("send"), send_val).FromJust()); + + bool r = Execute(context, js_filename, js_source); + CHECK(r); +} + +void AddIsolate(Deno* d, v8::Isolate* isolate) { + d->isolate = isolate; + // Leaving this code here because it will probably be useful later on, but + // disabling it now as I haven't got tests for the desired behavior. + // d->isolate->SetCaptureStackTraceForUncaughtExceptions(true); + // d->isolate->SetAbortOnUncaughtExceptionCallback(AbortOnUncaughtExceptionCallback); + // d->isolate->AddMessageListener(MessageCallback2); + // d->isolate->SetFatalErrorHandler(FatalErrorCallback2); + d->isolate->SetPromiseRejectCallback(deno::ExitOnPromiseRejectCallback); + d->isolate->SetData(0, d); +} + +} // namespace deno + +extern "C" { + +void deno_init() { + // v8::V8::InitializeICUDefaultLocation(argv[0]); + // v8::V8::InitializeExternalStartupData(argv[0]); + auto* p = v8::platform::CreateDefaultPlatform(); + v8::V8::InitializePlatform(p); + v8::V8::Initialize(); +} + +const char* deno_v8_version() { return v8::V8::GetVersion(); } + +void deno_set_flags(int* argc, char** argv) { + v8::V8::SetFlagsFromCommandLine(argc, argv, true); +} + +const char* deno_last_exception(Deno* d) { return d->last_exception.c_str(); } + +int deno_execute(Deno* d, const char* js_filename, const char* js_source) { + auto* isolate = d->isolate; + v8::Locker locker(isolate); + v8::Isolate::Scope isolate_scope(isolate); + v8::HandleScope handle_scope(isolate); + auto context = d->context.Get(d->isolate); + return deno::Execute(context, js_filename, js_source) ? 1 : 0; +} + +int deno_send(Deno* d, const char* channel, deno_buf buf) { + v8::Locker locker(d->isolate); + v8::Isolate::Scope isolate_scope(d->isolate); + v8::HandleScope handle_scope(d->isolate); + + auto context = d->context.Get(d->isolate); + v8::Context::Scope context_scope(context); + + v8::TryCatch try_catch(d->isolate); + + auto recv = d->recv.Get(d->isolate); + if (recv.IsEmpty()) { + d->last_exception = "deno.recv has not been called."; + return 0; + } + + // TODO(ry) support zero-copy. + auto ab = v8::ArrayBuffer::New(d->isolate, buf.len); + memcpy(ab->GetContents().Data(), buf.data, buf.len); + + v8::Local args[2]; + args[0] = deno::v8_str(channel); + args[1] = ab; + + recv->Call(context->Global(), 1, args); + + if (try_catch.HasCaught()) { + deno::HandleException(context, try_catch.Exception()); + return 0; + } + + return 1; +} + +void deno_set_response(Deno* d, deno_buf buf) { + // TODO(ry) Support zero-copy. + auto ab = v8::ArrayBuffer::New(d->isolate, buf.len); + memcpy(ab->GetContents().Data(), buf.data, buf.len); + d->currentArgs->GetReturnValue().Set(ab); +} + +void deno_delete(Deno* d) { + d->isolate->Dispose(); + delete d; +} + +void deno_terminate_execution(Deno* d) { d->isolate->TerminateExecution(); } + +} // extern "C" diff --git a/src/deno.cc b/src/deno.cc deleted file mode 100644 index af8b621cc..000000000 --- a/src/deno.cc +++ /dev/null @@ -1,315 +0,0 @@ -/* -Copyright 2018 Ryan Dahl . All rights reserved. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to -deal in the Software without restriction, including without limitation the -rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -sell copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -IN THE SOFTWARE. -*/ -#include -#include -#include -#include - -#include "third_party/v8/include/libplatform/libplatform.h" -#include "third_party/v8/include/v8.h" -#include "third_party/v8/src/base/logging.h" - -#include "./deno_internal.h" -#include "include/deno.h" - -namespace deno { - -// Extracts a C string from a v8::V8 Utf8Value. -const char* ToCString(const v8::String::Utf8Value& value) { - return *value ? *value : ""; -} - -static inline v8::Local v8_str(const char* x) { - return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x, - v8::NewStringType::kNormal) - .ToLocalChecked(); -} - -void HandleException(v8::Local context, - v8::Local exception) { - auto* isolate = context->GetIsolate(); - v8::HandleScope handle_scope(isolate); - v8::Context::Scope context_scope(context); - - auto message = v8::Exception::CreateMessage(isolate, exception); - auto onerrorStr = v8::String::NewFromUtf8(isolate, "onerror"); - auto onerror = context->Global()->Get(onerrorStr); - - if (onerror->IsFunction()) { - auto func = v8::Local::Cast(onerror); - v8::Local args[5]; - auto line = - v8::Integer::New(isolate, message->GetLineNumber(context).FromJust()); - auto column = - v8::Integer::New(isolate, message->GetStartColumn(context).FromJust()); - args[0] = exception->ToString(); - args[1] = message->GetScriptResourceName(); - args[2] = line; - args[3] = column; - args[4] = exception; - func->Call(context->Global(), 5, args); - /* message, source, lineno, colno, error */ - } else { - v8::String::Utf8Value exceptionStr(isolate, exception); - printf("Unhandled Exception %s\n", ToCString(exceptionStr)); - message->PrintCurrentStackTrace(isolate, stdout); - } -} - -/* -bool AbortOnUncaughtExceptionCallback(v8::Isolate* isolate) { - return true; -} - -void MessageCallback2(Local message, v8::Local data) { - printf("MessageCallback2\n\n"); -} - -void FatalErrorCallback2(const char* location, const char* message) { - printf("FatalErrorCallback2\n"); -} -*/ - -void ExitOnPromiseRejectCallback( - v8::PromiseRejectMessage promise_reject_message) { - auto* isolate = v8::Isolate::GetCurrent(); - Deno* d = static_cast(isolate->GetData(0)); - DCHECK_EQ(d->isolate, isolate); - v8::HandleScope handle_scope(d->isolate); - auto exception = promise_reject_message.GetValue(); - auto context = d->context.Get(d->isolate); - HandleException(context, exception); -} - -void Print(const v8::FunctionCallbackInfo& args) { - CHECK_EQ(args.Length(), 1); - auto* isolate = args.GetIsolate(); - v8::HandleScope handle_scope(isolate); - v8::String::Utf8Value str(isolate, args[0]); - const char* cstr = ToCString(str); - printf("%s\n", cstr); - fflush(stdout); -} - -// Sets the recv callback. -void Recv(const v8::FunctionCallbackInfo& args) { - v8::Isolate* isolate = args.GetIsolate(); - Deno* d = reinterpret_cast(isolate->GetData(0)); - DCHECK_EQ(d->isolate, isolate); - - v8::HandleScope handle_scope(isolate); - - if (!d->recv.IsEmpty()) { - isolate->ThrowException(v8_str("deno.recv already called.")); - return; - } - - v8::Local v = args[0]; - CHECK(v->IsFunction()); - v8::Local func = v8::Local::Cast(v); - - d->recv.Reset(isolate, func); -} - -void Send(const v8::FunctionCallbackInfo& args) { - v8::Isolate* isolate = args.GetIsolate(); - Deno* d = static_cast(isolate->GetData(0)); - DCHECK_EQ(d->isolate, isolate); - - v8::Locker locker(d->isolate); - v8::EscapableHandleScope handle_scope(isolate); - - CHECK_EQ(args.Length(), 2); - v8::Local channel_v = args[0]; - CHECK(channel_v->IsString()); - v8::String::Utf8Value channel_vstr(isolate, channel_v); - const char* channel = *channel_vstr; - - v8::Local ab_v = args[1]; - CHECK(ab_v->IsArrayBuffer()); - - auto ab = v8::Local::Cast(ab_v); - auto contents = ab->GetContents(); - - // data is only a valid pointer until the end of this call. - const char* data = - const_cast(reinterpret_cast(contents.Data())); - deno_buf buf{data, contents.ByteLength()}; - - DCHECK_EQ(d->currentArgs, nullptr); - d->currentArgs = &args; - - d->cb(d, channel, buf); - - d->currentArgs = nullptr; -} - -bool Execute(v8::Local context, const char* js_filename, - const char* js_source) { - auto* isolate = context->GetIsolate(); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope handle_scope(isolate); - - v8::Context::Scope context_scope(context); - - v8::TryCatch try_catch(isolate); - - auto name = v8_str(js_filename); - auto source = v8_str(js_source); - - v8::ScriptOrigin origin(name); - - auto script = v8::Script::Compile(context, source, &origin); - - if (script.IsEmpty()) { - DCHECK(try_catch.HasCaught()); - HandleException(context, try_catch.Exception()); - return false; - } - - auto result = script.ToLocalChecked()->Run(context); - - if (result.IsEmpty()) { - DCHECK(try_catch.HasCaught()); - HandleException(context, try_catch.Exception()); - return false; - } - - return true; -} - -void InitializeContext(v8::Isolate* isolate, v8::Local context, - const char* js_filename, const char* js_source) { - v8::HandleScope handle_scope(isolate); - v8::Context::Scope context_scope(context); - - auto global = context->Global(); - - auto deno_val = v8::Object::New(isolate); - CHECK(global->Set(context, deno::v8_str("deno"), deno_val).FromJust()); - - auto print_tmpl = v8::FunctionTemplate::New(isolate, Print); - auto print_val = print_tmpl->GetFunction(context).ToLocalChecked(); - CHECK(deno_val->Set(context, deno::v8_str("print"), print_val).FromJust()); - - auto recv_tmpl = v8::FunctionTemplate::New(isolate, Recv); - auto recv_val = recv_tmpl->GetFunction(context).ToLocalChecked(); - CHECK(deno_val->Set(context, deno::v8_str("recv"), recv_val).FromJust()); - - auto send_tmpl = v8::FunctionTemplate::New(isolate, Send); - auto send_val = send_tmpl->GetFunction(context).ToLocalChecked(); - CHECK(deno_val->Set(context, deno::v8_str("send"), send_val).FromJust()); - - bool r = Execute(context, js_filename, js_source); - CHECK(r); -} - -void AddIsolate(Deno* d, v8::Isolate* isolate) { - d->isolate = isolate; - // Leaving this code here because it will probably be useful later on, but - // disabling it now as I haven't got tests for the desired behavior. - // d->isolate->SetCaptureStackTraceForUncaughtExceptions(true); - // d->isolate->SetAbortOnUncaughtExceptionCallback(AbortOnUncaughtExceptionCallback); - // d->isolate->AddMessageListener(MessageCallback2); - // d->isolate->SetFatalErrorHandler(FatalErrorCallback2); - d->isolate->SetPromiseRejectCallback(deno::ExitOnPromiseRejectCallback); - d->isolate->SetData(0, d); -} - -} // namespace deno - -extern "C" { - -void deno_init() { - // v8::V8::InitializeICUDefaultLocation(argv[0]); - // v8::V8::InitializeExternalStartupData(argv[0]); - auto* p = v8::platform::CreateDefaultPlatform(); - v8::V8::InitializePlatform(p); - v8::V8::Initialize(); -} - -const char* deno_v8_version() { return v8::V8::GetVersion(); } - -void deno_set_flags(int* argc, char** argv) { - v8::V8::SetFlagsFromCommandLine(argc, argv, true); -} - -const char* deno_last_exception(Deno* d) { return d->last_exception.c_str(); } - -int deno_execute(Deno* d, const char* js_filename, const char* js_source) { - auto* isolate = d->isolate; - v8::Locker locker(isolate); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope handle_scope(isolate); - auto context = d->context.Get(d->isolate); - return deno::Execute(context, js_filename, js_source) ? 1 : 0; -} - -int deno_send(Deno* d, const char* channel, deno_buf buf) { - v8::Locker locker(d->isolate); - v8::Isolate::Scope isolate_scope(d->isolate); - v8::HandleScope handle_scope(d->isolate); - - auto context = d->context.Get(d->isolate); - v8::Context::Scope context_scope(context); - - v8::TryCatch try_catch(d->isolate); - - auto recv = d->recv.Get(d->isolate); - if (recv.IsEmpty()) { - d->last_exception = "deno.recv has not been called."; - return 0; - } - - // TODO(ry) support zero-copy. - auto ab = v8::ArrayBuffer::New(d->isolate, buf.len); - memcpy(ab->GetContents().Data(), buf.data, buf.len); - - v8::Local args[2]; - args[0] = deno::v8_str(channel); - args[1] = ab; - - recv->Call(context->Global(), 1, args); - - if (try_catch.HasCaught()) { - deno::HandleException(context, try_catch.Exception()); - return 0; - } - - return 1; -} - -void deno_set_response(Deno* d, deno_buf buf) { - // TODO(ry) Support zero-copy. - auto ab = v8::ArrayBuffer::New(d->isolate, buf.len); - memcpy(ab->GetContents().Data(), buf.data, buf.len); - d->currentArgs->GetReturnValue().Set(ab); -} - -void deno_delete(Deno* d) { - d->isolate->Dispose(); - delete d; -} - -void deno_terminate_execution(Deno* d) { d->isolate->TerminateExecution(); } - -} // extern "C" diff --git a/src/deno.h b/src/deno.h new file mode 100644 index 000000000..cd9ade42f --- /dev/null +++ b/src/deno.h @@ -0,0 +1,53 @@ +// Copyright 2018 Ryan Dahl +// All rights reserved. MIT License. +#ifndef DENO_H_ +#define DENO_H_ +// Neither Rust nor Go support calling directly into C++ functions, therefore +// the public interface to libdeno is done in C. +#ifdef __cplusplus +extern "C" { +#endif + +// Data that gets transmitted. +typedef struct { + const char* data; + size_t len; +} deno_buf; + +struct deno_s; +typedef struct deno_s Deno; + +// A callback to receive a message from deno.send javascript call. +// buf is valid only for the lifetime of the call. +typedef void (*deno_recv_cb)(Deno* d, const char* channel, deno_buf buf); + +void deno_init(); +const char* deno_v8_version(); +void deno_set_flags(int* argc, char** argv); + +Deno* deno_new(void* data, deno_recv_cb cb); +void deno_delete(Deno* d); + +// Returns false on error. +// Get error text with deno_last_exception(). +// 0 = fail, 1 = success +int deno_execute(Deno* d, const char* js_filename, const char* js_source); + +// Routes message to the javascript callback set with deno.recv(). A false +// return value indicates error. Check deno_last_exception() for exception text. +// 0 = fail, 1 = success +int deno_send(Deno* d, const char* channel, deno_buf buf); + +// Call this inside a deno_recv_cb to respond synchronously to messages. +// If this is not called during the life time of a deno_recv_cb callback +// the deno.send() call in javascript will return null. +void deno_set_response(Deno* d, deno_buf buf); + +const char* deno_last_exception(Deno* d); + +void deno_terminate_execution(Deno* d); + +#ifdef __cplusplus +} // extern "C" +#endif +#endif // DENO_H_ diff --git a/src/deno_internal.h b/src/deno_internal.h deleted file mode 100644 index ad0a6f537..000000000 --- a/src/deno_internal.h +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright 2018 Ryan Dahl -// All rights reserved. MIT License. -#ifndef DENO_INTERNAL_H_ -#define DENO_INTERNAL_H_ - -#include -#include "include/deno.h" -#include "third_party/v8/include/v8.h" - -extern "C" { -// deno_s = Wrapped Isolate. -struct deno_s { - v8::Isolate* isolate; - const v8::FunctionCallbackInfo* currentArgs; - std::string last_exception; - v8::Persistent recv; - v8::Persistent context; - deno_recv_cb cb; - void* data; -}; -} - -namespace deno { - -struct InternalFieldData { - uint32_t data; -}; - -void Print(const v8::FunctionCallbackInfo& args); -void Recv(const v8::FunctionCallbackInfo& args); -void Send(const v8::FunctionCallbackInfo& args); -static intptr_t external_references[] = {reinterpret_cast(Print), - reinterpret_cast(Recv), - reinterpret_cast(Send), 0}; - -Deno* NewFromSnapshot(void* data, deno_recv_cb cb); - -void InitializeContext(v8::Isolate* isolate, v8::Local context, - const char* js_filename, const char* js_source); - -void AddIsolate(Deno* d, v8::Isolate* isolate); - -} // namespace deno -#endif // DENO_INTERNAL_H_ diff --git a/src/from_snapshot.cc b/src/from_snapshot.cc index 8248997e2..5dd32025a 100644 --- a/src/from_snapshot.cc +++ b/src/from_snapshot.cc @@ -8,8 +8,8 @@ #include "third_party/v8/include/v8.h" #include "third_party/v8/src/base/logging.h" -#include "./deno_internal.h" -#include "include/deno.h" +#include "internal.h" +#include "deno.h" #ifdef DENO_MOCK_RUNTIME #include "snapshot_mock_runtime.cc" diff --git a/src/include/deno.h b/src/include/deno.h deleted file mode 100644 index e6516e427..000000000 --- a/src/include/deno.h +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2018 Ryan Dahl -// All rights reserved. MIT License. -#ifndef INCLUDE_DENO_H_ -#define INCLUDE_DENO_H_ -// Neither Rust nor Go support calling directly into C++ functions, therefore -// the public interface to libdeno is done in C. -#ifdef __cplusplus -extern "C" { -#endif - -// Data that gets transmitted. -typedef struct { - const char* data; - size_t len; -} deno_buf; - -struct deno_s; -typedef struct deno_s Deno; - -// A callback to receive a message from deno.send javascript call. -// buf is valid only for the lifetime of the call. -typedef void (*deno_recv_cb)(Deno* d, const char* channel, deno_buf buf); - -void deno_init(); -const char* deno_v8_version(); -void deno_set_flags(int* argc, char** argv); - -Deno* deno_new(void* data, deno_recv_cb cb); -void deno_delete(Deno* d); - -// Returns false on error. -// Get error text with deno_last_exception(). -// 0 = fail, 1 = success -int deno_execute(Deno* d, const char* js_filename, const char* js_source); - -// Routes message to the javascript callback set with deno.recv(). A false -// return value indicates error. Check deno_last_exception() for exception text. -// 0 = fail, 1 = success -int deno_send(Deno* d, const char* channel, deno_buf buf); - -// Call this inside a deno_recv_cb to respond synchronously to messages. -// If this is not called during the life time of a deno_recv_cb callback -// the deno.send() call in javascript will return null. -void deno_set_response(Deno* d, deno_buf buf); - -const char* deno_last_exception(Deno* d); - -void deno_terminate_execution(Deno* d); - -#ifdef __cplusplus -} // extern "C" -#endif -#endif // INCLUDE_DENO_H_ diff --git a/src/internal.h b/src/internal.h new file mode 100644 index 000000000..0ec0ea416 --- /dev/null +++ b/src/internal.h @@ -0,0 +1,44 @@ +// Copyright 2018 Ryan Dahl +// All rights reserved. MIT License. +#ifndef INTERNAL_H_ +#define INTERNAL_H_ + +#include +#include "deno.h" +#include "third_party/v8/include/v8.h" + +extern "C" { +// deno_s = Wrapped Isolate. +struct deno_s { + v8::Isolate* isolate; + const v8::FunctionCallbackInfo* currentArgs; + std::string last_exception; + v8::Persistent recv; + v8::Persistent context; + deno_recv_cb cb; + void* data; +}; +} + +namespace deno { + +struct InternalFieldData { + uint32_t data; +}; + +void Print(const v8::FunctionCallbackInfo& args); +void Recv(const v8::FunctionCallbackInfo& args); +void Send(const v8::FunctionCallbackInfo& args); +static intptr_t external_references[] = {reinterpret_cast(Print), + reinterpret_cast(Recv), + reinterpret_cast(Send), 0}; + +Deno* NewFromSnapshot(void* data, deno_recv_cb cb); + +void InitializeContext(v8::Isolate* isolate, v8::Local context, + const char* js_filename, const char* js_source); + +void AddIsolate(Deno* d, v8::Isolate* isolate); + +} // namespace deno +#endif // INTERNAL_H_ diff --git a/src/main.cc b/src/main.cc index 40d3d89d2..1f3b4cad8 100644 --- a/src/main.cc +++ b/src/main.cc @@ -11,7 +11,7 @@ #endif #include "flatbuffers/flatbuffers.h" -#include "include/deno.h" +#include "deno.h" #include "src/msg_generated.h" #include "third_party/v8/src/base/logging.h" diff --git a/src/mock_runtime_test.cc b/src/mock_runtime_test.cc index 953210ece..d3d9df2a6 100644 --- a/src/mock_runtime_test.cc +++ b/src/mock_runtime_test.cc @@ -2,7 +2,7 @@ // All rights reserved. MIT License. #include "testing/gtest/include/gtest/gtest.h" -#include "include/deno.h" +#include "deno.h" TEST(MockRuntimeTest, InitializesCorrectly) { Deno* d = deno_new(nullptr, nullptr); diff --git a/src/snapshot_creator.cc b/src/snapshot_creator.cc index a49f656aa..3f4d8c484 100644 --- a/src/snapshot_creator.cc +++ b/src/snapshot_creator.cc @@ -1,9 +1,9 @@ // Copyright 2018 Ryan Dahl // All rights reserved. MIT License. // Hint: --trace_serializer is a useful debugging flag. -#include "deno_internal.h" +#include "internal.h" #include "file_util.h" -#include "include/deno.h" +#include "deno.h" #include "third_party/v8/include/v8.h" #include "third_party/v8/src/base/logging.h" -- cgit v1.2.3