blob: 3e4df1150e9c633dad40ca445b366f2aefeb0c0b [file] [log] [blame]
{#
# Copyright 2017 Google Inc. 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}} Google Inc. 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 "cobalt/script/exception_state.h"
#include "third_party/mozjs/js/src/jsapi.h"
using {{fully_qualified_name}};
namespace cobalt {
namespace script {
namespace mozjs {
void ToJSValue(
JSContext* context,
{{enumeration_name}} in_enum,
JS::MutableHandleValue out_value) {
switch (in_enum) {
{% for value, idl_value in value_pairs %}
case {{namespace}}::{{value}}:
ToJSValue(context, std::string("{{idl_value}}"), out_value);
return;
{% endfor %}
default:
NOTREACHED();
out_value.set(JS::UndefinedValue());
}
}
void FromJSValue(JSContext* context, JS::HandleValue 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).
JS::RootedString rooted_string(context, JS_ValueToString(context, value));
JSBool match = JS_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 (JS_StringEqualsAscii(
context, rooted_string, "{{idl_value}}", &match)
&& match) {
*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 mozjs
} // namespace script
} // namespace cobalt