2018-07-23 14:46:30 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-07-06 03:19:19 -04:00
|
|
|
#ifndef INTERNAL_H_
|
|
|
|
#define INTERNAL_H_
|
2018-06-09 23:34:03 -04:00
|
|
|
|
2018-10-18 11:44:45 -04:00
|
|
|
#include <map>
|
2018-06-09 23:34:03 -04:00
|
|
|
#include <string>
|
2018-07-06 03:19:19 -04:00
|
|
|
#include "deno.h"
|
2018-07-03 04:15:32 -04:00
|
|
|
#include "third_party/v8/include/v8.h"
|
2018-10-23 23:58:20 -04:00
|
|
|
#include "third_party/v8/src/base/logging.h"
|
|
|
|
|
|
|
|
namespace deno {
|
2018-06-09 23:34:03 -04:00
|
|
|
|
|
|
|
// deno_s = Wrapped Isolate.
|
2018-10-23 23:58:20 -04:00
|
|
|
class DenoIsolate {
|
|
|
|
public:
|
2018-10-24 02:17:10 -04:00
|
|
|
DenoIsolate(deno_buf snapshot, deno_recv_cb cb, deno_buf shared)
|
2018-10-23 23:58:20 -04:00
|
|
|
: isolate_(nullptr),
|
2018-10-24 02:17:10 -04:00
|
|
|
shared_(shared),
|
2018-10-23 23:58:20 -04:00
|
|
|
current_args_(nullptr),
|
2018-10-24 01:12:13 -04:00
|
|
|
snapshot_creator_(nullptr),
|
2018-10-23 23:58:20 -04:00
|
|
|
global_import_buf_ptr_(nullptr),
|
|
|
|
pending_promise_events_(0),
|
|
|
|
cb_(cb),
|
|
|
|
next_req_id_(0),
|
|
|
|
user_data_(nullptr) {
|
|
|
|
if (snapshot.data_ptr) {
|
|
|
|
snapshot_.data = reinterpret_cast<const char*>(snapshot.data_ptr);
|
|
|
|
snapshot_.raw_size = static_cast<int>(snapshot.data_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddIsolate(v8::Isolate* isolate);
|
|
|
|
|
|
|
|
v8::Isolate* isolate_;
|
2018-10-24 02:17:10 -04:00
|
|
|
deno_buf shared_;
|
2018-10-23 23:58:20 -04:00
|
|
|
const v8::FunctionCallbackInfo<v8::Value>* current_args_;
|
2018-10-24 01:12:13 -04:00
|
|
|
v8::SnapshotCreator* snapshot_creator_;
|
2018-10-23 23:58:20 -04:00
|
|
|
void* global_import_buf_ptr_;
|
|
|
|
int32_t pending_promise_events_;
|
|
|
|
deno_recv_cb cb_;
|
|
|
|
int32_t next_req_id_;
|
|
|
|
void* user_data_;
|
|
|
|
|
|
|
|
v8::Persistent<v8::Context> context_;
|
|
|
|
std::map<int32_t, v8::Persistent<v8::Value>> async_data_map_;
|
|
|
|
std::string last_exception_;
|
|
|
|
v8::Persistent<v8::Function> recv_;
|
|
|
|
v8::Persistent<v8::Function> global_error_handler_;
|
|
|
|
v8::Persistent<v8::Function> promise_reject_handler_;
|
|
|
|
v8::Persistent<v8::Function> promise_error_examiner_;
|
|
|
|
v8::StartupData snapshot_;
|
|
|
|
v8::Persistent<v8::ArrayBuffer> global_import_buf_;
|
2018-10-24 02:17:10 -04:00
|
|
|
v8::Persistent<v8::ArrayBuffer> shared_ab_;
|
2018-06-09 23:34:03 -04:00
|
|
|
};
|
2018-06-10 08:18:15 -04:00
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
class UserDataScope {
|
|
|
|
DenoIsolate* deno;
|
|
|
|
void* prev_data;
|
|
|
|
void* data; // Not necessary; only for sanity checking.
|
|
|
|
|
|
|
|
public:
|
|
|
|
UserDataScope(DenoIsolate* deno_, void* data_) : deno(deno_), data(data_) {
|
|
|
|
CHECK(deno->user_data_ == nullptr || deno->user_data_ == data_);
|
|
|
|
prev_data = deno->user_data_;
|
|
|
|
deno->user_data_ = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
~UserDataScope() {
|
|
|
|
CHECK(deno->user_data_ == data);
|
|
|
|
deno->user_data_ = prev_data;
|
|
|
|
}
|
|
|
|
};
|
2018-06-10 08:18:15 -04:00
|
|
|
|
2018-06-12 00:36:01 -04:00
|
|
|
struct InternalFieldData {
|
|
|
|
uint32_t data;
|
|
|
|
};
|
|
|
|
|
2018-06-10 08:18:15 -04:00
|
|
|
void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
|
2018-07-01 12:07:12 -04:00
|
|
|
void Recv(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
|
|
void Send(const v8::FunctionCallbackInfo<v8::Value>& args);
|
2018-10-24 02:17:10 -04:00
|
|
|
void Shared(v8::Local<v8::Name> property,
|
|
|
|
const v8::PropertyCallbackInfo<v8::Value>& info);
|
2018-08-26 03:57:16 -04:00
|
|
|
void SetGlobalErrorHandler(const v8::FunctionCallbackInfo<v8::Value>& args);
|
2018-10-12 14:22:52 -04:00
|
|
|
void SetPromiseRejectHandler(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
|
|
void SetPromiseErrorExaminer(const v8::FunctionCallbackInfo<v8::Value>& args);
|
2018-08-26 13:22:07 -04:00
|
|
|
static intptr_t external_references[] = {
|
2018-10-12 14:22:52 -04:00
|
|
|
reinterpret_cast<intptr_t>(Print),
|
|
|
|
reinterpret_cast<intptr_t>(Recv),
|
2018-08-26 13:22:07 -04:00
|
|
|
reinterpret_cast<intptr_t>(Send),
|
2018-10-24 02:17:10 -04:00
|
|
|
reinterpret_cast<intptr_t>(Shared),
|
2018-10-12 14:22:52 -04:00
|
|
|
reinterpret_cast<intptr_t>(SetGlobalErrorHandler),
|
|
|
|
reinterpret_cast<intptr_t>(SetPromiseRejectHandler),
|
|
|
|
reinterpret_cast<intptr_t>(SetPromiseErrorExaminer),
|
|
|
|
0};
|
2018-06-10 08:18:15 -04:00
|
|
|
|
2018-10-24 01:12:13 -04:00
|
|
|
static const deno_buf empty_buf = {nullptr, 0, nullptr, 0};
|
|
|
|
|
2018-10-08 11:49:48 -04:00
|
|
|
Deno* NewFromSnapshot(void* user_data, deno_recv_cb cb);
|
2018-06-10 08:18:15 -04:00
|
|
|
|
2018-06-13 14:55:08 -04:00
|
|
|
void InitializeContext(v8::Isolate* isolate, v8::Local<v8::Context> context,
|
2018-10-24 01:12:13 -04:00
|
|
|
const char* js_filename, const char* js_source,
|
|
|
|
const char* source_map);
|
2018-06-13 14:55:08 -04:00
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
void HandleException(v8::Local<v8::Context> context,
|
|
|
|
v8::Local<v8::Value> exception);
|
|
|
|
|
|
|
|
void DeserializeInternalFields(v8::Local<v8::Object> holder, int index,
|
|
|
|
v8::StartupData payload, void* data);
|
|
|
|
|
2018-10-24 01:12:13 -04:00
|
|
|
v8::StartupData SerializeInternalFields(v8::Local<v8::Object> holder, int index,
|
|
|
|
void* data);
|
|
|
|
|
2018-10-23 23:58:20 -04:00
|
|
|
v8::Local<v8::Uint8Array> ImportBuf(DenoIsolate* d, deno_buf buf);
|
|
|
|
|
|
|
|
void DeleteDataRef(DenoIsolate* d, int32_t req_id);
|
|
|
|
|
|
|
|
bool Execute(v8::Local<v8::Context> context, const char* js_filename,
|
|
|
|
const char* js_source);
|
2018-06-09 23:34:03 -04:00
|
|
|
|
|
|
|
} // namespace deno
|
2018-10-23 23:58:20 -04:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
// This is just to workaround the linker.
|
|
|
|
struct deno_s {
|
|
|
|
deno::DenoIsolate isolate;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-06 03:19:19 -04:00
|
|
|
#endif // INTERNAL_H_
|