[api] TC39 Dynamic Code Brand checks - rename for consistency.

Rename-only CL: Rename "code kind" to "code like".

The reason is CL feedback when using this feature, and a desire for
consistency across V8 + Blink. An additional benefit would be to
disambiguate from the v8::internal::CodeKind type, which is unrelated to
any of this.

Original CL: crrev.com/c/v8/v8/+/2339618
CL whose review prompted this change: crrev.com/c/2340905

Bug: chromium:1096017
Change-Id: Id59016fc2906ab6cd1414e598338b3963811b92f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2509598
Reviewed-by: Camillo Bruni <cbruni@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Daniel Vogelheim <vogelheim@chromium.org>
Cr-Commit-Position: refs/heads/master@{#70970}
This commit is contained in:
Daniel Vogelheim 2020-10-30 17:38:33 +01:00 committed by Commit Bot
parent a8eea87933
commit 543e5633af
13 changed files with 95 additions and 95 deletions

View File

@ -4207,11 +4207,11 @@ class V8_EXPORT Object : public Value {
* Support for TC39 "dynamic code brand checks" proposal.
*
* This API allows to query whether an object was constructed from a
* "code kind" ObjectTemplate.
* "code like" ObjectTemplate.
*
* See also: v8::ObjectTemplate::SetCodeKind
* See also: v8::ObjectTemplate::SetCodeLike
*/
bool IsCodeKind(Isolate* isolate);
bool IsCodeLike(Isolate* isolate);
private:
Object();
@ -7007,14 +7007,14 @@ class V8_EXPORT ObjectTemplate : public Template {
/**
* Support for TC39 "dynamic code brand checks" proposal.
*
* This API allows to mark (& query) objects as "code kind", which causes
* them to be treated as code-like (i.e. like Strings) in the context of
* eval and function constructor.
* This API allows to mark (& query) objects as "code like", which causes
* them to be treated like Strings in the context of eval and function
* constructor.
*
* Reference: https://github.com/tc39/proposal-dynamic-code-brand-checks
*/
void SetCodeKind();
bool IsCodeKind();
void SetCodeLike();
bool IsCodeLike();
V8_INLINE static ObjectTemplate* Cast(Data* data);
@ -7598,7 +7598,7 @@ typedef ModifyCodeGenerationFromStringsResult (
typedef ModifyCodeGenerationFromStringsResult (
*ModifyCodeGenerationFromStringsCallback2)(Local<Context> context,
Local<Value> source,
bool is_code_kind);
bool is_code_like);
// --- WebAssembly compilation callbacks ---
typedef bool (*ExtensionCallback)(const FunctionCallbackInfo<Value>&);

View File

@ -2005,15 +2005,15 @@ void ObjectTemplate::SetImmutableProto() {
self->set_immutable_proto(true);
}
bool ObjectTemplate::IsCodeKind() {
return Utils::OpenHandle(this)->code_kind();
bool ObjectTemplate::IsCodeLike() {
return Utils::OpenHandle(this)->code_like();
}
void ObjectTemplate::SetCodeKind() {
void ObjectTemplate::SetCodeLike() {
auto self = Utils::OpenHandle(this);
i::Isolate* isolate = self->GetIsolate();
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
self->set_code_kind(true);
self->set_code_like(true);
}
// --- S c r i p t s ---
@ -9212,12 +9212,12 @@ void v8::Isolate::LocaleConfigurationChangeNotification() {
#endif // V8_INTL_SUPPORT
}
bool v8::Object::IsCodeKind(v8::Isolate* isolate) {
bool v8::Object::IsCodeLike(v8::Isolate* isolate) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
LOG_API(i_isolate, Object, IsCodeKind);
LOG_API(i_isolate, Object, IsCodeLike);
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
i::HandleScope scope(i_isolate);
return Utils::OpenHandle(this)->IsCodeKind(i_isolate);
return Utils::OpenHandle(this)->IsCodeLike(i_isolate);
}
// static

View File

@ -80,10 +80,10 @@ MaybeHandle<Object> CreateDynamicFunction(Isolate* isolate,
}
}
bool is_code_kind = true;
bool is_code_like = true;
for (int i = 0; i < argc; ++i) {
if (!args.at(i + 1)->IsCodeKind(isolate)) {
is_code_kind = false;
if (!args.at(i + 1)->IsCodeLike(isolate)) {
is_code_like = false;
break;
}
}
@ -96,7 +96,7 @@ MaybeHandle<Object> CreateDynamicFunction(Isolate* isolate,
isolate, function,
Compiler::GetFunctionFromString(
handle(target->native_context(), isolate), source,
ONLY_SINGLE_FUNCTION_LITERAL, parameters_end_pos, is_code_kind),
ONLY_SINGLE_FUNCTION_LITERAL, parameters_end_pos, is_code_like),
Object);
Handle<Object> result;
ASSIGN_RETURN_ON_EXCEPTION(

View File

@ -2076,7 +2076,7 @@ bool CodeGenerationFromStringsAllowed(Isolate* isolate, Handle<Context> context,
// or v8::Isolate::SetModifyCodeGenerationFromStringsCallback2)
bool ModifyCodeGenerationFromStrings(Isolate* isolate, Handle<Context> context,
Handle<i::Object>* source,
bool is_code_kind) {
bool is_code_like) {
DCHECK(isolate->modify_code_gen_callback() ||
isolate->modify_code_gen_callback2());
DCHECK(source);
@ -2092,7 +2092,7 @@ bool ModifyCodeGenerationFromStrings(Isolate* isolate, Handle<Context> context,
v8::Utils::ToLocal(*source))
: isolate->modify_code_gen_callback2()(v8::Utils::ToLocal(context),
v8::Utils::ToLocal(*source),
is_code_kind);
is_code_like);
if (result.codegen_allowed && !result.modified_source.IsEmpty()) {
// Use the new source (which might be the same as the old source).
*source =
@ -2118,7 +2118,7 @@ bool ModifyCodeGenerationFromStrings(Isolate* isolate, Handle<Context> context,
// static
std::pair<MaybeHandle<String>, bool> Compiler::ValidateDynamicCompilationSource(
Isolate* isolate, Handle<Context> context,
Handle<i::Object> original_source, bool is_code_kind) {
Handle<i::Object> original_source, bool is_code_like) {
// Check if the context unconditionally allows code gen from strings.
// allow_code_gen_from_strings can be many things, so we'll always check
// against the 'false' literal, so that e.g. undefined and 'true' are treated
@ -2133,9 +2133,9 @@ std::pair<MaybeHandle<String>, bool> Compiler::ValidateDynamicCompilationSource(
// (I.e., let allow_code_gen_callback decide, if it has been set.)
if (isolate->allow_code_gen_callback()) {
// If we run into this condition, the embedder has marked some object
// templates as "code kind", but has given us a callback that only accepts
// templates as "code like", but has given us a callback that only accepts
// strings. That makes no sense.
DCHECK(!original_source->IsCodeKind(isolate));
DCHECK(!original_source->IsCodeLike(isolate));
if (!original_source->IsString()) {
return {MaybeHandle<String>(), true};
@ -2154,7 +2154,7 @@ std::pair<MaybeHandle<String>, bool> Compiler::ValidateDynamicCompilationSource(
isolate->modify_code_gen_callback2()) {
Handle<i::Object> modified_source = original_source;
if (!ModifyCodeGenerationFromStrings(isolate, context, &modified_source,
is_code_kind)) {
is_code_like)) {
return {MaybeHandle<String>(), false};
}
if (!modified_source->IsString()) {
@ -2164,8 +2164,8 @@ std::pair<MaybeHandle<String>, bool> Compiler::ValidateDynamicCompilationSource(
}
if (!context->allow_code_gen_from_strings().IsFalse(isolate) &&
original_source->IsCodeKind(isolate)) {
// Codegen is unconditionally allowed, and we're been given a CodeKind
original_source->IsCodeLike(isolate)) {
// Codegen is unconditionally allowed, and we're been given a CodeLike
// object. Stringify.
MaybeHandle<String> stringified_source =
Object::ToString(isolate, original_source);
@ -2208,10 +2208,10 @@ MaybeHandle<JSFunction> Compiler::GetFunctionFromValidatedString(
// static
MaybeHandle<JSFunction> Compiler::GetFunctionFromString(
Handle<Context> context, Handle<Object> source,
ParseRestriction restriction, int parameters_end_pos, bool is_code_kind) {
ParseRestriction restriction, int parameters_end_pos, bool is_code_like) {
Isolate* const isolate = context->GetIsolate();
MaybeHandle<String> validated_source =
ValidateDynamicCompilationSource(isolate, context, source, is_code_kind)
ValidateDynamicCompilationSource(isolate, context, source, is_code_like)
.first;
return GetFunctionFromValidatedString(context, validated_source, restriction,
parameters_end_pos);

View File

@ -142,14 +142,14 @@ class V8_EXPORT_PRIVATE Compiler : public AllStatic {
// Create a (bound) function for a String source within a context for eval.
V8_WARN_UNUSED_RESULT static MaybeHandle<JSFunction> GetFunctionFromString(
Handle<Context> context, Handle<i::Object> source,
ParseRestriction restriction, int parameters_end_pos, bool is_code_kind);
ParseRestriction restriction, int parameters_end_pos, bool is_code_like);
// Decompose GetFunctionFromString into two functions, to allow callers to
// deal seperately with a case of object not handled by the embedder.
V8_WARN_UNUSED_RESULT static std::pair<MaybeHandle<String>, bool>
ValidateDynamicCompilationSource(Isolate* isolate, Handle<Context> context,
Handle<i::Object> source_object,
bool is_code_kind = false);
bool is_code_like = false);
V8_WARN_UNUSED_RESULT static MaybeHandle<JSFunction>
GetFunctionFromValidatedString(Handle<Context> context,
MaybeHandle<String> source,

View File

@ -790,7 +790,7 @@ class RuntimeCallTimer final {
V(Object_HasRealIndexedProperty) \
V(Object_HasRealNamedCallbackProperty) \
V(Object_HasRealNamedProperty) \
V(Object_IsCodeKind) \
V(Object_IsCodeLike) \
V(Object_New) \
V(Object_ObjectProtoToString) \
V(Object_Set) \

View File

@ -2068,7 +2068,7 @@ bool JSReceiver::HasProxyInPrototype(Isolate* isolate) {
return false;
}
bool JSReceiver::IsCodeKind(Isolate* isolate) const {
bool JSReceiver::IsCodeLike(Isolate* isolate) const {
DisallowGarbageCollection no_gc;
Object maybe_constructor = map().GetConstructor();
if (!maybe_constructor.IsJSFunction()) return false;
@ -2080,7 +2080,7 @@ bool JSReceiver::IsCodeKind(Isolate* isolate) const {
.get_api_func_data()
.GetInstanceTemplate();
if (instance_template.IsUndefined(isolate)) return false;
return ObjectTemplateInfo::cast(instance_template).code_kind();
return ObjectTemplateInfo::cast(instance_template).code_like();
}
// static

View File

@ -286,7 +286,7 @@ class JSReceiver : public HeapObject {
bool HasProxyInPrototype(Isolate* isolate);
// TC39 "Dynamic Code Brand Checks"
bool IsCodeKind(Isolate* isolate) const;
bool IsCodeLike(Isolate* isolate) const;
OBJECT_CONSTRUCTORS(JSReceiver, HeapObject);
};

View File

@ -1809,9 +1809,9 @@ bool Object::IterationHasObservableEffects() {
return true;
}
bool Object::IsCodeKind(Isolate* isolate) const {
bool Object::IsCodeLike(Isolate* isolate) const {
DisallowGarbageCollection no_gc;
return IsJSReceiver() && JSReceiver::cast(*this).IsCodeKind(isolate);
return IsJSReceiver() && JSReceiver::cast(*this).IsCodeLike(isolate);
}
void Object::ShortPrint(FILE* out) const {

View File

@ -587,7 +587,7 @@ class Object : public TaggedImpl<HeapObjectReferenceType::STRONG, Address> {
bool IterationHasObservableEffects();
// TC39 "Dynamic Code Brand Checks"
bool IsCodeKind(Isolate* isolate) const;
bool IsCodeLike(Isolate* isolate) const;
EXPORT_DECL_VERIFIER(Object)

View File

@ -134,12 +134,12 @@ void ObjectTemplateInfo::set_immutable_proto(bool immutable) {
return set_data(IsImmutablePrototypeBit::update(data(), immutable));
}
bool ObjectTemplateInfo::code_kind() const {
bool ObjectTemplateInfo::code_like() const {
return IsCodeKindBit::decode(data());
}
void ObjectTemplateInfo::set_code_kind(bool is_code_kind) {
return set_data(IsCodeKindBit::update(data(), is_code_kind));
void ObjectTemplateInfo::set_code_like(bool is_code_like) {
return set_data(IsCodeKindBit::update(data(), is_code_like));
}
bool FunctionTemplateInfo::IsTemplateFor(JSObject object) {

View File

@ -160,7 +160,7 @@ class ObjectTemplateInfo
public:
DECL_INT_ACCESSORS(embedder_field_count)
DECL_BOOLEAN_ACCESSORS(immutable_proto)
DECL_BOOLEAN_ACCESSORS(code_kind)
DECL_BOOLEAN_ACCESSORS(code_like)
// Dispatched behavior.
DECL_PRINTER(ObjectTemplateInfo)

View File

@ -19428,7 +19428,7 @@ void CheckCodeGenerationDisallowed() {
char first_fourty_bytes[41];
v8::ModifyCodeGenerationFromStringsResult CodeGenerationAllowed(
Local<Context> context, Local<Value> source, bool is_code_kind) {
Local<Context> context, Local<Value> source, bool is_code_like) {
String::Utf8Value str(CcTest::isolate(), source);
size_t len = std::min(sizeof(first_fourty_bytes) - 1,
static_cast<size_t>(str.length()));
@ -19439,13 +19439,13 @@ v8::ModifyCodeGenerationFromStringsResult CodeGenerationAllowed(
}
v8::ModifyCodeGenerationFromStringsResult CodeGenerationDisallowed(
Local<Context> context, Local<Value> source, bool is_code_kind) {
Local<Context> context, Local<Value> source, bool is_code_like) {
ApiTestFuzzer::Fuzz();
return {false, {}};
}
v8::ModifyCodeGenerationFromStringsResult ModifyCodeGeneration(
Local<Context> context, Local<Value> source, bool is_code_kind) {
Local<Context> context, Local<Value> source, bool is_code_like) {
// Allow (passthrough, unmodified) all objects that are not strings.
if (!source->IsString()) {
return {/* codegen_allowed= */ true, v8::MaybeLocal<String>()};
@ -19541,7 +19541,7 @@ TEST(ModifyCodeGenFromStrings) {
}
v8::ModifyCodeGenerationFromStringsResult RejectStringsIncrementNumbers(
Local<Context> context, Local<Value> source, bool is_code_kind) {
Local<Context> context, Local<Value> source, bool is_code_like) {
if (source->IsString()) {
return {false, v8::MaybeLocal<String>()};
}
@ -28624,19 +28624,19 @@ TEST(TriggerThreadSafeMetricsEvent) {
CHECK_EQ(recorder->module_count_, 42);
}
void SetupCodeKind(LocalContext* env, const char* name,
void SetupCodeLike(LocalContext* env, const char* name,
v8::Local<v8::FunctionTemplate> to_string,
bool is_code_kind) {
// Setup a JS constructor + object template for testing IsCodeKind.
bool is_code_like) {
// Setup a JS constructor + object template for testing IsCodeLike.
v8::Local<FunctionTemplate> constructor =
v8::FunctionTemplate::New((*env)->GetIsolate());
constructor->SetClassName(v8_str(name));
constructor->InstanceTemplate()->Set((*env)->GetIsolate(), "toString",
to_string);
if (is_code_kind) {
constructor->InstanceTemplate()->SetCodeKind();
if (is_code_like) {
constructor->InstanceTemplate()->SetCodeLike();
}
CHECK_EQ(is_code_kind, constructor->InstanceTemplate()->IsCodeKind());
CHECK_EQ(is_code_like, constructor->InstanceTemplate()->IsCodeLike());
CHECK((*env)
->Global()
->Set(env->local(), v8_str(name),
@ -28644,74 +28644,74 @@ void SetupCodeKind(LocalContext* env, const char* name,
.FromJust());
}
TEST(CodeKindEval) {
TEST(CodeLikeEval) {
LocalContext env;
v8::Isolate* isolate = env->GetIsolate();
v8::HandleScope scope(isolate);
// Setup two object templates with an eval-able string representation.
// One code kind, one not, and otherwise identical.
// One code-like, one not, and otherwise identical.
auto string_fn = v8::FunctionTemplate::New(
isolate, [](const v8::FunctionCallbackInfo<v8::Value>& info) {
info.GetReturnValue().Set(v8_str("2+2"));
});
SetupCodeKind(&env, "CodeKind", string_fn, true);
SetupCodeKind(&env, "OtherKind", string_fn, false);
SetupCodeLike(&env, "CodeLike", string_fn, true);
SetupCodeLike(&env, "Other", string_fn, false);
// Check v8::Object::IsCodeKind.
CHECK(CompileRun("new CodeKind()").As<v8::Object>()->IsCodeKind(isolate));
CHECK(!CompileRun("new OtherKind()").As<v8::Object>()->IsCodeKind(isolate));
// Check v8::Object::IsCodeLike.
CHECK(CompileRun("new CodeLike()").As<v8::Object>()->IsCodeLike(isolate));
CHECK(!CompileRun("new Other()").As<v8::Object>()->IsCodeLike(isolate));
// Expected behaviour for normal objects:
// - eval returns them as-is
// - when pre-stringified, the string gets evaluated (of course)
ExpectString("eval(new OtherKind()) + \"\"", "2+2");
ExpectInt32("eval(\"\" + new OtherKind())", 4);
ExpectString("eval(new Other()) + \"\"", "2+2");
ExpectInt32("eval(\"\" + new Other())", 4);
// Expected behaviour for 'code kind': Is always evaluated.
ExpectInt32("eval(new CodeKind())", 4);
ExpectInt32("eval(\"\" + new CodeKind())", 4);
// Expected behaviour for 'code like': Is always evaluated.
ExpectInt32("eval(new CodeLike())", 4);
ExpectInt32("eval(\"\" + new CodeLike())", 4);
// Modify callback will always returns a replacement string:
// Expected behaviour: Always execute the replacement string.
isolate->SetModifyCodeGenerationFromStringsCallback(
[](v8::Local<v8::Context> context, v8::Local<v8::Value> source,
bool is_code_kind) -> v8::ModifyCodeGenerationFromStringsResult {
bool is_code_like) -> v8::ModifyCodeGenerationFromStringsResult {
return {true, v8_str("3+3")};
});
ExpectInt32("eval(new OtherKind())", 6);
ExpectInt32("eval(new CodeKind())", 6);
ExpectInt32("eval(new Other())", 6);
ExpectInt32("eval(new CodeLike())", 6);
// Modify callback always disallows:
// Expected behaviour: Always fail to execute.
isolate->SetModifyCodeGenerationFromStringsCallback(
[](v8::Local<v8::Context> context, v8::Local<v8::Value> source,
bool is_code_kind) -> v8::ModifyCodeGenerationFromStringsResult {
bool is_code_like) -> v8::ModifyCodeGenerationFromStringsResult {
return {false, v8::Local<v8::String>()};
});
CHECK(CompileRun("eval(new OtherKind())").IsEmpty());
CHECK(CompileRun("eval(new CodeKind())").IsEmpty());
CHECK(CompileRun("eval(new Other())").IsEmpty());
CHECK(CompileRun("eval(new CodeLike())").IsEmpty());
// Modify callback allows only "code kind":
// Expected behaviour: Only code_kind executed, with replacement string.
// Modify callback allows only "code like":
// Expected behaviour: Only code-like executed, with replacement string.
isolate->SetModifyCodeGenerationFromStringsCallback(
[](v8::Local<v8::Context> context, v8::Local<v8::Value> source,
bool is_code_kind) -> v8::ModifyCodeGenerationFromStringsResult {
bool ok = is_code_kind ||
bool is_code_like) -> v8::ModifyCodeGenerationFromStringsResult {
bool ok = is_code_like ||
(source->IsObject() &&
source.As<v8::Object>()->IsCodeKind(context->GetIsolate()));
source.As<v8::Object>()->IsCodeLike(context->GetIsolate()));
return {ok, v8_str("5+7")};
});
CHECK(CompileRun("eval(new OtherKind())").IsEmpty());
ExpectInt32("eval(new CodeKind())", 12);
CHECK(CompileRun("eval(new Other())").IsEmpty());
ExpectInt32("eval(new CodeLike())", 12);
}
TEST(CodeKindFunction) {
TEST(CodeLikeFunction) {
LocalContext env;
v8::Isolate* isolate = env->GetIsolate();
v8::HandleScope scope(isolate);
// These follow the pattern of the CodeKindEval test above, but with
// These follow the pattern of the CodeLikeEval test above, but with
// "new Function" instead of eval.
// Setup two object templates with an eval-able string representation.
@ -28720,40 +28720,40 @@ TEST(CodeKindFunction) {
isolate, [](const v8::FunctionCallbackInfo<v8::Value>& info) {
info.GetReturnValue().Set(v8_str("return 2+2"));
});
SetupCodeKind(&env, "CodeKind", string_fn, true);
SetupCodeKind(&env, "OtherKind", string_fn, false);
SetupCodeLike(&env, "CodeLike", string_fn, true);
SetupCodeLike(&env, "Other", string_fn, false);
ExpectInt32("new Function(new OtherKind())()", 4);
ExpectInt32("new Function(new CodeKind())()", 4);
ExpectInt32("new Function(new Other())()", 4);
ExpectInt32("new Function(new CodeLike())()", 4);
// Modify callback will always return a replacement string:
env.local()->AllowCodeGenerationFromStrings(false);
isolate->SetModifyCodeGenerationFromStringsCallback(
[](v8::Local<v8::Context> context, v8::Local<v8::Value> source,
bool is_code_kind) -> v8::ModifyCodeGenerationFromStringsResult {
bool is_code_like) -> v8::ModifyCodeGenerationFromStringsResult {
return {true, v8_str("(function anonymous(\n) {\nreturn 7;\n})\n")};
});
ExpectInt32("new Function(new OtherKind())()", 7);
ExpectInt32("new Function(new CodeKind())()", 7);
ExpectInt32("new Function(new Other())()", 7);
ExpectInt32("new Function(new CodeLike())()", 7);
// Modify callback always disallows:
isolate->SetModifyCodeGenerationFromStringsCallback(
[](v8::Local<v8::Context> context, v8::Local<v8::Value> source,
bool is_code_kind) -> v8::ModifyCodeGenerationFromStringsResult {
bool is_code_like) -> v8::ModifyCodeGenerationFromStringsResult {
return {false, v8::Local<v8::String>()};
});
CHECK(CompileRun("new Function(new OtherKind())()").IsEmpty());
CHECK(CompileRun("new Function(new CodeKind())()").IsEmpty());
CHECK(CompileRun("new Function(new Other())()").IsEmpty());
CHECK(CompileRun("new Function(new CodeLike())()").IsEmpty());
// Modify callback allows only "code kind":
isolate->SetModifyCodeGenerationFromStringsCallback(
[](v8::Local<v8::Context> context, v8::Local<v8::Value> source,
bool is_code_kind) -> v8::ModifyCodeGenerationFromStringsResult {
bool ok = is_code_kind ||
bool is_code_like) -> v8::ModifyCodeGenerationFromStringsResult {
bool ok = is_code_like ||
(source->IsObject() &&
source.As<v8::Object>()->IsCodeKind(context->GetIsolate()));
source.As<v8::Object>()->IsCodeLike(context->GetIsolate()));
return {ok, v8_str("(function anonymous(\n) {\nreturn 7;\n})\n")};
});
CHECK(CompileRun("new Function(new OtherKind())()").IsEmpty());
ExpectInt32("new Function(new CodeKind())()", 7);
CHECK(CompileRun("new Function(new Other())()").IsEmpty());
ExpectInt32("new Function(new CodeLike())()", 7);
}