summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2019-03-07 15:45:31 +0100
committerAnna Henningsen <anna@addaleax.net>2019-03-15 16:54:19 +0100
commitb0de48e85441ff710aab240fdfa8a34adbbee976 (patch)
tree9b0d0ee1ca888a2d0db99d6350044d6095a84570 /src/api
parent7e2088f773d97e00e29cacdc20e1a36b80528be0 (diff)
downloadandroid-node-v8-b0de48e85441ff710aab240fdfa8a34adbbee976.tar.gz
android-node-v8-b0de48e85441ff710aab240fdfa8a34adbbee976.tar.bz2
android-node-v8-b0de48e85441ff710aab240fdfa8a34adbbee976.zip
src,lib: make DOMException available in all Contexts
This allows using `DOMException` from Node.js code for any `vm.Context`. PR-URL: https://github.com/nodejs/node/pull/26497 Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/api')
-rw-r--r--src/api/environment.cc36
1 files changed, 34 insertions, 2 deletions
diff --git a/src/api/environment.cc b/src/api/environment.cc
index c4a8120d1c..6e3c61fac1 100644
--- a/src/api/environment.cc
+++ b/src/api/environment.cc
@@ -15,6 +15,7 @@
namespace node {
using v8::Context;
+using v8::EscapableHandleScope;
using v8::Function;
using v8::HandleScope;
using v8::Isolate;
@@ -22,7 +23,9 @@ using v8::Local;
using v8::MaybeLocal;
using v8::Message;
using v8::MicrotasksPolicy;
+using v8::Object;
using v8::ObjectTemplate;
+using v8::Private;
using v8::String;
using v8::Value;
@@ -279,6 +282,26 @@ void FreePlatform(MultiIsolatePlatform* platform) {
delete platform;
}
+MaybeLocal<Object> GetPerContextExports(Local<Context> context) {
+ Isolate* isolate = context->GetIsolate();
+ EscapableHandleScope handle_scope(isolate);
+
+ Local<Object> global = context->Global();
+ Local<Private> key = Private::ForApi(isolate,
+ FIXED_ONE_BYTE_STRING(isolate, "node:per_context_binding_exports"));
+
+ Local<Value> existing_value;
+ if (!global->GetPrivate(context, key).ToLocal(&existing_value))
+ return MaybeLocal<Object>();
+ if (existing_value->IsObject())
+ return handle_scope.Escape(existing_value.As<Object>());
+
+ Local<Object> exports = Object::New(isolate);
+ if (context->Global()->SetPrivate(context, key, exports).IsNothing())
+ return MaybeLocal<Object>();
+ return handle_scope.Escape(exports);
+}
+
Local<Context> NewContext(Isolate* isolate,
Local<ObjectTemplate> object_template) {
auto context = Context::New(isolate, nullptr, object_template);
@@ -291,16 +314,25 @@ Local<Context> NewContext(Isolate* isolate,
{
// Run per-context JS files.
Context::Scope context_scope(context);
+ Local<Object> exports;
+ if (!GetPerContextExports(context).ToLocal(&exports))
+ return Local<Context>();
+
+ Local<String> global_string = FIXED_ONE_BYTE_STRING(isolate, "global");
+ Local<String> exports_string = FIXED_ONE_BYTE_STRING(isolate, "exports");
static const char* context_files[] = {
"internal/per_context/setup",
+ "internal/per_context/domexception",
nullptr
};
for (const char** module = context_files; *module != nullptr; module++) {
std::vector<Local<String>> parameters = {
- FIXED_ONE_BYTE_STRING(isolate, "global")};
- Local<Value> arguments[] = {context->Global()};
+ global_string,
+ exports_string
+ };
+ Local<Value> arguments[] = {context->Global(), exports};
MaybeLocal<Function> maybe_fn =
per_process::native_module_loader.LookupAndCompile(
context, *module, &parameters, nullptr);