Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: support returning Safe collections from C++ #36989

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.As<Object>() \
->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
12 changes: 12 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 Expand Up @@ -1005,6 +1011,12 @@ void GetOptions(const FunctionCallbackInfo<Value>& args) {
Local<Value> aliases;
if (!ToV8Value(context, _ppop_instance.aliases_).ToLocal(&aliases)) return;

if (aliases.As<Object>()
->SetPrototype(context, env->primordials_safe_map_prototype_object())
.IsNothing()) {
return;
}

Local<Object> ret = Object::New(isolate);
if (ret->Set(context, env->options_string(), options).IsNothing() ||
ret->Set(context, env->aliases_string(), aliases).IsNothing()) {
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
18 changes: 18 additions & 0 deletions test/parallel/test-options-binding.js
@@ -0,0 +1,18 @@
// Flags: --expose-internals
'use strict';

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

const { options, aliases, getOptionValue } = require('internal/options');
const assert = require('assert');

assert(options instanceof SafeMap,
"require('internal/options').options is a SafeMap");

assert(aliases instanceof SafeMap,
"require('internal/options').aliases is a SafeMap");

Map.prototype.get =
common.mustNotCall('`getOptionValue` must not call user-mutable method');
assert.strictEqual(getOptionValue('--expose-internals'), true);