0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-01 09:24:20 -04:00
denoland-deno/libdeno/internal.h

183 lines
5.4 KiB
C
Raw Normal View History

2019-01-21 14:03:30 -05:00
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
#ifndef INTERNAL_H_
#define INTERNAL_H_
#include <map>
#include <string>
2019-01-29 21:31:59 -05:00
#include <utility>
#include <vector>
#include "deno.h"
2018-07-03 04:15:32 -04:00
#include "third_party/v8/include/v8.h"
#include "third_party/v8/src/base/logging.h"
namespace deno {
struct ModuleInfo {
std::string name;
v8::Persistent<v8::Module> handle;
std::vector<std::string> import_specifiers;
ModuleInfo(v8::Isolate* isolate, v8::Local<v8::Module> module,
const char* name_, std::vector<std::string> import_specifiers_)
: name(name_), import_specifiers(import_specifiers_) {
handle.Reset(isolate, module);
}
};
// deno_s = Wrapped Isolate.
class DenoIsolate {
public:
2019-01-29 21:31:59 -05:00
explicit DenoIsolate(deno_config config)
: isolate_(nullptr),
shared_(config.shared),
current_args_(nullptr),
2018-10-24 01:12:13 -04:00
snapshot_creator_(nullptr),
global_import_buf_ptr_(nullptr),
recv_cb_(config.recv_cb),
next_req_id_(0),
user_data_(nullptr),
resolve_cb_(nullptr) {
array_buffer_allocator_ = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
if (config.load_snapshot.data_ptr) {
snapshot_.data =
reinterpret_cast<const char*>(config.load_snapshot.data_ptr);
snapshot_.raw_size = static_cast<int>(config.load_snapshot.data_len);
}
}
~DenoIsolate() {
if (snapshot_creator_) {
delete snapshot_creator_;
} else {
isolate_->Dispose();
}
delete array_buffer_allocator_;
}
2019-01-29 11:32:40 -05:00
static inline DenoIsolate* FromIsolate(v8::Isolate* isolate) {
return static_cast<DenoIsolate*>(isolate->GetData(0));
}
void AddIsolate(v8::Isolate* isolate);
deno_mod RegisterModule(const char* name, const char* source);
v8::Local<v8::Object> GetBuiltinModules();
void ClearModules();
ModuleInfo* GetModuleInfo(deno_mod id) {
if (id == 0) {
return nullptr;
}
auto it = mods_.find(id);
if (it != mods_.end()) {
return &it->second;
} else {
return nullptr;
}
}
v8::Isolate* isolate_;
v8::ArrayBuffer::Allocator* array_buffer_allocator_;
deno_buf shared_;
const v8::FunctionCallbackInfo<v8::Value>* current_args_;
2018-10-24 01:12:13 -04:00
v8::SnapshotCreator* snapshot_creator_;
void* global_import_buf_ptr_;
deno_recv_cb recv_cb_;
int32_t next_req_id_;
void* user_data_;
v8::Persistent<v8::Object> builtin_modules_;
std::map<deno_mod, ModuleInfo> mods_;
std::map<std::string, deno_mod> mods_by_name_;
deno_resolve_cb resolve_cb_;
v8::Persistent<v8::Context> context_;
std::map<int32_t, v8::Persistent<v8::Value>> async_data_map_;
std::map<int, v8::Persistent<v8::Value>> pending_promise_map_;
std::string last_exception_;
v8::Persistent<v8::Function> recv_;
v8::StartupData snapshot_;
v8::Persistent<v8::ArrayBuffer> global_import_buf_;
v8::Persistent<v8::ArrayBuffer> shared_ab_;
};
2018-06-10 08:18:15 -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;
};
2019-02-01 23:26:33 -05:00
static inline v8::Local<v8::String> v8_str(const char* x) {
2019-01-01 11:22:23 -05:00
return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x,
2019-02-01 23:26:33 -05:00
v8::NewStringType::kNormal)
2019-01-01 11:22:23 -05:00
.ToLocalChecked();
}
2018-06-10 08:18:15 -04:00
void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
void Recv(const v8::FunctionCallbackInfo<v8::Value>& args);
void Send(const v8::FunctionCallbackInfo<v8::Value>& args);
void Shared(v8::Local<v8::Name> property,
const v8::PropertyCallbackInfo<v8::Value>& info);
void BuiltinModules(v8::Local<v8::Name> property,
const v8::PropertyCallbackInfo<v8::Value>& info);
2019-01-29 11:32:40 -05:00
void MessageCallback(v8::Local<v8::Message> message, v8::Local<v8::Value> data);
2018-08-26 13:22:07 -04:00
static intptr_t external_references[] = {
reinterpret_cast<intptr_t>(Print),
reinterpret_cast<intptr_t>(Recv),
reinterpret_cast<intptr_t>(Send),
reinterpret_cast<intptr_t>(Shared),
reinterpret_cast<intptr_t>(BuiltinModules),
2019-01-29 11:32:40 -05:00
reinterpret_cast<intptr_t>(MessageCallback),
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};
Deno* NewFromSnapshot(void* user_data, deno_recv_cb cb);
2018-06-10 08:18:15 -04:00
void InitializeContext(v8::Isolate* isolate, v8::Local<v8::Context> context);
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);
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);
bool ExecuteMod(v8::Local<v8::Context> context, const char* js_filename,
const char* js_source, bool resolve_only);
} // namespace deno
extern "C" {
// This is just to workaround the linker.
struct deno_s {
deno::DenoIsolate isolate;
};
}
#endif // INTERNAL_H_