Skip to content

Commit

Permalink
lib: support returning Safe collections from C++
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Jan 19, 2021
1 parent fef2128 commit e5c872f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/env.cc
Expand Up @@ -273,6 +273,29 @@ void Environment::CreateProperties() {
CHECK(primordials->IsObject());
set_primordials(primordials.As<Object>());

Local<String> prototype_string =
FIXED_ONE_BYTE_STRING(isolate(), "prototype");

#define V(EnvPropertyName, PrimordialsPropertyName) \
{ \
Local<Value> ctor = \
primordials \
->Get(ctx, \
FIXED_ONE_BYTE_STRING(isolate(), PrimordialsPropertyName)) \
.ToLocalChecked(); \
CHECK(ctor->IsObject()); \
Local<Value> prototype = \
ctor.As<Object>()->Get(ctx, prototype_string).ToLocalChecked(); \
CHECK(prototype->IsObject()); \
set_##EnvPropertyName(prototype.As<Object>()); \
}

V(primordials_safe_map_prototype_object, "SafeMap");
V(primordials_safe_set_prototype_object, "SafeSet");
V(primordials_safe_weak_map_prototype_object, "SafeWeakMap");
V(primordials_safe_weak_set_prototype_object, "SafeWeakSet");
#undef V

Local<Object> process_object =
node::CreateProcessObject(this).FromMaybe(Local<Object>());
set_process_object(process_object);
Expand Down
4 changes: 4 additions & 0 deletions src/env.h
Expand Up @@ -550,6 +550,10 @@ constexpr size_t kFsStatsBufferLength =
V(prepare_stack_trace_callback, v8::Function) \
V(process_object, v8::Object) \
V(primordials, v8::Object) \
V(primordials_safe_map_prototype_object, v8::Object) \
V(primordials_safe_set_prototype_object, v8::Object) \
V(primordials_safe_weak_map_prototype_object, v8::Object) \
V(primordials_safe_weak_set_prototype_object, v8::Object) \
V(promise_hook_handler, v8::Function) \
V(promise_reject_callback, v8::Function) \
V(script_data_constructor_function, v8::Function) \
Expand Down
6 changes: 6 additions & 0 deletions src/node_options.cc
Expand Up @@ -917,6 +917,12 @@ void GetOptions(const FunctionCallbackInfo<Value>& args) {
});

Local<Map> options = Map::New(isolate);
if (options
->SetPrototype(context, env->primordials_safe_map_prototype_object())
.IsNothing()) {
return;
}

for (const auto& item : _ppop_instance.options_) {
Local<Value> value;
const auto& option_info = item.second;
Expand Down
2 changes: 2 additions & 0 deletions src/uv.cc
Expand Up @@ -81,6 +81,8 @@ void GetErrMap(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = env->isolate();
Local<Context> context = env->context();

// This can't return a SafeMap, because the uv binding can be referenced
// by user code by using `process.binding('uv').getErrorMap()`:
Local<Map> err_map = Map::New(isolate);

size_t errors_len = arraysize(per_process::uv_errors_map);
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-options-binding.js
@@ -0,0 +1,16 @@
// Flags: --expose-internals
'use strict';

require('../common');
const { internalBinding, primordials } = require('internal/test/binding');
const {
SafeMap,
} = primordials;

const options = internalBinding('options');
const assert = require('assert');

assert(
options.getOptions() instanceof SafeMap,
"internalBinding('options').getOptions() returns SafeMap",
);

0 comments on commit e5c872f

Please sign in to comment.