mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
lib: support returning Safe collections from C++
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: https://github.com/nodejs/node/pull/36989 Refs: https://github.com/nodejs/node/pull/36652 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
726ef40fcb
commit
346fc0ac21
5 files changed
+59
No files matched your search
+23
@@ -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);
|
||||
|
||||
@@ -478,6 +478,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) \
|
||||
|
||||
@@ -882,6 +882,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;
|
||||
@@ -970,6 +976,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()) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
Reference in new issue
Block a user