blob: 664bbf129248a0c837a8eae30e8b5e878a26f035 [file] [log] [blame]
{% from 'utilities.cpp.tmpl' import declare_enum_validation_variable, v8_value_to_local_cpp_value %}
{% filter format_blink_cpp_source_code %}
{% include 'copyright_block.txt' %}
#include "{{cpp_class}}.h"
{% for filename in cpp_includes %}
#include "{{filename}}"
{% endfor %}
namespace blink {
// static
{{cpp_class}}* {{cpp_class}}::Create(ScriptState* scriptState, v8::Local<v8::Value> callback) {
if (IsUndefinedOrNull(callback))
return nullptr;
return new {{cpp_class}}(scriptState, v8::Local<v8::Function>::Cast(callback));
}
{{cpp_class}}::{{cpp_class}}(ScriptState* scriptState, v8::Local<v8::Function> callback)
: script_state_(scriptState),
callback_(scriptState->GetIsolate(), this, callback) {
DCHECK(!callback_.IsEmpty());
}
DEFINE_TRACE_WRAPPERS({{cpp_class}}) {
visitor->TraceWrappers(callback_.Cast<v8::Value>());
}
bool {{cpp_class}}::call({{argument_declarations | join(', ')}}) {
if (callback_.IsEmpty())
return false;
if (!script_state_->ContextIsValid())
return false;
// TODO(bashi): Make sure that using DummyExceptionStateForTesting is OK.
// crbug.com/653769
DummyExceptionStateForTesting exceptionState;
{% for argument in arguments if argument.enum_values %}
{% set valid_enum_variables = 'valid_' + argument.name + '_values' %}
{{declare_enum_validation_variable(argument.enum_values, valid_enum_variables) | indent(2)}}
if (!IsValidEnum({{argument.name}}, {{valid_enum_variables}}, WTF_ARRAY_LENGTH({{valid_enum_variables}}), "{{argument.enum_type}}", exceptionState)) {
NOTREACHED();
return false;
}
{% endfor %}
ExecutionContext* context = ExecutionContext::From(script_state_.Get());
DCHECK(context);
if (context->IsContextSuspended() || context->IsContextDestroyed())
return false;
ScriptState::Scope scope(script_state_.Get());
v8::Isolate* isolate = script_state_->GetIsolate();
v8::Local<v8::Value> thisValue = ToV8(
scriptWrappable,
script_state_->GetContext()->Global(),
isolate);
{% for argument in arguments %}
v8::Local<v8::Value> {{argument.v8_name}} = {{argument.cpp_value_to_v8_value}};
{% endfor %}
{% if arguments %}
v8::Local<v8::Value> argv[] = { {{arguments | join(', ', 'v8_name')}} };
{% else %}
{# Empty array initializers are illegal, and don\'t compile in MSVC. #}
v8::Local<v8::Value> *argv = nullptr;
{% endif %}
v8::TryCatch exceptionCatcher(isolate);
exceptionCatcher.SetVerbose(true);
v8::Local<v8::Value> v8ReturnValue;
if (!V8ScriptRunner::CallFunction(callback_.NewLocal(isolate),
context,
thisValue,
{{arguments | length}},
argv,
isolate).ToLocal(&v8ReturnValue)) {
return false;
}
{% if return_value %}
{{v8_value_to_local_cpp_value(return_value) | indent(2)}}
returnValue = cppValue;
{% endif %}
return true;
}
{{cpp_class}}* NativeValueTraits<{{cpp_class}}>::NativeValue(v8::Isolate* isolate, v8::Local<v8::Value> value, ExceptionState& exceptionState) {
{{cpp_class}}* nativeValue = {{cpp_class}}::Create(ScriptState::Current(isolate), value);
if (!nativeValue) {
exceptionState.ThrowTypeError(ExceptionMessages::FailedToConvertJSValue(
"{{callback_function_name}}"));
}
return nativeValue;
}
} // namespace blink
{% endfilter %}{# format_blink_cpp_source_code #}