| OLD | NEW |
| 1 {% from 'utilities.cpp.tmpl' import v8_value_to_local_cpp_value %} | 1 {% from 'utilities.cpp.tmpl' import v8_value_to_local_cpp_value %} |
| 2 {% filter format_blink_cpp_source_code %} | 2 {% filter format_blink_cpp_source_code %} |
| 3 | 3 |
| 4 {% include 'copyright_block.txt' %} | 4 {% include 'copyright_block.txt' %} |
| 5 | 5 |
| 6 #include "{{cpp_class}}.h" | 6 #include "{{cpp_class}}.h" |
| 7 | 7 |
| 8 {% for filename in cpp_includes %} | 8 {% for filename in cpp_includes %} |
| 9 #include "{{filename}}" | 9 #include "{{filename}}" |
| 10 {% endfor %} | 10 {% endfor %} |
| 11 | 11 |
| 12 namespace blink { | 12 namespace blink { |
| 13 | 13 |
| 14 // static | 14 // static |
| 15 {{cpp_class}}* {{cpp_class}}::create(ScriptState* scriptState, v8::Local<v8::Val
ue> callback){ | 15 {{cpp_class}}* {{cpp_class}}::create(ScriptState* scriptState, v8::Local<v8::Val
ue> callback) { |
| 16 if (isUndefinedOrNull(callback)) | 16 if (isUndefinedOrNull(callback)) |
| 17 return nullptr; | 17 return nullptr; |
| 18 return new {{cpp_class}}(scriptState, v8::Local<v8::Function>::Cast(callback))
; | 18 return new {{cpp_class}}(scriptState, v8::Local<v8::Function>::Cast(callback))
; |
| 19 } | 19 } |
| 20 | 20 |
| 21 {{cpp_class}}::{{cpp_class}}(ScriptState* scriptState, v8::Local<v8::Function> c
allback) | 21 {{cpp_class}}::{{cpp_class}}(ScriptState* scriptState, v8::Local<v8::Function> c
allback) |
| 22 : m_scriptState(scriptState), | 22 : m_scriptState(scriptState), |
| 23 m_callback(scriptState->isolate(), this, callback) { | 23 m_callback(scriptState->isolate(), this, callback) { |
| 24 DCHECK(!m_callback.isEmpty()); | 24 DCHECK(!m_callback.isEmpty()); |
| 25 } | 25 } |
| 26 | 26 |
| 27 DEFINE_TRACE({{cpp_class}}) {} | |
| 28 | |
| 29 DEFINE_TRACE_WRAPPERS({{cpp_class}}) { | 27 DEFINE_TRACE_WRAPPERS({{cpp_class}}) { |
| 30 visitor->traceWrappers(m_callback.cast<v8::Value>()); | 28 visitor->traceWrappers(m_callback.cast<v8::Value>()); |
| 31 } | 29 } |
| 32 | 30 |
| 33 bool {{cpp_class}}::call({{argument_declarations | join(', ')}}) { | 31 bool {{cpp_class}}::call({{argument_declarations | join(', ')}}) { |
| 32 if (m_callback.isEmpty()) |
| 33 return false; |
| 34 |
| 34 if (!m_scriptState->contextIsValid()) | 35 if (!m_scriptState->contextIsValid()) |
| 35 return false; | 36 return false; |
| 36 | 37 |
| 37 ExecutionContext* context = m_scriptState->getExecutionContext(); | 38 ExecutionContext* context = m_scriptState->getExecutionContext(); |
| 38 DCHECK(context); | 39 DCHECK(context); |
| 39 if (context->isContextSuspended() || context->isContextDestroyed()) | 40 if (context->isContextSuspended() || context->isContextDestroyed()) |
| 40 return false; | 41 return false; |
| 41 | 42 |
| 42 if (m_callback.isEmpty()) | |
| 43 return false; | |
| 44 | |
| 45 // TODO(bashi): Make sure that using DummyExceptionStateForTesting is OK. | 43 // TODO(bashi): Make sure that using DummyExceptionStateForTesting is OK. |
| 46 // crbug.com/653769 | 44 // crbug.com/653769 |
| 47 DummyExceptionStateForTesting exceptionState; | 45 DummyExceptionStateForTesting exceptionState; |
| 48 ScriptState::Scope scope(m_scriptState.get()); | 46 ScriptState::Scope scope(m_scriptState.get()); |
| 47 v8::Isolate* isolate = m_scriptState->isolate(); |
| 48 |
| 49 v8::Local<v8::Value> thisValue = ToV8( |
| 50 scriptWrappable, |
| 51 m_scriptState->context()->Global(), |
| 52 isolate); |
| 49 | 53 |
| 50 {% for argument in arguments %} | 54 {% for argument in arguments %} |
| 51 v8::Local<v8::Value> {{argument.argument_name}} = {{argument.cpp_value_to_v8_v
alue}}; | 55 v8::Local<v8::Value> {{argument.argument_name}} = {{argument.cpp_value_to_v8_v
alue}}; |
| 52 {% endfor %} | 56 {% endfor %} |
| 53 | |
| 54 v8::Local<v8::Value> thisValue = ToV8(scriptWrappable, m_scriptState->context(
)->Global(), m_scriptState->isolate()); | |
| 55 | |
| 56 {% if arguments %} | 57 {% if arguments %} |
| 57 v8::Local<v8::Value> argv[] = { {{arguments | join(', ', 'argument_name')}} }; | 58 v8::Local<v8::Value> argv[] = { {{arguments | join(', ', 'argument_name')}} }; |
| 58 {% else %} | 59 {% else %} |
| 59 {# Empty array initializers are illegal, and don\'t compile in MSVC. #} | 60 {# Empty array initializers are illegal, and don\'t compile in MSVC. #} |
| 60 v8::Local<v8::Value> *argv = nullptr; | 61 v8::Local<v8::Value> *argv = nullptr; |
| 61 {% endif %} | 62 {% endif %} |
| 63 v8::TryCatch exceptionCatcher(isolate); |
| 64 exceptionCatcher.SetVerbose(true); |
| 62 | 65 |
| 63 v8::Local<v8::Value> v8ReturnValue; | 66 v8::Local<v8::Value> v8ReturnValue; |
| 64 v8::TryCatch exceptionCatcher(m_scriptState->isolate()); | 67 if (!V8ScriptRunner::callFunction(m_callback.newLocal(isolate), |
| 65 exceptionCatcher.SetVerbose(true); | 68 context, |
| 69 thisValue, |
| 70 {{arguments | length}}, |
| 71 argv, |
| 72 isolate).ToLocal(&v8ReturnValue)) { |
| 73 return false; |
| 74 } |
| 66 | 75 |
| 67 if (V8ScriptRunner::callFunction(m_callback.newLocal(m_scriptState->isolate())
, m_scriptState->getExecutionContext(), thisValue, {{arguments | length}}, argv,
m_scriptState->isolate()).ToLocal(&v8ReturnValue)) { | 76 {% if return_value %} |
| 68 {% if return_value %} | 77 {{v8_value_to_local_cpp_value(return_value) | indent(2)}} |
| 69 {{v8_value_to_local_cpp_value(return_value) | indent(8)}} | 78 returnValue = cppValue; |
| 70 returnValue = cppValue; | 79 {% endif %} |
| 71 {% endif %} | 80 return true; |
| 72 return true; | |
| 73 } | |
| 74 return false; | |
| 75 } | 81 } |
| 76 | 82 |
| 77 {{cpp_class}}* NativeValueTraits<{{cpp_class}}>::nativeValue(v8::Isolate* isolat
e, v8::Local<v8::Value> value, ExceptionState& exceptionState) { | 83 {{cpp_class}}* NativeValueTraits<{{cpp_class}}>::nativeValue(v8::Isolate* isolat
e, v8::Local<v8::Value> value, ExceptionState& exceptionState) { |
| 78 return {{cpp_class}}::create(ScriptState::current(isolate), value); | 84 return {{cpp_class}}::create(ScriptState::current(isolate), value); |
| 79 } | 85 } |
| 80 | 86 |
| 81 } // namespace blink | 87 } // namespace blink |
| 82 | 88 |
| 83 {% endfilter %}{# format_blink_cpp_source_code #} | 89 {% endfilter %}{# format_blink_cpp_source_code #} |
| OLD | NEW |