From b2ab1bab3f3cafaaba4274aa22a80c45a308af78 Mon Sep 17 00:00:00 2001 From: tatsuya fukui Date: Mon, 2 Nov 2026 16:02:25 +0900 Subject: [PATCH] =?UTF-8?q?deps:=20v8shim:=20N-API=20=E3=81=8C=E4=BD=BF?= =?UTF-8?q?=E3=81=86=20V8=20=E3=81=AE=20API=20=E3=82=92=E8=B6=B3=E3=81=97?= =?UTF-8?q?=E3=80=81node=20=E3=81=8B=E3=82=89=20N-API=20=E3=82=92=20export?= =?UTF-8?q?=20=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit N-API の実装(src/js_native_api_v8.cc)は、node が N-API を export しないあいだはリンクで捨てられていた。 export すると残り、v8shim に無い次の API を参照するので足す。 - Object::Has(context, index)・Value::IsName・Value::ToNumber・Value::InstanceOf - BigInt::NewFromWords・WordCount・ToWordsArray(ホストの bigint_* で 64 ビットの語をやり取りする) - Date::ValueOf(上書きされた valueOf ではなく内部の時刻)・Date::CheckCast・TypedArray::CheckCast - SharedArrayBuffer::NewBackingStore(外のメモリを包む形。ArrayBuffer のものと同じ) - Isolate::AdjustAmountOfExternalAllocatedMemory(ホストのエンジンが自分で集めるので、数えて返すだけ) ホストの側(value_instance_of・date_value・bigint_word_count・bigint_to_words・bigint_from_words)は Progate/packages の browser-nodev2 の src/v8shim が持つ。 Co-Authored-By: Claude Opus 5.5 --- deps/v8shim/src/arraybuffer.cc | 8 +++++++ deps/v8shim/src/host.h | 10 +++++++++ deps/v8shim/src/isolate.cc | 6 +++++ deps/v8shim/src/objects.cc | 4 ++++ deps/v8shim/src/state.h | 4 ++++ deps/v8shim/src/values.cc | 41 ++++++++++++++++++++++++++++++++++ 6 files changed, 73 insertions(+) diff --git a/deps/v8shim/src/arraybuffer.cc b/deps/v8shim/src/arraybuffer.cc index cefd23a7db9f..6b55caf82d17 100644 --- a/deps/v8shim/src/arraybuffer.cc +++ b/deps/v8shim/src/arraybuffer.cc @@ -197,6 +197,7 @@ ArrayBuffer::Allocator* ArrayBuffer::Allocator::NewDefaultAllocator() { void ArrayBuffer::CheckCast(Value* that) {} void ArrayBufferView::CheckCast(Value* that) {} void SharedArrayBuffer::CheckCast(Value* that) {} +void TypedArray::CheckCast(Value* that) {} void PrimitiveArray::CheckCast(Data* that) {} std::unique_ptr ArrayBuffer::NewBackingStore( @@ -298,6 +299,13 @@ Local SharedArrayBuffer::New( ArrayBufferFor(std::move(backing_store), true, false)); } +std::unique_ptr SharedArrayBuffer::NewBackingStore( + void* data, size_t byte_length, BackingStore::DeleterCallback deleter, + void* deleter_data) { + return std::unique_ptr( + NewStore(data, byte_length, true, deleter, deleter_data)); +} + size_t SharedArrayBuffer::ByteLength() const { return host::array_buffer_length(Self(this)); } diff --git a/deps/v8shim/src/host.h b/deps/v8shim/src/host.h index 4db1f4f098b6..c102c2861840 100644 --- a/deps/v8shim/src/host.h +++ b/deps/v8shim/src/host.h @@ -157,6 +157,16 @@ V8SHIM_IMPORT(bigint_value) int64_t bigint_value(Address value, int32_t is_unsig V8SHIM_IMPORT(identity_hash) int32_t identity_hash(Address value); // new Date(time). V8SHIM_IMPORT(date_new) Address date_new(double time); +// The time value of a Date ([[DateValue]], not its valueOf). +V8SHIM_IMPORT(date_value) double date_value(Address date); +// value instanceof constructor: 1 or 0; -1 if it threw. +V8SHIM_IMPORT(value_instance_of) int32_t value_instance_of(Address value, Address constructor); +// The magnitude of a BigInt as 64-bit words, least significant first: how many there are. +V8SHIM_IMPORT(bigint_word_count) int32_t bigint_word_count(Address value); +// Writes the sign (1 if negative) and up to `capacity` words; returns how many the value has. +V8SHIM_IMPORT(bigint_to_words) int32_t bigint_to_words(Address value, int32_t* sign_bit, int32_t capacity, uint64_t* words); +// A BigInt from a sign and words; 0 if it threw (too large). +V8SHIM_IMPORT(bigint_from_words) Address bigint_from_words(int32_t sign_bit, int32_t count, const uint64_t* words); // The host engine's heap, in bytes: 0 used, 1 total, 2 limit. V8SHIM_IMPORT(heap_size) double heap_size(int32_t which); diff --git a/deps/v8shim/src/isolate.cc b/deps/v8shim/src/isolate.cc index 9b55d574dd97..51e26a1ab075 100644 --- a/deps/v8shim/src/isolate.cc +++ b/deps/v8shim/src/isolate.cc @@ -127,6 +127,12 @@ Local Isolate::ThrowException(Local exception) { return Utils::ToLocal(shim::Undefined()); } +int64_t Isolate::AdjustAmountOfExternalAllocatedMemory(int64_t change_in_bytes) { + int64_t& amount = shim::StateOf(this).external_memory; + amount += change_in_bytes; + return amount; +} + void Isolate::TerminateExecution() { shim::StateOf(this).terminating = true; } bool Isolate::IsExecutionTerminating() { diff --git a/deps/v8shim/src/objects.cc b/deps/v8shim/src/objects.cc index 4bde6694e98a..9d9249bab429 100644 --- a/deps/v8shim/src/objects.cc +++ b/deps/v8shim/src/objects.cc @@ -88,6 +88,10 @@ Maybe Object::Has(Local context, Local key) { return ToMaybeBool(host::object_has(Self(this), Utils::Open(key))); } +Maybe Object::Has(Local context, uint32_t index) { + return ToMaybeBool(host::object_has(Self(this), host::number_new(index))); +} + Maybe Object::Delete(Local context, Local key) { return ToMaybeBool(host::object_delete(Self(this), Utils::Open(key))); } diff --git a/deps/v8shim/src/state.h b/deps/v8shim/src/state.h index 843d5fea7495..3cea221d948a 100644 --- a/deps/v8shim/src/state.h +++ b/deps/v8shim/src/state.h @@ -57,6 +57,10 @@ struct State { // The innermost open TryCatch; each links to the one around it. TryCatch* try_catch_top = nullptr; + + // What the embedder said it holds outside the heap + // (AdjustAmountOfExternalAllocatedMemory). The host engine collects on its own, so this is only reported back. + int64_t external_memory = 0; }; // The state of the current thread's isolate. diff --git a/deps/v8shim/src/values.cc b/deps/v8shim/src/values.cc index 9a511453990b..885469c4210c 100644 --- a/deps/v8shim/src/values.cc +++ b/deps/v8shim/src/values.cc @@ -36,6 +36,9 @@ bool Value::FullIsNull() const { return Self(this) == shim::Null(); } bool Value::FullIsTrue() const { return Self(this) == shim::True(); } bool Value::FullIsFalse() const { return Self(this) == shim::False(); } bool Value::FullIsString() const { return Is(this, host::kIsString); } +bool Value::IsName() const { + return Is(this, host::kIsString) || Is(this, host::kIsSymbol); +} #define V8SHIM_VALUE_IS(Name, kind) \ bool Value::Is##Name() const { return Is(this, host::kind); } @@ -143,6 +146,21 @@ Maybe Value::NumberValue(Local context) const { return Just(value); } +MaybeLocal Value::ToNumber(Local context) const { + if (Is(this, host::kIsNumber)) { + return Utils::ToLocal(Self(this)); + } + Maybe number = NumberValue(context); + if (number.IsNothing()) return MaybeLocal(); + return Utils::ToLocal(host::number_new(number.FromJust())); +} + +Maybe Value::InstanceOf(Local context, Local object) { + int32_t result = host::value_instance_of(Self(this), Utils::Open(object)); + if (result < 0) return Nothing(); + return Just(result != 0); +} + namespace { // ECMAScript ToInt32 / ToUint32 from a Number. @@ -259,6 +277,25 @@ uint64_t BigInt::Uint64Value(bool* lossless) const { return value; } +MaybeLocal BigInt::NewFromWords(Local context, int sign_bit, + int word_count, const uint64_t* words) { + if (word_count < 0) return MaybeLocal(); + Address value = host::bigint_from_words(sign_bit != 0, word_count, words); + if (value == 0) return MaybeLocal(); + return Utils::ToLocal(value); +} + +int BigInt::WordCount() const { return host::bigint_word_count(Self(this)); } + +void BigInt::ToWordsArray(int* sign_bit, int* word_count, + uint64_t* words) const { + // V8 writes at most *word_count words and then reports how many the value has. + int32_t sign = 0; + int32_t capacity = *word_count < 0 ? 0 : *word_count; + *word_count = host::bigint_to_words(Self(this), &sign, capacity, words); + *sign_bit = sign; +} + int64_t BigInt::Int64Value(bool* lossless) const { int32_t exact = 0; int64_t value = host::bigint_value(Self(this), 0, &exact); @@ -480,4 +517,8 @@ MaybeLocal Date::New(Local context, double time) { return Utils::ToMaybeLocal(host::date_new(time)); } +double Date::ValueOf() const { return host::date_value(Self(this)); } + +void Date::CheckCast(Value* that) {} + } // namespace v8