Commit 9f682d48 authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

bind-gen: Print a warning to console when invalid enum is set

Prints a warning message to the inspector's console when an
invalid enum value is set to an IDL attribute.

Bug: 839389
Change-Id: I27f12b5af8b510ea748ef2b3238af4d40e7e485a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2202831
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#770006}
parent 2d0380d5
......@@ -13,6 +13,7 @@
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/html/custom/ce_reactions_scope.h"
#include "third_party/blink/renderer/core/html/custom/v0_custom_element_processing_stack.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/core/xml/dom_parser.h"
#include "third_party/blink/renderer/platform/bindings/v8_per_context_data.h"
......@@ -188,6 +189,25 @@ base::Optional<size_t> FindIndexInEnumStringTable(
return base::nullopt;
}
void ReportInvalidEnumSetToAttribute(v8::Isolate* isolate,
const String& value,
const String& enum_type_name,
ExceptionState& exception_state) {
ScriptState* script_state = ScriptState::From(isolate->GetCurrentContext());
ExecutionContext* execution_context = ExecutionContext::From(script_state);
exception_state.ThrowTypeError("The provided value '" + value +
"' is not a valid enum value of type " +
enum_type_name + ".");
String message = exception_state.Message();
exception_state.ClearException();
execution_context->AddConsoleMessage(MakeGarbageCollected<ConsoleMessage>(
mojom::blink::ConsoleMessageSource::kJavaScript,
mojom::blink::ConsoleMessageLevel::kWarning, message,
SourceLocation::Capture(execution_context)));
}
bool IsEsIterableObject(v8::Isolate* isolate,
v8::Local<v8::Value> value,
ExceptionState& exception_state) {
......
......@@ -143,6 +143,12 @@ CORE_EXPORT base::Optional<size_t> FindIndexInEnumStringTable(
const String& str_value,
base::span<const char* const> enum_value_table);
CORE_EXPORT void ReportInvalidEnumSetToAttribute(
v8::Isolate* isolate,
const String& value,
const String& enum_type_name,
ExceptionState& exception_state);
CORE_EXPORT bool IsEsIterableObject(v8::Isolate* isolate,
v8::Local<v8::Value> value,
ExceptionState& exception_state);
......
......@@ -222,13 +222,17 @@ if (${exception_state}.HadException())
// step 4.6.2. If S is not one of the enumeration's values, then return
// undefined.
const auto arg1_value_maybe_enum = {enum_type}::Create(arg1_value_string);
if (!arg1_value_maybe_enum)
if (!arg1_value_maybe_enum) {{
bindings::ReportInvalidEnumSetToAttribute(
${isolate}, arg1_value_string, "{enum_type_name}", ${exception_state});
return; // Return undefined.
}}
const auto ${arg1_value} = arg1_value_maybe_enum.value();
"""
text = _format(
pattern,
enum_type=blink_class_name(real_type.type_definition_object))
text = _format(pattern,
enum_type=blink_class_name(
real_type.type_definition_object),
enum_type_name=real_type.identifier)
code_node.register_code_symbol(SymbolNode("arg1_value", text))
return
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment