blob: 037df6fc97b11d65fad842873d153de370e1d4a2 [file] [log] [blame]
{#
# Copyright 2017 The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#}
// Copyright {{today.year}} The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// clang-format off
// This file has been auto-generated by {{code_generator}}. DO NOT MODIFY!
// Auto-generated from template: {{template_path}}
#include "{{enum_include}}"
#include "{{generated_conversion_include}}"
#include "base/logging.h"
#include "cobalt/script/exception_state.h"
#include "cobalt/script/v8c/helpers.h"
#include "v8/include/v8.h"
using {{fully_qualified_name}};
namespace cobalt {
namespace script {
namespace v8c {
void ToJSValue(
v8::Isolate* isolate,
{{enumeration_name}} in_enum,
v8::Local<v8::Value>* out_value) {
switch (in_enum) {
{% for value, idl_value in value_pairs %}
case {{namespace}}::{{value}}:
ToJSValue(isolate, std::string("{{idl_value}}"), out_value);
return;
{% endfor %}
default:
NOTREACHED();
*out_value = v8::Null(isolate);
}
}
void FromJSValue(v8::Isolate* isolate, v8::Local<v8::Value> value,
int conversion_flags, ExceptionState* exception_state,
{{enumeration_name}}* out_enum) {
DCHECK_EQ(0, conversion_flags) << "Unexpected conversion flags.";
// JSValue -> IDL enum algorithm described here:
// http://heycam.github.io/webidl/#es-enumeration
// 1. Let S be the result of calling ToString(V).
v8::MaybeLocal<v8::String> maybe_string = value->ToString(isolate->GetCurrentContext());
v8::Local<v8::String> string;
if (!maybe_string.ToLocal(&string)) {
exception_state->SetSimpleException(cobalt::script::kConvertToEnumFailed);
return;
}
bool match = false;
// 3. Return the enumeration value of type E that is equal to S.
{% for value, idl_value in value_pairs %}
{{-" else " if not loop.first}} if (
NewInternalString(isolate, "{{idl_value}}")->Equals(value))
{
*out_enum = {{namespace}}::{{value}};
}
{% endfor %}
else {
// 2. If S is not one of E's enumeration values, then throw a TypeError.
exception_state->SetSimpleException(cobalt::script::kConvertToEnumFailed);
return;
}
}
} // namespace v8c
} // namespace script
} // namespace cobalt