Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions NativeScript/runtime/MetadataBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace tns {

struct NativeSetter;
struct NativeSetters;

class MetadataBuilder {
Expand All @@ -32,18 +33,21 @@ class MetadataBuilder {
void* data,
StructStorage storage,
v8::Local<v8::Object> parent);
// Runs the setter of `owner`'s native property `property` on `receiver`, as
// the property's accessor would, and returns true. Returns false, having
// run nothing, unless `owner` is a native class's prototype, still holding
// the accessor pair it was built with for `property`, which has a setter.
// The caller establishes that `owner` is where `receiver`'s prototype chain
// finds `property`. Only canary mode records the accessors this needs.
static bool TrySetNativeProperty(v8::Isolate* isolate,
v8::Local<v8::Context> context,
v8::Local<v8::Object> owner,
v8::Local<v8::Name> property,
v8::Local<v8::Object> receiver,
v8::Local<v8::Value> value);
// Canary mode: the setter of `owner`'s native property `property`, when
// `owner` is a native class's prototype still holding the accessor pair it
// was built with for `property`, which has a setter; null otherwise. The
// caller establishes that `owner` is where a receiver's prototype chain
// finds `property`. The result stays valid for the isolate's lifetime.
static NativeSetter* FindNativeSetter(v8::Isolate* isolate,
v8::Local<v8::Context> context,
v8::Local<v8::Object> owner,
v8::Local<v8::Name> property);
// Runs `setter` on `receiver`, as the property's accessor would, and
// returns true; false, having run nothing, once JS has assigned or defined
// the property on its prototype since FindNativeSetter.
static bool RunNativeSetter(v8::Isolate* isolate, NativeSetter* setter,
v8::Local<v8::Object> receiver,
v8::Local<v8::Value> value);
// Where the native member guard is on (GuardsNativeMembers): turns each
// of `names`, and Symbol.iterator with `iterator`, an own method of
// `prototype`, a prototype level of a JS-extended class, that native code
Expand Down Expand Up @@ -217,6 +221,7 @@ class MetadataBuilder {
Class klass_ = nil;
};

friend struct NativeSetter;
friend struct NativeSetters;

static void SetNativeProperty(v8::Isolate* isolate,
Expand Down
47 changes: 28 additions & 19 deletions NativeScript/runtime/MetadataBuilder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -89,30 +89,34 @@ bool ResolveProxyReceiver(Isolate* isolate, Local<Object>& receiver, const char*
constexpr int32_t kStructFieldIndexMask = (1 << kStructViewSlotShift) - 1;
} // namespace

// One native property setter a native class's prototype was built with
// (NativeSetters). Lives as long as the isolate; `item` is emptied for good
// once JS changes the name on that prototype.
struct NativeSetter {
Global<v8::Name> name;
MetadataBuilder::CacheItem<PropertyMeta>* item;
};

// The native property setters a native class's prototype was built with, for
// TrySetNativeProperty. JS can replace one only by defining the name on that
// FindNativeSetter. JS can replace one only by defining the name on that
// prototype, which its definer sees, and a name the prototype's own handlers
// see defined or assigned is forgotten for good. A deleted accessor needs no
// such care: the prototype then no longer has the property.
struct NativeSetters {
struct Entry {
Global<v8::Name> name;
MetadataBuilder::CacheItem<PropertyMeta>* item;
};

// In the order RegisterInstanceProperties installed them: a later accessor
// of a name replaces the earlier one on the prototype.
std::vector<std::pair<std::string, MetadataBuilder::CacheItem<PropertyMeta>*>> installed;
// By the name's identity hash. Names reaching an interceptor are internalized,
// so a lookup compares identities.
robin_hood::unordered_map<int, std::vector<Entry>> byHash;
// so a lookup compares identities. Filled once by Index: entries never move
// afterwards, as FindNativeSetter hands out pointers to them.
robin_hood::unordered_map<int, std::vector<NativeSetter>> byHash;

Entry* Find(Local<v8::Name> name) {
NativeSetter* Find(Local<v8::Name> name) {
auto it = byHash.find(name->GetIdentityHash());
if (it == byHash.end()) {
return nullptr;
}
for (Entry& entry : it->second) {
for (NativeSetter& entry : it->second) {
if (entry.name == name) {
return &entry;
}
Expand All @@ -127,7 +131,7 @@ void Index(Isolate* isolate) {
v8::String::NewFromUtf8(isolate, jsName.c_str(), NewStringType::kInternalized)
.ToLocalChecked();
MetadataBuilder::CacheItem<PropertyMeta>* setter = item->meta_->hasSetter() ? item : nullptr;
if (Entry* entry = Find(name)) {
if (NativeSetter* entry = Find(name)) {
entry->item = setter;
} else if (setter != nullptr) {
byHash[name->GetIdentityHash()].push_back({Global<v8::Name>(isolate, name), setter});
Expand All @@ -142,7 +146,7 @@ void Forget(Isolate* isolate, Local<v8::Name> name) {
if (it == byHash.end()) {
return;
}
for (Entry& entry : it->second) {
for (NativeSetter& entry : it->second) {
if (entry.name.Get(isolate)->StrictEquals(name)) {
entry.item = nullptr;
}
Expand Down Expand Up @@ -1596,18 +1600,23 @@ void CollectInstanceMemberNames(const BaseClassMeta* meta, Class klass, bool own
return method;
}

bool MetadataBuilder::TrySetNativeProperty(Isolate* isolate, Local<Context> context,
Local<Object> owner, Local<v8::Name> property,
Local<Object> receiver, Local<Value> value) {
NativeSetter* MetadataBuilder::FindNativeSetter(Isolate* isolate, Local<Context> context,
Local<Object> owner, Local<v8::Name> property) {
NativeSetters* setters = NativeSettersOf(isolate, context, owner);
if (setters == nullptr) {
return false;
return nullptr;
}
NativeSetters::Entry* entry = setters->Find(property);
if (entry == nullptr || entry->item == nullptr) {
NativeSetter* entry = setters->Find(property);
return entry != nullptr && entry->item != nullptr ? entry : nullptr;
}

bool MetadataBuilder::RunNativeSetter(Isolate* isolate, NativeSetter* setter,
Local<Object> receiver, Local<Value> value) {
CacheItem<PropertyMeta>* item = setter->item;
if (item == nullptr) {
return false;
}
SetNativeProperty(isolate, entry->item, receiver, value);
SetNativeProperty(isolate, item, receiver, value);
return true;
}

Expand Down
1 change: 1 addition & 0 deletions NativeScript/runtime/NsBuiltinModules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ void GetWrapperLifetimeStatsCallback(const FunctionCallbackInfo<Value>& info) {
{"canaryStates", stats->canaryStates},
{"canaryNativeDeaths", stats->canaryNativeDeaths},
{"canaryInterceptedWrites", stats->canaryInterceptedWrites},
{"canaryCachedNativeWrites", stats->canaryCachedNativeWrites},
};
for (const auto& [name, value] : fields) {
result
Expand Down
3 changes: 3 additions & 0 deletions NativeScript/runtime/ObjectManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ class ObjectManager {
uint64_t canaryNativeDeaths = 0;
// Canary mode: writes the canary handlers saw.
uint64_t canaryInterceptedWrites = 0;
// Canary mode: of those, native property writes whose setter the write
// handler found in its cache rather than along the prototype chain.
uint64_t canaryCachedNativeWrites = 0;
};
static LifetimeStats* GetLifetimeStats(v8::Isolate* isolate);

Expand Down
Loading
Loading