#include "gin/data_object_builder.h"
#include <string_view>
#include "base/check_op.h"
#include "base/debug/debugging_buildflags.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "gin/dictionary.h"
#include "gin/public/isolate_holder.h"
#include "gin/test/v8_test.h"
#include "v8/include/v8-context.h"
#include "v8/include/v8-function.h"
namespace gin {
namespace {
using DataObjectBuilderTest = V8Test;
TEST_F(DataObjectBuilderTest, CreatesDataProperties) {
v8::Isolate* isolate = instance_->isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = context_.Get(isolate);
v8::Local<v8::Object> object =
DataObjectBuilder(isolate).Set("key", 42).Build();
ASSERT_TRUE(object->HasOwnProperty(context, StringToSymbol(isolate, "key"))
.ToChecked());
v8::Local<v8::Value> descriptor_object;
ASSERT_TRUE(
object->GetOwnPropertyDescriptor(context, StringToSymbol(isolate, "key"))
.ToLocal(&descriptor_object));
gin::Dictionary descriptor(isolate, descriptor_object.As<v8::Object>());
int32_t value = 0;
ASSERT_TRUE(descriptor.Get("value", &value));
EXPECT_EQ(42, value);
bool writable = false;
ASSERT_TRUE(descriptor.Get("writable", &writable));
EXPECT_TRUE(writable);
bool enumerable = false;
ASSERT_TRUE(descriptor.Get("enumerable", &enumerable));
EXPECT_TRUE(enumerable);
bool configurable = false;
ASSERT_TRUE(descriptor.Get("configurable", &configurable));
EXPECT_TRUE(configurable);
}
TEST_F(DataObjectBuilderTest, DoesNotInvokeSetters) {
v8::Isolate* isolate = instance_->isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = context_.Get(isolate);
v8::Local<v8::Value> object_constructor;
ASSERT_TRUE(context->Global()
->Get(context, StringToSymbol(isolate, "Object"))
.ToLocal(&object_constructor));
v8::Local<v8::Value> object_prototype;
ASSERT_TRUE(object_constructor.As<v8::Function>()
->Get(context, StringToSymbol(isolate, "prototype"))
.ToLocal(&object_prototype));
ASSERT_TRUE(object_prototype.As<v8::Object>()
->SetNativeDataProperty(
context, StringToSymbol(isolate, "key"),
[](v8::Local<v8::Name>,
const v8::PropertyCallbackInfo<v8::Value>&) {},
[](v8::Local<v8::Name>, v8::Local<v8::Value>,
const v8::PropertyCallbackInfo<void>&) {
ADD_FAILURE() << "setter should not be invoked";
})
.ToChecked());
DataObjectBuilder(isolate).Set("key", 42).Build();
}
#if DCHECK_IS_ON() && !BUILDFLAG(DCHECK_IS_CONFIGURABLE)
TEST_F(DataObjectBuilderTest, UnusableAfterBuild) {
v8::Isolate* isolate = instance_->isolate();
v8::HandleScope handle_scope(isolate);
DataObjectBuilder builder(isolate);
EXPECT_FALSE(builder.Build().IsEmpty());
EXPECT_DEATH_IF_SUPPORTED(builder.Build(),
"DCHECK failed: !object_.IsEmpty\\(\\)");
}
#endif
TEST_F(DataObjectBuilderTest, ReplacesExistingProperties) {
v8::Isolate* isolate = instance_->isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Object> object =
DataObjectBuilder(isolate).Set("value", 42).Set("value", 55).Build();
gin::Dictionary dictionary(isolate, object);
int32_t value;
ASSERT_TRUE(dictionary.Get("value", &value));
EXPECT_EQ(55, value);
}
TEST_F(DataObjectBuilderTest, CreatesDataPropertiesForIndices) {
v8::Isolate* isolate = instance_->isolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = context_.Get(isolate);
v8::Local<v8::Object> object =
DataObjectBuilder(isolate).Set(42, std::string_view("forty-two")).Build();
ASSERT_TRUE(object->HasOwnProperty(context, 42).ToChecked());
v8::Local<v8::Value> descriptor_object;
ASSERT_TRUE(
object->GetOwnPropertyDescriptor(context, StringToSymbol(isolate, "42"))
.ToLocal(&descriptor_object));
gin::Dictionary descriptor(isolate, descriptor_object.As<v8::Object>());
std::string value;
ASSERT_TRUE(descriptor.Get("value", &value));
EXPECT_EQ("forty-two", value);
bool writable = false;
ASSERT_TRUE(descriptor.Get("writable", &writable));
EXPECT_TRUE(writable);
bool enumerable = false;
ASSERT_TRUE(descriptor.Get("enumerable", &enumerable));
EXPECT_TRUE(enumerable);
bool configurable = false;
ASSERT_TRUE(descriptor.Get("configurable", &configurable));
EXPECT_TRUE(configurable);
}
}
}