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
7 changes: 7 additions & 0 deletions NativeScript/runtime/ClassBuilder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,15 @@ void ScopeClassNameToIsolate(std::string& name, int isolateId) {

Local<v8::Array> propertyNames;

// An inherited iterator only when no superclass enumerates natively: NSArray's
// prototype iterator, or a JS superclass's, already backs that enumeration.
Local<Value> symbolIterator;
bool ownsIterator =
implementationObject->HasOwnProperty(context, Symbol::GetIterator(isolate)).FromMaybe(false);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
bool inheritsEnumeration = class_respondsToSelector(
class_getSuperclass(extendedClass), @selector(countByEnumeratingWithState:objects:count:));
bool success =
(!ownsIterator && inheritsEnumeration) ||
implementationObject->Get(context, Symbol::GetIterator(isolate)).ToLocal(&symbolIterator);
tns::Assert(success, isolate);

Expand Down
6 changes: 0 additions & 6 deletions NativeScript/runtime/Interop.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "Reference.h"
#include "Runtime.h"
#include "RuntimeConfig.h"
#include "SymbolIterator.h"
#include "SymbolLoader.h"
#include "UnmanagedType.h"
#include "WrapperLifetime.h"
Expand Down Expand Up @@ -1497,11 +1496,6 @@ static bool SnapshotArrayLike(Isolate* isolate, Local<Object> object, Local<v8::
[result release];
}

if ([result isKindOfClass:[NSArray class]]) {
// attach Symbol.iterator to the instance
SymbolIterator::Set(context, jsResult);
}

tns::DeleteWrapperIfUnused(isolate, jsResult, wrapper);

return jsResult;
Expand Down
4 changes: 4 additions & 0 deletions NativeScript/runtime/MetadataBuilder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "ObjectManager.h"
#include "Runtime.h"
#include "RuntimeConfig.h"
#include "SymbolIterator.h"
#include "SymbolLoader.h"
#include "Tasks.h"
#include "Worker.h"
Expand Down Expand Up @@ -606,6 +607,9 @@ throw NativeScriptException(
ctorFuncTemplate->PrototypeTemplate()->Set(
tns::ToV8String(isolate, "toString"),
FunctionTemplate::New(isolate, MetadataBuilder::ToStringFunctionCallback));
if (meta->type() == MetaType::Interface && strcmp(meta->name(), "NSArray") == 0) {
SymbolIterator::Install(isolate, ctorFuncTemplate->PrototypeTemplate());
}

if (meta->type() == MetaType::Interface) {
const InterfaceMeta* interfaceMeta = static_cast<const InterfaceMeta*>(meta);
Expand Down
5 changes: 4 additions & 1 deletion NativeScript/runtime/SymbolIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ namespace tns {

class SymbolIterator {
public:
static void Set(v8::Local<v8::Context> context, v8::Local<v8::Value> object);
// On NSArray's prototype template, so every array wrapper inherits it.
static void Install(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> prototypeTemplate);

private:
static v8::Local<v8::Object> CreateIteratorObject(v8::Local<v8::Context> context, v8::Local<v8::Value> object);
static void NextCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down
45 changes: 16 additions & 29 deletions NativeScript/runtime/SymbolIterator.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,27 @@
#include <Foundation/Foundation.h>
#include "ArgConverter.h"
#include "Helpers.h"
#include "WrapperLifetime.h"

using namespace v8;

namespace tns {

void SymbolIterator::Set(Local<Context> context, Local<Value> object) {
Isolate* isolate = v8::Isolate::GetCurrent();

// In the canary wrapper lifetime mode it iterates its receiver: this
// property lives on the object's state there, which a reference back to the
// object would keep alive until native death. Elsewhere it keeps `object`, so
// an unbound call still iterates it.
Local<Value> captured =
GetWrapperLifetimeMode() == WrapperLifetimeMode::kCanary ? Local<Value>() : object;
Local<v8::Function> iterator;
bool success = v8::Function::New(
context,
[](const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
Local<Value> target = args.Data();
if (target.IsEmpty() || target->IsUndefined()) {
target = args.This();
}
Local<Object> result = CreateIteratorObject(context, target);
args.GetReturnValue().Set(result);
},
captured)
.ToLocal(&iterator);
tns::Assert(success, isolate);

Local<Value> symbolIteratorKey = Symbol::GetIterator(isolate);
success = object.As<Object>()->Set(context, symbolIteratorKey, iterator).FromMaybe(false);
void SymbolIterator::Install(Isolate* isolate, Local<ObjectTemplate> prototypeTemplate) {
prototypeTemplate->Set(
Symbol::GetIterator(isolate),
FunctionTemplate::New(isolate, [](const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
// Like Array.prototype[Symbol.iterator], it needs its array as `this`.
BaseDataWrapper* wrapper = tns::GetValue(isolate, args.This());
if (wrapper == nullptr || wrapper->Type() != WrapperType::ObjCObject ||
![static_cast<ObjCDataWrapper*>(wrapper)->Data() isKindOfClass:[NSArray class]]) {
isolate->ThrowException(Exception::TypeError(tns::ToV8String(
isolate, "NSArray.prototype[Symbol.iterator] called on a non-NSArray")));
return;
}
Local<Context> context = isolate->GetCurrentContext();
args.GetReturnValue().Set(CreateIteratorObject(context, args.This()));
}));
}

Local<Object> SymbolIterator::CreateIteratorObject(Local<Context> context, Local<Value> object) {
Expand Down
15 changes: 15 additions & 0 deletions NativeScript/runtime/WeakRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "BuiltinLoader.h"
#include "Helpers.h"
#include "WrapperLifetime.h"
#include "WrapperLifetimeCanary.h"

using namespace v8;

Expand All @@ -18,6 +20,19 @@ void WeakRef::Init(Local<Context> context) {
tns::LogError(isolate, tc);
}
tns::Assert(success, isolate);

if (GetWrapperLifetimeMode() != WrapperLifetimeMode::kCanary) {
return;
}
Local<Object> binding = Object::New(isolate);
success =
WrapperLifetimeCanary::RegisterWrapFunctions(context, binding) &&
BuiltinLoader::RunBuiltin(context, BuiltinId::kWeakCollections, binding)
.ToLocal(&result);
if (!success && tc.HasCaught()) {
tns::LogError(isolate, tc);
}
tns::Assert(success, isolate);
}

} // namespace tns
6 changes: 6 additions & 0 deletions NativeScript/runtime/WrapperLifetimeCanary.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class WrapperLifetimeCanary {
// returned instead), which ArgConverter::ConstructObject wraps anew.
static bool NativeInstancesIntercept();

// Adds `unwrap` (a canary's native object -> its weak-collection key) and
// `wrap` (key -> live or new canary, or undefined once the object is gone)
// to `binding`, for the weak-collections builtin. Canary mode only.
static bool RegisterWrapFunctions(v8::Local<v8::Context> context,
v8::Local<v8::Object> binding);

// The record of an object that already has state in this isolate, for a new
// canary to share. Null otherwise.
static std::shared_ptr<CanaryRecord> Find(v8::Isolate* isolate, id object);
Expand Down
Loading
Loading